LiraNuna's Development Blog

Nintendo DS 2D HW Tutorials
By LiraNuna

Lesson 2: Talking to our DS

Table of content
Setting up a text BG
Setting up a video mode
Converting the image
Copying the data
Final code

Setting up a text BG
After the first lesson’s overview, you should now know theoreticly everything related to how text backgrounds are working. How about using this new knowladge and set up our own text BG?

There are several simple steps for setting up a normal text BG:

  1. Set up the registers to the settings we want.
  2. Convert the image that we want to a format recognizeable by the DS.
    • Note: It is also possible to create the data during run-time, but let’s keep it simple for now.
  3. Copy the tileset to the correct tile base.
  4. Copy the map to the correct map base.
  5. Copy the palette to the palette space.

Whoa whoa wait a second! Registers? what are those?
Registers are a memory offset that trigger a special ‘mode’ or ‘status’ in the hardware. In this case, we will change the video registers to match our requirements. Setting a register on the DS is easy as setting a variable – and with libnds’s predefined register set, it’s even more easy.

Setting up a video mode
Frist step would be to set up the video mode using libnds’ define for the main display control register – REG_DISPCNT (Register – Display Control)

REG_DISPCNT = MODE_0_2D | DISPLAY_BG0_ACTIVE;

REG_DISPCNT – The display register.
MODE_0_2D – Video mode 0 in 2D mode (See lesson 1).
DISPLAY_BG0_ACTIVE – BG0 will be shown up on the screen.

After setting up the video mode with the desirable backgrounds, we move on to setting the background itself using it’s own control register. Like the display, each background has it’s own control register, pre-defined in libnds as REG_BGxCNT (Replace x with a number of the BG). We will set up BG0.

REG_BG0CNT = BG_32x32 | BG_COLOR_256 | BG_MAP_BASE(0) | BG_TILE_BASE(1);

REG_BGxCNT – Background 0 control register.
BG_32x32 – Sets up the background to use the size of 32 tiles on 32 tiles (256×256).
BG_COLOR_256 – Sets up the BG to be in 256 color palatted mode.
BG_MAP_BASE(0) – The background will be using map base number 0.
BG_TILE_BASE(1) – The background will be using tile bases 1 – 4. (a full 256 colors background will take up four tile bases to fill up a whole tile set, as said in Lesson 1).

We are still not finished, there is another step to be taken, which is setting up the VRAM bank. I will not expend about VRAM banks right now, as I will give this subject a whole new lesson with tips and tricks of how to smartly use them.
VRAM bank is a partition in memory which is allocated to use for Video RAM, we will set VRAM Bank A (First one) to use with our Bacground display:

VRAM_A_CR = VRAM_ENABLE | VRAM_A_MAIN_BG;

After we have set the DS to be in our desirable mode, we proceed to convert our image to a format recognizeable by the DS.

Converting the image
To convert images to a format the DS can use, we will use a special tool called gfx2gba (Click to download).
although gfx2gba is a GBA tool, it is still relevent to the DS as the GBA video HW and the DS’s video HW are almost identical. Before we learn how to use this devine application (Trust me – after learning how to use it, you will not be able to leave it), we must find an image to use. gfx2gba takes all sorts of formats such as .bmp and .pcx – both paletted. Since it is not the place to explain how to convert a normal .bmp to paletted one (Not this lesson atleast), I will include the .bmp here.


The Image we will convert. a 256×256 .bmp file.

To convert the image to a format recognizeable by the DS using gfx2gba, start the command line and run gfx2gba using the following commands:

gfx2gba -c256 -m -t8 -pwoodPal.bin wood.bmp

Options:
-c256 – Color mode of 256 colors.
-m – Creates a map (.map file).
-t8 – Tiles sized 8×8
-pFILENAME – The palette filename.

After converting, you should get the following files:

  • wood.raw – The binary tile set
  • wood.map – The binary map of the background
  • woodPal.bin – The 256col palette

All of those files are neccessery in order to form a complete text BG, but we need to make one final adjustment and rename the files to an extention that devkitARM’s bin2o macro can handle. It is usefull to rename the files so you can know what data they contain. For this example, rename the files as follow:

wood.raw -> woodTiles.bin
wood.map -> woodMap.bin

After we successfully converted the images and made them ready to use, we include them into our project:

#include "woodTiles_bin.h"
#include "woodMap_bin.h"
#include "woodPal_bin.h"

And on to the next step.

Copying the data
The easiest part of all. normal copy the data to the place they should be. We setted up the tileset to start at tile base 1 and the map to be located at map base 0. Why is that? Why did I chose those numbers? As said in Lesson 1, each tile base takes 8 map bases. because we have only 16 map bases, it comes up that the tilebases are overlapping free starting from tile base 2. In this example, we will not use more then 8 maps (located in tile base 0) so we keep tile base 0 to contain maps.
Now to the actuall copy to those tile bases

	// Copying the tiles to tile base 1
memcpy((void*)BG_TILE_RAM(1), woodTiles_bin, woodTiles_bin_size);
	// Copying the map to map base 0
memcpy((void*)BG_MAP_RAM(0), woodMap_bin, woodMap_bin_size);
	// Copying the palette to the palette area
memcpy((void*)BG_PALETTE, woodPalette_bin, woodPalette_bin_size);

Simple, isn’t it?

After we successfully copied our data, we go into an infinate loop to keep our application running.

while(1);

Final code

	// Including libnds' set of defines
#include <nds.h>
	// Include basic memory functions
#include <string.h>
 
	// Including our converted graphics
#include "woodTiles_bin.h"
#include "woodMap_bin.h"
#include "woodPal_bin.h"
 
int main()
{
		// Setting up video mode
	REG_DISPCNT = MODE_0_2D | DISPLAY_BG0_ACTIVE;
	REG_BG0CNT = BG_32x32 | BG_COLOR_256 | BG_MAP_BASE(0) | BG_TILE_BASE(1);
 
		// Setting VRAM Bank A for Background display
	VRAM_A_CR = VRAM_ENABLE | VRAM_A_MAIN_BG;
 
		// Copying the tiles to tile base 1
	memcpy((void*)BG_TILE_RAM(1), woodTiles_bin, woodTiles_bin_size);
		// Copying the map to map base 0
	memcpy((void*)BG_MAP_RAM(0), woodMap_bin, woodMap_bin_size);
		// Copying the palette to the palette area
	memcpy((void*)BG_PALETTE, woodPal_bin, woodPal_bin_size);
 
		// Infinate loop to keep the program running
	while(1);
}

Download a complete project folder HERE.