run_6400_connector.py 7.9 KB

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