229. Cpu bus ppu register write routing

Route CpuBus writes from $2000-$3FFF to PPU registers.

Lesson 229 of 356 · tests/chapter_03_ppu_memory_and_graphics_data/test_229_cpu_bus_ppu_register_write_routing.py

File to update

emulator/bus/cpu_bus.py

Why this step exists

CPU instructions such as STA absolute write to the CPU bus. When the target address is inside $2000-$3FFF, the write should go to the PPU register window.

The same 8-byte mirroring rule used for reads applies to writes

unmirrored_addr = 0x2000 + ((addr - 0x2000) % 8)

Why this formula is necessary

The PPU only has 8 CPU-visible base registers at $2000-$2007, but the CPU address map repeats those registers until $3FFF.

Examples

$2000 -> $2000 -> ctrl
$2008 -> $2000 -> ctrl
$2009 -> $2001 -> mask
$3FFF -> $2007 -> data

Suggested implementation pseudocode

if 0x2000 <= addr <= 0x3FFF:
    unmirrored_addr = 0x2000 + ((addr - 0x2000) % 8)
    self.ppu.write_register(unmirrored_addr, value)
    return

Boundary rule

CpuBus should not directly set ppu.ctrl, ppu.mask, etc. It should route to PPU.write_register so register semantics remain inside the PPU.

Run this lesson

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