SHELL := /bin/bash
IVERILOG ?= iverilog
VVP      ?= vvp
YOSYS    ?= yosys
VERILATOR ?= verilator
RTL := rtl/mini_controller.sv
TB  := tb/tb_mini_controller.sv
BUILD := build

.PHONY: help sim lint synth all clean check-tools
.DEFAULT_GOAL := all
help:
	@echo "Targets: sim lint synth all check-tools clean"

all: lint sim synth

$(BUILD):
	mkdir -p $(BUILD)

sim: | $(BUILD)
	$(IVERILOG) -g2012 -Wall -s tb_mini_controller -o $(BUILD)/sim.vvp $(RTL) $(TB)
	set -o pipefail; cd $(BUILD) && $(VVP) sim.vvp | tee sim.log
	@grep -c "^PASS:" $(BUILD)/sim.log | grep -qx 1

lint:
	$(VERILATOR) --lint-only -Wall --top-module mini_controller $(RTL)

synth: | $(BUILD)
	$(YOSYS) -Q -T -p 'read_verilog -sv $(RTL); hierarchy -check -top mini_controller; proc; opt; check -assert; stat' > $(BUILD)/synth.log

check-tools:
	@bash scripts/check_tools.sh

clean:
	rm -rf $(BUILD)
