# Usage:   make STOP_TIME=10us
# Pere Palà, Feb 2025
#

# Top-Level Entity (Testbench)
TLE = full_adder_tb

# 1. Stop time can be specified as an input parameter
# 2. The testbench usually also constrains the stop time
#    Here we add a default STOP_TIME in case there is no
#    other constraint.
STOP_TIME ?= 10000ms 

# Other simulation parameters
GHDL_SIM_OPT = --stop-time=$(STOP_TIME)
FORMAT = ghw
EXTENSION = $(FORMAT)
GHDL_SIM_RES = --wave=$(TLE).$(EXTENSION)

# Tools
WAVEFORM_VIEWER = gtkwave

# File dependencies
VHDL_SOURCES := $(wildcard *.vhd)
GHDL_WORKDIR := work-obj93.cf

.PHONY: all compile elaborate run view clean debug

all: compile elaborate run view

compile: $(GHDL_WORKDIR)

$(GHDL_WORKDIR): $(VHDL_SOURCES)
	ghdl -a -Wall $(VHDL_SOURCES)

elaborate: compile
	ghdl -e $(TLE)

run: elaborate
	@echo "Running simulation with STOP_TIME=$(STOP_TIME)"
	ghdl -r $(TLE) $(GHDL_SIM_OPT) $(GHDL_SIM_RES)

view: run
	$(WAVEFORM_VIEWER) $(TLE).$(EXTENSION) &

clean:
	rm -f $(TLE).$(EXTENSION) $(GHDL_WORKDIR) *~

debug:
	@echo "STOP_TIME: $(STOP_TIME)"
	@echo "GHDL_SIM_OPT: $(GHDL_SIM_OPT)"
	@echo "GHDL_SIM_RES: $(GHDL_SIM_RES)"
