test_project_structure.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #!/usr/bin/env python3
  2. """Top-level project structure contract tests for the converged CPLD project."""
  3. from __future__ import annotations
  4. import re
  5. import unittest
  6. from pathlib import Path
  7. ROOT = Path(__file__).resolve().parents[1]
  8. THIS_FILE = Path(__file__).resolve()
  9. WORKSPACE_PATH_MARKER = re.compile(r"(?<![A-Za-z0-9_])workspace[\\/]")
  10. LEGACY_CHAIN_MARKERS = [
  11. "bringup",
  12. "raw_cr",
  13. "raw-cr",
  14. "cr_raw",
  15. "cr-raw",
  16. "sensor_raw",
  17. "sensor-raw",
  18. "host_control_pwm",
  19. "host-control-pwm",
  20. "host_sensor_pwm_debug921",
  21. "host-sensor-pwm-debug921",
  22. "j6_system_debug",
  23. "j6-system-debug",
  24. "j6_system_top",
  25. "j6-system-top",
  26. "j6sys",
  27. "uart_rs232_system",
  28. ]
  29. class ProjectStructureTests(unittest.TestCase):
  30. def test_converged_engineering_layout_has_single_user_bitstream_top(self) -> None:
  31. required_dirs = [
  32. "src/rtl",
  33. "src/top",
  34. "tests/sim",
  35. "tests/formal",
  36. "tools",
  37. "td/syn",
  38. "td/phy",
  39. "config",
  40. "constraints",
  41. "docs",
  42. "schemas",
  43. "reference/original/schematics",
  44. "test_vectors",
  45. ]
  46. for rel in required_dirs:
  47. self.assertTrue((ROOT / rel).is_dir(), rel)
  48. required_files = [
  49. "src/rtl/pid_core.v",
  50. "src/rtl/uart_rx_8n1.v",
  51. "src/rtl/uart_tx_8n1.v",
  52. "src/rtl/bmc_uart_raw48_poll_lite.v",
  53. "src/top/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top.v",
  54. "src/top/uart_rs232_j6_host_sensor_real_fan_ptc_temp_uart_ptc_remap_debug921_top.v",
  55. "src/top/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart_debug921_top.v",
  56. "src/top/uart_rs232_j6_host_sensor_real_fan_ptc_debug_photo_uart_bmc_replay_debug921_top.v",
  57. "src/top/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart1_bmc_replay_debug921_top.v",
  58. "src/top/uart_rs232_j6_host_sensor_real_fan_ptc_debug_photo_uart_bmc_replay_binary921_top.v",
  59. "src/top/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart1_bmc_replay_binary921_top.v",
  60. "src/top/prod2u_bmc_replay_binary921_top.v",
  61. "src/rtl/binary921_crc16.v",
  62. "src/rtl/binary921_frame_tx.v",
  63. "src/rtl/binary921_host_rx.v",
  64. "src/rtl/binary921_diag_core.v",
  65. "tests/sim/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top_tb.v",
  66. "tests/sim/uart_rs232_j6_host_sensor_real_fan_ptc_binary921_top_tb.v",
  67. "constraints/uart_rs232_j6_host_sensor_real_fan_ptc_temp_uart_ptc_remap_debug921.adc",
  68. "constraints/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart_debug921.adc",
  69. "constraints/uart_rs232_j6_host_sensor_real_fan_ptc_debug_photo_uart_bmc_replay_debug921.adc",
  70. "constraints/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart1_bmc_replay_debug921.adc",
  71. "constraints/uart_rs232_j6_host_sensor_real_fan_ptc_debug_photo_uart_bmc_replay_binary921.adc",
  72. "constraints/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart1_bmc_replay_binary921.adc",
  73. "constraints/prod2u_bmc_replay_binary921.adc",
  74. "td/uart_rs232_j6_host_sensor_real_fan_ptc_temp_uart_ptc_remap_debug921.prj",
  75. "td/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart_debug921.prj",
  76. "td/uart_rs232_j6_host_sensor_real_fan_ptc_debug_photo_uart_bmc_replay_debug921.prj",
  77. "td/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart1_bmc_replay_debug921.prj",
  78. "td/uart_rs232_j6_host_sensor_real_fan_ptc_debug_photo_uart_bmc_replay_binary921.prj",
  79. "td/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart1_bmc_replay_binary921.prj",
  80. "td/prod2u_bmc_replay_binary921.prj",
  81. "td/syn/uart_rs232_j6_host_sensor_real_fan_ptc_temp_uart_ptc_remap_debug921.tcl",
  82. "td/syn/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart_debug921.tcl",
  83. "td/syn/uart_rs232_j6_host_sensor_real_fan_ptc_debug_photo_uart_bmc_replay_debug921.tcl",
  84. "td/syn/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart1_bmc_replay_debug921.tcl",
  85. "td/syn/uart_rs232_j6_host_sensor_real_fan_ptc_debug_photo_uart_bmc_replay_binary921.tcl",
  86. "td/syn/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart1_bmc_replay_binary921.tcl",
  87. "td/syn/prod2u_bmc_replay_binary921.tcl",
  88. "td/phy/uart_rs232_j6_host_sensor_real_fan_ptc_temp_uart_ptc_remap_debug921.tcl",
  89. "td/phy/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart_debug921.tcl",
  90. "td/phy/uart_rs232_j6_host_sensor_real_fan_ptc_debug_photo_uart_bmc_replay_debug921.tcl",
  91. "td/phy/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart1_bmc_replay_debug921.tcl",
  92. "td/phy/uart_rs232_j6_host_sensor_real_fan_ptc_debug_photo_uart_bmc_replay_binary921.tcl",
  93. "td/phy/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart1_bmc_replay_binary921.tcl",
  94. "td/phy/prod2u_bmc_replay_binary921.tcl",
  95. "tools/pid_harness.py",
  96. "tools/binary921.py",
  97. "tools/bmc_replay_uart.py",
  98. "tools/uart_receipts.py",
  99. "tools/pin_inventory.py",
  100. "tools/package_j6_host_sensor_real_fan_ptc_debug921.ps1",
  101. "tools/remote/collect-j6-host-sensor-real-fan-ptc.ps1",
  102. "schemas/final_gate_v2_receipt.schema.json",
  103. "schemas/real_bmc_j6_windows_gate_v2_receipt.schema.json",
  104. "schemas/cpld_bmc_acceptance_v1_receipt.schema.json",
  105. "schemas/real_bmc_j6_host_sensor_real_fan_ptc_debug921_v1_receipt.schema.json",
  106. "docs/j6-host-sensor-real-fan-ptc-debug921-runbook.zh-CN.md",
  107. "reference/original/schematics/FCB_FPGA+MCU0611.pdf",
  108. ]
  109. for rel in required_files:
  110. self.assertTrue((ROOT / rel).is_file(), rel)
  111. self.assertEqual(
  112. sorted(path.name for path in (ROOT / "tools" / "remote").glob("*.ps1")),
  113. ["collect-j6-host-sensor-real-fan-ptc.ps1"],
  114. )
  115. def test_old_standalone_j6_diagnostic_entrypoints_are_removed(self) -> None:
  116. active_roots = ["src/top", "tests/sim", "td", "td/syn", "td/phy", "constraints", "tools", "tools/remote", "schemas", "docs"]
  117. offenders: list[str] = []
  118. for root_name in active_roots:
  119. root = ROOT / root_name
  120. if not root.exists():
  121. continue
  122. for path in root.rglob("*"):
  123. if not path.is_file():
  124. continue
  125. rel = path.relative_to(ROOT).as_posix()
  126. if rel in {
  127. "tools/uart_receipts.py",
  128. "tools/pid_harness.py",
  129. "tools/bmc_replay_uart.py",
  130. "docs/verification-gates-v2.zh-CN.md",
  131. "schemas/real_bmc_j6_windows_gate_v2_receipt.schema.json",
  132. }:
  133. continue
  134. lowered = path.name.lower()
  135. if any(marker in lowered for marker in LEGACY_CHAIN_MARKERS):
  136. offenders.append(rel)
  137. self.assertEqual(offenders, [])
  138. def test_workspace_is_not_an_active_project_entrypoint(self) -> None:
  139. self.assertFalse((ROOT / "workspace").exists(), "deprecated workspace tree must not remain active")
  140. def test_active_text_files_do_not_reference_workspace_entrypoints(self) -> None:
  141. active_roots = ["src", "tests", "tools", "td", "config", "constraints", "docs", "schemas"]
  142. offenders: list[str] = []
  143. for root_name in active_roots:
  144. root = ROOT / root_name
  145. if not root.exists():
  146. continue
  147. for path in root.rglob("*"):
  148. if not path.is_file() or path.suffix.lower() in {".png", ".jpg", ".jpeg", ".pdf", ".pyc"}:
  149. continue
  150. if "__pycache__" in path.parts or path.resolve() == THIS_FILE or path.name == "verify_pid_study.py":
  151. continue
  152. text = path.read_text(encoding="utf-8", errors="replace")
  153. if WORKSPACE_PATH_MARKER.search(text):
  154. offenders.append(path.relative_to(ROOT).as_posix())
  155. self.assertEqual(offenders, [])
  156. def test_latest_top_matches_locked_serial_and_real_fan_ptc_interface(self) -> None:
  157. top = (ROOT / "src/top/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top.v").read_text(
  158. encoding="utf-8",
  159. errors="replace",
  160. )
  161. module_header = top.split(");", 1)[0]
  162. for marker in [
  163. "bmc_uart_rx",
  164. "bmc_uart_tx",
  165. "debug_uart_rx",
  166. "debug_uart_tx",
  167. "host_uart_rx",
  168. "host_uart_tx",
  169. "ptc_pwm_out",
  170. "fan_pwm_out",
  171. "fan_fg_in",
  172. "fan_install_in",
  173. "SET PTC 01..24",
  174. "SET FAN 01..04",
  175. "GET SENSOR",
  176. "SET PID ENABLE",
  177. "pid_active=0",
  178. "J6CMD",
  179. "J6RAW",
  180. "J6TEMP",
  181. ]:
  182. self.assertIn(marker, top)
  183. self.assertIn("output wire [23:0] ptc_pwm_out", module_header)
  184. self.assertIn("output wire [3:0] fan_pwm_out", module_header)
  185. self.assertIn("input wire [7:0] fan_fg_in", module_header)
  186. self.assertIn("input wire [3:0] fan_install_in", module_header)
  187. self.assertNotIn("PROGRAM_FLASH", top)
  188. self.assertNotIn("ERASE_FLASH", top)
  189. def test_two_user_td_profiles_elaborate_distinct_wrapper_tops(self) -> None:
  190. profiles = {
  191. "uart_rs232_j6_host_sensor_real_fan_ptc_temp_uart_ptc_remap_debug921": 0,
  192. "uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart_debug921": 1,
  193. "uart_rs232_j6_host_sensor_real_fan_ptc_debug_photo_uart_bmc_replay_debug921": 0,
  194. "uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart1_bmc_replay_debug921": 1,
  195. }
  196. for stem, pin_profile in profiles.items():
  197. with self.subTest(stem=stem):
  198. wrapper_path = ROOT / "src" / "top" / f"{stem}_top.v"
  199. wrapper = wrapper_path.read_text(encoding="utf-8", errors="replace")
  200. self.assertIn(f"module {stem}_top", wrapper)
  201. self.assertIn("uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top #(", wrapper)
  202. self.assertIn(f".PIN_PROFILE({pin_profile})", wrapper)
  203. prj_text = (ROOT / "td" / f"{stem}.prj").read_text(encoding="utf-8")
  204. self.assertIn("../src/top/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top.v", prj_text)
  205. self.assertIn(f"../src/top/{stem}_top.v", prj_text)
  206. self.assertIn(f"<MODULE>{stem}_top</MODULE>", prj_text)
  207. syn_text = (ROOT / "td" / "syn" / f"{stem}.tcl").read_text(encoding="utf-8")
  208. self.assertIn("analyze -file {src/top/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top.v}", syn_text)
  209. self.assertIn(f"analyze -file {{src/top/{stem}_top.v}}", syn_text)
  210. self.assertIn(f"elaborate -top {{{stem}_top}}", syn_text)
  211. def test_binary921_profiles_use_binary_diag_core_not_ascii_debug921_top(self) -> None:
  212. profiles = {
  213. "uart_rs232_j6_host_sensor_real_fan_ptc_debug_photo_uart_bmc_replay_binary921": 0,
  214. "uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart1_bmc_replay_binary921": 1,
  215. }
  216. for stem, pin_profile in profiles.items():
  217. with self.subTest(stem=stem):
  218. wrapper_path = ROOT / "src" / "top" / f"{stem}_top.v"
  219. wrapper = wrapper_path.read_text(encoding="utf-8", errors="replace")
  220. self.assertIn(f"module {stem}_top", wrapper)
  221. self.assertIn("binary921_diag_core #(", wrapper)
  222. self.assertIn(f".PIN_PROFILE({pin_profile})", wrapper)
  223. self.assertNotIn("uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top #(", wrapper)
  224. prj_text = (ROOT / "td" / f"{stem}.prj").read_text(encoding="utf-8")
  225. self.assertIn("../src/rtl/binary921_diag_core.v", prj_text)
  226. self.assertIn(f"../src/top/{stem}_top.v", prj_text)
  227. self.assertIn(f"<MODULE>{stem}_top</MODULE>", prj_text)
  228. syn_text = (ROOT / "td" / "syn" / f"{stem}.tcl").read_text(encoding="utf-8")
  229. self.assertIn("analyze -file {src/rtl/binary921_diag_core.v}", syn_text)
  230. self.assertNotIn("analyze -file {src/top/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top.v}", syn_text)
  231. self.assertIn(f"elaborate -top {{{stem}_top}}", syn_text)
  232. def test_production_two_uart_binary921_top_uses_combined_uart_and_real_pin_constraints(self) -> None:
  233. stem = "prod2u_bmc_replay_binary921"
  234. wrapper_path = ROOT / "src" / "top" / f"{stem}_top.v"
  235. constraint_path = ROOT / "constraints" / f"{stem}.adc"
  236. wrapper = wrapper_path.read_text(encoding="utf-8", errors="replace")
  237. self.assertIn(f"module {stem}_top", wrapper)
  238. self.assertIn("binary921_diag_core #(", wrapper)
  239. self.assertIn(".COMBINED_HOST_DEBUG_UART(1)", wrapper)
  240. self.assertIn(".PIN_PROFILE(2)", wrapper)
  241. self.assertIn(".HOST_UART_CLKS_PER_BIT(HOST_UART_CLKS_PER_BIT)", wrapper)
  242. self.assertNotIn("debug_uart_rx", wrapper.split(");", 1)[0])
  243. self.assertNotIn("debug_uart_tx", wrapper.split(");", 1)[0])
  244. constraints = constraint_path.read_text(encoding="utf-8", errors="replace")
  245. self.assertIn("{ bmc_uart_tx } { LOCATION = L10", constraints)
  246. self.assertIn("{ bmc_uart_rx } { LOCATION = M9", constraints)
  247. self.assertIn("{ host_uart_tx } { LOCATION = M10", constraints)
  248. self.assertIn("{ host_uart_rx } { LOCATION = N11", constraints)
  249. self.assertNotIn("debug_uart", constraints)
  250. self.assertIn("{ ptc_pwm_out[2] } { LOCATION = T4", constraints)
  251. self.assertIn("{ fan_pwm_out[0] } { LOCATION = C4", constraints)
  252. prj_text = (ROOT / "td" / f"{stem}.prj").read_text(encoding="utf-8")
  253. self.assertIn("../src/rtl/binary921_diag_core.v", prj_text)
  254. self.assertIn(f"../src/top/{stem}_top.v", prj_text)
  255. self.assertIn(f"<MODULE>{stem}_top</MODULE>", prj_text)
  256. syn_text = (ROOT / "td" / "syn" / f"{stem}.tcl").read_text(encoding="utf-8")
  257. self.assertIn("analyze -file {src/rtl/binary921_diag_core.v}", syn_text)
  258. self.assertIn(f"elaborate -top {{{stem}_top}}", syn_text)
  259. self.assertIn(f"read_adc {{constraints/{stem}.adc}}", syn_text)
  260. def test_binary921_host_rx_crc_output_is_net_for_td_frontend(self) -> None:
  261. for path in [
  262. ROOT / "src" / "rtl" / "binary921_host_rx.v",
  263. ROOT / "src" / "rtl" / "binary921_frame_tx.v",
  264. ]:
  265. with self.subTest(path=path.name):
  266. source = path.read_text(encoding="utf-8", errors="replace")
  267. self.assertIn("wire [15:0] crc_next;", source)
  268. self.assertNotIn("reg [15:0] crc_next;", source)
  269. if __name__ == "__main__":
  270. unittest.main()