"""pytest wrapper: build crc.sv as CRC-8, CRC-16 and CRC-32 and run cocotb_crc.py on each.""" from __future__ import annotations import sys import tempfile from pathlib import Path import pytest from cocotb_tools.check_results import get_results from cocotb_tools.runner import get_runner HERE = Path(__file__).resolve().parent BUILDS = { "crc8_smbus": {"W": 8, "POLY": "8'h07", "INIT": "8'h00", "REFIN": "1'b0", "REFOUT": "1'b0", "XOROUT": "8'h00"}, "crc16_ibm3740": {}, # module defaults "crc32_isohdlc": {"W": 32, "POLY": "32'h04C11DB7", "INIT": "32'hFFFFFFFF", "REFIN": "1'b1", "REFOUT": "1'b1", "XOROUT": "32'hFFFFFFFF"}, } @pytest.mark.parametrize("name", list(BUILDS)) def test_crc(name: str) -> None: if str(HERE) not in sys.path: sys.path.insert(0, str(HERE)) with tempfile.TemporaryDirectory() as tmp: build = Path(tmp) runner = get_runner("icarus") runner.build(sources=[HERE / "crc.sv"], hdl_toplevel="crc", build_dir=build, parameters=BUILDS[name], timescale=("1ns", "1ps")) xml = runner.test(test_module="cocotb_crc", hdl_toplevel="crc", build_dir=build, test_dir=build, results_xml=str(build / "results.xml")) n_tests, n_failed = get_results(Path(xml)) assert n_tests == 2 and n_failed == 0