"""pytest wrappers: build the video designs with Icarus Verilog and run the cocotb tests.""" from __future__ import annotations 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 def run(sources: list[str], toplevel: str, module: str, parameters: dict[str, object] | None = None) -> tuple[int, int]: if str(HERE) not in sys.path: sys.path.insert(0, str(HERE)) # the runner passes sys.path on as PYTHONPATH with tempfile.TemporaryDirectory() as tmp: build = Path(tmp) runner = get_runner("icarus") runner.build(sources=[HERE / s for s in sources], hdl_toplevel=toplevel, parameters=parameters or {}, build_dir=build, timescale=("1ns", "1ps")) xml = runner.test(test_module=module, hdl_toplevel=toplevel, build_dir=build, test_dir=build, results_xml=str(build / "results.xml")) return get_results(Path(xml)) def test_cocotb_tmds_encoder() -> None: n_tests, n_failed = run(["tmds_encoder.sv"], "tmds_encoder", "cocotb_tmds_encoder") assert n_tests == 1 and n_failed == 0 def test_cocotb_video_top() -> None: params = dict(H_ACTIVE=64, H_FP=4, H_SYNC=8, H_BP=12, V_ACTIVE=24, V_FP=2, V_SYNC=3, V_BP=4, H_POL=1, V_POL=0, GRID=8) n_tests, n_failed = run(["video_timing.sv", "test_pattern.sv", "video_top_tfp410.sv"], "video_top_tfp410", "cocotb_video_top", params) assert n_tests == 1 and n_failed == 0