run_6400_connector.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/env python3
  2. """Test 6400 production RTL through actual ADC pad assignments and PCB inversion."""
  3. from __future__ import annotations
  4. import argparse
  5. import hashlib
  6. import json
  7. import re
  8. import shutil
  9. import subprocess
  10. import time
  11. from pathlib import Path
  12. # Independent physical evidence: FCB_FPGA+MCU_V2.0_20260914 BRD plus
  13. # temperature-system electrical drawing p4/p5/p6/p8. The ADC is the DUT
  14. # input, never the authority for these expected physical ball lists.
  15. FAN_BALLS = ("C4", "B5", "B1", "A14", "P12", "A10", "D8", "C7")
  16. PTC_BALLS = (
  17. "F16", "G16", "T4", "R5", "T6", "R7",
  18. "T2", "R3", "P16", "R16", "M16", "N16",
  19. "P10", "R10", "T13", "T12", "L3", "N2",
  20. "K5", "L4", "H4", "J6", "H5", "C15",
  21. )
  22. SPARE_BALLS = ("M1", "N1", "P1", "R1", "P8", "P7", "T9", "P9", "P2", "M3", "L5")
  23. SSR_BALLS = ("E14", "E8", "D9")
  24. def fixture_from_adc(adc: Path) -> tuple[str, dict[str, str]]:
  25. assignments = re.findall(
  26. r"set_pin_assignment\s*\{\s*([^{}]+?)\s*\}\s*\{[^{}]*?LOCATION\s*=\s*([A-Z]+\d+)\s*;[^{}]*\}",
  27. adc.read_text(encoding="utf-8"),
  28. )
  29. by_ball: dict[str, str] = {}
  30. by_port: dict[str, str] = {}
  31. for port, ball in assignments:
  32. port = port.strip()
  33. if ball in by_ball or port in by_port:
  34. raise ValueError(f"duplicate ADC ball or port: {ball} / {port}")
  35. by_ball[ball], by_port[port] = port, ball
  36. # UART/clock pins are explicit physical fixture inputs, so reject a
  37. # changed assignment instead of inadvertently driving the old port.
  38. for port, ball in {"sys_clk": "J1", "host_ttl_rx": "J4", "host_ttl_tx": "K2",
  39. "host_rs232_rx": "R13", "host_rs232_tx": "T14",
  40. "estop_btn_in": "F8", "hood_open_btn_in": "C11", "hood_close_btn_in": "A11",
  41. "buzzer_out": "H6"}.items():
  42. if by_port.get(port) != ball:
  43. raise ValueError(f"physical fixture input/output {port} expected {ball}, ADC has {by_port.get(port)}")
  44. if "F7" in by_ball:
  45. raise ValueError(f"CON165.12 (ball F7) must be NC/unbound in V3.0, ADC binds {by_ball['F7']}")
  46. lines = [
  47. "`timescale 1ns/1ps",
  48. "// Generated from the actual production ADC. No expected logical-to-pin permutation is substituted.",
  49. "module prod2u_6400_v2_physical_fixture (",
  50. " input wire clk, host_ttl_rx, host_rs232_rx,",
  51. " output wire host_ttl_tx, host_rs232_tx,",
  52. " output wire [7:0] fan_connector_pwm,",
  53. " output wire [23:0] ptc_connector_pwm,",
  54. " output wire [10:0] spare_connector_pwm,",
  55. " output wire [2:0] ssr_connector,",
  56. " input wire estop_btn_in, hood_open_btn_in, hood_close_btn_in,",
  57. " output wire buzzer_connector",
  58. ");",
  59. " wire [3:0] front_fan_pwm_out, rear_fan_pwm_out;",
  60. " wire [23:0] ptc_pwm_out;",
  61. " wire [10:0] ptc_spare_zero;",
  62. " wire ssr1_out, ssr2_out, ssr4_out;",
  63. " wire buzzer_out;",
  64. " tri1 temp_scl, temp_sda, temp_scl_alt, temp_sda_alt;",
  65. " prod2u_6400_v2_top #(.HOST_BAUD(921600)) dut (",
  66. " .sys_clk(clk), .host_ttl_rx(host_ttl_rx), .host_ttl_tx(host_ttl_tx),",
  67. " .host_rs232_rx(host_rs232_rx), .host_rs232_tx(host_rs232_tx),",
  68. " .bmc_rs232_rx(1'b1), .bmc_rs232_tx(),",
  69. " .ptc_pwm_out(ptc_pwm_out), .ptc_spare_zero(ptc_spare_zero),",
  70. " .front_fan_pwm_out(front_fan_pwm_out), .rear_fan_pwm_out(rear_fan_pwm_out),",
  71. " .temp_scl(temp_scl), .temp_sda(temp_sda),",
  72. " .temp_scl_alt(temp_scl_alt), .temp_sda_alt(temp_sda_alt),",
  73. " .ssr1_out(ssr1_out), .ssr2_out(ssr2_out), .ssr4_out(ssr4_out),",
  74. " .ssr_unused_zero(),",
  75. " .estop_btn_in(estop_btn_in), .hood_open_btn_in(hood_open_btn_in), .hood_close_btn_in(hood_close_btn_in),",
  76. " .buzzer_out(buzzer_out)",
  77. " );",
  78. ]
  79. used: dict[str, str] = {}
  80. def signal(ball: str) -> str:
  81. port = by_ball.get(ball)
  82. if port is None:
  83. raise ValueError(f"physical connector ball {ball} has no actual ADC assignment")
  84. if not re.fullmatch(r"(?:(?:front_fan_pwm_out|rear_fan_pwm_out|ptc_pwm_out|ptc_spare_zero)\[\d+\]|ssr[124]_out)", port):
  85. raise ValueError(f"connector ball {ball} mapped to unsupported/non-output port {port}")
  86. used[ball] = port
  87. return port
  88. for index, ball in enumerate(FAN_BALLS):
  89. gate = signal(ball)
  90. lines += [f" // Physical FAN_PWM{index+1}, FPGA ball {ball}: 2N7002 sink and +5V pull-up (digital model).",
  91. f" pullup p_fan{index+1}(fan_connector_pwm[{index}]);",
  92. f" nmos q_fan{index+1}(fan_connector_pwm[{index}], 1'b0, {gate});"]
  93. for name, balls in (("ptc_connector_pwm", PTC_BALLS),
  94. ("spare_connector_pwm", SPARE_BALLS), ("ssr_connector", SSR_BALLS)):
  95. for index, ball in enumerate(balls):
  96. lines.append(f" assign {name}[{index}] = {signal(ball)}; // physical ball {ball}")
  97. lines.append(" // Physical CON146.1 (ball H6): Q10621 2N7002 + Q10620 HL2309 high-side +24V buzzer switch (active high).")
  98. lines.append(" assign buzzer_connector = buzzer_out; // physical ball H6")
  99. lines.append("endmodule")
  100. return "\n".join(lines) + "\n", used
  101. def main() -> int:
  102. parser = argparse.ArgumentParser(description=__doc__)
  103. parser.add_argument("--repo", type=Path, required=True)
  104. parser.add_argument("--tb", type=Path)
  105. parser.add_argument("--out", type=Path, required=True)
  106. args = parser.parse_args()
  107. repo, out = args.repo.resolve(), args.out.resolve()
  108. tb = args.tb or Path(__file__).with_name("prod2u_6400_v2_connector_tb.v")
  109. if not tb.exists() and args.tb is None:
  110. tb = repo / "tests/sim/prod2u_6400_v2_connector_tb.v"
  111. tb = tb.resolve()
  112. for tool in ("iverilog", "vvp"):
  113. if shutil.which(tool) is None:
  114. parser.error(f"required executable unavailable: {tool}")
  115. closure_path = repo / "products/6400-v2/source-closure.json"
  116. adc_path = repo / "constraints/prod2u_6400_v2.adc"
  117. closure = json.loads(closure_path.read_text(encoding="utf-8"))
  118. sources = [repo / name for name in closure["sources"] if Path(name).suffix == ".v"]
  119. all_inputs = [tb, adc_path, closure_path] + [repo / name for name in closure["sources"]]
  120. for path in all_inputs:
  121. if not path.is_file():
  122. parser.error(f"missing input: {path}")
  123. out.mkdir(parents=True, exist_ok=True)
  124. fixture_text, pad_map = fixture_from_adc(adc_path)
  125. fixture_path = out / "actual_adc_physical_fixture.v"
  126. fixture_path.write_text(fixture_text, encoding="utf-8")
  127. receipt = {
  128. "test": "prod2u_6400_v2_connector_tb", "status": "NOT_RUN",
  129. "scope": "production RTL plus actual ADC plus digital NMOS/connector topology; no physical fan/airflow/analog claim",
  130. "source_sha256": {str(path): hashlib.sha256(path.read_bytes()).hexdigest() for path in all_inputs},
  131. "actual_adc_pad_mapping": pad_map,
  132. "physical_fan_balls": FAN_BALLS, "physical_ptc_balls": PTC_BALLS,
  133. "physical_spare_balls": SPARE_BALLS, "physical_ssr_balls": SSR_BALLS,
  134. "physical_button_balls": {"estop_btn_in": "F8", "hood_open_btn_in": "C11", "hood_close_btn_in": "A11"},
  135. "physical_buzzer_ball": "H6",
  136. "fixture_sha256": hashlib.sha256(fixture_path.read_bytes()).hexdigest(),
  137. "commands": [],
  138. }
  139. commands = [
  140. ["iverilog", "-g2012", "-I", str(repo), "-s", "prod2u_6400_v2_connector_tb",
  141. "-o", str(out / "connector.vvp"), str(tb), str(fixture_path), *map(str, sources)],
  142. ["vvp", str(out / "connector.vvp")],
  143. ]
  144. success = True
  145. for name, command in zip(("compile", "simulate"), commands):
  146. started = time.monotonic()
  147. try:
  148. result = subprocess.run(command, cwd=repo, text=True, stdout=subprocess.PIPE,
  149. stderr=subprocess.STDOUT, timeout=360, check=False)
  150. text, code = result.stdout, result.returncode
  151. except subprocess.TimeoutExpired as exc:
  152. raw = exc.stdout or ""
  153. text = raw.decode(errors="replace") if isinstance(raw, bytes) else raw
  154. text += "\nTIMEOUT after 360 seconds\n"
  155. code = 124
  156. log = out / f"{name}.log"
  157. log.write_text(text, encoding="utf-8")
  158. print(text, end="" if text.endswith("\n") else "\n")
  159. receipt["commands"].append({"argv": command, "returncode": code,
  160. "seconds": round(time.monotonic() - started, 3), "log": str(log)})
  161. if code:
  162. success = False
  163. break
  164. if name == "simulate":
  165. success = "PASS prod2u_6400_v2_connector_tb:" in text
  166. receipt["status"] = "PASS" if success else "FAIL"
  167. (out / "receipt.json").write_text(json.dumps(receipt, indent=2) + "\n", encoding="utf-8")
  168. print(f"{receipt['status']}: {out / 'receipt.json'}")
  169. return 0 if success else 1
  170. if __name__ == "__main__":
  171. raise SystemExit(main())