| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- #!/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)
- # Formal uses limit_*_sync[i]; top uses x/y/z_limit_*_sync1.
- text = text.replace("x_limit_ls_sync1", "limit_ls_sync[0]")
- text = text.replace("x_limit_hs_sync1", "limit_hs_sync[0]")
- text = text.replace("y_limit_ls_sync1", "limit_ls_sync[1]")
- text = text.replace("y_limit_hs_sync1", "limit_hs_sync[1]")
- text = text.replace("z_limit_ls_sync1", "limit_ls_sync[2]")
- text = text.replace("z_limit_hs_sync1", "limit_hs_sync[2]")
- text = text.replace("engine_step[0]", "engine_step")
- text = text.replace("engine_dir[0]", "engine_dir")
- text = text.replace("engine_step[1]", "engine_step")
- text = text.replace("engine_dir[1]", "engine_dir")
- text = text.replace("engine_step[2]", "engine_step")
- text = text.replace("engine_dir[2]", "engine_dir")
- text = text.replace("limit_ls_sync[0]", "LS")
- text = text.replace("limit_hs_sync[0]", "HS")
- text = text.replace("limit_ls_sync[1]", "LS")
- text = text.replace("limit_hs_sync[1]", "HS")
- text = text.replace("limit_ls_sync[2]", "LS")
- text = text.replace("limit_hs_sync[2]", "HS")
- 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 & ~(engine_dir ? LS : HS)
- for axis, expr in top_gates.items():
- self.assertIn("engine_step&~(engine_dir?LS:HS)", expr, axis)
- if __name__ == "__main__":
- unittest.main()
|