#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Run MAME's CoCo or Dragon headless as a closed-box detector: I/O log, trace, screenshot. For --frames frames it logs every CPU write to the PIA window FF00-FF3F and the SAM control window FFC0-FFDF (emulated time in seconds, address, data) through Lua write taps, optionally traces the first --trace-frames frames of CPU instructions with MAME's debugger (pc, a, b, x, y, u, s, dp, cc and the total cycle count), and then saves a screenshot. MAME_ROMPATH=... python -I scripts/mame_run.py --machine coco --frames 300 MAME_ROMPATH=... python -I scripts/mame_run.py --machine dragon32 --frames 300 --trace-frames 2 MAME_ROMPATH=... python -I scripts/mame_run.py --machine coco --ext "" --frames 180 Everything lands in tests/mame_traces// (git-ignored): io.log, cpu.trace, snap//0000.png, error.log. The firmware comes from $MAME_ROMPATH and is never copied. Only one MAME process at a time; keep runs to a few emulated seconds. """ from __future__ import annotations import argparse import collections import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] RUNS = ROOT / "tests" / "mame_traces" LUA = """ cpu = manager.machine.devices[":maincpu"] space = cpu.spaces["program"] log = io.open("io.log", "w") n = 0 function logger(offset, data, mask) n = n + 1 if n <= {limit} then log:write(string.format("%.9f W %04X %02X\\n", manager.machine.time:as_double(), offset, data)) end end tap_pia = space:install_write_tap(0xff00, 0xff3f, "pia", logger) tap_sam = space:install_write_tap(0xffc0, 0xffdf, "sam", logger) tap_cart = space:install_write_tap(0xff40, 0xff5f, "cart", logger) frames = 0 tracing = {trace} if tracing > 0 then dbg = manager.machine.debugger dbg.visible_cpu = cpu dbg:command('trace cpu.trace,maincpu,noloop,{{tracelog "%04X %02X %02X %04X %04X %04X %04X %02X %02X %d ",pc,a,b,x,y,u,s,dp,cc,totalcycles}}') dbg:command('go') end sub = emu.add_machine_frame_notifier(function() frames = frames + 1 if tracing > 0 and frames == tracing then dbg:command('trace off,maincpu') end if frames == {frames} then log:close() manager.machine.video:snapshot() print("WRITES " .. n .. " FRAMES " .. frames .. " TIME " .. string.format("%.6f", manager.machine.time:as_double())) manager.machine:exit() end end) """ def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--machine", default="coco") parser.add_argument("--bios", help="MAME BIOS option (coco: b12e11 is the default)") parser.add_argument("--frames", type=int, default=300) parser.add_argument("--trace-frames", type=int, default=0, help="trace CPU for N frames") parser.add_argument("--limit", type=int, default=200_000, help="most writes logged") parser.add_argument("--ext", help="cartridge slot device; '' leaves it empty (coco's default is the FDC)") parser.add_argument("--mame", default="mame") args = parser.parse_args() rompath = os.environ.get("MAME_ROMPATH") if not rompath: print("set MAME_ROMPATH", file=sys.stderr) return 2 run = RUNS / args.machine run.mkdir(parents=True, exist_ok=True) script = run / "run.lua" script.write_text( LUA.format(limit=args.limit, frames=args.frames, trace=args.trace_frames), encoding="utf-8" ) command = [ "nice", args.mame, args.machine, "-rompath", rompath, "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-snapshot_directory", "snap", "-autoboot_script", str(script), "-seconds_to_run", str(args.frames // 50 + 5), ] if args.bios: command += ["-bios", args.bios] if args.ext is not None: command += ["-ext", args.ext] if args.trace_frames: command += ["-debug", "-debugger", "none", "-log"] result = subprocess.run(command, cwd=run, capture_output=True, text=True, check=False) done = [line for line in result.stdout.splitlines() if line.startswith("WRITES")] if not done: print(result.stdout[-2000:], result.stderr[-2000:], sep="\n", file=sys.stderr) return 1 print(done[0], "->", run / "io.log") writes = collections.Counter( line.split()[2] for line in (run / "io.log").read_text().splitlines() if line ) print("writes by address:", ", ".join(f"{a} {c}" for a, c in sorted(writes.items()))) trace = run / "cpu.trace" if trace.exists(): with trace.open() as handle: print("trace lines", sum(1 for _ in handle), "->", trace) snaps = sorted((run / "snap" / args.machine).glob("*.png")) if snaps: print("screenshot", snaps[-1]) return 0 if __name__ == "__main__": sys.exit(main())