#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Run a Channel F in MAME and log every I/O port write (a closed-box detector, tier 5). Runs MAME headless on a Channel F machine for --frames frames, taps the F8's I/O space with a Lua write tap (every OUT/OUTS: frame, port, value), takes a screenshot at the end, and exits. The log is what the board's ports must see: VRAM writes (ports 0, 1, 4, 5), the sound bits (port 5 bits 6-7), and any cartridge ports (VC-10's 24h/25h, VC-18's 20h/21h, a 3853's 0Ch-0Fh). MAME_ROMPATH=... python -I scripts/mame_run.py --machine channelf --bios sl31253 \ --frames 300 --out tests/mame_traces/io.log [--cart path/to/cart.bin] The run's cwd is tests/mame_traces/ (git-ignored): error.log, cfg/, nvram/ and snap/ land there. The BIOS comes from $MAME_ROMPATH (channelf.zip); cartridges are read in place. """ from __future__ import annotations import argparse import os import subprocess import sys import tempfile import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] RUNS = ROOT / "tests" / "mame_traces" LUA = """ cpu = manager.machine.devices[":maincpu"] io = cpu.spaces["io"] fh = assert(io_open_file("{out}", "w")) frames = 0 writes = 0 tap = io:install_write_tap(0x00, 0xff, "iolog", function(offset, data, mask) writes = writes + 1 fh:write(string.format("%d %02X %02X\\n", frames, offset & 0xff, data & 0xff)) end) sub = emu.add_machine_frame_notifier(function() frames = frames + 1 if frames == {stop} then manager.machine.video:snapshot() fh:close() print(string.format("WRITES %d FRAMES %d TIME %.6f", writes, frames, 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="channelf") parser.add_argument("--bios", default="sl31253") parser.add_argument("--frames", type=int, default=300) parser.add_argument("--cart", type=Path, help="cartridge image (a .bin, or a .zip whose first member is used)") parser.add_argument("--out", type=Path, default=RUNS / "io.log") parser.add_argument("--mame", default="mame") args = parser.parse_args() rompath = os.environ.get("MAME_ROMPATH") if not rompath: print("set MAME_ROMPATH (MAME's ';'-separated rompath holding channelf.zip)", file=sys.stderr) return 2 RUNS.mkdir(parents=True, exist_ok=True) out = args.out.resolve() # Lua's io library is shadowed by the address space named io above, so open the file # through a saved reference. script = RUNS / "run.lua" script.write_text("io_open_file = io.open\n" + LUA.format(stop=args.frames, out=out.as_posix()), encoding="utf-8") command = [ "nice", args.mame, args.machine, "-bios", args.bios, "-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), ] with tempfile.TemporaryDirectory(dir=RUNS) as scratch: if args.cart: cart = args.cart if cart.suffix.lower() == ".zip": with zipfile.ZipFile(cart) as archive: member = archive.infolist()[0] cart = Path(scratch) / Path(member.filename).name cart.write_bytes(archive.read(member)) command += ["-cart", str(cart.resolve())] result = subprocess.run(command, cwd=RUNS, capture_output=True, text=True, check=False) for line in result.stdout.splitlines(): if line.startswith("WRITES"): print(line) if not out.exists(): print(result.stdout[-2000:], result.stderr[-2000:], file=sys.stderr) return 1 return 0 if __name__ == "__main__": sys.exit(main())