"""The expansion port's sockets (docs/expansion.md) and test-only fakes. An add-on board pins this board and plugs into these. With every socket empty the base behaves exactly as it does alone: the board picks its fast paths at construction and an empty socket costs nothing. The fakes record what they see so tests can show each socket is wired. """ from __future__ import annotations from typing import Protocol class MemoryOverride(Protocol): """/MEMMAPOVR: claim an access in $0000-$7FFF before the console's decode. ``read`` returns a byte to claim the read, or None to leave it to the console; ``write`` returns True if it claimed the write. """ def read(self, address: int) -> int | None: ... def write(self, address: int, value: int) -> bool: ... class IOPorts(Protocol): """Ports $00-$7F (A7 low: the console decodes nothing there), and /IOMAPOVR for the rest: ``read`` returns a byte or None, ``write`` True if claimed.""" def read(self, port: int) -> int | None: ... def write(self, port: int, value: int) -> bool: ... class AudioSource(Protocol): """Expansion audio summed with the SN76489A: ``run`` to a CPU clock, then ``take_samples`` gives mono floats at the board's audio rate.""" def run(self, cpu_clock: int) -> None: ... def take_samples(self) -> list[float]: ... class InterruptLines(Protocol): """The expansion port's /INT and /NMI, polled after each step: levels.""" @property def int_line(self) -> bool: ... @property def nmi_line(self) -> bool: ... class WaitLine(Protocol): """/WAIT from the expansion port: extra CPU clocks for the step just made.""" def wait_clocks(self, cpu_clock: int) -> int: ... class VideoSwitch(Protocol): """An external picture instead of the VDP's (Expansion Module 1's path).""" def select(self, frame: object) -> object: ... class Sockets: """The board's sockets; None is empty.""" def __init__( self, memory: MemoryOverride | None = None, io: IOPorts | None = None, audio: AudioSource | None = None, interrupts: InterruptLines | None = None, wait: WaitLine | None = None, video: VideoSwitch | None = None, ) -> None: self.memory = memory self.io = io self.audio = audio self.interrupts = interrupts self.wait = wait self.video = video @property def empty(self) -> bool: return all( s is None for s in (self.memory, self.io, self.audio, self.interrupts, self.wait, self.video) ) # ---------------------------------------------------------------------- fakes (tests only) class FakeMemory: """Claims ``base .. base + len(ram)`` and records every access it sees.""" def __init__(self, base: int, size: int) -> None: self.base, self.ram, self.log = base, bytearray(size), [] def read(self, address: int) -> int | None: self.log.append(("r", address)) offset = address - self.base return self.ram[offset] if 0 <= offset < len(self.ram) else None def write(self, address: int, value: int) -> bool: self.log.append(("w", address, value)) offset = address - self.base if 0 <= offset < len(self.ram): self.ram[offset] = value return True return False class FakeIO: """Answers ``ports`` with a fixed byte and records writes.""" def __init__(self, ports: dict[int, int]) -> None: self.ports, self.log = dict(ports), [] def read(self, port: int) -> int | None: return self.ports.get(port) def write(self, port: int, value: int) -> bool: self.log.append((port, value)) return port in self.ports class FakeAudio: """A constant level, one sample per call to ``take_samples`` per run.""" def __init__(self, level: float) -> None: self.level, self.clock, self._pending = level, 0, 0 def run(self, cpu_clock: int) -> None: self._pending += 1 self.clock = cpu_clock def take_samples(self) -> list[float]: n, self._pending = self._pending, 0 return [self.level] * n class FakeInterrupts: def __init__(self) -> None: self.int_line = False self.nmi_line = False class FakeWait: """Adds ``clocks`` to every step while ``active``.""" def __init__(self, clocks: int) -> None: self.clocks, self.active = clocks, True def wait_clocks(self, cpu_clock: int) -> int: return self.clocks if self.active else 0 class FakeVideo: def __init__(self, replacement: object) -> None: self.replacement = replacement def select(self, frame: object) -> object: return self.replacement