# SPDX-License-Identifier: MIT # Bounty Bob and Super Cart bank rules: facts restated (no code) from MAME's # bus/a800/bbsb.cpp and bus/a800/a5200_supercart.cpp, BSD-3-Clause, copyright # Fabio Priuli and Angelo Salese; their notice is in NOTICE. """The cartridge port: what each cartridge class puts on the data bus. The 5200's cartridge connector carries D0-D7, A0-A13 and two decoded enables, "Enable 40-7F" and "Enable 80-BF"; there is no A14, A15 or R/W (docs/cartridge.md "The connector"). Every class below is which ROM chips hang on which enable. The protocol (docs/cartridge.md "The cartridge port protocol"): - ``read(address) -> int | None`` for a cycle with the address in $4000-$BFFF; None means nothing drives the bus (the board keeps its floating-bus latch). A bank-switching class switches on this access too. - ``access(address)`` for a write cycle in $4000-$BFFF: the connector has no R/W, so a hotspot switches on a write as well; ROM ignores the data. ANTIC's DMA reads come through ``read`` as well: the cartridge cannot tell who drives the address. This module imports nothing from the board (tests/test_boundaries.py). Evidence tags (methodology): the connector's lines are DOC ([FAQ] 4.6 pinout, [AHRM] 13.2); the classes' chip arrangements are DOC (docs/cartridge.md "Classes"; MAME's CC0 software list for which titles use which) and every class boots the commercial dumps that use it (milestone 5, MEASURED on dumps read here, judged by the board's own run). Unknowns are tagged where they are. """ from __future__ import annotations class Cartridge: """A plain ROM class: a fixed 32K window, None where no chip is enabled.""" mapping = "none" def __init__(self, payload: bytes) -> None: self.payload = bytes(payload) # $4000-$BFFF as 32 768 entries, -1 where nothing drives the bus. self.window = self._build_window() def _build_window(self) -> list[int]: return [-1] * 0x8000 def read(self, address: int) -> int | None: value = self.window[address - 0x4000] return None if value < 0 else value def access(self, address: int) -> None: return None # -- state ------------------------------------------------------------ def snapshot(self) -> dict: return {} def restore(self, state: dict) -> None: return None class Cart32K(Cartridge): """Two 16K ROMs: the first on Enable 40-7F, the second on Enable 80-BF.""" mapping = "32k" def _build_window(self) -> list[int]: return list(self.payload) class Cart16KTwoChip(Cartridge): """Two 8K ROMs with A13 unconnected: each is seen twice in its 16K half.""" mapping = "16k-two" def _build_window(self) -> list[int]: low, high = self.payload[:0x2000], self.payload[0x2000:] return list(low + low + high + high) class Cart16KOneChip(Cartridge): """One 16K ROM on Enable 80-BF, and on Enable 40-7F as well. C4: Atari800 leaves $4000-$7FFF empty, MAME's hash file says the 16K is seen at $4000 too. The board follows MAME (mirrored) unless a cartridge is described with ``low_mirror=False``. HYPOTHESIS (C4, undecidable here): a one-chip cartridge's board traces, or a probe reading $4000 with a one-chip cartridge on a console, would settle it. """ mapping = "16k-one" def __init__(self, payload: bytes, low_mirror: bool = True) -> None: self.low_mirror = low_mirror super().__init__(payload) def _build_window(self) -> list[int]: low = list(self.payload) if self.low_mirror else [-1] * 0x4000 return low + list(self.payload) class Cart8K(Cartridge): """One 8K ROM on Enable 80-BF: $8000-$BFFF shows it twice; $4000-$7FFF is undriven.""" mapping = "8k" def _build_window(self) -> list[int]: return [-1] * 0x4000 + list(self.payload) * 2 class Cart4K(Cartridge): """One 4K ROM on Enable 80-BF: seen four times at $8000-$BFFF.""" mapping = "4k" def _build_window(self) -> list[int]: return [-1] * 0x4000 + list(self.payload) * 4 class BountyBob(Cartridge): """Bounty Bob Strikes Back! (40K): two switched 4K windows and a fixed 8K. Payload in canonical (Atari800 type 7) order: four 4K banks for $4000-$4FFF, four for $5000-$5FFF, then the fixed 8K at $8000-$9FFF and again at $A000-$BFFF (DOC: [A800] DOC/cart.txt type 7; [MAME] bus/a800/bbsb.cpp, BSD-3-Clause, Fabio Priuli and Angelo Salese, notice in NOTICE: facts restated, no code). An access (read or write) to $4FF6-$4FF9 selects the first window's bank 0-3, to $5FF6-$5FF9 the second's. What $6000-$7FFF shows is not stated in the sources (U9b): undriven here. Power-on banks are not stated either (U9b): bank 0 in both windows, a labelled default. Both HYPOTHESIS: the cartridge's PCB (a 74-series latch pair) would settle them. """ mapping = "bbsb" def __init__(self, payload: bytes) -> None: self.bank = [0, 0] super().__init__(payload) def _build_window(self) -> list[int]: window = [-1] * 0x8000 fixed = list(self.payload[0x8000:0xA000]) window[0x4000:0x6000] = fixed window[0x6000:0x8000] = fixed return window def read(self, address: int) -> int | None: if address < 0x6000: self.access(address) if address < 0x5000: return self.payload[self.bank[0] * 0x1000 + (address & 0xFFF)] return self.payload[0x4000 + self.bank[1] * 0x1000 + (address & 0xFFF)] value = self.window[address - 0x4000] return None if value < 0 else value def access(self, address: int) -> None: low = address & 0xFFF if 0xFF6 <= low <= 0xFF9: if 0x4000 <= address < 0x5000: self.bank[0] = low - 0xFF6 elif 0x5000 <= address < 0x6000: self.bank[1] = low - 0xFF6 def snapshot(self) -> dict: return {"bank": list(self.bank)} def restore(self, state: dict) -> None: self.bank = list(state["bank"]) class SuperCart(Cartridge): """The homebrew "Super Cart" (64K-512K): 32K banks over the whole window. DOC: [A800] DOC/cart.txt types 71-74 (citing the designer's post); [MAME] bus/a800/a5200_supercart.cpp, BSD-3-Clause, Angelo Salese, notice in NOTICE: facts restated, no code. A read of $BFD0-$BFDF sets bank bits 0-1 from address bits 2-3; a read of $BFC0-$BFCF sets bits 2-3 from address bits 2-3; a read of $BFE0-$BFFF selects the last bank, which the BIOS's own trailer reads do at power-on. The board applies the same rule to write cycles (the connector has no R/W: DOC [FAQ] 4.6). Power-on: the last bank (DOC, as the bank rule implies through the BIOS's trailer reads). """ mapping = "supercart" def __init__(self, payload: bytes) -> None: self.banks = len(payload) // 0x8000 self.bank = self.banks - 1 super().__init__(payload) def read(self, address: int) -> int | None: self.access(address) return self.payload[self.bank * 0x8000 + address - 0x4000] def access(self, address: int) -> None: if address >= 0xBFC0: mask = self.banks - 1 if address >= 0xBFE0: self.bank = mask elif address >= 0xBFD0: self.bank = ((self.bank & 0x0C) | ((address >> 2) & 3)) & mask else: self.bank = ((self.bank & 0x03) | (address & 0x0C)) & mask def snapshot(self) -> dict: return {"bank": self.bank} def restore(self, state: dict) -> None: self.bank = state["bank"] CLASSES: dict[str, type[Cartridge]] = { "4k": Cart4K, "8k": Cart8K, "16k-one": Cart16KOneChip, "16k-two": Cart16KTwoChip, "32k": Cart32K, "bbsb": BountyBob, "supercart": SuperCart, } def make_port(mapping: str, payload: bytes) -> Cartridge: """The port object for ``mapping`` (one of :data:`CLASSES`) over ``payload``.""" return CLASSES[mapping](payload)