281. Nametable with palette ram

Render nametable background using attribute table and PPU palette RAM bytes.

Lesson 281 of 356 · tests/chapter_05_rendering_pipeline/test_281_nametable_with_palette_ram.py

File to update

emulator/rendering/nametable_renderer.py

Why this step exists

The previous steps built the pieces separately

attribute table
    -> palette ID for each tile coordinate

palette RAM bytes
    -> four RGB background palettes

nametable + attributes + background palettes
    -> framebuffer

This step composes those pieces into one pure rendering helper

nametable_with_palette_ram_to_framebuffer(
    nametable_bytes,
    attribute_table,
    pattern_table_bytes,
    palette_ram,
)

What it should do

background_palettes = build_background_palettes_from_palette_ram(palette_ram)

return nametable_with_attributes_to_framebuffer(
    nametable_bytes,
    attribute_table,
    pattern_table_bytes,
    background_palettes,
)

Why this is useful

This helper accepts data shaped closer to real PPU rendering inputs while staying fully testable:

nametable visible tile bytes
attribute table bytes
pattern table CHR bytes
background palette RAM bytes

Still pure data

No PPU bus reads, no pygame, no window, no frame loop.

Important hardware model

CHR pixel color index 0-3
    -> attribute table selects background palette ID 0-3
    -> palette RAM selects NES color index $00-$3F
    -> NES RGB palette converts to RGB
    -> framebuffer pixel

Suggested implementation example

from emulator.rendering.palette_ram import build_background_palettes_from_palette_ram


def nametable_with_palette_ram_to_framebuffer(
    nametable_bytes: bytes,
    attribute_table: bytes,
    pattern_table_bytes: bytes,
    palette_ram: bytes,
) -> Framebuffer:
    background_palettes = build_background_palettes_from_palette_ram(palette_ram)

    return nametable_with_attributes_to_framebuffer(
        nametable_bytes,
        attribute_table,
        pattern_table_bytes,
        background_palettes,
    )

Out of scope

  • reading nametable/palette bytes from PPU bus
  • palette RAM mirroring
  • scrolling
  • sprites
  • OAMDMA
  • pygame display

Run this lesson

uv run pytest tests/chapter_05_rendering_pipeline/test_281_nametable_with_palette_ram.py -v