# SPDX-License-Identifier: MIT """Run MAME 0.285's SNK triple-Z80 driver headless, as a closed-box detector. python -I scripts/mame_trace.py --map # dump every CPU's address map python -I scripts/mame_trace.py --frames 3 # trace the main Z80 python -I scripts/mame_trace.py --frames 3 --cpu sub # ... or the sub or sound Z80 python -I scripts/mame_trace.py --snap 20 # no trace: run, save a PNG The romsets come from MAME_ROMPATH (MAME's non-merged sets: ikari.zip and the rest, read in place). Output goes to reference/mame/-/ (git-ignored): trace.log (one line per instruction, "PC: disassembly"), run.log (MAME's log, the watchpoint lines and the map dump), snap/ and stdout.txt. MAME is run, never read (docs/mame-oracle.md): the address maps come from the Lua API's view of the running machine, not from its source. Tags: ":maincpu", ":sub", ":audiocpu" (mame -listdevices ikari). """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] CPUS = {"main": ":maincpu", "sub": ":sub", "sound": ":audiocpu"} MAP_DUMP = """ for _, tag in ipairs({":maincpu", ":sub", ":audiocpu"}) do local dev = manager.machine.devices[tag] for sname, space in pairs(dev.spaces) do for _, e in ipairs(space.map.entries) do emu.print_error(string.format("MAP %s %s %04X-%04X mirror=%04X r=%s w=%s share=%s", tag, sname, e.address_start, e.address_end, e.address_mirror, e.read.handlertype, e.write.handlertype, tostring(e.share))) end end end """ def lua(cpu: str, frames: int, trace: bool, dump_map: bool, watch: str | None) -> str: lines = [] if dump_map: lines.append(MAP_DUMP) if trace: lines += [ "local dbg = manager.machine.debugger", f'dbg.visible_cpu = manager.machine.devices["{CPUS[cpu]}"]', # No action: in MAME 0.285 a {logerror ...} action on a Z80 trace printed stale # pc and totalcycles (repeated per timeslice); the bare trace is exact. f"dbg:command('trace Z.trace,{CPUS[cpu]},noloop')", ] if watch: start, length = watch.split(",") for tag in CPUS.values(): lines.append(f'dbg.visible_cpu = manager.machine.devices["{tag}"]') lines.append( f"dbg:command('wpset {start},{length},rw,1," f'{{logerror "W {tag} %X %X %X %X %d\\\\n",pc,wpaddr,wpdata,wpsize,totalcycles; g}}\')' ) lines.append('dbg:command("go")') lines += [ "count = 0", "notifier = emu.add_machine_frame_notifier(function()", " count = count + 1", 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("--set", default="ikari", help="ikari, victroad, psychos, athena, tnk3, ...") parser.add_argument("--cpu", default="main", choices=sorted(CPUS)) parser.add_argument("--frames", type=int, default=0, help="trace this many frames") parser.add_argument("--snap", type=float, default=0, help="untraced run of N seconds") parser.add_argument("--map", action="store_true", help="dump the address maps, then 1 frame") parser.add_argument("--watch", help="START,LENGTH (hex) watched on all three CPUs while tracing") 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 (MAME's non-merged sets)", file=sys.stderr) return 2 trace = args.frames > 0 frames = args.frames if trace else (1 if args.map else max(1, round(args.snap * 60))) mode = "map" if args.map and not trace else (args.cpu if trace else "snap") out = ROOT / "reference" / "mame" / f"{args.set}-{mode}" out.mkdir(parents=True, exist_ok=True) script = out / "run.lua" script.write_text(lua(args.cpu, frames, trace, args.map, args.watch), encoding="utf-8") command = [ args.mame, args.set, "-rompath", args.rompath, "-homepath", str(out), "-cfg_directory", str(out / "cfg"), "-nvram_directory", str(out / "nvram"), "-snapshot_directory", str(out / "snap"), "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-log", "-seconds_to_run", str(int(frames / 60) + 5), "-autoboot_script", str(script), ] # fmt: skip if trace: command += ["-debug", "-debugger", "none"] result = subprocess.run(["nice", "taskset", "-c", "24-31", *command], cwd=out, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) # fmt: skip (out / "stdout.txt").write_text(result.stdout, encoding="utf-8") log = out / "error.log" if log.exists(): log.replace(out / "run.log") trace_file = out / "Z.trace" if trace_file.exists(): # one line per instruction: "PC: disassembly" trace_file.replace(out / "trace.log") print(f"exit {result.returncode}; output in {out.relative_to(ROOT)}") return result.returncode if __name__ == "__main__": sys.exit(main())