#!/usr/bin/env python3 """Run the independent four-baud production-endpoint wire regression.""" from __future__ import annotations import argparse import hashlib import json import shutil import subprocess import time from pathlib import Path def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--repo", type=Path, required=True) parser.add_argument("--tb", type=Path) parser.add_argument("--out", type=Path, required=True) args = parser.parse_args() repo, out = args.repo.resolve(), args.out.resolve() tb = args.tb or Path(__file__).with_name("host_uart_endpoint_nominal_tb.v") if not tb.exists() and args.tb is None: tb = repo / "tests/sim/host_uart_endpoint_nominal_tb.v" tb = tb.resolve() sources = [tb] + [repo / name for name in ( "src/transport/host_uart_endpoint.v", "src/common/uart_tx_byte.v", "src/common/uart_rx_byte.v", "src/common/frame_tx.v", "src/common/frame_rx.v", "src/common/crc16_serial.v", )] missing = [str(path) for path in sources if not path.is_file()] if missing: parser.error("missing source files: " + ", ".join(missing)) for tool in ("iverilog", "vvp"): if shutil.which(tool) is None: parser.error(f"required executable is unavailable: {tool}") out.mkdir(parents=True, exist_ok=True) receipt = { "test": "host_uart_endpoint_nominal_tb", "rates": [921600, 460800, 230400, 115200], "scope": "production endpoint wire behavior; no analog RS232 or physical hardware claim", "source_sha256": {str(path): hashlib.sha256(path.read_bytes()).hexdigest() for path in sources}, "status": "NOT_RUN", "commands": [], } commands = [ ["iverilog", "-g2012", "-s", "host_uart_endpoint_nominal_tb", "-o", str(out / "uart_nominal.vvp"), *map(str, sources)], ["vvp", str(out / "uart_nominal.vvp")], ] success = True for name, command in zip(("compile", "simulate"), commands): started = time.monotonic() try: result = subprocess.run(command, cwd=repo, text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=120, check=False) text, code = result.stdout, result.returncode except subprocess.TimeoutExpired as exc: data = exc.stdout or "" text = data.decode(errors="replace") if isinstance(data, bytes) else data text += "\nTIMEOUT after 120 seconds\n" code = 124 log = out / f"{name}.log" log.write_text(text, encoding="utf-8") print(text, end="" if text.endswith("\n") else "\n") receipt["commands"].append({"argv": command, "returncode": code, "seconds": round(time.monotonic() - started, 3), "log": str(log)}) if code != 0: success = False break if name == "simulate": success = all(f"PASS nominal UART baud={rate} " in text for rate in receipt["rates"]) success = success and "PASS host_uart_endpoint_nominal_tb:" in text receipt["status"] = "PASS" if success else "FAIL" (out / "receipt.json").write_text(json.dumps(receipt, indent=2) + "\n", encoding="utf-8") print(f"{receipt['status']}: {out / 'receipt.json'}") return 0 if success else 1 if __name__ == "__main__": raise SystemExit(main())