# SPDX-License-Identifier: MIT """Boot a 16K dump under both readings (one chip, two chips) and keep the pictures. Milestone 5 (c) and open decision 3: a 16K dump in no public table is run for N frames as ``16k-one`` and as ``16k-two``; for each reading the script reports whether the CPU jammed, whether it left the BIOS, and how many colours the last frame holds, and writes one PNG per dump with the two readings side by side (left one chip, right two chips; top row at N/2 frames, bottom row at N; one pixel per half colour clock, lines not doubled). A person looks at the picture and decides which reading reaches a sensible screen; the verdict goes into ``table.json``'s ``measured`` section as an "observed-boot" row with the PNG's path as evidence. python scripts/observed_boot.py --unknown --frames 900 --out DIR [--shard I/N] python scripts/observed_boot.py PATH [PATH ...] --frames 900 --out DIR ``--unknown`` takes the No-Intro rows of the Phase 0 census whose class is ``16k-?`` (tests/detectors/census-2026-11-11.tsv). Images are read in place; only PNGs and a TSV are written, under ``--out`` (keep it in an ignored run directory). """ from __future__ import annotations import argparse import struct import sys import time import zlib from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "src")) sys.path.insert(0, str(ROOT / "scripts")) from romsets import nointro_5200 # noqa: E402 from a5200_python.board import FRAME_LINES, FRAME_WIDTH # noqa: E402 from a5200_python.cartridge import load_cartridge # noqa: E402 from a5200_python.cartridge.walk import guess_16k # noqa: E402 from a5200_python.media import ntsc_palette # noqa: E402 from a5200_python.system import Console # noqa: E402 CENSUS = ROOT / "tests" / "detectors" / "census-2026-11-11.tsv" READINGS = ("16k-one", "16k-two") def unknown_dumps() -> list[Path]: nointro = nointro_5200() # $NO_INTRO_DIR/Atari - Atari 5200 paths = [] for line in CENSUS.read_text("utf-8").splitlines()[1:]: cols = line.split("\t") if len(cols) > 12 and cols[12] == "16k-?" and cols[0].startswith("Atari"): paths.append(nointro / cols[1]) return paths def boot(payload: bytes, reading: str, frames: int) -> tuple[str, list[bytes], int]: """(outcome, [frame at N/2, frame at N], colours in the last frame).""" cart = load_cartridge(payload, mapping=reading) console = Console(cart) board = console.board shots: list[bytes] = [] left_bios = False outcome = "" for n in range(1, frames + 1): console.run_frames(1) if 0x4000 <= board.cpu.pc < 0xC000: left_bios = True if n in (frames // 2, frames): shots.append(bytes(board.frame)) if board.cpu.halted: outcome = f"JAM at frame {board.frames}" break while len(shots) < 2: shots.append(bytes(board.frame)) if not outcome: outcome = "left BIOS" if left_bios else "still in BIOS" return outcome, shots, len(set(shots[-1])) def write_panel(path: Path, panels: list[list[bytes]]) -> None: """panels[row][column] frames, tiled, one pixel per value, lines not doubled.""" lut = [bytes(c) for c in ntsc_palette()] rows = bytearray() for row in panels: for y in range(FRAME_LINES): rows += b"\x00" for frame in row: rows += b"".join(lut[v] for v in frame[y * FRAME_WIDTH : (y + 1) * FRAME_WIDTH]) width, height = FRAME_WIDTH * len(panels[0]), FRAME_LINES * len(panels) def chunk(kind: bytes, data: bytes) -> bytes: return ( struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data)) ) header = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0) path.write_bytes( b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", header) + chunk(b"IDAT", zlib.compress(bytes(rows), 6)) + chunk(b"IEND", b"") ) def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("paths", nargs="*", type=Path) parser.add_argument("--unknown", action="store_true", help="the census's 16k-? No-Intro rows") parser.add_argument("--frames", type=int, default=900) parser.add_argument("--out", type=Path, required=True) parser.add_argument("--shard", default="0/1", help="I/N: every Nth dump from the Ith") parser.add_argument("--skip-done", action="store_true", help="skip dumps whose PNG exists") args = parser.parse_args() paths = list(args.paths) + (unknown_dumps() if args.unknown else []) shard, shards = (int(v) for v in args.shard.split("/")) args.out.mkdir(parents=True, exist_ok=True) tsv = args.out / f"observed-{shard}of{shards}{'-resumed' if args.skip_done else ''}.tsv" lines = ["file\tsha1\twalk\treading\toutcome\tcolours\tpng"] for path in paths[shard::shards]: start = time.perf_counter() png = args.out / f"{path.stem.replace(' ', '_')}.png" if args.skip_done and png.exists(): continue # The payload does not depend on the reading; naming one avoids the # loader refusing a dump the walk abstains on (U16). cart = load_cartridge(path, mapping="16k-one") guess, _detail = guess_16k(cart.payload) top: list[bytes] = [] bottom: list[bytes] = [] for reading in READINGS: outcome, shots, colours = boot(cart.payload, reading, args.frames) top.append(shots[0]) bottom.append(shots[1]) lines.append( f"{path.name}\t{cart.sha1}\t{guess or 'either'}\t{reading}\t{outcome}\t{colours}\t{png}" ) print(f"{path.name} [{reading}]: {outcome}; {colours} colours", flush=True) write_panel(png, [top, bottom]) print(f" wrote {png} [{time.perf_counter() - start:.0f}s]", flush=True) tsv.write_text("\n".join(lines) + "\n", encoding="utf-8") return 0 if __name__ == "__main__": raise SystemExit(main())