#!/usr/bin/env python3 """Summarise MAME's graphics-mapper tables and per-set board data. Reads the pinned MAME source (``reference/src/mame``, BSD-3-Clause, Paul Leaman and contributors) and prints, for each ``mapper_*`` table of ``cps1_v.cpp``: the bank sizes, the code ranges per layer type, and the comment that says where the table came from ("verified from PAL dump", a guess, ...). Then, per set of ``cps1_config_table``: the extra input/output register offsets (IN2, IN3, OUT2) and the kludge field. MAME is a detector here. The judge for a mapper is the PAL's own fuse map, which ships in the ROM set (docs/gfx-mapping.md). python -I scripts/mapper_table.py # everything python -I scripts/mapper_table.py --summary # one line per mapper """ from __future__ import annotations import argparse import re import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] VIDEO = ROOT / "reference/src/mame/src/mame/capcom/cps1_v.cpp" TYPES = ("SPRITES", "SCROLL1", "SCROLL2", "SCROLL3", "STARS") def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--summary", action="store_true") args = parser.parse_args() text = VIDEO.read_text(encoding="utf-8", errors="replace") start = text.index("static const struct CPS1config cps1_config_table[]") users: dict[str, list[str]] = {} extras = [] for match in re.finditer(r'\{"(\w+)",\s*(\w+),\s*mapper_(\w+)\s*(?:,([^}]*))?\}', text[start:]): users.setdefault(match.group(3), []).append(match.group(1)) if match.group(4) and match.group(4).strip(): extras.append((match.group(1), match.group(2), match.group(4).strip())) pattern = re.compile( r"#define mapper_(\w+)\s+\{([^}]*)\},\s*mapper_\w+_table\s*\n" r"static const struct gfx_range mapper_\w+_table\[\] =\s*\{(.*?)\n\};", re.DOTALL, ) for match in pattern.finditer(text[:start]): name, sizes, body = match.group(1), match.group(2), match.group(3) comments = [c.strip() for c in re.findall(r"//\s*(.*)", body)] origin = next((c for c in comments if "verified" in c.lower() or "guess" in c.lower() or "unknown" in c.lower()), "(no statement)") ranges = re.findall(r"\{\s*([A-Z_0-9 |]+?),\s*(0x[0-9a-fA-F]+),\s*(0x[0-9a-fA-F]+),\s*(\d)\s*\}", body) used = users.get(name, []) print(f"{name}: banks {' '.join(s.strip() for s in sizes.split(','))}; {len(ranges)} ranges; {origin}; sets: {' '.join(used) or '(none)'}") if args.summary: continue for types, low, high, bank in ranges: kinds = "+".join(t for t in TYPES if f"GFXTYPE_{t}" in types) print(f" {kinds:<40} {low}-{high} bank {bank}") if not args.summary: print("\nsets with IN2/IN3/OUT2/kludge fields (set, CPS-B, fields as written):") for name, cpsb, fields in extras: print(f" {name:<12} {cpsb:<13} {fields}") return 0 if __name__ == "__main__": sys.exit(main())