<?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>Tummai Games &#187; code cleanup</title>
	<atom:link href="http://www.tummaigames.com/blog/tag/code-cleanup/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tummaigames.com/blog</link>
	<description>NES homebrew! Development blog for homebrew NES games</description>
	<lastBuildDate>Tue, 16 Mar 2010 13:23:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Button Combos, Sound Engines and Scopes</title>
		<link>http://www.tummaigames.com/blog/2010/02/08/button-combos-sound-engines-and-scopes/</link>
		<comments>http://www.tummaigames.com/blog/2010/02/08/button-combos-sound-engines-and-scopes/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 17:06:51 +0000</pubDate>
		<dc:creator>Thomas</dc:creator>
				<category><![CDATA[NES]]></category>
		<category><![CDATA[code cleanup]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[scope]]></category>
		<category><![CDATA[sound engine]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.tummaigames.com/blog/?p=208</guid>
		<description><![CDATA[It&#8217;s been a while since I posted here.  I took a break from my homebrew to write some tutorials on how to code a sound engine for the NES.  You can find them here: NES Sound Tutorials.
After a long break I finally got back to some homebrew coding.  I worked in 6502 [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a while since I posted here.  I took a break from my homebrew to write some tutorials on how to code a <strong>sound engine</strong> for the <strong>NES</strong>.  You can find them here: <a href="http://nintendoage.com/forum/messageview.cfm?catid=22&#038;threadid=7155">NES Sound Tutorials</a>.</p>
<p>After a long break I finally got back to some homebrew coding.  I worked in <strong>6502</strong> for a few hours today.  I didn&#8217;t work on <a href="http://www.tummaigames.com/blog/category/development/nes/explorer/">Explorer</a>, but rather a different game project that is less ambitious.  I&#8217;ll introduce the game properly in a future post.</p>
<h2>What I Did Today</h2>
<h3>Button Combos</h3>
<p>One thing that I needed for the new game project was a way to detect <strong>button combinations</strong> like up+A or right+B &#8211; the kind that you use to select plays in TecmoBowl.  It was a little tricky to pull off.  For one thing, if the player presses something like up+left+A, I need to resolve that to either up+A or left+A.  I need to work that out for all possible combinations of button presses.  This includes taking care of cases where A+B are pressed at the same time.</p>
<p>Another issue is with <strong>holding buttons</strong>.  If the player holds B down and makes circles around the dpad, I only want it to catch the first combination.  To do two combos in a row, they should release the B button and press it again.  But what if the player hits up+B, and then while still holding B they press A+left?  Does the holding of B stop the 2nd combination from being picked up or not?  There are a lot of possible cases since the controller reading routine is called 60 times per second.  The player can&#8217;t control their input on a frame-by-frame scale.  Sliding from B to A might very well contain several frames (fraction of a second) where both buttons are held down before the B button is released.  Not impossible to solve, but it wasn&#8217;t as straightforward as I had expected.</p>
<h3>Sound Engine</h3>
<p>Another thing I did today was port my sound engine to <a href="http://www.cc65.org/doc/ca65.html">ca65</a>.  I wrote my <a href="http://nintendoage.com/forum/messageview.cfm?catid=22&#038;threadid=7155">NES sound tutorials</a> for the <strong>NESASM</strong> assembler.  My tutorials are a continuation of another set of tutorials which use NESASM and I wanted to stay consistent.  Plus most new nesdevers start with NESASM as it is very newbie-friendly.</p>
<p>My tutorial sound engine was an improvement over the one I had previously written in ca65, so I wanted to replace the old with the new.  So I spent some time today porting my sound engine from NESASM to ca65.  I was sure everything was going to break as soon as I got all the syntax converted.  But it didn&#8217;t!  Once I got it to assemble it worked perfectly on the first try!  That was a relief.</p>
<h3>Scopes</h3>
<p>ca65 lets you define <strong>scopes</strong>.  Scopes enable you to keep parts of your program hidden from the other parts of your program.  It&#8217;s good to separate modules, especially in large programs, so that you don&#8217;t have variable conflicts and the like.  These bugs are the hardest to trace in assembly language.</p>
<p>I didn&#8217;t use scopes before because I didn&#8217;t really know how.  But I found a great thread on <a href="http://nesdev.parodius.com/bbs/viewtopic.php?t=5481">nesdev about ca65 scopes</a> that told me how to do it, so I spent some time today separating my sound and controller reading modules from the main program.  Everything feels much &#8230; cleaner now.  Like washing your hands after working outside on a summer day.  I can physically feel how much easier things are going to be in the future for me with scopes.</p>
<h2>New NES</h2>
<p>I bought an NES!  Ordered it on Yahoo Auctions on Saturday and it arrived at my house on Sunday!  All I need now is a <a href="http://www.retrousb.com/product_info.php?cPath=24&#038;products_id=34">PowerPak</a> and I can start testing my programs on real hardware.  I can&#8217;t wait!</p>
<p>I&#8217;m too lazy to upload pictures here, but you can see the ones I posted to twitter here:</p>
<p><a href="http://twitpic.com/11ucz8">My new NES 1</a><br />
<a href="http://twitpic.com/11udov">My new NES 2</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tummaigames.com/blog/2010/02/08/button-combos-sound-engines-and-scopes/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
