# SPDX-License-Identifier: MIT """CartDescription: what the board must build for one cartridge, with where each fact came from. The vocabulary is the A78 v4 header's (docs/cartridge.md "What comes out"; the 7800 Development Wiki's "A78 header specification"), because that is what flash carts and the homebrew toolchain write. ``provenance`` names the source of every field that is not a default; ``certainty`` is the weakest source among the fields the board needs to build the hardware (scheme, $4000, sound chips). The vocabulary, sources and defaults are a data model, not hardware values; each field's evidence is its ``provenance`` (DOC for header and table sources). """ from __future__ import annotations import hashlib import zlib from dataclasses import dataclass, field, replace SCHEMES = ("linear", "supergame", "activision", "absolute", "souper") AT_4000 = ( "none", "ram16", "ram8-mirror", "ram32-halt", "ram32-latch", "rom", "fixed-bank", "mirror-ram-2k", "pokey", ) # fmt: skip # Sources, strongest first (docs/cartridge.md "When an .a78 header contradicts the table"). SOURCES = ( "override", "softlist", "confirmed", "a78 header v4", "a78 header v3", "a78 header", "size", "default", ) # fmt: skip @dataclass(frozen=True) class CartDescription: payload: bytes = field(repr=False) console: str = "7800" # or "2600" scheme: str = "linear" bankset: bool = False at_4000: str = "none" pokey: tuple[int, ...] = () # base addresses: 0x440, 0x450, 0x800, 0x4000 pokey_write_only: bool = False ym2151: bool = False covox: bool = False adpcm: bool = False bupchip: bool = False irq_sources: tuple[str, ...] = () hsc: bool = False xm: bool = False save: str = "none" # or "savekey" controllers: tuple[str, str] = ("proline", "proline") region: str = "ntsc" # ntsc | pal | both | unknown composite_artifacts: bool = False title: str = "" provenance: dict[str, str] = field(default_factory=dict, compare=False) certainty: str = "default" notes: tuple[str, ...] = () # matched after trimming, header/table conflicts, labels a2600_scheme: str | None = None # for console == "2600": a2600-python's family @property def sha256(self) -> str: return hashlib.sha256(self.payload).hexdigest() @property def sha1(self) -> str: return hashlib.sha1(self.payload).hexdigest() @property def crc32(self) -> int: return zlib.crc32(self.payload) @property def size(self) -> int: return len(self.payload) def with_fields(self, source: str, **fields) -> CartDescription: """A copy with ``fields`` set and attributed to ``source``.""" provenance = dict(self.provenance) for name in fields: provenance[name] = source return replace(self, provenance=provenance, **fields) def summary(self) -> str: parts = [self.console, self.scheme] if self.bankset: parts.append("bankset") if self.at_4000 != "none": parts.append(f"$4000={self.at_4000}") parts += [f"pokey@{a:X}" for a in self.pokey] for flag in ("ym2151", "covox", "hsc", "xm", "bupchip"): if getattr(self, flag): parts.append(flag) if self.controllers != ("proline", "proline"): parts.append("/".join(self.controllers)) return " ".join(parts) + f" [{self.region}; {self.certainty}]" class Refused(Exception): """The documented refusal: the loader cannot say what hardware this payload needs.""" def __init__(self, message: str, sha256: str = "") -> None: super().__init__(message) self.sha256 = sha256