# SPDX-License-Identifier: MIT """Where the romsets are: environment variables only, never a machine path. - ``NO_INTRO_DIR``: the No-Intro root; the 5200 set is its folder ``Atari - Atari 5200`` (183 zips, one of them the BIOS). - ``MAME_ROMPATH``: a MAME ROM directory holding ``a5200.zip`` (and, in a non-merged set, ``a5200a.zip`` for revision A). Merged and non-merged sets both work: BIOS images are found by SHA-1, not by member name. - ``A5200_EXTRA_DIRS`` (optional): more cartridge folders, separated by the path separator. The 2026-11 census also read a front-end copy of 104 7z images, every one of them also in the No-Intro set by SHA-1 (docs/cartridge.md "Census"). - ``MAME_HASHPATH`` (optional): MAME's hash directory, default the Debian package's ``/usr/share/games/mame/hash`` (a program install path, not a romset). ``MAME`` (optional): the MAME program, default ``mame`` on PATH. A script that needs a set calls these and stops with a clear message when the variable is unset or the folder is missing. """ from __future__ import annotations import hashlib import io import os import shutil import zipfile from pathlib import Path NO_INTRO_5200 = "Atari - Atari 5200" BIOS_SHA1 = { "orig": "6ad7a1e8c9fad486fbec9498cb48bf5bc3adc530", # CO19156, 4-port ([AHRM] F.2) "a": "1d2a3f00109d75d2d79fecb565775eb95b7d04d5", # CO19156A, 2-port ([AHRM] F.2) } def _variable(name: str, what: str) -> Path: value = os.environ.get(name) if not value: raise SystemExit(f"{name} is not set: export it to the folder holding {what}") path = Path(value) if not path.is_dir(): raise SystemExit(f"{name}={value} is not a folder (expected {what})") return path def nointro_5200() -> Path: """The No-Intro 5200 folder, ``$NO_INTRO_DIR/Atari - Atari 5200``.""" folder = _variable("NO_INTRO_DIR", f"the No-Intro sets (its '{NO_INTRO_5200}' folder)") folder = folder / NO_INTRO_5200 if not folder.is_dir(): raise SystemExit(f"no '{NO_INTRO_5200}' folder under $NO_INTRO_DIR ({folder.parent})") return folder def mame_rompath() -> Path: """``$MAME_ROMPATH``, the folder holding ``a5200.zip``.""" return _variable("MAME_ROMPATH", "MAME's ROM sets (a5200.zip)") def extra_dirs() -> list[Path]: """The optional ``$A5200_EXTRA_DIRS`` folders that exist.""" value = os.environ.get("A5200_EXTRA_DIRS", "") return [Path(p) for p in value.split(os.pathsep) if p and Path(p).is_dir()] def cartridge_sets() -> list[Path]: """No-Intro first, then any extra folders.""" return [nointro_5200(), *extra_dirs()] def mame_hashpath() -> Path: return Path(os.environ.get("MAME_HASHPATH") or "/usr/share/games/mame/hash") def mame_program() -> str: program = os.environ.get("MAME") or shutil.which("mame") or "/usr/games/mame" return program def mame_bios(revision: str) -> bytes: """The 2K BIOS image ("orig" or "a") from the MAME set, found by SHA-1.""" want = BIOS_SHA1[revision] root = mame_rompath() for name in ("a5200.zip", "a5200a.zip"): path = root / name if not path.is_file(): continue with zipfile.ZipFile(io.BytesIO(path.read_bytes())) as archive: for info in archive.infolist(): if info.file_size == 2048: data = archive.read(info) if hashlib.sha1(data).hexdigest() == want: return data raise SystemExit(f"BIOS {revision} (SHA-1 {want}) not in a5200.zip or a5200a.zip under {root}")