#!/usr/bin/env python3 """Run physical-port CT7428/BMC models through the current 6400 production top.""" from __future__ import annotations import argparse import hashlib import json import re import shutil import subprocess import time from pathlib import Path def fixture_from_adc(adc: Path) -> tuple[str, dict[str, str]]: assignments = re.findall( r"set_pin_assignment\s*\{\s*([^{}]+?)\s*\}\s*\{[^{}]*?LOCATION\s*=\s*([A-Z]+\d+)\s*;[^{}]*\}", adc.read_text(encoding="utf-8"), ) by_ball: dict[str, str] = {} by_port: dict[str, str] = {} for port, ball in assignments: port = port.strip() if ball in by_ball or port in by_port: raise ValueError(f"duplicate ADC ball/port {ball}/{port}") by_ball[ball], by_port[port] = port, ball for port, ball in {"sys_clk": "J1", "host_ttl_rx": "J4", "host_ttl_tx": "K2", "bmc_rs232_rx": "M9", "bmc_rs232_tx": "L10"}.items(): if by_port.get(port) != ball: raise ValueError(f"physical connector {port} expected {ball}, ADC has {by_port.get(port)}") ct_mapping = {ball: by_ball.get(ball) for ball in ("B14", "A15", "M8", "N9")} for ball, signal in ct_mapping.items(): if signal not in ("temp_scl", "temp_sda", "temp_scl_alt", "temp_sda_alt"): raise ValueError(f"physical CT branch {ball} has unsupported ADC signal {signal}") # B14/A15 are the populated R1888/R1889 path. M8/N9 lead through # R744/R745 marked NC and cannot drive the sensor in this assembly. fixture = """`timescale 1ns/1ps module prod2u_6400_v2_acquisition_fixture ( input wire clk, host_rx, bmc_rx, output wire host_tx, bmc_tx, inout wire physical_ct_scl, physical_ct_sda, output wire nc_ct_scl, nc_ct_sda, output wire [23:0] ptc ); tri temp_scl, temp_sda, temp_scl_alt, temp_sda_alt; prod2u_6400_v2_top #(.HOST_BAUD(921600)) dut ( .sys_clk(clk), .host_ttl_rx(host_rx), .host_ttl_tx(host_tx), .host_rs232_rx(1'b1), .host_rs232_tx(), .bmc_rs232_rx(bmc_rx), .bmc_rs232_tx(bmc_tx), .ptc_pwm_out(ptc), .ptc_spare_zero(), .front_fan_pwm_out(), .rear_fan_pwm_out(), .temp_scl(temp_scl), .temp_sda(temp_sda), .temp_scl_alt(temp_scl_alt), .temp_sda_alt(temp_sda_alt), .ssr1_out(), .ssr2_out(), .ssr4_out() ); // Timer-only elaboration acceleration. UART bit timing, I2C timing, // 25 MHz clock and every acquisition/state/RAM path stay unchanged. defparam dut.u_bmc_poll.POLL_SECONDS = 0; defparam dut.u_bmc_poll.ROUND_TIMEOUT_CLKS = 750000; """ fixture += f" tran u_scl_populated(physical_ct_scl, {ct_mapping['B14']});\n" fixture += f" tran u_sda_populated(physical_ct_sda, {ct_mapping['A15']});\n" fixture += f" assign nc_ct_scl = {ct_mapping['M8']};\n" fixture += f" assign nc_ct_sda = {ct_mapping['N9']};\n" fixture += "endmodule\n" return fixture, ct_mapping def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--repo", type=Path, required=True) parser.add_argument("--tests", type=Path, help="directory containing the three new Verilog test/model files") parser.add_argument("--out", type=Path, required=True) args = parser.parse_args() repo, out = args.repo.resolve(), args.out.resolve() test_dir = args.tests or Path(__file__).resolve().parent if args.tests is None and not (test_dir / "prod2u_6400_v2_acquisition_tb.v").is_file(): test_dir = repo / "tests/sim" test_files = [test_dir / name for name in ( "prod2u_6400_v2_acquisition_tb.v", "ct7428_normal_slave_bfm.v", "bmc_uart_normal_bfm.v")] closure_path = repo / "products/6400-v2/source-closure.json" adc_path = repo / "constraints/prod2u_6400_v2.adc" closure = json.loads(closure_path.read_text(encoding="utf-8")) source_files = [repo / name for name in closure["sources"] if Path(name).suffix == ".v"] inputs = test_files + [adc_path, closure_path] + [repo / name for name in closure["sources"]] for path in inputs: if not path.is_file(): parser.error(f"missing input: {path}") for tool in ("iverilog", "vvp"): if shutil.which(tool) is None: parser.error(f"required executable unavailable: {tool}") out.mkdir(parents=True, exist_ok=True) fixture_text, mapping = fixture_from_adc(adc_path) fixture_path = out / "actual_adc_acquisition_fixture.v" fixture_path.write_text(fixture_text, encoding="utf-8") receipt = { "test": "prod2u_6400_v2_acquisition_tb", "status": "NOT_RUN", "scope": "physical-port digital BFM through production RTL; not analog or actual CT7428 silicon validation", "ct_evidence": "U6 AD0 pin9 via populated R578=0ohm to VCC; physical B14/A15 via R1888/R1889; M8/N9 R744/R745 NC", "ct_protocol_basis": "0x49, registers 0..8 and raw>>7 from historical MCU contract; vendor brief does not fully specify address/register packing", "ct_model_raw_words": ["0000", "0cb0", "1400", "2378", "3200", "07c0", "0820", "08e0", "0900"], "ct_expected_celsius": [0, 25, 40, 70, 100, 15, 16, 17, 18], "ct_unfitted_channels": "9..26 always 0xFF", "bmc_rounds": ["43-byte command; echo plus 48 tokens on 3 lines plus prompt", "no response; all previous BMC values must become 0xFF"], "clock_hz": 25000000, "host_baud": 921600, "bmc_baud": 115200, "i2c_half_period_clocks": 125, "timer_only_elaboration_overrides": {"u_bmc_poll.POLL_SECONDS": 0, "u_bmc_poll.ROUND_TIMEOUT_CLKS": 750000}, "hierarchical_force_or_ram_injection": False, "actual_adc_ct_mapping": mapping, "source_sha256": {str(path): hashlib.sha256(path.read_bytes()).hexdigest() for path in inputs}, "fixture_sha256": hashlib.sha256(fixture_path.read_bytes()).hexdigest(), "commands": [], } commands = [ ["iverilog", "-g2012", "-I", str(repo), "-s", "prod2u_6400_v2_acquisition_tb", "-o", str(out / "acquisition.vvp"), *map(str, test_files), str(fixture_path), *map(str, source_files)], ["vvp", str(out / "acquisition.vvp")], ] success = True for name, command in zip(("compile", "simulate"), commands): started = time.monotonic() try: result = subprocess.run(command, cwd=repo, text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=240, check=False) text, code = result.stdout, result.returncode except subprocess.TimeoutExpired as exc: raw = exc.stdout or "" text = raw.decode(errors="replace") if isinstance(raw, bytes) else raw text += "\nTIMEOUT after 240 seconds\n" code = 124 log = out / f"{name}.log" log.write_text(text, encoding="utf-8") print(text, end="" if text.endswith("\n") else "\n") receipt["commands"].append({"argv": command, "returncode": code, "seconds": round(time.monotonic()-started, 3), "log": str(log)}) if code: success = False break if name == "simulate": success = "PASS prod2u_6400_v2_acquisition_tb:" in text receipt["status"] = "PASS" if success else "FAIL" (out / "receipt.json").write_text(json.dumps(receipt, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") print(f"{receipt['status']}: {out / 'receipt.json'}") return 0 if success else 1 if __name__ == "__main__": raise SystemExit(main())