#!/usr/bin/env python3 """Run focused single-product full-top cocotb test suites.""" from __future__ import annotations import argparse import json import os from pathlib import Path import tempfile import xml.etree.ElementTree as ET try: from cocotb_tools.runner import get_runner except ModuleNotFoundError: # cocotb 1.9 compatibility in the pinned image from cocotb.runner import get_runner ROOT = Path(__file__).resolve().parents[2] TEST_DIR = Path(__file__).resolve().parent CLOSURE = ROOT / "products/6400-front-rear/source-closure.json" WRAPPER = TEST_DIR / "prod2u_6400_v1_cocotb_top.v" def rtl_sources() -> list[Path]: payload = json.loads(CLOSURE.read_text(encoding="utf-8")) return [ROOT / item for item in payload["sources"] if item.endswith(".v")] def run_suite(name: str, build_dir: Path) -> None: os.environ["HYPOTHESIS_STORAGE_DIRECTORY"] = str(build_dir / "hypothesis") runner = get_runner("icarus") runner.build( sources=[*rtl_sources(), WRAPPER], hdl_toplevel="prod2u_6400_v1_cocotb_top", build_dir=build_dir, build_args=["-g2012", "-I", str(ROOT)], always=True, ) results_path = build_dir / "results.xml" result = Path(runner.test( hdl_toplevel="prod2u_6400_v1_cocotb_top", test_module=f"test_6400_v1_{name}_integration", test_dir=TEST_DIR, build_dir=build_dir, results_xml=str(results_path), )) root = ET.parse(result).getroot() cases = root.findall(".//testcase") failures = root.findall(".//failure") + root.findall(".//error") skipped = root.findall(".//skipped") if not cases or failures or skipped: raise RuntimeError( f"cocotb {name} profile incomplete: " f"cases={len(cases)} failures={len(failures)} skipped={len(skipped)} " f"results={result}" ) def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser() parser.add_argument( "profiles", nargs="*", metavar="PROFILE", ) args = parser.parse_args(argv) supported = {"control", "host_protocol", "pwm_boot"} invalid = [profile for profile in args.profiles if profile not in supported] if invalid: parser.error(f"unsupported profile: {invalid[0]}") profiles = args.profiles or ("host_protocol",) with tempfile.TemporaryDirectory(prefix="prod2u-6400-cocotb-") as directory: root = Path(directory) for profile in profiles: run_suite(profile, root / profile) print(f"PASS: 6400-front-rear cocotb suites: {', '.join(profiles)}") return 0 if __name__ == "__main__": raise SystemExit(main())