#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Census of save hardware and ROM sizes over the local No-Intro GBA sets, read in place. Nintendo's SDK links a library ID string into every ROM that uses a save chip (``SRAM_V``, ``SRAM_F_V``, ``EEPROM_V``, ``FLASH_V``, ``FLASH512_V``, ``FLASH1M_V``) and ``SIIRTC_V`` for the Seiko real-time clock on the GPIO port (GBATEK "GBA Cart Backup IDs", "GBA Cart Real-Time Clock"). Emulators detect the save type from these strings, so this census says which save chips and how many of them the board must model to run what was sold. The ROMs are read from the zips where they are; nothing is copied. The directory defaults to ``$NO_INTRO_DIR/Nintendo - Game Boy Advance``; ``GBA_NOINTRO_DIR`` or ``--dir`` moves it. python scripts/count_saves.py # the licensed set python scripts/count_saves.py --json counts.json """ from __future__ import annotations import argparse import collections import json import os import sys import zipfile from concurrent.futures import ProcessPoolExecutor from pathlib import Path DEFAULT_DIR = Path( os.environ.get( "GBA_NOINTRO_DIR", str(Path(os.environ.get("NO_INTRO_DIR", ".")) / "Nintendo - Game Boy Advance"), ) ) # Longest first: FLASH1M_V and FLASH512_V also contain FLASH, SRAM_F_V is not SRAM_V. TAGS = (b"FLASH1M_V", b"FLASH512_V", b"FLASH_V", b"EEPROM_V", b"SRAM_F_V", b"SRAM_V") RTC = b"SIIRTC_V" def scan(path: Path) -> dict: try: with zipfile.ZipFile(path) as archive: names = [n for n in archive.namelist() if n.lower().endswith(".gba")] if not names: return {"name": path.name, "error": "no .gba"} rom = archive.read(names[0]) except (zipfile.BadZipFile, OSError) as error: return {"name": path.name, "error": str(error)} tags = [tag.decode() for tag in TAGS if rom.find(tag) >= 0] return { "name": path.name, "size": len(rom), "tags": tags, "rtc": rom.find(RTC) >= 0, "code": rom[0xAC:0xB0].decode("ascii", "replace"), } def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--dir", type=Path, default=DEFAULT_DIR) parser.add_argument("--json", type=Path, help="write every ROM's result here") args = parser.parse_args() zips = sorted(p for p in args.dir.glob("*.zip") if not p.name.startswith("[BIOS]")) with ProcessPoolExecutor() as pool: results = list(pool.map(scan, zips, chunksize=8)) ok = [r for r in results if "error" not in r] kinds: collections.Counter[str] = collections.Counter() for r in ok: # 46 ROMs carry IDs of two families (a library linked but unused); count each # under the first ID in TAGS order (Flash, then EEPROM, then SRAM). kinds[r["tags"][0] if r["tags"] else "none"] += 1 sizes = collections.Counter(r["size"] >> 20 for r in ok) multi = sum(len({t.split("_")[0] for t in r["tags"]}) > 1 for r in ok) print(f"{len(ok)} ROMs read in place from {args.dir} ({len(results) - len(ok)} unreadable)") for kind, count in kinds.most_common(): print(f" {kind:12} {count:5} {100 * count / len(ok):5.1f} %") print(f" RTC (SIIRTC_V) {sum(r['rtc'] for r in ok)}") print(f" more than one save family named: {multi}") print(" sizes (MiB):", ", ".join(f"{s}: {c}" for s, c in sorted(sizes.items()))) if args.json: args.json.write_text(json.dumps(results, indent=1) + "\n", encoding="utf-8") return 0 if __name__ == "__main__": sys.exit(main())