# SPDX-License-Identifier: MIT """Run MAME 0.285's Oric drivers headless, as a closed-box detector (docs/mame-oracle.md). python -I scripts/mame_trace.py --frames 150 # trace the 6502, Atmos python -I scripts/mame_trace.py --frames 150 --driver oric1 python -I scripts/mame_trace.py --snap 4 --driver oric1 # no trace: run, save a PNG No tape or disk is mounted: the ROM runs its reset, RAM test and screen set-up and BASIC prints its banner, which is what the board's first milestones need. The romsets come from MAME_ROMPATH (oric1.zip, orica.zip). Output goes to reference/mame/-/ (git-ignored): run.log holds one line per instruction ("S pc a x y p sp totalcycles") and one line per CPU write to the VIA ($0300-$030F) with the value and the cycle; snap/ holds the last frame as a PNG. Tags (`mame -listdevices orica`): ":maincpu" (6502), ":via6522", ":ay8912". The ULA has no device of its own in MAME's list. MAME is run, never read (the closed-box rule). """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] CPU = ":maincpu" FIELDS = "pc,a,x,y,p,sp" WATCHES = [("VIA", "300", "10")] # MAME's screen: 6 MHz pixel clock, 384 x 312, 50.080128 Hz (`mame -listxml orica`). RATE = {"oric1": 50.080128, "orica": 50.080128} def lua(frames: int, trace: bool) -> str: lines = [] if trace: names = FIELDS.split(",") fmt = " ".join(["S"] + ["%X"] * len(names) + ["%d"]) lines += [ "local dbg = manager.machine.debugger", f'dbg.visible_cpu = manager.machine.devices["{CPU}"]', f"dbg:command('trace S.trace,{CPU},noloop,{{logerror \"{fmt}\\\\n\",{FIELDS},totalcycles}}')", ] for tag, start, length in WATCHES: lines.append( f"dbg:command('wpset {start},{length},w,1," f'{{logerror "{tag} %X %X %d\\\\n",wpaddr,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="orica", choices=sorted(RATE)) 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 oric1.zip / orica.zip)", file=sys.stderr) return 2 trace = args.frames > 0 frames = args.frames if trace else max(1, round(args.snap * RATE[args.driver])) 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"), "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-log", "-seconds_to_run", str(int(frames / RATE[args.driver]) + 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 / "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())