#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Count the TI-99/4A CPU's memory-mapped device accesses in MAME (a closed-box detector, tier 5). Runs MAME headless on a TI-99/4A machine for --frames frames after --start, with read and write taps on the CPU's program space at >8000->9FFF, and prints how many accesses each device block received (the 1 KiB blocks of the console technical data: scratchpad, sound, VDP read/write, speech read/write, GROM read/write; docs/memory-and-bus.md), split by read and write, plus the accesses to each 8 KiB block of the rest of the map. The counts are the channel traffic docs/domains-and-speed.md uses. Nothing of MAME's is read but its output. MAME_ROMPATH=... python -I scripts/mame_bus_census.py --machine ti99_4a --start 120 --frames 60 MAME runs in tests/mame_runs/ (git-ignored). A cartridge from MAME's software list can be inserted with --cart (MAME's -cart option). """ 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_runs" LUA = """ space = manager.machine.devices[":maincpu"].spaces["program"] counts = {} function bump(key) counts[key] = (counts[key] or 0) + 1 end function block(offset) if offset >= 0x8000 and offset < 0xA000 then return string.format("%04X", offset & 0xFC00) end return string.format("%04X", offset & 0xE000) end armed = false rtap = space:install_read_tap(0x0000, 0xFFFF, "census_r", function(offset, data, mask) if armed then bump("R " .. block(offset)) end end) wtap = space:install_write_tap(0x0000, 0xFFFF, "census_w", function(offset, data, mask) if armed then bump("W " .. block(offset)) end end) frames = 0 t0 = 0 sub = emu.add_machine_frame_notifier(function() frames = frames + 1 if frames == {start} then armed = true; t0 = manager.machine.time:as_double() end if frames == {stop} then armed = false print(string.format("WINDOW %.6f s", manager.machine.time:as_double() - t0)) keys = {} for k in pairs(counts) do table.insert(keys, k) end table.sort(keys) for _, k in ipairs(keys) do print(string.format("COUNT %s %d", k, counts[k])) end manager.machine:exit() end end) """ NAMES = { "8000": "scratchpad RAM (8000-83FF)", "8400": "sound (8400-87FF)", "8800": "VDP read (8800-8BFF)", "8C00": "VDP write (8C00-8FFF)", "9000": "speech read (9000-93FF)", "9400": "speech write (9400-97FF)", "9800": "GROM read (9800-9BFF)", "9C00": "GROM write (9C00-9FFF)", "0000": "console ROM (0000-1FFF)", "2000": "low expansion (2000-3FFF)", "4000": "DSR space (4000-5FFF)", "6000": "cartridge ROM (6000-7FFF)", "A000": "high expansion (A000-BFFF)", "C000": "high expansion (C000-DFFF)", "E000": "high expansion (E000-FFFF)", } def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--machine", default="ti99_4a") parser.add_argument("--start", type=int, default=120) parser.add_argument("--frames", type=int, default=60) parser.add_argument("--cart", help="cartridge for MAME's -cart option") 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 the ti99_4a set)", file=sys.stderr) return 2 RUNS.mkdir(parents=True, exist_ok=True) script = RUNS / "census.lua" script.write_text(LUA.replace("{start}", str(args.start)).replace( "{stop}", str(args.start + args.frames)), encoding="utf-8") command = [ "nice", args.mame, args.machine, "-rompath", rompath, "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-autoboot_script", str(script), "-seconds_to_run", str(max(5, (args.start + args.frames) // 50 + 5)), ] if args.cart: command += ["-cart", args.cart] result = subprocess.run(command, cwd=RUNS, capture_output=True, text=True, check=False) lines = [line for line in result.stdout.splitlines() if line.startswith(("WINDOW", "COUNT"))] if not lines: print(result.stdout[-2000:], result.stderr[-2000:], sep="\n", file=sys.stderr) return 1 for line in lines: if line.startswith("WINDOW"): print(line) continue _, direction, key, count = line.split() print(f"{direction} {int(count):9} {NAMES.get(key, key)}") return 0 if __name__ == "__main__": sys.exit(main())