# SPDX-License-Identifier: MIT """Run MAME 0.285's VIC-20 driver headless, as a closed-box detector (docs/mame-oracle.md). python -I scripts/mame_trace.py --frames 120 # trace the 6502, NTSC python -I scripts/mame_trace.py --frames 120 --driver vic20p python -I scripts/mame_trace.py --snap 4 # no trace: run, save a PNG No cartridge, tape or disk is mounted: the KERNAL runs its reset, RAM test and screen set-up and BASIC prints its banner, which is all the board's first milestones need. The romsets come from MAME_ROMPATH (vic20.zip / vic20p.zip hold the KERNAL, BASIC and character ROMs, and the 1541's DOS ROMs: MAME attaches a 1541 on IEC device 8 by default, so the drive's 6502 runs too; only the computer's CPU is traced). Output goes to reference/mame/-/ (git-ignored): run.log with one line per instruction ("S pc a x y p sp totalcycles"), one line per write to the VIC ($9000-$900F) and the two VIAs ($9110-$912F) with the value and the cycle, and snap/. The CPU's tag is ":ue10" (its chip location on the board); the VIC is ":ub7", the VIAs ":uab3" (VIA1, $9110) and ":uab1" (VIA2, $9120): `mame -listdevices vic20`. """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] CPU = ":ue10" FIELDS = "pc,a,x,y,p,sp" # (label, start, length): CPU-side writes to the VIC and VIA register windows. WATCHES = [("VIC", "9000", "10"), ("VIA1", "9110", "10"), ("VIA2", "9120", "10")] RATE = {"vic20": 60, "vic20p": 50, "vic1001": 60, "vic20_se": 50} 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="vic20", 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 vic20.zip / vic20p.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())