#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Summarise a MAME trace from scripts/mame_trace.py: counts by mnemonic and by region. python -I scripts/survey_trace.py tests/mame_traces/title-1s.trace Reads lines of the form ``WP=83E0 ST=2000 0060: movb R6,@>0402(R13)`` and prints the number of instructions, the distinct PCs, the mnemonic histogram and how many instructions ran from the console ROM (0000-1FFF, 16-bit bus) against everywhere else (the 8-bit bus behind the multiplexer, docs/timing-and-bus.md). A detector summary only: MAME is a closed box. """ from __future__ import annotations import collections import re import sys from pathlib import Path LINE = re.compile(r"(?:\w+=[0-9A-F]+ )*([0-9A-F]{4}): (\S+)") def main() -> int: if len(sys.argv) != 2: print(__doc__, file=sys.stderr) return 2 mnemonics: collections.Counter[str] = collections.Counter() regions: collections.Counter[str] = collections.Counter() pcs: set[int] = set() total = 0 with Path(sys.argv[1]).open(encoding="ascii", errors="replace") as handle: for line in handle: match = LINE.match(line) if not match: continue pc = int(match.group(1), 16) total += 1 pcs.add(pc) mnemonics[match.group(2)] += 1 regions["console ROM 0000-1FFF" if pc < 0x2000 else "scratchpad 8000-83FF" if 0x8000 <= pc < 0x8400 else "8-bit bus"] += 1 print(f"{total} instructions, {len(pcs)} distinct PCs") for region, count in regions.most_common(): print(f" {count:8} {region}") print("mnemonics:") for name, count in mnemonics.most_common(): print(f" {count:8} {name}") return 0 if __name__ == "__main__": sys.exit(main())