237. Ppu bus chr area routing

Route PpuBus CHR-area reads through the mapper.

Lesson 237 of 356 · tests/chapter_03_ppu_memory_and_graphics_data/test_237_ppu_bus_chr_area_routing.py

File to update

emulator/bus/ppu_bus.py

Constants to add/use

CHR_START = 0x0000
CHR_END = 0x1FFF

What is the CHR area? The PPU address range $0000-$1FFF is the pattern table area. It contains tile graphics bytes used later for background and sprite pixels.

Important NES fact

This area often belongs to the cartridge, not internal VRAM.

CHR ROM cartridge:
    PPU reads pattern bytes from cartridge CHR ROM.

CHR RAM cartridge:
    PPU can write pattern bytes into cartridge CHR RAM.

Current scope

For now, PpuBus must detect CHR-area addresses. Reads should use mapper.read_chr(addr) when a mapper exists.

We intentionally do not lock down CHR write behavior in this test. A future step may add mapper.write_chr(addr, value) for CHR RAM support, and this test should not block that evolution.

Suggested read pseudocode

def read(self, addr: int) -> int:
    addr = addr & PPU_ADDRESS_MASK

    if CHR_START <= addr <= CHR_END:
        if self.mapper is not None:
            return self.mapper.read_chr(addr)
        return self.vram.read(addr)

    return self.vram.read(addr)

Possible current write pseudocode

def write(self, addr: int, value: int) -> None:
    addr = addr & PPU_ADDRESS_MASK

    if CHR_START <= addr <= CHR_END:
        if self.mapper is not None:
            raise ValueError("CHR writes are not supported yet")
        self.vram.write(addr, value)
        return

    self.vram.write(addr, value)

Possible future write pseudocode

if CHR_START <= addr <= CHR_END and self.mapper is not None:
    self.mapper.write_chr(addr, value)
    return

Future regions

$2000-$3EFF:
    currently backed by big VRAM, later nametable VRAM/mirroring

$3F00-$3FFF:
    currently backed by big VRAM, later palette RAM/mirroring

Run this lesson

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