# SPDX-License-Identifier: GPL-3.0-or-later """hg51b-mesen-harness's ``mesen_cx4_trace.py`` (``tools/hg51b-python/mesen_cx4_trace.py`` loads this). Run a Cx4 cartridge on Mesen2 2.1.1 headless: the Cx4 test screen, and a per-instruction Cx4 trace (the ``cx4`` adapter says what each output holds). python -I "$HG51B_GPL_TOOLS/mesen_cx4_trace.py" CART.sfc [--test] [--frames N] \ [--trace-start N --trace-frames N] [--press FRAME:BUTTON ...] [--host] [--png PATH] \ [--out DIR] [--timeout S] [--lib PATH] The cartridge is a loose ``.sfc`` (this Mesen2 build's ``LoadRom`` refuses a ``.zip``). Output goes to ``--out``, else ``tests/mesen_traces//`` under the hg51b-python checkout (``HG51B_PYTHON_ROOT``, else the working directory; in the separate repository it was the folder above the harness's own). The library: ``--lib``, ``MESEN_CORE``, else a build in that checkout's ``reference/`` or in a sibling snes-python or famicom-python checkout. """ from __future__ import annotations import argparse import os from pathlib import Path from .. import cx4 from ..lua import hex_pixels from ..snes import frame_size ROOT = Path(os.environ.get("HG51B_PYTHON_ROOT", Path.cwd())).resolve() LIBS = tuple( base / "reference/build-mesen2/Mesen2/InteropDLL/obj.linux-x64/MesenCore.so" for base in (ROOT, ROOT.parent / "snes-python", ROOT.parent / "famicom-python") ) def save_png(log: str, path: Path) -> None: from PIL import Image # only for --png count, data = hex_pixels(log) width, _ = frame_size(count) Image.frombytes("RGB", (width, len(data) // 3 // width), data).save(path) def main() -> int: parser = argparse.ArgumentParser( description="Run a Cx4 cartridge on Mesen2 2.1.1 headless: the Cx4 test screen, and a " "per-instruction Cx4 trace." ) parser.add_argument("cart", type=Path) parser.add_argument("--test", action="store_true", help="hold controller 2 B from power-on") parser.add_argument("--frames", type=int, default=300, help="frames to run") parser.add_argument("--trace-start", type=int, default=0, help="first traced frame") parser.add_argument("--trace-frames", type=int, default=0, help="frames to trace (0: no trace)") parser.add_argument("--png", type=Path, help="save the last frame (needs Pillow)") parser.add_argument( "--press", action="append", default=[], metavar="FRAME:BUTTON", help="hold a controller 1 button (a b x y l r up down left right select start) 6 frames", ) parser.add_argument( "--host", action="store_true", help="record the S-CPU's $6000-$7FFF accesses (host.txt.gz)" ) parser.add_argument( "--out", type=Path, help="run directory (default tests/mesen_traces/)" ) parser.add_argument("--timeout", type=float, default=300.0, help="wall-clock seconds") parser.add_argument( "--lib", type=Path, default=Path(os.environ["MESEN_CORE"]) if "MESEN_CORE" in os.environ else None, ) args = parser.parse_args() lib = args.lib or next((x for x in LIBS if x.exists()), None) if lib is None: raise SystemExit( "no MesenCore.so: build Mesen2 b9fa69dd (snes-python docs/validation.md) or pass --lib" ) out = args.out or ROOT / "tests" / "mesen_traces" / args.cart.stem.replace(" ", "_") out.mkdir(parents=True, exist_ok=True) log, wall = cx4.run( lib, args.cart, out, test=args.test, frames=args.frames, trace_start=args.trace_start, trace_frames=args.trace_frames, press=args.press, host=args.host, timeout=args.timeout, ) print(f"{cx4.frame_of(log)} frames in {wall:.1f} s") lines = cx4.finish_trace(out) if lines is not None: print(f"{lines} Cx4 instructions traced: {out / 'cx4.trace.gz'}") if args.png and "PIXELS" in log: save_png(log, args.png) print(f"screen: {args.png}") elif "MESEN-DONE" not in log: print("timed out") return 1 return 0