#!/usr/bin/env python """List every MAME machine that instantiates an OKI MSM6295, from `mame -listxml`. python scripts/mame_census.py # writes reference/census/machines.json python scripts/mame_census.py --summary # prints the counts from that file The XML (about 290 MB in MAME 0.285) is streamed, never stored. For each machine with at least one chip named "OKI MSM6295 ADPCM" the script keeps: the driver source file, the parent, each chip's tag and clock, and every ROM whose region is a chip's tag or sits under it (the sample ROMs, as far as the listing can say: a board that maps its samples through an address map names the region differently, and those machines are kept with an empty ROM list). Pin 7 is not in the listing: it is a constructor argument in the driver source. One MAME process, about a minute. Nothing here is committed: the output goes to reference/census/ (ignored). """ from __future__ import annotations import argparse import json import subprocess import sys from collections import Counter from pathlib import Path from xml.etree import ElementTree ROOT = Path(__file__).resolve().parent.parent OUT = ROOT / "reference" / "census" / "machines.json" CHIP_NAME = "OKI MSM6295 ADPCM" #: Device names that hold one of these are counted too: the other OKI voice #: chips, for the question of which of them deserve a core (handoff brief). FAMILY_WORDS = ("OKI", "MSM", "Oki") def collect(mame: str) -> dict: process = subprocess.Popen([mame, "-listxml"], stdout=subprocess.PIPE) machines = {} family: dict[str, list[str]] = {} total = 0 build = "" for event, element in ElementTree.iterparse(process.stdout, events=("start", "end")): if event == "start": if element.tag == "mame": build = element.get("build", "") continue if element.tag != "machine": continue total += 1 for name in {chip.get("name") for chip in element.findall("chip")}: if any(word in name for word in FAMILY_WORDS): family.setdefault(name, []).append(element.get("name")) chips = [ {"tag": chip.get("tag"), "clock": int(chip.get("clock", "0"))} for chip in element.findall("chip") if chip.get("name") == CHIP_NAME ] if chips: tags = {chip["tag"] for chip in chips} roms = [ { "name": rom.get("name"), "size": int(rom.get("size", "0")), "crc": rom.get("crc"), "sha1": rom.get("sha1"), "region": rom.get("region"), "offset": int(rom.get("offset", "0"), 16), "status": rom.get("status", "good"), "merge": rom.get("merge"), } for rom in element.findall("rom") if rom.get("region") in tags or any(rom.get("region", "").startswith(tag + ":") for tag in tags) ] machines[element.get("name")] = { "sourcefile": element.get("sourcefile"), "cloneof": element.get("cloneof"), "romof": element.get("romof"), "description": element.findtext("description"), "year": element.findtext("year"), "manufacturer": element.findtext("manufacturer"), "chips": chips, "roms": roms, } element.clear() process.wait() return {"build": build, "machines_total": total, "machines": machines, "family": family} def summary(data: dict) -> None: machines = data["machines"] parents = {name: m for name, m in machines.items() if not m["cloneof"]} print(f"MAME build: {data['build']}") print(f"machines in the listing: {data['machines_total']}") print(f"machines with an MSM6295: {len(machines)} ({len(parents)} parents)") files = Counter(m["sourcefile"] for m in machines.values()) print(f"driver source files: {len(files)}") chips = Counter(len(m["chips"]) for m in machines.values()) print("chips per machine:", dict(sorted(chips.items()))) clocks = Counter(chip["clock"] for m in machines.values() for chip in m["chips"]) print(f"distinct clocks: {len(clocks)}") for clock, count in clocks.most_common(): print(f" {clock:>9} Hz {count:>5} chips /132 = {clock / 132:9.1f} /165 = {clock / 165:9.1f}") folders = Counter(m["sourcefile"].split("/")[0] for m in machines.values()) print("machines by source folder:") for folder, count in folders.most_common(): print(f" {folder:<14} {count}") print("machines by source file (top 40):") for name, count in files.most_common(40): print(f" {name:<34} {count}") without = sum(1 for m in machines.values() if not m["roms"]) print(f"machines whose sample ROM region is not the chip's tag: {without}") unique = {rom["sha1"] for m in machines.values() for rom in m["roms"] if rom["sha1"]} print(f"distinct sample ROM images (by SHA-1) in tag-named regions: {len(unique)}") print("machines per OKI-family device name:") for name, users in sorted(data.get("family", {}).items(), key=lambda item: -len(item[1])): print(f" {len(users):>5} {name}") def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--mame", default="mame") parser.add_argument("--summary", action="store_true", help="print counts from the saved file") args = parser.parse_args(argv) if not args.summary: data = collect(args.mame) OUT.parent.mkdir(parents=True, exist_ok=True) OUT.write_text(json.dumps(data, indent=1)) print(f"wrote {OUT.relative_to(ROOT)}") elif not OUT.exists(): print("run without --summary first", file=sys.stderr) return 1 summary(json.loads(OUT.read_text())) return 0 if __name__ == "__main__": raise SystemExit(main())