#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Summarise a MAME ARM trace made by scripts/mame_trace.py (counts only, no code). Prints: instructions, distinct PCs, the CPSR mode values seen (26-bit modes have M4 = 0), the I/F bits, and a mnemonic histogram, with the instruction classes that matter for the ARMv3-vs-ARMv4T question singled out (halfword and signed loads, long multiplies, BX, MRS/MSR, SWP, coprocessor instructions, SWI). The trace itself is firmware-derived and stays in the git-ignored tests/mame_traces/; this summary holds counts only. python -I scripts/survey_trace.py tests/mame_traces/boot.trace """ from __future__ import annotations import re import sys from collections import Counter from pathlib import Path LINE = re.compile(r"CPSR=([0-9A-F]{8}) ([0-9A-F]{8}): (\S+)") WATCH = { "halfword/signed loads (ARMv4)": re.compile(r"^(LDR|STR)\w*(H|SH|SB)$"), "long multiply (ARMv3M)": re.compile(r"^[US](MULL|MLAL)"), "BX (ARMv4T)": re.compile(r"^BX"), "MRS/MSR (ARMv3)": re.compile(r"^(MRS|MSR)"), "SWP (ARMv2a+)": re.compile(r"^SWP"), "coprocessor": re.compile(r"^(CDP|LDC|STC|MRC|MCR)"), "SWI": re.compile(r"^SWI"), "MUL/MLA": re.compile(r"^(MUL|MLA)"), } MODES = {0x10: "usr32", 0x11: "fiq32", 0x12: "irq32", 0x13: "svc32", 0x17: "abt32", 0x1B: "und32", 0x1F: "sys32", 0x00: "usr26", 0x01: "fiq26", 0x02: "irq26", 0x03: "svc26"} BASES = sorted( "ADC ADD AND ASR BIC BL BX B CDP CMN CMP EOR LDC LDM LDR LSL LSR MCR MLA MOV MRC MRS MSR " "MUL MVN NOP ORR ROR RRX RSB RSC SBC STC STM STR SUB SWI SWP TEQ TST ADR UMULL UMLAL SMULL " "SMLAL".split(), key=len, reverse=True) CONDS = ("EQ", "NE", "CS", "CC", "HS", "LO", "MI", "PL", "VS", "VC", "HI", "LS", "GE", "LT", "GT", "LE", "AL", "NV") def split_mnemonic(text: str) -> str: """The base mnemonic plus its non-condition suffix (LDMEQDB -> LDMDB, MOVS -> MOVS).""" if len(text) == 3 and text[0] == "B" and text[1:] in CONDS: return "B" # BLT, BLE, BLS are B with a condition, not BL for base in BASES: if text.startswith(base): rest = text[len(base):] if rest[:2] in CONDS: rest = rest[2:] return base + rest return text def main() -> int: path = Path(sys.argv[1]) total = 0 pcs: set[int] = set() modes: Counter[str] = Counter() mnemonics: Counter[str] = Counter() watched: Counter[str] = Counter() with path.open("r", encoding="ascii", errors="replace") as handle: for line in handle: match = LINE.search(line) if not match: continue total += 1 cpsr = int(match.group(1), 16) pcs.add(int(match.group(2), 16)) modes[MODES.get(cpsr & 0x1F, f"?{cpsr & 0x1F:02X}")] += 1 # MAME prints the condition and the S/B/H/addressing suffixes inside the # mnemonic (LDMEQDB, MOVS, LDRNEB); split off the base so the classes compare. base = split_mnemonic(match.group(3)) mnemonics[base] += 1 for name, pattern in WATCH.items(): if pattern.search(base): watched[name] += 1 print(f"trace: {path.name}") print(f"instructions: {total}") print(f"distinct PCs: {len(pcs)}") print("modes: " + ", ".join(f"{m} {n}" for m, n in modes.most_common())) print("classes:") for name in WATCH: print(f" {name}: {watched[name]}") print("top mnemonics:") for name, count in mnemonics.most_common(40): print(f" {name:10} {count:9} {100.0 * count / max(total, 1):6.2f}%") return 0 if __name__ == "__main__": sys.exit(main())