# SPDX-License-Identifier: MIT # Reads MAME 0.285 hash/a5200.xml and hash/a5200.hsi (CC0-1.0); the table it # writes keeps that attribution in its rows and in NOTICE. """Build src/a5200_python/cartridge/table.json from MAME's two CC0 hash files plus hand rows. Seeds (docs/cartridge.md "The table and its seeds", open decision 4): - MAME 0.285 ``hash/a5200.xml``: licence CC0-1.0 (file header). Gives SHA-1, CRC-32, size, slot (a5200_rom / a5200_2chips / a5200_bbsb / a5200_supercart), title, year, publisher, supported. - MAME 0.285 ``hash/a5200.hsi``: licence CC0-1.0 (file header). CRC-32 only, with ``A13MIRRORING`` (two 8K chips), ``TYPE16`` (one 16K chip) and ``BOUNTYBOB`` flags; a flag ending in "?" or none at all gives no mapping. - Hand rows in this script, each with its source. Rows are keyed by the payload's SHA-1 (``sha1``) or, for the .hsi, CRC-32 (``crc32``). Every row carries ``source``. Run: python scripts/build_cart_table.py [--hash-dir DIR] """ from __future__ import annotations import argparse import json import sys import xml.etree.ElementTree as ET from pathlib import Path ROOT = Path(__file__).resolve().parents[1] OUT = ROOT / "src" / "a5200_python" / "cartridge" / "table.json" sys.path.insert(0, str(ROOT / "scripts")) from romsets import mame_hashpath # noqa: E402 ($MAME_HASHPATH, default the MAME package's) DEFAULT_HASH_DIRS = [ ROOT / "reference" / "src" / "mame" / "hash", mame_hashpath(), ] SLOT_MAPPING = {"a5200_2chips": "16k-two", "a5200_bbsb": "bbsb", "a5200_supercart": "supercart"} SIZE_MAPPING = {4096: "4k", 8192: "8k", 16384: "16k-one", 32768: "32k", 40960: "bbsb"} # Hand rows: facts from prose sources, matched on MAME software names. # [FAQ] 2.7: titles that support the CX53 Trak-Ball (12). # Marks as given there: * best, # either, X works but not recommended. TRAKBALL_FAQ = { "centiped": "*", "rsfootb": "#", "galaxian": "X", "kaboom": "#", "kshoot": "X", "milliped": "*", "missile": "*", "polepos": "X", "rsbaseb": "X", "rssoccer": "#", "spaceinv": "X", "sbrkout": "#", } # Break on the revision A BIOS: MEASURED here (milestone 6, validation.md: each # copies $FE95-$FEA0 of the 4-port BIOS and fails on revision A); the mechanism # is DOC [AHRM] F.2 (revision A's PAL check shifts the startup code). FOUR_PORT_BIOS = ["kshoot", "mking", "pitfall"] def _hash_dir(given: str | None) -> Path: for candidate in ([Path(given)] if given else []) + DEFAULT_HASH_DIRS: if (candidate / "a5200.xml").exists(): return candidate raise SystemExit("a5200.xml not found; run scripts/fetch_references.py or pass --hash-dir") def _observed_rows(path: Path) -> dict[str, dict]: """Observed-boot rows from a verdict file written after looking at the pictures.""" rows = {} date = path.stem.removeprefix("observed-boot-") lines = [ln for ln in path.read_text("utf-8").splitlines() if ln and not ln.startswith("#")] header = lines[0].split("\t") for line in lines[1:]: cols = dict(zip(header, line.split("\t"), strict=True)) reading = cols["reading"] stem = Path(cols["file"]).stem.replace(" ", "_") row = { "title": "", "file": cols["file"], "kind": "observed-boot", "reading": reading, "frame": 450, "evidence": f"tests/detector_runs/observed_boot/{stem}.png " "(scripts/observed_boot.py --unknown, 900 frames; frames 450 and 900)", "seen": cols["seen"], "runs": f"one chip: {cols['one_chip_run']}; two chips: {cols['two_chip_run']}", "walk": cols["walk"], "date": date, "source": f"measured here: {path.relative_to(ROOT)} (detector-level evidence)", } if reading in ("16k-one", "16k-two"): row["mapping"] = reading rows[cols["sha1"]] = row return rows def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--hash-dir") args = parser.parse_args() hash_dir = _hash_dir(args.hash_dir) by_sha1: dict[str, dict] = {} names: dict[str, str] = {} for software in ET.parse(hash_dir / "a5200.xml").getroot().iter("software"): slot = "a5200_rom" for feature in software.iter("feature"): if feature.get("name") == "slot": slot = feature.get("value", slot) roms = [r for r in software.iter("rom") if r.get("sha1")] if len(roms) != 1: continue # multi-file items (none for loose dumps) rom = roms[0] size = int(rom.get("size", "0"), 0) mapping = SLOT_MAPPING.get(slot) or SIZE_MAPPING.get(size) name = software.get("name", "") row = { "title": (software.findtext("description") or "").strip(), "year": (software.findtext("year") or "").strip(), "publisher": (software.findtext("publisher") or "").strip(), "size": size, "crc32": rom.get("crc", "").lower(), "mame": name, "slot": slot, "supported": software.get("supported", "yes"), "mapping": mapping, "source": "MAME 0.285 hash/a5200.xml (CC0-1.0)", } if name in TRAKBALL_FAQ: row["trakball"] = f"{TRAKBALL_FAQ[name]} ([FAQ] 2.7)" if name in FOUR_PORT_BIOS: row["bios_notes"] = ( "calls 4-port BIOS addresses (measured here, milestone 6; mechanism [AHRM] F.2)" ) by_sha1[rom.get("sha1", "").lower()] = row names[name] = rom.get("sha1", "").lower() by_crc: dict[str, dict] = {} for entry in ET.parse(hash_dir / "a5200.hsi").getroot().iter("hash"): extra = (entry.findtext("extrainfo") or "").strip() mapping = {"A13MIRRORING": "16k-two", "TYPE16": "16k-one", "BOUNTYBOB": "bbsb"}.get(extra) by_crc[entry.get("crc32", "").lower()] = { "title": entry.get("name", ""), "year": (entry.findtext("year") or "").strip(), "flag": extra, "mapping": mapping, "source": "MAME 0.285 hash/a5200.hsi (CC0-1.0)", } missing = [n for n in [*TRAKBALL_FAQ, *FOUR_PORT_BIOS] if n not in names] # The "measured" section holds observed-boot rows entered by hand from # scripts/observed_boot.py runs (docs/cartridge.md "Measured rows"); a # rebuild of the CC0 seed keeps them. measured = {} if OUT.exists(): measured = json.loads(OUT.read_text("utf-8")).get("measured", {}) for path in sorted((ROOT / "tests" / "detectors").glob("observed-boot-*.tsv")): measured.update(_observed_rows(path)) table = { "about": "Built by scripts/build_cart_table.py from MAME's CC0 hash files and hand rows.", "licence": "MIT for this repository's rows; the 'sha1' and 'crc32' seed rows are from " "MAME 0.285 hash/a5200.xml and hash/a5200.hsi, licence CC0-1.0 (the MAME project; " "attribution kept, see NOTICE).", "sha1": dict(sorted(by_sha1.items())), "crc32": dict(sorted(by_crc.items())), "measured": dict(sorted(measured.items())), } OUT.write_text(json.dumps(table, indent=1, sort_keys=False) + "\n", encoding="utf-8") print(f"{OUT.relative_to(ROOT)}: {len(by_sha1)} SHA-1 rows, {len(by_crc)} CRC-32 rows") if missing: print("hand rows naming software not in the list:", ", ".join(missing)) return 0 if __name__ == "__main__": raise SystemExit(main())