SOCLIB_CC := soclib-cc
RM        := rm -rf
CP        := cp -f
MAKE      := make
TARGET    := simul.x

all: $(TARGET) preloader tags

# create simulator

SRCS := top.cpp
SRCS += tsar_iob_cluster/caba/source/src/tsar_iob_cluster.cpp
INCS := tsar_iob_cluster/caba/source/include/tsar_iob_cluster.h
INCS += top.desc
INCS += soclib.conf

$(TARGET): $(SRCS) $(INCS)
	$(SOCLIB_CC) -P -p top.desc -I. -o simul.x

# create preloader

MESHSIZE       := 4c1p
PRELOADER_CONF := $(PWD)/conf/preloader_$(MESHSIZE)
PRELOADER_PATH := $(TSARPATH)/trunk/softs/tsar_boot
PRELOADER_ARGS := "PLATFORM_DIR=$(PRELOADER_CONF) USE_DT=0 SOCLIB=1"
PRELOADER_NAME ?= soft/soft.elf

$(PRELOADER_NAME):
	$(MAKE) -C $(PRELOADER_PATH) "$(PRELOADER_ARGS)"
	$(CP)   $(PRELOADER_PATH)/preloader.elf $(PRELOADER_NAME)

preloader: $(PRELOADER_NAME)

# run simulator

SIMULATOR_ARGS  = -SOFT $(PRELOADER_NAME)
SIMULATOR_ARGS += -DISK soft/disk.dmg
SIMULATOR_ARGS += -XSIZE 2 -YSIZE 2 -NPROCS 1

run: $(TARGET) $(PRELOADER_NAME)
	./$< $(SIMULATOR_ARGS)

# create tags file with ctags

tags:
	$(SOCLIB_CC) -p top.desc --tags --tags-type=ctags --tags-output=$@

# clean targets

clean:
	$(SOCLIB_CC) -x -p top.desc -I.
	$(RM) *.o *.x tty* term*

clean-soft:
	$(RM) $(PRELOADER_NAME)

clean-tags:
	$(RM) tags

distclean: clean clean-soft clean-tags

clean-preloader: clean-soft
	$(MAKE) -C $(PRELOADER_PATH) clean


.PHONY: tags preloader clean clean-soft clean-preloader

