"""MAME 0.285: every port access of a run, with scripted inputs (no instruction trace). python -I scripts/mame_io.py coleco CART.zip --frames 1000 --input 800:806:p1.1 \ --out tests/mame_traces/io-donkey_kong Writes ``error.log`` in OUT with one line per access, ``IW port data totalcycles`` or ``IR port data totalcycles`` (``totalcycles`` = the start of the instruction that made it), the I/O half of docs/mame-oracle.md's recipe, about 5 times cheaper than the full trace. Inputs as in ``scripts/mame_frames.py`` (frame k = MAME's k-th frame notifier, 0-based; an input is set in notifier START-1 and released in notifier END-1). The run stops in notifier FRAMES-1. """ from __future__ import annotations import argparse import subprocess import sys import time from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) from mame_frames import MAME, ROMPATH, mame_field LUA = """\ local dbg = manager.machine.debugger dbg.visible_cpu = manager.machine.devices[":maincpu"] dbg:command('wpiset {ports},w,1,{{logerror "IW %X %X %d\\\\n",wpaddr&0xff,wpdata,totalcycles; g}}') {reads}dbg:command('wpiset {ports},r,1,{{logerror "IR %X %X %d\\\\n",wpaddr&0xff,wpdata,totalcycles; g}}') dbg:command("go") local inputs = {{{inputs}}} local last = {last} local fields = {{}} for i, inp in ipairs(inputs) do fields[i] = manager.machine.ioport.ports[inp[3]].fields[inp[4]] end local function apply(k) for i, inp in ipairs(inputs) do fields[i]:set_value((inp[1] <= k and k < inp[2]) and 1 or 0) end end frame = 0 apply(0) notifier = emu.add_machine_frame_notifier(function() if frame >= last then manager.machine:exit() end if frame == {trace_from} then dbg:command('trace z80.trace,,noloop,{{logerror "%X %X %X %X %X %X %X %X %X %X %X %d %d %d\\\\n",pc,af,bc,de,hl,ix,iy,sp,af2,i,r,totalcycles,beamy,beamx}}') end frame = frame + 1 apply(frame) end) """ def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("driver", choices=["coleco", "colecop"]) parser.add_argument("cart", type=Path, nargs="?") parser.add_argument("--frames", type=int, required=True) parser.add_argument("--input", action="append", default=[]) parser.add_argument("--out", type=Path, required=True) parser.add_argument("--ports", default="0,100", help="wpiset address,length (hex), default all") parser.add_argument("--writes-only", action="store_true") parser.add_argument( "--trace-from", type=int, default=-1, help="also trace every instruction from this notifier on" ) args = parser.parse_args() out = args.out.resolve() if out.exists(): raise SystemExit(f"{out} exists: a run never overwrites") out.mkdir(parents=True) rows = [] for text in args.input: start, end, control = text.split(":", 2) tag, field = mame_field(control) rows.append(f'{{{int(start)}, {int(end)}, "{tag}", "{field}"}}') lua = LUA.format( inputs=", ".join(rows), last=args.frames - 1, trace_from=args.trace_from, ports=args.ports, reads="-- " if args.writes_only else "", ) (out / "io.lua").write_text(lua, encoding="utf-8") (out / "inputs.txt").write_text("\n".join(args.input) + "\n", encoding="utf-8") command = [MAME, args.driver] if args.cart: command += ["-cart", str(args.cart.resolve())] command += [ "-rompath", ROMPATH, "-homepath", str(out), "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-log", "-debug", "-debugger", "none", "-seconds_to_run", str(args.frames // 50 + 10), "-autoboot_script", str(out / "io.lua"), ] # fmt: skip began = time.monotonic() subprocess.run(command, cwd=out, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) lines = sum(1 for line in (out / "error.log").open("rb") if line[:3] in (b"IW ", b"IR ")) print(f"MAME {args.driver}, {args.frames} frames in {time.monotonic() - began:.1f} s: {lines:,} accesses") return 0 if __name__ == "__main__": sys.exit(main())