# ============================================================================
# Terminal-only (batch) Vivado/xsim flow for the Calc1 UVM environment.
#
#   make            # compile + elaborate + run the default test (test_sanity)
#   make TEST=test_corner
#   make run TEST=test_random VERB=UVM_HIGH
#   make WAVES=1    # also dump calc.vcd
#   make clean
#
# Override the tool location if needed:  make VIVADO=/path/to/Vivado/2022.2
# ============================================================================

SHELL    := /bin/bash
VIVADO   ?= /tools/Xilinx/Vivado/2022.2
SETTINGS := $(VIVADO)/settings64.sh

TEST     ?= test_sanity
VERB     ?= UVM_LOW
SEED     ?= random
SNAP     := calc_sim
TOP      := calc_verif_top

# run xsim until UVM calls $finish; the driver/monitor have response timeouts
# so a non-responding DUT is reported, never hung on
RUN      := -R

# optional waveform dump
ifeq ($(WAVES),1)
  WAVEARG := -testplusarg WAVES
else
  WAVEARG :=
endif

DUT_SRCS := $(wildcard dut/*.v)
INCDIRS  := -i ./verif -i ./verif/Agent -i ./verif/Sequences -i ./verif/Configurations
TB_SRCS  := ./verif/Configurations/configurations_pkg.sv \
            ./verif/Agent/calc_agent_pkg.sv \
            ./verif/Sequences/calc_seq_pkg.sv \
            ./verif/calc_test_pkg.sv \
            ./verif/calc_if.sv \
            ./verif/calc_verif_top.sv

.PHONY: all run comp elab sim clean

all: run

# ---- compile ---------------------------------------------------------------
comp:
	@bash -c 'source $(SETTINGS) && \
	  xvlog --nolog $(DUT_SRCS) && \
	  xvlog --nolog --sv -L uvm $(INCDIRS) $(TB_SRCS)'

# ---- elaborate -------------------------------------------------------------
elab: comp
	@bash -c 'source $(SETTINGS) && \
	  xelab --nolog -L uvm -timescale 1ns/10ps $(TOP) -s $(SNAP)'

# ---- simulate --------------------------------------------------------------
sim:
	@bash -c 'source $(SETTINGS) && \
	  xsim --nolog $(SNAP) $(RUN) \
	    -testplusarg UVM_TESTNAME=$(TEST) \
	    -testplusarg UVM_VERBOSITY=$(VERB) \
	    $(WAVEARG) -sv_seed $(SEED)'

run: elab sim

clean:
	@rm -rf xsim.dir xsim.covdb .Xil *.jou *.log *.pb *.wdb *.vcd xelab.* xvlog.* \
	        webtalk* usage_statistics_webtalk.* 2>/dev/null; echo "cleaned"
