# SPDX-License-Identifier: MIT """Census of the Bally Astrocade library and firmware we hold, read in place. python -I scripts/census.py nointro # $NO_INTRO_DIR/"Bally - Astrocade" (+ tapes) python -I scripts/census.py bios # the BIOS images, by SHA-1 python -I scripts/census.py arcade # the Bally/Midway arcade sets in $MAME_ROMPATH Nothing is copied or written except the report on stdout. Zip members are hashed in memory. `nointro` also matches every cartridge against MAME's `astrocde` software list (hash files are CC0-1.0; found through MAME_HASHPATH or `mame -showconfig`), which is how a cartridge's slot type and the BASIC cartridges missing from No-Intro are found. Where the sets live is an environment variable, never a path in this repository. """ from __future__ import annotations import argparse import collections import hashlib import os import re import subprocess import sys import xml.etree.ElementTree as ET import zipfile from pathlib import Path # The BIOS images MAME 0.285 names (`mame -listroms`), by SHA-1: firmware, never copied. KNOWN_BIOS = { "b902c941997c9d150a560435bf517c6a28137ecc": "Bally Astrocade / Professional Arcade BIOS, 8 KB (MAME astrocde: astro.bin)", "6b2bef5d970e54ed204549f58ba6d197a8bfd3cc": "Bally Home Library Computer BIOS, 8 KB (MAME astrocdl: ballyhlc.bin)", "d84341feec1a0a0e8aa6151b649bc3cf6ef69fbf": "Bally Astrocade 'white' BIOS, 8 KB (MAME astrocdw: bioswhit.bin)", } BIOS_SETS = ("astrocde", "astrocdl", "astrocdw") # MAME 0.285 drivers in midway/astrocde.cpp (`mame -listsource`), the same Data/Address # chips (and the I/O chip) on an arcade board. ARCADE_SETS = ( "seawolf2", "spacezap", "ebases", "wow", "wowg", "gorf", "gorfpgm1", "gorfpgm1f", "gorfpgm1g", "gorfirec", "robby", "profpac", "demndrgn", "tenpindx", ) # fmt: skip FOLDERS = ("Bally - Astrocade", "Bally - Astrocade (Tapes)", "Bally - Astrocade (Tapes) (WAV)") def hashpath() -> Path | None: env = os.environ.get("MAME_HASHPATH") if env: return Path(env) try: out = subprocess.run(["mame", "-showconfig"], capture_output=True, text=True, timeout=60).stdout except (OSError, subprocess.TimeoutExpired): return None match = re.search(r"^hashpath\s+(.+)$", out, re.M) return Path(match.group(1).split(";")[0].strip()) if match else None def softlist(name: str) -> dict[str, tuple[str, str, str]]: """SHA-1 -> (software name, description, slot feature) from a CC0 MAME hash file.""" base = hashpath() if base is None or not (base / f"{name}.xml").exists(): return {} table = {} for soft in ET.parse(base / f"{name}.xml").getroot().iter("software"): slot = next((f.get("value") for f in soft.iter("feature") if f.get("name") == "slot"), "") for rom in soft.iter("rom"): if rom.get("sha1"): table[rom.get("sha1")] = (soft.get("name"), soft.findtext("description", ""), slot) return table def members(path: Path): with zipfile.ZipFile(path) as archive: for info in archive.infolist(): if not info.is_dir(): data = archive.read(info) yield info.filename, len(data), hashlib.sha1(data).hexdigest() def nointro(_: argparse.Namespace) -> int: base = os.environ.get("NO_INTRO_DIR") if not base: print("set NO_INTRO_DIR", file=sys.stderr) return 2 soft = softlist("astrocde") matched_names = set() for folder_name in FOLDERS: folder = Path(base) / folder_name if not folder.is_dir(): print(f"{folder_name}: missing") continue zips = sorted(p for p in folder.iterdir() if p.suffix == ".zip") sizes, exts, tags, slots = (collections.Counter() for _ in range(4)) unmatched = [] for path in zips: for group in re.findall(r"\(([^)]*)\)|\[([^\]]*)\]", path.name): tag = group[0] or group[1] if tag not in ("USA",): tags[re.sub(r" \d+$", "", tag)] += 1 for member, size, sha1 in members(path): exts[Path(member).suffix.lower()] += 1 sizes[size] += 1 hit = soft.get(sha1) if hit: matched_names.add(hit[0]) slots[hit[2] or "(standard)"] += 1 elif not path.name.startswith("[BIOS]"): unmatched.append(path.name) print(f"== {folder_name}: {len(zips)} zips") print(" member sizes:", dict(sorted(sizes.items()))) print(" extensions:", dict(exts)) print(" tags:", dict(tags.most_common())) if soft: print(" matched in MAME softlist astrocde, by slot:", dict(slots)) print(f" not in the softlist ({len(unmatched)}):", "; ".join(unmatched) or "-") if soft: missing = sorted({v[0] + ": " + v[1] for v in soft.values() if v[0] not in matched_names}) print(f"softlist entries with no No-Intro match: {len(missing)}") for line in missing: if re.search(r"basic|bas\b", line, re.I): print(" BASIC:", line) return 0 def bios(_: argparse.Namespace) -> int: seen = collections.defaultdict(list) base = os.environ.get("NO_INTRO_DIR") if base: for path in sorted((Path(base) / FOLDERS[0]).glob("[[]BIOS[]]*.zip")): for member, size, sha1 in members(path): seen[sha1].append(f"No-Intro {path.name} :: {member} ({size} bytes)") rompath = os.environ.get("MAME_ROMPATH") if rompath: for name in BIOS_SETS: path = Path(rompath) / f"{name}.zip" if path.exists(): for member, size, sha1 in members(path): seen[sha1].append(f"MAME {name}.zip :: {member} ({size} bytes)") for sha1, where in sorted(seen.items()): print(sha1, KNOWN_BIOS.get(sha1, "UNKNOWN image")) for line in where: print(" ", line) absent = [v for k, v in KNOWN_BIOS.items() if k not in seen] print(f"{len(seen)} distinct images held; known images not held: {absent or 'none'}") return 0 def arcade(_: argparse.Namespace) -> int: rompath = os.environ.get("MAME_ROMPATH") if not rompath: print("set MAME_ROMPATH", file=sys.stderr) return 2 for name in ARCADE_SETS: path = Path(rompath) / f"{name}.zip" if path.exists(): files = list(members(path)) print(f" {name:10} {len(files):3} files {sum(f[1] for f in files):7} bytes") else: print(f" {name:10} missing") return 0 def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("what", choices=("nointro", "bios", "arcade")) args = parser.parse_args() return {"nointro": nointro, "bios": bios, "arcade": arcade}[args.what](args) if __name__ == "__main__": sys.exit(main())