# SPDX-License-Identifier: GPL-3.0-or-later """snes-python's ``mesen_run.py``, as it was (``tools/snes-python/mesen_run.py`` loads this). Run a SNES ROM on a locally built Mesen2 core (MesenCore.so) headless and save its frame. python -I "$SNES_GPL_TOOLS/mesen_run.py" ROM.sfc --frames 300 --png out.png \ [--home DIR] [--timeout S] [--region auto|ntsc|pal] [--reset-at N] [--lib PATH] A Lua script counts frames and, at the last one, logs the frame buffer as hex; this turns it into a PNG (needs Pillow). Mesen2 frames are 256x239 (512x478 in hi-res/interlace), overscan included. Mesen2 prints an "Uninitialized memory read" line per power-on WRAM read; send it to /dev/null. Module names kept for snes-python's and gsu-python's scripts: ``ROOT``, ``LIBS``, ``DONE``, ``run``, ``snes_config``, ``save_png`` and the ``SNES_CONFIG_*`` offsets. First published under MIT by alewman in snes-python (``notices/MIT-emulator-family.txt``). """ from __future__ import annotations import argparse import os from pathlib import Path from ..lua import DONE, HEX_FRAME_LUA, hex_pixels # noqa: F401 from ..snes import ( # noqa: F401 REGIONS, SNES_CONFIG_BSX_DATE, SNES_CONFIG_CHANNEL_VOLUMES, SNES_CONFIG_GSU_SPEED, SNES_CONFIG_PORT_TYPES, SNES_CONFIG_RAM_POWER_ON, SNES_CONFIG_REGION, SNES_CONFIG_SIZE, SNES_CONTROLLER, SnesConfig, frame_size, run, snes_config, ) from ..api import CONSOLE_MODE, MAXIMUM_SPEED # noqa: F401 LUA = HEX_FRAME_LUA # The checkout comes from SNES_PYTHON_ROOT, which snes-python's scripts/gpl_tools.py sets, else # the working directory. ROOT = Path(os.environ.get("SNES_PYTHON_ROOT", Path.cwd())).resolve() LIBS = ( ROOT / "reference/build-mesen2/Mesen2/InteropDLL/obj.linux-x64/MesenCore.so", # A Mesen2 core built elsewhere (famicom-python builds one): MESEN_CORE=path. *([Path(os.environ["MESEN_CORE"])] if os.environ.get("MESEN_CORE") else []), ) def save_png(log: str, path: Path) -> tuple[int, int]: from PIL import Image count, data = hex_pixels(log) width, height = frame_size(count) Image.frombytes("RGB", (width, height), data).save(path) return width, height def main() -> int: p = argparse.ArgumentParser( description="Run a SNES ROM on a locally built Mesen2 core (MesenCore.so) headless and " "save its frame." ) p.add_argument("rom", type=Path) p.add_argument("--frames", type=int, default=300) p.add_argument("--png", type=Path, required=True) p.add_argument("--home", type=Path, default=ROOT / "tests/mame_traces/mesen-home") p.add_argument("--timeout", type=float, default=120.0, help="wall-clock seconds") p.add_argument( "--region", choices=sorted(REGIONS), help="the console (default: Mesen2's own, auto)" ) p.add_argument( "--reset-at", type=int, metavar="N", help="press the console's reset button at the end of frame N (frames count on)", ) p.add_argument( "--lib", type=Path, default=Path(os.environ["MESEN_CORE"]) if "MESEN_CORE" in os.environ else None, ) a = p.parse_args() lib = a.lib or next((x for x in LIBS if x.exists()), None) if lib is None: raise SystemExit("no MesenCore.so: build it (docs/validation.md) or pass --lib") log, wall = run(lib, a.rom, a.frames, a.home, a.timeout, region=a.region, reset_at=a.reset_at) if DONE not in log: print(f"timed out after {wall:.1f} s") return 1 width, height = save_png(log, a.png) print(f"{a.frames} frames in {wall:.1f} s: {a.png} ({width}x{height})") return 0