227. Cpu bus ppu default factory

Add a default PPU to CpuBus.

Lesson 227 of 356 · tests/chapter_03_ppu_memory_and_graphics_data/test_227_cpu_bus_ppu_default_factory.py

File to update

emulator/bus/cpu_bus.py

What to add

CpuBus should own a PPU instance.

Why this step exists

The NES CPU communicates with the PPU through memory-mapped registers. Before CpuBus can route reads/writes in $2000-$3FFF, the bus needs a PPU object to forward those accesses to.

Important design choice

Use default_factory instead of PPU() directly in the dataclass field.

Why

Each CpuBus should get its own PPU instance. Sharing one mutable PPU between bus instances would create hidden coupling between tests and emulator instances.

Suggested implementation pseudocode

from dataclasses import dataclass, field
from emulator.ppu.ppu import PPU


@dataclass
class CpuBus:
    ...
    ppu: PPU = field(default_factory=PPU)

Run this lesson

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