refactor(hw/sim): extract common cocotb make infrastructure into shared mk/ directory

- Split monolithic hw/sim/Makefile into modular include files (mk/cocotb-common.mk, mk/rtl-sources.mk)
- Add per-module test Makefiles (cam_core_banked, cam_read_noise, cam_write_noise, match_engine_pipeline, perf, top)
- Add missing simulation tools (yosys, graphviz, xdot) to devenv.nix
- Fix path handling in sweep_noise.py and test modules to be HASH_BITS-aware
This commit is contained in:
2026-05-16 19:23:49 +08:00
parent 2e0e36eea5
commit ca167e79c6
27 changed files with 241 additions and 97 deletions

View File

@@ -1,90 +1,41 @@
SIM ?= verilator
TOPLEVEL_LANG ?= verilog
TOPLEVEL ?= cam_top
MODULE_TESTS := cam_core_banked match_engine_pipeline cam_write_noise cam_read_noise
COCOTB_TEST_MODULES ?= tests.test_cam_basic
.PHONY: help test-all test-top test-modules test-module test-model test-perf clean $(MODULE_TESTS:%=test-module-%)
NUM_ROWS ?= 4096
HASH_BITS ?= 512
LANES ?= 8
help:
@echo "Available hw/sim targets:"
@echo " make test-model"
@echo " make test-top"
@echo " make test-module MODULE=cam_core_banked"
@echo " make test-modules"
@echo " make test-perf"
@echo " make test-all"
@echo " make clean"
EXTRA_ARGS += +define+NUM_ROWS=$(NUM_ROWS) +define+HASH_BITS=$(HASH_BITS) +define+LANES=$(LANES)
test-all: test-model test-top test-modules
COMPILE_ARGS += -Wall -Wno-fatal
COMPILE_ARGS += -I$(PWD)/../rtl -I$(PWD)/../rtl/core -I$(PWD)/../rtl/noise
COMPILE_ARGS += +define+SIM_DEBUG
COMPILE_ARGS += $(EXTRA_DEFINES)
test-top:
$(MAKE) -C tests/top
# Noise parameters — only passed when explicitly set (non-empty).
# For cam_noisy/cam_top tests, pass WRITE_NOISE_*=... READ_NOISE_*=...
# For individual module tests, leave them unset to skip.
WRITE_NOISE_EN ?= $(NOISE_EN)
WRITE_NOISE_RATE_NUM ?= $(NOISE_RATE_NUM)
WRITE_NOISE_RATE_DEN ?= $(NOISE_RATE_DEN)
WRITE_NOISE_BITS ?= $(NOISE_BITS)
READ_NOISE_EN ?=
READ_NOISE_RATE_NUM ?=
READ_NOISE_RATE_DEN ?=
READ_NOISE_BITS ?=
test-modules: $(MODULE_TESTS:%=test-module-%)
# Perf-test compatibility override: when running cam_perf, force deterministic
# noise parameters so noise_mask_grouped.sv elaborates correctly for perf
# sweep HASH_BITS values (64, 128, 192, 256, 320, 384, 448, 512 — all
# divisible by 64 such that HASH_BITS / NOISE_BITS == 64 in the RTL).
# This is a testbench parameter, not a hardware performance/resource claim.
ifeq ($(COCOTB_TEST_MODULES),tests.test_cam_perf)
WRITE_NOISE_EN := 0
READ_NOISE_EN := 0
WRITE_NOISE_BITS := $(shell echo $$(( $(HASH_BITS) / 64 )))
READ_NOISE_BITS := $(shell echo $$(( $(HASH_BITS) / 64 )))
endif
test-module:
@test -n "$(MODULE)" || (echo "Usage: make test-module MODULE=<name>"; exit 2)
$(MAKE) -C tests/modules/$(MODULE)
ifneq ($(strip $(WRITE_NOISE_EN)),)
COMPILE_ARGS += -GWRITE_NOISE_EN=$(WRITE_NOISE_EN)
endif
ifneq ($(strip $(WRITE_NOISE_RATE_NUM)),)
COMPILE_ARGS += -GWRITE_NOISE_RATE_NUM=$(WRITE_NOISE_RATE_NUM)
endif
ifneq ($(strip $(WRITE_NOISE_RATE_DEN)),)
COMPILE_ARGS += -GWRITE_NOISE_RATE_DEN=$(WRITE_NOISE_RATE_DEN)
endif
ifneq ($(strip $(WRITE_NOISE_BITS)),)
COMPILE_ARGS += -GWRITE_NOISE_BITS=$(WRITE_NOISE_BITS)
endif
ifneq ($(strip $(READ_NOISE_EN)),)
COMPILE_ARGS += -GREAD_NOISE_EN=$(READ_NOISE_EN)
endif
ifneq ($(strip $(READ_NOISE_RATE_NUM)),)
COMPILE_ARGS += -GREAD_NOISE_RATE_NUM=$(READ_NOISE_RATE_NUM)
endif
ifneq ($(strip $(READ_NOISE_RATE_DEN)),)
COMPILE_ARGS += -GREAD_NOISE_RATE_DEN=$(READ_NOISE_RATE_DEN)
endif
ifneq ($(strip $(READ_NOISE_BITS)),)
COMPILE_ARGS += -GREAD_NOISE_BITS=$(READ_NOISE_BITS)
endif
$(MODULE_TESTS:%=test-module-%):
$(MAKE) -C tests/modules/$(@:test-module-%=%)
# Cleaner terminal output
export QUIET ?= 1
export VERBOSE ?= 0
export COCOTB_LOG_LEVEL ?= INFO
export PYTHONWARNINGS ?= ignore::pytest.PytestDeprecationWarning
test-model:
uv run pytest tests/model -q
# Optional temporary suppression
SUPPRESS_VERILATOR_WARNINGS ?= 0
ifeq ($(SUPPRESS_VERILATOR_WARNINGS),1)
COMPILE_ARGS += -Wno-WIDTHEXPAND
COMPILE_ARGS += -Wno-UNOPTFLAT
endif
test-perf:
$(MAKE) -C tests/perf
VERILOG_SOURCES += $(PWD)/../rtl/random/random128.sv
VERILOG_SOURCES += $(PWD)/../rtl/noise/noise_mask_grouped.sv
VERILOG_SOURCES += $(PWD)/../rtl/core/cam_core_banked.sv
VERILOG_SOURCES += $(PWD)/../rtl/noise/cam_write_noise.sv
VERILOG_SOURCES += $(PWD)/../rtl/noise/cam_read_noise.sv
VERILOG_SOURCES += $(PWD)/../rtl/core/popcount_pipeline.sv
VERILOG_SOURCES += $(PWD)/../rtl/core/match_engine_pipeline.sv
VERILOG_SOURCES += $(PWD)/../rtl/cam_noisy.sv
VERILOG_SOURCES += $(PWD)/../rtl/cam_top.sv
include $(shell uv run cocotb-config --makefiles)/Makefile.sim
clean:
$(MAKE) -C tests/top clean
@for module in $(MODULE_TESTS); do \
$(MAKE) -C tests/modules/$$module clean || exit $$?; \
done
$(MAKE) -C tests/perf clean
rm -rf .pytest_cache tests/model/.pytest_cache