| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- #!/usr/bin/env python3
- """CDC and sequential motion-phase contracts for the minimal axis executors."""
- from __future__ import annotations
- import json
- import re
- import unittest
- from pathlib import Path
- ROOT = Path(__file__).resolve().parents[2]
- AXIS = ROOT / "src" / "rtl" / "prod2u_p4_axis_executor.v"
- SYN = ROOT / "td" / "syn" / "prod2u_p4_normal_v3.tcl"
- PHY = ROOT / "td" / "phy" / "prod2u_p4_normal_v3.tcl"
- TD_RECEIPT_SCHEMA = (
- ROOT / "schemas" / "prod2u_p4_normal_v3_td_build_receipt.schema.json"
- )
- class Prod2UP4TdCdcStructureTests(unittest.TestCase):
- def test_limit_inputs_have_no_axis_synchronizer_or_hub_connection(self) -> None:
- axis = AXIS.read_text(encoding="ascii")
- hub = (ROOT / "src" / "rtl" / "prod2u_p4_motor_hub.v").read_text(encoding="ascii")
- self.assertNotIn("limit_sync_pipe", axis)
- self.assertNotRegex(axis, r"\blimit_raw\b")
- self.assertEqual(hub.count(") u_x_axis ("), 1)
- self.assertEqual(hub.count(") u_y_axis ("), 1)
- self.assertNotIn("limit_sync_pipe", hub)
- self.assertNotRegex(hub, r"\b(?:x_limit_raw|y_limit_raw|limit_raw)\b")
- self.assertFalse((ROOT / "src" / "rtl" / "prod2u_p4_limit_filter.v").exists())
- def test_motion_phase_has_exact_four_state_sequential_encoding(self) -> None:
- axis = AXIS.read_text(encoding="ascii")
- expected = {
- "PHASE_IDLE": 0,
- "PHASE_DIR_SETUP": 1,
- "PHASE_STEP_HIGH": 2,
- "PHASE_STEP_LOW": 3,
- }
- observed = {
- name: int(value)
- for name, value in re.findall(
- r"localparam \[1:0\] (PHASE_[A-Z_]+) = 2'd([0-3]);",
- axis,
- )
- }
- self.assertEqual(observed, expected)
- self.assertIn(
- '(* keep = "true", syn_encoding = "sequential", fsm_encoding = "none" *)',
- axis,
- )
- self.assertRegex(axis, r"reg \[1:0\] motion_phase;")
- def test_syn_and_phy_track_only_two_uart_cdc_registers(self) -> None:
- expected_groups = {
- "u_uart_rx/rx_sync_pipe[0] {{u_uart_rx/rx_sync_pipe[0]} {*/u_uart_rx/rx_sync_pipe[0]}}",
- "u_uart_rx/rx_sync_pipe[1] {{u_uart_rx/rx_sync_pipe[1]} {*/u_uart_rx/rx_sync_pipe[1]}}",
- }
- for path in (SYN, PHY):
- source = path.read_text(encoding="ascii")
- block = source.split("set cdc_groups {", 1)[1].split("\n}", 1)[0]
- observed = {line.strip() for line in block.splitlines() if line.strip()}
- with self.subTest(path=path.name):
- self.assertEqual(observed, expected_groups)
- self.assertIn("proc p4_find_unique_regs {patterns}", source)
- self.assertIn("lsearch -exact $matches $reg", source)
- self.assertIn("if {[llength $cdc_regs] != 2}", source)
- self.assertIn('puts $cdc_report "retained_sync_register=$normalized"', source)
- self.assertNotIn("limit_sync_pipe", source)
- def test_only_uart_input_to_first_stage_is_false_pathed(self) -> None:
- expected_pairs = {
- "host_uart_rx u_uart_rx/rx_sync_pipe[0]",
- }
- for path in (SYN, PHY):
- source = path.read_text(encoding="ascii")
- block = source.split("set cdc_input_pairs {", 1)[1].split("}", 1)[0]
- observed = {line.strip() for line in block.splitlines() if line.strip()}
- with self.subTest(path=path.name):
- self.assertEqual(observed, expected_pairs)
- self.assertEqual(source.count("set_false_path -from"), 1)
- self.assertIn("set_false_path -from $source -to $endpoint", source)
- self.assertNotIn("set_false_path -from $source\n", source)
- self.assertIn('puts $cdc_report "false_path_endpoint=$normalized"', source)
- self.assertNotIn("x_limit_raw", source)
- self.assertNotIn("y_limit_raw", source)
- def test_td_structure_discovery_fails_closed(self) -> None:
- for path in (SYN, PHY):
- source = path.read_text(encoding="ascii")
- with self.subTest(path=path.name):
- self.assertIn("structural_failure=", source)
- self.assertIn("set_false_path -from $source -to $endpoint", source)
- self.assertLess(
- source.index("set cdc_report [open"),
- source.index("foreach {normalized patterns} $cdc_groups"),
- )
- self.assertIn("CDC_GATE_FAIL=", source)
- self.assertIn("FSM_ENCODING_GATE_FAIL=", source)
- self.assertIn(
- 'error "PROD2U_P4_NORMAL_V3_CDC_GATE_FAIL:[join $cdc_failures ,]"',
- source,
- )
- self.assertIn(
- 'error "PROD2U_P4_NORMAL_V3_FSM_ENCODING_GATE_FAIL:[join $fsm_failures ,]"',
- source,
- )
- self.assertNotIn("CDC_GATE_ADVISORY_FAIL=", source)
- self.assertNotIn("FSM_ENCODING_GATE_ADVISORY_FAIL=", source)
- def test_td_receipt_schema_matches_uart_only_cdc_and_two_bit_axes(self) -> None:
- schema = json.loads(TD_RECEIPT_SCHEMA.read_text(encoding="utf-8"))
- structural = schema["$defs"]["structuralGate"]["properties"]
- cdc = structural["cdc"]["properties"]
- fsm = structural["fsm_encoding"]["properties"]
- self.assertEqual(cdc["async_reg"]["properties"]["used"]["const"], 2)
- self.assertEqual(cdc["input_registers"]["properties"]["used"]["const"], 0)
- self.assertEqual(
- cdc["retained_sync_registers"]["properties"]["used"]["const"],
- 2,
- )
- self.assertEqual(
- cdc["first_stage_endpoints"]["properties"]["used"]["const"],
- 1,
- )
- self.assertEqual(fsm["axes"]["const"], {"x": 2, "y": 2})
- def test_td_reports_x_and_y_two_bit_sequential_motion_phase(self) -> None:
- for path in (SYN, PHY):
- source = path.read_text(encoding="ascii")
- with self.subTest(path=path.name):
- self.assertIn("schema=prod2u-p4-normal-v3-fsm-encoding-v2", source)
- self.assertIn(
- "x {{u_core/u_motors/u_x_axis/motion_phase*} {*/u_core/u_motors/u_x_axis/motion_phase*}}",
- source,
- )
- self.assertIn(
- "y {{u_core/u_motors/u_y_axis/motion_phase*} {*/u_core/u_motors/u_y_axis/motion_phase*}}",
- source,
- )
- self.assertIn("(u_core/u_motors/u_%s_axis/motion_phase\\[[0-1]\\])$", source)
- self.assertNotIn("/motion_phase\\[[0-2]\\]$", source)
- self.assertIn("lsearch -exact $normalized_matches $normalized", source)
- self.assertIn(
- "if {[llength $matches] != 2 || "
- "[llength $normalized_matches] != 2}",
- source,
- )
- self.assertIn("motion_phase=${axis}:[llength $matches]:sequential", source)
- if __name__ == "__main__":
- unittest.main()
|