"""The first picture where the board and NanoBoyAdvance 1.8.3 part ways on a set entry. python scripts/first_frame.py --set tests/games_set.txt "Super Mario Advance" [--out DIR] NanoBoyAdvance's driver writes every picture of the run (``--raw``); the board then runs the same keys a frame at a time and stops at the first picture that is not NanoBoyAdvance's. Both pictures are kept in ``--out``, with the frame before. A detector: it says where two models disagree, not which is right. ``--past N`` goes on N frames after the first difference and counts the pictures that differ, which tells a passing glitch from two runs a frame apart. The first pictures are ignored (``--start``): the two leave the BIOS skip with different blank screens. """ from __future__ import annotations import argparse import subprocess import sys import tempfile import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT / "scripts")) sys.path.insert(0, str(ROOT / "src")) import smoke # noqa: E402 from referee_run import bios_args # noqa: E402 from gba_python import bios as bios_module # noqa: E402 from gba_python.board import GBA # noqa: E402 from gba_python.testrom import read_rom, rgb_png, screen_rgb # noqa: E402 FRAME = 240 * 160 * 3 def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("text", help="the entry whose ROM has TEXT") parser.add_argument("--set", type=Path, default=ROOT / "tests" / "smoke_set.txt") parser.add_argument("--frames", type=int, help="how far to look (default: the last checkpoint)") parser.add_argument("--start", type=int, default=60, help="ignore pictures before N") parser.add_argument("--past", type=int, default=0, help="go on N frames after the first") parser.add_argument("--out", type=Path, help="keep the pictures here") args = parser.parse_args() entry = next(e for e in smoke.load_set(args.set) if args.text in e.rom) frames = args.frames or entry.frames path = smoke.rom_path(entry.rom) skip = entry.start == "skip" with tempfile.TemporaryDirectory() as scratch: rom = path if path.suffix == ".zip": with zipfile.ZipFile(path) as archive: name = next(n for n in archive.namelist() if n.lower().endswith(".gba")) rom = Path(scratch) / "rom.gba" rom.write_bytes(archive.read(name)) raw = Path(scratch) / "nba.raw" command = [str(smoke.NBA), str(rom)] command += bios_args(str(bios_module.find(None).source), Path(scratch)) command += ["--frames", str(frames), "--raw", str(raw)] if skip: command.append("--skip-bios") for key in entry.keys: command += ["--keys", key] subprocess.run(command, capture_output=True, check=True, timeout=1800) gba = GBA(bios_module.find(None), read_rom(path), skip_bios=skip, rtc_start=smoke.RTC_START) keys = {int(f): int(m, 0) for f, m in (k.split(":") for k in entry.keys)} first = 0 differing = 0 before = b"" with raw.open("rb") as theirs: for frame in range(1, frames + 1): if frame - 1 in keys: gba.keypad.set_pressed(keys[frame - 1]) gba.run_frame() ours, nba = screen_rgb(gba), theirs.read(FRAME) if ours != nba and frame >= args.start: differing += 1 if not first: first = frame pixels = sum(ours[i : i + 3] != nba[i : i + 3] for i in range(0, FRAME, 3)) print(f"{smoke.short(entry.rom)}: first differs at frame {frame}") print(f" {pixels} of 38,400 pixels; mode {gba.ppu.registers[0] & 7}") if args.out: args.out.mkdir(parents=True, exist_ok=True) stem = smoke.short(entry.rom).split(" (")[0].replace(" ", "_") for label, picture in ( (f"{frame - 1:05d}-before", before), (f"{frame:05d}-board", ours), (f"{frame:05d}-nba", nba), ): target = args.out / f"{stem}-{label}.png" target.write_bytes(rgb_png(240, 160, picture)) print(f" {target}") if first and frame >= first + args.past: break before = ours if not first: print(f"{smoke.short(entry.rom)}: the same picture for all {frames} frames") elif args.past: print(f" {differing} of the {args.past + 1} pictures from there differ") return 0 if __name__ == "__main__": raise SystemExit(main())