#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Run a 3DO machine in MAME headless and count the CPU's register accesses (tier 5 detector). Taps the ARM60's writes and reads to MADAM (0x0330_0000-0x0330_07FF) and CLIO (0x0340_0000-0x0340_FFFF) for --frames frames, prints one line per register address with write and read counts, and saves a snapshot of the last frame. Counts only: no firmware bytes, no disassembly. A disc image may be mounted with --cdrom (a CHD or .cue the player supplies through REDUMP_DIR or their own library). MAME_ROMPATH=... python -I scripts/mame_run.py --machine 3do_fz10 --frames 600 The run's cwd is tests/mame_traces/ (git-ignored): error.log, cfg/, nvram/ and snap/ land there. The firmware comes from $MAME_ROMPATH, never from the repository. """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] RUNS = ROOT / "tests" / "mame_traces" LUA = """ writes = {{}} reads = {{}} space = manager.machine.devices[':maincpu'].spaces['program'] wtap = space:install_write_tap(0x03300000, 0x0340FFFF, 'io_w', function(offset, data, mask) writes[offset] = (writes[offset] or 0) + 1 end) rtap = space:install_read_tap(0x03300000, 0x0340FFFF, 'io_r', function(offset, data, mask) reads[offset] = (reads[offset] or 0) + 1 end) frames = 0 sub = emu.add_machine_frame_notifier(function() frames = frames + 1 if frames == {stop} then manager.machine.video:snapshot() local keys = {{}} for k, _ in pairs(writes) do keys[#keys + 1] = k end for k, _ in pairs(reads) do if writes[k] == nil then keys[#keys + 1] = k end end table.sort(keys) for _, k in ipairs(keys) do print(string.format("REG %08X W %d R %d", k, writes[k] or 0, reads[k] or 0)) end print(string.format("STOP %.6f s frames %d", manager.machine.time:as_double(), frames)) manager.machine:exit() end end) """ def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--machine", default="3do_fz10") parser.add_argument("--frames", type=int, default=600) parser.add_argument("--cdrom", type=Path, help="disc image to mount (CHD or .cue)") parser.add_argument("--out", type=Path, default=RUNS / "registers.txt") parser.add_argument("--mame", default="mame") args = parser.parse_args() rompath = os.environ.get("MAME_ROMPATH") if not rompath: print("set MAME_ROMPATH (the 3do_* sets)", file=sys.stderr) return 2 RUNS.mkdir(parents=True, exist_ok=True) script = RUNS / "registers.lua" script.write_text(LUA.format(stop=args.frames), encoding="utf-8") command = [ "nice", args.mame, args.machine, "-rompath", rompath, "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-snapshot_directory", str(RUNS / "snap"), "-autoboot_script", str(script), "-seconds_to_run", str(args.frames // 50 + 10), ] if args.cdrom: command += ["-cdrom", str(args.cdrom.resolve())] result = subprocess.run(command, cwd=RUNS, capture_output=True, text=True, check=False) lines = [line for line in result.stdout.splitlines() if line.startswith(("REG", "STOP"))] if not lines: print(result.stdout[-2000:], result.stderr[-2000:], sep="\n", file=sys.stderr) return 1 args.out.write_text("\n".join(lines) + "\n", encoding="ascii") print(lines[-1]) print(f"{len(lines) - 1} register addresses written to {args.out}") return 0 if __name__ == "__main__": sys.exit(main())