# SPDX-License-Identifier: MIT """Run MAME 0.285's PET drivers headless, as a closed-box detector (docs/mame-oracle.md). python -I scripts/mame_trace.py --snap 4 # untraced, PNG after 4 s python -I scripts/mame_trace.py --snap 4 --driver cbm8032 python -I scripts/mame_trace.py --frames 60 --driver pet2001n # trace the 6502 for 1 s python -I scripts/mame_trace.py --frames 60 --drive c4040 # keep MAME's default drive The romsets come from MAME_ROMPATH (pet2001.zip, pet2001n.zip, pet4016.zip, pet8032.zip and the clones' zips hold BASIC, the KERNAL, the editor and the character ROM; c4040.zip / c8050.zip the drives' DOS ROMs). MAME attaches a drive on IEEE-488 device 8 by default (a 4040 on the 2001/4000 drivers, an 8050 on the 8000s): its two processors run too. The default here is `-ieee8 ""` (no drive), so only the computer runs; `--drive` names one. Output goes to reference/mame/-/ (git-ignored): run.log with one line per instruction ("S pc a x y p sp totalcycles") of the computer's 6502 (tag ":f3"), one line per CPU write to the two PIAs ($E810, $E820), the VIA ($E840) and the CRTC ($E880) with the value and the cycle, and snap/. Tags: `mame -listdevices pet2001n`. """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] CPU = ":f3" FIELDS = "pc,a,x,y,p,sp" # (label, start, length): CPU-side writes to the I/O page's chip windows. WATCHES = [("PIA1", "e810", "4"), ("PIA2", "e820", "4"), ("VIA", "e840", "10"), ("CRTC", "e880", "2")] DRIVERS = ["pet2001", "pet2001n", "pet2001b", "pet4016", "pet4032", "pet4032f", "cbm4032", "pet8032", "cbm8032", "cbm8096", "cbm8296", "superpet"] # fmt: skip 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}"]', # The disassembly goes to /dev/null: the logerror action puts the state in run.log. f"dbg:command('trace /dev/null,{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="pet2001n", choices=DRIVERS) parser.add_argument("--frames", type=int, default=0, help="trace this many MAME frames") parser.add_argument("--snap", type=float, default=0, help="untraced run of N seconds (60 frames a second)") parser.add_argument("--drive", default="", help='device on IEEE-488 address 8 ("" = none)') 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 the PET machine zips)", 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}-{'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", "-ieee8", args.drive, "-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") print(f"exit {result.returncode}; output in {out.relative_to(ROOT)}") return result.returncode if __name__ == "__main__": sys.exit(main())