236. Mapper interface protocol

Create MapperInterface as a protocol for bus/mapper boundaries.

Lesson 236 of 356 · tests/chapter_03_ppu_memory_and_graphics_data/test_236_mapper_interface_protocol.py

File to create

emulator/cartridge/mapper_interface.py

Protocol to implement

MapperInterface

Why this helper exists

PpuBus should not depend on Mapper000 directly. Mapper000 is only one cartridge mapper. Later mappers may bank-switch PRG/CHR differently, but PpuBus should only need a small interface:

read_prg(addr)
read_chr(addr)

This keeps PpuBus mapper-aware without making it Mapper000-specific.

What is a Protocol? A Protocol describes the methods an object must provide. A class does not need to inherit from the Protocol explicitly. If it has the right methods, it matches the protocol structurally.

Suggested implementation pseudocode

from typing import Protocol


class MapperInterface(Protocol):
    def read_prg(self, addr: int) -> int:
        ...

    def read_chr(self, addr: int) -> int:
        ...

Future note

When CHR RAM writes are implemented, this protocol may grow

write_chr(addr, value)

Do not require it yet, because Mapper000 does not implement CHR writes at this stage.

Run this lesson

uv run pytest tests/chapter_03_ppu_memory_and_graphics_data/test_236_mapper_interface_protocol.py -v