#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Trace the PlayStation 2's EE core (or its IOP) in MAME: a closed-box detector, tier 5. MAME 0.285's `ps2` machine (sony/ps2sony.cpp) is marked preliminary: it runs the BIOS's first instructions, not games. This script runs it headless, switches the debugger's instruction trace on for the CPU named by --cpu (MAME tags: `maincpu` is the R5900, "Emotion Engine Core (little)"; `iop` is the IOP; `vu0`/`vu1` the vector units) from reset or from frame --start, for --frames frames, and exits. Each line is MAME's own disassembly, prefixed with the registers named by --regs (MAME state symbols; see docs/mame-oracle.md). MAME_ROMPATH=... python -I scripts/mame_trace.py --bios scph10000_t --start 0 \ --frames 2 --out tests/mame_traces/ee-boot.trace The run's cwd is tests/mame_traces/ (git-ignored): error.log, cfg/, nvram/ land there. The BIOS comes from $MAME_ROMPATH (ps2.zip), never from the repository. One MAME process at a time, short windows only (the family's machine-sharing rules). """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] RUNS = ROOT / "tests" / "mame_traces" LUA = """ dbg = manager.machine.debugger if {start} == 0 then dbg:command('trace {out},{cpu},noloop{action}') end dbg:command('go') frames = 0 t0 = 0 sub = emu.add_machine_frame_notifier(function() frames = frames + 1 if frames == {start} and {start} > 0 then t0 = manager.machine.time:as_double() dbg:command('trace {out},{cpu},noloop{action}') end if frames == {stop} then dbg:command('trace off,{cpu}') print(string.format("WINDOW %.6f s", manager.machine.time:as_double() - t0)) manager.machine:exit() end end) """ STATE_LUA = """ for tag, dev in pairs(manager.machine.devices) do if dev.state ~= nil and (tag == ':maincpu' or tag == ':iop' or tag == ':vu0' or tag == ':vu1') then local names = {{}} for name, _ in pairs(dev.state) do names[#names + 1] = name end table.sort(names) print('STATE ' .. tag .. ' ' .. table.concat(names, ' ')) end end manager.machine:exit() """ def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--machine", default="ps2") parser.add_argument("--bios", default="scph10000_t") parser.add_argument("--cpu", default="maincpu", help="MAME device tag: maincpu, iop, vu0, vu1") parser.add_argument("--start", type=int, default=0, help="frame at which tracing starts (0 = reset)") parser.add_argument("--frames", type=int, default=2) parser.add_argument("--regs", default="", help="comma-separated MAME state symbols to prefix") parser.add_argument("--list-state", action="store_true", help="print each CPU's state symbols") parser.add_argument("--out", type=Path, default=RUNS / "trace.trace") 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() regs = [r for r in args.regs.split(",") if r] action = "" if regs: fmt = " ".join(f"{r}=%X" for r in regs) + " " action = ',{tracelog "' + fmt + '",' + ",".join(regs) + "}" script = RUNS / "trace.lua" body = STATE_LUA.format() if args.list_state else LUA.format( start=args.start, stop=args.start + args.frames, cpu=args.cpu, out=out.as_posix(), action=action) script.write_text(body, encoding="utf-8") command = [ "nice", args.mame, args.machine, "-bios", args.bios, "-rompath", rompath, "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-debug", "-debugger", "none", "-autoboot_script", str(script), "-seconds_to_run", str(max(5, (args.start + args.frames) // 50 + 5)), ] result = subprocess.run(command, cwd=RUNS, capture_output=True, text=True, check=False) for line in result.stdout.splitlines(): if line.startswith(("WINDOW", "STATE")): print(line) if args.list_state: return 0 if not out.exists(): print(result.stdout[-2000:], result.stderr[-2000:], sep="\n", file=sys.stderr) return 1 with out.open("rb") as handle: count = sum(1 for _ in handle) print(f"{count} lines traced to {out}") return 0 if __name__ == "__main__": sys.exit(main())