# SPDX-License-Identifier: MIT """Run a NES ROM headlessly with famicom-python and capture evidence. set FAMICOM_PYTHON_SRC=/src set FAMICOM_DEPS=<6502-python>/src;/src set NES_PALETTE=.pal python tools/run_rom.py "NO_INTRO_DIR/.../rom.zip" --frames 600 --out evidence/ Input can be scripted at a frame: --press 600:A:4,900:START:4 The ROM is never stored here. Paths come from the environment so the repository does not acquire a dependency on one machine. """ from __future__ import annotations import argparse import importlib.util import json import os import sys from pathlib import Path def _load_famicom() -> tuple: 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) from famicom_python.machine import MACHINES from famicom_python.system import System from famicom_python.video import machine_palette, to_rgb, write_png return MACHINES, System, machine_palette, to_rgb, write_png def _install(path: str) -> None: spec = importlib.util.spec_from_file_location("mapper_experiment", path) if spec is None or spec.loader is None: raise SystemExit(f"cannot load mapper experiment {path}") module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) install = getattr(module, "install", None) if install is None: raise SystemExit(f"{path} has no install()") install() def _parse_ranges(text: str) -> list[tuple[int, int]]: ranges = [] for part in text.split(","): if not part: continue lo, hi = part.split("-") ranges.append((int(lo, 16), int(hi, 16))) return ranges def _parse_presses(text: str) -> dict[int, tuple[int, int]]: import famicom_python.joypad as joypad presses = {} for part in text.split(","): if not part: continue frame, name, hold = part.split(":") button = getattr(joypad, name.upper(), None) if not isinstance(button, int): raise SystemExit(f"unknown button {name!r}") presses[int(frame)] = (button, int(hold)) return presses def main() -> None: ap = argparse.ArgumentParser() ap.add_argument("rom", type=Path) ap.add_argument("--frames", type=int, default=600) ap.add_argument("--shots", default="60,180,600", help="comma-separated frame numbers") ap.add_argument("--out", type=Path, default=Path("evidence")) ap.add_argument("--machine", default="auto") ap.add_argument("--install-module", help="a Python module with install() for an experimental mapper") ap.add_argument("--press", default="", help="frame:BUTTON:hold, e.g. 600:A:4") ap.add_argument("--trace", type=Path, help="write register writes as JSONL") ap.add_argument("--trace-ranges", default="4000-5FFF,8000-FFFF") ap.add_argument("--trace-skip", default="2007", help="comma-separated addresses to omit") a = ap.parse_args() MACHINES, System, machine_palette, to_rgb, write_png = _load_famicom() if a.install_module: _install(a.install_module) machine = MACHINES[a.machine] if a.machine != "auto" else System.machine_for(a.rom) system = System.from_rom(a.rom, machine) print(f"{a.rom.name}: mapper {system.loaded.image.board.mapper}, machine {machine.name}", flush=True) ranges = _parse_ranges(a.trace_ranges) skip = {int(x, 16) for x in a.trace_skip.split(",") if x} trace_file = None if a.trace: a.trace.parent.mkdir(parents=True, exist_ok=True) trace_file = a.trace.open("w", encoding="utf-8") def observer(kind: str, address: int, value: int, _t: int) -> None: if kind != "write" or address in skip: return if any(lo <= address <= hi for lo, hi in ranges): trace_file.write(json.dumps({"frame": system.board.ppu.frame, "address": f"{address:04X}", "value": f"{value:02X}"}) + "\n") system.board.bus_observers.append(observer) a.out.mkdir(parents=True, exist_ok=True) palette = None palette_path = os.environ.get("NES_PALETTE") if palette_path and Path(palette_path).exists(): palette = machine_palette(machine.timing, palette_path) shots = {int(x) for x in a.shots.split(",") if x} presses = _parse_presses(a.press) pad = system.board.pads[0] release_at = None for frame in range(1, a.frames + 1): if frame in presses: button, hold = presses[frame] pad.buttons = button release_at = frame + hold system.run_frames(1) if release_at is not None and frame >= release_at: pad.buttons = 0 release_at = None if frame in shots: path = a.out / f"{a.rom.stem}_{frame}f.png" if palette is not None: write_png(path, to_rgb(system.board.ppu.picture, palette)) print(f"{frame}f pc={system.board.cpu.pc:04X} mask={system.board.ppu.mask:02X} -> {path}", flush=True) else: print(f"{frame}f pc={system.board.cpu.pc:04X} mask={system.board.ppu.mask:02X} (no NES_PALETTE)", flush=True) print(f"{a.frames}f pc={system.board.cpu.pc:04X} mask={system.board.ppu.mask:02X}", flush=True) if trace_file is not None: trace_file.close() print(f"trace -> {a.trace}", flush=True) if __name__ == "__main__": main()