247. Ppumask bit constants

Define PPUMASK ($2001) bit constants.

Lesson 247 of 356 · tests/chapter_03_ppu_memory_and_graphics_data/test_247_ppumask_bit_constants.py

Reference

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

File to update

emulator/ppu/ppu.py

Constants to add

MASK_GRAYSCALE
MASK_SHOW_BACKGROUND_LEFT_8
MASK_SHOW_SPRITES_LEFT_8
MASK_SHOW_BACKGROUND
MASK_SHOW_SPRITES
MASK_EMPHASIZE_RED
MASK_EMPHASIZE_GREEN
MASK_EMPHASIZE_BLUE

Why this step exists

PPUMASK is the CPU-writable rendering mask register at $2001. It controls which parts of rendering are visible and how colors are emphasized.

The bit layout is commonly written as

BGRs bMmG

Meaning

B = emphasize blue
G = emphasize green
R = emphasize red
s = show sprites
b = show background
M = show sprites in leftmost 8 pixels
m = show background in leftmost 8 pixels
G = grayscale

Important scope

This test only requires named constants. Rendering behavior will use these names later. Do not implement actual grayscale, color emphasis, background rendering, or sprite rendering in this step.

Suggested implementation pseudocode

# PPUMASK ($2001) bits: BGRs bMmG
MASK_GRAYSCALE = 1 << 0
MASK_SHOW_BACKGROUND_LEFT_8 = 1 << 1
MASK_SHOW_SPRITES_LEFT_8 = 1 << 2
MASK_SHOW_BACKGROUND = 1 << 3
MASK_SHOW_SPRITES = 1 << 4
MASK_EMPHASIZE_RED = 1 << 5
MASK_EMPHASIZE_GREEN = 1 << 6
MASK_EMPHASIZE_BLUE = 1 << 7

Common value

$1E == 0b0001_1110

This enables

  • background left 8 pixels
  • sprites left 8 pixels
  • background rendering
  • sprite rendering

Out of scope

  • actual pixel rendering
  • grayscale palette behavior
  • color emphasis behavior
  • left-edge clipping behavior

Run this lesson

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