test_prod2u_p4_td_cdc_structure.py 8.0 KB

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