Hello DS

 

Programming the Nintendo DS involves direct communicating with hardware resources. It requires low level approach since no operating system is between the programmer and the hardware and that’s why C is the preferred language. It is however possible to write  DS programs in other language. I know of people who write in C++ and also in Lua.

When writing for the hardware, it is almost always about reading and writing to and from memory addresses. It requires a lot of memory allocations and recycling and so forth. There are registers for display and other components that the programmer has to set values. This means that the programmer will need to memorize many memory locations. Fortunately the libnds contains meaningful macros that helps make life easier. There is another library called PA_lib that some programmers use, but libnds is by far the best library, and it gives full control over the hardware.

 

The best way to start programming is to…well, to write a program.

 

The code we are going to study is here:

 

#include <nds.h>

 

int main()

{

            powerON(POWER_ALL);

            irqInit();

            irqEnable(IRQ_VBLANK);

            while(1)

{

 swiWaitForVBlank();

}

}

 

Explanation:

We first include the libnds to be able to access its macros. It is possible to write and compile nds programs without libnds but including it will make our life much easier.

Then we have our main function. Like all C programs the execution of the program starts from the main function.

The first nds specific part is the next line:

powerON(POWER_ALL)

 

powerON is a function that turns on the hardware for use. It’s syntax is:

 

void powerON(int on)

 

and possible values for “int on” are (don’t be scared, you wont be needing to use all of these, at least not yet):

 

POWER_LCD : Controls the power for both LCD screens.

POWER_2D_A: Controls the power for the main 2D

POWER_MATRIX: Controls the power for the 3D matrix.

POWER_3D_CORE: Controls the power for the main 3D core

POWER_2D_B: Controls the power for the sub 2D

POWER_SWAP_LCDS: Controls which screen should use the main

POWER_ALL_2D: Enables power to all hardware required for 2D video.

POWER_ALL: Enables power to all hardware required for 3D video.

 

We use PowerON(POWER_ALL) throughout these tutorials and maybe throughout our life. With PowerON(POWER_ALL) we prepare NDS for the most challenging jobs because we turn on everything including 2D and 3D support.

 

Now to the next 2 lines:

irqInit();

irqEnable(IRQ_VBLANK);

 

The above 2 lines prepare NDS to handle V-BLANK interrupts. I myself don’t know much about interrupts and so I don’t try to explain them here (will leave it to a  later chapter). Dovotto (the great man behind the drunkercoders.com) advices us to add those two line to all our programs, and that’s why it’s been included there.

 

The while(1){} is the applications main application’s loop. Most of the game codes and functions and in fact the game loop will be executed from here. The function swiWaitForVBlank() which is in the while block has something to do with the interrupts that will be explained later.

 

 

To finish this chapter, we write a “Hello DS” program to at least see something printed on the screen. In the next chapter, however we will discover more ways to display text on the screen.

 

I imagine that while learning the C language you came across the function printf. The function allows you to print text on output. The Nintendo DS screen is no exception. We will use the printf function to write text on the screen, and if you recall, in order to do simple IO in C you need to include the header stdio.

By including the stdio.h you can use printf. However in order to write text on the screen we need to prepare the hardware to do just so, and since it is too early to talk about graphic modes and vram banks we will limit ourselves to a simple function that comes in handy when you want to test your ptogram(s).

The function consoleDemoInit is used in the below program. This function is included in console.h and it initializes the nds for prototyping.

 

#include <nds.h>

#include <stdio.h>

 

int main()

{

            powerON(POWER_ALL);

consoleDemoInit();

            irqInit();

            irqEnable(IRQ_VBLANK);

            printf("hello ds!");

            while(1){swiWaitForVBLANK}

}

 

 

Summary:

-         Include nds.h fpr all your programs.

-         Like every C program, an NDS program written in C has a main function.

-         Use powerON(POWER_ALL) to turn on hardware for 2D and 3D.

-         irqInit() and irqEnable(V_BLANK) are good stuff,

-         While(1) loop is the main loop.