Published using Google Docs
PPU (RP2C02) Nintendo NES Documentation
Updated automatically every 5 minutes

Introduction

The ppu is the video engine for NES (Famicom) console also known as RP2C02 or RP2C07. The PPU is controlled via eight registers visible in the CPU's address space in the addresses $2000 through $2007. All data and information is passed to the PPU through these, except the raw tile data, which is hardwired to the PPU's address space. The PPU uses the tile graphics data together with information stored by the program in the PPU's RAM, such as color and position, to render the final graphical output to the screen. The lowest graphical components the PPU operates with are tiles, which are blocks of 8×8 or 8×16 pixels. The tiles are stored in a ROM or RAM (CHR) chip on the game cartridge. According to DWedit (a user from Nesdev forum) 24% of all NES games used CHR RAM instead of ROM.  The tiles are the basic building blocks, used to create larger moving objects, or large static backgrounds. Essentially, the PPU supports two different kinds of objects: movable (sprites) and non-movable (background). Both kind of objects are basically a tile, and moreover a sprite and background object can use the same tile. The difference is that a tile used as a sprite can move around, whereas a tile used as a background cannot. Sprite data is stored in a special memory called the "Sprite-RAM" or "SPR-RAM" for short, which is a 256-byte memory built into the PPU core. The data stored here is 4 bytes; the position, color and tile, for each of the 64 sprites. This data is used by the PPU to place the sprite when it renders the frame. Background objects, however, are stored in a much less exclusive way, which is more like the way characters are stored in text mode on PCs. A background is defined by a simple data structure called a nametable. It's basically a two dimensional array where each entry is the tile number of one tile in the background, and the entry's position is the tile's place on the screen. The PPU has, without the use of memory mappers, two nametables, so smooth scrolling between backgrounds is possible. A color palette must be defined in order to show graphics on the screen. It is stored in a separate 32 byte location in RAM, known as "palette-RAM". Each entry here picks a color from the hardware color palette, which are the predefined colors to choose between. 16 colors can be chosen for sprites, and 16 colors for backgrounds. However, bytes 4, 8 and 12 of the sprite palette, and bytes 0, 4, 8, and 12 of the background palette, are not in use by the PPU. Therefore, the number of actually usable colors is reduced to 25 instead of 32. The first byte of the sprite palette also defines the global background color for both sprites and the background. Just one note about PPU, on the real NES hardware the CPU and PPU run simultaneously. This article tries to show you in a quick and very high level how ppu works.

Do you know the memory of PPU chip?

A.K.A memory map, this is the meaning of each range of ppu's memory.

 0000h-0FFFh Pattern Table 0 (4K) (256 Tiles)

 1000h-1FFFh Pattern Table 1 (4K) (256 Tiles)

 2000h-23FFh Name Table 0 and Attribute Table 0 (1K) (32x30 BG Map)

 2400h-27FFh Name Table 1 and Attribute Table 1 (1K) (32x30 BG Map)

 2800h-2BFFh Name Table 2 and Attribute Table 2 (1K) (32x30 BG Map)

 2C00h-2FFFh Name Table 3 and Attribute Table 3 (1K) (32x30 BG Map)

 3000h-3EFFh Mirror of 2000h-2EFFh

 3F00h-3F1Fh Background and Sprite Palettes (25 entries used)

 3F20h-3FFFh Mirrors of 3F00h-3F1Fh

* SPR-RAM Memory Map (8bit buswidth, 0-FFh)

  0-FF Sprite Attributes (256 bytes, for 64 sprites / 4 bytes each)

Sprite RAM is directly built-in in the PPU chip. SPR-RAM is not connected to PPU or PPU bus, and can be accessed via I/O Ports only.

What are those unfamiliar names/concepts ?

Tile : the lowest graphical component that ppu works with, you can understand it as a 8x8 pixel matrix that keeps the minimal data about graphics. The Super Mario Brothers 1 fills the initial PPU Memory with the following numbers (in binary representation).

[00000011]

[00001111]

[00011111]

[00011111]

[00011100]

[00100100]

[00100110]

[01100110]

ps: to form one 8x8 matrix is needed 16 bytes, the NES color is 2 bit depth.

[00-transparent, 01-first color, 10-second color, 11 third color]

Name Table : Is one bi-dimensional array that keeps the background map of all tiles evolved. Is 32x30 positions.

|1...32|

|......|

|......|

|30....|

Attribute Table: Is the attributes of set of tile in name table map. The properties of each piece of picture (color, position, tile number).

Maybe the image below can help you understand!

Understanding the Pattern Table

FIRST OF ALL DON'T WORRY ABOUT COLORS yet, try to think in just number 00(0), 01(1), 10(2) and 11(3).

What is a Pattern table? Quick Answer: the range of ppu memory 0000h to 1FFFh but I think that the easiest way to teach something is starting with one real example. The game Super Mario Brothers 1 has the following first tile and this first tile is formed by 16 bytes of data. (from address 0x0000 to 0x000F).

First C.            Second.                  Third.

First column has the 0x0000 to 0x0007 bytes, second column is the next 8 bytes and third column is the result:

To get the third column you will just: Take one bit from the second column shift one position to the left and make an OR operation with the one bit from the first column (both need to be in the same bit position). You can keep the tile information in something like this.

tile[y][x] = ((chr_rom[y+8] >> x & 0x1) << 1) | (chr_rom[y] >> x & 0x1);

It will keep the color in the refered tile.

The following pattern table is from Super Mario 3, note that the colors chosen were: 0=White, 1=Red, 2=Blue and 3=Green. (0= transparency) just for showing them. Two divisions, the upper is the pattern table 0 and the lower is pattern table 1, usually games put the Sprite tiles and Background tiles in there.

After you see that, you may be wondering... Hey! So the NES has just four colors? (three because one is "transparent")? No, Nes can have more colors by screen but by tile is just four. For a techie explanation the pixel depth of NES is only 2 bit.

Look at this Super Mario 3 picture, you can see it has much more than just four colors on the screen and when we pick one tile we see 5 colors (Super Mario's Head): red, black, pink (skin color), weak green and strong green.

So has it 5 colors? no, just remember in the tile we have one color that is TRANSPARENT, then the background layer is shown. In this case mario is a sprite and the green stuff is just the background (different layers). (remember sprite => movable and background => ‘static’)

(these black dots in the final image, I guess, is something generated by jpeg compression)

The feature that says what color (according with nes palette) is to each pixel is the joining of Tile with the attribute table. (one piece of memory too!)

The tile holds the lower two bits and the attribute table holds the upper two bits. Joining them you will have a binary with xxyy that is the index of the color in the palette. (You won't work with rgb (or hsb) values directly, just the index)

Name Table for what?

After you know the pattern table, you can move forward to the next concept, Name Table. Try to think of this is a map of tiles indexes. The following example was taken by the execution of F-8000 game. The Name Table 0 is a map of 32 columns by 30 rows (Quick and silly math here: if each tile has 8x8 pixel then the resolution will be 32*8 x 30*8 which is 256x240). All this summed 960 positions in the ppu memory.

When you run the F-8000 game you will have the screen. It takes just the lower part of name table (tile map) and we can see that the indexes (pointing to tiles) are to form the airplane. The PPU works like that: "hey in this position we need the tile number E0h to print on the screen, we need to lookup on pattern table".

Attribute table I never understand this!

NES Palette!

PS: All the examples below were done using the F800 Game.

The NES System Palette.

NES works with this color scheme. (and some variations too) As you probably had observed the 3F00h-3F1Fh address range (PPU Memory) keeps the Background and Sprite Palettes. In this piece of memory you'll have the index values to this colors. One more time let's sample this!

This snapshot was taken by JNesBr Memory Video Viewer from Game F8000 (binary and source of fighter_f8000) when the game first fills up the palette memory.

More specific palette memory mapper.

 3F00h Background Color (Color 0)

 3F01h-3F03h Background Palette 0 (Color 1-3)

 3F05h-3F07h Background Palette 1 (Color 1-3)

 3F09h-3F0Bh Background Palette 2 (Color 1-3)

 3F0Dh-3F0Fh Background Palette 3 (Color 1-3)

 3F11h-3F13h Sprite Palette 0 (Color 1-3)

 3F15h-3F17h Sprite Palette 1 (Color 1-3)

 3F19h-3F1Bh Sprite Palette 2 (Color 1-3)

 3F1Dh-3F1Fh Sprite Palette 3 (Color 1-3)

Joining the concepts and information we got this to First Filled Palette Memory (to F8000 game).

Try to find some colors patterns on running F8000 game.

And remember this values usually changes during the execution of the games. The JNesBR offers two ways to see the palette, you can see the actual palette and you also can see the system palette as picture below. (the same snapshot of F-8000 first palette filled)

Thanks for...

All trademarks and logos (Nintendo and the others) belongs to theirs owners and all rights must be respected.