# SPDX-License-Identifier: MIT """Run MAME 0.285's Plus/4 family drivers headless, as a closed-box detector (docs/mame-oracle.md). python -I scripts/mame_trace.py --frames 150 # trace the 7501, plus4 (NTSC) python -I scripts/mame_trace.py --frames 150 --driver plus4p # PAL python -I scripts/mame_trace.py --snap 5 --driver c16p # no trace: run, save a PNG No cartridge, tape or disk is mounted, and the IEC drive MAME attaches by default on device 8 is removed (`-iec8 ""`), so only the computer runs: the KERNAL's reset, RAM test, screen set-up and BASIC's banner, which is what the board's first milestones need. The romsets come from MAME_ROMPATH (plus4.zip, plus4p.zip, c16.zip, c16p.zip, c116.zip). Output goes to reference/mame/-/ (git-ignored): run.log holds one line per instruction ("S pc a x y p sp totalcycles"), one line per CPU write to the TED ($FF00-$FF3F), the ROM/RAM switches ($FF3E/$FF3F), the ACIA, the 6529s and the 7501 port ($0000-$0001) with the value and the cycle; snap/ holds the last frame as a PNG. Tags (`mame -listdevices plus4`): the 7501 is ":u2", the TED ":u1", the ACIA ":u3", the 6529s ":u5" (user port) and ":u27" (keyboard), the PLA ":u19". MAME is run, never read (the closed-box rule): nothing here comes from its source. """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] CPU = ":u2" FIELDS = "pc,a,x,y,p,sp" # (label, start, length in hex): CPU-side writes to watch. WATCHES = [("PORT", "0", "2"), ("TED", "FF00", "40"), ("IO", "FD00", "40")] # Frames a second by MAME's screen for each driver (`mame -listxml`: all five report a # "50.000000" Hz legacy screen, NTSC ones included: docs/mame-oracle.md, trap 1). RATE = {"plus4": 50, "plus4p": 50, "c16": 50, "c16p": 50, "c116": 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="plus4", 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 plus4.zip and the other sets)", 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", "-iec8", "", "-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())