How to use DevkitPro with Code::Blocks ( + code-level-debugging)

Note: This tutorial is for Windows users. Still, chances are high, that you can abstract it for use on Linux.

I recently took some interest in Nintendo DS development. I tried several IDE’s but what worked best for me was Code::Blocks. It took some time to get everything right (including code-level-debugging with Insight and DeSmuMe) but in the end it worked out pretty well. When I started I didn’t know very much about configuring Code::Blocks, so when I got it done, I lived in constant fear, that I wouldn’t be able to get it done again. Then the day hit, when I decided to reinstall my OS. I knew I’d have to do set up everything from scratch afterwards. It was quite a bit of work and took me two days to get everything up and running again, but this time I took notes, so I wouldn’t have to live in fear again.

A big part of the solutions I use in this tutorial is based on another tutorial by a guy named “Niozero”.  It’s in spanish but google-translator does a pretty good job this time. You can find it here:

http://niozero.blogspot.de/2008/08/tutorial-codeblocks-devkitpro.html

TABLE OF CONTENTS:

1.   DOWNLOADING SOFTWARE

2.   INSTALLING SOFTWARE

3.   SETTING UP CODE::BLOCKS FOR DEVKITPRO

3.1    Setting up the compiler

3.2   Creating a template project

3.3   Save the template

4.  ADD “Run in Emulator” TO TOOLS

5.  CODE LEVEL DEBUGGING

1. DOWNLOADING SOFTWARE:

This step is easy! Just download the following files:

Code:Blocks IDE:

http://www.codeblocks.org/downloads

devkitPro Windows Installer/Updater:

http://devkitpro.org/wiki/Getting_Started

DeSmuMe 0.9.10 developer version with GDB stub activated:

http://bit.ly/DeSmuMeGDBstub

2. INSTALLING SOFTWARE:

Run the devkitPro-installer. If you choose not to install to “c:\devkitpro”, wich should be the standart-path, keep in mind that you’ll have to use your own path whenever in this document is refered to “c:\devkitpro”.

Run the Code::Blocks installer. Since the Code::Blocks-path is not used in this tutorial you can install it to wherever you want without having to be smart to use this tutorial.

Extract the DeSmuMe-archive to “c:\devkitpro\emulators”.

3. SETTING UP CODE::BLOCKS FOR DEVKITPRO:

WARNING: IF CODE::BLOCKS CRASHES WHILE YOUR SETTING IT UP, ALL YOUR CHANGES ARE LOST! HOPEFULLY IT WON’T CRASH, BUT TO PLAY SAVE I HIGHLY RECOMMEND YOU TO QUIT AND RESTART  CODE::BLOCKS AFTER EVERY FEW CONFIGURATIONS YOU MAKE. THIS WAY CRASHING WILL JUST TAKE YOU BACK TO THE LAST TIME YOU RESTARTED.

I use Code::Blocks 12.11 for this tutorial. Other (espescially newer) versions will probably do the job, but some things might be found in other places.

3.1 Setting up the compiler:

  • Go to menu ‘settings -> compiler’.

  • Choose “GNU GCC COMPILER” from the drop down menu entitled

  • “Selected compiler”.

  • Click on “Copy” and enter “DevKitPro ARM Compiler” as a name. “OK”

  • A window reminding you to set toolchains will appear. “OK”

  • Click the “Toolchain executables” tab.

  • Set “Compiler’s installation directory” to: “c:\devkitPro\msys”.

  • Click the “Additional Paths” tab.

  • Click “add” and type in “c:\devkitPro\devkitARM\bin”. “OK”

  • Go back to the “Program Files” tab and fill in the following settings:

C compiler:                 arm-none-eabi-gcc.exe

c++ compier:                arm-none-eabi-c++.exe

linker for dynamic libs:    arm-none-eabi-c++.exe

linker for static libs:        arm-none-eabi-ar.exe

make program:                make.exe

Code::Blocks should now be able to use the devkitPro-ARM-compiler. Since some settings are also to be made to a project in order to compile to NDS-ROMS we will now create a template project so we can later easily kickstart into NDS-development.

3.2 Creating a template project:

  • Go to menu ‘file -> new -> project’.

  • Select “Empty project”. “Go”

  • If a welcome message appears click “Next”.

  • Choose “Project title” and “Folder to create project in”. It’s up to “Next”

  • Choose “DevKitPro ARM Compiler” as your compiler. (Or whatever you named it.)

  • You can check of “Create “Debug” configuration” as we won’t need it. “Finish”

  • Now copy all contents from “C:\devkitPro\examples\nds\hello_world” to your newly created project’s directory.

  • In codeblocks rightclick on your new project in the management sidebar (right under “workspace”).

  • Select “add files recursively” and navigate to your project’s directory. “OK”.

  • Press “Select All”. “OK”

  • You should now see two folders “Sources” and “Others” below your project in management sidebar.

  • Click the “+” left to Sources and again “+” left to the now visible “source” folder and doubleclick main.cpp.

You should now see the code to a simple “hello world” program. Try pressing the compile button (ctrl + f9). You should get an error stating that “nds.h” is missing. This happens because you didn’t specify in your project’s settings, where to find the libnds-library.

  • Go to menu ‘project -> properties’.

  • Check “This is a custom Makefile”.

  • Set “Execution directory” to “${PROJECT_DIR}”. “OK”

  • Go to menu ‘project -> build options’.

  • Make sure “DevKitPro ARM Compiler” is “Selected compiler”.

  • Click on the “”Make” commands” tab.

  • Set the following values:

Build project/target:     $make -f $makefile

Compile single file:        $make -f $makefile

Clean project/target:        $make -f $makefile clean

Ask if rebuild is needed:    $make -q -f $makefile clean

3.3 Save the template:

Delete the hello_world.pnproj and hello_world.prj files from your project folder.

The main.cpp file will be your starting point for every project you create from the template. You can change it to whatever you like. I’ve simplified mine to the following:

#include <nds.h>
#include <stdio.h>

int main(void)
{
while(1)
{

swiWaitForVBlank();
}
return 0;
}

When you’re done changing your file go to menu ‘file -> save project as template’ and name it “NDS ROM” or anything you find fitting.

4. ADD “Run in Emulator” TO TOOLS:

My emulator of choice is DeSmuMe and I’ll use that one in this tutorial. Mainly because I got it to communicate with the debugger “Insight” (part of the devkitPro-package). If you prefer another emulator like no$gba you should easily be able to adapt my settings to your choice, since it’s not very complicated.

  • Go to menu ‘tools-> configure tools’. “Add”

  • Set to the following values:

Name:                 Run in DeSmuMe (or whatever you use)

Executable:            C:\devkitPro\emulators\DeSmuME\DeSmuME_VS2008_dev+.exe (Name of the exe can differ with other versions.)

Parameters:            ${PROJECT_DIR}\${PROJECT_NAME}.nds

Working Directory:    ${PROJECT_DIR}

  • Check “Launch tool visible (without output redirection)”. “OK” “OK”

  • Go to menu ‘settings -> editor’.

  • Select “Keyboard shortcuts” in the left sidebar.

  • In the “Commands”-subwindow, click on the little “+” left to “Tools”.

  • Click on the now visible entry “Run in DeSmuMe”.

  • Click into the field below “new shortcut” and press what ever keys you want to use. (In my case ctrl+shift+F5). “Add” “OK”

5. CODE LEVEL DEBUGGING:

If you used another emulator than the GDB-stub-enabled DeSmuMe version you downloaded in step 1. you’ll have to download and extract it now. (Revisite steps 1 and 2.) This version of DeSmuMe is able to communicate

with Insight, our debugger of choice. Insight is part of the devkitPro-package and should be already installed in your devkitpro folder.

Note: I have a strong feeling, that it’s also possible to use the debugger that comes with Code::Blocks along with DeSmuMe, but I didn’t get it to work. If you do I would really like to learn about how you did it! – In the insight folder “C:\devkitPro\insight\bin” create a batch file. I called mine “forcodeblocks.bat”. (Just a .txt file renamed to .bat.)

  • Into the batch file put the following lines:

start C:\devkitPro\emulators\DeSmuME\DeSmuME_VS2008_dev+.exe –arm9gdb=20000 %1.nds
C:\devkitPro\insight\bin\arm-eabi-insight.exe %1.elf
exit

  • Now again go to ‘tools->configure tools’ and click “Add”.

  • Fill in the following:

Name:                    Debug in Insight

Executable:            C:\devkitPro\insight\bin\forcodeblocks.bat

Parameters:            ${PROJECT_NAME}

Working Directory:    ${PROJECT_DIR}

  • Again you can add a shortcut like we did with “Run in DeSmuMe”. (Step 4.) I chose “ctrl+shift+F4”.

  • If you don’t have the template project we created earlier open open it now (or start a new from template).

  • Go to the menu ‘tools -> Debug in Insight’ or press your shortcut.

Insight and DeSmuMe should open. Now we have to connect the two programs. Therefore we go to insight’s menu ‘run->connect to target’.

  • A new window should pop up.

  • Set “Target” to GDBserver/TCP

  • Set “Port” to 20000. Hostname can be left blank. “OK”

This has to be done only once. After you clicked on OK you should get the message “successfully connected”. If you want to change this settings later go to file->target settings.

You should now be able to use insight as your debugger and see the results live in DeSmuMe.

A problem that at least occurs on my machine is, that I am unable to set breakpoints by mouse clicking. Fortunately there is a  workaround. Just enable the insight console by pressing ctrl+n and enter “b x” (where is x is the line you want to break at). Voila: There’s your breakpoint.

Note, that you must set your breakpoints after you connected to DeSmuMe. If you set them before, they will be ignored.

Now after you set your first breakpoint press C. Your ROM should start in DeSmuMe and runs until the breakpoint is reached. At that point you’re thrown back into insight, exactly at the line you wanted to break. You can now step through the program as you want with DeSmuMe updating for every step.