## these need to be set before including

ifndef TOP
	$(error TOP is not set)
endif
ifndef HDL_SOURCE
	$(error HDL_SOURCE is not set)
endif

SIMARGS= -g2012 -Wall -Wno-timescale
SIM=iverilog


SIM_PATH=$(abspath sim)
SIMFILE=$(SIM_PATH)/$(TOP).vvp
WAVEFILE=$(SIM_PATH)/$(TOP).vcd
WAVESAVEFILE=$(TOP).sav


.PHONY: all
all: $(WAVEFILE)


$(SIMFILE):  $(abspath $(HDL_SOURCE))
	@mkdir -p $(SIM_PATH);
	cd $(SIM_PATH) && \
	$(SIM) $(SIMARGS) -o $@ -s $(TOP) \
	-D'DUMP_WAVE_FILE="$(WAVEFILE)"' \
	$(CUSTOMSIMARGS) $^ 


.PHONY: view
view: $(WAVEFILE)
	cd $(SIM_PATH) && \
	gtkwave $^ -a $(WAVESAVEFILE) &


%.vcd: %.vvp
	cd $(SIM_PATH) && \
	vvp $<

					
.PHONY: clean
clean:
	rm -rf sim

# keep intermediate files
.SECONDARY: