# SPDX-License-Identifier: MIT """Census of the Atari ST library and firmware, read in place (nothing copied). python -I scripts/census.py nointro # $NO_INTRO_DIR/"Atari - Atari ST" python -I scripts/census.py tosec # $TOSEC_DIR, if the TOSEC ST sets are there python -I scripts/census.py firmware # TOS and IKBD images in $MAME_ROMPATH python -I scripts/census.py firmware --dir D # ... and any loose directory (BIOS packs) Locations come only from environment variables (NO_INTRO_DIR, TOSEC_DIR, MAME_ROMPATH) or the command line; no machine path is written here. Firmware is identified by SHA-1 and never copied. Floppy images are opened only to read their container headers (the IPF "CAPS" record, the MSA and STX magic numbers); nothing is decoded or run. """ from __future__ import annotations import argparse import collections import hashlib import os import re import sys import zipfile from pathlib import Path ST_SETS = "Atari - Atari ST" # MAME 0.285 romsets of the ST family (atari/atarist.cpp); the keyboard MCU is its own set. MAME_SETS = [ "st", "st_de", "st_es", "st_fr", "st_nl", "st_se", "st_sg", "st_uk", "megast", "megast_de", "megast_fr", "megast_se", "megast_sg", "megast_uk", "ste", "ste_de", "ste_es", "ste_fr", "ste_it", "ste_se", "ste_sg", "ste_uk", "megaste", "megaste_de", "megaste_es", "megaste_fr", "megaste_it", "megaste_se", "megaste_uk", "st_kbd", ] # fmt: skip MAGIC = {b"CAPS": "IPF", b"\x0e\x0f": "MSA", b"RSY\x00": "STX"} def sha1(data: bytes) -> str: return hashlib.sha1(data).hexdigest() def kind(name: str, head: bytes) -> str: for magic, label in MAGIC.items(): if head.startswith(magic): return label suffix = Path(name).suffix.lower().lstrip(".") return suffix.upper() or "?" def nointro(root: Path) -> int: folder = root / ST_SETS if not folder.is_dir(): print(f"no folder {ST_SETS!r} under NO_INTRO_DIR", file=sys.stderr) return 2 zips = sorted(folder.glob("*.zip")) kinds: collections.Counter[str] = collections.Counter() regions: collections.Counter[str] = collections.Counter() tags: collections.Counter[str] = collections.Counter() per_zip: collections.Counter[int] = collections.Counter() titles: set[str] = set() sizes: collections.Counter[int] = collections.Counter() for path in zips: name = path.stem groups = re.findall(r"\(([^)]*)\)", name) regions[groups[0] if groups else "-"] += 1 for group in groups[1:]: for tag in ("Disk", "Rev", "Demo", "Beta", "Proto", "Unl", "Alt", "Budget"): if group.startswith(tag): tags[tag] += 1 titles.add(re.sub(r"\s*\(.*", "", name)) with zipfile.ZipFile(path) as archive: members = [info for info in archive.infolist() if not info.is_dir()] per_zip[len(members)] += 1 for info in members: with archive.open(info) as handle: head = handle.read(16) kinds[kind(info.filename, head)] += 1 sizes[info.file_size // 1024] += 1 print(f"{len(zips)} zips, {sum(kinds.values())} images, {len(titles)} titles") print("containers:", dict(kinds.most_common())) print("images per zip:", dict(sorted(per_zip.items()))) print("first region tag:", dict(regions.most_common())) print("tags:", dict(tags.most_common())) small = sum(count for size, count in sizes.items() if size < 1024) print(f"images under 1 MB: {small}; largest: {max(sizes)} KB") return 0 def tosec(root: Path) -> int: hits = [p for p in root.rglob("*") if p.is_dir() and "Atari ST" in p.name] print(f"{len(hits)} TOSEC folders naming the Atari ST under TOSEC_DIR") for path in hits[:40]: print(" ", path.relative_to(root)) return 0 def firmware(rompath: Path | None, extra: list[Path]) -> int: seen: dict[str, list[str]] = collections.defaultdict(list) sizes: dict[str, int] = {} missing = [] if rompath is not None: for name in MAME_SETS: path = rompath / f"{name}.zip" if not path.exists(): missing.append(name) continue with zipfile.ZipFile(path) as archive: for info in archive.infolist(): data = archive.read(info) digest = sha1(data) seen[digest].append(f"{name}.zip:{info.filename}") sizes[digest] = len(data) for folder in extra: for path in sorted(folder.rglob("*")): if path.is_file() and path.stat().st_size in range(16 << 10, (1 << 20) + 4096): low = path.name.lower() if "tos" in low: # tos*.img, etos*.img, tos104.rom, ... data = path.read_bytes() digest = sha1(data) seen[digest].append(f"{folder.name}/{path.relative_to(folder)}") sizes[digest] = len(data) for digest, where in sorted(seen.items(), key=lambda item: item[1][0]): print(f"{digest} {sizes[digest]:>7} {where[0]}" + (f" (+{len(where) - 1})" if where[1:] else "")) print(f"{len(seen)} distinct images; MAME sets missing: {missing or 'none'}") return 0 def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("what", choices=["nointro", "tosec", "firmware"]) parser.add_argument("--dir", action="append", default=[], type=Path) args = parser.parse_args() if args.what == "nointro": root = os.environ.get("NO_INTRO_DIR") if not root: print("set NO_INTRO_DIR", file=sys.stderr) return 2 return nointro(Path(root)) if args.what == "tosec": root = os.environ.get("TOSEC_DIR") if not root: print("set TOSEC_DIR (the TOSEC sets' folder)", file=sys.stderr) return 2 return tosec(Path(root)) rompath = os.environ.get("MAME_ROMPATH") return firmware(Path(rompath) if rompath else None, args.dir) if __name__ == "__main__": sys.exit(main())