{"id":1990,"date":"2018-03-02T05:22:18","date_gmt":"2018-03-02T05:22:18","guid":{"rendered":"http:\/\/www.sydneysmith.com\/wordpress\/?p=1990"},"modified":"2018-03-02T05:37:59","modified_gmt":"2018-03-02T05:37:59","slug":"hp-21-keypress","status":"publish","type":"post","link":"https:\/\/www.sydneysmith.com\/wordpress\/1990\/hp-21-keypress\/","title":{"rendered":"HP-21 Keypress"},"content":{"rendered":"<p>The HP-21 keypress routine is incredibly simple. It looks like<!--more-->:<\/p>\n<p><code><\/p>\n<pre>\r\n00400: display off                         ;\r\n00401: p <- 0                              ;\r\n00402: 0 -> a[p]                           ; A=0000FFFFFFFFF0\r\n00403: keys -> rom address                 ;\r\n<\/pre>\n<p><\/code><\/p>\n<p>It really just does a goto based on which key was pressed.<\/p>\n<p>The digit keys are, as usual, cleverly arranged and coded to put the correct value in A[0]. You can see from <a href=\"http:\/\/www.sydneysmith.com\/wordpress\/1987\/hp-21-scan-codes\/\">hp-21-scan-codes<\/a> that &#8216;7&#8217;, &#8216;8&#8217; and &#8216;9&#8217; have key scancodes of 0x63, 0x62 and 0x61; and that &#8216;4&#8217;, &#8216;5&#8217;, &#8216;6&#8217; and &#8216;1&#8217;, &#8216;2&#8217;, &#8216;3&#8217; follow the pattern with 0xa3, 0xa2, 0xa1, 0x73, 0x72 and 0x71. &#8216;0&#8217; has a keycode of 0x93 which also fits into the pattern.<\/p>\n<p>The digit scancodes translate into octal values of 0143, 0142, 0141; 0243, 0242, 0241; 0163, 0162, 0161; and 0223. The instruction at 00403 above effectively adds 0400 to each of these values and does a goto. You get:<\/p>\n<p><code><\/p>\n<pre>\r\nkey9: 00541: a + 1 -> a[p]\r\nkey8: 00542: a + 1 -> a[p]\r\nkey7: 00543: if n\/c go to  0640\r\n      00640: a + 1 -> a[p]\r\nkey6: 00641: a + 1 -> a[p]\r\nkey5: 00642: a + 1 -> a[p]\r\nkey4: 00643: if n\/c go to  0560\r\n      00560: a + 1 -> a[p]\r\nkey3: 00561: a + 1 -> a[p]\r\nkey2: 00562: a + 1 -> a[p]\r\nkey1: 00563: if n\/c go to  0730\r\n      00730: a + 1 -> a[p]\r\n      00731: return\r\nkey0: 00623: return\r\n<\/pre>\n<p><\/code><br \/>\nThe microcode puts 0-9 in A[0] to match the key (&#8216;0&#8217;-&#8216;9&#8217;) that was pressed. It is quite elegant and is a very tight way to implement 10 keys in 14 microinstructions total.<\/p>\n<p>This same code is used whenever any of the digit keys are pressed. Even if you pressed the &#8216;g&#8217; (blue, function) key beforehand or DSP or DSP and dot. Very efficient, and used for all cases.<\/p>\n<p>Here&#8217;s what it looks like when you press &#8216;7&#8217;:<br \/>\n<code><\/p>\n<pre>\r\nC:\\Test\\hp21w>hp21w -d\r\n\r\nHP21w Ver 0.01\r\nCopyright 2018 Greg Sydney-Smith\r\n\r\nCPU speed is 200 kHz\r\n\r\n>>> g,403\r\nBreakpoint 1 at 00403\r\n>>> ts200\r\n00403: keys -> rom address                 ;\r\n00543: if n\/c goto 0240                    ;\r\n00640: a + 1 -> a[p]                       ; A=0000FFFFFFFFF1\r\n00641: a + 1 -> a[p]                       ; A=0000FFFFFFFFF2\r\n00642: a + 1 -> a[p]                       ; A=0000FFFFFFFFF3\r\n00643: if n\/c goto 0160                    ;\r\n00560: a + 1 -> a[p]                       ; A=0000FFFFFFFFF4\r\n00561: a + 1 -> a[p]                       ; A=0000FFFFFFFFF5\r\n00562: a + 1 -> a[p]                       ; A=0000FFFFFFFFF6\r\n00563: if n\/c goto 0330                    ;\r\n00730: a + 1 -> a[p]                       ; A=0000FFFFFFFFF7\r\n00731: return                              ;\r\n00141: ...\r\n<\/pre>\n<p><\/code><br \/>\nThe return takes us back to 00141 which suggests a jsb at 00140, which is the case. The <a href=\"http:\/\/www.sydneysmith.com\/wordpress\/1964\/hp-21-wait-loop\/\">HP-21 wait loop<\/a> exists within a subroutine. I haven&#8217;t seen that done before, but the Woodstock CPU used in the HP-21 has a larger subroutine stack than its predecessors, so that might be why they considered it now.<\/p>\n<p>This is part of the <a href=\"http:\/\/www.sydneysmith.com\/wordpress\/hp21-main\/\">HP-21 topic<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The HP-21 keypress routine is incredibly simple. It looks like<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,5,68],"tags":[],"_links":{"self":[{"href":"https:\/\/www.sydneysmith.com\/wordpress\/wp-json\/wp\/v2\/posts\/1990"}],"collection":[{"href":"https:\/\/www.sydneysmith.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sydneysmith.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sydneysmith.com\/wordpress\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sydneysmith.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1990"}],"version-history":[{"count":2,"href":"https:\/\/www.sydneysmith.com\/wordpress\/wp-json\/wp\/v2\/posts\/1990\/revisions"}],"predecessor-version":[{"id":1992,"href":"https:\/\/www.sydneysmith.com\/wordpress\/wp-json\/wp\/v2\/posts\/1990\/revisions\/1992"}],"wp:attachment":[{"href":"https:\/\/www.sydneysmith.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sydneysmith.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sydneysmith.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}