# SPDX-License-Identifier: MIT """Census of MAME's Sega System 1 / System 2 romsets, read in place (docs/census.md). MAME_ROMPATH=... python -I scripts/census.py # table + summary MAME_ROMPATH=... python -I scripts/census.py --group ppi-big MAME 0.285 is run only as a list tool (`-listsource`, `-listxml`); its source is never read. For every machine MAME drives from `sega/system1.cpp` the script records parent, year, the main CPU (plain Z80, a Sega 315-xxxx / 317-xxxx encrypted Z80, or an MC-8123 with its key ROM), the 8751 MCU and its ROM, the I/O chip MAME fits (Z80 PIO or 8255 PPI), the PSG clocks, the ROM region sizes, and whether `$MAME_ROMPATH/.zip` holds every dumped ROM (by CRC-32 from the zip directory: nothing is decompressed, copied or kept). Output: a table on stdout and reference/census/machines.tsv (git-ignored). Names and hashes are MAME's data (its machine-description XML); no ROM content is written anywhere. The `group` column is this census's own grouping of MAME's machine descriptions (I/O chip and tile-ROM size), not a board name: which group is "System 2" is docs/boards.md's question. """ from __future__ import annotations import argparse import collections import os import subprocess import sys import xml.etree.ElementTree as ET import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] SOURCE = "sega/system1.cpp" def machines(mame: str) -> list[ET.Element]: listing = subprocess.run(["nice", mame, "-listsource"], capture_output=True, text=True, check=True).stdout # fmt: skip names = [line.split()[0] for line in listing.splitlines() if len(line.split()) == 2 and line.split()[1] == SOURCE] # fmt: skip xml = subprocess.run(["nice", mame, "-listxml", *names], capture_output=True, check=True).stdout return [m for m in ET.fromstring(xml) if m.get("isdevice") != "yes" and m.get("name") in names] def region_size(roms: list[ET.Element], region: str) -> int: return sum(int(r.get("size", "0")) for r in roms if r.get("region") == region) def row(machine: ET.Element, rompath: Path | None) -> dict: chips = machine.findall("chip") roms = machine.findall("rom") refs = {d.get("name") for d in machine.findall("device_ref")} cpus = {c.get("tag"): c.get("name") for c in chips if c.get("type") == "cpu"} psgs = sorted(c.get("clock", "?") for c in chips if c.get("name") == "SN76489A") main = cpus.get("maincpu", "-") kind = ("Z80" if main == "Zilog Z80" else "MC-8123" if main == "MC-8123" else main.replace("Sega ", "") if main.startswith("Sega ") else main) # fmt: skip io = "ppi" if "i8255" in refs else "pio" if "z80pio" in refs else "-" tiles = region_size(roms, "tiles") group = f"{io}-{'big' if tiles > 0xC000 else 'small'}" dumped = [r for r in roms if r.get("crc") and r.get("status") != "nodump"] have = "no zip" if rompath is not None: path = rompath / f"{machine.get('name')}.zip" if path.exists(): with zipfile.ZipFile(path) as archive: crcs = {f"{info.CRC:08x}" for info in archive.infolist()} matched = sum(r.get("crc") in crcs for r in dumped) have = "complete" if matched == len(dumped) else f"{matched}/{len(dumped)}" display = machine.find("display") mcu_rom = [r for r in roms if r.get("region") == "mcu"] return { "name": machine.get("name"), "parent": machine.get("cloneof") or "-", "year": machine.findtext("year") or "?", "group": group, "main": kind, "main_key": "yes" if region_size(roms, "maincpu:key") else "-", "mcu": "8751" if cpus.get("mcu") == "Intel 8751" else cpus.get("mcu", "-"), "mcu_rom": ",".join(f"{r.get('name')}({r.get('status', 'good')})" for r in mcu_rom) or "-", "sound_clock": next((c.get("clock") for c in chips if c.get("tag") == "soundcpu"), "-"), "psg_clocks": "+".join(psgs), "program_kb": str(region_size(roms, "maincpu") // 1024), "tiles_kb": str(tiles // 1024), "sprites_kb": str(region_size(roms, "sprites") // 1024), "color_proms": "yes" if region_size(roms, "color_proms") else "-", "rotate": display.get("rotate", "?") if display is not None else "-", "zip": have, "description": machine.findtext("description") or "", } def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--mame", default="mame") parser.add_argument("--group", help="only this group (pio-small, pio-big, ppi-small, ppi-big)") args = parser.parse_args() env = os.environ.get("MAME_ROMPATH") rompath = Path(env) if env else None if rompath is None: print("MAME_ROMPATH is not set: listing without the zip check", file=sys.stderr) rows = [row(m, rompath) for m in machines(args.mame)] if args.group: rows = [r for r in rows if r["group"] == args.group] out = ROOT / "reference" / "census" / "machines.tsv" out.parent.mkdir(parents=True, exist_ok=True) keys = list(rows[0]) lines = ["\t".join(keys)] + ["\t".join(r[k] for k in keys) for r in rows] out.write_text("\n".join(lines) + "\n", encoding="utf-8") def tally(label: str, field: str) -> None: counts = collections.Counter((r["group"], r[field]) for r in rows) print(f"\n{label}:") for (group, value), count in sorted(counts.items()): print(f" {group:10} {value:28} {count:4}") print(f"{len(rows)} machines, {sum(r['parent'] == '-' for r in rows)} parents; " f"table in {out.relative_to(ROOT)}") # fmt: skip tally("Main CPU", "main") tally("MCU", "mcu") tally("Sound Z80 clock", "sound_clock") tally("PSG clocks", "psg_clocks") tally("Colour PROMs", "color_proms") tally("Zip in MAME_ROMPATH", "zip") return 0 if __name__ == "__main__": sys.exit(main())