VRAM Banks

Before we start loading graphics, we must understand the VRAM bank settings. This is one of the things that I was stuck on for a while when switching to DS homebrew from GBA.

The DS has 9 VRAM banks, they have varied sizes and purposes. Here is a diagram that shows what each VRAM bank size is, and what they can be used for.

This is a confusing diagram ;). There is some information that is not shown, like what memory address is used when the VRAM is mapped for certain functions. If you want a complete description of each vram bank and it's settings, read the GBATEK article named "DS Memory Control - VRAM".

I shall now explain what the purpose is for each VRAM bank setting.

LCDC

This is a somewhat special mode that every VRAM bank can use (not shown in diagram). It maps the memory for fast access by the ARM9 cpu. There are a few exceptions where the video hardware can use it too though. The display capture hardware can write to banks A, B, C or D when they are allocated for LCDC access. The rendering engine can also display a 16-bit bitmap from banks A, B, C, or D when they are allocated to this mode.

MAIN ENGINE, BG GRAPHICS

Almost every bank can be allocated to hold graphical information for the main engine's backgrounds. When banks are allocated to this mode, they can be used for holding tile data for backgrounds, tilemaps, bitmap data for extended BGs, and maybe something else that I missed. Banks A, B, C, D, E, F, and G can use this mode. We want to use one of those banks to hold the background graphics for this game.

MAIN ENGINE, OBJ GRAPHICS

OBJ means object or more commonly sprite. Banks allocated to this mode can hold tile graphics (or bitmaps) to be displayed by sprites (or objects). We want one of these banks to hold our bouncy ball sprite graphic.

MAIN ENGINE, BG EXT. PALETTE

I haven't played with this much, in this mode, the bank can be used to hold an extended palette for use by backgrounds (with special settings). This mode is not mapped for ARM9 CPU access, so you must allocate the bank to another mode (like LCDC) to write the palette data to it.

MAIN ENGINE, OBJ EXT. PALETTE

I have never used this mode. It is like the BG EXT. PALETTE but it's for sprites.

ARM7 WORK RAM

In this mode, the vram bank is assigned to a memory region that the ARM7 CPU can access. This mode is used in GBA mode for the GBA's 256KB of external work ram.

3D TEXTURE PALETTE

When VRAM banks are allocated for this purpose, they can hold palette data for 3D stuff. The 3D textures may be in a special paletted format that will use data from banks allocated for this purpose.

3D TEXTURES

This mode is used for holding 3D textures.

SUB ENGINE, BG GRAPHICS

This is just like the main engine's mode, but it is a bit more limited. Only three banks can be assigned to this mode. But only 128KB max can be used with this mode; Banks H and I overlap bank C when they are allocated together.

SUB ENGINE, OBJ GRAPHICS

This one is just like the main engine's mode for holding sprite graphics. Notice that it is also very limited on the amount of memory that can be allocated, although this is actually quite huge compared to the GBA's 32KB of OBJ vram :).

SUB ENGINE, BG EXT. PALETTE

The sub-engine can use extended palettes too, this is for the sub-engine's backgrounds.

SUB ENGINE, OBJ EXT. PALETTE

And finally, this one is for the sub-engine's sprites that use extended palettes.

Changing the settings

Now, to change the VRAM bank mapping, you can use the libnds vramSetBankX function where X is replaced by the letter associated with the bank. There are many libnds definitions for each bank setting, you can look at video.h in the libnds headers to see the enumerations for them. In this tutorial we will use only 2 bank settings: VRAM_E_MAIN_BG, and VRAM_F_MAIN_SPRITE. The bank setup code should look something like this:

vramSetBankE( VRAM_E_MAIN_BG );
vramSetBankF( VRAM_F_MAIN_SPRITE );
Previous: Program flowContentsNext: Converting the graphics