Setting up Your Project

Copy the template

Now that you have devkitARM & friends setup, navigate to the examples folder (I hope you downloaded the examples!). Inside this folder there is plenty of example source code to learn from. For now, we are only interested in the DS template. Navigate to examples/nds/templates/arm9.

Make a little folder somewhere on your computer to hold your project files. Copy all of the arm9 template files into it.

The basics

Now it's time to write a little bit of code. Browse to the source folder of your project and open up main.c. Start by deleting everything :P, keystroke: Ctrl+A, Delete.

Now with our fresh source file, we want to add the basics to it. Start by #including the libnds definitions.

#include <nds.h>

Next, we will create the main function of the program.

int main( void )
{
    irqInit();                // initialize interrupts
    irqEnable( IRQ_VBLANK );  // enable vblank interrupt
    while(1) 
        swiWaitForVBlank();
    
    return 0;
}

In the above code, we make two calls to the libnds irq handler functions. The first one enables the interrupt handler. If you don't know what interrupts are, don't worry, you'll learn more about them later. Anyway, the second call enables the vertical blanking interrupt, it is required for the swiWaitForVBlank function to work.

What does swiWaitForVBlank do??? It is a special function that is built into the DS BIOS (basic in/out system (some standard functions for doing things)). What it does is halts the program until the next VBlank interrupt occurs.

Some newbies write code like this to wait for the next vblank:

void vblank_wait( void )
{
    while( REG_VCOUNT >= 160 ) {}
    while( REG_VCOUNT < 160 ) {}
}

Whenever I see code like the example above, I go curl up into a little corner and cry. Although this code will wait for the next VBlank state, it does not halt the CPU! While the CPU is halted it can be considered to be in a sleeping state, waiting to be woken up by an interrupt. In the sleeping state, the CPU will consume less power. swiWaitForVBlank will halt the CPU until the Vblank interrupt. swiWaitForVBlank makes your program more energy efficient!!

If you don't know what a "Vblank" is, don't worry! It will be explained soon. Basically, the code we have here will initialize interrupts, and wait in an (energy efficient) infinite loop.

Previous: Your suppliesContentsNext: Designing