A palette table stores the NES color choices used by graphics. It has separate 16-entry sets for background tiles and sprites: four palettes of four color values each. A default table can live in ROM as fixed byte data, while a 32-byte working table in RAM holds the values copied to the PPU. section 5.8 section 6.4
For background tiles, pattern data supplies a 2-bit color number from 0 to 3, and an attribute value selects one of four palettes. Each 2 × 2 tile area shares that palette selection. Color 0 is the shared background color: only the first background palette's color-0 value is used, leaving 13 usable background colors on screen at once. section 6.5.2
Listing 5.8 RODATA code segment (example.s)
;***************************************************************** ; Our default palette table has 16 entries for tiles ; and 16 entries for sprites ;***************************************************************** .segment "RODATA" default_palette: .byte $0F,$15,$26,$37 #1 .byte $0F,$09,$19,$29 #2 .byte $0F,$01,$11,$21 #3 .byte $0F,$00,$10,$30 #4 .byte $0F,$18,$28,$38 #5 .byte $0F,$14,$24,$34 #6 .byte $0F,$1B,$2B,$3B #7 .byte $0F,$12,$22,$32 #8 welcome_txt: .byte 'W','E','L','C', 'O', 'M', 'E', 0
This 32-byte default table shows eight rows of four color values: four background palettes followed by four sprite palettes. section 5.8
Use a palette table as part of startup and rendering:
- Reserve 32 bytes in RAM for the current palette, and define fixed starting values in RODATA with .byte directives. section 5.8 section 6.4
- Copy all 32 default values into the RAM palette before enabling the game display. section 6.4
- During each NMI, write the 32 RAM entries to PPU palette memory beginning at $3F00. section 6.4
- Change RAM entries during the game; the next palette transfer updates every screen element using those entries. section 13.2.2
Flash shared colors without redrawing tiles
Swap two entries in the first tile palette to make all tiles using those colors alternate. For a screen flash, toggle the shared color in both the background and sprite portions of the RAM table; because the whole table is sent every NMI, updating only one copy can be overwritten by the later sprite value. section 13.2.2 section 13.3.1
Palette edits are a low-cost animation technique because one changed entry affects every tile or sprite that refers to it. Design graphics around colors that can be swapped or progressed through, then verify the effect by compiling and running in an emulator. section 13.2.2 section 13.5
