243. Ppuctrl controls ppudata increment

PPUCTRL controls how much PPUDATA increments vram_addr.

Lesson 243 of 356 · tests/chapter_03_ppu_memory_and_graphics_data/test_243_ppuctrl_controls_ppudata_increment.py

Reference

https://www.nesdev.org/wiki/PPU_registers#PPUCTRL

File to update

emulator/ppu/ppu.py

Constant to add

CTRL_VRAM_INCREMENT_BY_32 = 1 << 2

Why this step exists

PPUDATA ($2007) accesses PPU memory at the current internal VRAM address. After each access, the PPU increments that address.

The increment amount is controlled by PPUCTRL ($2000) bit 2:

bit 2 clear -> increment by 1
bit 2 set   -> increment by 32

Why this matters

Increment-by-1 is useful for writing across a row of consecutive PPU memory. Increment-by-32 is useful for writing down a column in nametable memory.

Suggested implementation pseudocode

CTRL_VRAM_INCREMENT_BY_32 = 1 << 2

case 0x2007:
    self.data = value
    self.ppu_bus.write(self.vram_addr, value)

    increment = 32 if self.ctrl & CTRL_VRAM_INCREMENT_BY_32 else 1
    self.vram_addr = (self.vram_addr + increment) & 0x3FFF

Important

Keep masking vram_addr with 0x3FFF because the PPU address space is 14-bit:

$0000-$3FFF

Out of scope

  • other PPUCTRL bits
  • NMI enable
  • pattern table selection
  • base nametable selection
  • rendering

Run this lesson

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