# SPDX-License-Identifier: GPL-3.0-or-later """pcengine-python's ``mesen_run.py`` (``tools/pcengine-python/mesen_run.py`` loads this). Run a PC Engine/TurboGrafx-16/SuperGrafx ROM on a locally built Mesen2 core (MesenCore.so) headless and save its frame. python -I "$PCE_GPL_TOOLS/mesen_run.py" CARD.pce --frames 300 --png out.png \ [--home DIR] [--no-sprites] [--width N] [--press BUTTONS@FRAME+HOLD ...] \ [--timeout S] [--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). As parked on 2026-11-22 the file imported ``presses`` from beside itself, which was not there, so it stopped at import; the press spelling now comes from the ``pce`` adapter (a copy of pcengine-python's ``scripts/presses.py`` Mesen2 parts, MIT), and the default home lies under ``PCENGINE_PYTHON_ROOT`` (else the working directory) instead of beside this tool. First published under MIT by alewman in pcengine-python (``notices/MIT-emulator-family.txt``). """ from __future__ import annotations import argparse import os from pathlib import Path from ..api import CONSOLE_MODE, MAXIMUM_SPEED # noqa: F401 from ..lua import DONE, hex_pixels from ..pce import LUA, PceConfig, ambiguous_widths, frame_size, parse_presses, pce_config, run # noqa: F401 ROOT = Path(os.environ.get("PCENGINE_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, width: int = 0) -> tuple[int, int]: from PIL import Image count, data = hex_pixels(log) width, height = frame_size(count, width) if not width: raise SystemExit(f"{count} pixels fit widths {ambiguous_widths(count)}: pass --width") Image.frombytes("RGB", (width, height), data).save(path) return width, height def main() -> int: p = argparse.ArgumentParser( description="Run a PC Engine/TurboGrafx-16/SuperGrafx 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/runs/mesen-home") p.add_argument("--no-sprites", action="store_true", help="Mesen2 DisableSprites") p.add_argument("--width", type=int, default=0, help="frame width when it is ambiguous") p.add_argument( "--press", action="append", default=[], metavar="BUTTONS@FRAME+HOLD", help="pad 1 input (pcengine-python scripts/presses.py's spelling); connects a pad, " "which no --press leaves out", ) p.add_argument("--timeout", type=float, default=120.0, help="wall-clock seconds") 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, a.no_sprites, parse_presses(a.press)) if DONE not in log: print(f"timed out after {wall:.1f} s") return 1 width, height = save_png(log, a.png, a.width) print(f"{a.frames} frames in {wall:.1f} s: {a.png} ({width}x{height})") return 0