006. Fakerom

Create writable FakeROM storage for CPU tests.

Lesson 6 of 356 · tests/chapter_01_cpu/test_006_FakeROM.py

File to create

emulator/memory/fake_rom.py

Location

class FakeROM(MemoryDevice)

Why this step exists

Instruction tests need deterministic program bytes before real cartridge parsing is available. FakeROM models the maximum $8000-$FFFF PRG window as 0x8000 local bytes and allows tests to populate it directly.

Complete example implementation

from dataclasses import dataclass, field

from emulator.memory.memory_device import MemoryDevice


@dataclass
class FakeROM(MemoryDevice):
    _data: bytearray = field(
        default_factory=lambda: bytearray(0x8000),
        init=False,
    )

    def read(self, addr: int) -> int:
        return self._data[addr]

    def write(self, addr: int, value: int) -> None:
        self._data[addr] = value

Important invariants

  • FakeROM stores exactly 0x8000 bytes
  • its addresses are local offsets $0000-$7FFF
  • writes exist only to arrange test programs and data

Common misconception

FakeROM is not a real cartridge mapper and its write method does not claim that NES PRG ROM is writable. It is controlled test infrastructure.

Out of scope

  • mapping FakeROM into CPU addresses
  • iNES parsing
  • bank switching

Run this lesson

uv run pytest tests/chapter_01_cpu/test_006_FakeROM.py -v