ARGS           ?=
SOCLIB_CC      := soclib-cc
RM             := rm -rf
CP             := cp -f
MAKE           := make
CONFIG_DIR     := $(PWD)/conf

TARGET         := simul.x

all: $(TARGET) 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 += $(CONFIG_DIR)/hard_config.h
INCS += top.desc
INCS += soclib.conf

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

# create preloader

PRELOADER_PATH := $(TSARPATH)/trunk/softs/tsar_boot
PRELOADER_ARGS := "PLATFORM_DIR=$(CONFIG_DIR) 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

SOFT           ?= $(PRELOADER_NAME)
DISK           ?= /dev/null

SIMULATOR_ARGS  = -SOFT $(SOFT)
SIMULATOR_ARGS += -DISK $(DISK)
SIMULATOR_ARGS += $(ARGS)

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

# create tags file with cscope
tags: cscope.out

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

# clean targets

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

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

clean-tags:
	$(RM) tags

distclean: clean clean-soft clean-tags

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


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

