# SPDX-License-Identifier: MIT """Run MAME 0.285's Atari ST driver headless, as a closed-box detector (docs/mame-oracle.md). python -I scripts/mame_trace.py --snap 12 # untraced run, PNG at the end python -I scripts/mame_trace.py --frames 3 # trace the 68000 python -I scripts/mame_trace.py --frames 50 --cpu ikbd # trace the keyboard's HD6301V1 python -I scripts/mame_trace.py --driver ste --bios tos162 --snap 12 No floppy is mounted: TOS boots to its desktop on its own. The romsets (st.zip, ste.zip, st_kbd.zip, ...) come from MAME_ROMPATH. Output goes to reference/mame/-/ (git-ignored): run.log with one line per traced instruction ("S pc sr sp totalcycles" for the 68000, "K pc a b x s totalcycles" for the HD6301) plus register-write watchpoints on the 68000 (Shifter, GLUE sync, MFP, ACIA, PSG, DMA), and snap/. MAME is run, never read: its source is GPL-2.0+/BSD-3 mixed (the ST files are BSD-3 by their license: lines, docs/sources.md). One process at a time, a few emulated seconds at most. """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] CPUS = {"main": ":m68000", "ikbd": ":ikbd:cpu"} FIELDS = {"main": ("S", "pc,sr,sp"), "ikbd": ("K", "pc,a,b,x,s")} # 68000 writes to watch (address, length): video base/sync/palette/mode, DMA and FDC, PSG, # MFP, the two ACIAs. MAME's 68000 sees 24-bit addresses. WATCHES = [ ("VID", "ff8200", "70"), ("DMA", "ff8600", "10"), ("PSG", "ff8800", "4"), ("MFP", "fffa00", "30"), ("ACIA", "fffc00", "8"), ] def lua(cpu: str, frames: int, trace: bool, watch: bool) -> str: lines = [] if trace: tag, names = FIELDS[cpu] fmt = " ".join([tag] + ["%X"] * len(names.split(",")) + ["%d"]) lines += [ "local dbg = manager.machine.debugger", f'dbg.visible_cpu = manager.machine.devices["{CPUS[cpu]}"]', f"dbg:command('trace T.trace,{CPUS[cpu]},noloop," f'{{logerror "{fmt}\\\\n",{names},totalcycles}}\')', ] if cpu == "main" and watch: for label, start, length in WATCHES: lines.append( f"dbg:command('wpset {start},{length},w,1," f'{{logerror "W {label} %X %X %X %d\\\\n",wpaddr,wpsize,wpdata,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="st", help="st, st_uk, megast, ste, megaste, ...") parser.add_argument("--bios", default=None, help="tos100, tos102, tos104, tos162, tos206 ...") 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("--rate", type=float, default=60.0, help="frames a second for --snap (TOS sets 60 Hz on US rows, 50 Hz elsewhere)") parser.add_argument("--no-watch", action="store_true", help="no register watchpoints") 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 st.zip and st_kbd.zip)", file=sys.stderr) return 2 trace = args.frames > 0 frames = args.frames if trace else max(1, round(args.snap * args.rate)) what = args.cpu if trace else "snap" out = ROOT / "reference" / "mame" / f"{args.driver}{'-' + args.bios if args.bios else ''}-{what}" out.mkdir(parents=True, exist_ok=True) script = out / "run.lua" script.write_text(lua(args.cpu, frames, trace, not args.no_watch), encoding="utf-8") command = [ args.mame, args.driver, "-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 / 40) + 5), "-autoboot_script", str(script), ] # fmt: skip if args.bios: command += ["-bios", args.bios] 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 holds the state print(f"exit {result.returncode}; output in {out.relative_to(ROOT)}") return result.returncode if __name__ == "__main__": sys.exit(main())