236. Mapper 接口协议
创建 MapperInterface,作为总线/mapper 边界的协议。
第 236 / 356 · tests/chapter_03_ppu_memory_and_graphics_data/test_236_mapper_interface_protocol.py
要创建的文件
emulator/cartridge/mapper_interface.py要实现的协议
MapperInterface为什么需要这个辅助类型
PpuBus 不应该直接依赖 Mapper000。Mapper000 只是众多卡带 mapper 中的一种。之后的 mapper 可能会以不同方式对 PRG/CHR 进行分块切换,但 PpuBus 应该只需要一个小接口:
read_prg(addr)
read_chr(addr)这样可以让 PpuBus 感知 mapper,而不需要针对 Mapper000 写死特定逻辑。
什么是 Protocol?Protocol 描述了一个对象必须提供的方法。类不需要显式继承自该 Protocol,只要它拥有正确的方法,就在结构上符合该协议。
建议实现的伪代码
from typing import Protocol
class MapperInterface(Protocol):
def read_prg(self, addr: int) -> int:
...
def read_chr(self, addr: int) -> int:
...未来说明
当实现 CHR RAM 写入时,该协议可能会扩展
write_chr(addr, value)目前不需要它,因为 Mapper000 在此阶段还没有实现 CHR 写入。
运行本课
uv run pytest tests/chapter_03_ppu_memory_and_graphics_data/test_236_mapper_interface_protocol.py -v