test_top_step_gate_matches_formal.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env python3
  2. """Structure-equivalence: formal STEP gate must match real top pin gate."""
  3. from __future__ import annotations
  4. import re
  5. import unittest
  6. from pathlib import Path
  7. ROOT = Path(__file__).resolve().parents[2]
  8. TOP = ROOT / "src/top/prod2u_p4_normal_v3_top.v"
  9. FORMAL = ROOT / "tests/formal/runtime_limit_hard_stop_formal.v"
  10. def _normalize_gate_expr(expr: str) -> str:
  11. text = re.sub(r"\s+", "", expr)
  12. # Formal uses limit_*_sync[i]; top uses x/y/z_limit_*_sync1.
  13. text = text.replace("x_limit_ls_sync1", "limit_ls_sync[0]")
  14. text = text.replace("x_limit_hs_sync1", "limit_hs_sync[0]")
  15. text = text.replace("y_limit_ls_sync1", "limit_ls_sync[1]")
  16. text = text.replace("y_limit_hs_sync1", "limit_hs_sync[1]")
  17. text = text.replace("z_limit_ls_sync1", "limit_ls_sync[2]")
  18. text = text.replace("z_limit_hs_sync1", "limit_hs_sync[2]")
  19. text = text.replace("engine_step[0]", "engine_step")
  20. text = text.replace("engine_dir[0]", "engine_dir")
  21. text = text.replace("engine_step[1]", "engine_step")
  22. text = text.replace("engine_dir[1]", "engine_dir")
  23. text = text.replace("engine_step[2]", "engine_step")
  24. text = text.replace("engine_dir[2]", "engine_dir")
  25. text = text.replace("limit_ls_sync[0]", "LS")
  26. text = text.replace("limit_hs_sync[0]", "HS")
  27. text = text.replace("limit_ls_sync[1]", "LS")
  28. text = text.replace("limit_hs_sync[1]", "HS")
  29. text = text.replace("limit_ls_sync[2]", "LS")
  30. text = text.replace("limit_hs_sync[2]", "HS")
  31. return text
  32. def _extract_top_gates(text: str) -> dict[str, str]:
  33. pattern = re.compile(
  34. r"assign\s+([xyz])_step\s*=\s*([^;]+);",
  35. re.MULTILINE,
  36. )
  37. return {m.group(1): _normalize_gate_expr(m.group(2)) for m in pattern.finditer(text)}
  38. def _extract_formal_gates(text: str) -> dict[str, str]:
  39. pattern = re.compile(
  40. r"assign\s+gated_step\[(\d)\]\s*=\s*([^;]+);",
  41. re.MULTILINE,
  42. )
  43. axis = {"0": "x", "1": "y", "2": "z"}
  44. return {
  45. axis[m.group(1)]: _normalize_gate_expr(m.group(2))
  46. for m in pattern.finditer(text)
  47. }
  48. class TopStepGateMatchesFormalTests(unittest.TestCase):
  49. def test_formal_gate_matches_top_structure(self) -> None:
  50. top = TOP.read_text(encoding="utf-8")
  51. formal = FORMAL.read_text(encoding="utf-8")
  52. top_gates = _extract_top_gates(top)
  53. formal_gates = _extract_formal_gates(formal)
  54. self.assertEqual(set(top_gates), {"x", "y", "z"})
  55. self.assertEqual(set(formal_gates), {"x", "y", "z"})
  56. for axis in ("x", "y", "z"):
  57. self.assertEqual(
  58. top_gates[axis],
  59. formal_gates[axis],
  60. f"axis {axis}: top={top_gates[axis]!r} formal={formal_gates[axis]!r}",
  61. )
  62. # Canonical shape: engine_step & ~(engine_dir ? LS : HS)
  63. for axis, expr in top_gates.items():
  64. self.assertIn("engine_step&~(engine_dir?LS:HS)", expr, axis)
  65. if __name__ == "__main__":
  66. unittest.main()