# SPDX-License-Identifier: MIT """Census of the No-Intro WonderSwan sets and of the boot ROMs we hold, read in place. python -I scripts/census.py nointro [--checksum] # $NO_INTRO_DIR python -I scripts/census.py bios # $MAME_ROMPATH, $NO_INTRO_DIR's [BIOS] entries, $WS_BIOS_DIR Nothing is copied or extracted to disk; each cartridge is read from inside its zip and only its last sixteen bytes (the header, WSdev "ROM header") are decoded. With --checksum the whole image is summed and compared with the header's checksum field. `bios` hashes the boot ROMs and internal EEPROM images inside MAME's sets and No-Intro's [BIOS] entries by SHA-1. Where the sets live is an environment variable, never a path in this repository: NO_INTRO_DIR is the directory holding No-Intro's folders (their own names below). """ from __future__ import annotations import argparse import collections import hashlib import os import re import sys import zipfile from pathlib import Path FOLDERS = ( "Bandai - WonderSwan", "Bandai - WonderSwan (Aftermarket)", "Bandai - WonderSwan Color", "Bandai - WonderSwan Color (Aftermarket)", "Benesse - Pocket Challenge V2", ) EXTENSIONS = (".ws", ".wsc", ".pc2", ".bin") # WSdev "ROM header" (pinned revision): byte $A ROM size, $B save type, $C flags, $D mapper. ROM_SIZE = {0x00: "128K", 0x01: "256K", 0x02: "512K", 0x03: "1M", 0x04: "2M", 0x05: "3M", 0x06: "4M", 0x07: "6M", 0x08: "8M", 0x09: "16M", 0x0A: "32M", 0x0B: "64M"} # fmt: skip SAVE = {0x00: "none", 0x01: "SRAM 32K", 0x02: "SRAM 32K", 0x03: "SRAM 128K", 0x04: "SRAM 256K", 0x05: "SRAM 512K", 0x10: "EEPROM 128B", 0x20: "EEPROM 2K", 0x50: "EEPROM 1K"} # fmt: skip MAPPER = {0x00: "2001 (or KARNAK)", 0x01: "2003"} # Images MAME 0.285 names (mame -listxml, hashed in place 2026-11-26), by SHA-1. KNOWN = { "4015bcacea76bb0b5bbdb13c5358f7e1abb986a1": "boot ROM, WonderSwan (ASWAN), 4 KiB (wswan, pockchv2 [baddump there]: boot.rom; No-Intro [BIOS] WonderSwan Boot ROM)", "c5ad0b8af45d762662a69f50b64161b9c8919efb": "boot ROM, WonderSwan Color (SPHINX), 8 KiB (wscolor: boot.rom; No-Intro [BIOS] WonderSwan Color Boot ROM)", "8270c4a72624692842a3cb9f195058f2ffba48c5": "boot ROM, SwanCrystal (SPHINX2), 8 KiB (not in MAME 0.285; No-Intro [BIOS] SwanCrystal Boot ROM)", "7b76c3d59c9add9501f95e8bfc34427773fcbd28": "internal EEPROM image, WonderSwan, 128 B (wswan, pockchv2: internal_eeprom.ws; MAME: baddump)", "0951845f01f83bee497268a63b5fb7baccfeff7c": "internal EEPROM image, WonderSwan Color, 2 KiB (wscolor: internal_eeprom.wsc; MAME: baddump)", } MAME_SETS = ("wswan", "wscolor", "pockchv2", "pockchal") def rom_member(archive: zipfile.ZipFile) -> zipfile.ZipInfo | None: for info in archive.infolist(): if info.filename.lower().endswith(EXTENSIONS): return info return None def tags(name: str) -> list[str]: return [t for group in re.findall(r"\(([^)]*)\)", name) for t in group.split(", ")] def nointro(args: argparse.Namespace) -> int: base = os.environ.get("NO_INTRO_DIR") if not base: print("set NO_INTRO_DIR to the directory holding No-Intro's folders", file=sys.stderr) return 2 for folder in FOLDERS: path = Path(base) / folder zips = sorted(path.glob("*.zip")) if path.is_dir() else [] counts: dict[str, collections.Counter] = collections.defaultdict(collections.Counter) bad_sum = 0 for item in zips: with zipfile.ZipFile(item) as archive: info = rom_member(archive) if info is None: counts["member"]["no ROM member"] += 1 continue data = archive.read(info) head = data[-16:] counts["extension"][Path(info.filename).suffix.lower()] += 1 counts["size"][f"{len(data) // 1024} KiB"] += 1 counts["color byte"]["colour" if head[7] & 1 else "mono"] += 1 counts["mapper"][MAPPER.get(head[13], f"unknown ${head[13]:02X}")] += 1 counts["save"][SAVE.get(head[11], f"unknown ${head[11]:02X}")] += 1 counts["rom size byte"][ROM_SIZE.get(head[10], f"unknown ${head[10]:02X}")] += 1 counts["orientation"]["vertical" if head[12] & 1 else "horizontal"] += 1 counts["bus"]["16-bit" if head[12] & 4 else "8-bit"] += 1 counts["far jump"]["EA" if head[0] == 0xEA else f"${head[0]:02X}"] += 1 for tag in tags(item.name): if tag in ("WonderWitch", "Unl", "Aftermarket", "Proto", "Beta", "Demo", "Program", "Sample"): counts["tag"][tag] += 1 if args.checksum: total = (sum(data) - head[14] - head[15]) & 0xFFFF if total != head[14] | head[15] << 8: bad_sum += 1 kind = "WonderWitch" if "WonderWitch" in tags(item.name) else "other" counts["checksum mismatch"][kind] += 1 print(f"== {folder}: {len(zips)} zips") for key in sorted(counts): row = ", ".join(f"{k} {v}" for k, v in counts[key].most_common()) print(f" {key}: {row}") if args.checksum: print(f" header checksum mismatches: {bad_sum}") return 0 def bios(_: argparse.Namespace) -> int: places: list[Path] = [] rompath = os.environ.get("MAME_ROMPATH") if rompath: places += [Path(p) / f"{s}.zip" for p in rompath.split(";") for s in MAME_SETS] nointro = os.environ.get("NO_INTRO_DIR") if nointro: for folder in FOLDERS: path = Path(nointro) / folder if path.is_dir(): places += sorted(path.glob("[[]BIOS[]]*.zip")) extra = os.environ.get("WS_BIOS_DIR") if extra: places += sorted(p for p in Path(extra).iterdir() if p.is_file()) if not places: print("set MAME_ROMPATH and/or NO_INTRO_DIR (and optionally WS_BIOS_DIR)", file=sys.stderr) return 2 seen = set() for place in places: if not place.exists(): continue if place.suffix == ".zip": with zipfile.ZipFile(place) as archive: items = [(f"{place.stem}:{i.filename}", archive.read(i)) for i in archive.infolist()] else: items = [(place.name, place.read_bytes())] for label, data in items: digest = hashlib.sha1(data).hexdigest() seen.add(digest) print(f"{digest} {len(data):6} {label}: {KNOWN.get(digest, 'not a known image')}") for digest, what in KNOWN.items(): if digest not in seen: print(f"MISSING {digest} {what}") return 0 def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) sub = parser.add_subparsers(dest="command", required=True) one = sub.add_parser("nointro") one.add_argument("--checksum", action="store_true", help="sum each image against its header") sub.add_parser("bios") args = parser.parse_args() return {"nointro": nointro, "bios": bios}[args.command](args) if __name__ == "__main__": sys.exit(main())