#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Replay a Sim2600 die trace into the board's TIA and score every pin, every half clock. The TIA's judge (docs/validation.md "Die-derived"): tia-fpga's traces record, for each half colour clock, what the console drove into the TIA's pins and what the simulated 10444D drove back. This replays the inputs into :class:`a2600_python.tia.TIA` and compares its pins with the die's: PH0, RDY, SYNC, luminance, colour, blanking and the data-bus drivers. python scripts/die_trace.py reference/src/tia-fpga/sim/traces/phi2/playfield.trace **Writes** are decoded from the pins: a write lands on the record where CLK2 falls with the TIA selected and R/W low. With the console's wiring (the ``phi2`` traces) the record in which CLK2 falls already shows the next cycle's bus, while the die latched the old one, so the bus is held while CLK2 is low (tia-fpga sim/README.md "Which clock the TIA sees"). The wiring is detected from how CLK2 follows PH0. **Scoring** starts at the first HBLANK rise after record 2,000 (``--warm``), as tia-fpga's testbench does: a trace that starts at power-on begins inside the 6507's reset sequence, and the cartridges' first act is an RSYNC that sets the counter's phase. After that every record counts; one mismatch fails the trace. """ from __future__ import annotations import argparse import sys import time from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "src")) # The replay itself lives in the package, so other boards can run it on their # own TIA (gates.py, docs/api.md). from a2600_python.gates import COLUMNS, PINS, console_wiring # noqa: E402, F401 from a2600_python.gates import load_trace as load # noqa: E402 def replay(records, *, warm: int, audio: bool, report: int, stop_after: int | None, **kw): from a2600_python.gates import replay_trace return replay_trace(records, warm=warm, audio=audio, report=report, stop_after=stop_after, **kw) def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("traces", nargs="+", type=Path) parser.add_argument("--warm", type=int, default=2000) parser.add_argument("--audio", action="store_true", help="score AUD0/AUD1 too") parser.add_argument("--report", type=int, default=10, help="mismatching records to print") parser.add_argument("--stop-after", type=int, help="stop after this many printed mismatches") args = parser.parse_args() failed = 0 for path in args.traces: began = time.monotonic() records = load(path) print(f"{path.name}: {len(records):,} records") scored, mismatches, first, console = replay( records, warm=args.warm, audio=args.audio, report=args.report, stop_after=args.stop_after, ) # fmt: skip total = sum(mismatches.values()) wiring = "console" if console else "Sim2600" summary = ", ".join(f"{p} {n} (first {first[p]})" for p, n in mismatches.items() if n) verdict = "MATCH" if total == 0 else "MISMATCH" print( f" {verdict}: {scored:,} records scored, {wiring} wiring, " f"{time.monotonic() - began:.1f} s{'; ' + summary if summary else ''}" ) failed += total != 0 return 1 if failed else 0 if __name__ == "__main__": sys.exit(main())