#!/usr/bin/env python3 """Run one CPS-1 set on MAME headless and record what a lockstep needs. MAME is a detector (tier 5), never a judge: docs/mame-oracle.md says what this recipe can and cannot show. One run traces ONE CPU (the family's habit: the debugger parses a trace expression against the visible CPU's symbols, and a second CPU's trace in the same run logged nothing here). Each run writes, into a fresh directory ``tests/mame_traces/-/`` (git-ignored): - ``error.log``: - ``--cpu main``: one ``M`` line per 68000 instruction for the first ``--trace-frames`` frames; for the whole run one ``W`` line per write to the CPS-A/CPS-B window (0x800100-0x80017f), one ``R`` line per read of it, one ``S`` line per sound-latch write (0x800180-0x80018f) and one ``I`` line per read of the input window (0x800000-0x80001f); - ``--cpu audio``: one ``Z`` line per Z80 instruction for the first ``--trace-frames`` frames; for the whole run ``YW``/``YR`` lines for the 0xf000-0xf00f window (YM2151, OKI, bank, pin 7, the two latches); - MAME's own log lines (unmapped accesses and so on); - ``facts.txt``: device tags, screen geometry and refresh, and the debugger's register symbols for both CPUs, as MAME reports them; - ``snap//final.png``: the last frame. python -I scripts/mame_trace.py pnickj --cpu main --seconds 3 --trace-frames 2 python -I scripts/mame_trace.py pnickj --cpu audio --seconds 3 --trace-frames 2 ROMs are read in place from ``--rompath`` (default: the local MAME non-merged tree); nothing is copied. """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] ROMPATH = os.environ.get("MAME_ROMPATH") FACTS = r""" local machine = manager.machine local dbg = machine.debugger local screen = machine.screens[":screen"] local facts = io.open("facts.txt", "w") facts:write("machine " .. machine.system.name .. "\n") for tag, device in pairs(machine.devices) do facts:write("device " .. tag .. " | " .. device.shortname .. " | " .. device.name .. "\n") end facts:write(string.format("screen %dx%d refresh %.6f Hz (attoseconds %s)\n", screen.width, screen.height, screen.refresh, tostring(screen.refresh_attoseconds))) for _, tag in ipairs({":maincpu", ":audiocpu"}) do local names = {} for name, _ in pairs(machine.devices[tag].state) do names[#names + 1] = name end table.sort(names) facts:write("symbols " .. tag .. ": " .. table.concat(names, " ") .. "\n") end facts:flush() """ # An unknown symbol drops the whole trace command silently; logerror drops # values past 14. The 68000 has no A7 symbol: it is SP. MAIN = r""" dbg.visible_cpu = machine.devices[":maincpu"] dbg:command('trace cpu.trace,:maincpu,noloop,{logerror "M %06X %04X %08X %08X %08X %08X %08X %08X %d %d %d\\n",curpc,sr,d0,d1,d7,a0,a1,sp,totalcycles,beamy,beamx}') dbg:command('wpset 800100,80,w,1,{logerror "W %06X %X %04X %d %d %d\\n",wpaddr,wpsize,wpdata,totalcycles,beamy,beamx; g}') dbg:command('wpset 800100,80,r,1,{logerror "R %06X %X %04X %d %d %d\\n",wpaddr,wpsize,wpdata,totalcycles,beamy,beamx; g}') dbg:command('wpset 800180,10,w,1,{logerror "S %06X %X %04X %d %d %d\\n",wpaddr,wpsize,wpdata,totalcycles,beamy,beamx; g}') dbg:command('wpset 800000,20,r,1,{logerror "I %06X %X %04X %d %d %d\\n",wpaddr,wpsize,wpdata,totalcycles,beamy,beamx; g}') """ AUDIO = r""" dbg.visible_cpu = machine.devices[":audiocpu"] dbg:command('trace cpu.trace,:audiocpu,noloop,{logerror "Z %04X %04X %04X %04X %04X %04X %04X %04X %d\\n",pc,af,bc,de,hl,ix,iy,sp,totalcycles}') dbg:command('wpset f000,10,w,1,{logerror "YW %04X %02X %d\\n",wpaddr,wpdata,totalcycles; g}') dbg:command('wpset f000,10,r,1,{logerror "YR %04X %02X %d\\n",wpaddr,wpdata,totalcycles; g}') """ TAIL = r""" dbg:command("go") local trace_frames = TRACE_FRAMES local last = LAST_FRAME frame_sub = emu.add_machine_frame_notifier(function() local frame = screen:frame_number() if frame == trace_frames then dbg:command('trace off,CPU') end if frame == last then screen:snapshot("final.png") facts:write(string.format("frames %d emulated_time %s\n", frame, tostring(machine.time))) facts:close() machine:exit() end end) """ def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("set", help="MAME set name, for example pnickj") parser.add_argument("--cpu", choices=("main", "audio"), default="main") parser.add_argument("--seconds", type=int, default=3, help="emulated seconds to run") parser.add_argument("--trace-frames", type=int, default=2, help="frames of per-instruction trace") parser.add_argument("--rompath", default=ROMPATH) args = parser.parse_args() if not args.rompath: raise SystemExit("set MAME_ROMPATH to the MAME (non-merged) ROM folder, or pass --rompath") out = ROOT / "tests" / "mame_traces" / f"{args.set}-{args.cpu}" if out.exists(): print(f"{out} exists; move it into trash/ first (a stale error.log survives a crashed run)") return 1 out.mkdir(parents=True) tail = ( TAIL.replace("TRACE_FRAMES", str(args.trace_frames)) .replace("LAST_FRAME", str(args.seconds * 60)) .replace("CPU", ":maincpu" if args.cpu == "main" else ":audiocpu") ) script = out / "trace.lua" script.write_text(FACTS + (MAIN if args.cpu == "main" else AUDIO) + tail, encoding="utf-8") command = [ "mame", args.set, "-rompath", args.rompath, "-homepath", str(out), "-snapshot_directory", "snap", "-nvram_directory", "nvram", "-cfg_directory", "cfg", "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-debug", "-debugger", "none", "-log", "-seconds_to_run", str(args.seconds + 2), "-autoboot_script", str(script), ] # fmt: skip print(" ".join(command)) result = subprocess.run(command, cwd=out, check=False, timeout=600) print(f"mame exit {result.returncode}; output in {out}") return result.returncode if __name__ == "__main__": sys.exit(main())