PYTHON ?= python3
SBT ?= sbt
GEMMINI_TEST_SUITES ?= gemmini.Lab4OSLoopMatmulExecuteUnitTest gemmini.Lab4MeshUnitTest
SHELL := /bin/bash
LAB4_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
LAB4_JAVA_HOME ?= $(shell dirname $$(dirname $$(readlink -f $$(command -v java))))
# Keep a Chipyard worktree beside this Makefile by default. Gemmini is the
# generators/gemmini submodule inside that worktree, matching Gemmini's README.
CHIPYARD_HOME ?= $(LAB4_DIR)/chipyard
GEMMINI_HOME ?= $(CHIPYARD_HOME)/generators/gemmini
CHIPYARD_ENV ?= $(CHIPYARD_HOME)/env.sh
GEMMINI_TESTS ?= $(GEMMINI_HOME)/software/gemmini-rocc-tests
BENCHMARK_CONFIG ?= Lab4GemminiRocketConfig
BENCHMARK_TIMEOUT_CYCLES ?= 50000000

BUILD_DIR := build
BENCHMARK_DIR := $(LAB4_DIR)benchmark
BENCHMARK_BUILD_DIR := $(LAB4_DIR)build/benchmark
BENCHMARK_BASELINE_ELF := $(BENCHMARK_BUILD_DIR)/lab4_benchmark-baseline-baremetal
BENCHMARK_TILED_ELF := $(BENCHMARK_BUILD_DIR)/lab4_benchmark-tiled-baremetal
BENCHMARK_BASELINE_LOG := $(BENCHMARK_BUILD_DIR)/baseline.log
BENCHMARK_TILED_LOG := $(BENCHMARK_BUILD_DIR)/tiled.log
SBT_CACHE_DIR ?= $(LAB4_DIR)$(BUILD_DIR)/sbt
SBT_ARGS := --sbt-dir $(SBT_CACHE_DIR)/dir \
	--sbt-boot $(SBT_CACHE_DIR)/boot \
	--sbt-cache $(SBT_CACHE_DIR)/cache \
	--ivy $(SBT_CACHE_DIR)/ivy
.DEFAULT_GOAL := scheduler-test
.PHONY: scheduler-test gemmini-mesh-test integration-check gemmini-install \
	gemmini-compile gemmini-test benchmark benchmark-baseline benchmark-tiled \
	benchmark-parse clean _benchmark-build

scheduler-test: GEMMINI_TEST_SUITES = gemmini.Lab4OSLoopMatmulExecuteUnitTest
scheduler-test: gemmini-test

gemmini-mesh-test: GEMMINI_TEST_SUITES = gemmini.Lab4MeshUnitTest
gemmini-mesh-test: gemmini-test

integration-check:
	$(PYTHON) scripts/verify_gemmini_overlay.py --gemmini "$(GEMMINI_HOME)"

# All hardware entry points share this setup; installed student edits are kept.
gemmini-install: integration-check
	@if grep -Fq 'new Lab4Mesh(' "$(GEMMINI_HOME)/src/main/scala/gemmini/MeshWithDelays.scala"; then \
		printf '%s\n' 'Lab4 Gemmini seams are already installed; keeping the current implementation.'; \
	else \
		$(PYTHON) scripts/apply_gemmini_overlay.py --gemmini "$(GEMMINI_HOME)" --install; \
	fi

gemmini-compile: gemmini-install
	@test -f "$(CHIPYARD_ENV)" || \
		{ printf '%s\n' 'Chipyard env.sh is missing. Set CHIPYARD_ENV to an initialized Chipyard environment.'; exit 1; }
	mkdir -p "$(SBT_CACHE_DIR)"
	cd "$(CHIPYARD_HOME)" && \
	source "$(CHIPYARD_ENV)" && \
	export JAVA_HOME="$(LAB4_JAVA_HOME)" && \
	export PATH="$(LAB4_JAVA_HOME)/bin:$$PATH" && \
		COURSIER_CACHE="$(SBT_CACHE_DIR)/coursier" \
		$(SBT) $(SBT_ARGS) "project gemmini" compile

gemmini-test: gemmini-install
	@test -f "$(CHIPYARD_ENV)" || \
		{ printf '%s\n' 'Chipyard env.sh is missing. Set CHIPYARD_ENV to an initialized Chipyard environment; standalone Gemmini is not enough for gemmini-test.'; exit 1; }
	@command -v sbt >/dev/null 2>&1 || \
		{ printf '%s\n' 'sbt is unavailable in this standalone environment; use a Gemmini/Chipyard environment to run the installed overlay unit test.'; exit 1; }
	@test -f "$(GEMMINI_HOME)/lab4-test/scala/gemmini/Lab4OSLoopMatmulExecuteUnitTest.scala" || \
		{ printf '%s\n' 'Lab4 overlay test is not installed. Run make gemmini-install in the selected Gemmini worktree first.'; exit 1; }
	mkdir -p "$(SBT_CACHE_DIR)"
	cd "$(CHIPYARD_HOME)" && \
	source "$(CHIPYARD_ENV)" && \
	export JAVA_HOME="$(LAB4_JAVA_HOME)" && \
	export PATH="$(LAB4_JAVA_HOME)/bin:$$PATH" && \
		COURSIER_CACHE="$(SBT_CACHE_DIR)/coursier" \
		$(SBT) $(SBT_ARGS) "project gemmini" \
		'set Test / unmanagedSourceDirectories := Seq(file("generators/gemmini/lab4-test/scala"))' \
		"testOnly $(GEMMINI_TEST_SUITES)"

# Part C system benchmark ---------------------------------------------------
# Processor-level simulation stays separate from the default scheduler test.
benchmark: benchmark-baseline benchmark-tiled

# Internal build pipeline, shared by both benchmark modes.
_benchmark-build: gemmini-install
	@test -f "$(CHIPYARD_ENV)" || \
		{ printf '%s\n' 'Chipyard env.sh is missing. Set CHIPYARD_ENV to an initialized Chipyard environment.'; exit 1; }
	@test -d "$(GEMMINI_TESTS)" || \
		{ printf '%s\n' 'Gemmini software tests are missing. Initialize the gemmini-rocc-tests submodule before running the benchmark.'; exit 1; }
	@test -f "$(GEMMINI_TESTS)/include/gemmini.h" && \
		test -f "$(GEMMINI_TESTS)/include/gemmini_testutils.h" || \
		{ printf '%s\n' 'Gemmini software headers are missing. Initialize the gemmini-rocc-tests submodule before running the benchmark.'; exit 1; }
	cd "$(CHIPYARD_HOME)" && \
	source "$(CHIPYARD_ENV)" && \
	export JAVA_HOME="$(LAB4_JAVA_HOME)" && \
	export PATH="$(LAB4_JAVA_HOME)/bin:$$PATH" && \
		$(MAKE) -C "$(CHIPYARD_HOME)/sims/verilator" CONFIG="$(BENCHMARK_CONFIG)" firrtl

	bash -c '. "$(CHIPYARD_ENV)" && $(MAKE) -C "$(BENCHMARK_DIR)" \
		LAB4_DIR="$(LAB4_DIR)" CHIPYARD_HOME="$(abspath $(CHIPYARD_HOME))" \
		GEMMINI_HOME="$(abspath $(GEMMINI_HOME))" \
		GEMMINI_TESTS="$(abspath $(GEMMINI_TESTS))" all'

	bash -c '. "$(CHIPYARD_ENV)" && $(MAKE) -C "$(CHIPYARD_HOME)/sims/verilator" \
		CONFIG="$(BENCHMARK_CONFIG)"'

benchmark-baseline: _benchmark-build
	@mkdir -p "$(BENCHMARK_BUILD_DIR)"
	@set -o pipefail; \
		bash -c '. "$(CHIPYARD_ENV)" && $(MAKE) -C "$(CHIPYARD_HOME)/sims/verilator" \
			CONFIG="$(BENCHMARK_CONFIG)" run-binary-fast \
			BINARY="$(BENCHMARK_BASELINE_ELF)" LOADMEM=1 \
			TIMEOUT_CYCLES="$(BENCHMARK_TIMEOUT_CYCLES)"' | \
		tee "$(BENCHMARK_BASELINE_LOG)"
	$(PYTHON) scripts/parse_benchmark.py --input "$(BENCHMARK_BASELINE_LOG)"

benchmark-tiled: _benchmark-build
	@mkdir -p "$(BENCHMARK_BUILD_DIR)"
	@set -o pipefail; \
		bash -c '. "$(CHIPYARD_ENV)" && $(MAKE) -C "$(CHIPYARD_HOME)/sims/verilator" \
			CONFIG="$(BENCHMARK_CONFIG)" run-binary-fast \
			BINARY="$(BENCHMARK_TILED_ELF)" LOADMEM=1 \
			TIMEOUT_CYCLES="$(BENCHMARK_TIMEOUT_CYCLES)"' | \
		tee "$(BENCHMARK_TILED_LOG)"
	$(PYTHON) scripts/parse_benchmark.py --input "$(BENCHMARK_TILED_LOG)"

benchmark-parse:
	@test -n "$(LOG)" || \
		{ printf '%s\n' 'Set LOG=/path/to/benchmark.log, or use make benchmark-baseline/benchmark-tiled.'; exit 1; }
	$(PYTHON) scripts/parse_benchmark.py --input "$(LOG)"

clean:
	$(RM) -r $(BUILD_DIR)
