test_project_structure.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. "tests/sim/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top_tb.v",
  59. "constraints/uart_rs232_j6_host_sensor_real_fan_ptc_temp_uart_ptc_remap_debug921.adc",
  60. "constraints/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart_debug921.adc",
  61. "constraints/uart_rs232_j6_host_sensor_real_fan_ptc_debug_photo_uart_bmc_replay_debug921.adc",
  62. "constraints/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart1_bmc_replay_debug921.adc",
  63. "td/uart_rs232_j6_host_sensor_real_fan_ptc_temp_uart_ptc_remap_debug921.prj",
  64. "td/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart_debug921.prj",
  65. "td/uart_rs232_j6_host_sensor_real_fan_ptc_debug_photo_uart_bmc_replay_debug921.prj",
  66. "td/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart1_bmc_replay_debug921.prj",
  67. "td/syn/uart_rs232_j6_host_sensor_real_fan_ptc_temp_uart_ptc_remap_debug921.tcl",
  68. "td/syn/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart_debug921.tcl",
  69. "td/syn/uart_rs232_j6_host_sensor_real_fan_ptc_debug_photo_uart_bmc_replay_debug921.tcl",
  70. "td/syn/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart1_bmc_replay_debug921.tcl",
  71. "td/phy/uart_rs232_j6_host_sensor_real_fan_ptc_temp_uart_ptc_remap_debug921.tcl",
  72. "td/phy/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart_debug921.tcl",
  73. "td/phy/uart_rs232_j6_host_sensor_real_fan_ptc_debug_photo_uart_bmc_replay_debug921.tcl",
  74. "td/phy/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart1_bmc_replay_debug921.tcl",
  75. "tools/pid_harness.py",
  76. "tools/bmc_replay_uart.py",
  77. "tools/uart_receipts.py",
  78. "tools/pin_inventory.py",
  79. "tools/package_j6_host_sensor_real_fan_ptc_debug921.ps1",
  80. "tools/remote/collect-j6-host-sensor-real-fan-ptc.ps1",
  81. "schemas/final_gate_v2_receipt.schema.json",
  82. "schemas/real_bmc_j6_windows_gate_v2_receipt.schema.json",
  83. "schemas/cpld_bmc_acceptance_v1_receipt.schema.json",
  84. "schemas/real_bmc_j6_host_sensor_real_fan_ptc_debug921_v1_receipt.schema.json",
  85. "docs/j6-host-sensor-real-fan-ptc-debug921-runbook.zh-CN.md",
  86. "reference/original/schematics/FCB_FPGA+MCU0611.pdf",
  87. ]
  88. for rel in required_files:
  89. self.assertTrue((ROOT / rel).is_file(), rel)
  90. self.assertEqual(
  91. sorted(path.name for path in (ROOT / "tools" / "remote").glob("*.ps1")),
  92. ["collect-j6-host-sensor-real-fan-ptc.ps1"],
  93. )
  94. def test_old_standalone_j6_diagnostic_entrypoints_are_removed(self) -> None:
  95. active_roots = ["src/top", "tests/sim", "td", "td/syn", "td/phy", "constraints", "tools", "tools/remote", "schemas", "docs"]
  96. offenders: list[str] = []
  97. for root_name in active_roots:
  98. root = ROOT / root_name
  99. if not root.exists():
  100. continue
  101. for path in root.rglob("*"):
  102. if not path.is_file():
  103. continue
  104. rel = path.relative_to(ROOT).as_posix()
  105. if rel in {
  106. "tools/uart_receipts.py",
  107. "tools/pid_harness.py",
  108. "tools/bmc_replay_uart.py",
  109. "docs/verification-gates-v2.zh-CN.md",
  110. "schemas/real_bmc_j6_windows_gate_v2_receipt.schema.json",
  111. }:
  112. continue
  113. lowered = path.name.lower()
  114. if any(marker in lowered for marker in LEGACY_CHAIN_MARKERS):
  115. offenders.append(rel)
  116. self.assertEqual(offenders, [])
  117. def test_workspace_is_not_an_active_project_entrypoint(self) -> None:
  118. self.assertFalse((ROOT / "workspace").exists(), "deprecated workspace tree must not remain active")
  119. def test_active_text_files_do_not_reference_workspace_entrypoints(self) -> None:
  120. active_roots = ["src", "tests", "tools", "td", "config", "constraints", "docs", "schemas"]
  121. offenders: list[str] = []
  122. for root_name in active_roots:
  123. root = ROOT / root_name
  124. if not root.exists():
  125. continue
  126. for path in root.rglob("*"):
  127. if not path.is_file() or path.suffix.lower() in {".png", ".jpg", ".jpeg", ".pdf", ".pyc"}:
  128. continue
  129. if "__pycache__" in path.parts or path.resolve() == THIS_FILE or path.name == "verify_pid_study.py":
  130. continue
  131. text = path.read_text(encoding="utf-8", errors="replace")
  132. if WORKSPACE_PATH_MARKER.search(text):
  133. offenders.append(path.relative_to(ROOT).as_posix())
  134. self.assertEqual(offenders, [])
  135. def test_latest_top_matches_locked_serial_and_real_fan_ptc_interface(self) -> None:
  136. top = (ROOT / "src/top/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top.v").read_text(
  137. encoding="utf-8",
  138. errors="replace",
  139. )
  140. module_header = top.split(");", 1)[0]
  141. for marker in [
  142. "bmc_uart_rx",
  143. "bmc_uart_tx",
  144. "debug_uart_rx",
  145. "debug_uart_tx",
  146. "host_uart_rx",
  147. "host_uart_tx",
  148. "ptc_pwm_out",
  149. "fan_pwm_out",
  150. "fan_fg_in",
  151. "fan_install_in",
  152. "SET PTC 01..24",
  153. "SET FAN 01..04",
  154. "GET SENSOR",
  155. "SET PID ENABLE",
  156. "pid_active=0",
  157. "J6CMD",
  158. "J6RAW",
  159. "J6TEMP",
  160. ]:
  161. self.assertIn(marker, top)
  162. self.assertIn("output wire [23:0] ptc_pwm_out", module_header)
  163. self.assertIn("output wire [3:0] fan_pwm_out", module_header)
  164. self.assertIn("input wire [7:0] fan_fg_in", module_header)
  165. self.assertIn("input wire [3:0] fan_install_in", module_header)
  166. self.assertNotIn("PROGRAM_FLASH", top)
  167. self.assertNotIn("ERASE_FLASH", top)
  168. def test_two_user_td_profiles_elaborate_distinct_wrapper_tops(self) -> None:
  169. profiles = {
  170. "uart_rs232_j6_host_sensor_real_fan_ptc_temp_uart_ptc_remap_debug921": 0,
  171. "uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart_debug921": 1,
  172. "uart_rs232_j6_host_sensor_real_fan_ptc_debug_photo_uart_bmc_replay_debug921": 0,
  173. "uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart1_bmc_replay_debug921": 1,
  174. }
  175. for stem, pin_profile in profiles.items():
  176. with self.subTest(stem=stem):
  177. wrapper_path = ROOT / "src" / "top" / f"{stem}_top.v"
  178. wrapper = wrapper_path.read_text(encoding="utf-8", errors="replace")
  179. self.assertIn(f"module {stem}_top", wrapper)
  180. self.assertIn("uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top #(", wrapper)
  181. self.assertIn(f".PIN_PROFILE({pin_profile})", wrapper)
  182. prj_text = (ROOT / "td" / f"{stem}.prj").read_text(encoding="utf-8")
  183. self.assertIn("../src/top/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top.v", prj_text)
  184. self.assertIn(f"../src/top/{stem}_top.v", prj_text)
  185. self.assertIn(f"<MODULE>{stem}_top</MODULE>", prj_text)
  186. syn_text = (ROOT / "td" / "syn" / f"{stem}.tcl").read_text(encoding="utf-8")
  187. self.assertIn("analyze -file {src/top/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top.v}", syn_text)
  188. self.assertIn(f"analyze -file {{src/top/{stem}_top.v}}", syn_text)
  189. self.assertIn(f"elaborate -top {{{stem}_top}}", syn_text)
  190. if __name__ == "__main__":
  191. unittest.main()