#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Census of the No-Intro Virtual Boy sets, read in place (nothing copied). For every zip in "$NO_INTRO_DIR/Nintendo - Virtual Boy" and "... (Aftermarket)": the ROM's size, CRC32 and SHA-1, and its header (Sacred Tech Scroll, "ROM Header": the 32 bytes at 0xFFFFFDE0, i.e. 0x220 bytes before the end of the ROM: title in Shift JIS, maker code, game code, minor version). Rows are matched to MAME 0.285's software list by SHA-1 to learn which carts carry SRAM (the list's `sram` data area); the ROM itself does not say. NO_INTRO_DIR=... python -I scripts/census.py # summary NO_INTRO_DIR=... python -I scripts/census.py --rows # one line per ROM The "(Private)" folder is never read (family rule: private sets stay out of reports). """ from __future__ import annotations import argparse import collections import hashlib import os import sys import xml.etree.ElementTree as ET import zipfile import zlib from pathlib import Path ROOT = Path(__file__).resolve().parents[1] MAME_LIST = ROOT / "reference" / "mame" / "hash" / "vboy.xml" SETS = ["Nintendo - Virtual Boy", "Nintendo - Virtual Boy (Aftermarket)"] HEADER_FROM_END = 0x220 # 0x100000000 - 0xFFFFFDE0 def mame_rows() -> dict[str, tuple[str, list[str]]]: rows: dict[str, tuple[str, list[str]]] = {} if not MAME_LIST.exists(): return rows for software in ET.parse(MAME_LIST).getroot().findall("software"): areas = [d.get("name", "") for p in software.findall("part") for d in p.findall("dataarea")] for rom in software.iter("rom"): if rom.get("sha1"): rows[rom.get("sha1")] = (software.get("name", ""), areas) return rows def header(data: bytes) -> dict[str, object]: h = data[len(data) - HEADER_FROM_END:][:32] title = h[0:20].rstrip(b"\0 ").decode("shift_jis", "replace") return { "title": title, "reserved_zero": h[20:25] == bytes(5), "maker": h[25:27].decode("ascii", "replace"), "game": h[27:31].decode("ascii", "replace"), "version": h[31], } def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--rows", action="store_true") args = parser.parse_args() base = os.environ.get("NO_INTRO_DIR") if not base: print("set NO_INTRO_DIR", file=sys.stderr) return 2 mame = mame_rows() for name in SETS: folder = Path(base) / name zips = sorted(folder.glob("*.zip")) sizes: collections.Counter[int] = collections.Counter() makers: collections.Counter[str] = collections.Counter() tags: collections.Counter[str] = collections.Counter() in_mame = sram = not_power_of_two = bad_reserved = 0 print(f"== {name}: {len(zips)} zips") for path in zips: with zipfile.ZipFile(path) as archive: members = [m for m in archive.infolist() if not m.is_dir()] if len(members) != 1: print(f" {path.name}: {len(members)} members") data = archive.read(members[0]) sha1 = hashlib.sha1(data).hexdigest() crc = f"{zlib.crc32(data):08x}" h = header(data) sizes[len(data)] += 1 makers[str(h["maker"])] += 1 for tag in ("Proto", "Beta", "Demo", "Sample", "Unl", "Aftermarket", "Rev", "[b]"): if tag in path.name: tags[tag] += 1 if len(data) & (len(data) - 1): not_power_of_two += 1 if not h["reserved_zero"]: bad_reserved += 1 row = mame.get(sha1) if row: in_mame += 1 sram += "sram" in row[1] if args.rows: mark = (row[0] + ("+sram" if "sram" in row[1] else "")) if row else "-" print(f" {len(data):8d} {crc} {sha1} {h['maker']!s:2} {h['game']!s:4} v{h['version']:<3} {mark:16} {path.stem}") print(f" ROM sizes: " + ", ".join(f"{s // 1024} KiB x{n}" for s, n in sorted(sizes.items()))) print(f" not a power of two: {not_power_of_two}; header reserved bytes not zero: {bad_reserved}") print(f" maker codes: " + ", ".join(f"{m!r} x{n}" for m, n in makers.most_common())) print(f" name tags: " + ", ".join(f"{t} x{n}" for t, n in tags.most_common())) print(f" in MAME 0.285's list by SHA-1: {in_mame}; of those with SRAM: {sram}") return 0 if __name__ == "__main__": sys.exit(main())