{"id":190,"date":"2022-02-10T21:06:50","date_gmt":"2022-02-11T02:06:50","guid":{"rendered":"https:\/\/paulcarbone.com\/50days\/?p=190"},"modified":"2022-02-10T23:22:32","modified_gmt":"2022-02-11T04:22:32","slug":"day-14-keep-on-going","status":"publish","type":"post","link":"https:\/\/paulcarbone.com\/50days\/2022\/02\/10\/day-14-keep-on-going\/","title":{"rendered":"Day 14: Keep on going"},"content":{"rendered":"<p>So, for whatever reason, I can&#8217;t use include files, which means I just have to have everything in one large assembly file.\u00a0 I guess that&#8217;s not the end of the world.<\/p>\n<p>Lets start by trying to set up the timer and output a simple message.<\/p>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><div><code class=\"hljs language-php\">TIMER0\tEQU\t<span class=\"hljs-number\">40<\/span>H\t\t;LOCATION OF THE FIRST TIMER\nTWORD0\tEQU\t<span class=\"hljs-number\">00010101<\/span>B\t;TIMER WORD (A <span class=\"hljs-keyword\">STATIC<\/span> VALUE <span class=\"hljs-keyword\">FOR<\/span> NOW)\nTTCON0\tEQU\t<span class=\"hljs-number\">01<\/span>H\t\t;TIMER CONSTANT (<span class=\"hljs-keyword\">STATIC<\/span> <span class=\"hljs-keyword\">FOR<\/span> NOW)\n; LETS FIRST <span class=\"hljs-keyword\">TRY<\/span> TO SET UP THE TIMER WITH A SINGLE, HAND BUILT CONTROL WORD\n\n\tLD\tA,TWORD0\n\tLD\tC,TIMER0\n\tOUT\t(C),A\n\tLD\tA,TTCON0\n\tOUT\t(C),A<\/code><\/div><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>OK, that worked!<\/p>\n\n\n\n<p>Next I&#8217;d like to try to build that word in code, using a number of variables.  Up until now we&#8217;ve mostly been dealing with symbols, using the EQU pseudo-0p.  I think if we want to define single byte storage areas, we can use DB, and give those locations a label. <\/p>\n\n\n\n<p>To build up a control byte from a number of single bits, we have to shift each bit into it&#8217;s correct place, then OR it with the control byte we&#8217;re building.  Right now, I&#8217;m thinking of this where each &#8216;bit&#8217; is actually a byte that&#8217;s 1 or 0, and needs to be left shifted by the number of times as it&#8217;s position is in the byte. <br>Since we&#8217;re dealing with bytes that should only be a 0 or a 1, we don&#8217;t need to worry about shift vs rotate vs carry vs whatever makes a bit pop back onto the other end.  So lets use&#8230;. RLCA &#8211; Rotate Left Circular Accumulator.  This will put a bit into the carry flag, but won&#8217;t put the carry flag back into the byte. <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/www.chebucto.ns.ca\/~af380\/Z80-RLCA.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p>So we have to:<br>1) load each byte into the Accumulator<br>2) perform some number of RLCAs<br>3) load the control word into another register<br>4) perform an OR with that register<br>5) put the result from the accumulator back into that other register<br>6) load the next byte into the Accumulator and keep going <\/p>\n\n\n\n<p>So I tried this little snip of code<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><div><code class=\"hljs language-php\">;LETS <span class=\"hljs-keyword\">TRY<\/span> TO BUILD A CONTROL WORD WITH LEFT SHIFTS\n\tLD\tA,TB7IN\n\tRLCA\n\tRLCA\n\tRLCA\n\tRLCA\n\tRLCA\n\tLD\tB,TWRD0\n\t<span class=\"hljs-keyword\">OR<\/span>\tB\n\tLD\tB,A\n\n; LETS OUTPUT THIS ONTO THE CONSOLE.  IT SHOULD BE AN @ CHARACTER, SINCE \n; WE SHIFTED <span class=\"hljs-number\">1<\/span> OVER <span class=\"hljs-number\">5<\/span> TIMES TO THE <span class=\"hljs-number\">64<\/span><span class=\"hljs-string\">'S BIT.\n<\/span><\/code><\/div><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>With these DB entries below<\/p>\n\n\n<pre class=\"wp-block-code\"><div><code class=\"hljs\">;VARIABLE STORAGE\n;TIMER CONTROL WORD BITS (EVEN THOGH THESE ARE REALLY BYTES.. THIS IS DUMB BUT WHATEVES)\nTB0CV\tDB\t1\t;CONTOL WORD OR VECTOR ADDRESS - 1 BYTE IS CTRL WRD\nTB1RS\tDB\t0\t;RESET\nTB2TC\tDB\t1\t;IS NEXT BYTE TIME CONSTANT\nTB3TT\tDB\t0\t;TIMER TRIGGER - 0 STARTS RIGHT AWAY\nTB4ES\tDB\t1\t;EDGE SELECTION - 1 RISING\nTB5PS\tDB\t0\t;PRESCALER - 0 = 16, 1 = 256\nTB6MD\tDB\t0\t;MODE - 0 = TIMER, 1 = COUNTER\nTB7IN\tDB\t1\t;INTERRUPT - 0 DISABLES, 1 ENABLES \nTWRD0\tDB\t00\t;THE WHOLE WORD\t\n<\/code><\/div><\/pre>\n\n\n<p>And I get errors on both lines that reference those memory locations:<br>DAY142.Z80 &#8211; Byte Out of Range Line 00039<br>LD A,TB7IN<br>DAY142.Z80 &#8211; Byte Out of Range Line 00045<br>LD B,TWRD0<\/p>\n\n\n\n<p>Lets find out why:  It&#8217;s been really useful to run the compiler with the \/F option, which forces the creation of a listing.  That way, we can see what opcodes were used, and see where the difference between what we thought should happen, and reality. <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><div><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">DAY142<\/span><span class=\"hljs-selector-class\">.Z80<\/span> <span class=\"hljs-selector-tag\">-<\/span> <span class=\"hljs-selector-tag\">Byte<\/span> <span class=\"hljs-selector-tag\">Out<\/span> <span class=\"hljs-selector-tag\">of<\/span> <span class=\"hljs-selector-tag\">Range<\/span> <span class=\"hljs-selector-tag\">Line<\/span> 00039\n\t<span class=\"hljs-selector-tag\">LD<\/span>\t<span class=\"hljs-selector-tag\">A<\/span>,<span class=\"hljs-selector-tag\">TB7IN<\/span>\n   39 011<span class=\"hljs-selector-tag\">E<\/span>  3<span class=\"hljs-selector-tag\">E<\/span> 5<span class=\"hljs-selector-tag\">A<\/span>       \t<span class=\"hljs-selector-tag\">LD<\/span>\t<span class=\"hljs-selector-tag\">A<\/span>,<span class=\"hljs-selector-tag\">TB7IN<\/span>\n   40 0120  07          \t<span class=\"hljs-selector-tag\">RLCA<\/span>\n   41 0121  07          \t<span class=\"hljs-selector-tag\">RLCA<\/span>\n   42 0122  07          \t<span class=\"hljs-selector-tag\">RLCA<\/span>\n   43 0123  07          \t<span class=\"hljs-selector-tag\">RLCA<\/span>\n   44 0124  07          \t<span class=\"hljs-selector-tag\">RLCA<\/span>\n<span class=\"hljs-selector-tag\">DAY142<\/span><span class=\"hljs-selector-class\">.Z80<\/span> <span class=\"hljs-selector-tag\">-<\/span> <span class=\"hljs-selector-tag\">Byte<\/span> <span class=\"hljs-selector-tag\">Out<\/span> <span class=\"hljs-selector-tag\">of<\/span> <span class=\"hljs-selector-tag\">Range<\/span> <span class=\"hljs-selector-tag\">Line<\/span> 00045\n\t<span class=\"hljs-selector-tag\">LD<\/span>\t<span class=\"hljs-selector-tag\">B<\/span>,<span class=\"hljs-selector-tag\">TWRD0<\/span>\n   45 0125  06 5<span class=\"hljs-selector-tag\">B<\/span>       \t<span class=\"hljs-selector-tag\">LD<\/span>\t<span class=\"hljs-selector-tag\">B<\/span>,<span class=\"hljs-selector-tag\">TWRD0<\/span><\/code><\/div><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>So the LD opcodes were 5A  &amp; 06, For LD A, I think I wanted opcode 3A, which expects a 16 bit address. Did I need to put the name of the variable in parentheses ? I assumed it would have figured that out for me.  OK! Using parentheses for the first entry worked, and it chose the right opcode.  It didn&#8217;t chose the right opcode for B, <em>because there isn&#8217;t one. <\/em><\/p>\n\n\n\n<p>Not all registers are created equal.  The A register can be loaded from any 16 bit address in memory, while the other registers have more limited instructions.  If we want to load the B register from memory, we must first place that address in the HL register.  So lets try that. <\/p>\n\n\n\n<p>OK! Well that compiled without error, but I don&#8217;t know if it <em>worked<\/em> &#8211; More tomorrow.<\/p>\n\n\n\n<p>What I&#8217;m still not 100% sure on is when to use parentheses with memory location labels, and when not to.  I should really investigate the various flavors of LD tomorrow. <\/p>\n\n\n\n<p>Also, in retrospect, I don&#8217;t think we actually need separate areas in memory to hold the individual bits.  I think we&#8217;re better off just writing routines that can set or clear each bit in the control word. <br><br>And&#8230; Looking at the list file, we can see that LD A,(LOCATION) loads the contents of LOCATION into A, while LD HL,LOCATION loads the <em>address<\/em> of LOCATIOn into HL. <\/p>\n\n\n<pre class=\"wp-block-code\"><div><code class=\"hljs\">   39 011E  3A 015D     \tLD\tA,(TB7IN)\n   45 0126  21 015E     \tLD\tHL,TWRD0\n\n   96 015D  01          TB7IN\tDB\t1\t;INTERRUPT - 0 DISABLES, 1 ENABLES \n   97 015E  00          TWRD0\tDB\t00\t;THE WHOLE WORD\t\n<\/code><\/div><\/pre>\n\n\n<p>  <\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, for whatever reason, I can&#8217;t use include files, which means I just have to have everything in one large assembly file.\u00a0 I guess that&#8217;s not the end of the world. Lets start by trying to set up the timer &hellip; <a href=\"https:\/\/paulcarbone.com\/50days\/2022\/02\/10\/day-14-keep-on-going\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[5],"tags":[],"class_list":["post-190","post","type-post","status-publish","format-standard","hentry","category-blinkinglights"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/paulcarbone.com\/50days\/wp-json\/wp\/v2\/posts\/190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/paulcarbone.com\/50days\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/paulcarbone.com\/50days\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/paulcarbone.com\/50days\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/paulcarbone.com\/50days\/wp-json\/wp\/v2\/comments?post=190"}],"version-history":[{"count":8,"href":"https:\/\/paulcarbone.com\/50days\/wp-json\/wp\/v2\/posts\/190\/revisions"}],"predecessor-version":[{"id":198,"href":"https:\/\/paulcarbone.com\/50days\/wp-json\/wp\/v2\/posts\/190\/revisions\/198"}],"wp:attachment":[{"href":"https:\/\/paulcarbone.com\/50days\/wp-json\/wp\/v2\/media?parent=190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/paulcarbone.com\/50days\/wp-json\/wp\/v2\/categories?post=190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/paulcarbone.com\/50days\/wp-json\/wp\/v2\/tags?post=190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}