back

GPU

Table of Contents

Overview

All the GPU does is display to the screen based off what is in the VRAM. Therefore, to display to the screen, you must fill the VRAM accordingly. If you are using Mapache 64's custom linker script, you can use the API described in vram.h .

There are 4 parts of the VRAM: Pattern Memory, Nametable, Object Memory, and Text Table.


If you’ve never worked with sprites and tiles, I recommend learning how NES sprites are stored and assembled into metasprites before proceeding:

Note: the GPU in this project took heavy inspiration from the NES’s PPU:

Pattern Memory

Pattern Memory stores collections of 8px×8px sprites. Each pixel is stored with 2 bits, and a group of 4 pixels is stored as 1 byte. Each sprite is 16 bytes total.

Pixel Data

Note: pixel data is separate from color data. A color filter can be applied over a sprite by the Nametable or Object Memory.

Pixel Data

Observe the layout of the Pattern Memory:

Pattern Memory Diagram

This is an example of how sprites are stored in PMF.

Nametable

The Nametable is a 32×30 array of tiles that represent the entire 256px×240px screen. Each tile must choose 1 sprite from PMB to display. Each tile can be flipped horizontally and flipped vertically. This allows for reuse of sprites.

The Nametable also stores 2 global colors. Each tile must select one of the two colors. You can look at the VRAM Randomizer example to see this in action.

Nametable Diagram

Object Memory

The Object Memory is used to identify the location of the sprites on the screen.

Each object has an X and Y position, a sprite address, an H-Flip and V-Flip modifier, and a color. If two objects overlap, whichever object appears first in OBM will be on top.

Object Memory Diagram

Text Table

The Text Table (TXBL) can be used to write text over the screen. The TXBL is similar to the NTBL, except the TXBL's tiles appear in front of foreground objects and uses read-only character pattern memory (PMC) instead of custom sprites from the PMB.

There are 128 characters in the font. Each character can be accessed by using the lower 7 bits of the character data in the TXBL. The font uses ASCII for all printable characters.

PMC Font

If the character data's color select is 0, the character is printed black. If the character data's color select is 1, the character is printed white.

Text Table Diagram

Video Timing

The GPU will follow the VESA 640×480 @ 60 Hz protocol. Observe this timing diagram:

VGA Timing Diagram

The GPU updates each pixel row by row. After drawing a row, it waits for a horizontal blanking period. After finishing the entire frame, it waits for a vertical blanking period.

If you want more information about video timings, watch this:

Interrupts

The CPU must only touch the VRAM while inside the fill_vram subroutine. This ensures that the CPU and the GPU are not trying to access the VRAM at the same time. Every time the GPU needs to use the VRAM, it sends an interrupt to the CPU. If the CPU is in fill_vram, it will pause. Once the GPU no longer needs the VRAM, it will send another interrupt to the CPU so that the CPU knows when to continue. Observe this code: