run_uart_nominal.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python3
  2. """Run the independent four-baud production-endpoint wire regression."""
  3. from __future__ import annotations
  4. import argparse
  5. import hashlib
  6. import json
  7. import shutil
  8. import subprocess
  9. import time
  10. from pathlib import Path
  11. def main() -> int:
  12. parser = argparse.ArgumentParser(description=__doc__)
  13. parser.add_argument("--repo", type=Path, required=True)
  14. parser.add_argument("--tb", type=Path)
  15. parser.add_argument("--out", type=Path, required=True)
  16. args = parser.parse_args()
  17. repo, out = args.repo.resolve(), args.out.resolve()
  18. tb = args.tb or Path(__file__).with_name("host_uart_endpoint_nominal_tb.v")
  19. if not tb.exists() and args.tb is None:
  20. tb = repo / "tests/sim/host_uart_endpoint_nominal_tb.v"
  21. tb = tb.resolve()
  22. sources = [tb] + [repo / name for name in (
  23. "src/transport/host_uart_endpoint.v",
  24. "src/common/uart_tx_byte.v", "src/common/uart_rx_byte.v",
  25. "src/common/frame_tx.v", "src/common/frame_rx.v",
  26. "src/common/crc16_serial.v",
  27. )]
  28. missing = [str(path) for path in sources if not path.is_file()]
  29. if missing:
  30. parser.error("missing source files: " + ", ".join(missing))
  31. for tool in ("iverilog", "vvp"):
  32. if shutil.which(tool) is None:
  33. parser.error(f"required executable is unavailable: {tool}")
  34. out.mkdir(parents=True, exist_ok=True)
  35. receipt = {
  36. "test": "host_uart_endpoint_nominal_tb",
  37. "rates": [921600, 460800, 230400, 115200],
  38. "scope": "production endpoint wire behavior; no analog RS232 or physical hardware claim",
  39. "source_sha256": {str(path): hashlib.sha256(path.read_bytes()).hexdigest() for path in sources},
  40. "status": "NOT_RUN", "commands": [],
  41. }
  42. commands = [
  43. ["iverilog", "-g2012", "-s", "host_uart_endpoint_nominal_tb", "-o", str(out / "uart_nominal.vvp"), *map(str, sources)],
  44. ["vvp", str(out / "uart_nominal.vvp")],
  45. ]
  46. success = True
  47. for name, command in zip(("compile", "simulate"), commands):
  48. started = time.monotonic()
  49. try:
  50. result = subprocess.run(command, cwd=repo, text=True, stdout=subprocess.PIPE,
  51. stderr=subprocess.STDOUT, timeout=120, check=False)
  52. text, code = result.stdout, result.returncode
  53. except subprocess.TimeoutExpired as exc:
  54. data = exc.stdout or ""
  55. text = data.decode(errors="replace") if isinstance(data, bytes) else data
  56. text += "\nTIMEOUT after 120 seconds\n"
  57. code = 124
  58. log = out / f"{name}.log"
  59. log.write_text(text, encoding="utf-8")
  60. print(text, end="" if text.endswith("\n") else "\n")
  61. receipt["commands"].append({"argv": command, "returncode": code,
  62. "seconds": round(time.monotonic() - started, 3),
  63. "log": str(log)})
  64. if code != 0:
  65. success = False
  66. break
  67. if name == "simulate":
  68. success = all(f"PASS nominal UART baud={rate} " in text for rate in receipt["rates"])
  69. success = success and "PASS host_uart_endpoint_nominal_tb:" in text
  70. receipt["status"] = "PASS" if success else "FAIL"
  71. (out / "receipt.json").write_text(json.dumps(receipt, indent=2) + "\n", encoding="utf-8")
  72. print(f"{receipt['status']}: {out / 'receipt.json'}")
  73. return 0 if success else 1
  74. if __name__ == "__main__":
  75. raise SystemExit(main())