#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Record the rung 4 windows on Mesen2: host stream from power-on plus a Cx4 trace window. Each window is one mesen2-runner run (``$EMU_TOOLS_GPL/tools/mesen2/run.py --machine snes-cx4 --trace cx4-text,host``), one at a time under ``nice -n 19``, into ``tests/mesen_traces//`` (gitignored): ``host.txt.gz``, ``cx4.trace.gz``, the run's ``manifest.json`` and, for windows that press Start, the contract's input script ``input.jsonl``. ``tests/test_mesen_replay.py`` replays whatever windows are present. The cartridges are extracted from the No-Intro zips into a scratch directory first (Mesen2's ``LoadRom`` refuses a zip); nothing is kept but the recordings. python -I scripts/record_mesen_windows.py # all windows python -I scripts/record_mesen_windows.py x2-boot x3-title The windows (2026-11-07) and the Cx4 pages they reach. Each is ``--trace-window START:END``, which holds END minus START frames: in the trace's own frame column (``F:``, Mesen2's counter from 0) frames START to END - 1, **END excluded** (mesen2-runner README; OBSERVED in these recordings 2026-11-24: x2-test's ``F:`` column is 120 and 121, x2-play's 1800 to 1803). The host stream holds frames 1 to ``--frames``. - ``x2-boot``: Mega Man X2 (USA), ``0:120`` (``F:`` 0-119), no input: the boot's 0E:5C and 0E:89. - ``x2-test``: the built-in test (controller 2 B), ``120:122`` (``F:`` 120-121): 0Eh's DMA/sum loop. - ``x2-title``: Start at 900, ``1000:1002`` (``F:`` 1000-1001): page 3 (the title's 3-D logo) and page 0. - ``x2-play``: Start at 900-1900, ``1800:1804`` (``F:`` 1800-1803): the intro stage, page 0 (build_oam). - ``x3-title``: Mega Man X3 (USA), ``860:864`` (``F:`` 860-863), no input: pages 2 and 4. """ from __future__ import annotations import json import os import subprocess import sys import tempfile import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] #: The closed-box runner's documented path under EMU_TOOLS_GPL (emu-tools docs/contract.md, #: section 5): mesen2-runner (0.2.0: --input for snes-cx4), a GPL-3.0-or-later tool. RUNNER = "tools/mesen2/run.py" #: A press holds its button this many frames (the old harness's --press did the same). PRESS_FRAMES = 6 def mesen2_run() -> str: """The closed-box runner, a GPL tool in emu-tools-gpl, never part of this repository.""" root = os.environ.get("EMU_TOOLS_GPL") if not root or not (Path(root) / RUNNER).exists(): print(f"$EMU_TOOLS_GPL/{RUNNER} not found: set EMU_TOOLS_GPL (emu-tools docs/contract.md)", file=sys.stderr) # fmt: skip raise SystemExit(3) return str(Path(root) / RUNNER) def input_script(presses: list[tuple[int, str]]) -> str: """Pad 1 presses as the contract's input script: from FRAME completed frames the button is held, from FRAME + PRESS_FRAMES released. A record at FRAME gives the picture, Cx4 trace and host stream the old harness gave for ``--press FRAME:BUTTON`` (OBSERVED 2026-11-24, Mega Man X2's title, Mesen2 b9fa69d via mesen2-runner 0.2.0).""" records = [] for frame, button in presses: records.append({"frame": frame, "port": 1, "buttons": [button]}) records.append({"frame": frame + PRESS_FRAMES, "port": 1, "buttons": []}) return "".join(json.dumps(r) + "\n" for r in records) NO_INTRO = Path( os.environ.get("SNES_NO_INTRO") or Path(os.environ.get("NO_INTRO_DIR", "")) / "Nintendo - Super Nintendo Entertainment System" ) # the player's own No-Intro set: SNES_NO_INTRO, or NO_INTRO_DIR's SNES directory START_PRESSES = [(frame, "start") for frame in range(900, 2000, 200)] #: name: (game, run.py options, presses). --trace-window START:END traces frames START to #: END - 1 (the old harness's --trace-start START --trace-frames END - START). WINDOWS = { "x2-boot": ("Mega Man X2 (USA)", ["--frames", "120", "--trace-window", "0:120"], []), "x2-test": ("Mega Man X2 (USA)", ["--hold-test", "--frames", "200", "--trace-window", "120:122"], []), "x2-title": ("Mega Man X2 (USA)", ["--frames", "1010", "--trace-window", "1000:1002"], [(900, "start")]), "x2-play": ("Mega Man X2 (USA)", ["--frames", "1810", "--trace-window", "1800:1804"], START_PRESSES), "x3-title": ("Mega Man X3 (USA)", ["--frames", "870", "--trace-window", "860:864"], []), } # fmt: skip def main() -> int: names = sys.argv[1:] or list(WINDOWS) unknown = [n for n in names if n not in WINDOWS] if unknown: raise SystemExit(f"unknown windows {unknown}; known: {', '.join(WINDOWS)}") runner = mesen2_run() with tempfile.TemporaryDirectory(prefix="hg51b-carts-") as scratch: for name in names: game, options, presses = WINDOWS[name] with zipfile.ZipFile(NO_INTRO / f"{game}.zip") as archive: member = next(n for n in archive.namelist() if n.lower().endswith(".sfc")) cart = Path(scratch) / Path(member).name if not cart.exists(): cart.write_bytes(archive.read(member)) out = ROOT / "tests" / "mesen_traces" / name out.mkdir(parents=True, exist_ok=True) command = ["nice", "-n", "19", sys.executable, "-I", runner, "--machine", "snes-cx4", "--rom", str(cart), "--out", str(out), "--trace", "cx4-text,host", *options] # fmt: skip if presses: (out / "input.jsonl").write_text(input_script(presses)) command += ["--input", str(out / "input.jsonl")] print(f"{name}: {game} {' '.join(options)} presses {presses}", flush=True) subprocess.run(command, check=True) return 0 if __name__ == "__main__": sys.exit(main())