#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Run a Taito F2 or F3 set in MAME headless as a closed-box detector. MAME is run, never read (docs/mame-oracle.md). For one machine this script: - counts every main-CPU write by 64 KiB region (address >> 16) for the whole run, through a Lua write tap on the main CPU's program space; - optionally traces CPUs for a short window of frames with the debugger's ``trace`` command (one file per CPU tag, no loop compression), so instructions per frame can be counted; - saves a screenshot at the last frame and exits. MAME_ROMPATH=... python -I scripts/mame_run.py cameltry --frames 600 MAME_ROMPATH=... python -I scripts/mame_run.py dariusg --frames 600 \\ --trace maincpu --trace taito_en:audiocpu --trace-from 590 --trace-frames 2 Everything lands in tests/mame_traces// (git-ignored): writes.txt, .tr, snap//0000.png, error.log. The sets come from $MAME_ROMPATH. One MAME process at a time; keep the window short (a few emulated seconds). """ from __future__ import annotations import argparse import os import subprocess import sys from collections import Counter from pathlib import Path ROOT = Path(__file__).resolve().parents[1] RUNS = ROOT / "tests" / "mame_traces" LUA = """ cpu = manager.machine.devices[":maincpu"] space = cpu.spaces["program"] dbg = manager.machine.debugger regions = {{}} tap = space:install_write_tap(0x0, {top}, "w", function(offset, data, mask) local r = offset >> 16 regions[r] = (regions[r] or 0) + 1 end) traces = {{{traces}}} frames = 0 sub = emu.add_machine_frame_notifier(function() frames = frames + 1 if frames == {trace_from} then for _, tag in ipairs(traces) do dbg:command("trace " .. tag:gsub(":", "_") .. ".tr," .. tag .. ",noloop") end end if frames == {trace_to} then for _, tag in ipairs(traces) do dbg:command("trace off," .. tag) end end if frames == {frames} then local out = io.open("writes.txt", "w") for r, n in pairs(regions) do out:write(string.format("%06X %d\\n", r << 16, n)) end out:close() manager.machine.video:snapshot() print("FRAMES " .. frames) manager.machine:exit() end end) """ def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("machine") parser.add_argument("--frames", type=int, default=600) parser.add_argument("--trace", action="append", default=[], help="CPU tag to trace") parser.add_argument("--trace-from", type=int, default=0, help="frame the trace starts") parser.add_argument("--trace-frames", type=int, default=1) parser.add_argument("--top", default="0xffffff", help="highest main-CPU address tapped") parser.add_argument("--mame", default="mame") args = parser.parse_args() rompath = os.environ.get("MAME_ROMPATH") if not rompath: print("set MAME_ROMPATH", file=sys.stderr) return 2 run = RUNS / args.machine run.mkdir(parents=True, exist_ok=True) script = run / "run.lua" traces = ", ".join(f'"{tag}"' for tag in args.trace) trace_from = args.trace_from if args.trace else -1 script.write_text( LUA.format( top=args.top, traces=traces, trace_from=trace_from, trace_to=trace_from + args.trace_frames, frames=args.frames, ), encoding="utf-8", ) command = [ "nice", args.mame, args.machine, "-rompath", rompath, "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-snapshot_directory", "snap", "-autoboot_script", str(script), "-seconds_to_run", str(args.frames // 50 + 10), ] if args.trace: command += ["-debug", "-debugger", "none"] result = subprocess.run(command, cwd=run, capture_output=True, text=True, check=False) if "FRAMES" not in result.stdout: print(result.stdout[-2000:], result.stderr[-2000:], sep="\n", file=sys.stderr) return 1 writes = Counter() for line in (run / "writes.txt").read_text(encoding="utf-8").splitlines(): region, count = line.split() writes[region] = int(count) print(f"{args.machine}: {args.frames} frames, {sum(writes.values())} main-CPU writes") for region, count in sorted(writes.items()): print(f" {region} {count}") for tag in args.trace: path = run / (tag.replace(":", "_") + ".tr") lines = sum(1 for _ in path.open("rb")) if path.exists() else 0 print(f" trace {tag}: {lines} lines over {args.trace_frames} frame(s) -> {path}") return 0 if __name__ == "__main__": sys.exit(main())