# SPDX-License-Identifier: MIT """Run MAME 0.285's Amiga driver headless, as a closed-box detector (docs/mame-oracle.md). python -I scripts/mame_trace.py --frames 5 # trace the 68000, log custom writes python -I scripts/mame_trace.py --snap 12 # no trace: run 12 s, save a PNG python -I scripts/mame_trace.py --driver a500n --bios kick31 --snap 12 No disk is inserted: Kickstart runs from reset to its "insert disk" screen, which is all the board's first milestones need. The romsets come from MAME_ROMPATH (a500.zip holds the Kickstarts and the keyboard MCU's ROM). Output goes to reference/mame/-- / (git-ignored): run.log with one line per 68000 instruction ("T pc sr sp cycles") and one line per write to the custom registers ("W addr data beamy beamx frame cycles"), and snap/. Facts used: the custom registers sit at DFF000-DFF1FF (HRM appendix B); MAME's 68000 is ":maincpu"; the debugger's beamx/beamy/frame symbols are the primary screen's. """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] def lua(frames: int, trace: bool, cia: bool) -> str: lines = [] if trace: lines += [ "local dbg = manager.machine.debugger", 'dbg.visible_cpu = manager.machine.devices[":maincpu"]', "dbg:command('trace T.trace,:maincpu,noloop," '{logerror "T %X %X %X %d\\\\n",pc,sr,sp,totalcycles}\')', "dbg:command('wpset dff000,200,w,1," '{logerror "W %X %X %d %d %d %d\\\\n",wpaddr,wpdata,beamy,beamx,frame,totalcycles; g}\')', ] if cia: # CIA-A at BFE001 (odd bytes), CIA-B at BFD000 (even bytes): HRM appendix F. lines.append( "dbg:command('wpset bfd000,2000,w,1," '{logerror "C %X %X %d %d %d %d\\\\n",wpaddr,wpdata,beamy,beamx,frame,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("--driver", default="a500", help="a500 (PAL), a500n (NTSC), a500p, ...") parser.add_argument("--bios", default="kick13", help="kick12, kick13, kick204, kick31") 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("--cia", action="store_true", help="also log CIA register writes") 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 (it must hold a500.zip)", file=sys.stderr) return 2 trace = args.frames > 0 rate = 60 if args.driver.endswith("n") else 50 frames = args.frames if trace else max(1, round(args.snap * rate)) out = ROOT / "reference" / "mame" / f"{args.driver}-{args.bios}-{'trace' if trace else 'snap'}" out.mkdir(parents=True, exist_ok=True) script = out / "run.lua" script.write_text(lua(frames, trace, args.cia), encoding="utf-8") command = [ args.mame, args.driver, "-bios", args.bios, "-rompath", args.rompath, "-homepath", str(out), "-snapshot_directory", str(out / "snap"), "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-log", "-seconds_to_run", str(int(frames / rate) + 5), "-autoboot_script", str(script), ] # fmt: skip if trace: command += ["-debug", "-debugger", "none"] 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") (out / "T.trace").unlink(missing_ok=True) # the disassembly; run.log has the state print(f"exit {result.returncode}; output in {out.relative_to(ROOT)}") return result.returncode if __name__ == "__main__": sys.exit(main())