| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621 |
- #!/usr/bin/env python3
- """Pin inventory, IO budget, and ADC guardrails for UART/RS232 migration."""
- from __future__ import annotations
- import argparse
- import json
- import sys
- from datetime import datetime, timezone
- from pathlib import Path
- from typing import Any
- ROOT = Path(__file__).resolve().parents[1]
- LOCKED_DEBUG_UART_ASSIGNMENTS = {
- "bmc_uart_tx": "T4",
- "bmc_uart_rx": "R5",
- "host_uart_tx": "P7",
- "host_uart_rx": "P8",
- "debug_uart_rx": "P10",
- "debug_uart_tx": "R10",
- }
- TEMP_UART_PTC_REMAP_SUBSTITUTIONS = {
- "PTC03": {"signal": "ptc_pwm_out[2]", "substitute_ball": "P5", "bank_io_net": "BANK2_GPIO40"},
- "PTC04": {"signal": "ptc_pwm_out[3]", "substitute_ball": "T8", "bank_io_net": "BANK2_GPIO41"},
- "PTC17": {"signal": "ptc_pwm_out[16]", "substitute_ball": "M14", "bank_io_net": "BANK2_GPIO42"},
- "PTC18": {"signal": "ptc_pwm_out[17]", "substitute_ball": "M15", "bank_io_net": "BANK2_GPIO43"},
- "PTC21": {"signal": "ptc_pwm_out[20]", "substitute_ball": "P15", "bank_io_net": "BANK2_GPIO44"},
- "PTC22": {"signal": "ptc_pwm_out[21]", "substitute_ball": "N14", "bank_io_net": "BANK2_GPIO45"},
- }
- FORMAL_UART_ASSIGNMENTS = {
- "bmc_uart_tx": "P5",
- "bmc_uart_rx": "T8",
- "host_uart_tx": "M14",
- "host_uart_rx": "M15",
- "debug_uart_rx": "P15",
- "debug_uart_tx": "N14",
- }
- FORMAL_UART1_BMC_REPLAY_ASSIGNMENTS = {
- "bmc_uart_tx": "L10",
- "bmc_uart_rx": "M9",
- "host_uart_tx": "M10",
- "host_uart_rx": "N11",
- "debug_uart_rx": "P15",
- "debug_uart_tx": "N14",
- }
- FORMAL_UART1_BMC_REPLAY_BINARY921_ASSIGNMENTS = {
- "bmc_uart_tx": "L10",
- "bmc_uart_rx": "M9",
- "host_uart_tx": "M10",
- "host_uart_rx": "N11",
- "debug_uart_rx": "M14",
- "debug_uart_tx": "M15",
- }
- PRODUCTION_TWO_UART_BMC_REPLAY_BINARY921_ASSIGNMENTS = {
- "bmc_uart_tx": "L10",
- "bmc_uart_rx": "M9",
- "host_uart_tx": "M10",
- "host_uart_rx": "N11",
- }
- RESERVED_FUTURE_UART_SIGNALS = ["UART1_TX", "UART1_RX", "UART2_TX", "UART2_RX"]
- REQUIRED_IO_GROUPS = [
- {"name": "ptc_pwm[23:0]", "count": 24, "reason": "24 HEAT/PTC PWM outputs"},
- {"name": "fan_pwm[3:0]", "count": 4, "reason": "4 FAN_PWM drive outputs"},
- {"name": "fan_fg[7:0]", "count": 8, "reason": "8 FAN_FG tachometer inputs"},
- {"name": "fan_install[3:0]", "count": 4, "reason": "4 FAN_INSTALL presence inputs"},
- {"name": "bmc_uart(rx,tx)", "count": 2, "reason": "temporary J6 BMC UART debug wiring"},
- {"name": "debug_uart(rx,tx)", "count": 2, "reason": "temporary COM7 P10/R10 debug UART"},
- {"name": "host_uart(rx,tx)", "count": 2, "reason": "temporary COM8 P7/P8 host-control UART"},
- ]
- def utc_now() -> str:
- return datetime.now(timezone.utc).isoformat(timespec="seconds").replace("+00:00", "Z")
- def load_pin_inventory(path: Path | str) -> dict[str, Any]:
- inventory_path = Path(path)
- return json.loads(inventory_path.read_text(encoding="utf-8"))
- def _available_gpio_pins(inventory: dict[str, Any]) -> list[dict[str, Any]]:
- pins = []
- for pin in inventory.get("pins", []):
- capabilities = set(pin.get("capability") or [])
- role = pin.get("role")
- if pin.get("reserved"):
- continue
- if role not in {"user_io", "HEAT", "FAN_PWM", "FAN_FG", "FAN_INSTALL"}:
- continue
- if "gpio" not in capabilities:
- continue
- pins.append(pin)
- return pins
- def build_io_budget(inventory: dict[str, Any]) -> dict[str, Any]:
- required_total = sum(int(item["count"]) for item in REQUIRED_IO_GROUPS)
- available_gpio = _available_gpio_pins(inventory)
- verified_gpio = [pin for pin in available_gpio if pin.get("manually_verified") is True]
- required_groups = {item["name"]: {"count": item["count"], "reason": item["reason"]} for item in REQUIRED_IO_GROUPS}
- locked_conflicts = check_locked_uart_conflicts(inventory)
- if inventory.get("status") == "product_pin_map_missing":
- status = "blocked_product_pin_map_missing"
- elif locked_conflicts["status"] != "pass":
- status = locked_conflicts["status"]
- elif len(verified_gpio) < required_total:
- status = "blocked_unverified_or_insufficient_io"
- else:
- status = "pass"
- return {
- "schema": "cpld-io-budget-v1",
- "generated_at": utc_now(),
- "board_id": inventory.get("board_id"),
- "status": status,
- "required_groups": required_groups,
- "required_signals_total": required_total,
- "available_gpio_count": len(available_gpio),
- "verified_gpio_count": len(verified_gpio),
- "product_thermal_gate_allowed": bool(inventory.get("product_thermal_gate_allowed")),
- "locked_uart_conflicts": locked_conflicts["conflicts"],
- "authority_boundary": "Budget proves only signal count feasibility. It does not prove connector orientation, voltage, current drive, or product thermal safety.",
- }
- def real_fan_ptc_assignments(inventory: dict[str, Any]) -> dict[str, str]:
- assignments: dict[str, str] = {}
- for pin in inventory.get("pins", []):
- signal = str(pin.get("signal") or "")
- ball = str(pin.get("fpga_ball") or "")
- if not signal or not ball:
- continue
- role = str(pin.get("role") or "")
- channel = int(pin.get("channel") or 0)
- if role == "HEAT" and 1 <= channel <= 24:
- assignments[f"ptc_pwm_out[{channel - 1}]"] = ball
- elif role == "FAN_PWM" and 1 <= channel <= 4:
- assignments[f"fan_pwm_out[{channel - 1}]"] = ball
- elif role == "FAN_FG" and 1 <= channel <= 8:
- assignments[f"fan_fg_in[{channel - 1}]"] = ball
- elif role == "FAN_INSTALL" and 1 <= channel <= 4:
- assignments[f"fan_install_in[{channel - 1}]"] = ball
- return dict(sorted(assignments.items()))
- def check_locked_uart_conflicts(inventory: dict[str, Any]) -> dict[str, Any]:
- locked_by_ball = {ball: signal for signal, ball in LOCKED_DEBUG_UART_ASSIGNMENTS.items()}
- conflicts: list[dict[str, Any]] = []
- for pin in inventory.get("pins", []):
- ball = str(pin.get("fpga_ball") or "")
- if ball not in locked_by_ball:
- continue
- role = str(pin.get("role") or "")
- if role not in {"HEAT", "FAN_PWM", "FAN_FG", "FAN_INSTALL"}:
- continue
- conflicts.append(
- {
- "function_net": pin.get("function_net") or pin.get("signal"),
- "signal": pin.get("signal"),
- "role": role,
- "channel": pin.get("channel"),
- "fpga_ball": ball,
- "locked_signal": locked_by_ball[ball],
- "bank_io_net": pin.get("bank_io_net"),
- "source_pages": pin.get("source_pages"),
- }
- )
- return {
- "schema": "cpld-locked-uart-conflict-check-v1",
- "generated_at": utc_now(),
- "board_id": inventory.get("board_id"),
- "locked_uart_assignments": LOCKED_DEBUG_UART_ASSIGNMENTS,
- "status": "blocked_pin_conflict_with_locked_debug_uart" if conflicts else "pass",
- "conflicts": conflicts,
- "authority_boundary": "A real FAN/PTC bitstream cannot bind a schematic function and a locked temporary UART to the same FPGA ball.",
- }
- def _pin_by_ball(inventory: dict[str, Any]) -> dict[str, dict[str, Any]]:
- return {str(pin.get("fpga_ball")): pin for pin in inventory.get("pins", []) if pin.get("fpga_ball")}
- def _pin_by_signal(inventory: dict[str, Any]) -> dict[str, dict[str, Any]]:
- return {str(pin.get("signal")): pin for pin in inventory.get("pins", []) if pin.get("signal")}
- def _pin_ref(pin: dict[str, Any] | None, *, role: str | None = None) -> dict[str, Any]:
- if pin is None:
- return {"status": "missing"}
- payload: dict[str, Any] = {
- "signal": pin.get("signal"),
- "function_net": pin.get("function_net"),
- "bank_io_net": pin.get("bank_io_net"),
- "fpga_ball": pin.get("fpga_ball"),
- "role": role or pin.get("role"),
- "direction": pin.get("direction"),
- "io_standard": pin.get("io_standard"),
- "source_pages": pin.get("source_pages"),
- "manually_verified": pin.get("manually_verified") is True,
- "reserved": pin.get("reserved") is True,
- }
- if pin.get("capability") is not None:
- payload["capability"] = pin.get("capability")
- return payload
- def _reserved_future_uart(inventory: dict[str, Any]) -> dict[str, dict[str, Any]]:
- pins_by_signal = _pin_by_signal(inventory)
- return {name: _pin_ref(pins_by_signal.get(name), role="reserved_future_uart") for name in RESERVED_FUTURE_UART_SIGNALS}
- def _uart_assignment_rows(
- inventory: dict[str, Any],
- assignments: dict[str, str],
- *,
- aliases: dict[str, str] | None = None,
- ) -> dict[str, dict[str, Any]]:
- pins_by_ball = _pin_by_ball(inventory)
- rows: dict[str, dict[str, Any]] = {}
- for signal, ball in assignments.items():
- pin = pins_by_ball.get(ball)
- rows[signal] = _pin_ref(pin, role="uart")
- rows[signal]["fpga_ball"] = ball
- for alias, signal in (aliases or {}).items():
- if signal not in assignments:
- continue
- ball = assignments[signal]
- pin = pins_by_ball.get(ball)
- rows[alias] = _pin_ref(pin, role="uart")
- rows[alias]["fpga_ball"] = ball
- rows[alias]["hdl_signal"] = signal
- return rows
- def _ptc_substitution_rows(
- inventory: dict[str, Any],
- assignments: dict[str, str],
- *,
- reason: str = "temporary_uart_overlap_remap",
- ) -> dict[str, dict[str, Any]]:
- real_assignments = real_fan_ptc_assignments(inventory)
- pins_by_ball = _pin_by_ball(inventory)
- rows: dict[str, dict[str, Any]] = {}
- for label, subst in TEMP_UART_PTC_REMAP_SUBSTITUTIONS.items():
- signal = subst["signal"]
- original_ball = real_assignments.get(signal)
- substitute_ball = subst["substitute_ball"]
- rows[label] = {
- "signal": signal,
- "function_net": f"HEAT{int(label[3:])}",
- "original_ball": original_ball,
- "substitute_ball": substitute_ball,
- "substitute_bank_io_net": subst["bank_io_net"],
- "active_ball": assignments.get(signal),
- "substitute_pin": _pin_ref(pins_by_ball.get(substitute_ball), role="HEAT_substitute"),
- "reason": reason,
- }
- return rows
- def diagnostic_profile_assignments(inventory: dict[str, Any], profile: str) -> dict[str, Any]:
- """Return complete board assignments for the active diagnostic profiles."""
- supported_profiles = {
- "temp-uart-ptc-remap",
- "formal-uart",
- "debug-photo-uart-bmc-replay",
- "formal-uart1-bmc-replay",
- "debug-photo-bmc-replay-binary921",
- "formal-uart1-bmc-replay-binary921",
- "production-two-uart-bmc-replay-binary921",
- "prod2u-bmc-replay-binary921",
- "prod2u-fan-tach-binary921-v2",
- }
- if profile not in supported_profiles:
- return {
- "schema": "cpld-diagnostic-pin-profile-v1",
- "generated_at": utc_now(),
- "board_id": inventory.get("board_id"),
- "profile": profile,
- "status": "fail_unknown_profile",
- "assignments": {},
- "errors": [f"unknown_profile:{profile}"],
- }
- assignments = real_fan_ptc_assignments(inventory)
- bmc_source = "live_bmc_j6_uart"
- live_bmc_link_status = "used"
- hardware_access_status = "not_run"
- reserved_uart_use_policy = "reserved_unused"
- uart_aliases: dict[str, str] = {}
- allowed_reserved_balls: set[str] = set()
- profile4_fan_tach = profile == "prod2u-fan-tach-binary921-v2"
- production_profile = profile in {
- "production-two-uart-bmc-replay-binary921",
- "prod2u-bmc-replay-binary921",
- "prod2u-fan-tach-binary921-v2",
- }
- if profile in {"temp-uart-ptc-remap", "debug-photo-uart-bmc-replay", "debug-photo-bmc-replay-binary921"}:
- uart_assignments = dict(LOCKED_DEBUG_UART_ASSIGNMENTS)
- for item in TEMP_UART_PTC_REMAP_SUBSTITUTIONS.values():
- assignments[item["signal"]] = item["substitute_ball"]
- substitution_reason = (
- "photo_visible_uart_overlap_remap"
- if profile in {"debug-photo-uart-bmc-replay", "debug-photo-bmc-replay-binary921"}
- else "temporary_uart_overlap_remap"
- )
- ptc_substitutions = _ptc_substitution_rows(inventory, assignments, reason=substitution_reason)
- hardware_status = "not_run"
- if profile in {"debug-photo-uart-bmc-replay", "debug-photo-bmc-replay-binary921"}:
- bmc_source = "pc_replay_from_real_bmc_capture"
- live_bmc_link_status = "not_used"
- hardware_access_status = "photo_visible_header"
- uart_aliases = {
- "bmc_replay_uart_tx": "bmc_uart_tx",
- "bmc_replay_uart_rx": "bmc_uart_rx",
- }
- if profile == "debug-photo-bmc-replay-binary921":
- boundary = (
- "BMC-replay debug-photo binary921 profile: photo-visible/current J6/COM7/COM8 UART wiring is kept, "
- "the CPLD BMC UART talks to a PC poll-response replay emulator instead of a live BMC, "
- "HostPort and DebugPort use binary921 machine frames, and six overlapping HEAT/PTC channels are moved to verified spare GPIO. "
- "It is debug-only and does not claim full real HEAT pinout pass."
- )
- else:
- boundary = (
- "BMC-replay debug-photo profile: photo-visible/current J6/COM7/COM8 UART wiring is kept, "
- "the CPLD BMC UART talks to a PC poll-response replay emulator instead of a live BMC, "
- "and six overlapping HEAT/PTC channels are moved to verified spare GPIO. "
- "It is debug-only and does not claim full real HEAT pinout pass."
- )
- else:
- boundary = (
- "Temporary debug pin profile: current J6/COM7/COM8 UART wiring is kept, "
- "and six overlapping HEAT/PTC channels are moved to verified spare GPIO for signal-level diagnostic use."
- )
- elif profile == "formal-uart":
- uart_assignments = dict(FORMAL_UART_ASSIGNMENTS)
- ptc_substitutions = {}
- hardware_status = "not_run"
- boundary = (
- "Formal debug pin profile: all HEAT/PTC/FAN/FG/INSTALL pins use the schematic mapping, "
- "and all three diagnostic UARTs move to verified spare GPIO; schematic UART1/2 are reserved unused."
- )
- else:
- if production_profile:
- uart_assignments = dict(PRODUCTION_TWO_UART_BMC_REPLAY_BINARY921_ASSIGNMENTS)
- elif profile == "formal-uart1-bmc-replay-binary921":
- uart_assignments = dict(FORMAL_UART1_BMC_REPLAY_BINARY921_ASSIGNMENTS)
- else:
- uart_assignments = dict(FORMAL_UART1_BMC_REPLAY_ASSIGNMENTS)
- ptc_substitutions = {}
- hardware_status = "not_run"
- bmc_source = "pc_replay_from_real_bmc_capture"
- live_bmc_link_status = "not_used"
- hardware_access_status = (
- "production_uart1_uart2_access_confirmed_by_user"
- if production_profile
- else "blocked_waiting_for_uart1_uart2_access"
- )
- reserved_uart_use_policy = "intentional_uart1_uart2_bmc_replay_host"
- uart_aliases = {
- "bmc_replay_uart_tx": "bmc_uart_tx",
- "bmc_replay_uart_rx": "bmc_uart_rx",
- }
- if production_profile:
- uart_aliases.update(
- {
- "combined_debug_host_uart_tx": "host_uart_tx",
- "combined_debug_host_uart_rx": "host_uart_rx",
- }
- )
- if profile4_fan_tach:
- bmc_source = "out_of_scope"
- live_bmc_link_status = "out_of_scope"
- reserved_uart_use_policy = "intentional_uart1_idle_uart2_host"
- uart_aliases = {
- "idle_uart1_tx": "bmc_uart_tx",
- "ignored_uart1_rx": "bmc_uart_rx",
- "combined_debug_host_uart_tx": "host_uart_tx",
- "combined_debug_host_uart_rx": "host_uart_rx",
- }
- allowed_reserved_balls = {"L10", "M9", "M10", "N11"}
- if profile4_fan_tach:
- boundary = (
- "Prod2U Profile 4 production pin profile: HEAT/FAN/FG/INSTALL stay on the PDF/inventory pins, "
- "UART2 M10/N11 is the combined Host+Debug binary921-v2 port, and UART1 L10/M9 is retained only "
- "as an idle TX and ignored RX electrical boundary. BMC is out of scope. J3 is PROGRAM_SRAM-only. "
- "It does not qualify RPM calibration, PWM electrical accuracy, PTC thermal performance, PID, flash, or nonvolatile programming."
- )
- elif production_profile:
- boundary = (
- "Production two-UART BMC-replay binary921 profile for J3 SRAM-only bring-up: HEAT/FAN/FG/INSTALL stay on PDF/inventory pins, "
- "BMC replay uses UART1 L10/M9 at 115200, and a combined Host+Debug binary921 port uses UART2 M10/N11 at 921600. "
- "The board is programmed through J3 with target board power present and PROGRAM_SRAM only; VREF is a reference, not board power. "
- "It does not claim live BMC, flash, thermal performance, PID closed-loop, or real PTC/FAN load pass."
- )
- elif profile == "formal-uart1-bmc-replay-binary921":
- boundary = (
- "BMC-replay formal-uart1 binary921 profile: HEAT/FAN/FG/INSTALL stay on the schematic pins, "
- "BMC replay uses UART1 L10/M9, HostPort uses UART2 M10/N11, DebugPort uses M14/M15 spare GPIO, "
- "and HostPort/DebugPort use binary921 machine frames. "
- "Photo evidence did not prove UART1/2 are physically accessible, so hardware access remains blocked until continuity/header access is verified."
- )
- else:
- boundary = (
- "BMC-replay formal-uart1 profile: HEAT/FAN/FG/INSTALL stay on the schematic pins, "
- "BMC replay uses UART1 L10/M9, HostPort uses UART2 M10/N11, and DebugPort uses SPARE_GPIO44/45 "
- "as TX=N14/RX=P15. "
- "Photo evidence did not prove UART1/2 are physically accessible, so hardware access remains blocked until continuity/header access is verified."
- )
- assignments.update(uart_assignments)
- conflict = check_pin_conflicts(inventory, assignments, allowed_reserved_balls=allowed_reserved_balls)
- reserved_future_uart = _reserved_future_uart(inventory)
- used_balls = {str(ball) for ball in assignments.values()}
- reserved_balls = {
- str(item.get("fpga_ball"))
- for item in reserved_future_uart.values()
- if item.get("fpga_ball")
- }
- uses_reserved_future_uart = bool(used_balls & reserved_balls)
- errors = list(conflict.get("errors") or [])
- allowed_reserved_policies = {
- "intentional_uart1_uart2_bmc_replay_host",
- "intentional_uart1_idle_uart2_host",
- }
- if uses_reserved_future_uart and reserved_uart_use_policy not in allowed_reserved_policies:
- errors.append("uses_reserved_future_uart")
- status = "pass" if conflict["status"] == "pass" and not errors else "fail"
- return {
- "schema": "cpld-diagnostic-pin-profile-v1",
- "generated_at": utc_now(),
- "board_id": inventory.get("board_id"),
- "profile": profile,
- "status": status,
- "assignments": dict(sorted(assignments.items())),
- "uart_assignments": _uart_assignment_rows(inventory, uart_assignments, aliases=uart_aliases),
- "ptc_substitutions": ptc_substitutions,
- "reserved_future_uart": reserved_future_uart,
- "uses_reserved_future_uart": uses_reserved_future_uart,
- "reserved_uart_use_policy": reserved_uart_use_policy,
- "bmc_source": bmc_source,
- "live_bmc_link_status": live_bmc_link_status,
- "transport": "combined_host_debug_uart" if production_profile else "separate_host_debug_uart",
- "hardware_access_status": hardware_access_status,
- "real_fan_ptc_hardware_status": hardware_status,
- "conflict_check": conflict,
- "errors": sorted(set(errors)),
- "authority_boundary": boundary,
- }
- def check_pin_conflicts(
- inventory: dict[str, Any],
- assignments: dict[str, str],
- *,
- allowed_reserved_balls: set[str] | None = None,
- ) -> dict[str, Any]:
- pins = _pin_by_ball(inventory)
- allowed_reserved_balls = allowed_reserved_balls or set()
- seen: dict[str, str] = {}
- errors: list[str] = []
- warnings: list[str] = []
- assignment_rows: list[dict[str, Any]] = []
- for signal, ball in assignments.items():
- ball_text = str(ball)
- pin = pins.get(ball_text)
- if ball_text in seen:
- errors.append(f"duplicate_ball:{ball_text}")
- seen[ball_text] = signal
- if pin is None:
- errors.append(f"unknown_ball:{ball_text}")
- assignment_rows.append({"signal": signal, "fpga_ball": ball_text, "status": "unknown_ball"})
- continue
- capabilities = set(pin.get("capability") or [])
- if (
- (pin.get("reserved") and ball_text not in allowed_reserved_balls)
- or pin.get("role") not in {"user_io", "HEAT", "FAN_PWM", "FAN_FG", "FAN_INSTALL"}
- or "gpio" not in capabilities
- ):
- errors.append(f"reserved_or_not_gpio:{ball_text}")
- if pin.get("manually_verified") is not True:
- warnings.append(f"unverified_pin:{ball_text}")
- assignment_rows.append(
- {
- "signal": signal,
- "fpga_ball": ball_text,
- "connector": pin.get("connector"),
- "connector_pin": pin.get("connector_pin"),
- "io_standard": pin.get("io_standard"),
- "manually_verified": pin.get("manually_verified") is True,
- }
- )
- return {
- "schema": "cpld-pin-conflict-check-v1",
- "generated_at": utc_now(),
- "board_id": inventory.get("board_id"),
- "status": "fail" if errors else "pass",
- "assignments": assignment_rows,
- "errors": sorted(set(errors)),
- "warnings": sorted(set(warnings)),
- }
- def generate_adc(
- inventory: dict[str, Any],
- *,
- assignments: dict[str, str],
- output: Path | str,
- allow_unverified: bool = False,
- ) -> dict[str, Any]:
- conflict = check_pin_conflicts(inventory, assignments)
- if conflict["errors"]:
- return {"schema": "cpld-adc-generation-v1", "status": "fail_pin_conflict", "conflict": conflict}
- if conflict["warnings"] and not allow_unverified:
- return {"schema": "cpld-adc-generation-v1", "status": "blocked_unverified_pin_map", "conflict": conflict}
- pins = _pin_by_ball(inventory)
- lines = [
- "# Generated by tools/pin_inventory.py",
- f"# Board: {inventory.get('board_id')}",
- ]
- if conflict["warnings"]:
- lines.append("# LAB_SMOKE_UNVERIFIED_PIN_MAP: connector orientation and schematic cross-check are still required.")
- for signal, ball in assignments.items():
- pin = pins[str(ball)]
- io_standard = pin.get("io_standard") or "LVCMOS33"
- lines.append(f"set_pin_assignment {{ {signal} }} {{ LOCATION = {ball}; IOSTANDARD = {io_standard}; PULLTYPE = NONE; }}")
- output_path = Path(output)
- output_path.parent.mkdir(parents=True, exist_ok=True)
- output_path.write_text("\n".join(lines) + "\n", encoding="utf-8")
- return {
- "schema": "cpld-adc-generation-v1",
- "status": "pass",
- "path": str(output_path),
- "conflict": conflict,
- "allow_unverified": allow_unverified,
- "authority_boundary": "Generated ADC is a candidate lab-smoke constraint only unless all pins are manually verified and product gate is explicitly enabled.",
- }
- def _parse_assignments(items: list[str]) -> dict[str, str]:
- assignments: dict[str, str] = {}
- for item in items:
- if "=" not in item:
- raise ValueError(f"assignment must be signal=BALL: {item}")
- signal, ball = item.split("=", 1)
- assignments[signal.strip()] = ball.strip()
- return assignments
- def build_parser() -> argparse.ArgumentParser:
- parser = argparse.ArgumentParser(description=__doc__)
- sub = parser.add_subparsers(dest="command", required=True)
- budget = sub.add_parser("budget")
- budget.add_argument("--inventory", type=Path, default=ROOT / "config" / "pin_inventory.mini.json")
- budget.add_argument("--json", action="store_true")
- budget.set_defaults(func=cmd_budget)
- conflict = sub.add_parser("conflict")
- conflict.add_argument("--inventory", type=Path, default=ROOT / "config" / "pin_inventory.mini.json")
- conflict.add_argument("--assign", action="append", default=[])
- conflict.add_argument("--json", action="store_true")
- conflict.set_defaults(func=cmd_conflict)
- adc = sub.add_parser("generate-adc")
- adc.add_argument("--inventory", type=Path, default=ROOT / "config" / "pin_inventory.mini.json")
- adc.add_argument("--assign", action="append", default=[])
- adc.add_argument("--output", type=Path, required=True)
- adc.add_argument("--allow-unverified", action="store_true")
- adc.add_argument("--json", action="store_true")
- adc.set_defaults(func=cmd_generate_adc)
- return parser
- def cmd_budget(args: argparse.Namespace) -> int:
- payload = build_io_budget(load_pin_inventory(args.inventory))
- print(json.dumps(payload, indent=2) if args.json else f"pin-budget: {payload['status']}")
- return 0 if payload["status"] == "pass" else 1
- def cmd_conflict(args: argparse.Namespace) -> int:
- payload = check_pin_conflicts(load_pin_inventory(args.inventory), _parse_assignments(args.assign))
- print(json.dumps(payload, indent=2) if args.json else f"pin-conflict: {payload['status']}")
- return 0 if payload["status"] == "pass" else 1
- def cmd_generate_adc(args: argparse.Namespace) -> int:
- payload = generate_adc(
- load_pin_inventory(args.inventory),
- assignments=_parse_assignments(args.assign),
- output=args.output,
- allow_unverified=args.allow_unverified,
- )
- print(json.dumps(payload, indent=2) if args.json else f"generate-adc: {payload['status']}")
- return 0 if payload["status"] == "pass" else 1
- def main(argv: list[str] | None = None) -> int:
- return build_parser().parse_args(argv).func(build_parser().parse_args(argv))
- if __name__ == "__main__":
- parser = build_parser()
- parsed = parser.parse_args()
- raise SystemExit(parsed.func(parsed))
|