# SPDX-License-Identifier: MIT """MAME 0.285 as a closed-box detector for the Pico: which I/O registers a cartridge touches. NO_INTRO_DIR=... python -I scripts/mame_trace.py "Magic Crayons (USA).zip" --seconds 8 NO_INTRO_DIR=... python -I scripts/mame_trace.py CART.zip --machine picoj --seconds 2 --trace One MAME process, a few emulated seconds, headless. The cartridge is read in place from the No-Intro "Sega - PICO" set (MAME opens the zip itself; nothing is extracted). A Lua script taps the 68000's program space at $800000-$80001F (the Pico I/O block of notaz's picodoc) and logs every access with the frame, the 68000's PC, the address, the data and the mask, and snapshots the screen at the end. With --trace the MAME debugger also writes every 68000 instruction of the run (`trace` command) to reference/mame//cpu.trace: keep such runs to a second or two (about 8 M lines a second). Outputs go to reference/mame// (git-ignored): io.log, cpu.trace, error.log, snap/. MAME is GPL-2.0+ as a whole (its pico driver `sega/segapico.cpp` says BSD-3-Clause); it is run as a binary only, its source is not opened. What it shows is tier 5: a detector of what software does, never a judge of what the hardware does (docs/validation.md). """ from __future__ import annotations import argparse import os import re import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] LUA = r""" -- SPDX-License-Identifier: MIT -- Written for pico-python's mame_trace.py: log the Pico I/O block, then snapshot. local out = io.open("io.log", "w") local space = manager.machine.devices[":maincpu"].spaces["program"] local cpu = manager.machine.devices[":maincpu"] local screen = manager.machine.screens[":megadriv"] local frames = 0 local function log(kind, offset, data, mask) out:write(string.format("%d %s pc=%06X %06X data=%04X mask=%04X\n", frames, kind, cpu.state["PC"].value, offset, data, mask)) end -- held in globals so they are not collected io_read_tap = space:install_read_tap(0x800000, 0x80001f, "pico_io_r", function(offset, data, mask) log("R", offset, data, mask) end) io_write_tap = space:install_write_tap(0x800000, 0x80001f, "pico_io_w", function(offset, data, mask) log("W", offset, data, mask) end) frame_sub = emu.add_machine_frame_notifier(function() frames = frames + 1 if frames == FRAMES then out:close() screen:snapshot() manager.machine:exit() end end) """ def main() -> None: parser = argparse.ArgumentParser() parser.add_argument("cart", help="zip name inside $NO_INTRO_DIR/Sega - PICO") parser.add_argument("--machine", default="picou", choices=["pico", "picou", "picoj", "copera"]) parser.add_argument("--seconds", type=float, default=8.0) parser.add_argument("--trace", action="store_true", help="also trace every 68000 instruction") args = parser.parse_args() base = os.environ.get("NO_INTRO_DIR") if not base: sys.exit("mame_trace: set NO_INTRO_DIR") cart = Path(base) / "Sega - PICO" / args.cart if not cart.exists(): sys.exit(f"mame_trace: no such cartridge: {args.cart}") rate = 50 if args.machine == "pico" else 60 frames = max(1, int(args.seconds * rate)) run = re.sub(r"[^A-Za-z0-9]+", "_", Path(args.cart).stem)[:48] + f"_{args.machine}" out = ROOT / "reference" / "mame" / run out.mkdir(parents=True, exist_ok=True) lua = LUA.replace("FRAMES", str(frames)) command = [ "mame", args.machine, "-cart", str(cart), "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-snapshot_directory", "snap", "-cfg_directory", "cfg", "-nvram_directory", "nvram", "-autoboot_script", "run.lua", ] if args.trace: lua = ( "dbg = manager.machine.debugger\n" "dbg.execution_state = 'run'\n" "dbg:command('trace cpu.trace,maincpu,noloop')\n" ) + lua command += ["-debug", "-debugger", "none"] (out / "run.lua").write_text(lua) print(" ".join(command)) subprocess.run(["nice", "taskset", "-c", "24-31", *command], cwd=out, check=False, timeout=600) log = out / "io.log" if log.exists(): lines = log.read_text().splitlines() print(f"{len(lines)} I/O accesses in {frames} frames -> {log.relative_to(ROOT)}") if __name__ == "__main__": main()