257. Decode pattern table
Implement full pattern table decoding.
Lesson 257 of 356 · tests/chapter_03_ppu_memory_and_graphics_data/test_257_decode_pattern_table.py
Reference
https://www.nesdev.org/wiki/PPU_pattern_tablesFile to update
emulator/ppu/chr_decoder.pyFunction to implement
decode_pattern_table(pattern_table_bytes: bytes) -> PatternTableConstants to add
PATTERN_TABLE_SIZE = 0x1000
CHR_TILE_SIZE = 16
PATTERN_TABLE_TILE_COUNT = 256What is a pattern table? A pattern table is a block of CHR graphics data containing 256 tiles.
Basic math
one CHR tile = 16 bytes
one pattern table = 4096 bytes = $1000
4096 / 16 = 256 tilesThe NES PPU has two pattern table address ranges
$0000-$0FFF -> pattern table 0
$1000-$1FFF -> pattern table 1This step only decodes one 4096-byte pattern table into 256 already-decoded tiles. It does not draw an image yet.
Suggested implementation example
PATTERN_TABLE_SIZE = 0x1000
CHR_TILE_SIZE = 16
PATTERN_TABLE_TILE_COUNT = 256
PatternTile = list[list[int]]
PatternTable = list[PatternTile]
def decode_pattern_table(pattern_table_bytes: bytes) -> PatternTable:
if len(pattern_table_bytes) != PATTERN_TABLE_SIZE:
raise ValueError("Pattern table must be 4096 bytes")
tiles = []
for tile_index in range(PATTERN_TABLE_TILE_COUNT):
start = tile_index * CHR_TILE_SIZE
end = start + CHR_TILE_SIZE
tiles.append(decode_chr_tile(pattern_table_bytes[start:end]))
return tilesOut of scope
- arranging tiles into a 128x128 debug grid
- PNG/image output
- RGB/NES palette colors
- nametable background rendering
- PPU timing
Run this lesson
uv run pytest tests/chapter_03_ppu_memory_and_graphics_data/test_257_decode_pattern_table.py -v