"""pytest wrapper: build each RTL module with Icarus Verilog and run its cocotb tests. Run: /Volumes/M2-Dev/conda/envs/toptech/bin/python -m pytest -q The simulator build goes into a temporary directory, never into this folder. """ from __future__ import annotations import os import sys import tempfile from pathlib import Path from cocotb_tools.check_results import get_results from cocotb_tools.runner import get_runner HERE = Path(__file__).resolve().parent if str(HERE) not in sys.path: sys.path.insert(0, str(HERE)) # the simulator imports cocotb_*.py and int8_layer.py from here def run_cocotb(toplevel: str, sources: list[str], module: str, parameters: dict | None = None) -> None: with tempfile.TemporaryDirectory(prefix=f"cocotb_{toplevel}_") as tmp: runner = get_runner("icarus") runner.build( sources=[HERE / s for s in sources], hdl_toplevel=toplevel, parameters=parameters or {}, build_dir=tmp, always=True, ) results = runner.test( hdl_toplevel=toplevel, test_module=module, build_dir=tmp, test_dir=tmp, extra_env={"PYTHONDONTWRITEBYTECODE": "1", "PYTHONPATH": os.pathsep.join(sys.path)}, ) n_tests, n_failed = get_results(Path(results)) assert n_tests > 0, "no cocotb tests ran" assert n_failed == 0, f"{n_failed} of {n_tests} cocotb tests failed" def test_systolic_array() -> None: run_cocotb("systolic_array", ["systolic_array.sv"], "cocotb_systolic", {"N": 4}) def test_requant() -> None: run_cocotb("requant", ["requant.sv"], "cocotb_requant")