<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>NES (6502) Assembler</title>
		<link>http://www.allegro.cc/forums/view/593971</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Wed, 14 Nov 2007 19:51:24 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>So, I&#39;m trying to read controller data. I have written a little function thing to handle all this, but uhh... I get problems when I try to program responses for buttons other than the directional buttons.</p><p>This is what I&#39;ve got:
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>controller equ $<span class="n">4016</span> <span class="k2">;</span> controller <span class="n">1</span> is at <span class="k1">register</span> $<span class="n">4016</span></td></tr><tr><td class="number">2</td><td>&#160;</td></tr><tr><td class="number">3</td><td><span class="k2">;</span> <span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span> reads the controller <span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span></td></tr><tr><td class="number">4</td><td>&#160;</td></tr><tr><td class="number">5</td><td>readController:</td></tr><tr><td class="number">6</td><td>        <span class="k2">;</span> <span class="k1">this</span> bit is required, just ignore <span class="k1">if</span> you wish</td></tr><tr><td class="number">7</td><td>  lda #<span class="n">1</span>             <span class="k2">;</span> strobe controller</td></tr><tr><td class="number">8</td><td>  sta controller</td></tr><tr><td class="number">9</td><td>  lda #<span class="n">0</span>             <span class="k2">;</span> reset controller</td></tr><tr><td class="number">10</td><td>  sta controller</td></tr><tr><td class="number">11</td><td>        <span class="k2">;</span> end that bit</td></tr><tr><td class="number">12</td><td>&#160;</td></tr><tr><td class="number">13</td><td>&#160;</td></tr><tr><td class="number">14</td><td>  jsr buttonA     <span class="k2">;</span> check buttons by jumping to the sub-routine</td></tr><tr><td class="number">15</td><td>  jsr buttonB</td></tr><tr><td class="number">16</td><td>  jsr buttonSelect</td></tr><tr><td class="number">17</td><td>  jsr buttonStart</td></tr><tr><td class="number">18</td><td>  jsr buttonUp</td></tr><tr><td class="number">19</td><td>  jsr buttonDown</td></tr><tr><td class="number">20</td><td>  jsr buttonLeft</td></tr><tr><td class="number">21</td><td>  jsr buttonRight</td></tr><tr><td class="number">22</td><td>  </td></tr><tr><td class="number">23</td><td>  rts                <span class="k2">;</span> <span class="k1">return</span> from sub-routine</td></tr><tr><td class="number">24</td><td>&#160;</td></tr><tr><td class="number">25</td><td><span class="k2">;</span> <span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span></td></tr></tbody></table></div></div><p>

The buttons on the NES controller are read one a time, in that order. And then the button* sub-routines are all basically the same, like this:
</p><div class="source-code snippet"><div class="inner"><pre>button<span class="k3">*</span><span class="k2">:</span>
        <span class="k2">;</span> check <span class="k1">if</span> button <span class="k3">*</span> is pressed
  lda controller       <span class="k2">;</span> load button state into <span class="k1">register</span> A
  <span class="k1">and</span> #<span class="n">1</span>               <span class="k2">;</span> bitwise AND with <span class="n">1</span>
  beq callbackReturn   <span class="k2">;</span> <span class="k1">if</span> the AND was <span class="k1">true</span>, branch to callbackReturn
                             <span class="k2">;</span> callbackReturn jumps to an <span class="s">"rts"</span> command
                             <span class="k2">;</span> in other words, it<span class="s">'s equivalent to a return in C</span>
<span class="s">  </span>
<span class="s">  ; do whatever</span>
<span class="s"></span>
<span class="s">  rts                  ; return</span>
</pre></div></div><p>
Now, whenever that <tt>; do whatever</tt> (besides the four directional buttons) involves the loading of a non-immediate value (ie- from a label or from a specific register) or the storing of a value, it sort of freezes the game (or at least prevents the working input from happening).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jeff Bernard)</author>
		<pubDate>Tue, 13 Nov 2007 08:29:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Umm.. your method looks overcomplicated but before i can figure out what might be wrong i need some more details.</p><p>So.. you say that controller is register-mapped to address $4016. I assume that each button is a &#39;bit&#39; (how is the signal stored) 8 buttons = 8-bits?</p><p>Otherwise i don&#39;t see anything inherently wrong. You don&#39;t touch the stack, and you have a simple branch..</p><p>Actually.. how far is the branch (in memory distance)? Relative addressing is only 8-bit IIRC.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goalie Ca)</author>
		<pubDate>Tue, 13 Nov 2007 08:41:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It may be overly complicated, but I don&#39;t currently know of any better ways to do it. I&#39;ve seen some examples of a loop that reads all 8 buttons, but I have no idea how it works, so I&#39;m staying away from it for now.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
So.. you say that controller is register-mapped to address $4016. I assume that each button is a &#39;bit&#39; (how is the signal stored) 8 buttons = 8-bits?
</p></div></div><p>
As far as I understand, yes that&#39;s how it works. It seems to only read one bit at a time, each time that register is read. I&#39;ve tried bitwise ANDing it, having only read it once, using an 8 bit binary number, but it doesn&#39;t work, so it has to be read 8 times.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
how far is the branch (in memory distance)?
</p></div></div><p>
I&#39;m new to assembly, so let me know if this was not what you meant. It&#39;s not very deep, it&#39;s within the &quot;main loop&quot;, so it only goes 2 branches deep when running.
</p><div class="source-code snippet"><div class="inner"><pre>gameLoop:
  <span class="k2">;</span> check <span class="k1">if</span> in VBlank

  <span class="k2">;</span> draw sprite

  jsr readController
  jmp gameLoop
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jeff Bernard)</author>
		<pubDate>Tue, 13 Nov 2007 12:44:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I can&#39;t recall the 6502 ISA off the top of my head but why not something like this</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="k2">;</span>possibly the wrong numbers</td></tr><tr><td class="number">2</td><td><span class="k2">;</span>masks <span class="k1">for</span> each button</td></tr><tr><td class="number">3</td><td>buttonA  equ #<span class="n">1</span></td></tr><tr><td class="number">4</td><td>buttonB  equ #<span class="n">2</span></td></tr><tr><td class="number">5</td><td>buttonSt  equ #<span class="n">4</span></td></tr><tr><td class="number">6</td><td>buttonSe  equ #<span class="n">8</span></td></tr><tr><td class="number">7</td><td>buttonUp  equ #<span class="n">16</span></td></tr><tr><td class="number">8</td><td>buttonDo  equ #<span class="n">32</span></td></tr><tr><td class="number">9</td><td>buttonLe  equ #<span class="n">64</span></td></tr><tr><td class="number">10</td><td>buttonRi  equ #<span class="n">128</span></td></tr><tr><td class="number">11</td><td>&#160;</td></tr><tr><td class="number">12</td><td>&#160;</td></tr><tr><td class="number">13</td><td>readController:</td></tr><tr><td class="number">14</td><td>&#160;</td></tr><tr><td class="number">15</td><td><span class="k2">;</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_642.html" target="_blank">read</a> value from controller <span class="k2">(</span>make sure these are right<span class="k3">!</span><span class="k2">)</span></td></tr><tr><td class="number">16</td><td>  lda #<span class="n">1</span>             <span class="k2">;</span> strobe controller</td></tr><tr><td class="number">17</td><td>  sta controller</td></tr><tr><td class="number">18</td><td>  lda #<span class="n">0</span>             <span class="k2">;</span> reset controller</td></tr><tr><td class="number">19</td><td>  sta controller</td></tr><tr><td class="number">20</td><td>        <span class="k2">;</span> end that bit</td></tr><tr><td class="number">21</td><td>&#160;</td></tr><tr><td class="number">22</td><td><span class="k2">;</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_642.html" target="_blank">read</a> value from controller</td></tr><tr><td class="number">23</td><td>        lda controller</td></tr><tr><td class="number">24</td><td>&#160;</td></tr><tr><td class="number">25</td><td><span class="k2">;</span>store <span class="k1">this</span> value somewhere convenient.</td></tr><tr><td class="number">26</td><td>        sta controller_status</td></tr><tr><td class="number">27</td><td>&#160;</td></tr><tr><td class="number">28</td><td><span class="k2">;</span>check <span class="k1">if</span> button A pushed</td></tr><tr><td class="number">29</td><td>        lda controller_status</td></tr><tr><td class="number">30</td><td>        <span class="k1">and</span> buttonA</td></tr><tr><td class="number">31</td><td>        beq butB <span class="k2">;</span><span class="k1">if</span> button a is <span class="k1">not</span> pressed.. <span class="k1">and</span> will <span class="k1">return</span> <span class="n">0</span>. beq jumps when z flag is zero.. correct<span class="k3">!</span>?</td></tr><tr><td class="number">32</td><td>        jsr buttonA_action</td></tr><tr><td class="number">33</td><td>butB: </td></tr><tr><td class="number">34</td><td>        lda controller_status</td></tr><tr><td class="number">35</td><td>        <span class="k1">and</span> buttonB</td></tr><tr><td class="number">36</td><td>        beq butC</td></tr><tr><td class="number">37</td><td>        jsr buttonB_action</td></tr><tr><td class="number">38</td><td>butC: <span class="k2">;</span>.. <span class="k1">and</span> so on</td></tr><tr><td class="number">39</td><td>&#160;</td></tr><tr><td class="number">40</td><td>        rts <span class="k2">;</span>end of function<span class="k3">!</span></td></tr></tbody></table></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goalie Ca)</author>
		<pubDate>Tue, 13 Nov 2007 13:10:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>That&#39;s what I was talking about by only reading the controller once and then ANDing it with a single binary number. Unfortunately, it does not work.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jeff Bernard)</author>
		<pubDate>Tue, 13 Nov 2007 13:38:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If it doesn&#39;t work then maybe you should read the documents and open up your debugger. Test the simplest of cases.. track the stack. check out what happens when you hit that instruction. It is quite possibly you are overwriting your own instructions or perhaps your stack is screwed up.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goalie Ca)</author>
		<pubDate>Wed, 14 Nov 2007 13:10:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>ah 6502, takes me back, like Goalie Ca said, it may be your branch distance.<br />I think that you can branch back 127 bytes and forward 128.</p><p>To keep jumps small it may be better doing it like this :-</p><div class="source-code snippet"><div class="inner"><pre>   bne dontreturn
   rts
dontreturn: ...
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (kikabo)</author>
		<pubDate>Wed, 14 Nov 2007 18:21:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
like Goalie Ca said, it may be your branch distance.<br />I think that you can branch back 127 bytes and forward 128.
</p></div></div><p>
That&#39;s JR (jump relative), not JSR (jump to subroutine). JSR takes an absolute, 16bit, address.</p><p>I think the problem is most likely that you&#39;ve misunderstood the NES specs, but I don&#39;t know them off hand. Maybe I can help more at lunchtime...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Harte)</author>
		<pubDate>Wed, 14 Nov 2007 18:30:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>eh, bne is a branch which is 8 bit, jump is 16 bit yes, but the potential problem is that there is a &quot;beq callbackReturn&quot;, callbackReturn isn&#39;t listed and could easily be beyond 128 bytes away from the branch.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (kikabo)</author>
		<pubDate>Wed, 14 Nov 2007 18:44:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
eh, bne is a branch which is 8 bit, jump is 16 bit yes, but the potential problem is that there is a &quot;beq callbackReturn&quot;
</p></div></div><p>
I didn&#39;t spot the BEQ. I was looking at the JSRs.</p><p>Besides that, your code seems to be compliant to the docs <a href="http://fms.komkon.org/EMUL8/NES.html#LABI">here</a>. Does it work if you change it to the code below?
</p><div class="source-code snippet"><div class="inner"><pre>button<span class="k3">*</span><span class="k2">:</span>
        <span class="k2">;</span> check <span class="k1">if</span> button <span class="k3">*</span> is pressed
  lda controller       <span class="k2">;</span> load button state into <span class="k1">register</span> A
  <span class="k1">and</span> #<span class="n">1</span>               <span class="k2">;</span> bitwise AND with <span class="n">1</span>
  bne .noReturnNow  <span class="k2">;</span> <span class="k1">if</span> the AND was <span class="k1">true</span>, avoid the rts that comes next

  rts
.noReturnNow
  
  <span class="k2">;</span> <span class="k1">do</span> whatever

  rts                  <span class="k2">;</span> <span class="k1">return</span>
</pre></div></div><p>
I&#39;m not sure if it&#39;s a universal trait of 6502 assemblers, but the 65SC02 assembler I used to use treated labels with a dot at the start of their name as local labels, i.e. they&#39;re accessible only between one real label (such as buttonA) and the next (presumably buttonB). So it&#39;s safe to use them in macros, and have a different .noReturnNow for each of your button functions.</p><p>Also, if you want to save a cycle, try this:
</p><div class="source-code snippet"><div class="inner"><pre>button<span class="k3">*</span><span class="k2">:</span>
        <span class="k2">;</span> check <span class="k1">if</span> button <span class="k3">*</span> is pressed
  lda controller       <span class="k2">;</span> load button state into <span class="k1">register</span> A
  lsr               <span class="k2">;</span> shift lowest bit into the carry <span class="k1">register</span>
  bcs .noReturnNow  <span class="k2">;</span> <span class="k1">if</span> the AND was <span class="k1">true</span>, branch to callbackReturn
                             <span class="k2">;</span> callbackReturn jumps to an <span class="s">"rts"</span> command
                             <span class="k2">;</span> in other words, it<span class="s">'s equivalent to a return in C</span>
<span class="s"></span>
<span class="s">  rts</span>
<span class="s">.noReturnNow</span>
<span class="s">  </span>
<span class="s">  ; do whatever</span>
<span class="s"></span>
<span class="s">  rts                  ; return</span>
</pre></div></div><p>
It&#39;s not very helpful here, but if you had an 8bit byte and wanted to do different things depending on the status of individual bits then often the fastest way to decompose it is to shift each bit in turn into carry.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Harte)</author>
		<pubDate>Wed, 14 Nov 2007 19:51:24 +0000</pubDate>
	</item>
</rss>
