#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Decode ale-79's serial test ROMs (Stella #699) from the board and compare with six consoles. The ROMs print ASCII tables of object positions on the left joystick port's pin 7: SWCHA D7 as an output, 57,600 baud 7N1, after the console's RESET switch is pressed (docs/validation.md "Machine-readable console results"). This runs a ROM on the board, presses RESET, records every SWCHA write with its CPU cycle, and decodes the pin the way a UART on it would: a start bit is the line falling, then each bit is sampled in its middle at the baud rate of the machine's own clock (20.7 CPU cycles per bit on NTSC, 20.5 on PAL), whatever the ROM's own loop takes (21 cycles, or 20 in the v2 ROMs, built for ale-79's slower PAL 7800). The consoles' tables are the judge, cell by cell: six consoles (PAL 7800, PAL Jr. '84 and '91, PAL light sixer, PAL Vader, SECAM woody), all with TIAs other than the 10444D the board models. So a cell where all six agree is a claim about every TIA and the board must match it; where they disagree the report says which consoles the board sides with (docs/undocumented-behavior.md C10: the RESxx/HMOVE race differs per console and per object). python scripts/serial_699.py ROM.bin RESULT.txt [RESULT.txt ...] [--machine pal] """ from __future__ import annotations import argparse import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "src")) from a2600_python import MACHINES # noqa: E402 from a2600_python.gates import compare_table as compare_one # noqa: E402 from a2600_python.gates import serial_decode as decode # noqa: E402 from a2600_python.gates import serial_record # noqa: E402 from a2600_python.gates import table_cells as cells # noqa: E402 def record(rom: bytes, machine: str, frames: int, idle_frames: int) -> tuple[list, float]: """(SWCHA D7 levels as (cycle, level), CPU cycles per bit) for the whole run.""" row = MACHINES[f"Atari VCS ({machine.upper()})"] return serial_record(rom, row, frames=frames, idle_frames=idle_frames) def compare(ours: str, results: dict[str, str]) -> int: ours_rows = cells(ours) tables = {name: cells(text) for name, text in results.items()} agreed = agreed_match = 0 split_report: dict[str, int] = dict.fromkeys(tables, 0) split = 0 misses = [] for r, row in enumerate(next(iter(tables.values()))): for c in range(len(row)): theirs = { n: t[r][c] if r < len(t) and c < len(t[r]) else None for n, t in tables.items() } mine = ours_rows[r][c] if r < len(ours_rows) and c < len(ours_rows[r]) else None if len(set(theirs.values())) == 1: agreed += 1 if mine == row[c]: agreed_match += 1 else: misses.append((r, c, mine, row[c])) else: split += 1 for n, v in theirs.items(): split_report[n] += v == mine print(f" cells where all {len(tables)} consoles agree: {agreed_match:,} of {agreed:,} match") for r, c, mine, theirs in misses[:10]: print(f" row {r} cell {c}: board {mine!r}, consoles {theirs!r}") if split: print(f" cells where the consoles disagree: {split:,}; the board sides with") for n, v in split_report.items(): print(f" {n}: {v:,}") return len(misses) def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("rom", type=Path) parser.add_argument("results", nargs="*", type=Path) parser.add_argument("--machine", default="ntsc", choices=("ntsc", "pal")) parser.add_argument("--frames", type=int, default=20000) parser.add_argument( "--idle-frames", type=int, default=30, help="stop after this many silent frames' worth" ) parser.add_argument("--out", type=Path, help="write the decoded text here") parser.add_argument( "--each", action="store_true", help="judge each console's table on its own, every cell" ) parser.add_argument( "--text", type=Path, help="compare a decode written earlier with --out instead of running" ) args = parser.parse_args() if args.text: text = args.text.read_text() else: levels, period = record(args.rom.read_bytes(), args.machine, args.frames, args.idle_frames) text = decode(levels, period) print(f"{args.rom.name}: {len(text):,} characters at {period:.2f} cycles a bit") if args.out: args.out.write_text(text) if not args.results: print(text) return 0 results = {p.stem: p.read_text(errors="replace") for p in args.results} if args.each: bad = 0 for name, table in results.items(): matched, total, misses = compare_one(text, table) bad += len(misses) print(f" {name}: {matched:,} of {total:,} cells match") for r, c, mine, theirs in misses[:8]: print(f" row {r} cell {c}: board {mine!r}, console {theirs!r}") return 1 if bad else 0 return 1 if compare(text, results) else 0 if __name__ == "__main__": sys.exit(main())