| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- #!/usr/bin/env python3
- """Structure-equivalence: formal STEP gate must match real top pin gate."""
- from __future__ import annotations
- import re
- import unittest
- from pathlib import Path
- ROOT = Path(__file__).resolve().parents[2]
- TOP = ROOT / "src/top/prod2u_p4_normal_v3_top.v"
- FORMAL = ROOT / "tests/formal/runtime_limit_hard_stop_formal.v"
- def _normalize_gate_expr(expr: str) -> str:
- text = re.sub(r"\s+", "", expr)
- text = text.replace("engine_step[0]", "engine_step")
- text = text.replace("engine_step[1]", "engine_step")
- text = text.replace("engine_step[2]", "engine_step")
- text = text.replace("selected_limit[0]", "selected_limit")
- text = text.replace("selected_limit[1]", "selected_limit")
- text = text.replace("selected_limit[2]", "selected_limit")
- return text
- def _extract_top_gates(text: str) -> dict[str, str]:
- pattern = re.compile(
- r"assign\s+([xyz])_step\s*=\s*([^;]+);",
- re.MULTILINE,
- )
- return {m.group(1): _normalize_gate_expr(m.group(2)) for m in pattern.finditer(text)}
- def _extract_formal_gates(text: str) -> dict[str, str]:
- pattern = re.compile(
- r"assign\s+gated_step\[(\d)\]\s*=\s*([^;]+);",
- re.MULTILINE,
- )
- axis = {"0": "x", "1": "y", "2": "z"}
- return {
- axis[m.group(1)]: _normalize_gate_expr(m.group(2))
- for m in pattern.finditer(text)
- }
- class TopStepGateMatchesFormalTests(unittest.TestCase):
- def test_formal_gate_matches_top_structure(self) -> None:
- top = TOP.read_text(encoding="utf-8")
- formal = FORMAL.read_text(encoding="utf-8")
- top_gates = _extract_top_gates(top)
- formal_gates = _extract_formal_gates(formal)
- self.assertEqual(set(top_gates), {"x", "y", "z"})
- self.assertEqual(set(formal_gates), {"x", "y", "z"})
- for axis in ("x", "y", "z"):
- self.assertEqual(
- top_gates[axis],
- formal_gates[axis],
- f"axis {axis}: top={top_gates[axis]!r} formal={formal_gates[axis]!r}",
- )
- # Canonical shape: engine_step & ~selected_limit
- for axis, expr in top_gates.items():
- self.assertIn("engine_step&~selected_limit", expr, axis)
- if __name__ == "__main__":
- unittest.main()
|