# SPDX-License-Identifier: MIT """Run MAME 0.285's NGP/NGPC driver headless, as a closed-box detector (docs/mame-oracle.md). MAME_ROMPATH=... python -I scripts/mame_run.py --driver ngpc --frames 120 MAME_ROMPATH=... NO_INTRO_DIR=... python -I scripts/mame_run.py --cart "" Logs, one line each, into reference/mame/[-]/run.log (git-ignored): - "IO addr data cycles" for the main CPU's writes to its internal I/O window 0xB0-0xBF (the Z80 control, PSG/DAC and communication ports, ares and Judge's ngpctech.txt); - "K addr data cycles" for writes to the K1GE/K2GE registers 0x8000-0x80FF; - "Z pc data cycles" for the Z80's writes to the T6W28 ports 0x4000/0x4001; - "F n" at each frame, and a PNG snapshot at the end. The power button is held for frames 5-6 (the NGP waits for it after power-on). Without --cart the BIOS runs alone. A cartridge is passed from its No-Intro zip, read in place. With MAME_LOCK set, the run is wrapped in `flock $MAME_LOCK` (one MAME run of ours at a time). Watchpoint actions end with `g` so the run continues. """ from __future__ import annotations import argparse import os import re import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] def lua(frames: int) -> str: watch = [ (":maincpu", "b0", "10", "IO"), (":maincpu", "8000", "100", "K"), (":soundcpu", "4000", "2", "Z"), ] lines = ["local dbg = manager.machine.debugger"] for cpu, start, length, tag in watch: lines.append(f'dbg.visible_cpu = manager.machine.devices["{cpu}"]') what = "pc" if tag == "Z" else "wpaddr" lines.append( f"dbg:command('wpset {start},{length},w,1," f'{{logerror "{tag} %X %X %d\\\\n",{what},wpdata,totalcycles; g}}\')' ) lines += [ 'dbg:command("go")', "count = 0", "notifier = emu.add_machine_frame_notifier(function()", " count = count + 1", ' emu.print_verbose("F " .. count)', # The NGP halts at power-on until its power button is pressed (ares cpu.cpp's note; # MAME's ":Power" port): hold it for frames 5-6. ' local power = manager.machine.ioport.ports[":Power"].fields["Power On"]', " if count == 5 then power:set_value(1) end", " if count == 7 then power:set_value(0) end", f" if count == {frames} then", " manager.machine.video:snapshot()", " manager.machine:exit()", " end", "end)", ] return "\n".join(lines) + "\n" def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--driver", default="ngpc", choices=["ngp", "ngpc"]) parser.add_argument("--frames", type=int, default=120) parser.add_argument("--cart", help="a zip name in the No-Intro folder for --driver") parser.add_argument("--rompath", default=os.environ.get("MAME_ROMPATH")) parser.add_argument("--mame", default="mame") args = parser.parse_args() if not args.rompath: print("set MAME_ROMPATH (it must hold ngp.zip / ngpc.zip)", file=sys.stderr) return 2 name = args.driver extra: list[str] = [] if args.cart: folder = "SNK - NeoGeo Pocket Color" if args.driver == "ngpc" else "SNK - NeoGeo Pocket" cart = Path(os.environ.get("NO_INTRO_DIR", "")) / folder / args.cart if not cart.is_file(): print(f"no such cartridge zip under $NO_INTRO_DIR/{folder}", file=sys.stderr) return 2 extra = ["-cart", str(cart)] name += "-" + re.sub(r"[^A-Za-z0-9]+", "_", args.cart.split(" (")[0]).strip("_")[:24] out = ROOT / "reference" / "mame" / name out.mkdir(parents=True, exist_ok=True) script = out / "run.lua" script.write_text(lua(args.frames), encoding="utf-8") command = [ args.mame, args.driver, *extra, "-rompath", args.rompath, "-homepath", str(out), "-snapshot_directory", str(out / "snap"), "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-log", "-verbose", "-seconds_to_run", str(args.frames // 60 + 5), "-autoboot_script", str(script), "-debug", "-debugger", "none", ] # fmt: skip lock = os.environ.get("MAME_LOCK") prefix = ["flock", lock] if lock else [] result = subprocess.run([*prefix, "nice", "taskset", "-c", "24-31", *command], cwd=out, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) # fmt: skip frames = sum(1 for line in result.stdout.splitlines() if line.startswith("F ")) (out / "stdout.txt").write_text(result.stdout, encoding="utf-8") log = out / "error.log" if log.exists(): log.replace(out / "run.log") print(f"exit {result.returncode}; {frames} frames; output in {out.relative_to(ROOT)}") return result.returncode if __name__ == "__main__": sys.exit(main())