run_6400_connector.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. "hood_switch_in": "F7", "buzzer_out": "H6"}.items():
  41. if by_port.get(port) != ball:
  42. raise ValueError(f"physical fixture input/output {port} expected {ball}, ADC has {by_port.get(port)}")
  43. lines = [
  44. "`timescale 1ns/1ps",
  45. "// Generated from the actual production ADC. No expected logical-to-pin permutation is substituted.",
  46. "module prod2u_6400_v2_physical_fixture (",
  47. " input wire clk, host_ttl_rx, host_rs232_rx,",
  48. " output wire host_ttl_tx, host_rs232_tx,",
  49. " output wire [7:0] fan_connector_pwm,",
  50. " output wire [23:0] ptc_connector_pwm,",
  51. " output wire [10:0] spare_connector_pwm,",
  52. " output wire [2:0] ssr_connector,",
  53. " input wire hood_switch_in,",
  54. " output wire buzzer_connector",
  55. ");",
  56. " wire [3:0] front_fan_pwm_out, rear_fan_pwm_out;",
  57. " wire [23:0] ptc_pwm_out;",
  58. " wire [10:0] ptc_spare_zero;",
  59. " wire ssr1_out, ssr2_out, ssr4_out;",
  60. " wire buzzer_out;",
  61. " tri1 temp_scl, temp_sda, temp_scl_alt, temp_sda_alt;",
  62. " prod2u_6400_v2_top #(.HOST_BAUD(921600)) dut (",
  63. " .sys_clk(clk), .host_ttl_rx(host_ttl_rx), .host_ttl_tx(host_ttl_tx),",
  64. " .host_rs232_rx(host_rs232_rx), .host_rs232_tx(host_rs232_tx),",
  65. " .bmc_rs232_rx(1'b1), .bmc_rs232_tx(),",
  66. " .ptc_pwm_out(ptc_pwm_out), .ptc_spare_zero(ptc_spare_zero),",
  67. " .front_fan_pwm_out(front_fan_pwm_out), .rear_fan_pwm_out(rear_fan_pwm_out),",
  68. " .temp_scl(temp_scl), .temp_sda(temp_sda),",
  69. " .temp_scl_alt(temp_scl_alt), .temp_sda_alt(temp_sda_alt),",
  70. " .ssr1_out(ssr1_out), .ssr2_out(ssr2_out), .ssr4_out(ssr4_out),",
  71. " .ssr_unused_zero(),",
  72. " .hood_switch_in(hood_switch_in), .buzzer_out(buzzer_out)",
  73. " );",
  74. ]
  75. used: dict[str, str] = {}
  76. def signal(ball: str) -> str:
  77. port = by_ball.get(ball)
  78. if port is None:
  79. raise ValueError(f"physical connector ball {ball} has no actual ADC assignment")
  80. if not re.fullmatch(r"(?:(?:front_fan_pwm_out|rear_fan_pwm_out|ptc_pwm_out|ptc_spare_zero)\[\d+\]|ssr[124]_out)", port):
  81. raise ValueError(f"connector ball {ball} mapped to unsupported/non-output port {port}")
  82. used[ball] = port
  83. return port
  84. for index, ball in enumerate(FAN_BALLS):
  85. gate = signal(ball)
  86. lines += [f" // Physical FAN_PWM{index+1}, FPGA ball {ball}: 2N7002 sink and +5V pull-up (digital model).",
  87. f" pullup p_fan{index+1}(fan_connector_pwm[{index}]);",
  88. f" nmos q_fan{index+1}(fan_connector_pwm[{index}], 1'b0, {gate});"]
  89. for name, balls in (("ptc_connector_pwm", PTC_BALLS),
  90. ("spare_connector_pwm", SPARE_BALLS), ("ssr_connector", SSR_BALLS)):
  91. for index, ball in enumerate(balls):
  92. lines.append(f" assign {name}[{index}] = {signal(ball)}; // physical ball {ball}")
  93. lines.append(" // Physical CON146.1 (ball H6): Q10621 2N7002 + Q10620 HL2309 high-side +24V buzzer switch (active high).")
  94. lines.append(" assign buzzer_connector = buzzer_out; // physical ball H6")
  95. lines.append("endmodule")
  96. return "\n".join(lines) + "\n", used
  97. def main() -> int:
  98. parser = argparse.ArgumentParser(description=__doc__)
  99. parser.add_argument("--repo", type=Path, required=True)
  100. parser.add_argument("--tb", type=Path)
  101. parser.add_argument("--out", type=Path, required=True)
  102. args = parser.parse_args()
  103. repo, out = args.repo.resolve(), args.out.resolve()
  104. tb = args.tb or Path(__file__).with_name("prod2u_6400_v2_connector_tb.v")
  105. if not tb.exists() and args.tb is None:
  106. tb = repo / "tests/sim/prod2u_6400_v2_connector_tb.v"
  107. tb = tb.resolve()
  108. for tool in ("iverilog", "vvp"):
  109. if shutil.which(tool) is None:
  110. parser.error(f"required executable unavailable: {tool}")
  111. closure_path = repo / "products/6400-v2/source-closure.json"
  112. adc_path = repo / "constraints/prod2u_6400_v2.adc"
  113. closure = json.loads(closure_path.read_text(encoding="utf-8"))
  114. sources = [repo / name for name in closure["sources"] if Path(name).suffix == ".v"]
  115. all_inputs = [tb, adc_path, closure_path] + [repo / name for name in closure["sources"]]
  116. for path in all_inputs:
  117. if not path.is_file():
  118. parser.error(f"missing input: {path}")
  119. out.mkdir(parents=True, exist_ok=True)
  120. fixture_text, pad_map = fixture_from_adc(adc_path)
  121. fixture_path = out / "actual_adc_physical_fixture.v"
  122. fixture_path.write_text(fixture_text, encoding="utf-8")
  123. receipt = {
  124. "test": "prod2u_6400_v2_connector_tb", "status": "NOT_RUN",
  125. "scope": "production RTL plus actual ADC plus digital NMOS/connector topology; no physical fan/airflow/analog claim",
  126. "source_sha256": {str(path): hashlib.sha256(path.read_bytes()).hexdigest() for path in all_inputs},
  127. "actual_adc_pad_mapping": pad_map,
  128. "physical_fan_balls": FAN_BALLS, "physical_ptc_balls": PTC_BALLS,
  129. "physical_spare_balls": SPARE_BALLS, "physical_ssr_balls": SSR_BALLS,
  130. "fixture_sha256": hashlib.sha256(fixture_path.read_bytes()).hexdigest(),
  131. "commands": [],
  132. }
  133. commands = [
  134. ["iverilog", "-g2012", "-I", str(repo), "-s", "prod2u_6400_v2_connector_tb",
  135. "-o", str(out / "connector.vvp"), str(tb), str(fixture_path), *map(str, sources)],
  136. ["vvp", str(out / "connector.vvp")],
  137. ]
  138. success = True
  139. for name, command in zip(("compile", "simulate"), commands):
  140. started = time.monotonic()
  141. try:
  142. result = subprocess.run(command, cwd=repo, text=True, stdout=subprocess.PIPE,
  143. stderr=subprocess.STDOUT, timeout=360, check=False)
  144. text, code = result.stdout, result.returncode
  145. except subprocess.TimeoutExpired as exc:
  146. raw = exc.stdout or ""
  147. text = raw.decode(errors="replace") if isinstance(raw, bytes) else raw
  148. text += "\nTIMEOUT after 360 seconds\n"
  149. code = 124
  150. log = out / f"{name}.log"
  151. log.write_text(text, encoding="utf-8")
  152. print(text, end="" if text.endswith("\n") else "\n")
  153. receipt["commands"].append({"argv": command, "returncode": code,
  154. "seconds": round(time.monotonic() - started, 3), "log": str(log)})
  155. if code:
  156. success = False
  157. break
  158. if name == "simulate":
  159. success = "PASS prod2u_6400_v2_connector_tb:" in text
  160. receipt["status"] = "PASS" if success else "FAIL"
  161. (out / "receipt.json").write_text(json.dumps(receipt, indent=2) + "\n", encoding="utf-8")
  162. print(f"{receipt['status']}: {out / 'receipt.json'}")
  163. return 0 if success else 1
  164. if __name__ == "__main__":
  165. raise SystemExit(main())