#########################################################################################
# set default shell for make
#########################################################################################
SHELL=/bin/bash

#########################################################################################
# variables to get all the scala sources
#########################################################################################
lookup_scala_srcs = $(shell find $(1)/ -iname "*.scala" 2> /dev/null)
BASE_PACKAGES = rocket-chip boom
EXTRA_PACKAGES ?=
ALL_PACKAGES = $(BASE_PACKAGES) $(EXTRA_PACKAGES)
SCALA_SOURCES = $(foreach pkg,$(ALL_PACKAGES),$(call lookup_scala_srcs,$(base_dir)/$(pkg)/src/main/scala))

#########################################################################################
# create firrtl jar
#########################################################################################
$(FIRRTL_JAR): $(call lookup_scala_srcs, $(ROCKETCHIP_DIR)/firrtl/src/main/scala)
	$(MAKE) -C $(ROCKETCHIP_DIR)/firrtl SBT="$(SBT)" root_dir=$(ROCKETCHIP_DIR)/firrtl build-scala
	mkdir -p $(ROCKETCHIP_DIR)/lib
	cp -p $(FIRRTL_JAR) $(ROCKETCHIP_DIR)/lib

#########################################################################################
# create firrtl file
#########################################################################################
$(FIRRTL_FILE): $(SCALA_SOURCES) $(FIRRTL_JAR) $(bootrom_img)
	mkdir -p $(build_dir)
	cd $(base_dir) && $(SBT) "runMain $(PROJECT).Generator $(CHISEL_ARGS) $(build_dir) $(PROJECT) $(MODEL) $(CFG_PROJECT) $(CONFIG)"

#########################################################################################
# generate verilog from firrtl
#########################################################################################
# Compiling Verilog code may also generate a *.conf file.
# EXTRA arguments allows for options such as black-boxing of the SeqMems.
$(CONF_FILE): $(VERILOG_FILE) ;
$(VERILOG_FILE): $(FIRRTL_FILE) $(FIRRTL_JAR) $(ANNO_FILE)
	$(FIRRTL) -i $< -o $@ -X verilog -faf $(ANNO_FILE) $(EXTRA_FIRRTL_ARGS)

#########################################################################################
# helper rule to build verilog
#########################################################################################
.PHONY: verilog
verilog: $(sim_vsrcs)

#########################################################################################
# run assembly tests and benchmarks
#########################################################################################
# Important Makefile fragment .d is auto-generated by compiling Chisel code -- configuration-specific.
ifneq ($(filter run% %.run %.out %.vpd %.vcd %.fsdb %.verdi,$(MAKECMDGOALS)),)
include $(build_dir)/$(long_name).d
endif

.PRECIOUS: $(output_dir)/%.vpd $(output_dir)/%.fsdb

$(output_dir)/%.run: $(output_dir)/% $(sim)
	cd $(sim_dir) && $(sim) +max-cycles=$(timeout_cycles) $< 2> /dev/null 2> $@ && [ $$PIPESTATUS -eq 0 ]

$(output_dir)/%.out: $(output_dir)/% $(sim)
	cd $(sim_dir) && $(sim) $(PERMISSIVE) +max-cycles=$(timeout_cycles) $(SIMFLAGS) +verbose $(PERMISSIVEOFF) $< $(disasm) $@ && [ $$PIPESTATUS -eq 0 ]

$(output_dir)/%.vpd: $(output_dir)/% $(sim_debug)
	cd $(sim_dir) && $(sim_debug) $(PERMISSIVE) +max-cycles=$(timeout_cycles) $(SIMFLAGS) +verbose $(DUMP_FLAG)$@ $(PERMISSIVEOFF) $< $(disasm) $(patsubst %.vpd,%.out,$@) && [ $$PIPESTATUS -eq 0 ]

$(output_dir)/%.fsdb: $(output_dir)/% $(sim_fsdb_debug)
	cd $(sim_dir) && $(sim_fsdb_debug) $(PERMISSIVE) +max-cycles=$(timeout_cycles) $(SIMFLAGS) +verbose $(DUMP_FSDB_FLAG)$@ $(PERMISSIVEOFF) $< $(disasm) $(patsubst %.fsdb,%.out,$@) && [ $$PIPESTATUS -eq 0 ]

$(output_dir)/%.verdi: $(output_dir)/%.fsdb
	verdi -ssf $< $(VCS_NONCC_OPTS)

run: run-asm-tests run-bmark-tests
run-debug: run-asm-tests-debug run-bmark-tests-debug
run-fast: run-asm-tests-fast run-bmark-tests-fast

.PHONY: run-asm-tests run-bmark-tests
.PHONY: run-asm-tests-debug run-bmark-tests-debug
.PHONY: run run-debug run-fast
.PHONY: $(output_dir)/%.verdi

