# SPDX-License-Identifier: MIT """Run MAME 0.285's BBC Micro drivers headless, as a closed-box detector (docs/mame-oracle.md). python -I scripts/mame_trace.py --frames 50 # trace the main 6502 (bbcb) python -I scripts/mame_trace.py --driver bbcm --frames 50 # the Master's 65SC12 python -I scripts/mame_trace.py --snap 3 # no trace: run 3 s, save a PNG No disc or tape is mounted: the MOS boots to BASIC's prompt (and on the Model B the DFS looks for a disc), which is all the board's first milestones need. The romsets come from MAME_ROMPATH (bbcb.zip and the others: docs/census.md). Output goes to reference/mame/-/ (git-ignored): run.log with one line per instruction ("S pc a x y p sp totalcycles") and one line per write into SHEILA (&FE00-&FEFF), and snap//*.png. MAME is run, never read (its source is a closed box in this repository). """ 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) -> str: lines = [] if trace: lines += [ "local dbg = manager.machine.debugger", 'dbg.visible_cpu = manager.machine.devices[":maincpu"]', "dbg:command('trace S.trace,,noloop," '{logerror "S %X %X %X %X %X %X %d\\\\n",pc,a,x,y,p,sp,totalcycles}\')', "dbg:command('wpset fe00,100,w,1," '{logerror "W %X %X %d\\\\n",wpaddr,wpdata,totalcycles; g}\')', "dbg:command('wpset fe00,100,r,1," '{logerror "R %X %X %d\\\\n",wpaddr,wpdata,totalcycles; g}\')', '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="bbcb", help="bbcb, bbcbp, bbcm, bbcmc, ...") 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("--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 bbcb.zip)", file=sys.stderr) return 2 trace = args.frames > 0 frames = args.frames if trace else max(1, round(args.snap * 50)) out = ROOT / "reference" / "mame" / f"{args.driver}-{'trace' if trace else 'snap'}" out.mkdir(parents=True, exist_ok=True) script = out / "run.lua" script.write_text(lua(frames, trace), encoding="utf-8") command = [ args.mame, args.driver, "-rompath", args.rompath, "-homepath", str(out), "-snapshot_directory", str(out / "snap"), "-cfg_directory", str(out / "cfg"), "-nvram_directory", str(out / "nvram"), "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-log", "-seconds_to_run", str(int(frames / 50) + 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") (out / "S.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())