# SPDX-License-Identifier: MIT """Run MAME 0.285's C64 driver headless, as a closed-box detector (docs/mame-oracle.md). python -I scripts/mame_trace.py --frames 3 # trace the 6510 (c64, NTSC) python -I scripts/mame_trace.py --driver c64p --frames 3 # ... the PAL machine python -I scripts/mame_trace.py --cpu drive --frames 3 # ... the 1541's 6502 python -I scripts/mame_trace.py --snap 4 # no trace: run, save a PNG No cartridge, disk or tape is mounted: the KERNAL boots to BASIC's READY prompt, which is what the board's first milestones need. The romsets come from MAME_ROMPATH (c64.zip, c64p.zip and c1541.zip hold the KERNAL, BASIC, character ROM, PLA table and DOS ROMs). Output goes to reference/mame/-/ (git-ignored): run.log with one line per instruction ("C pc a x y p sp totalcycles") and the VIC-II and CIA register writes, and snap/. Tags: ":u7" (6510), ":iec8:c1541:ucd5" (the 1541's 6502); MAME's 6502 debugger names its registers pc, a, x, y, p, sp. """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] CPUS = {"main": ":u7", "drive": ":iec8:c1541:ucd5"} FIELDS = "pc,a,x,y,p,sp" # Writes the main CPU makes to the VIC-II ($D000-$D02E) and the two CIAs ($DC00, $DD00). WATCHES = [("VIC", "d000", "2f", "w"), ("CIA1", "dc00", "10", "w"), ("CIA2", "dd00", "10", "w")] def lua(cpu: str, frames: int, trace: bool) -> str: lines = ["local dbg = manager.machine.debugger"] if trace else [] if trace: fmt = " ".join(["C"] + ["%X"] * len(FIELDS.split(",")) + ["%d"]) lines += [ f'dbg.visible_cpu = manager.machine.devices["{CPUS[cpu]}"]', f"dbg:command('trace C.trace,{CPUS[cpu]},noloop," f'{{logerror "{fmt}\\\\n",{FIELDS},totalcycles}}\')', ] if cpu == "main": for tag, start, length, rw in WATCHES: lines.append( f"dbg:command('wpset {start},{length},{rw},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="c64", help="c64 (NTSC), c64p (PAL), c64c, ...") 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("--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 c64.zip and c1541.zip)", file=sys.stderr) return 2 trace = args.frames > 0 frames = args.frames if trace else max(1, round(args.snap * 60)) out = ROOT / "reference" / "mame" / f"{args.driver}-{args.cpu if trace else 'snap'}" out.mkdir(parents=True, exist_ok=True) script = out / "run.lua" script.write_text(lua(args.cpu, 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 / 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 / "C.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())