#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Run MAME's preliminary `ps2` machine headless and log the EE's writes to the board. A closed-box detector (tier 5). Installs a write tap on the EE's program space over the GS privileged registers (12000000h-12001FFFh) and the EE register block (10000000h- 1000FFFFh), runs --frames frames, writes one line per write (frame, address, value) to --out, prints a per-register count, and saves a screenshot at the end (tests/mame_traces/snap/). No debugger, so it runs much faster than a trace. MAME_ROMPATH=... python -I scripts/mame_run.py --bios scph10000_t --frames 120 The BIOS comes from $MAME_ROMPATH (ps2.zip). Runs in tests/mame_traces/ (git-ignored). """ from __future__ import annotations import argparse import collections import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] RUNS = ROOT / "tests" / "mame_traces" LUA = """ local cpu = manager.machine.devices[':maincpu'] local space = cpu.spaces['program'] out = io.open('{out}', 'w') frames = 0 local function log(offset, data, mask) out:write(string.format('%d %08X %016X %016X\\n', frames, offset, data, mask)) end tap_gs = space:install_write_tap(0x12000000, 0x12001fff, 'gs', function(o, d, m) log(o, d, m) end) tap_ee = space:install_write_tap(0x10000000, 0x1000ffff, 'ee', function(o, d, m) log(o, d, m) end) sub = emu.add_machine_frame_notifier(function() frames = frames + 1 if frames == {stop} then manager.machine.video:snapshot() out:close() manager.machine:exit() end end) """ def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--bios", default="scph10000_t") parser.add_argument("--frames", type=int, default=120) parser.add_argument("--out", type=Path, default=RUNS / "ee-writes.log") parser.add_argument("--mame", default="mame") args = parser.parse_args() rompath = os.environ.get("MAME_ROMPATH") if not rompath: print("set MAME_ROMPATH (MAME's rompath holding ps2.zip)", file=sys.stderr) return 2 RUNS.mkdir(parents=True, exist_ok=True) out = args.out.resolve() script = RUNS / "run.lua" script.write_text(LUA.format(out=out.as_posix(), stop=args.frames), encoding="utf-8") command = [ "nice", args.mame, "ps2", "-bios", args.bios, "-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), ] result = subprocess.run(command, cwd=RUNS, capture_output=True, text=True, check=False) if not out.exists(): print(result.stdout[-2000:], result.stderr[-2000:], sep="\n", file=sys.stderr) return 1 counts: collections.Counter[str] = collections.Counter() last_frame = 0 with out.open(encoding="utf-8") as handle: for line in handle: frame, address, _, _ = line.split() counts[address] += 1 last_frame = int(frame) total = sum(counts.values()) print(f"{total} writes in {args.frames} frames (last write in frame {last_frame}) to {out}") for address, count in sorted(counts.items()): print(f" {address} {count}") return 0 if __name__ == "__main__": sys.exit(main())