#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Survey the Super FX game dumps: header, chip family, board, RAM size, MAME's slot. Reads the No-Intro dumps in place (BYOR: ``zipfile`` decompresses each member in memory; nothing is extracted, copied or written but the optional JSON report), and MAME's software list ``snes.xml`` for the board (``slot`` ``lorom_gsu1``/``lorom_gsu2``, ``pcb``, ``nvram`` size), matched by the ROM's SHA-1. python -I scripts/survey_games.py # the GSU titles by name python -I scripts/survey_games.py --json out.json # and keep the rows python -I scripts/survey_games.py --scan-all # every dump's header (slow: ~3,500 zips) What the header can and cannot say (fullsnes "GSU Cartridge Header"; Book II has no header chapter): ``$FFD5`` is ``$20`` (LoROM, SlowROM) on every board; ``$FFD6`` (chipset) ``$13-$1A`` names "GSU" but not which one; ``$FFBD`` (the extended header's expansion RAM size, 2^n KiB) is absent from Star Fox, Starwing, Powerslide and Star Fox 2. The chip family therefore comes from the software list's PCB (fullsnes "GSU PCB Versions"), else from the title. """ from __future__ import annotations import argparse import hashlib import json import os import re import sys import xml.etree.ElementTree as ET import zipfile from pathlib import Path #: The No-Intro SNES set, from NO_INTRO_DIR (the folder holding the No-Intro systems). SNES_SET = "Nintendo - Super Nintendo Entertainment System" NO_INTRO = Path(os.environ["NO_INTRO_DIR"]) / SNES_SET if os.environ.get("NO_INTRO_DIR") else None SOFTLIST = Path("/usr/share/games/mame/hash/snes.xml") TITLES = re.compile( r"^(Star ?Fox|Starwing|Super Star Fox|Stunt Race|Wild Trax|Vortex|Dirt (Racer|Trax)|" r"Power ?Slide|Doom \(|Super Mario World 2|Super Mario - Yoshi Island|Winter Gold|FX Skiing|" r"Comanche|FX Fighter)" ) # fullsnes "GSU PCB Versions" (research tier); the MAME list adds the SFC Box and # prototype boards, matched by prefix. PCB_CHIP = [ ("SHVC-1C0N", "MC1"), ("SNSP-1C0N", "MC1"), ("GS 0871", "MC1"), ("SHVC-1CA0N", "GSU-1"), ("SHVC-1CA6B", "GSU-1"), ("SHVC-1RA2B6S", "GSU-1A"), ("SHVC-1CB5B-20", "GSU-2-SP1"), ("SHVC-1CB", "GSU-2"), ] TITLE_CHIP = [ ("Star Fox 2", "GSU-2"), ("Star Fox", "MC1"), ("Starwing", "MC1"), ("Super Star Fox", "MC1"), ("Power", "GSU-1A"), ("Stunt", "GSU-1"), ("Wild Trax", "GSU-1"), ("Vortex", "GSU-1"), ("Dirt", "GSU-1"), ("Doom", "GSU-2"), ("Super Mario", "GSU-2"), ("Winter Gold", "GSU-2"), ] def software_list() -> dict[str, dict]: """SHA-1 of the ROM data area -> the software entry's name, slot, pcb, nvram size.""" entries: dict[str, dict] = {} if not SOFTLIST.exists(): return entries for sw in ET.parse(SOFTLIST).getroot().iter("software"): for part in sw.iter("part"): features = {f.get("name"): f.get("value") for f in part.iter("feature")} areas = {a.get("name"): a for a in part.iter("dataarea")} rom = areas.get("rom") if rom is None: continue for item in rom.iter("rom"): if item.get("sha1"): nvram = areas.get("nvram") entries[item.get("sha1")] = { "software": sw.get("name"), "description": sw.findtext("description"), "slot": features.get("slot"), "pcb": features.get("pcb"), "nvram": int(nvram.get("size"), 0) if nvram is not None else None, } return entries def read_rom(path: Path) -> bytes | None: with zipfile.ZipFile(path) as archive: members = [m for m in archive.namelist() if m.lower().endswith((".sfc", ".smc"))] if not members: return None data = archive.read(members[0]) if len(data) % 1024 == 512: # copier header data = data[512:] return data def header(rom: bytes) -> dict: """The LoROM header at $7FC0 (the GSU boards are LoROM-headed: fullsnes).""" h = rom[0x7FB0:0x8000] extended = h[0x2A] == 0x33 # $FFDA = $33: the extended header at $FFB0 is valid return { "title": h[0x10:0x25].decode("ascii", "replace").rstrip(), "map": h[0x25], "chipset": h[0x26], "rom_size_byte": h[0x27], "sram_size_byte": h[0x28], "region": h[0x29], "version": h[0x2B], "expansion_ram_kib": (1 << h[0x0D]) if extended and h[0x0D] else None, } def chip_family(name: str, entry: dict | None) -> tuple[str, str]: if entry and entry.get("pcb"): chips = [] # one entry can list several board revisions (Yoshi's Island: -01/-10/-20) for pcb in (p.strip() for p in entry["pcb"].split(",")): chip = next((c for prefix, c in PCB_CHIP if pcb.startswith(prefix)), None) if chip and chip not in chips: chips.append(chip) if chips: return "/".join(chips), f"pcb {entry['pcb']}" for key, chip in TITLE_CHIP: if key in name: return chip, "title" return "?", "unknown" def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--dir", type=Path, default=NO_INTRO) parser.add_argument("--json", type=Path, help="write the rows here") parser.add_argument( "--scan-all", action="store_true", help="read every dump's header, not only the GSU titles" ) args = parser.parse_args() if args.dir is None: raise SystemExit("set NO_INTRO_DIR (or --dir) to find the SNES dumps") listed = software_list() rows = [] for path in sorted(args.dir.glob("*.zip")): if not args.scan_all and not TITLES.match(path.name): continue rom = read_rom(path) if rom is None or len(rom) < 0x8000: continue info = header(rom) if info["chipset"] >> 4 != 1 or not 0x13 <= info["chipset"] <= 0x1A: continue # not a GSU board by its header sha1 = hashlib.sha1(rom).hexdigest() entry = listed.get(sha1) chip, basis = chip_family(path.stem, entry) rows.append( { "file": path.name, "size": len(rom), "sha1": sha1, **info, "mame": entry, "chip": chip, "chip_basis": basis, } ) print( f"{'file':<62} {'KiB':>5} {'map':>4} {'chip':>4} {'xRAM':>5} {'slot':<11} {'family':<10} basis" ) for r in rows: mame = r["mame"] or {} print( f"{r['file'][:62]:<62} {r['size'] // 1024:>5} {r['map']:>4X} {r['chipset']:>4X} " f"{r['expansion_ram_kib'] or '-':>5} {mame.get('slot') or '-':<11} {r['chip']:<10} " f"{r['chip_basis']}" ) print(f"{len(rows)} GSU dumps; {sum(1 for r in rows if r['mame'])} in MAME's software list") if args.json: args.json.write_text(json.dumps(rows, indent=1) + "\n", encoding="utf-8") return 0 if __name__ == "__main__": sys.exit(main())