Day 23 - Attributes

Published: 2005-07-22
Updated: 2010-12-19
Author: Mike Huber

How the palette works...

As you know, the palette is a table of 32 colors that exists at PPU address $3F00. Also, $3F00 and $3F10 will always be the same, so that brings it down to 31 colors to be more precise.

However, I have not shown how to get more than just the first 4 colors on the screen. The tiles themselves will yield a 2 bit number (NES tiles are 2bpp for those of you who have used the old DOS Tile Layer Pro...). However, to be able to use any of the 16 colors we have available (remember 16 for sprites, 16 for BGs), we have to use attributes.

Attributes are the upper 2 bits of the palette index (the tiles themselves are the lower 2 bits). With the Attributes and the tiles themselves, we get a 16 bit number. You can think of how it works in two ways:

  1. Just an index into a 16 color palette.
  2. The palette being split into 4, with each fourth having 4 colors in it. The Attribute would select which set of 4, and the actual tile using its 2bit number to create its gfx from the 4 colors (as usual).

Where to put BG Attributes

BG Attributes go in the $40 bytes after each Name Table in PPU memory: For example (from Marat's doc):

--------------------------------------- $2800
 Attribute Table 1
--------------------------------------- $27C0
 Name Table 1 (32x30 tiles)
--------------------------------------- $2400
 Attribute Table 0
--------------------------------------- $23C0
 Name Table 0 (32x30 tiles)
--------------------------------------- $2000

Where to put Sprite Attributes

Sprite Attributes go in the special byte of a sprite's OAM entry. To be specific: Bits 0 and 1 of byte 2 (remember, always count from 0). Here's the OAM format from Marat's doc:

Sprite Attribute RAM:
| Sprite#0 | Sprite#1 | ... | Sprite#62 | Sprite#63 |
     |          |
     +---- 4 bytes: 0: Y position of the left-top corner - 1
                    1: Sprite pattern number
                    2: Color and attributes:
                       bits 1,0: two upper bits of color
                       bits 2,3,4: Unknown (???)
                       bit 5: if 1, display sprite behind background
                       bit 6: if 1, flip sprite horizontally
                       bit 7: if 1, flip sprite vertically
                    3: X position of the left-top corner

(NOTE: Always check Marat's doc when in doubt (or not :) ), when programming, leave it open.)

This Day In Review

I finally did attributes, yay! Perhaps it would be useful to write a new BMP2NES utility...

Have fun!,
-GbaGuy