Nintendo DS: Print text to iDeaS debug console

In this article I develop a routine to print text to iDeaS debug console. You can download the source code of this article here.

“iDeaS” is a Nintendo DS emulator. As of version (21 Dec 2008), it supports program breakpoints and user messages that can be sent to its debug console.

Copy/Paste from iDeaS (21 Dec 2008) changelog:

  • Added program breakpoint (SWI #0xFDFDFD).
  • Added output on console for user’s messages (SWI #0xFCFCFC).

Unfortunalty, both features have been implemented using software interrupts that do not exist on the target hardware. These software interrupt ID’s actually make the NDS application incompatible with the NDS hardware. Whenever you print an iDeaS debug message, the application will crash/freeze on real hardware.

How a workaround could look like

A short time after the release, I contacted the iDeaS author and told him his approach shouldn’t the way to go and asked to change it that it won’t break program execution on actual hardware. Unfortunalety he didn’t see the advantage, because you could:

  • Remove all prints before you test on hardware

Obviously, this cannot be the way to handle it. Manually removing all prints would be enormously time consuming and error-prone.

  • Use some sort of #ifdef blocks to automatically remove all prints

This might sound like a solid idea at first glance, but thinking about it a further minute, proves it isn’t. The problem is it’s too time consuming. If you use the pre-processor to remove all print calls, you have to rebuild the entire project every time you change the corresponding pre-processor switch. Depending on how many files the project contains, it can take a significant amount of time to rebuild, which is no option for me.

  • Wrap print calls with some enabled/disabled mechanism

A function that wraps print calls with a surrounding “if” block, does remove the need to rebuild the project. Just disable debug output at program initialization and you’re set. However, I tend to forget unimportant things and this is definitely something I will forget many times.

How a solution could look like

I want a debug text system that doesn’t break program execution, no matter if the application runs in an emulator or on real hardware. All previous points do make some sense, but don’t remove the problem entirely.

The debug text system should:

  • Print text to the debug console
  • Can be switched on/off at runtime
  • Automatically detects when it runs on hardware and discards all print calls in this case
  • Remove all print calls when building a “release version” (pre-processor)

Everthing on the list should be quite clear, except for hardware detection. While wondering how to implement that, I remembered this post at They used a hardware feature that no emulator seems to emulate correctly at this time (Dec 2008), called “instruction fetching”:

The “str r0, _0” instruction overwrites the following instruction, where “r0” is set to “1”. But on real hardware, the instruction would have been fetched already and the change has no effect. Basically the memory at this address is being overwritten, but the instruction pipeline fetched the instruction before, so the original instruction is used. When you call the function more than once, it won’t work correctly anymore.

I slightly modified the above code to restore the original instruction to support calling it more than once. I also made it arm and thumb compatible. Here is the entire source code to print text to iDeaS debug console, which detects if running on real hardware and then discards all prints. You can also use a pre-processor #define to remove all print calls when you build a “release version”.