# SPDX-License-Identifier: MIT """Census of the Atari 8-bit library and firmware, read in place by environment variable. NO_INTRO_DIR=... python -I scripts/census.py media # the No-Intro "Atari - 8-bit Family" set MAME_ROMPATH=... python -I scripts/census.py firmware # OS, BASIC, XEGS and drive ROMs by SHA-1 `media` hashes every member of every zip (never extracts to disk), names its container format from its header (ATR, ATX, SCP flux, raw cartridge), reads an ATR's sector size and count, and looks raw cartridges up by SHA-1 in MAME 0.285's CC0 software lists (fetched to reference/softlists/ by fetch_references.py) for the slot type MAME gives. `firmware` asks the installed MAME for each machine's ROM list (`mame -listxml`, run as a closed box) and looks for each SHA-1 in the zips under MAME_ROMPATH. Nothing is copied, nothing is written outside stdout. Firmware is named by SHA-1 only. """ from __future__ import annotations import argparse import collections import hashlib import os import subprocess import sys import xml.etree.ElementTree as ET import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] SET = "Atari - 8-bit Family" SOFTLISTS = ("a800.xml", "xegs.xml", "a800_flop.xml", "a800_cass.xml") MACHINES = ("a400", "a400pal", "a800", "a800pal", "a1200xl", "a600xl", "a800xl", "a800xlp", "a65xe", "a65xea", "a130xe", "a800xe", "xegs", "atari810", "atari1050") def need(var: str) -> Path: value = os.environ.get(var) if not value: sys.exit(f"census: set {var} (see the family's romset rule)") return Path(value) def container(data: bytes) -> str: if data[:2] == b"\x96\x02": return "atr" if data[:4] == b"AT8X": return "atx" if data[:3] == b"SCP": return "scp" if data[:4] == b"CART": return "car" if data[:4] == b"FUJI": return "cas" # No header: a raw cartridge dump (a $FF $FF start is not evidence of an # executable: several raw cartridges begin that way). return "raw" def atr_geometry(data: bytes) -> str: paragraphs = int.from_bytes(data[2:4], "little") | (data[6] << 16) sector = int.from_bytes(data[4:6], "little") body = paragraphs * 16 # The first three sectors are 128 bytes even on a double-density image. count = body // 128 if sector == 128 else 3 + (body - 384) // sector return f"{sector}-byte sectors, {count} sectors" def softlist_index() -> dict[str, tuple[str, str, str]]: index: dict[str, tuple[str, str, str]] = {} for name in SOFTLISTS: path = ROOT / "reference" / "softlists" / name if not path.exists(): continue for sw in ET.parse(path).getroot().iter("software"): for part in sw.iter("part"): slot = next((f.get("value") for f in part.iter("feature") if f.get("name") == "slot"), part.get("interface", "")) for rom in part.iter("rom"): if rom.get("sha1"): index[rom.get("sha1")] = (name, sw.get("name"), slot) return index def media() -> int: folder = need("NO_INTRO_DIR") / SET index = softlist_index() if not index: print("note: reference/softlists/ is empty; run scripts/fetch_references.py first") kinds: collections.Counter[str] = collections.Counter() slots: collections.Counter[str] = collections.Counter() sizes: collections.Counter[tuple[str, int]] = collections.Counter() zips = sorted(folder.glob("*.zip")) for path in zips: with zipfile.ZipFile(path) as archive: for info in archive.infolist(): data = archive.read(info) kind = container(data) sha1 = hashlib.sha1(data).hexdigest() kinds[kind] += 1 sizes[(kind, len(data))] += 1 extra = "" suffix = Path(info.filename).suffix.lower().lstrip(".") if suffix != kind and not (suffix == "bin" and kind == "raw"): extra = f"named .{suffix}, contents {kind}" kinds[f"name/contents mismatch"] += 1 if kind == "atr": extra = (extra + "; " if extra else "") + atr_geometry(data) hit = index.get(sha1) if hit: slots[hit[2]] += 1 extra = (extra + "; " if extra else "") + f"MAME {hit[0]}:{hit[1]} slot {hit[2]}" elif kind == "raw": slots["(not in MAME's lists)"] += 1 print(f"{sha1} {len(data):>9} {kind:4} {info.filename}" + (f" [{extra}]" if extra else "")) print(f"\n{len(zips)} zips; members by container: " + ", ".join(f"{k} {v}" for k, v in sorted(kinds.items()))) print("sizes: " + ", ".join(f"{k} {s} x{v}" for (k, s), v in sorted(sizes.items()))) print("cartridge slot types (MAME 0.285 CC0 lists): " + ", ".join(f"{k} {v}" for k, v in sorted(slots.items()))) return 0 def firmware() -> int: rompath = need("MAME_ROMPATH") have: dict[str, str] = {} for name in MACHINES: zpath = rompath / f"{name}.zip" if zpath.exists(): with zipfile.ZipFile(zpath) as archive: for info in archive.infolist(): have.setdefault(hashlib.sha1(archive.read(info)).hexdigest(), f"{name}.zip") seen: set[str] = set() for name in MACHINES: out = subprocess.run(["mame", "-listxml", name], capture_output=True, text=True, check=False).stdout for machine in ET.fromstring(out).iter("machine"): if machine.get("name") != name: continue print(f"## {name}: {machine.findtext('description')}") for rom in machine.iter("rom"): sha1 = rom.get("sha1") flag = rom.get("status") or "good" where = have.get(sha1, "MISSING") tag = "" if sha1 not in seen else " (listed above)" seen.add(sha1) print(f" {sha1} {rom.get('size'):>6} {flag:8} {rom.get('bios') or '-':8} " f"{rom.get('name')} [{where}]{tag}") composites(rompath) return 0 # Emulators load the 400/800 OS and the 1200XL OS as one composite file; MAME keeps the # chips apart. The composite SHA-1s are AHRM appendix F's (DOC); the chip order is the # address order ($D800 floating point, then $E000, then $F000). COMPOSITES = ( ("a400", ("co12399b.rom", "co12499a.rom", "co14599a.rom"), "f821911f16ed613d72d4b255b3e68ab2d07041ac", "400/800 OS-A NTSC"), ("a400", ("co12399b.rom", "co12499b.rom", "co14599b.rom"), "db1031585968cfc6ec2ecda5c9a5a52f61444a3b", "400/800 OS-B NTSC"), ("a400pal", ("co12399b.rom", "co15199.rom", "co15299.rom"), None, "400/800 OS-A PAL (no AHRM row)"), ("a1200xl", ("co60616a.rom", "co60617a.rom"), "83c9afee8828a4026c55f10f920c42f76ad9ff65", "1200XL OS (MAME rev10)"), ("a1200xl", ("co60616b.rom", "co60617b.rom"), None, "1200XL OS (MAME rev11, no AHRM row)"), ) def composites(rompath: Path) -> None: print("## composites (chips joined in address order)") for zname, chips, expect, label in COMPOSITES: zpath = rompath / f"{zname}.zip" if not zpath.exists(): print(f" {label}: {zname}.zip MISSING") continue with zipfile.ZipFile(zpath) as archive: sha1 = hashlib.sha1(b"".join(archive.read(c) for c in chips)).hexdigest() verdict = "no reference" if expect is None else ("matches AHRM F" if sha1 == expect else "DIFFERS from AHRM F") print(f" {sha1} {label}: {' + '.join(chips)} [{verdict}]") def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("what", choices=("media", "firmware")) args = parser.parse_args() return media() if args.what == "media" else firmware() if __name__ == "__main__": sys.exit(main())