#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """The PAL comparison window, judged on the PAL BIOS alone (its built-in Asteroids). No cartridge: the PAL BIOS finds the slot empty and runs its own Asteroids. The board (empty slot, PAL row) and MAME 0.285 ``a7800p`` (no cartridge) run to the same frames; the board's frames are cut with television.py's PAL crop (lines 35-294) and compared with MAME's snapshots under one colour map, as ``frames_mame.py`` does. ``--crop-shift k`` moves the board's window k lines (the seeded fault: any shift must fail). python scripts/pal_bios_frame.py --frames 400 --every 50 [--crop-shift 1] """ from __future__ import annotations import argparse import subprocess import sys from collections import Counter, defaultdict 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 frames_mame import LUA, pairs, read_png # noqa: E402 from mame_trace import BIOS_DIR, MAME # noqa: E402 from a7800_python.cartridge.connector import Cartridge # noqa: E402 from a7800_python.machine import PAL # noqa: E402 from a7800_python.system import System # noqa: E402 from a7800_python.television import Television, crop_for # noqa: E402 def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--frames", type=int, default=400) parser.add_argument("--every", type=int, default=50) parser.add_argument("--crop-shift", type=int, default=0) args = parser.parse_args() out = ROOT / "tests" / "detector_runs" / "pal-bios-frame" snap = out / "snap" snap.mkdir(parents=True, exist_ok=True) for stale in snap.rglob("*.png"): stale.unlink() (out / "frames.lua").write_text(LUA.replace("EVERY", str(args.every)) .replace("LAST", str(args.frames))) # fmt: skip subprocess.run([MAME, "a7800p", "-rompath", BIOS_DIR, "-homepath", str(out), "-snapshot_directory", str(snap), "-snapname", "%i", "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-autoboot_script", str(out / "frames.lua")], cwd=out, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) # fmt: skip snaps = sorted(snap.rglob("*.png"), key=lambda p: int(p.stem) if p.stem.isdigit() else 0) system = System(Cartridge(), PAL) first, lines = crop_for(PAL) tv = Television(system.board.maria, keep=1, first_line=first + args.crop_shift, lines=lines) mine = {} while tv.count < args.frames: before = tv.count system.step() if tv.count != before and tv.count % args.every == 0: mine[tv.count] = tv.frames[-1] frames = {(i + 1) * args.every: read_png(p)[2] for i, p in enumerate(snaps)} votes: dict[int, Counter] = defaultdict(Counter) for n, rows in frames.items(): for byte, rgb in pairs(mine[n], rows, 1): votes[byte][rgb] += 1 mapping = {b: c.most_common(1)[0][0] for b, c in votes.items()} differing = 0 for n, rows in sorted(frames.items()): bad = sum(mapping[b] != rgb for b, rgb in pairs(mine[n], rows, 1)) differing += bad > 0 print(f"frame {n}: {bad} pixels differ ({len(rows)} MAME rows, {len(mine[n])} board rows)") print(f"PAL BIOS, no cartridge: {len(frames)} frames compared, {differing} differ " f"(crop lines {first + args.crop_shift}-{first + args.crop_shift + lines - 1})") return 0 if differing == 0 and frames else 1 if __name__ == "__main__": sys.exit(main())