#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Milestone 2 (d): the board's game state against ProSystem's, frame by frame. ProSystem's system memory after every ``retro_run`` (``prosystem_run.py --ram-log``; the core is a black box) against the board, started the same way (the labelled direct start), sampled every 16 lines of each frame. For each ProSystem frame the board sample within +-3 frames with the fewest differing bytes of $1800-$27FF outside the stack page ($2100-$21FF: interrupt return addresses) is taken. A frame whose best sample is equal shows the same game state, however the picture differs (the two machines' frame edges fall at other points of a program's loop, which is why the board is sampled densely). python scripts/ram_prosystem.py ".../Centipede (USA).zip" --frames 600 Seeded fault: ``--fault shift`` looks 10 frames away; a title equal in every frame (Dig Dug) must then be equal in none. """ from __future__ import annotations import argparse import struct import subprocess import sys from collections import Counter from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "src")) from a7800_python.system import System # noqa: E402 STACK = range(0x900, 0xA00) # $2100-$21FF in the board's $1800-$27FF array def differing(a: bytes, b: bytes, limit: int | None = None) -> list[int]: out = [] for c in range(0, 0x1000, 64): if a[c : c + 64] != b[c : c + 64]: for i in range(c, c + 64): if a[i] != b[i] and i not in STACK: out.append(i) if limit is not None and len(out) > limit: return out return out def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("cart", type=Path) parser.add_argument("--frames", type=int, default=600) parser.add_argument( "--out", type=Path, default=ROOT / "tests" / "detector_runs" / "ram-prosystem" ) parser.add_argument("--fault", choices=["shift"]) args = parser.parse_args() frames = args.frames args.out.mkdir(parents=True, exist_ok=True) log = args.out / "prosystem-ram.bin" log.unlink(missing_ok=True) subprocess.run([sys.executable, str(ROOT / "scripts" / "prosystem_run.py"), str(args.cart), "--frames", str(frames + 4), "--every", "1000000", "--ram-log", str(log)], check=True, stdout=subprocess.DEVNULL) # fmt: skip data = log.read_bytes() size = len(data) // (frames + 4) - 4 theirs = {} for i in range(frames + 4): record = data[i * (size + 4) : (i + 1) * (size + 4)] theirs[struct.unpack("