# SPDX-License-Identifier: MIT """Census of the PET/CBM firmware and software on this machine, read in place. MAME_ROMPATH=... python -I scripts/census.py firmware # every ROM MAME's PET sets name, by SHA-1 MAME_HASHPATH=... python -I scripts/census.py softlists # MAME's PET software lists (CC0) NO_INTRO_DIR=... PET_SOFTWARE_DIRS=... python -I scripts/census.py software Where the sets live is an environment variable, never a path in this repository: - MAME_ROMPATH: MAME's machine and device sets (pet2001.zip ... c8050.zip). Firmware is identified by SHA-1 and never copied. - MAME_HASHPATH: MAME's hash/ directory (software lists, CC0-1.0). - NO_INTRO_DIR: searched for a PET or CBM folder (No-Intro has none on 2026-11-26). - PET_SOFTWARE_DIRS: os.pathsep-separated folders or single files holding other PET collections (zips, one level of nested zips, or loose files), counted by type and load address. The machine list comes from `mame -listxml` (MAME 0.285); nothing is run but MAME's listing. Hashing and listing only: no emulator runs over the library. """ from __future__ import annotations import collections import hashlib import io import os import pathlib import subprocess import sys import xml.etree.ElementTree as ET import zipfile MACHINES = [ "pet2001", "pet20018", "pet2001j", "pet2001n", "pet2001n16", "pet2001n32", "pet2001b", "pet2001b16", "pet2001b32", "cbm3008", "cbm3016", "cbm3032", "cbm3032b", "pet4016", "pet4032", "pet4032f", "pet4032b", "cbm4016", "cbm4032", "cbm4032f", "cbm4032b", "pet8032", "cbm8032", "cbm8032_de", "cbm8032_fr", "cbm8032_se", "superpet", "mmf9000", "mmf9000_se", "cbm8096", "cbm8296", "cbm8296d", "cbm8296ed", "cbm8296gd", "pet64", ] # fmt: skip DEVICES = ["c2031", "c2040", "c3040", "c4040", "c8050", "c8250", "c8250lp", "sfd1001", "c8280", "c2040_fdc", "c8050fdc", "d9060", "d9090", "pet_superpet", "cbm8000_hsg_a", "cbm8000_hsg_b"] # fmt: skip CBM2 = ["b500", "b128", "b256", "cbm610", "cbm620", "cbm710", "cbm720", "cbm730", "b128hp", "b256hp", "bx256hp", "p500", "p500p"] # fmt: skip SOFTLISTS = ["pet_cass", "pet_flop", "pet_hdd", "pet_quik", "pet_rom", "superpet_flop", "cbm8096_flop", "cbm8296_flop", "cbm2_flop", "cbm2_cart", "p500_flop"] # fmt: skip TYPES = {".prg", ".p00", ".tap", ".t64", ".d64", ".d80", ".d82", ".d67", ".g64", ".bin", ".rom", ".crt"} def need(var: str) -> pathlib.Path: value = os.environ.get(var) if not value: sys.exit(f"set {var}") return pathlib.Path(value) def listxml(names: list[str]) -> ET.Element: out = subprocess.run(["mame", "-listxml", *names], capture_output=True, text=True, check=True).stdout return ET.fromstring(out) def zip_hashes(path: pathlib.Path) -> dict[str, str]: hashes = {} with zipfile.ZipFile(path) as z: for info in z.infolist(): if not info.is_dir(): hashes[hashlib.sha1(z.read(info)).hexdigest()] = info.filename return hashes def firmware() -> None: rompath = need("MAME_ROMPATH") root = listxml(MACHINES + DEVICES + CBM2) print("| set | kind | parent | ROMs | present | missing | ROM name | size | SHA-1 | MAME status |") print("| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |") totals = collections.Counter() wanted = set(MACHINES + DEVICES + CBM2) for m in root: name = m.get("name") if name not in wanted: continue # devices pulled in by the listing's slots kind = "device" if m.get("isdevice") == "yes" else "machine" roms = [r for r in m.findall("rom") if r.get("sha1")] have: dict[str, str] = {} for zname in {name, m.get("romof") or name}: zpath = rompath / f"{zname}.zip" if zpath.exists(): have.update(zip_hashes(zpath)) present = sum(1 for r in roms if r.get("sha1") in have) totals["sets"] += 1 totals["roms"] += len(roms) totals["present"] += present first = True for r in roms: lead = f"| {name} | {kind} | {m.get('cloneof') or ''} | {len(roms)} | {present} | {len(roms) - present} " if first else "| | | | | | " mark = "" if r.get("sha1") in have else " (absent)" print(f"{lead}| {r.get('name')} | {r.get('size')} | `{r.get('sha1')}`{mark} | {r.get('status') or 'good'} |") first = False print(f"\n{totals['sets']} sets, {totals['roms']} ROM entries, {totals['present']} present by SHA-1") def softlists() -> None: hashes = need("MAME_HASHPATH") rompath = os.environ.get("MAME_ROMPATH") print("| list | licence line | entries | parts by interface | sets present under MAME_ROMPATH |") print("| --- | --- | --- | --- | --- |") for name in SOFTLISTS: path = hashes / f"{name}.xml" if not path.exists(): print(f"| {name} | - | missing | | |") continue text = path.read_text(encoding="utf-8", errors="replace") licence = "CC0-1.0" if "license:CC0" in text[:400] else "not stated" root = ET.fromstring(text) soft = root.findall("software") parts = collections.Counter(p.get("interface") for s in soft for p in s.findall("part")) present = "n/a" if rompath: d = pathlib.Path(rompath) / name present = str(len(list(d.glob("*.zip")))) if d.is_dir() else "0 (no folder)" print(f"| {name} | {licence} | {len(soft)} | {dict(parts)} | {present} |") def walk_collection(base: pathlib.Path): """Yield (name, bytes) for every file in base (a folder or one file), opening zips (nested too).""" for path in [base] if base.is_file() else sorted(base.rglob("*")): if path.is_dir(): continue if path.suffix.lower() == ".zip": yield from walk_zip(zipfile.ZipFile(path), path.name) else: yield path.name, path.read_bytes() def walk_zip(z: zipfile.ZipFile, label: str, depth: int = 0): for info in z.infolist(): if info.is_dir(): continue data = z.read(info) if info.filename.lower().endswith(".zip") and depth < 2: yield from walk_zip(zipfile.ZipFile(io.BytesIO(data)), f"{label}/{info.filename}", depth + 1) else: yield f"{label}/{info.filename}", data def software() -> None: nointro = os.environ.get("NO_INTRO_DIR") if nointro: hits = [p.name for p in pathlib.Path(nointro).iterdir() if any(k in p.name for k in ("PET", "CBM"))] print(f"NO_INTRO_DIR: PET/CBM folders: {hits or 'none'}") dirs = [d for d in os.environ.get("PET_SOFTWARE_DIRS", "").split(os.pathsep) if d] if not dirs: print("PET_SOFTWARE_DIRS not set: no other collections counted") for d in dirs: types = collections.Counter() loads = collections.Counter() sizes = [] top = collections.Counter() for name, data in walk_collection(pathlib.Path(d)): ext = os.path.splitext(name)[1].lower() types[ext if ext in TYPES else (ext or "(none)")] += 1 parts = name.split("/") top[parts[1] if len(parts) > 2 else "(top)"] += 1 if ext == ".prg" and len(data) >= 2: loads[f"${data[0] | data[1] << 8:04X}"] += 1 sizes.append(len(data)) print(f"\n## {pathlib.Path(d).name}\nfiles by type: {dict(types.most_common())}") print(f"top-level folders inside: {dict(top.most_common(20))}") print(f"PRG load addresses: {dict(loads.most_common(12))}") if sizes: sizes.sort() print(f"PRG sizes: n={len(sizes)} min={sizes[0]} median={sizes[len(sizes) // 2]} max={sizes[-1]}") def main() -> int: commands = {"firmware": firmware, "softlists": softlists, "software": software} if len(sys.argv) != 2 or sys.argv[1] not in commands: print(__doc__) return 2 commands[sys.argv[1]]() return 0 if __name__ == "__main__": sys.exit(main())