# SPDX-License-Identifier: MIT """Fit the 559 IRQ counter's timing against the licensed cart. Plays both carts to the map screen once (about 4,400 frames, scripted START/A presses), saves both states, then for each timing variant reloads them, runs 60 frames and scores the mean and worst differing pixels per frame. This produced the fit table in audit entry 16 (115 cycles: ~1,782 px/frame; 341/3: ~7). python fit_irq.py ORIGINAL.zip PIRATE.zip WORKDIR --sweep coarse|fine coarse: PRESCALE 113-116 x START_PHASE (0, last) x FIRE_AT (0, $FF). fine: PRESCALE 113.5 ... 114.5 around one scanline (341/3), phase 0, fire at 0. It patches the class settings of diff_mapper.Subor559, so it always runs the experiment here, not famicom-python's port. FAMICOM_PYTHON_SRC and FAMICOM_DEPS as for compare_carts.py. """ from __future__ import annotations import argparse import itertools import sys from pathlib import Path HERE = Path(__file__).resolve().parent sys.path.insert(0, str(HERE)) from compare_carts import install, load_famicom # noqa: E402 FINE = (113.5, 341 / 3, 113.75, 113.8333, 113.9, 114.0, 114.1, 114.25, 114.5) def variants(sweep: str): if sweep == "fine": for pre in FINE: yield {"PRESCALE": pre, "START_PHASE": 0, "FIRE_AT": 0} else: for pre, last, fire in itertools.product((113, 114, 115, 116), (False, True), (0, 0xFF)): yield {"PRESCALE": pre, "START_PHASE": pre - 1 if last else 0, "FIRE_AT": fire} def main() -> None: ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) ap.add_argument("original") ap.add_argument("pirate") ap.add_argument("work", type=Path) ap.add_argument("--sweep", choices=("coarse", "fine"), default="fine") args = ap.parse_args() load_famicom() experiment = install(str(HERE / "diff_mapper.py")) # Save states refuse classes outside the famicom_python package: register # the experiment class on a package module for this run only. import famicom_python.cartridge.banked as banked cls = experiment.Subor559 cls.__module__ = banked.__name__ banked.Subor559 = cls import famicom_python.joypad as joypad from famicom_python.machine import MACHINES from famicom_python.state import load_state, save_state from famicom_python.system import System machine = MACHINES["famicom"] args.work.mkdir(parents=True, exist_ok=True) a = System.from_rom(Path(args.original), machine) b = System.from_rom(Path(args.pirate), machine) sa, sb = args.work / "orig.state", args.work / "pirate.state" if not (sa.exists() and sb.exists()): presses = {f: joypad.START if (f // 150) % 3 == 0 else joypad.A for f in range(1900, 4201, 150)} releases = set() for frame in range(1, 4401): if frame in presses: for s in (a, b): s.board.pads[0].buttons = presses[frame] releases.add(frame + 8) if frame in releases: for s in (a, b): s.board.pads[0].buttons = 0 a.run_frames(1) b.run_frames(1) sa.write_bytes(save_state(a)) sb.write_bytes(save_state(b)) print("saved map-screen states", flush=True) da, db = sa.read_bytes(), sb.read_bytes() for settings in variants(args.sweep): for key, value in settings.items(): setattr(cls, key, value) load_state(a, da) load_state(b, db) total = worst = 0 for _ in range(60): a.run_frames(1) b.run_frames(1) diff = sum(1 for x, y in zip(a.board.ppu.picture, b.board.ppu.picture) if x != y) total += diff worst = max(worst, diff) print( f"PRESCALE {settings['PRESCALE']:9.4f} START_PHASE {settings['START_PHASE']:7.2f}" f" FIRE_AT {settings['FIRE_AT']:3}: mean {total / 60:8.1f} px/frame, worst {worst}", flush=True, ) if __name__ == "__main__": main()