# SPDX-License-Identifier: MIT """The 32-pin connector protocol (docs/cartridge.md "The connector"). The board calls :meth:`Cartridge.access` once for **every** bus cycle: the CPU's cycles (dummy ones included) with ``halt`` false and each of MARIA's DMA reads with ``halt`` true, with the full 16-bit address. The return value is the byte the cartridge drives, or ``None`` when it drives nothing (the data bus keeps its last value). ``t`` is the MARIA clock of the access. Separately, :meth:`tick` is called once per Φ2 period, after that period's access (DOC: [1702B] p. 29, C22), DMA periods included (DOC: two reports, no trace; U10: a PCLK0 capture across DMA would settle it; docs/timing.md "Φ2 periods at the cartridge port"); cartridge sound chips (POKEY) count on it. ``irq`` is the level of the cartridge's IRQ pin, read by the board after every period (the board's sampling granularity: not a hardware value). The console's own devices win where they respond (DOC: [MT84] Figure 2); the cartridge sees every cycle anyway (bank-select hot spots decode writes; banksets decode HALT; DOC: the connector carries the whole bus, [SG] "Cartridge"). """ from __future__ import annotations class Cartridge: name = "empty slot" irq = False def access(self, address: int, data: int, read: bool, halt: bool, t: int) -> int | None: return None def tick(self) -> None: """One Φ2 period has passed (after its access).""" def audio_level(self) -> float | None: """The cartridge's audio pin, 0.0-1.0, or None if nothing drives it.""" return None def reset(self) -> None: """The console's reset button (RESET reaches the cartridge port).""" def snapshot(self) -> dict: return {} def restore(self, state: dict) -> None: pass class Linear(Cartridge): """No switching: the ROM ends at $FFFF (up to 48 KiB from $4000; 52 KiB from $3000 exists in homebrew). Nothing below the ROM is driven (DOC: Stolberg's guide, the wiki).""" name = "linear" def __init__(self, rom: bytes) -> None: if not rom or len(rom) > 0xD000: raise ValueError(f"a linear cartridge is 1 to 52 KiB, not {len(rom)} bytes") self.rom = bytes(rom) self.base = 0x10000 - len(rom) def access(self, address: int, data: int, read: bool, halt: bool, t: int) -> int | None: if read and address >= self.base: return self.rom[address - self.base] return None