# SPDX-License-Identifier: MIT """Run MAME 0.285's Williams driver headless and log board events (docs/mame-oracle.md). python -I scripts/mame_trace.py robotron --seconds 16 # board events, main + sound CPU python -I scripts/mame_trace.py sinistar --seconds 4 --instructions # ... plus every main-CPU instruction python -I scripts/mame_trace.py defender --seconds 4 MAME is a closed box here: run, never read. The romsets come from MAME_ROMPATH, used in place. Output goes to reference/mame// (git-ignored): run.log holds one line per event, with the main CPU's total cycles and MAME's beam position: BLIT addr data cycles beamy main CPU writes to the blitter registers CA00-CA07 PIAW addr data cycles beamy main CPU writes to the widget and ROM-board PIAs PIAR addr data cycles beamy ... and reads of them (reads of a data register clear its flags) VCNT data cycles beamy main CPU reads of the vertical count buffer (CB00) WDOG data cycles beamy writes to the watchdog (CBFF) BANK data cycles beamy writes to the bank/flip latch (C900) SPIA addr data cycles sound CPU reads and writes of its PIA (0400-0403) INST pc a b x y u s dp cc cycles beamy (only with --instructions) Defender has a different map (C000-CFFF is paged; docs/defender.md): its watchpoints are the page-0 I/O window instead. Every address here was found in the Williams documents (docs/memory-map.md), not in MAME's source. """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] # Main CPU watchpoints: (tag, start, length, kind). Later boards (Stargate onward). LATER = [ ("BLIT", "ca00", "8", "w"), ("PIA", "c804", "4", "rw"), ("PIA", "c80c", "4", "rw"), ("VCNT", "cb00", "1", "r"), ("WDOG", "cbff", "1", "w"), ("BANK", "c900", "1", "w"), ] # Defender: the I/O sits in C000-CFFF with page 0 selected (DEF-T, "Page 0 decoder"). DEFENDER = [ ("PIA", "cc00", "8", "rw"), # both PIAs (CC00-CC07 seen in the 2026-11-26 run; DEF-T fig. 3-15) ("VCNT", "c800", "1", "r"), # the vertical count buffer (E7); a wider range also catches ROM pages ("WDOG", "c3f0", "10", "w"), # DEF-T: 38h/39h to C3FF; the run saw 38h written to C3FC ("BANK", "d000", "1", "w"), # DEF-T: the page latch, written at D000 ] # Where a fresh-CMOS ROM waits for Advance with IRQs masked (m6809-python's observation, # its docs/mame-oracle.md): press it once so the attract mode starts. # Sinistar's loop was found here on 2026-11-26 (PC sampled once a frame from frame 720 on). WAIT_LOOPS = {"robotron": (0x74EB, 0x74F7), "joust": (0x3D69, 0x3D73), "sinistar": (0x4A26, 0x4A30)} def watch(tag: str, start: str, length: str, kind: str) -> list[str]: lines = [] for rw in kind: name = tag + rw.upper() if tag == "PIA" else tag if length == "1": fmt, args = f"{name} %X %d %d", "wpdata,totalcycles,beamy" else: fmt, args = f"{name} %X %X %d %d", "wpaddr,wpdata,totalcycles,beamy" lines.append(f"dbg:command('wpset {start},{length},{rw},1,{{logerror \"{fmt}\\\\n\",{args}; g}}')") return lines def lua(game: str, frames: int, instructions: bool, sound: bool) -> str: table = DEFENDER if game.startswith("defen") else LATER out = [ "local dbg = manager.machine.debugger", 'local main = manager.machine.devices[":maincpu"]', "dbg.visible_cpu = main", ] for entry in table: out += watch(*entry) if instructions: out.append( "dbg:command('trace main.trace,,noloop,{logerror \"INST %X %X %X %X %X %X %X %X %X %d %d\\\\n\"," "pc,a,b,x,y,u,s,dp,cc,totalcycles,beamy}')" ) if sound: out += [ 'local snd = manager.machine.devices[":soundcpu"]', "dbg.visible_cpu = snd", "dbg:command('wpset 0400,4,rw,1,{logerror \"SPIA %X %X %d\\\\n\",wpaddr,wpdata,totalcycles; g}')", "dbg.visible_cpu = main", ] out.append('dbg:command("go")') wait = WAIT_LOOPS.get(game) out += ["count = 0", "pressed = nil"] if wait: out.append('advance = manager.machine.ioport.ports[":IN2"].fields["Advance"]') out += [ "notifier = emu.add_machine_frame_notifier(function()", " count = count + 1", ] if wait: out += [ " local pc = main.state['PC'].value", f" if pressed == nil and pc >= {wait[0]} and pc <= {wait[1]} then", " pressed = count; advance:set_value(1); print('ADVANCE pressed at frame ' .. count)", " elseif pressed ~= nil and count == pressed + 10 then advance:set_value(0) end", ] out += [ f" if count == {frames} then", " manager.machine.video:snapshot()", " manager.machine:exit()", " end", "end)", ] return "\n".join(out) + "\n" def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("game", nargs="?", default="robotron") parser.add_argument("--seconds", type=float, default=6.0, help="emulated seconds (60 frames each)") parser.add_argument("--instructions", action="store_true", help="also trace every main-CPU instruction") parser.add_argument("--no-sound", action="store_true", help="skip the sound CPU's PIA watchpoint") 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 (the non-merged MAME romsets)", file=sys.stderr) return 2 frames = max(1, round(args.seconds * 60)) out = ROOT / "reference" / "mame" / args.game out.mkdir(parents=True, exist_ok=True) script = out / "run.lua" script.write_text(lua(args.game, frames, args.instructions, not args.no_sound), encoding="utf-8") command = [ args.mame, args.game, "-rompath", args.rompath, "-homepath", str(out), "-nvram_directory", str(out / "nvram"), "-cfg_directory", str(out / "cfg"), "-snapshot_directory", str(out / "snap"), "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-log", "-debug", "-debugger", "none", "-seconds_to_run", str(int(args.seconds) + 5), "-autoboot_script", str(script), ] # fmt: skip result = subprocess.run(["nice", *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") print(f"exit {result.returncode}; output in {out.relative_to(ROOT)}") return result.returncode if __name__ == "__main__": sys.exit(main())