# SPDX-License-Identifier: MIT """Plain 2K/4K, and the 4K-bank schemes: Atari's F8/F6/F4 (optional Super Chip) and CBS's FA. Sources: Kevin Horton's mapper document (``reference/docs/kevtris-atari-2600-mappers.txt``, 2012, DOC) and his older ``qotile-sizes.txt`` (1997, DOC); docs/cartridge.md. - **F8, F6, F4** (DOC Horton): every access with A12 high to a hotspot selects a 4K bank, read or write, dummy or not ("ANY kind of access will trigger the switching"): F8 $1FF8-$1FF9 (2 banks), F6 $1FF6-$1FF9 (4), F4 $1FF4-$1FFB (8), the lowest hotspot selecting bank 0. - **FA** (CBS RAM Plus, DOC Horton): three 4K banks at $1FF8-$1FFA, and 256 bytes of RAM, written at $1000-$10FF and read at $1100-$11FF. - **The Super Chip** (SC, DOC Horton): 128 bytes of RAM written at $1000-$107F, read at $1080-$10FF, the ROM disabled for those 256 bytes. Without an R/W line any access to a write window writes the byte on the bus (connector.py). **The byte a hotspot read returns** is the new bank's. HYPOTHESIS: the bank latch changes during the access and the ROM's output settles from the new bank before the data is sampled at the end of the cycle; a logic-analyser capture of a hotspot read would settle it. MAME switches in a read tap after the read and returns the old bank's byte (BSD-3 rom.cpp:125-126): docs/undocumented-behavior.md C20. Programs normally read hotspots only as dummy accesses or as the target of a bank-switch instruction, where the value is not used. **The power-on bank** is arbitrary on a console (HYPOTHESIS: no document states it; the programs switch banks from code present in every bank). The default is MAME 0.285's (BSD-3 rom.h:53, 70, 97: F6 and FA bank 0, F4 bank 7, F8 bank 1), which milestone 1's lockstep needs. """ from __future__ import annotations from .connector import Cartridge BANK = 0x1000 # DOC Horton: 4K banks, the whole A12-high window # scheme: (first hotspot, banks, MAME's power-on bank, RAM bytes). Hotspots, # banks and RAM: DOC Horton; the power-on bank: OBSERVED/DOC MAME 0.285 BSD-3 # rom.cpp, a HYPOTHESIS for consoles (see the module notes). SCHEMES = { "F8": (0x1FF8, 2, 1, 0), "F6": (0x1FF6, 4, 0, 0), "F4": (0x1FF4, 8, 7, 0), "FA": (0x1FF8, 3, 0, 256), } SUPER_CHIP = 128 # DOC Horton: 128 bytes, written at $1000-$107F, read at $1080-$10FF class Plain(Cartridge): """2K or 4K with no banking; a 2K image appears twice in the 4K window (DOC Horton).""" def __init__(self, image: bytes) -> None: if len(image) not in (0x800, 0x1000): raise ValueError(f"2K/4K cartridge must be 2048 or 4096 bytes, not {len(image)}") self.name = "2K" if len(image) == 0x800 else "4K" self.rom = bytes(image) * (0x1000 // len(image)) def access(self, address: int, value: int, t: int, write: bool = False) -> int | None: if address & 0x1000: return self.rom[address & 0xFFF] return None class Banked(Cartridge): """F8, F6, F4 (with ``super_chip`` RAM if asked) or FA (always with its 256 bytes).""" def __init__( self, image: bytes, scheme: str, *, super_chip: bool = False, start_bank: int | None = None, ram_fill: int = 0x00, ) -> None: first, banks, mame_start, ram_size = SCHEMES[scheme] if len(image) != banks * BANK: raise ValueError(f"{scheme} needs {banks * BANK} bytes, not {len(image)}") if super_chip and not ram_size: ram_size = SUPER_CHIP self.name = scheme + "SC" else: self.name = scheme self.rom = bytes(image) self.first_hotspot = first self.last_hotspot = first + banks - 1 self.bank = mame_start if start_bank is None else start_bank self.ram = bytearray([ram_fill]) * ram_size if ram_size else None self.ram_size = ram_size # the write window, then the read window, from $1000 def access(self, address: int, value: int, t: int, write: bool = False) -> int | None: if not address & 0x1000: return None if self.first_hotspot <= address <= self.last_hotspot: self.bank = address - self.first_hotspot ram = self.ram if ram is not None: offset = address & 0xFFF size = self.ram_size if offset < size: ram[offset] = value return None # the ROM is disabled and the RAM is being written if offset < 2 * size: return ram[offset - size] return self.rom[(self.bank << 12) | (address & 0xFFF)]