test_prod2u_p4_td_cdc_structure.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/env python3
  2. """CDC and sequential motion-phase contracts for the minimal axis executors."""
  3. from __future__ import annotations
  4. import json
  5. import re
  6. import unittest
  7. from pathlib import Path
  8. ROOT = Path(__file__).resolve().parents[2]
  9. AXIS = ROOT / "src" / "rtl" / "prod2u_p4_axis_executor.v"
  10. SYN = ROOT / "td" / "syn" / "prod2u_p4_normal_v3.tcl"
  11. PHY = ROOT / "td" / "phy" / "prod2u_p4_normal_v3.tcl"
  12. TD_RECEIPT_SCHEMA = (
  13. ROOT / "schemas" / "prod2u_p4_normal_v3_td_build_receipt.schema.json"
  14. )
  15. class Prod2UP4TdCdcStructureTests(unittest.TestCase):
  16. def test_limit_inputs_have_no_axis_synchronizer_or_hub_connection(self) -> None:
  17. axis = AXIS.read_text(encoding="ascii")
  18. hub = (ROOT / "src" / "rtl" / "prod2u_p4_motor_hub.v").read_text(encoding="ascii")
  19. self.assertNotIn("limit_sync_pipe", axis)
  20. self.assertNotRegex(axis, r"\blimit_raw\b")
  21. self.assertEqual(hub.count(") u_x_axis ("), 1)
  22. self.assertEqual(hub.count(") u_y_axis ("), 1)
  23. self.assertNotIn("limit_sync_pipe", hub)
  24. self.assertNotRegex(hub, r"\b(?:x_limit_raw|y_limit_raw|limit_raw)\b")
  25. self.assertFalse((ROOT / "src" / "rtl" / "prod2u_p4_limit_filter.v").exists())
  26. def test_motion_phase_has_exact_four_state_sequential_encoding(self) -> None:
  27. axis = AXIS.read_text(encoding="ascii")
  28. expected = {
  29. "PHASE_IDLE": 0,
  30. "PHASE_DIR_SETUP": 1,
  31. "PHASE_STEP_HIGH": 2,
  32. "PHASE_STEP_LOW": 3,
  33. }
  34. observed = {
  35. name: int(value)
  36. for name, value in re.findall(
  37. r"localparam \[1:0\] (PHASE_[A-Z_]+) = 2'd([0-3]);",
  38. axis,
  39. )
  40. }
  41. self.assertEqual(observed, expected)
  42. self.assertIn(
  43. '(* keep = "true", syn_encoding = "sequential", fsm_encoding = "none" *)',
  44. axis,
  45. )
  46. self.assertRegex(axis, r"reg \[1:0\] motion_phase;")
  47. def test_syn_and_phy_track_only_two_uart_cdc_registers(self) -> None:
  48. expected_groups = {
  49. "u_uart_rx/rx_sync_pipe[0] {{u_uart_rx/rx_sync_pipe[0]} {*/u_uart_rx/rx_sync_pipe[0]}}",
  50. "u_uart_rx/rx_sync_pipe[1] {{u_uart_rx/rx_sync_pipe[1]} {*/u_uart_rx/rx_sync_pipe[1]}}",
  51. }
  52. for path in (SYN, PHY):
  53. source = path.read_text(encoding="ascii")
  54. block = source.split("set cdc_groups {", 1)[1].split("\n}", 1)[0]
  55. observed = {line.strip() for line in block.splitlines() if line.strip()}
  56. with self.subTest(path=path.name):
  57. self.assertEqual(observed, expected_groups)
  58. self.assertIn("proc p4_find_unique_regs {patterns}", source)
  59. self.assertIn("lsearch -exact $matches $reg", source)
  60. self.assertIn("if {[llength $cdc_regs] != 2}", source)
  61. self.assertIn('puts $cdc_report "retained_sync_register=$normalized"', source)
  62. self.assertNotIn("limit_sync_pipe", source)
  63. def test_only_uart_input_to_first_stage_is_false_pathed(self) -> None:
  64. expected_pairs = {
  65. "host_uart_rx u_uart_rx/rx_sync_pipe[0]",
  66. }
  67. for path in (SYN, PHY):
  68. source = path.read_text(encoding="ascii")
  69. block = source.split("set cdc_input_pairs {", 1)[1].split("}", 1)[0]
  70. observed = {line.strip() for line in block.splitlines() if line.strip()}
  71. with self.subTest(path=path.name):
  72. self.assertEqual(observed, expected_pairs)
  73. self.assertEqual(source.count("set_false_path -from"), 1)
  74. self.assertIn("set_false_path -from $source -to $endpoint", source)
  75. self.assertNotIn("set_false_path -from $source\n", source)
  76. self.assertIn('puts $cdc_report "false_path_endpoint=$normalized"', source)
  77. self.assertNotIn("x_limit_raw", source)
  78. self.assertNotIn("y_limit_raw", source)
  79. def test_td_structure_discovery_fails_closed(self) -> None:
  80. for path in (SYN, PHY):
  81. source = path.read_text(encoding="ascii")
  82. with self.subTest(path=path.name):
  83. self.assertIn("structural_failure=", source)
  84. self.assertIn("set_false_path -from $source -to $endpoint", source)
  85. self.assertLess(
  86. source.index("set cdc_report [open"),
  87. source.index("foreach {normalized patterns} $cdc_groups"),
  88. )
  89. self.assertIn("CDC_GATE_FAIL=", source)
  90. self.assertIn("FSM_ENCODING_GATE_FAIL=", source)
  91. self.assertIn(
  92. 'error "PROD2U_P4_NORMAL_V3_CDC_GATE_FAIL:[join $cdc_failures ,]"',
  93. source,
  94. )
  95. self.assertIn(
  96. 'error "PROD2U_P4_NORMAL_V3_FSM_ENCODING_GATE_FAIL:[join $fsm_failures ,]"',
  97. source,
  98. )
  99. self.assertNotIn("CDC_GATE_ADVISORY_FAIL=", source)
  100. self.assertNotIn("FSM_ENCODING_GATE_ADVISORY_FAIL=", source)
  101. def test_td_receipt_schema_matches_uart_only_cdc_and_two_bit_axes(self) -> None:
  102. schema = json.loads(TD_RECEIPT_SCHEMA.read_text(encoding="utf-8"))
  103. structural = schema["$defs"]["structuralGate"]["properties"]
  104. cdc = structural["cdc"]["properties"]
  105. fsm = structural["fsm_encoding"]["properties"]
  106. self.assertEqual(cdc["async_reg"]["properties"]["used"]["const"], 2)
  107. self.assertEqual(cdc["input_registers"]["properties"]["used"]["const"], 0)
  108. self.assertEqual(
  109. cdc["retained_sync_registers"]["properties"]["used"]["const"],
  110. 2,
  111. )
  112. self.assertEqual(
  113. cdc["first_stage_endpoints"]["properties"]["used"]["const"],
  114. 1,
  115. )
  116. self.assertEqual(fsm["axes"]["const"], {"x": 2, "y": 2})
  117. def test_td_reports_x_and_y_two_bit_sequential_motion_phase(self) -> None:
  118. for path in (SYN, PHY):
  119. source = path.read_text(encoding="ascii")
  120. with self.subTest(path=path.name):
  121. self.assertIn("schema=prod2u-p4-normal-v3-fsm-encoding-v2", source)
  122. self.assertIn(
  123. "x {{u_core/u_motors/u_x_axis/motion_phase*} {*/u_core/u_motors/u_x_axis/motion_phase*}}",
  124. source,
  125. )
  126. self.assertIn(
  127. "y {{u_core/u_motors/u_y_axis/motion_phase*} {*/u_core/u_motors/u_y_axis/motion_phase*}}",
  128. source,
  129. )
  130. self.assertIn("(u_core/u_motors/u_%s_axis/motion_phase\\[[0-1]\\])$", source)
  131. self.assertNotIn("/motion_phase\\[[0-2]\\]$", source)
  132. self.assertIn("lsearch -exact $normalized_matches $normalized", source)
  133. self.assertIn(
  134. "if {[llength $matches] != 2 || "
  135. "[llength $normalized_matches] != 2}",
  136. source,
  137. )
  138. self.assertIn("motion_phase=${axis}:[llength $matches]:sequential", source)
  139. if __name__ == "__main__":
  140. unittest.main()