<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>console-dev.de &#187; NDS</title>
	<atom:link href="http://www.console-dev.de/category/nintendo-ds/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.console-dev.de</link>
	<description>Home of VsTortoise, VisualHAM, N3D and HEL Library</description>
	<lastBuildDate>Fri, 14 May 2010 07:15:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>guru meditation error (data abort exception)</title>
		<link>http://www.console-dev.de/2009/09/20/guru-meditation-error-data-abort-exception/</link>
		<comments>http://www.console-dev.de/2009/09/20/guru-meditation-error-data-abort-exception/#comments</comments>
		<pubDate>Sun, 20 Sep 2009 08:06:25 +0000</pubDate>
		<dc:creator>Peter Schraut</dc:creator>
				<category><![CDATA[NDS]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.console-dev.de/?p=751</guid>
		<description><![CDATA[The processors built-in the Nintendo DS, an ARM946E-S and ARM7TDMI, support hardware exceptions such as &#8220;Data Abort &#8211; accessing invalid memory addresses&#8221; and &#8220;Undefined Instruction&#8221;. The Nintendo DS homebrew library libnds includes exception handling and comes with a default handler that displays the exception type, at which address in the instruction sequence the exception occured [...]]]></description>
			<content:encoded><![CDATA[<p>The processors built-in the <a href="http://en.wikipedia.org/wiki/Nintendo_DS">Nintendo DS</a>, an <a href="http://en.wikipedia.org/wiki/ARM9">ARM946E-S</a> and <a href="http://en.wikipedia.org/wiki/ARM7">ARM7TDMI</a>, support hardware exceptions such as <em>&#8220;Data Abort &#8211; accessing invalid memory addresses&#8221;</em> and <em>&#8220;Undefined Instruction&#8221;</em>. The Nintendo DS homebrew library <a href="http://en.wikipedia.org/wiki/Libnds">libnds</a> includes exception handling and comes with a default handler that displays the exception type, at which address in the instruction sequence the exception occured and a dump of all registers.</p>
<p>In this article I show what you can do with this information and why it is useful during development.</p>
<h3>libnds exceptionTest example</h3>
<p>If you installed the libnds examples, you should find the <em>exeptionTest</em> example in <em>devkitPro\examples\nds\debugging\exceptionTest</em>.</p>
<p>Copy&#038;Paste from exceptionTest.c:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;nds.h&gt;</span>
&nbsp;
<span style="color: #ff0000; font-style: italic;">/*---------------------------------------------------------------------------------
&nbsp;
 The default exception handler displays the exception type - data abort or undefined
 instruction you can relate the exception to your code using
&nbsp;
    arm-eabi-addr2line -e &lt;elf file&gt; &lt;address&gt;
&nbsp;
 assuming you built with debug info this will display a source file and a line number
 The address of the instruction is shown as pc, beside the address which faulted
 the rest of the screen is a dump of the registers.
&nbsp;
---------------------------------------------------------------------------------*/</span>
&nbsp;
<span style="color: #666666;">//---------------------------------------------------------------------------------</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">// install the default exception handler</span>
  defaultExceptionHandler<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// generate an exception</span>
  <span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #208080;">0xFA</span> <span style="color: #000080;">=</span> <span style="color: #208080;">0x64</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// never comes here</span>
  <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
    swiWaitForVBlank<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>When you build and run the example project, it displays:</p>
<p><a href="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/libnds_guru_meditation_data.png"><img src="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/libnds_guru_meditation_data.png" alt="Guru Meditation Error! - Data Abort" title="Guru Meditation Error! - Data Abort" width="286" height="443" class="alignnone size-full wp-image-755" /></a></p>
<p>This is actually a fake screenshot. I rebuilt the <em>Guru Meditation Error</em> with printf, because the example didn&#8217;t behave in any emulator as on actual hardware. I tested <a href="http://nocash.emubase.de/gba.htm">no$gba 2.6a</a>, <a href="http://www.ideasemu.org">iDeaS 1.0.3.3 beta</a> and <a href="http://sourceforge.net/projects/desmume/">DeSmeME 0.9.5</a>, none of them displayed the error report as on the real Nintendo DS hardware.</p>
<h3>guru meditation origin</h3>
<p><a href="http://en.wikipedia.org/wiki/Guru_meditation">Guru Meditation</a> is the name of the error that occurred on early versions of the <a href="http://en.wikipedia.org/wiki/Commodore_Amiga">Commodore Amiga</a> computer when they crashed. It is analogous to the &#8220;Blue Screen Of Death&#8221; in Microsoft Windows operating systems.</p>
<p>The Guru Meditation error message was a black screen with the following animation at the top:</p>
<p><a href="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/amiga_guru_meditation.gif"><img src="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/amiga_guru_meditation.gif" alt="Commodore Amiga - Guru Meditation error message" title="Commodore Amiga - Guru Meditation error message" width="652" height="93" class="alignnone size-full wp-image-756" /></a></p>
<h3>decode nds guru meditation data abort message</h3>
<p>One of the Nintendo DS variants of the Guru Meditation error message is the data abort, which includes the following information:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">     Guru Meditation Error!    
          data abort!          
&nbsp;
  pc: 02000386 addr: 000000FA  
&nbsp;
  r0: 020005BD   r8 : 00000000 
  r1: 00000000   r9 : 00000000 
  r2: 00000064   r10: 00000000 
  r3: 000000FA   r11: 00000000 
  r4: 0B000000   r12: 02000000 
  r5: 00000000   sp : 0B003CF8 
  r6: 00000000   lr : 020005B5 
  r7: 00000000   pc : 0200038E 
&nbsp;
  0B003CF8: 0B000000 02000180  
  0B003D00: 00000000 00000000  
  0B003D08: 00000000 00000000  
  0B003D10: 00000000 00000000  
  0B003D18: 00000000 00000000  
  0B003D20: 00000000 00000000  
  0B003D28: 00000000 00000000  
  0B003D30: 00000000 00000000  
  0B003D38: 00000000 00000000  
  0B003D40: 00000000 00000000</pre></td></tr></table></div>

<p>The first line is always &#8220;Guru Meditation Error!&#8221;, followed by the type of the exception:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">     Guru Meditation Error!    
          data abort!</pre></td></tr></table></div>

<p>The libnds default exception handler displays &#8220;Data Abort&#8221; and &#8220;Undefined Instructions&#8221; exceptions.</p>
<p>Below the exception type is <em>pc</em> and <em>addr</em> displayed, which I guess is not very obvious for many novice programmers:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">  pc: 02000386 addr: 000000FA</pre></td></tr></table></div>

<p><em>pc</em> is short for <a href="http://en.wikipedia.org/wiki/Program_counter">Program Counter</a>. The Program Counter register indicates where the processor is in its instruction sequence. In this case, it&#8217;s the address of the instruction that caused the exception.</p>
<p><em>addr</em> represents the exception address. In this case, writing to the memory address 0xFA:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">  <span style="color: #666666;">// generate an data abort exception</span>
  <span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #208080;">0xFA</span> <span style="color: #000080;">=</span> <span style="color: #208080;">0x64</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>This again is followed by a register dump:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">  r0: 020005BD   r8 : 00000000 
  r1: 00000000   r9 : 00000000 
  r2: 00000064   r10: 00000000 
  r3: 000000FA   r11: 00000000 
  r4: 0B000000   r12: 02000000 
  r5: 00000000   sp : 0B003CF8 
  r6: 00000000   lr : 020005B5 
  r7: 00000000   pc : 0200038E</pre></td></tr></table></div>

<ul>
<li><em>r0</em> &#8211; <em>r12</em> are the so called <a href="http://en.wikipedia.org/wiki/Processor_register">General Purpose Registers</a>.</li>
<li><em>sp</em> (r13) is the <a href="http://en.wikipedia.org/wiki/Call_stack#Structure">Stack Pointer Register</a>, which indicates the current top of stack memory.</li>
<li><em>lr</em> (r14) represents the <a href="http://en.wikipedia.org/wiki/Link_register">Link Register</a>, which holds the return address when the current function completes (it&#8217;s the address of the caller).</li>
<li><em>pc</em> (r15) is short for <a href="http://en.wikipedia.org/wiki/Program_counter">Program Counter</a>. The Program Counter register indicates where the processor is in its instruction sequence.</li>
</ul>
<p>The last information in the error report is a memory dump of the top 80 bytes of stack memory:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">  0B003CF8: 0B000000 02000180  
  0B003D00: 00000000 00000000  
  0B003D08: 00000000 00000000  
  0B003D10: 00000000 00000000  
  0B003D18: 00000000 00000000  
  0B003D20: 00000000 00000000  
  0B003D28: 00000000 00000000  
  0B003D30: 00000000 00000000  
  0B003D38: 00000000 00000000  
  0B003D40: 00000000 00000000</pre></td></tr></table></div>

<ul>
<li>First column specifies the stack address (0B003CF8)</li>
<li>Second column shows the 32bit value from the stack address (0B000000)</li>
<li>Third column shows the 32bit value from the stack address + 4 bytes (02000180)</li>
</ul>
<h3>how to make use of all this</h3>
<p>This information is informative only when the output file was built with debug information. You can include debug information by adding the <a href="http://gcc.gnu.org/onlinedocs/gcc-4.4.1/gcc/Debugging-Options.html#Debugging-Options">-g option (produce debugging information)</a> to CFLAGS in the project makefile:</p>
<p>Copy&#038;Paste from <em>exceptionTest</em> makefile:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">CFLAGS	:= -g -Wall -O2\
		-march=armv5te -mtune=arm946e-s \
 		-fomit-frame-pointer\
		-ffast-math \
		$(ARCH)</pre></td></tr></table></div>

<p><a href="http://www.devkitpro.org">devkitARM</a> includes an application named <em>&#8220;arm-eabi-addr2line.exe&#8221;</em>, located in <em>devkitPro\devkitARM\bin</em>, that can be used to convert addresses into line number/file name pairs. It supports the following command line options:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">Usage: arm-eabi-addr2line.exe [option(s)] [addr(s)]
 Convert addresses into line number/file name pairs.
 If no addresses are specified on the command line, they will be read from stdin
&nbsp;
 The options are:
  @&lt;file&gt;                Read options from &lt;file&gt;
  -b --target=&lt;bfdname&gt;  Set the binary file format
  -e --exe=&lt;executable&gt;  Set the input file name (default is a.out)
  -i --inlines           Unwind inlined functions
  -j --section=&lt;name&gt;    Read section-relative offsets instead of addresses
  -s --basenames         Strip directory names
  -f --functions         Show function names
  -C --demangle[=style]  Demangle function names
  -h --help              Display this information
  -v --version           Display the program's version</pre></td></tr></table></div>

<p>We are interested in the address where the Data Abort exception occured, named <em>pc</em> here:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">     Guru Meditation Error!    
          data abort!          
&nbsp;
  pc: 02000386 addr: 000000FA</pre></td></tr></table></div>

<p>I used the following commandline for conversion:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">arm-eabi-addr2line.exe -f -C -e  -s -i exceptionTest.elf 02000386</pre></td></tr></table></div>

<p>The output is:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">main
exceptionTest.c:23</pre></td></tr></table></div>

<p>When you scroll at the beginning of this article, where I present the source code of the exceptionTest example, you see line 23 is inside main(), which is stored in exceptionTest.c and contains the code that writes to the address 0xFA.</p>
<h3>conclusion</h3>
<p>Make sure to install an exception handler in your projects. It&#8217;s very helpful to actually see that an error occured rather than a freeze. libnds makes it enormously easy, it&#8217;s just one line of code!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.console-dev.de/2009/09/20/guru-meditation-error-data-abort-exception/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>create nintendo ds applications with visual studio 2008</title>
		<link>http://www.console-dev.de/2009/09/03/create-nintendo-ds-applications-with-visual-studio-2008/</link>
		<comments>http://www.console-dev.de/2009/09/03/create-nintendo-ds-applications-with-visual-studio-2008/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 07:43:00 +0000</pubDate>
		<dc:creator>Peter Schraut</dc:creator>
				<category><![CDATA[NDS]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Homebrew]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[VisualHAM]]></category>

		<guid isPermaLink="false">http://www.console-dev.de/?p=712</guid>
		<description><![CDATA[introduction
In this article I show how to set up Microsoft Visual Studio 2008 to use the devkitARM compiler to create applications for the Nintendo DS. After you have read this article, you are able to use Visual Studio 2008 to build, rebuild, clean and execute your Nintendo DS application.
Visual Studio 2008 Express can be downloaded [...]]]></description>
			<content:encoded><![CDATA[<h3>introduction</h3>
<p>In this article I show how to set up <a href="http://www.microsoft.com/express/">Microsoft Visual Studio 2008</a> to use the <a href="http://www.devkitpro.org">devkitARM</a> compiler to create applications for the Nintendo DS. After you have read this article, you are able to use Visual Studio 2008 to build, rebuild, clean and execute your Nintendo DS application.</p>
<p>Visual Studio 2008 Express can be downloaded for free at microsoft.com, you want to have the C++ edition: <a href="http://www.microsoft.com/express/download/">Download Visual C++ 2008 Express Edition</a>.</p>
<h3>prerequisites</h3>
<p>I assume you already have devkitARM and Visual Studio 2008 installed and both function correctly. I further assume you have a project that you want to migrate from e.g. VisualHAM, Programmers Notepad, etc&#8230; to Visual Studio, so I use the <em>hello world</em> example from libnds as existing project reference, which is located in <em>devkitPro\examples\nds\hello_world</em>.</p>
<h3>create new visual studio project</h3>
<p>We need to create an empty makefile project. You can find this in Visual Studio 2008 under <em>Main menu -> New -> Project</em>:</p>
<p><a href="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_new_project.png"><img src="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_new_project-300x252.png" alt="Visual Studio 2008 - Showing the path to create a new project" title="Visual Studio 2008 - Showing the path to create a new project" width="300" height="252" class="alignnone size-medium wp-image-717" /></a></p>
<p>Select the project template and target location:</p>
<p><a href="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_new_project_dialog.png"><img src="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_new_project_dialog-300x201.png" alt="Visual Studio 2008 - Create new project dialog" title="Visual Studio 2008 - Create new project dialog" width="300" height="201" class="alignnone size-medium wp-image-718" /></a></p>
<p>Rather than selecting the <em>hello_world</em> directory, we specify its parent, because Visual Studio creates a sub directory in the specified location with the specified name. This configuration will save the project file directly in the existing <em>hello_world</em> directory.</p>
<p>Click OK to continue.</p>
<h3>configure new project</h3>
<p>A new dialog pops up, the <em>New Project Wizard</em>:</p>
<p><a href="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_new_project_wizard1.png"><img src="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_new_project_wizard1-300x253.png" alt="Visual Studio 2008 - Create new project wizard" title="Visual Studio 2008 - Create new project wizard" width="300" height="253" class="alignnone size-medium wp-image-719" /></a></p>
<p>Just click <em>Next</em> here to move on to the next page.</p>
<p>The following page looks more like fun! Settings specified in this <em>New Project Wizard</em> can be changed at any time in the <em>Project Settings</em> as well:</p>
<p><a href="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_new_project_wizard2.png"><img src="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_new_project_wizard2-300x253.png" alt="Visual Studio 2008 - Create new project wizard" title="Visual Studio 2008 - Create new project wizard" width="300" height="253" class="alignnone size-medium wp-image-720" /></a></p>
<p>Please note that none of these settings have influence on the build process: <em>Output, Preprocessor definitions, Include search path, Forced include files, CMR assembly search path and Forced using .NET assemblies</em>.</p>
<p>We set <em>Preprocessor definitions</em> so Visual Studio knows which ARM9/ARM7 #if&#8217;s to syntax highlight differently. This is <strong>not</strong> passed to devkitARM.</p>
<p>We set <em>Include search path</em> so Visual Studio knows where to look for the libnds headers to provide code completion and intellisense, it will <strong>not</strong> add it to the search path used by devkitARM.</p>
<p>Here is an example what code completion and intellisense look like, first code completion:</p>
<p><a href="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_codecompletion.png"><img src="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_codecompletion-300x252.png" alt="Visual Studio 2008 - Code completion (with Visual AssistX add-in activated)" title="Visual Studio 2008 - Code completion (with Visual AssistX add-in activated)" width="300" height="252" class="alignnone size-medium wp-image-738" /></a></p>
<p>Intellisense:<br />
<a href="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_intellisense.png"><img src="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_intellisense-300x252.png" alt="Visual Studio 2008 - Showing intellisense (with Visual AssistX add-in activated)" title="Visual Studio 2008 - Showing intellisense (with Visual AssistX add-in activated)" width="300" height="252" class="alignnone size-medium wp-image-739" /></a></p>
<p>The remaining commands in the <em>New Project Wizard</em> are what you typically use when working via the command prompt or shell. Now click <em>Finish</em> to complete the Wizard.</p>
<p>The <em>New Project Wizard</em> closes and Visual Studio shows an empty workspace now:</p>
<p><a href="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_empty_project.png"><img src="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_empty_project-300x252.png" alt="Visual Studio 2008 - Showing an empty project" title="Visual Studio 2008 - Showing an empty project" width="300" height="252" class="alignnone size-medium wp-image-716" /></a></p>
<h3>add files to project</h3>
<p>It&#8217;s essential to know that Visual Studio will just invoke the commands specified in the <em>New Project Wizard</em> to build the project.</p>
<p><strong>Compiling and linking your Nintendo DS application is entirely handled by devkitARM, NOT Visual Studio!</p>
<p></strong> Visual Studio is only the graphical front-end that triggers these devkitARM build processes and serves as code editor.</p>
<p>The devkitPro makefile system has a feature to add directories that are supposed to contain source code to the so called <em>SOURCES</em> variable. All source files in those directories, added to this variables, are being compiled and considered during the link process.</p>
<p>Copied from <em>devkitPro\examples\nds\hello_world\Makefile</em>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files &amp; intermediate files will be placed
# SOURCES is a list of directories containing source code
# INCLUDES is a list of directories containing extra header files
#---------------------------------------------------------------------------------
TARGET		:=	$(shell basename $(CURDIR))
BUILD		:=	build
SOURCES		:=	gfx source data  
INCLUDES	:=	include build</pre></td></tr></table></div>

<p>This means that source files don&#8217;t get compiled when you add them to the Visual Studio project, but when you store them in the <em>source</em> directory of your file system!</p>
<h3>add files to project, really</h3>
<p>Now that you know your files don&#8217;t get compiled when you only add them to the <em>Visual Studio Project</em>, let&#8217;s really add them. You can do this via drag&#038;drop operations from the Windows Explorer or via the <em>Visual Studio Project</em> context menu:</p>
<p><a href="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_add_existing_item.png"><img src="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_add_existing_item-300x252.png" alt="Visual Studio 2008 - Menu path to add an existing item to a project file" title="Visual Studio 2008 - Menu path to add an existing item to a project file" width="300" height="252" class="alignnone size-medium wp-image-722" /></a></p>
<p>A new &#8220;File Open&#8221; dialog appears, which you have to navigate to your source files, select them and click <em>Add</em>. I recommend you add the <em>makefile</em> too.</p>
<p>Once done, your <em>Visual Studio Project</em> should look similar to this:</p>
<p><a href="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_code_editor.png"><img src="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_code_editor-300x252.png" alt="Visual Studio 2008 - Editing a .cpp file" title="Visual Studio 2008 - Editing a .cpp file" width="300" height="252" class="alignnone size-medium wp-image-723" /></a></p>
<h3>compile project</h3>
<p>The magic moment has arrived, to compile the project you can use the <em>Build</em> sub-menu from the mainbar or hit CTRL+SHIFT+B:</p>
<p><a href="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_build_popup.png"><img src="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_build_popup-300x252.png" alt="Visual Studio 2008 - Showing the Build sub-menu in the mainbar" title="Visual Studio 2008 - Showing the Build sub-menu in the mainbar" width="300" height="252" class="alignnone size-medium wp-image-725" /></a></p>
<p>When everything is configured properly, the project should build just fine and the output looks like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">1&gt;------ Build started: Project: hello_world, Configuration: Debug Win32 ------
1&gt;Performing Makefile project actions
1&gt;main.cpp
1&gt;arm-eabi-g++ -MMD -MP -MF /c/devkitPro/examples/nds/hello_world/build/main.d -g -Wall -O2 -march=armv5te -mtune=arm946e-s -fomit-frame-pointer -ffast-math -mthumb -mthumb-interwork -I/c/devkitPro/examples/nds/hello_world/include -I/c/devkitPro/examples/nds/hello_world/build -I/c/devkitPro/libnds/include -I/c/devkitPro/libnds/include -I/c/devkitPro/examples/nds/hello_world/build -DARM9 -fno-rtti -fno-exceptions -c /c/devkitPro/examples/nds/hello_world/source/main.cpp -o main.o
1&gt;linking hello_world.elf
1&gt;built ... hello_world.arm9
1&gt;Nintendo DS rom tool 1.41 - May  1 2009
1&gt;by Rafael Vuijk, Dave Murphy, Alexei Karpenko
1&gt;built ... hello_world.nds
1&gt;Build log was saved at &quot;file://c:\devkitPro\examples\nds\hello_world\Debug\BuildLog.htm&quot;
1&gt;hello_world - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========</pre></td></tr></table></div>

<h3>compile project, the Vista barrier</h3>
<p>I use Microsoft Vista and have to run Visual Studio 2008 as administrator, otherwise it outputs this error:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">1&gt;------ Build started: Project: hello_world, Configuration: Debug Win32 ------
1&gt;Performing Makefile project actions
1&gt;      0 [main] us 0 init_cheap: VirtualAlloc pointer is null, Win32 error 487
1&gt;AllocationBase 0x0, BaseAddress 0x71110000, RegionSize 0x2C0000, State 0x10000
1&gt;c:\devkitPro\msys\bin\make.exe: *** Couldn't reserve space for cygwin's heap, Win32 error 0
1&gt;Project : error PRJ0019: A tool returned an error code from &quot;Performing Makefile project actions&quot;
1&gt;Build log was saved at &quot;file://c:\devkitPro\examples\nds\hello_world\Debug\BuildLog.htm&quot;
1&gt;hello_world - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========</pre></td></tr></table></div>

<p>If you encounter the same problem, see if it fixes the problem when you start Visual Studio 2008 with administrator privileges. You can do this when you right-click the Visual Studio 2008 startmenu entry and select &#8220;Run as administrator&#8221;.</p>
<h3>add a rebuild target</h3>
<p>We specified &#8220;make rebuild&#8221; in the <em>New Project Wizard</em>, but this target does not exist by default, so we have to create it first. Basically it should perform:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">make clean
make</pre></td></tr></table></div>

<p>Open the <em>makefile</em> and add this below <em>clean</em>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">rebuild: clean $(BUILD)</pre></td></tr></table></div>

<p><a href="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_makefile_rebuild.png"><img src="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_makefile_rebuild-300x252.png" alt="Visual Studio 2008 - Adding a &#039;rebuild&#039; target to makefile" title="Visual Studio 2008 - Adding a &#039;rebuild&#039; target to makefile" width="300" height="252" class="alignnone size-medium wp-image-729" /></a></p>
<p>Select <em>Build -> Rebuild Solution</em> and see the output if it&#8217;s performing correctly.</p>
<h3>add run target</h3>
<p>So far we were able to compile, clean and rebuild the Nintendo DS project out of Visual Studio, but how to run it? We use a software emulator! I use <a href="http://nocash.emubase.de/gbapics.htm">no$gba</a> to run my Game Boy Advance and Nintendo DS applications, so I want Visual Studio to start no$gba as well!</p>
<p>We do this like we did with <em>rebuild</em>, we add a new target to <em>makefile</em> which is responsible to start and pass the filename to the Nintendo DS emulator, in my case no$gba.</p>
<p>Put this below our earlier added <em>rebuild</em> target:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">run: $(BUILD)
	$(DEVKITPRO)/utilities/nogba/nogba.exe $(TARGET).nds</pre></td></tr></table></div>

<p>The tab at the beginning at the 2nd line is important and must not be removed, you also want to modify the path to where the emulator of your choice is located.</p>
<p><a href="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_makefile_run.png"><img src="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_makefile_run-300x252.png" alt="Visual Studio 2008 - Adding a target to run the output in an emulator" title="Visual Studio 2008 - Adding a target to run the output in an emulator" width="300" height="252" class="alignnone size-medium wp-image-734" /></a></p>
<p>Now open the <em>Visual Studio Project Properties</em>:</p>
<p><a href="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_project_popup.png"><img src="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_project_popup-300x252.png" alt="Visual Studio 2008 - Project context menu" title="Visual Studio 2008 - Project context menu" width="300" height="252" class="alignnone size-medium wp-image-727" /></a></p>
<p>Select <em>Debugging</em> in the following dialog and fill out the <em>Command</em> and <em>Command Argument</em> text boxes:</p>
<p><a href="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_project_debugging_proper.png"><img src="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_project_debugging_proper-300x209.png" alt="Visual Studio 2008 - Project debugging properties" title="Visual Studio 2008 - Project debugging properties" width="300" height="209" class="alignnone size-medium wp-image-736" /></a></p>
<p>Confirm the dialog with OK and select <em>Debug -> Start Debugging</em>, the project should build and start in the specified emulator!</p>
<h3>Visual Studio compatible build messages</h3>
<p>All build messages are redirected to the Visual Studio output &#8220;Build&#8221; pane. Warnings and errors include the filename and line number, but those are in the GCC format and when you double click them, Visual Studio is smart enough to open this file, but not to go to the line.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">1&gt;c:/devkitPro/examples/nds/hello_world/source/main.cpp:17: error: expected constructor, destructor, or type conversion before 'void'</pre></td></tr></table></div>

<p>Double clicking this line opens main.cpp, but keeps the cursor at line 1.</p>
<p>Fortunalety, <a href="http://www.coranac.com">Jasper &#8216;cearn&#8217; Vijn</a> once had a regular expression on his website that transforms GCC messages in to Visual Studio format and I was clever enough to make a backup before the information went offline:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">'s/\(\w\+\):\([0-9]\+\):/\1(\2):/'</pre></td></tr></table></div>

<p>We can redirect the build output to <a href="http://en.wikipedia.org/wiki/Sed">sed.exe (stream editor)</a>, which uses this regular expresssion to transform and output the text so Visual Studio can handle it.</p>
<p>We do this by extending the commands earlier specified in the <em>New Project Wizard</em>.</p>
<p><a href="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_project_popup.png"><img src="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_project_popup-300x252.png" alt="Visual Studio 2008 - Project context menu" title="Visual Studio 2008 - Project context menu" width="300" height="252" class="alignnone size-medium wp-image-727" /></a></p>
<p>In the following dialog select <em>NMake</em> and modify the settings as shown below:</p>
<p><a href="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_project_nmake_properties.png"><img src="http://www.console-dev.de/wordpress/wp-content/uploads/2009/09/vs_project_nmake_properties-300x209.png" alt="Visual Studio 2008 - Makefile Project properties" title="Visual Studio 2008 - Makefile Project properties" width="300" height="209" class="alignnone size-medium wp-image-728" /></a></p>
<p>Build Command line:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">make 2&gt;&amp;1 | sed -e 's/\(\w\+\):\([0-9]\+\):/\1(\2):/'</pre></td></tr></table></div>

<p>Rebuild All Command line:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">make rebuild 2&gt;&amp;1 | sed -e 's/\(\w\+\):\([0-9]\+\):/\1(\2):/'</pre></td></tr></table></div>

<p>If you have never seen this <strong>2>&#038;1</strong> before, it&#8217;s called <a href="http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true">command redirection operator</a> and a feature of Microsoft Windows. <strong>>&#038;</strong> writes the output from one handle to the input of another handle. Next comes <strong>|</strong>, the pipe operator. It takes the output of one command and directs it into the input of another command.</p>
<p>Confirm the Project Properties Dialog with OK, add an invalid line of code to one source file and build. The error message should look like this now and is clickable:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">1&gt;c:/devkitPro/examples/nds/hello_world/source/main.cpp(17): error: expected constructor, destructor, or type conversion before 'void'</pre></td></tr></table></div>

<h3>we are done</h3>
<p>Happy compiling!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.console-dev.de/2009/09/03/create-nintendo-ds-applications-with-visual-studio-2008/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>callstack trace on the nintendo ds</title>
		<link>http://www.console-dev.de/2009/08/16/callstack-trace-on-the-nintendo-ds/</link>
		<comments>http://www.console-dev.de/2009/08/16/callstack-trace-on-the-nintendo-ds/#comments</comments>
		<pubDate>Sat, 15 Aug 2009 23:00:38 +0000</pubDate>
		<dc:creator>Peter Schraut</dc:creator>
				<category><![CDATA[GBA]]></category>
		<category><![CDATA[NDS]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.console-dev.de/?p=675</guid>
		<description><![CDATA[introduction
I was experimenting how I could retrieve a stack trace on the Nintendo DS, using the devkitARM compiler, recently. A callstack is a list of active subroutines of a computer program. This can be used to identify from which code-path a particular subroutine was called, which is enormously helpful when detecting logical errors at run-time.
I [...]]]></description>
			<content:encoded><![CDATA[<h3>introduction</h3>
<p>I was experimenting how I could retrieve a <a href="http://en.wikipedia.org/wiki/Backtrace">stack trace</a> on the Nintendo DS, using the <a href="http://www.devkitpro.org">devkitARM</a> compiler, recently. A callstack is a list of active subroutines of a computer program. This can be used to identify from which code-path a particular subroutine was called, which is enormously helpful when detecting logical errors at run-time.</p>
<p>I tend to flood my code with &#8220;ASSERT&#8217;s&#8221;. ASSERT is used to identify logic errors during program development by implementing the expression argument to evaluate to FALSE only when the program is operating incorrectly and then calling a function to report the error:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> Vector2Add<span style="color: #008000;">&#40;</span>Vector2 <span style="color: #000040;">*</span>dest, <span style="color: #0000ff;">const</span> Vector2 <span style="color: #000040;">*</span>v0, <span style="color: #0000ff;">const</span> Vector2 <span style="color: #000040;">*</span>v1<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  ASSERT<span style="color: #008000;">&#40;</span>dest <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  ASSERT<span style="color: #008000;">&#40;</span>v0 <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  ASSERT<span style="color: #008000;">&#40;</span>v1 <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  dest<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>x <span style="color: #000080;">=</span> v0<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>x <span style="color: #000040;">+</span> v1<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>x<span style="color: #008080;">;</span>
  dest<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>y <span style="color: #000080;">=</span> v0<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>y <span style="color: #000040;">+</span> v1<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>y<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>When any of the incoming arguments point to NULL, ASSERT will call a subroutine to report the error. Typically the report includes the filename, line number and functionname. This information is helpful, but lacks of information from where the function was called.</p>
<p>I use Vector2Add all over the place, how should I know what part in my program code does not work, when I only know at some point a NULL argument is passed to Vector2Add?</p>
<p>Right, I need to know from where Vector2Add was called, I need a callstack trace!</p>
<h3>my earlier instrumentation approach</h3>
<p>Around 2005-2006 I implemented a <a href="http://www.console-dev.de/hel/docs/group__helDebugAssert.html">callstack trace feature in HEL Library</a>. HEL Library is a middleware solution to ease development of Game Boy Advance titles.</p>
<p>The callstack trace was displayed together with an assertion-report, that looked like:<br />
<img src="http://www.console-dev.de/hel/docs/assert_01.png" alt="HEL Library assertion report" /></p>
<p><img src="http://www.console-dev.de/hel/docs/assert_02.png" alt="HEL Library callstack trace" /></p>
<p>Back then I used function instrumentation (<a href="http://gcc.gnu.org/onlinedocs/gcc-4.3.3/gcc/Code-Gen-Options.html">-finstrument-functions</a>) to build a list of subroutine calls at run-time. It was basically a callback that was called for every function that is about to be entered (__cyg_profile_func_enter) and left (__cyg_profile_func_exit).</p>
<p>This was a huge overhead and slowed down program-execution dramatically, but the callstack trace was so precious, that the performance penalty didn&#8217;t matter for the <a href="http://www.console-dev.de/hel/docs/PageLibraries.html#SectionLibhel2_dcs">debug library</a>.</p>
<h3>fail does not mean to stop</h3>
<p>When I started the Nintendo DS version, one goal was to use an approach which does not have any impact on run-time performance! I have tried several libraries, unfortunalety I didn&#8217;t find anything that worked for me.</p>
<p>This includes <a href="http://www.nongnu.org/libunwind/">libunwind</a>, <a href="http://www.gnu.org/s/libc/manual/html_node/Backtraces.html">backtrace</a>, several GCC builtin-keywords to get return addresses of different calldepths, just to name a few.</p>
<p>I gave all up. It was either a never ending story to integrate the library in to the project or headers / functions were missing in libraries that come with devkitARM.</p>
<h3>hacking to success, then fail</h3>
<p>Rather than giving up, I decided to accept it as challenge and come up with my own callstack trace code. It was a long and rocky journey, but at the end I had something that worked suprisingly well.</p>
<p>What I do is basically pretty simple, I interpret instructions of interest to simulate the beaviour of the <a href="http://en.wikipedia.org/wiki/Program_counter">Program Counter (PC)</a> and <a href="http://en.wikipedia.org/wiki/Stack_pointer">Stack Pointer (SP)</a>.  The Program Counter indicates where the program is in its intruction sequence. Stack Pointer indicates the current top of stack memory. The stack this is where local variables are located.</p>
<p>Armed with my own Program Counter and Stack Pointer variables, I can traverse program code and react when Program Counter is assigned a new value, which basically means to jump to a different address and continue to operate there.</p>
<p>Example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> FirstFunc<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  SecondFunc<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> SecondFunc<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">// Program Counter is located here  &lt;--</span>
  ThirdFunc<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Let&#8217;s presume the Program Counter is located in SecondFunc, which was previously called by FirstFunc.</p>
<p>The thumb assembler version of this looks like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="asm" style="font-family:monospace;">FirstFunc<span style="color: #339933;">:</span>
  <span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #009900; font-weight: bold;">&#123;</span>lr<span style="color: #009900; font-weight: bold;">&#125;</span>       <span style="color: #666666; font-style: italic;">; Push Link Register (LR) on stack</span>
  <span style="color: #00007f; font-weight: bold;">sub</span> <span style="color: #00007f;">sp</span><span style="color: #339933;">,</span> <span style="color: #00007f;">sp</span><span style="color: #339933;">,</span> #<span style="color: #0000ff;">4</span>  <span style="color: #666666; font-style: italic;">; Update Stack Pointer by 4bytes because LR was pushed</span>
  <span style="color: #00007f;">bl</span>  SecondFunc  <span style="color: #666666; font-style: italic;">; Call to SecondFunc()</span>
  <span style="color: #00007f; font-weight: bold;">add</span> <span style="color: #00007f;">sp</span><span style="color: #339933;">,</span> <span style="color: #00007f;">sp</span><span style="color: #339933;">,</span> #<span style="color: #0000ff;">4</span>  <span style="color: #666666; font-style: italic;">; Update Stack Pointer to point to pushed LR again</span>
  <span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #009900; font-weight: bold;">&#123;</span>pc<span style="color: #009900; font-weight: bold;">&#125;</span>        <span style="color: #666666; font-style: italic;">; Pop LR from stack and store in Program Counter, this will return to caller</span>
&nbsp;
SecondFunc<span style="color: #339933;">:</span>
  <span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #009900; font-weight: bold;">&#123;</span>lr<span style="color: #009900; font-weight: bold;">&#125;</span>       <span style="color: #666666; font-style: italic;">; Push Link Register (LR) on stack</span>
  <span style="color: #00007f; font-weight: bold;">sub</span> <span style="color: #00007f;">sp</span><span style="color: #339933;">,</span> <span style="color: #00007f;">sp</span><span style="color: #339933;">,</span> #<span style="color: #0000ff;">4</span>  <span style="color: #666666; font-style: italic;">; Update Stack Pointer by 4bytes because LR was pushed</span>
  <span style="color: #00007f;">bl</span>  ThirdFunc   <span style="color: #666666; font-style: italic;">; Call to ThirdFunc()</span>
  <span style="color: #00007f; font-weight: bold;">add</span> <span style="color: #00007f;">sp</span><span style="color: #339933;">,</span> <span style="color: #00007f;">sp</span><span style="color: #339933;">,</span> #<span style="color: #0000ff;">4</span>  <span style="color: #666666; font-style: italic;">; Update Stack Pointer to point to pushed LR again</span>
  <span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #009900; font-weight: bold;">&#123;</span>pc<span style="color: #009900; font-weight: bold;">&#125;</span>        <span style="color: #666666; font-style: italic;">; Pop LR from stack and store in Program Counter, this will return to caller</span></pre></td></tr></table></div>

<p>I need to get my hands on the <a href="http://en.wikipedia.org/wiki/Link_register">Link Register (LR)</a> to return to SecondFunc&#8217;s caller (*1). Since LR was being pushed on the stack, I know where to look! What is left is to traverse the program code in reversed order and interpret the &#8220;sub&#8221; and &#8220;push&#8221; instructions.</p>
<p>Moving the Program Counter in reversed order, which actually would be a backtrace imo, didn&#8217;t work out for some reasons and I wasn&#8217;t able to solve those problems. It did, however, worked in some cases, unfortunalety too unreliable.</p>
<h3>when in doubt, try it out</h3>
<p>I stayed away from this problem for week and then had the idea why not trying to simulate the Program Counter the same way it would walk along when it continues program execution normally. It means rather then traversing program code backwards, I tried what happens, when I forward advance PC. Now I have to interpret &#8220;add&#8221; and &#8220;pop&#8221; instructions and this works suprisingly well!</p>
<p>From here on it took a few hours to come up with a version that is robust enough to work in all places that I tested with my code base, which consists of both, C and C++ source code.</p>
<p>Obviously it does not mean it always works in every sitiuation with all source code in the world. For example, optimization level -O0 generates so many unconditional branches and places return code all over the place, that the <em>Stacktrace</em> function fails. It supports thumb code only and has problens with hand written/optimized assembler routines (when LR is not push&#8217;ed and PC not pop&#8217;ed).</p>
<p>I added a couple of more instruction <em>Stacktrace</em> looks out for:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="asm" style="font-family:monospace;"><span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #009900; font-weight: bold;">&#123;</span><span style="color: #339933;">...</span>pc<span style="color: #009900; font-weight: bold;">&#125;</span> <span style="color: #666666; font-style: italic;">; adjust PC to the pop'ed value</span>
<span style="color: #00007f; font-weight: bold;">add</span> <span style="color: #00007f;">sp</span><span style="color: #339933;">,</span> nn  <span style="color: #666666; font-style: italic;">; adjust SP by nn</span>
<span style="color: #00007f; font-weight: bold;">sub</span> <span style="color: #00007f;">sp</span><span style="color: #339933;">,</span> nn  <span style="color: #666666; font-style: italic;">; adjust SP by nn</span>
b   ofs     <span style="color: #666666; font-style: italic;">; set PC to the addr PC+ofs</span>
<span style="color: #00007f; font-weight: bold;">add</span> <span style="color: #00007f;">sp</span><span style="color: #339933;">,</span> rn  <span style="color: #666666; font-style: italic;">;  adjust SP by the value rn points to</span></pre></td></tr></table></div>

<p>The unconditional branch instruction &#8220;b&#8221; is from hell, because it could lead to an infinite loop in <em>Stacktrace</em>, such as <em>while(1) {}</em>. For this reason I added two special cases:</p>
<ul>
<li>Limit how many instructions are allowed to touch</li>
<li>Branch only to the target address when it&#8217;s different from PC</li>
</ul>
<p>When any of the conditions evaluates to true, <em>Stacktrace</em> aborts and outputs the callstack only to this position. It will, however, report that an error occured.</p>
<p>Support for the unconditional branch was important, because the compiler generates code like this sometimes:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="asm" style="font-family:monospace;">ExampleFunc<span style="color: #339933;">:</span>
  <span style="color: #00007f; font-weight: bold;">push</span>   <span style="color: #009900; font-weight: bold;">&#123;</span>lr<span style="color: #009900; font-weight: bold;">&#125;</span>
  <span style="color: #339933;">...</span>
  tst    r0<span style="color: #339933;">,</span> #<span style="color: #0000ff;">2</span>
  bne    <span style="color: #339933;">.</span>L92 <span style="color: #666666; font-style: italic;">; if r0 is not 2 then jump to L92</span>
  <span style="color: #339933;">...</span>
<span style="color: #339933;">.</span>L90<span style="color: #339933;">:</span>
  <span style="color: #00007f; font-weight: bold;">pop</span>    <span style="color: #009900; font-weight: bold;">&#123;</span>pc<span style="color: #009900; font-weight: bold;">&#125;</span> <span style="color: #666666; font-style: italic;">; returns to caller</span>
&nbsp;
<span style="color: #339933;">.</span>L92<span style="color: #339933;">:</span>
  <span style="color: #00007f;">bl</span>     AnotherFunction
  b      <span style="color: #339933;">.</span>L90 <span style="color: #666666; font-style: italic;">; jump to L90 (above)</span></pre></td></tr></table></div>

<p>When we return from <em>AnotherFunction</em> we must jump to <em>L90</em> to pop the return address from the stack and return to the caller. This is the reason why I added support for unconditional branches. I haven&#8217;t noticed other branches that are of importance for <em>Stacktrace</em> yet.</p>
<p>At this point of time I was able to return to each function-caller and have the return addresses, e.g:</p>
<p><img src="http://www.console-dev.de/wordpress/wp-content/uploads/2009/08/stacktrace1.png" alt="Stacktrace output with return addresses only" title="Stacktrace output with return addresses only" width="286" height="443" class="alignnone size-full wp-image-692" /></p>
<h3>resolving function names by hand</h3>
<p>In order to map the memory address to an actual function, we have to look up the address in the so called Map file. Map files are typically plain text files that indicate the relative offsets of functions for a given version of a compiled binary. This file is generated by the <a href="http://en.wikipedia.org/wiki/Linker">linker</a> when you pass <em>-Map</em> to it, here is a part of it:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">.text   0x02000310    0xdf28
.text   0x02000380    0x1ac main.o
        0x02000394      PrintStacktrace
        0x0200046c      FirstFunc
        0x0200040c      ThirdFunc
        0x020004c8      main
        0x02000450      SecondFunc</pre></td></tr></table></div>

<p>Line 1 specifies where the .text section (program code) starts and its size. Line 2 specifies the start address of the <a href="http://en.wikipedia.org/wiki/OBJ_file">object file</a> <em>main.o</em> followed by all functions it contains. While writing this article, I have learned <em>static</em> functions are not listed in the map file.</p>
<p>In order to find the function name of the memory address <em>0&#215;02000458</em> (from screenshot above), we have to check in which range it is located. But it would be too simple when the .map file has everything sorted by addresses, so we have to do this instead.</p>
<p>Here is a sorted list by addresses:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">.text   0x02000380    0x1ac main.o
        0x02000394      PrintStacktrace
        0x0200040c      ThirdFunc
        0x02000450      SecondFunc
        0x0200046c      FirstFunc
        0x020004c8      main</pre></td></tr></table></div>

<p>The memory address <em>0&#215;02000458</em> is between:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="txt" style="font-family:monospace;">        0x02000450      SecondFunc
        0x0200046c      FirstFunc</pre></td></tr></table></div>

<p>So <em>0&#215;02000458</em> belongs to <em>SecondFunc</em>. While this approach seems to work, it is complicated and really only works when you have the exact .map file available that was used to build the project. You can&#8217;t use a .map file of a different version of your code, this is very unlikely to work.</p>
<p>It would be so much better when the actual function name could be displayed instead!</p>
<h3>what is your function name, dear memory address</h3>
<p>Fortunalety, the compiler has an option to insert function names in plain-text infront of each function. This can be activated by adding <em>-mpoke-function-name</em> to the <em>CFLAGS</em> variable in your makefile.</p>
<p>But it not only inserts the name, it also inserts a bit-pattern to recognize that there is a function name as well as a relative offset to the name from this pattern.</p>
<p>What <em>Stacktrace</em> function does, it takes a memory address and then decreases this address until it detects the bit-pattern of the function name and we have it!</p>
<p><img src="http://www.console-dev.de/wordpress/wp-content/uploads/2009/08/stacktrace2.png" alt="Stacktrace without functio names and return addresses" title="Stacktrace without functio names and return addresses" width="286" height="443" class="alignnone size-full wp-image-695" /></p>
<p>Well, there is of course a special case again. When <em>-mpoke-function-name</em> has not been specified, the function name bit-pattern is missing, thus <em>Stacktrace</em> would try to find it until it reaches the start of the text section. While this makes little sense, I added a limit how many 32bit words the &#8220;GetFunctionnameTag&#8221; function is allowed to touch before it returns &#8220;did not find&#8221;.</p>
<h3>how to integrate in your own project</h3>
<p>The stacktrace.zip archive contains the entire source code, you only need those two files:</p>
<ul>
<li>stacktrace.h</li>
<li>stacktrace.c</li>
</ul>
<p>Copy them to your source code directory. Then open the makefile file which should be located in your project directory also. You have to adjust the following variables:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">ARCH   <span style="color: #008080;">:</span><span style="color: #000080;">=</span> <span style="color: #000040;">-</span>mthumb <span style="color: #000040;">-</span>mthumb<span style="color: #000040;">-</span>interwork
CFLAGS <span style="color: #008080;">:</span><span style="color: #000080;">=</span> <span style="color: #000040;">-</span>O2 <span style="color: #000040;">-</span>mpoke<span style="color: #000040;">-</span>function<span style="color: #000040;">-</span>name</pre></td></tr></table></div>

<p>Now do a rebuild, rather than a regular build. You can do this by performing a “make clean” first, then a “make”. Don’t forget to include <em>stacktrace.h</em> in that file where you want to use <em>Stacktrace</em>.</p>
<p>Optimization level -O2 worked best with <em>Stacktrace</em> so far. When you use more aggressive optimization levels the compiler also starts to inline code heavily, which can be quite confusing when the <em>Stacktrace</em> output does not reflect what you see in your high level code.</p>
<h3>stacktrace interface and usage</h3>
<p>The usage of <em>Stacktrace</em> function is fairly simple.</p>
<p>All it expects is an array of <em>StacktraceEntry</em> elements and the count of it. Here is the interface from stacktrace.h:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">struct</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> addr<span style="color: #008080;">;</span>  <span style="color: #666666;">//!&lt; Return address</span>
  <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>name<span style="color: #008080;">;</span>   <span style="color: #666666;">//!&lt; Name of function</span>
<span style="color: #008000;">&#125;</span> StacktraceEntry<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> Stacktrace<span style="color: #008000;">&#40;</span>StacktraceEntry<span style="color: #000040;">*</span> dest, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> count<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Use it like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> OutputStacktrace<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> n<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> count<span style="color: #008080;">;</span>
  StacktraceEntry entries<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">32</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// allocate memory for 32 stacktrace entries</span>
&nbsp;
  <span style="color: #666666;">// get only the 10 deepest stacktrace entries</span>
  count <span style="color: #000080;">=</span> Stacktrace<span style="color: #008000;">&#40;</span>entries, <span style="color: #0000dd;">10</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>n<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> n<span style="color: #000080;">&lt;</span>count<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>n<span style="color: #008000;">&#41;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;0x%08x: %s&quot;</span>, entries<span style="color: #008000;">&#91;</span>n<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">addr</span>, entries<span style="color: #008000;">&#91;</span>n<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">name</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<h3>improving stacktrace</h3>
<p>If you want to improve <em>Stacktrace</em>, I recommend to read through the comments in <em>stacktrace.c</em> and enable <em>STACKTRACE_VERBOSE</em>, by setting it to 1.</p>
<p>This will, as far as <em>OutputDebugString</em> (also in stacktrace.c) uses a debug message type your Nintendo DS software emulator understands, output what instructions <em>Stacktrace</em> comes across and interprets.</p>
<p>I&#8217;ve added an opcode structure for all instructions <em>Stacktrace</em> interprets as well as print-routines to dump the contents.</p>
<p>I have used <a href="http://nocash.emubase.de/gba.htm">no$gba</a> during development, which was of great help because of its &#8220;<a href="http://nocash.emubase.de/gbapics.htm">debugger</a>&#8220;. Usually I yell about only see the assembler code in the shareware version of no$gba, but for this project it was exactly what I needed <img src='http://www.console-dev.de/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>conclusion</h3>
<p>The <em>Stacktrace</em> function&#8230;</p>
<ul>
<li>works suprisingly well for being such a hack</li>
<li>does not need debugging information/symbols</li>
<li>it runs directly on the target device, not needed to be connected to GDB or a similar debugger</li>
<li>does not come with any run-time performance penalty</li>
<li>works best with optimization level -O2</li>
<li>is most informative when adding -mpoke-function-name to CFLAGS in your makefile</li>
<li>comes with full documented source code</li>
<li>uses very little Nintendo DS specifics, except the text section start and length (portable to other ARM devices?)</li>
<li>works with thumb code only</li>
<li>does not detect when switching from thumb to arm mode</li>
<li>depends on LR / PC being push&#8217;ed and pop&#8217;ed, hand-written assembler routine can cause trouble</li>
<li>is tested in/with my code base only</li>
</ul>
<h3>frequently asked questions (faq)</h3>
<p><i>Q</i>: Why do I see so weird function names, like _<em>ZN13DEMOPARTQUEUE9CutToPartEj</em> and the like?</p>
<p><i>A</i>: Because in C++, the function names go through <a href="http://en.wikipedia.org/wiki/Name_mangling">name mangling</a>. Name mangling is a technique used to solve various problems caused by the need to resolve unique names for programming entities.</p>
<h3>download</h3>
<p><a href="http://www.console-dev.de/news/2009/stacktrace.zip">Dear visiter, 100% OFF on console-dev.de, take your chance and download stacktrace.zip now!</a> <img src='http://www.console-dev.de/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  </p>
<h3>what I didn&#8217;t tell you yet</h3>
<p>*1) LR does not always contains the address from where it was being called, but it holds the address where to return when the function completes. When using code optimization levels above -O2 the compiler e.g. generates code that does not return to functions where it was the last instruction involved anyway.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.console-dev.de/2009/08/16/callstack-trace-on-the-nintendo-ds/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>sine approximation with fixed point math part 2</title>
		<link>http://www.console-dev.de/2009/07/19/sine-approximation-with-fixed-point-math-part-2/</link>
		<comments>http://www.console-dev.de/2009/07/19/sine-approximation-with-fixed-point-math-part-2/#comments</comments>
		<pubDate>Sun, 19 Jul 2009 09:56:34 +0000</pubDate>
		<dc:creator>Peter Schraut</dc:creator>
				<category><![CDATA[NDS]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.console-dev.de/?p=658</guid>
		<description><![CDATA[In my previous post sine approximation with fixed point math I wrote the output precision of f32sin is getting worse when the incoming radians parameter grows and guessed it is due to using fixed point.
Well, Jasper &#8220;cearn&#8221; Vijn suggested to use a higher resolution fixed point format for 1.0f/(2*pi) and this solves the problem indeed! [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous post <a href="http://www.console-dev.de/2009/07/06/sine-approximation-with-fixed-point-math/">sine approximation with fixed point math</a> I wrote the output precision of <em>f32sin</em> is getting worse when the incoming radians parameter grows and guessed it is due to using fixed point.</p>
<p>Well, <a href="http://www.coranac.com">Jasper &#8220;cearn&#8221; Vijn</a> suggested to use a higher resolution fixed point format for 1.0f/(2*pi) and this solves the problem indeed! But it does not end here!</p>
<p>He also created an awesome article about the ins and outs of fixed point sine approximation and presents a couple of highly precise and optimized routines.</p>
<p>Head over to Jasper Vijn&#8217;s document <a href="http://www.coranac.com/2009/07/sines/">Another fast fixed-point sine approximation</a> now!</p>
<p>I&#8217;ll post a corrected version of the <em>f32sin</em> routine from my previous post for completeness, but I highly encourage to vists Jasper&#8217;s site and get one of his!</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Gets sin of specified radians.</span>
<span style="color: #666666;">// Input and output format is fixed point sign.19.12</span>
int32 f32sin<span style="color: #008000;">&#40;</span>int32 radians<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">const</span> int32 PI2_PRECISION_BITS <span style="color: #000080;">=</span> <span style="color: #0000dd;">20</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> int32 PI2_RECIPROCAL     <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color:#800080;">1.0f</span> <span style="color: #000040;">/</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #000040;">*</span>M_PI<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #000080;">&lt;&lt;</span>PI2_PRECISION_BITS<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// Divide incoming radians by 2*PI, so it is a &quot;normalized&quot;</span>
  <span style="color: #666666;">// angle between -1 and +1, where 1 equals 2*PI (360 degree).</span>
  int32 value <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>radians <span style="color: #000040;">*</span> PI2_RECIPROCAL<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&gt;&gt;</span> PI2_PRECISION_BITS<span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// Now that we have our normalized angle,</span>
  <span style="color: #666666;">// we just discard all numbers which are not in the range -1..+1.</span>
  <span style="color: #666666;">// Since this is fixed point, a simple AND operation can be used.</span>
  value <span style="color: #000040;">&amp;</span><span style="color: #000080;">=</span> floattof32<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// Always wrap normalized angle to -0.5(-PI)..+0.5(+PI)</span>
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>value <span style="color: #000080;">&gt;</span> floattof32<span style="color: #008000;">&#40;</span><span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    value <span style="color: #000040;">-</span><span style="color: #000080;">=</span> floattof32<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// subtract 2*PI in normalized form</span>
  <span style="color: #0000ff;">else</span>
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>value <span style="color: #000080;">&lt;</span> floattof32<span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
      value <span style="color: #000040;">+</span><span style="color: #000080;">=</span> floattof32<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// add 2*PI in normalized form</span>
&nbsp;
  <span style="color: #666666;">// Convert normalized angle (-1..1) back to radians (-2*PI..2*PI)</span>
  value <span style="color: #000080;">=</span> f32mul<span style="color: #008000;">&#40;</span>value, floattof32<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #000040;">*</span>M_PI<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// Approximate sin value</span>
  <span style="color: #0000ff;">const</span> int32 B <span style="color: #000080;">=</span> floattof32<span style="color: #008000;">&#40;</span><span style="color:#800080;">1.2732395447351f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> int32 C <span style="color: #000080;">=</span> floattof32<span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color:#800080;">0.405284734569f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">return</span> f32mul<span style="color: #008000;">&#40;</span>B, value<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> f32mul<span style="color: #008000;">&#40;</span>f32mul<span style="color: #008000;">&#40;</span>C, value<span style="color: #008000;">&#41;</span>, f32abs<span style="color: #008000;">&#40;</span>value<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.console-dev.de/2009/07/19/sine-approximation-with-fixed-point-math-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>sine approximation with fixed point math</title>
		<link>http://www.console-dev.de/2009/07/06/sine-approximation-with-fixed-point-math/</link>
		<comments>http://www.console-dev.de/2009/07/06/sine-approximation-with-fixed-point-math/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 18:02:59 +0000</pubDate>
		<dc:creator>Peter Schraut</dc:creator>
				<category><![CDATA[NDS]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.console-dev.de/?p=651</guid>
		<description><![CDATA[When I was writing the Nintendo DS 4k intro, I obviously needed sin and cos functions to set up rotation matrices. Since using the standard C library math module would have blown the 4kb limit to mars, I was looking for an alternative that does not use much memory.
I was searching for an approximation method [...]]]></description>
			<content:encoded><![CDATA[<p>When I was writing the <a href="http://www.console-dev.de/2009/04/25/nintendo-ds-4k-intro/">Nintendo DS 4k intro</a>, I obviously needed <a href="http://www.cplusplus.com/reference/clibrary/cmath/sin/">sin</a> and <a href="http://www.cplusplus.com/reference/clibrary/cmath/cos/">cos</a> functions to set up rotation matrices. Since using the <a href="http://www.cplusplus.com/reference/clibrary/cmath/">standard C library math module</a> would have blown the 4kb limit to mars, I was looking for an alternative that does not use much memory.</p>
<p>I was searching for an approximation method using <a href="http://en.wikipedia.org/wiki/Taylor_series">taylor series</a> and came across the following sites:</p>
<ul>
<li><a href="http://lab.polygonal.de/2007/07/18/fast-and-accurate-sinecosine-approximation/">polygonal labs &#8211; Fast and accurate sine/cosine approximation</a></li>
<li><a href="http://www.devmaster.net/forums/showthread.php?t=5784">devmaster.net &#8211; Fast and accurate sine/cosine</a></li>
</ul>
<p>The only task that remained was porting it from float to fixed point, which is rather trivial, yay!</p>
<p>Most amazing for me is how precise the approximation actually is. Here is a screenshot of the original sinf routine (green) and the approximated one (red) using the source code below:</p>
<p><div id="attachment_653" class="wp-caption alignnone" style="width: 296px"><img class="size-full wp-image-653" title="Sine approximation (sinf vs approximation in fixed point)" src="http://www.console-dev.de/wordpress/wp-content/uploads/2009/07/sin_approximation1.png" alt="Sine approximation" width="286" height="443" /><p class="wp-caption-text">Sine approximation</p></div><br />
Well, when the incoming radians parameter is getting bigger (1000 and above), precision becomes is not so good anymore, but this is rather a fixed point problem and the way I implemented it, I guess.</p>
<p>The routine from polygon labs also can&#8217;t really handle radians greater than 2*PI, which I tried to fix by normalizing it to an angle between -1..+1 and then just use a bit-AND to discard all numbers outside this range. </p>
<p>I wanted to keep the radians format for the incoming parameter, because it&#8217;s how the standard sinf routine works. Using another range, like 0..4096 to represent a full rotation would probably solve that precision problem with large radians because this would remove the need for the first two code lines, but I wanted 2*PI. <img src='http://www.console-dev.de/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Here comes the source code, to be compiled with libnds and devkitARM:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Gets sin of specified radians.</span>
<span style="color: #666666;">// Input and output format is fixed point sign.19.12</span>
int32 f32sin<span style="color: #008000;">&#40;</span>int32 radians<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">// Offset to slightly restore precision when radians is large.</span>
  <span style="color: #0000ff;">const</span> int32 offset<span style="color: #000080;">=</span> f32mul<span style="color: #008000;">&#40;</span>radians, floattof32<span style="color: #008000;">&#40;</span><span style="color:#800080;">0.00147f</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// Divide incoming radians by 2*PI, so it is a &quot;normalized&quot;</span>
  <span style="color: #666666;">// angle between -1 and +1, where 1 equals 2*PI (360 degree).</span>
  int32 value <span style="color: #000080;">=</span> f32mul<span style="color: #008000;">&#40;</span>radians <span style="color: #000040;">+</span> offset, floattof32<span style="color: #008000;">&#40;</span><span style="color:#800080;">1.0f</span> <span style="color: #000040;">/</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #000040;">*</span>M_PI<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// Now that we have our normalized angle,</span>
  <span style="color: #666666;">// we just discard all numbers which are not in the range -1..+1.</span>
  <span style="color: #666666;">// Since this is fixed point, a simple AND operation can be used.</span>
  value <span style="color: #000040;">&amp;</span><span style="color: #000080;">=</span> floattof32<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// Always wrap normalized angle to -0.5(-PI)..+0.5(+PI)</span>
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>value <span style="color: #000080;">&gt;</span> floattof32<span style="color: #008000;">&#40;</span><span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    value <span style="color: #000040;">-</span><span style="color: #000080;">=</span> floattof32<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// subtract 2*PI in normalized form</span>
  <span style="color: #0000ff;">else</span>
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>value <span style="color: #000080;">&lt;</span> floattof32<span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    value <span style="color: #000040;">+</span><span style="color: #000080;">=</span> floattof32<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// add 2*PI in normalized form</span>
&nbsp;
  <span style="color: #666666;">// Convert normalized angle (-1..1) back to radians (-2*PI..2*PI)</span>
  value <span style="color: #000080;">=</span> f32mul<span style="color: #008000;">&#40;</span>value, floattof32<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #000040;">*</span>M_PI<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// Approximate sin value</span>
  <span style="color: #0000ff;">const</span> int32 B <span style="color: #000080;">=</span> floattof32<span style="color: #008000;">&#40;</span><span style="color:#800080;">1.2732395447351f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> int32 C <span style="color: #000080;">=</span> floattof32<span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color:#800080;">0.405284734569f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">return</span> f32mul<span style="color: #008000;">&#40;</span>B, value<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> f32mul<span style="color: #008000;">&#40;</span>f32mul<span style="color: #008000;">&#40;</span>C, value<span style="color: #008000;">&#41;</span>, f32abs<span style="color: #008000;">&#40;</span>value<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">inline</span> int32 f32abs<span style="color: #008000;">&#40;</span>int32 a<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> a <span style="color: #000080;">&gt;=</span> <span style="color: #0000dd;">0</span> <span style="color: #008080;">?</span> a <span style="color: #008080;">:</span> <span style="color: #000040;">-</span>a<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// multiplies two fixed point numbers.</span>
<span style="color: #666666;">// since the result is shifted rather than the two</span>
<span style="color: #666666;">// operands, make sure a and b are small.</span>
<span style="color: #0000ff;">inline</span> int32 f32mul<span style="color: #008000;">&#40;</span>int32 a, int32 b<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span>a<span style="color: #000040;">*</span>b<span style="color: #008000;">&#41;</span><span style="color: #000080;">&gt;&gt;</span><span style="color: #0000dd;">12</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Let me know when you fixed the precision problem with large radian values <img src='http://www.console-dev.de/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.console-dev.de/2009/07/06/sine-approximation-with-fixed-point-math/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Single language vs. multi-language</title>
		<link>http://www.console-dev.de/2009/06/21/single-language-vs-multi-language/</link>
		<comments>http://www.console-dev.de/2009/06/21/single-language-vs-multi-language/#comments</comments>
		<pubDate>Sun, 21 Jun 2009 16:00:17 +0000</pubDate>
		<dc:creator>Peter Schraut</dc:creator>
				<category><![CDATA[GBA]]></category>
		<category><![CDATA[NDS]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.console-dev.de/?p=610</guid>
		<description><![CDATA[Introduction
During my daily forum visits, I came across a thread about how to create a game in more than one language.
This reminded me on the first multi-language game I was working on. We ran into several issues back then and I want to share my experience with you, so you don&#8217;t make the same mistakes.
When [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>During my daily forum visits, I came across a thread about how to create a game in more than one language.</p>
<p>This reminded me on the first multi-language game I was working on. We ran into several issues back then and I want to share my experience with you, so you don&#8217;t make the same mistakes.</p>
<p>When you create a multi-language game, several things change that would be static otherwise. Here is a list a few things to consider:</p>
<ul>
<li>You can&#8217;t hard-code every text, because you&#8217;re usually not the person who translates text.</li>
<li>You can&#8217;t assume texts have same  lengths in different languages.</li>
<li>Prepare your GUI to word-/line-break and squeze text.</li>
<li>Different languages might imply different character ranges (char, wchar, utf-8, choose which one to support before you start)</li>
<li>Different languages might imply different font definitions</li>
<li>Different languages might imply different graphics: title logo, textures with baked-in text, etc</li>
<li>Different languages might imply different sounds: voice output</li>
<li>Being able to change language in the options menu, in case it is required.</li>
<li>Being able to change language at runtime during development.</li>
<li>Testing time increases with more languages</li>
<li>You might need to communicate with people who don&#8217;t speak your language (bug reports etc)</li>
<li>Several persons involved in development have more work to do: programmer, translator, quality assurance, (artist, sound artist maybe)</li>
</ul>
<h3>Localization from a programmers perspective</h3>
<p>If text is available in more than one language, it must not be hard-coded in the game code.</p>
<p>Functionality to retrieve texts, depending on the current language setting is essential. It can be something simple like a function that expects an identifier that represents the text in question and returns its text or a database query.</p>
<p>We decided to edit texts in Microsoft Excel, because it&#8217;s standard in many offices and we don&#8217;t wanted to force translators to use some unknown custom in-house tool that he/she might not understand and we would have to support.</p>
<p>The spreadsheet structure looked like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">ID               EN                  DE            DESC
<span style="color: #000040;">---------------+-------------------+-------------+---------------</span>
helloworld     <span style="color: #000040;">|</span> HELLO WORLD       <span style="color: #000040;">|</span> HALLO WELT  <span style="color: #000040;">|</span> Displayed in the welcome screen
goodday        <span style="color: #000040;">|</span> GOOD DAY          <span style="color: #000040;">|</span> GUTEN TAG   <span style="color: #000040;">|</span> Displayed when the player confirms the welcome screen
...</pre></td></tr></table></div>

<p><em>ID</em> represents the text identifier that is used to query the translated text.</p>
<p><em>EN</em> and <em>DE</em> are the english and german translations.</p>
<p><em>DESC</em> is an optional field that describes the purpose of this text. It can be quite challenging to find a good translation when you don&#8217;t know the context of the text, that&#8217;s what <em>DESC</em> should solve.</p>
<h3>Localization, 1st iteration</h3>
<p>Our first language system iteration worked like this:</p>
<ul>
<li>Create texts in Microsoft Excel and save as XML</li>
<li>Custom tool to convert XML to our text format</li>
<li>Custom tool outputs .cpp file with texts as array</li>
<li>Custom tool outputs .h file with identifier #define&#8217;s that index into the text arrays</li>
</ul>
<p>In order to get the translated text, we had a function that expects one of the generated identifiers and returned the corresponding text. The text system looked like this:</p>
<p>language.h file:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#ifndef __language_h__</span>
<span style="color: #339900;">#define __language_h__</span>
&nbsp;
<span style="color: #339900;">#define TID_helloworld  0</span>
<span style="color: #339900;">#define TID_goodday     1</span>
<span style="color: #339900;">#define TID__MAX        2</span>
&nbsp;
<span style="color: #339900;">#define LANG_EN  0</span>
<span style="color: #339900;">#define LANG_DE  1</span>
&nbsp;
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> GetText<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> textId<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #339900;">#endif // __language_h__</span></pre></div></div>

<p>language.cpp file:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &quot;language.h&quot;</span>
&nbsp;
<span style="color: #666666;">// initial language is english</span>
<span style="color: #0000ff;">int</span> CurrentLanguage<span style="color: #000080;">=</span>LANG_EN<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// german texts</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span> TEXT_DE<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #FF0000;">&quot;HALLO WELT&quot;</span>,
  <span style="color: #FF0000;">&quot;GUTEN TAG&quot;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// english texts</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span> TEXT_EN<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #FF0000;">&quot;HELLO WORLD&quot;</span>,
  <span style="color: #FF0000;">&quot;GOOD DAY&quot;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// get text of the specified text identifer</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> GetText<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> textId<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000dd;">assert</span><span style="color: #008000;">&#40;</span>textId <span style="color: #000080;">&lt;</span> TID__MAX<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// invalid text id</span>
&nbsp;
  <span style="color: #0000ff;">switch</span><span style="color: #008000;">&#40;</span>CurrentLanguage<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">case</span> LANG_DE<span style="color: #008080;">:</span>
      <span style="color: #0000ff;">return</span> TEXT_DE<span style="color: #008000;">&#91;</span>textId<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">default</span><span style="color: #008080;">:</span>
      <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> TEXT_EN<span style="color: #008000;">&#91;</span>textId<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Once we worked with it for a while, we realised updating the language file is a time killer.</p>
<p>Everytime we modified text and exported, the header file changed, thus all source files that include language.h were recompiled due to header dependencies. This was a huge problem for us, nobody wanted to wait several minutes for a recompile, only because someone fixed a typo in the translation or added a new text.</p>
<h3>Localization, 2nd iteration</h3>
<p>What we learned from the 1st approach is no matter if we change, add or even remove text, it must not have a significant influence on compile times.</p>
<p>Rather than using generated identifiers, we used crc32&#8217;s of string literals to identify texts. This completely removed the header dependency / recompile problem! Our text system now worked like:</p>
<ul>
<li>Create texts in Microsoft Excel and save as XML</li>
<li>Custom tool to convert XML to our text format, verify that all text identifiers generate unique checksums</li>
<li>Custom tool outputs .cpp file with texts as array and checksum lookup table</li>
</ul>
<p>In order to get a tranlated text, GetText() now expects a string literal as id, generates a <a href="http://en.wikipedia.org/wiki/Crc32">crc32</a> of the incoming id, performs a <a href="http://en.wikipedia.org/wiki/Binary_search">binary search</a> on the checksum table and then uses the lookup position to index into the language array.</p>
<p>This even allowed us to return the text id when the translated text was not found, so the tester could add the text id of the translated text that is missing to the bug report. But it also allowed us to switch text at runtime to display the id rather than text (&#8221;uh what text id is display here&#8221; belongs to the past).</p>
<p>The new text system was a bit slower than the 1st iteration, but it had no significant influence on the overall runtime performance.</p>
<p>We worried more about being able to have typos in text id&#8217;s, as those are strings and located in game code now, but this was never really a problem I think.</p>
<p>We added text id&#8217;s to the Excel file first, then always copy/paste text id&#8217;s from Excel to game code. However, we can&#8217;t be 100% certain that all possible missing texts were hunted down by the QA team. We still needed a verification system that does this automatically and 100% reliable, but more on this later.</p>
<p>Due to the additional checksum lookup table and the string literal id&#8217;s in the code itself, the game also requires more memory. </p>
<p>Most development systems feature some kind of debug memory (eg <a href="http://nocash.emubase.de/gba.htm#debug">no$gba</a> has an option to emulate 8MB rather than 4MB main memory), this is at least during development not a problem. More on this later, again.</p>
<p>language.h looked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#pragma once</span>
&nbsp;
<span style="color: #0000ff;">enum</span> Languages
<span style="color: #008000;">&#123;</span>
  LANG_EN,
  LANG_DE,
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> GetText<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> textId<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>We changed from #defines to enum&#8217;s too, because those are much more debug friendly.</p>
<p>language.cpp looked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &quot;language.h&quot;</span>
&nbsp;
<span style="color: #666666;">// initial language is english</span>
Languages CurrentLanguage<span style="color: #000080;">=</span>LANG_EN<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// crc32 checksums / hashes of text id's</span>
<span style="color: #666666;">// in sorted ascending order</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> TEXT_IDs<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000dd;">12345</span>, <span style="color: #666666;">// text id: helloworld</span>
  <span style="color: #0000dd;">23456</span>, <span style="color: #666666;">// text id: goodday</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// german texts</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span> TEXT_DE<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #FF0000;">&quot;HALLO WELT&quot;</span>,
  <span style="color: #FF0000;">&quot;GUTEN TAG&quot;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// english texts</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span> TEXT_EN<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #FF0000;">&quot;HELLO WORLD&quot;</span>,
  <span style="color: #FF0000;">&quot;GOOD DAY&quot;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// performs a binary search on the the TEXT_IDs array</span>
<span style="color: #666666;">// and returns the index where the hash is located, or</span>
<span style="color: #666666;">// -1 when it could not be found.</span>
<span style="color: #0000ff;">int</span> FindTextIndex<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> hash<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">int</span> left  <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">int</span> right <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>TEXT_IDs<span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>TEXT_IDs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>left <span style="color: #000080;">&lt;=</span> right<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> index <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>left <span style="color: #000040;">+</span> right<span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>hash <span style="color: #000080;">==</span> TEXT_IDd<span style="color: #008000;">&#91;</span>index<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
      <span style="color: #0000ff;">return</span> index<span style="color: #008080;">;</span> <span style="color: #666666;">// hash found, leave!</span>
&nbsp;
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>hash <span style="color: #000080;">&gt;</span> TEXT_IDd<span style="color: #008000;">&#91;</span>index<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
      left <span style="color: #000080;">=</span> index <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">else</span>
      right <span style="color: #000080;">=</span> index <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> <span style="color: #666666;">// hash not found</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// get text of the specified text identifer,</span>
<span style="color: #666666;">// returns the textId if text could not be found</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> GetText<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> textId<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">// generate checksum / hash of incoming text id</span>
  <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> hash <span style="color: #000080;">=</span> CalcCRC32<span style="color: #008000;">&#40;</span>textId<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// search for the checksum / hash in our TEXT_IDs array</span>
  <span style="color: #0000ff;">int</span> index<span style="color: #000080;">=</span> FindTextIndex<span style="color: #008000;">&#40;</span>hash<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>index <span style="color: #000080;">==</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// text not found, return the id instead!</span>
    <span style="color: #0000ff;">return</span> textId<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">switch</span><span style="color: #008000;">&#40;</span>CurrentLanguage<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">case</span> LANG_DE<span style="color: #008080;">:</span>
      <span style="color: #0000ff;">return</span> TEXT_DE<span style="color: #008000;">&#91;</span>index<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">default</span><span style="color: #008080;">:</span>
      <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> TEXT_EN<span style="color: #008000;">&#91;</span>index<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h3>Localization, 3rd iteration</h3>
<p>The 2nd iteration is not bad, but as the project came along, new requirements did pop up.</p>
<p>We not only needed to display texts in different languages, different title logos should be displayed. We just hacked to load different resources, depending on the language setting, in game code.</p>
<p>But we should have known before, this ain&#8217;t fulfils the artists vision. So before we clump the whole game code, we decided it should be handled automatically without any action from a programmer and this was easier than we thought.</p>
<p>We already used some sort of file archive, where all game content is stored, to load files from. Think of it as a zip archive. In order to load language dependend resources, all we had to do is to support more than one file archive and priorize it. When the game requests a file, file archives are searched by priority.</p>
<p>We added an additional file archive with german content and high-priorized it. When the game requests &#8220;title.bmp&#8221;, the german archive was searched first. If the resource could not be found, the next archive was consulted. This allowed to add language dependend resources without any programmer work!</p>
<h3>Localization, 4th iteration</h3>
<p>Having all languages in main memory is quite a waste, at least on systems that don&#8217;t feature hundrets of mega-bytes. In my experience, non-text-heavy games contain about 700 texts, where edutainment games can contain thousands.</p>
<p>If every text would be 50 chars and 700 texts are available, it&#8217;s 50*700 = 35000 chars, which in ASCII is about 34kb for one language! 5 languages sum up to 34kb*5 = 170kb.</p>
<p>This is more than 4% of the Nintendo DS main memory, only for text! Not really an option to spend that much precious memory for text if you could use those wasted 135kb and spend them on a larger level, more sounds or more textures instead.</p>
<p>On memory limited systems it makes sense to have one language in memory only, namely the current language. However,  this comes with several things to consider:</p>
<ul>
<li>Memory consumption is different for different languages.</li>
<li>GetText() returns different pointers for  different languages.</li>
</ul>
<p>Different memory consumption is a huge problem. It&#8217;s irresponsible if some levels don&#8217;t load when german language is activated, because german texts consume 2kb more memory than the english ones. It makes it also impossible to replace entire language files on-the-fly, change language setting in options for example, without any delete/new mechanism involved.</p>
<p>Furthermore it&#8217;s also quirky when GetText() returns a different pointer for every language, because GUI widgets can no longer store pointers to texts, because they would point to whatever memory if the language setting changes.</p>
<p>The secret is always try to keep memory consumption as static as possible! Our custom text tool compared texts of every language and padded shorter texts of different languages with zero-bytes to consume as much space as the longest text, for example:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">You won.000000000
Du hast gewonnen.
&nbsp;
<span style="color: #007788;">Hello</span> World
Hallo Welt0</pre></div></div>

<p>Where &#8220;0&#8243; represents the padding 0&#215;00 byte.</p>
<p>This makes sure that:</p>
<ul>
<li>every language file has the exact same size.</li>
<li>offsets to texts inside the language file are always the same.</li>
</ul>
<p>This approach makes it possible to allocate memory once for the language file and then being able to work with that buffer, because the size never changes for different language files of the same category. You can load other language files to this buffer and the text system still works.</p>
<p>When text is located in a language file, we also no longer have the const char* overhead from our text arrays, just make sure to null-terminate every text!</p>
<h3>Localization, 5th iteration</h3>
<p>The additional memory footprint introduced with the 2nd iteration bothered us and we wanted it better spending on textures than text and this is very simple again.</p>
<p>We supported a hybrid system of the 1st and 2nd iteration. The 2nd iteration was perfect for development purposes, as it does not require much recompile, but comes with higher memory footprint.</p>
<p>The 1st iteration on the other hand is horrible during development, because of the recompile times, but does not require any additional data (crc table) and is lighting-fast.</p>
<p>Instead of using string literals for the text identifier directly, we wrapped them in a <em>TXT</em> macro. The debug build stringified the incoming parameter, where as the release build concatenated it to create an identifier:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#if _DEBUG</span>
  <span style="color: #339900;">#define TXT(id) #id</span>
<span style="color: #339900;">#else</span>
  <span style="color: #339900;">#define TXT(id) TID_##id</span>
<span style="color: #339900;">#endif</span></pre></div></div>

<p>It was used like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> text <span style="color: #000080;">=</span> GetText<span style="color: #008000;">&#40;</span>TXT<span style="color: #008000;">&#40;</span>helloworld<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>The debug build replaced it with:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> text <span style="color: #000080;">=</span> GetText<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;helloworld&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>and the release build with:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> text <span style="color: #000080;">=</span> GetText<span style="color: #008000;">&#40;</span>TID_helloworld<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>where <em>TID_helloworld</em> is the generated #define identifier of our custom text tool, as shown in 1st iteration.</p>
<p>We used the 2nd approach for debug builds and the 1st approach for release builds. When you switch between debug and release builds, you need to do a recompile anyway, so using the 1st does not hurt.</p>
<p>And at this point I can also resolve the &#8220;more on it later..&#8221; note from the 2nd iteration paragraph.</p>
<p>Because we used the 1st approach in release builds, we could <strong>catch all invalid text identifiers at</strong> <strong>compile time</strong> and had <strong>no memory overhead</strong> anymore, yay! Supporting both systems is also not really problematic in my opinion, since they&#8217;re pretty similar and not complicated anyway.</p>
<h3>Conclusion</h3>
<p>Creating a multi-language game comes with a couple of new tasks, don&#8217;t underestimate it! <img src='http://www.console-dev.de/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.console-dev.de/2009/06/21/single-language-vs-multi-language/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>#include &#8220;data.c&#8221; revised</title>
		<link>http://www.console-dev.de/2009/05/21/include-data-c-revised/</link>
		<comments>http://www.console-dev.de/2009/05/21/include-data-c-revised/#comments</comments>
		<pubDate>Thu, 21 May 2009 19:00:17 +0000</pubDate>
		<dc:creator>Peter Schraut</dc:creator>
				<category><![CDATA[NDS]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.console-dev.de/?p=560</guid>
		<description><![CDATA[Introduction
In the palib homebrew community (Nintendo DS homebrew development) is #include &#8220;data.c&#8221; a daily occurence. Several people point out not to follow this approach, because it&#8217;s &#8220;bad practice&#8221;.
In this article, I&#8217;m not so much trying to convince you about the evilness of including data, but I&#8217;m telling you when this approach is appropriate, when it [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>In the <a href="http://palib.info">palib</a> homebrew community (Nintendo DS homebrew development) is <em>#include &#8220;data.c&#8221;</em> a daily occurence. Several people point out not to follow this approach, because it&#8217;s &#8220;bad practice&#8221;.</p>
<p>In this article, I&#8217;m not so much trying to convince you about the evilness of including data, but I&#8217;m telling you when this approach is appropriate, when it isn&#8217;t and why. The article assumes you use <a href="http://www.devkitpro.org">devkitARM</a> to build your .nds files.</p>
<h3>History</h3>
<p>I guess the palib author adapted this approach from the early <a href="http://ngine.de/article/id/40/">HAM</a> days. HAM was around 2001-2005 a very popular free software development solution for the <a href="http://en.wikipedia.org/wiki/Game_Boy_Advance">Nintendo Game Boy Advance</a>, including almost everything you would need to create games for this lovely device.</p>
<p>The example projects in HAM used to integrate resources (graphics, sounds, etc&#8230;) by converting resources to C source code arrays. The generated files were #included in the example source file (main.c).</p>
<p>The intention behind this approach makes sense for the Game Boy Advance. GBA games are shipped on <a href="http://en.wikipedia.org/wiki/ROM_cartridge">Game Pak&#8217;s</a> that consist of <a href="http://en.wikipedia.org/wiki/Read-only_memory">Read-Only-Memory (ROM)</a> containing the program code (<a href="http://en.wikipedia.org/wiki/Code_segment">.text</a>) and data (<a href="http://en.wikipedia.org/wiki/Data_segment">.rodata</a>). When you declare <a href="http://en.wikipedia.org/wiki/Const">const</a> data on the heap, the memory is allocated in ROM. In order to load graphics to video ram, you could tranfer image data directly from ROM to Vram, without the need to have it in main memory.</p>
<h3>What&#8217;s the deal with that?</h3>
<p>The <a href="http://en.wikipedia.org/wiki/Nintendo_ds">Nintendo DS</a> on the other hand does not feature this ROM section in the traditional way anymore, everything (code, data) is located in the <a href="http://en.wikipedia.org/wiki/Nintendo_ds#Technical_specifications">4MB main memory</a> region. When you #include data in your program code, memory for it is <strong>always</strong> allocated and present in main memory, even when you don&#8217;t need it!</p>
<p>Let&#8217;s take a simple example to clearify: Imagine you create a platform game with several levels. Each level features a different set of graphics (sunny, snowy, rainy). If you would #include graphics in program code, memory for every single graphic would be allocated in main memory for every level in each level. So you would have graphics of level 1 (sunny) in main memory, even when you play level 2 (snowy). It might work for the first 1 1/2 graphic-sets, but then exceeds the main memory capacity of 4MB eventually.</p>
<p>As you already know, program code is located in main memory too, so you can&#8217;t even calculate with 4MB for resources. As far as my experience goes, real-world-projects use about 400-600 KB for program code, leaving you with only about 3.5MB for internal game management and resources.</p>
<h3>#include or not #include, that&#8217;s the question</h3>
<p>When you aim for a game with a decent quality, you can&#8217;t #include data as this would blow up memory, you have to load resources from the file system. To be more precise, you load only resources to memory, that you actually need in the particular level (<a href="http://www.console-dev.de/2009/05/09/file-io-handling-in-nds-homebrew/">file i/o handling in nds homebrew</a>).</p>
<p>However, sometimes it&#8217;s necessary to have access to resources all the time, for example to display on-screen debug text. For such &#8220;low-level-system&#8221;, you want to have the resource in memory, because what happens when loading of the debug-font resource would fail?! You couldn&#8217;t even display an error at this time, because the font is missing! For this special case, I suggest you either convert the resource in question to an <a href="http://en.wikipedia.org/wiki/.o">object file (.o)</a> and link this one, or convert to assembler code and assemble as well as link, but don&#8217;t #include.</p>
<p>If you generate C code, rather than assembler, the C compiler must parse the code, then generate the corresponding assembler code, that is being transformed to object code afterwards. When you don&#8217;t like waiting for your compiler to complete, choose .o or .s as target!</p>
<h3>Why did I just say &#8220;don&#8217;t #include&#8221;?</h3>
<p>When you create a seperate source file that contains data and store it in your &#8220;source&#8221; directory, it&#8217;s being compiled only when the target object file (.o) is either missing or the source file is newer. As for the debug font resource, this would be once or at least very rarely. After the source file has been compiled and transformed to an object file, it only needs to get passed to the linker when you build the project. It removes the need to compile the file every time again.</p>
<p>When you #include data in a file where you currently work at, the compiler must parse the #included data every time you change something on the code and recompile. Performance-wise isn&#8217;t it really much of a problem anymore, since todays computers have several GB memory, multi-core processors and relatively fast hard-drives, but it makes the compile process slower anyway, which sums up when your project grows.</p>
<p>I think the error that occurs most often when dealing with #include data is <a href="http://forum.palib.info/index.php?topic=823.0">multiple definitions of the same symbol</a>. This occurs when you include a file in more than one source file, there are dozens of these posts on the palib forum!</p>
<h3>How to embed data</h3>
<p>You want to embed &#8220;debugfont.bmp&#8221; in your game, so it&#8217;s always in memory, how you doing that? It&#8217;s simple! Use your favorite graphics converter to ouput a &#8220;binary&#8221; version of the converted data, lets call it &#8220;output.bin&#8221;. In order to embed output.bin, all you have to do is to move it to the &#8220;build&#8221; directory of your project. The example makefiles of libnds/devkitARM include a target that takes every .bin file in the &#8220;build&#8221; directory and convert, compile and link it.</p>
<p>If you want to do it by hand, use <em>devkitARM/bin/bin2s.exe</em> to convert the .bin file to assembler code and move the output file to your &#8220;source&#8221; directory. Next time you build the project, the file gets compiled and linked.</p>
<h3>Conclusion</h3>
<ul>
<li>You can&#8217;t complete a game project by #include&#8217;ing data, which contains more than about 3.5MB resources.</li>
<li>Sometimes you want to embed data, eg graphic for debug text, compile and link the file.</li>
<li>#include data is error-prone, multiple definition of&#8230;</li>
<li>#include data slows down compile process which can become significant as the project grows.</li>
</ul>
<p><a href="http://nocash.emubase.de/gbatek.htm#dsmemorymaps"><br />
</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.console-dev.de/2009/05/21/include-data-c-revised/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>print formatted text to iDeaS debug console</title>
		<link>http://www.console-dev.de/2009/05/17/print-formatted-text-to-ideas-debug-console/</link>
		<comments>http://www.console-dev.de/2009/05/17/print-formatted-text-to-ideas-debug-console/#comments</comments>
		<pubDate>Sun, 17 May 2009 11:12:39 +0000</pubDate>
		<dc:creator>Peter Schraut</dc:creator>
				<category><![CDATA[NDS]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.console-dev.de/?p=534</guid>
		<description><![CDATA[Introduction
iDeaS is a Nintendo DS software emulator available for Linux and Microsoft Windows. Since version 1.0.2.8 (21 Dec 2008) iDeaS features program breakpoints and user messages that can be sent to the debug console:
# Added program breakpoint (SWI #0xFDFDFD).
# Added output on console for user&#8217;s messages (SWI #0xFCFCFC).
My alarm bells started to bang in the [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p><a href="http://www.ideasemu.org">iDeaS</a> is a Nintendo DS software emulator available for Linux and Microsoft Windows. Since version 1.0.2.8 (21 Dec 2008) iDeaS features program breakpoints and user messages that can be sent to the debug console:</p>
<blockquote><p># Added program breakpoint (SWI #0xFDFDFD).<br />
# Added output on console for user&#8217;s messages (SWI #0xFCFCFC).</p></blockquote>
<p>My alarm bells started to bang in the moment I saw the changelog. Both features have been implemented using software interrupts, that do not exist on the target hardware.</p>
<p>What does it mean? It means, those software interrupt ID&#8217;s make your NDS application incompatible with the NDS hardware. Whenever you accidentally print an iDeaS debug message, the application will crash on actual hardware.</p>
<p><a href="http://nocash.emubase.de/gba.htm">no$gba</a> implemented the breakpoint and debug message facility quite excellent. In no$gba a breakpoint is just a <em>mov r11, r11</em> instruction (a nop), which can be executed by the hardware by all means. The no$gba debug message system is a bit more complicated. It uses a combination of harmless instructions to detect you want to print text. However, all instructions used for that feature do work on hardware as well, this is what we want, this is how it should be implemented.</p>
<h3>The current scenario</h3>
<p>Shortly after the release I contacted the iDeaS author and told him the current approach isn&#8217;t top notch and should be changed that it won&#8217;t break program execution on actual hardware. Unfortunalety he didn&#8217;t see the advantage, since you can:</p>
<ul>
<li> Remove all prints before you test on hardware</li>
</ul>
<p>Obviously, this cannot be the way to handle it. Manually removing all prints would be enormously time consuming and error-prone.</p>
<ul>
<li> Use some sort of <em>#ifdef</em> blocks to automatically remove all prints</li>
</ul>
<p>This might sound like a solid idea at first glance, but thinking about it a further minute, proves it isn&#8217;t. The problem is simple, it&#8217;s too time comsuning. When 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 has, can it take a significant amount of time to rebuild, which is no option for many people.</p>
<ul>
<li>Wrap print calls with some enabled/disabled mechanism</li>
</ul>
<p>A function that wraps print calls with a surrounding <em>if</em> block, removes the need to rebuild the project. Just disable debug output at program initialization and there you go. However, I tend to forget things that are of no importance for me and this is something I would forget many times!</p>
<h3>How to solve the puzzle</h3>
<p>We developers want a debug text system that will not break program execution, no matter if the application runs in an emulator or on actual hardware. All those previous points do make some sense, but don&#8217;t remove the problem in a whole. What we have to create is a system that:</p>
<ul>
<li>Is able to print text to the debug console</li>
<li>Can be switched on/off at runtime</li>
<li>Automatically detects when it runs on hardware and discards all print calls in this case</li>
<li>Remove all print calls when building a &#8220;release version&#8221; (pre-processor)</li>
</ul>
<p>Everthing on the list should be quite clear, except for the hardware detection. When I had the requirements-list done and was wondering how to implement that, I remembered <a href="http://forum.gbadev.org/viewtopic.php?t=910">this post at gbadev.org</a>. They used a hardware feature that no emulator seems to emulate correctly, instruction fetching:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="asm" style="font-family:monospace;">detectGBA<span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">;returns 0 if emulated</span>
<span style="color: #00007f; font-weight: bold;">mov</span> r0<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0</span>
<span style="color: #00007f; font-weight: bold;">str</span> r0<span style="color: #339933;">,</span>_0
_0<span style="color: #339933;">:</span> <span style="color: #00007f; font-weight: bold;">mov</span> r0<span style="color: #339933;">,</span>#<span style="color: #0000ff;">1</span>
<span style="color: #00007f; font-weight: bold;">mov</span> pc<span style="color: #339933;">,</span>lr</pre></td></tr></table></div>

<p>The <em>str r0, _0</em> instruction overwrites the following instruction, where r0 is set to 1. However, on real hardware the instruction would be 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&#8217;t work correctly anymore.</p>
<p>So I slightly changed the code to restore the original instruction to be able to call the function more than once. I also made it arm and thumb compatible. Here is the entire source code to print text to iDeaS debug console, that detects real hardware and discards all prints at runtime. You can also use a pre-processor #define to remove all print calls when you build a &#8220;release version&#8221;.</p>
<p>ideas.h</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#ifndef __ideas_h__</span>
<span style="color: #339900;">#define __ideas_h__</span>
&nbsp;
<span style="color: #339900;">#if __cplusplus</span>
  <span style="color: #0000ff;">extern</span> <span style="color: #FF0000;">&quot;C&quot;</span> <span style="color: #008000;">&#123;</span>
<span style="color: #339900;">#endif</span>
&nbsp;
<span style="color: #339900;">#if !_RELEASE</span>
&nbsp;
<span style="color: #0000ff;">int</span> IdeasEnableDebugOutput<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> enable<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> IdeasOutputDebugString<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> format, ...<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #339900;">#else</span>
&nbsp;
<span style="color: #666666;">// in release mode use empty functions, so the compiler</span>
<span style="color: #666666;">// optimizes any call to them away.</span>
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">int</span> IdeasEnableDebugOutput<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> <span style="color: #ff0000; font-style: italic;">/*enable*/</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">int</span> IdeasOutputDebugString<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> <span style="color: #ff0000; font-style: italic;">/*format*/</span>, ...<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #339900;">#endif // _RELEASE</span>
&nbsp;
<span style="color: #0000ff;">int</span> IdeasIsEmulator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #339900;">#if __cplusplus</span>
  <span style="color: #008000;">&#125;</span> <span style="color: #666666;">// extern &quot;C&quot;</span>
<span style="color: #339900;">#endif</span>
&nbsp;
<span style="color: #339900;">#endif // __ideas_h__</span></pre></td></tr></table></div>

<p>ideas.c</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &quot;ideas.h&quot;</span>
<span style="color: #339900;">#include &lt;stdio.h&gt;</span>
<span style="color: #339900;">#include &lt;stdarg.h&gt;</span>
&nbsp;
<span style="color: #339900;">#if !_RELEASE</span>
&nbsp;
<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">int</span> IdeasDebugOutputEnabled<span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// Gets if the program runs in an emulator.</span>
__attribute__ <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>noinline<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #0000ff;">int</span> IdeasIsEmulator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
<span style="color: #666666;">// The idea behind the code is to overwrite</span>
<span style="color: #666666;">// the &quot;mov r0, #0&quot; instruction with &quot;mov r0, r0&quot; (NOP).</span>
<span style="color: #666666;">// On real hardware, the instruction would have been fetched,</span>
<span style="color: #666666;">// so the overwrite has no effect for the first time executed.</span>
<span style="color: #666666;">// In order to be able to call the function more than once,</span>
<span style="color: #666666;">// the original instruction is being restored.</span>
<span style="color: #666666;">// see http://forum.gbadev.org/viewtopic.php?t=910</span>
<span style="color: #339900;">#ifdef __thumb__</span>
  <span style="color: #0000ff;">int</span> mov_r0_r0 <span style="color: #000080;">=</span> <span style="color: #208080;">0x1c00</span><span style="color: #008080;">;</span>   <span style="color: #666666;">// mov r0, r0</span>
  <span style="color: #0000ff;">int</span> mov_r0_0  <span style="color: #000080;">=</span> <span style="color: #208080;">0x2000</span><span style="color: #008080;">;</span>   <span style="color: #666666;">// mov r0, #0</span>
&nbsp;
  asm <span style="color: #0000ff;">volatile</span> <span style="color: #008000;">&#40;</span>
    <span style="color: #FF0000;">&quot;mov  r0, %1     <span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span>  <span style="color: #666666;">// r0 = mov_r0_r0</span>
    <span style="color: #FF0000;">&quot;mov  r2, %2     <span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span>  <span style="color: #666666;">// r2 = mov_r0_0</span>
    <span style="color: #FF0000;">&quot;mov  r1, pc     <span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span>  <span style="color: #666666;">// r1 = program counter</span>
    <span style="color: #FF0000;">&quot;strh r0, [r1]   <span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span>  <span style="color: #666666;">// Overwrites following instruction with mov_r0_r0</span>
    <span style="color: #FF0000;">&quot;mov  r0, #0     <span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span>  <span style="color: #666666;">// r0 = 0</span>
    <span style="color: #FF0000;">&quot;strh r2, [r1]   <span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span>  <span style="color: #666666;">// Restore previous instruction</span>
    <span style="color: #008080;">:</span> <span style="color: #FF0000;">&quot;=r&quot;</span><span style="color: #008000;">&#40;</span>mov_r0_r0<span style="color: #008000;">&#41;</span>       <span style="color: #666666;">// output registers</span>
    <span style="color: #008080;">:</span> <span style="color: #FF0000;">&quot;r&quot;</span><span style="color: #008000;">&#40;</span>mov_r0_r0<span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">&quot;r&quot;</span><span style="color: #008000;">&#40;</span>mov_r0_0<span style="color: #008000;">&#41;</span>    <span style="color: #666666;">// input registers</span>
    <span style="color: #008080;">:</span> <span style="color: #FF0000;">&quot;%r1&quot;</span>,<span style="color: #FF0000;">&quot;%r2&quot;</span>           <span style="color: #666666;">// clobbered registers</span>
    <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #339900;">#else</span>
  <span style="color: #0000ff;">int</span> mov_r0_r0 <span style="color: #000080;">=</span> <span style="color: #208080;">0xe1a00000</span><span style="color: #008080;">;</span> <span style="color: #666666;">// mov r0, r0</span>
  <span style="color: #0000ff;">int</span> mov_r0_0  <span style="color: #000080;">=</span> <span style="color: #208080;">0xe3a00000</span><span style="color: #008080;">;</span> <span style="color: #666666;">// mov r0, #0</span>
&nbsp;
  asm <span style="color: #0000ff;">volatile</span> <span style="color: #008000;">&#40;</span>
    <span style="color: #FF0000;">&quot;mov  r0, %1     <span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span>  <span style="color: #666666;">// r0 = mov_r0_r0</span>
    <span style="color: #FF0000;">&quot;mov  r2, %2     <span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span>  <span style="color: #666666;">// r2 = mov_r0_0</span>
    <span style="color: #FF0000;">&quot;mov  r1, pc     <span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span>  <span style="color: #666666;">// r1 = program counter</span>
    <span style="color: #FF0000;">&quot;str  r0, [r1]   <span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span>  <span style="color: #666666;">// Overwrites following instruction with mov_r0_r0</span>
    <span style="color: #FF0000;">&quot;mov  r0, #0     <span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span>  <span style="color: #666666;">// r0 = 0</span>
    <span style="color: #FF0000;">&quot;str  r2, [r1]   <span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span>  <span style="color: #666666;">// Restore previous instruction</span>
    <span style="color: #008080;">:</span> <span style="color: #FF0000;">&quot;=r&quot;</span><span style="color: #008000;">&#40;</span>mov_r0_r0<span style="color: #008000;">&#41;</span>       <span style="color: #666666;">// output registers</span>
    <span style="color: #008080;">:</span> <span style="color: #FF0000;">&quot;r&quot;</span><span style="color: #008000;">&#40;</span>mov_r0_r0<span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">&quot;r&quot;</span><span style="color: #008000;">&#40;</span>mov_r0_0<span style="color: #008000;">&#41;</span>    <span style="color: #666666;">// input registers</span>
    <span style="color: #008080;">:</span> <span style="color: #FF0000;">&quot;%r1&quot;</span>,<span style="color: #FF0000;">&quot;%r2&quot;</span>           <span style="color: #666666;">// clobbered registers</span>
    <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #339900;">#endif</span>
&nbsp;
  <span style="color: #0000ff;">return</span> mov_r0_r0 <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// This function must be noinline, because</span>
<span style="color: #666666;">// iDeaS expects the text to output in register r0.</span>
<span style="color: #666666;">// If this code is inlined somewhere, it's not guaranteed</span>
<span style="color: #666666;">// that text is located in r0 anymore, thus will not work.</span>
<span style="color: #0000ff;">static</span> __attribute__ <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>noinline<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #0000ff;">void</span> IdeasOutputDebugStringInternal<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> text<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
<span style="color: #339900;">#ifdef __thumb__</span>
  asm <span style="color: #0000ff;">volatile</span> <span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;swi #0xfc&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #339900;">#else</span>
  asm <span style="color: #0000ff;">volatile</span> <span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;swi #0xfc000&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #339900;">#endif</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #666666;">// Prints formatted output to the iDeaS debug console</span>
<span style="color: #666666;">// Returns false when text has not been printed, true otherwise.</span>
<span style="color: #0000ff;">int</span> IdeasOutputDebugString<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> format, ...<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">va_list</span> args<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">char</span>    buffer<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">128</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// increase to support more characters</span>
&nbsp;
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>IdeasDebugOutputEnabled <span style="color: #000040;">||</span> <span style="color: #000040;">!</span>IdeasIsEmulator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000dd;">va_start</span><span style="color: #008000;">&#40;</span>args,<span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  vsnprintf<span style="color: #008000;">&#40;</span>buffer, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>buffer<span style="color: #008000;">&#41;</span>, format, args<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000dd;">va_end</span><span style="color: #008000;">&#40;</span>args<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  IdeasOutputDebugStringInternal<span style="color: #008000;">&#40;</span>buffer<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// Enables or disables debug output.</span>
<span style="color: #666666;">// Returns the previous enabled state.</span>
<span style="color: #0000ff;">int</span> IdeasEnableDebugOutput<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> enable<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">int</span> old <span style="color: #000080;">=</span> IdeasDebugOutputEnabled<span style="color: #008080;">;</span>
  IdeasDebugOutputEnabled <span style="color: #000080;">=</span> enable<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">return</span> old<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #339900;">#endif // !_RELEASE</span></pre></td></tr></table></div>

<p><a href="http://www.console-dev.de/news/2009/ideas-outputdebugstring.zip">Download the files here.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.console-dev.de/2009/05/17/print-formatted-text-to-ideas-debug-console/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>file i/o handling in nds homebrew</title>
		<link>http://www.console-dev.de/2009/05/09/file-io-handling-in-nds-homebrew/</link>
		<comments>http://www.console-dev.de/2009/05/09/file-io-handling-in-nds-homebrew/#comments</comments>
		<pubDate>Sat, 09 May 2009 18:00:46 +0000</pubDate>
		<dc:creator>Peter Schraut</dc:creator>
				<category><![CDATA[NDS]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.console-dev.de/?p=513</guid>
		<description><![CDATA[One problem in many nds homebrew games is improper handling of missing assets and improper handling when user content could not be created.
A commercial game that is being shipped on its own cartrige can rely on the fact all game assets are available. There is no way to delete files from it. In this case, [...]]]></description>
			<content:encoded><![CDATA[<p>One problem in many nds homebrew games is improper handling of missing assets and improper handling when user content could not be created.</p>
<p>A commercial game that is being shipped on its own cartrige can rely on the fact all game assets are available. There is no way to delete files from it. In this case, you don&#8217;t need to keep attention if fopen or fread succeded, because they always do.</p>
<p>With nds homebrew it&#8217;s a whole different. The user of your game has to copy related game assets and the .nds executable itself to his/her flashcard. Everything can go wrong during this process. Beside missing files, it could be also a flashcard compatibility problem, such as an invalid DLDI driver. In these cases, many games just die silently, rather that displaying an error-message to let the user know what happened.</p>
<p>You often find file loading code such as (don&#8217;t copy!):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">uint8_t<span style="color: #000040;">*</span> LoadFile<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> filename<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">FILE</span><span style="color: #000040;">*</span> file <span style="color: #000080;">=</span> <span style="color: #0000dd;">fopen</span><span style="color: #008000;">&#40;</span>filename, <span style="color: #FF0000;">&quot;rb&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000dd;">fseek</span><span style="color: #008000;">&#40;</span>file, <span style="color: #0000dd;">0</span>, <span style="color: #0000ff;">SEEK_END</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">size_t</span> size <span style="color: #000080;">=</span> <span style="color: #0000dd;">ftell</span><span style="color: #008000;">&#40;</span>file<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000dd;">rewind</span><span style="color: #008000;">&#40;</span>file<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  uint8_t<span style="color: #000040;">*</span> buffer <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>uint8_t<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #0000dd;">malloc</span><span style="color: #008000;">&#40;</span>size<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000dd;">fread</span><span style="color: #008000;">&#40;</span>buffer, size, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>uint8_t<span style="color: #008000;">&#41;</span>, file<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000dd;">fclose</span><span style="color: #008000;">&#40;</span>file<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">return</span> buffer<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>All those <a href="http://www.cplusplus.com/reference/clibrary/cstdio/">functions</a> have return values, which must <strong>not</strong> be ignored. If any function call fails here, it&#8217;s very unlikely the program will continue to operate correctly.</p>
<p>We do have to find a better way to ensure loading either works &#8220;in all cases&#8221; or does not. The very first thing that comes to mind is to use some sort of <a href="http://en.wikipedia.org/wiki/File_archiver">file archive</a>. If you bundle all assets in one archive, there is a good chance everything is available and there is no way to delete files from it. At application startup, test if the archive is available, open it and read a test file to confirm the system works. If it does not, display a human readable error message, numerical error codes don&#8217;t count.</p>
<p>Another critical part is deployment. If there is more than one file to deploy, there is also more than one chance to fail. So, it would be quite good if the user needs to deploy only one file for the entire game, rather than the .nds file and the archive seperately. This can be done by appending the archive to the .nds file!</p>
<p>I&#8217;ve created my own tool set for all this, which I won&#8217;t share,  but there is a free public solution for it called <a href="http://nodadev.wordpress.com/nds-projects/efs-library/">Embedded File System Library</a> as well. I&#8217;ve never used EFS, but it should do exactly what we need if we trust its documentation. If we use this approach, an archive appended to the .ds file, we can be pretty sure assets are available as long as the appended archive is available. Only two problems left</p>
<ul>
<li>Incompatible DLDI driver.</li>
</ul>
<p>To detect an incompatible DLDI driver, check the return value of <em>fatInitDefault</em>. This function is part of libfat. If it returns false, display an appropriate message and make the user aware it could be due to an incompatible DLDI driver. This gives him/her a chance to solve the problem rather than just being frustrated, deleting your game and sending you hate-mails.</p>
<ul>
<li>User aborted the copy process before the whole file was written to the flashcard.</li>
</ul>
<p>To check if the whole file was written to the flashcard could be done by appending a magic value at the end of the archive. At application startup seek to this position, read the value and compare it with what it should be. If it&#8217;s different, display a warning that the attached data seems to be corrupted. You could let the user continue with the game, he/she will know what could have caused the problem when the game crashes eventually.</p>
<p><strong>What I do to ensure file i/o works</strong></p>
<p>Create an archive of all assets, append it to the .nds file as well as a magic value.</p>
<p>The magic value can be a string like &#8220;I FEEL GOOD&#8221; at the end of the file. At application startup, initialize libfat and repsond to its return value. Display an error message when <em>fatInitDefault </em>failed.</p>
<p>Open the application file where the archive is appened. Check if this operation succeded and display an error message if anything went wrong.</p>
<p>Seek to and read the &#8220;I FEEL GOOD&#8221; magic value. If it&#8217;s different, display a &#8220;data is corrupted&#8221; warning. Open and read a file from the archive to verify the system works, display an error if it fails.</p>
<p>From now on assume the file i/o system will work and continue with further initialization.</p>
<p>If all checks last longer than a few milliseconds, display a &#8220;Please wait, initializing file system&#8230;&#8221; message before.</p>
<p>Source code snippet to verify that at the end of the program file is &#8220;I FEEL GOOD&#8221; located:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// opens the file specified by filename and checks if at the</span>
<span style="color: #666666;">// end of the file is &quot;I FEED GOOD&quot; located.</span>
<span style="color: #666666;">// returns true on success, false otherwise. If return value</span>
<span style="color: #666666;">// is false, further error information is stored at errorString.</span>
<span style="color: #0000ff;">bool</span> VerifyProgramFile<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> filename, <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> errorString<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span> MAGICVALUE<span style="color: #000080;">=</span><span style="color: #FF0000;">&quot;I FEEL GOOD&quot;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// open file</span>
  <span style="color: #0000ff;">FILE</span> <span style="color: #000040;">*</span>file <span style="color: #000080;">=</span> <span style="color: #0000dd;">fopen</span><span style="color: #008000;">&#40;</span>filename, <span style="color: #FF0000;">&quot;rb&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>file <span style="color: #000080;">==</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">sprintf</span><span style="color: #008000;">&#40;</span>errorString, <span style="color: #FF0000;">&quot;Cannot open file '%s'.&quot;</span>, filename<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #666666;">// seek to magic value offset</span>
  <span style="color: #0000dd;">fseek</span><span style="color: #008000;">&#40;</span>file, <span style="color: #0000dd;">0</span>, <span style="color: #0000ff;">SEEK_END</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">size_t</span> magicOffset <span style="color: #000080;">=</span> <span style="color: #0000dd;">ftell</span><span style="color: #008000;">&#40;</span>file<span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> <span style="color: #0000dd;">strlen</span><span style="color: #008000;">&#40;</span>MAGICVALUE<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">fseek</span><span style="color: #008000;">&#40;</span>file, magicOffset, <span style="color: #0000ff;">SEEK_SET</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// could not seek to magic</span>
    <span style="color: #0000dd;">sprintf</span><span style="color: #008000;">&#40;</span>errorString, <span style="color: #FF0000;">&quot;Data in file '%s' seems to be corrupted.&quot;</span>, filename<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">fclose</span><span style="color: #008000;">&#40;</span>file<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #666666;">// read magic value</span>
  <span style="color: #0000ff;">char</span> magicValue<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">64</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">fread</span><span style="color: #008000;">&#40;</span>magicValue, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span><span style="color: #008000;">&#41;</span>, <span style="color: #0000dd;">strlen</span><span style="color: #008000;">&#40;</span>MAGICVALUE<span style="color: #008000;">&#41;</span>, file<span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">strlen</span><span style="color: #008000;">&#40;</span>MAGICVALUE<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// could not read magic</span>
    <span style="color: #0000dd;">sprintf</span><span style="color: #008000;">&#40;</span>errorString, <span style="color: #FF0000;">&quot;Data in file '%s' seems to be corrupted.&quot;</span>, filename<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">fclose</span><span style="color: #008000;">&#40;</span>file<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #666666;">// compare magic value</span>
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">memcmp</span><span style="color: #008000;">&#40;</span>magicValue, MAGICVALUE, <span style="color: #0000dd;">strlen</span><span style="color: #008000;">&#40;</span>MAGICVALUE<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// magic is different</span>
    <span style="color: #0000dd;">sprintf</span><span style="color: #008000;">&#40;</span>errorString, <span style="color: #FF0000;">&quot;Data in file '%s' seems to be corrupted.&quot;</span>, filename<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">fclose</span><span style="color: #008000;">&#40;</span>file<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #666666;">// all tests successfully passed</span>
  <span style="color: #0000dd;">fclose</span><span style="color: #008000;">&#40;</span>file<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Source code snippet of application startup code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>fatInitDefault<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    DisplayMessage<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;libfat file system initialization failed. Did you apply the correct DLDI driver?&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    Halt<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">char</span> errorString<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1024</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>VerifyProgramFile<span style="color: #008000;">&#40;</span>pathToFileArchive, errorString<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    DisplayMessage<span style="color: #008000;">&#40;</span>errorString<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    WaitForUserConfirmation<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #666666;">// open archive and test if a file</span>
  <span style="color: #666666;">// can be read from the archive. if all tests</span>
  <span style="color: #666666;">// succeed, continue with application initialization...</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.console-dev.de/2009/05/09/file-io-handling-in-nds-homebrew/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Nintendo DS 4k Intro</title>
		<link>http://www.console-dev.de/2009/04/25/nintendo-ds-4k-intro/</link>
		<comments>http://www.console-dev.de/2009/04/25/nintendo-ds-4k-intro/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 07:57:02 +0000</pubDate>
		<dc:creator>Peter Schraut</dc:creator>
				<category><![CDATA[NDS]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.console-dev.de/?p=487</guid>
		<description><![CDATA[Over eastern I followed the breakpoint demo party via live stream and was especially attired in the 4k intro competition, that motivated me to try if it is possible to create an application that draws a simple quad in 4096 bytes on the Nintendo DS, using the devkitARM tool-chain.
However, even compiling a .c file containing [...]]]></description>
			<content:encoded><![CDATA[<p>Over eastern I followed the <a href="http://breakpoint.untergrund.net">breakpoint demo party</a> via live stream and was especially attired in the 4k intro competition, that motivated me to try if it is possible to create an application that draws a simple quad in 4096 bytes on the Nintendo DS, using the <a href="http://www.devkitpro.org">devkitARM tool-chain</a>.</p>
<p>However, even compiling a .c file containing only the application entry point, without any external libraries, already creates a .nds file that is 54848 bytes:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>After fiddling around for &#8220;some&#8221; time, I had a .nds file that not only shows a simple quad, I had one that shows hundreds of lit textured cubes that create a tunnel where the camera flies through with a fullscreen distortion effect in less than 4096 bytes.</p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/dqybl_TCWe8&#038;hl=de&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/dqybl_TCWe8&#038;hl=de&#038;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
<p>Once we&#8217;ve released it at pouet.net, I figured from the comments I could had shrink the filesize further 480 bytes by just compressing it &#8211; I feel so dumb! <img src='http://www.console-dev.de/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  </p>
<p>Obviously it can not compete with the breakpoint 4k entries, but I felt good that I was able to do it anyway, especially because it seems to be the worlds first 4k intro for the Nintendo DS! Unfortunalety, it features no music.</p>
<p>You can download the binary at: <a href="http://pouet.net/prod.php?which=53081">http://pouet.net/prod.php?which=53081</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.console-dev.de/2009/04/25/nintendo-ds-4k-intro/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
