<?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>strcpy(blog_title, witty_saying) &#187; dice roller</title>
	<atom:link href="http://blogs.ijw.co.nz/matthew/index.php/tag/dice-roller/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.ijw.co.nz/matthew</link>
	<description>//what a dirty hack</description>
	<lastBuildDate>Tue, 29 Sep 2009 23:03:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Brewing the Nintendo DS from the comfort of the home, Part 2</title>
		<link>http://blogs.ijw.co.nz/matthew/index.php/2009/09/brewing-the-nintendo-ds-from-the-comfort-of-the-home-part-2/</link>
		<comments>http://blogs.ijw.co.nz/matthew/index.php/2009/09/brewing-the-nintendo-ds-from-the-comfort-of-the-home-part-2/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 21:00:34 +0000</pubDate>
		<dc:creator>Matthew Bowra-Dean</dc:creator>
				<category><![CDATA[Personal Project]]></category>
		<category><![CDATA[dice roller]]></category>
		<category><![CDATA[ds homebrew]]></category>

		<guid isPermaLink="false">http://blogs.ijw.co.nz/matthew/?p=32</guid>
		<description><![CDATA[Well my first version of the dice roller has turned out to be very easy to produce. I initially wanted to stay away from any graphics code and devkitPro provides a good avenue for that in the form of a console that supports ANSI escape codes. A built-in on-screen keyboard can be used for user [...]]]></description>
			<content:encoded><![CDATA[<p>Well my first version of the dice roller has turned out to be very easy to produce. I initially wanted to stay away from any graphics code and devkitPro provides a good avenue for that in the form of a console that supports <a href="http://en.wikipedia.org/wiki/ANSI_escape_code">ANSI escape codes</a>. A built-in on-screen keyboard can be used for user input and access to the SLOT-1 device&#8217;s filesystem is accessible through the standard C I/O functions (fopen, getc etc.).</p>
<p>Here&#8217;s a basic overview of the code:<br />
<span id="more-32"></span></p>
<h2>main.c</h2>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">**</span> argv<span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
    PROGRAM_STATE pState <span style="color: #339933;">=</span> MENU<span style="color: #339933;">;</span>
    touchPosition touch<span style="color: #339933;">;</span>
    consoleDemoInit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    filesystem_init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><code>consoleDemoInit()</code> is a libnds function that initialises the DS&#8217; displays for console only display. <code>filesystem_init()</code> calls the libndsfat function <code>fatInitDefault()</code> to initialise the filesystem and if successful reads from a config file on the SLOT-1 device.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">;;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//update input state</span>
        scanKeys<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        touchRead<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>touch<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span>pState<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">case</span> MENU<span style="color: #339933;">:</span>
            menu_draw<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span>menu_handleinput<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>touch<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">:</span>
                pState <span style="color: #339933;">=</span> CONFIG<span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">:</span>
                pState <span style="color: #339933;">=</span> ROLLER<span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">case</span> CONFIG<span style="color: #339933;">:</span>
            config_draw<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>config_handleinput<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>touch<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
                pState <span style="color: #339933;">=</span> MENU<span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">case</span> ROLLER<span style="color: #339933;">:</span>
            roller_draw<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>roller_handleinput<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>touch<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
                pState <span style="color: #339933;">=</span> MENU<span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        swiWaitForVBlank<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        consoleClear<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The second part of the main function is the program loop. It is a simple state machine that has three states, a main menu, a configuration menu and the roller itself.<br />
The first couple of function calls each loop are to libnds functions that update the user input state, <code>scanKeys()</code> for button presses and <code>touchRead(&#038;touch)</code> for the touchscreen&#8217;s currently pressed coordinates. Each state then has a <code>draw</code> function and a <code>handleInput</code> function. </p>
<p>Finally there are another couple of calls to libnds. The first (<code>swiWaitForVBlank()</code>) blocks the program until the VBlank interrupt is triggered indicating that the screen has finished drawing. The second (<code>consoleClear()</code>) clears the console with an ANSI escape sequence.</p>
<h2>menu.c</h2>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> menu_draw<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
    iprintf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #660099; font-weight: bold;">\x1b</span>[2;2H&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    iprintf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Welcome to DS Dice Roller<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is the menu drawing function. <code>iprintf</code> is used rather than <code>printf</code> to avoid any inadvertent formatting of floats. The &#8220;\x1b[&#8221; string is the ANSI control sequence indicator and is followed by the row and the column of the desired position of the cursor.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> menu_handleinput<span style="color: #009900;">&#40;</span>touchPosition <span style="color: #339933;">*</span> touch<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> keys <span style="color: #339933;">=</span> keysDown<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><code>keysDown()</code> is a libnds function that returns an integer that is a bitwise OR of the current buttons pressed by the user. It is masked off to find the status of the buttons.</p>
<h2>config.c</h2>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> config_numberinput<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> <span style="color: #339933;">*</span> n<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> max<span style="color: #339933;">,</span> Keyboard <span style="color: #339933;">**</span> k<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> key<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!*</span>k<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #339933;">*</span>k <span style="color: #339933;">=</span> keyboardDemoInit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>k<span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span>visible<span style="color: #009900;">&#41;</span>
        keyboardShow<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    key <span style="color: #339933;">=</span> keyboardUpdate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>key <span style="color: #339933;">==</span> <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\b</span>'</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">*</span>n <span style="color: #339933;">/=</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>key <span style="color: #339933;">&gt;=</span> <span style="color: #ff0000;">'0'</span> <span style="color: #339933;">&amp;&amp;</span> key <span style="color: #339933;">&lt;=</span> <span style="color: #ff0000;">'9'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #339933;">*</span>n <span style="color: #339933;">*=</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">;</span>
        <span style="color: #339933;">*</span>n <span style="color: #339933;">+=</span> key <span style="color: #339933;">-</span> <span style="color: #ff0000;">'0'</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>n <span style="color: #339933;">&gt;</span> max<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span>n <span style="color: #339933;">=</span> max<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>key <span style="color: #339933;">==</span> <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        keyboardHide<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>filesystemInitialised<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #993333;">char</span> buf<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">8</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #993333;">int</span> n<span style="color: #339933;">;</span>
            FILE <span style="color: #339933;">*</span> f <span style="color: #339933;">=</span> fopen<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;diceconfig.bin&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;wb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            n <span style="color: #339933;">=</span> snprintf<span style="color: #009900;">&#40;</span>buf<span style="color: #339933;">,</span> <span style="color: #0000dd;">8</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%d<span style="color: #000099; font-weight: bold;">\n</span>%d&quot;</span><span style="color: #339933;">,</span> num_sides<span style="color: #339933;">,</span> num_dice<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">8</span><span style="color: #009900;">&#41;</span> n <span style="color: #339933;">=</span> <span style="color: #0000dd;">8</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>f <span style="color: #339933;">!=</span> NULL<span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                fwrite<span style="color: #009900;">&#40;</span>buf<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> n<span style="color: #339933;">,</span> f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                fclose<span style="color: #009900;">&#40;</span>f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        cState <span style="color: #339933;">=</span> CONFIG_SELECTION<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The only other interesting function. <code>keyboardDemoInit()</code> is called once to initialise the default on-screen keyboard. <code>keyboardShow()</code> displays the keyboard and <code>keyboardUpdate()</code> returns the ASCII character that represents the key pressed. As you can see, writing the results to storage is a simple matter of calling the appropriate C I/O functions.</p>
<p>Next on the schedule is the interesting part, actually rendering something other than text on the screen.</p>
<p>Up to date source code for the project can be cloned from the <a href="http://github.com/beedee/DSDiceRoller">project site on Github</a> or if you don&#8217;t use Git, can be downloaded from <a href="http://blogs.ijw.co.nz/matthew/wp-content/uploads/2009/09/DSDiceRoller.tar.gz">here.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.ijw.co.nz/matthew/index.php/2009/09/brewing-the-nintendo-ds-from-the-comfort-of-the-home-part-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Brewing the Nintendo DS from the comfort of the home, Part 1</title>
		<link>http://blogs.ijw.co.nz/matthew/index.php/2009/07/brewing-the-nintendo-ds-from-the-comfort-of-the-home-part-1/</link>
		<comments>http://blogs.ijw.co.nz/matthew/index.php/2009/07/brewing-the-nintendo-ds-from-the-comfort-of-the-home-part-1/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 19:00:55 +0000</pubDate>
		<dc:creator>Matthew Bowra-Dean</dc:creator>
				<category><![CDATA[Personal Project]]></category>
		<category><![CDATA[dice roller]]></category>
		<category><![CDATA[ds homebrew]]></category>

		<guid isPermaLink="false">http://blogs.ijw.co.nz/matthew/?p=29</guid>
		<description><![CDATA[A little while ago I was looking to start doing some hobbyist embedded programming. I’d fallen in love with the challenges embedded platforms present in software development thanks in part to both the course I did on hardware architecture and assembly language last trimester and the contract work I’ve been doing in the uni holidays. [...]]]></description>
			<content:encoded><![CDATA[<p>A little while ago I was looking to start doing some hobbyist embedded programming. I’d fallen in love with the challenges embedded platforms present in software development thanks in part to both the course I did on hardware architecture and assembly language last trimester and the contract work I’ve been doing in the uni holidays.</p>
<p>Luckily for me the Nintendo DS has a fairly mature homebrew scene so back in April I bought the new DSi hoping all the eager software pirates out there would be quick to crack the firmware and enable the running of unofficial software on it. Unfortunately this hasn’t really happened but fortunately enough, a few <a href="http://en.wikipedia.org/wiki/Nintendo_DS_homebrew#SLOT-1_and_SLOT-2_devices" target="_blank">DS SLOT-1 device</a> manufacturers were able get versions of their devices out that would support DS homebrew on the DSi. None of the extra DSi features or power are available but it’s better than nothing.</p>
<p>So, on to the meat of the story. The Nintendo DS is a very capable little beast. It has two CPUs; an ARM7 clocked at 33MHz which is mainly used for interacting with the hardware; and an ARM9 clocked at 67MHz which does most of the processing grunt work. You also have 4MB of RAM to play with not including the VRAM and numerous caches. The DSi one ups all of this; doubling the clock speed of the processors and quadrupling the size of the RAM. Unfortunately this is unusable to homebrew developers at the moment as only the “DS mode” has been cracked meaning the clock speeds and available RAM are constrained to the DS’ capabilities.</p>
<p>Using the fantastic <a href="http://www.devkitpro.org/" target="_blank">devkitPro</a> toolchain I was quickly able to get a build environment up and going and through the tutorials <a href="http://patater.com/manual" target="_blank">here</a> and <a href="http://www.dev-scene.com/NDS/Tutorials" target="_blank">here</a> I was able to get a handle on the libnds library provided by devkit. So I’ve decided to start hacking on a dice roller. One of my loves in life is pen and paper roleplaying games and a recent game of <a href="http://www.white-wolf.com/Scion/index.php" target="_blank">Scion</a> is beginning to get out of hand with the amount of 10-sided dice I am having to roll. In the interest of having something to waffle on about and impress random strangers on the internet with I am going to chronicle my attempts here (with BSD licensed source of course). Hopefully someone gets something out of this other than me.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.ijw.co.nz/matthew/index.php/2009/07/brewing-the-nintendo-ds-from-the-comfort-of-the-home-part-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

