# SPDX-License-Identifier: MIT """Run the licensed mapper-19 cart and the mapper-559 pirate side by side. Both carts get the same button presses on the same frames; every 60 frames the script reports how many pixels differ, both PCs and both PPU masks, and every 300 frames it writes both pictures (when NES_PALETTE is set). This is the lockstep used for audit entry 16: the pirate is the original with 968 bytes patched, so a correct 559 model draws what the original draws, apart from the erased copyright text. set FAMICOM_PYTHON_SRC=/src set FAMICOM_DEPS=<6502-python>/src;... python compare_carts.py ORIGINAL.zip PIRATE.zip --frames 4400 \ --press 1900:START:8,2050:A:8 --out cmp/ \ [--install-module diff_mapper.py] [--machine famicom] Without --install-module, famicom-python's own Subor559 runs the pirate. ROMs are arguments; none is stored here. """ from __future__ import annotations import argparse import importlib.util import os import sys from pathlib import Path def load_famicom(): src = os.environ.get("FAMICOM_PYTHON_SRC") if not src: raise SystemExit("set FAMICOM_PYTHON_SRC to the famicom-python src directory") for path in [src, *os.environ.get("FAMICOM_DEPS", "").split(os.pathsep)]: if path: sys.path.insert(0, path) def install(path: str): spec = importlib.util.spec_from_file_location("mapper_experiment", path) if spec is None or spec.loader is None: raise SystemExit(f"cannot load {path}") module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) module.install() return module def parse_presses(text: str, joypad) -> dict[int, tuple[int, int]]: presses = {} for item in filter(None, text.split(",")): frame, button, hold = item.split(":") presses[int(frame)] = (getattr(joypad, button), int(hold)) return presses def main() -> None: ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) ap.add_argument("original") ap.add_argument("pirate") ap.add_argument("--frames", type=int, default=600) ap.add_argument("--press", default="", help="frame:BUTTON:hold,...") ap.add_argument("--out", type=Path, default=Path("cmp")) ap.add_argument("--machine", default="famicom") ap.add_argument("--install-module") args = ap.parse_args() load_famicom() if args.install_module: install(args.install_module) import famicom_python.joypad as joypad from famicom_python.machine import MACHINES from famicom_python.system import System from famicom_python.video import machine_palette, to_rgb, write_png machine = MACHINES[args.machine] palette_path = os.environ.get("NES_PALETTE") palette = machine_palette(machine.timing, palette_path) if palette_path else None args.out.mkdir(parents=True, exist_ok=True) a = System.from_rom(Path(args.original), machine) b = System.from_rom(Path(args.pirate), machine) print("mappers", a.loaded.image.board.mapper, b.loaded.image.board.mapper, flush=True) presses = parse_presses(args.press, joypad) releases: set[int] = set() for frame in range(1, args.frames + 1): if frame in presses: button, hold = presses[frame] for s in (a, b): s.board.pads[0].buttons = button releases.add(frame + hold) if frame in releases: for s in (a, b): s.board.pads[0].buttons = 0 a.run_frames(1) b.run_frames(1) if frame % 60 == 0 or frame == args.frames: pa, pb = a.board.ppu.picture, b.board.ppu.picture diff = sum(1 for x, y in zip(pa, pb) if x != y) print( f"{frame:5}f diff {diff:6} px pc {a.board.cpu.pc:04X}/{b.board.cpu.pc:04X}" f" mask {a.board.ppu.mask:02X}/{b.board.ppu.mask:02X}", flush=True, ) if palette and (frame % 300 == 0 or frame == args.frames): write_png(args.out / f"orig_{frame}.png", to_rgb(pa, palette)) write_png(args.out / f"pirate_{frame}.png", to_rgb(pb, palette)) if __name__ == "__main__": main()