# SPDX-License-Identifier: MIT """Run MAME 0.285's Spectrum drivers headless, as a closed-box detector (docs/mame-oracle.md). python -I scripts/mame_trace.py --driver spectrum --frames 200 # I/O writes + PNG python -I scripts/mame_trace.py --driver spec128 --frames 200 --trace 2 Without a tape or disc the ROM runs to its own screen (the 48K copyright message, the 128K menu, the +3 menu), which is all the board's first milestones need. A write tap on the Z80's I/O space logs every OUT with the frame number MAME reports and the emulated time in seconds ("O port value frame seconds", in outs.log; MAME 0.285's Lua screen object has no hpos/vpos); ``--trace N`` also traces the Z80 for the first N frames through the debugger ("T pc totalcycles", one line per instruction, in run.log). The romsets come from MAME_ROMPATH. Output goes to reference/mame// (git-ignored): outs.log, run.log, stdout.txt and snap/. Traps: the tap must be held in a Lua global or it is collected; -seconds_to_run takes whole seconds, so the run is stopped by frame count from Lua. """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] DRIVERS = ("spectrum", "spec128", "specpls2", "specpl2a", "specpls3") def lua(frames: int, trace: int) -> str: lines = [ 'local cpu = manager.machine.devices[":maincpu"]', 'screen = manager.machine.screens[":screen"]', 'outs = io.open("outs.log", "w")', 'tap = cpu.spaces["io"]:install_write_tap(0x0000, 0xffff, "outs", function(offset, data, mask)', ' outs:write(string.format("O %04X %02X %d %.9f\\n", offset, data,' " screen:frame_number(), manager.machine.time:as_double()))", " outs:flush()", "end)", ] if trace: lines += [ "local dbg = manager.machine.debugger", "dbg.visible_cpu = cpu", "dbg:command('trace T.trace,:maincpu,noloop,{logerror \"T %X %d\\\\n\",pc,totalcycles}')", 'dbg:command("go")', ] lines += [ "count = 0", "notifier = emu.add_machine_frame_notifier(function()", " count = count + 1", ] if trace: lines += [f" if count == {trace} then manager.machine.debugger:command('trace off') end"] lines += [ f" if count == {frames} then", " manager.machine.video:snapshot()", " outs:close()", " 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="spectrum", choices=DRIVERS) parser.add_argument("--frames", type=int, default=200, help="run this many frames") parser.add_argument("--trace", type=int, default=0, help="trace the Z80 for N frames") 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(f"set MAME_ROMPATH (it must hold {args.driver}.zip)", file=sys.stderr) return 2 out = ROOT / "reference" / "mame" / args.driver out.mkdir(parents=True, exist_ok=True) script = out / "run.lua" script.write_text(lua(args.frames, args.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(args.frames / 50) + 5), "-autoboot_script", str(script), ] # fmt: skip if args.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 / "T.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())