299. Sprite entry definition

Define the basic OAM sprite entry data model.

Lesson 299 of 356 · tests/chapter_09_sprite_rendering/test_299_sprite_entry_definition.py

File to create

emulator/rendering/sprite_renderer.py

Why this step exists

OAMDMA already copies 256 bytes into PPU.oam. Before rendering sprites, we need a small data model for one sprite entry.

What is OAM? OAM means Object Attribute Memory. It is the PPU's internal sprite memory:

64 sprites * 4 bytes = 256 bytes

Each sprite entry uses four bytes

byte 0: Y position
byte 1: tile index
byte 2: attributes
byte 3: X position

This first sprite-rendering step only creates constants and the SpriteEntry dataclass. It does not decode from OAM yet and does not render pixels.

Suggested implementation example

from dataclasses import dataclass


OAM_SPRITE_COUNT = 64
BYTES_PER_SPRITE = 4
OAM_SIZE = OAM_SPRITE_COUNT * BYTES_PER_SPRITE


@dataclass(frozen=True)
class SpriteEntry:
    y: int
    tile_index: int
    attributes: int
    x: int

Common misconception

"OAMDMA means sprites are visible."

No. OAMDMA only fills PPU.oam. Rendering those entries into pixels is a later chapter 09 step.

Out of scope

  • decode_sprite_entry()
  • sprite attribute bit decoding
  • sprite palettes
  • rendering pixels
  • sprite 0 hit
  • sprite overflow
  • pygame

Run this lesson

uv run pytest tests/chapter_09_sprite_rendering/test_299_sprite_entry_definition.py -v