{"id":1454,"date":"2015-12-18T09:17:43","date_gmt":"2015-12-18T09:17:43","guid":{"rendered":"http:\/\/www.sydneysmith.com\/wordpress\/?p=1454"},"modified":"2017-04-25T14:00:36","modified_gmt":"2017-04-25T14:00:36","slug":"a-better-hp-65-emulator","status":"publish","type":"post","link":"https:\/\/www.sydneysmith.com\/wordpress\/1454\/a-better-hp-65-emulator\/","title":{"rendered":"A Better HP-65 Emulator"},"content":{"rendered":"<p>I was wrong. The HP-65 microcode available on the internet works fine. See <a href=\"http:\/\/www.sydneysmith.com\/wordpress\/1428\/hp-65-rs\/\">HP-65 R\/S<\/a>. It&#8217;s the emulators that are broken (slightly). See <a href=\"http:\/\/www.sydneysmith.com\/wordpress\/1448\/hp-65-delayed-select-group\/\">HP-65 Delayed Select Group<\/a>. Here&#8217;s how I fixed mine: <!--more--><\/p>\n<p>This will be in JavaScript for those that speak the language. The usual implementation of &#8220;delayed select group&#8221; is:<\/p>\n<pre>\r\nfunction dlyselgrp(num) { return function () {\r\n   dlygrp = num;\r\n}; }\r\n\r\nfunction goto(addr) { return function () {\r\n   if (prevCarry != 1) {\r\n      ...\r\n      romchip = dlyrom;\r\n      grp = dlygrp;\r\n   }\r\n}; }\r\n\r\nfunction jsb(addr) { return function () {\r\n   ...\r\n}; }\r\n\r\nfunction setrom(num) { return function () {\r\n   romchip = num;\r\n   dlyrom = romchip;\r\n   grp = dlygrp;\r\n}; }\r\n<\/pre>\n<p>Interestingly, I was also wrong about &#8220;jsb&#8221;. It isn&#8217;t affected at all (per this) by the &#8220;delayed select group&#8221;.<\/p>\n<p>The &#8220;delayed select group (n)&#8221; saves (n) somewhere and, later on, a &#8220;goto&#8221; or a &#8220;rom&#8221; microinstruction causes a branch which then uses the saved group (n).<\/p>\n<p>If you look at the microcode for the HP-65, you&#8217;ll almost always see a &#8220;delayed select group&#8221; followed immediately with a &#8220;goto&#8221; or a &#8220;rom&#8221; instruction. But not always.<\/p>\n<p>It had been assumed &#8211; by me, and obviously by those that did the earlier code &#8211; that &#8220;delayed select group&#8221; sets up the value, and &#8220;goto\/rom&#8221; carries it out. But that is wrong (see <a href=\"http:\/\/www.sydneysmith.com\/wordpress\/1448\/hp-65-delayed-select-group\/\">HP-65 Delayed Select Group<\/a>). Some people figured that out before me and we&#8217;re all better off as a result.<\/p>\n<p>The trick to getting the &#8220;delayed select group&#8221; microinstruction to work in those few cases when it isn&#8217;t followed by &#8220;goto\/rom&#8221;, is something like this:<\/p>\n<pre>\r\nfunction dlyselgrp(num) { return function () {\r\n   dlygrp = num;\r\n   dgs=1;\r\n}; }\r\n\r\n\/\/ and in the Control And Timing chip emulation:\r\nvar dgs = 0; \/\/ \"delayed group select\" state.\r\n             \/\/ 0=off, 1=on, 2=pending, 3=act\r\n\r\n\/\/ microcode processing loop\r\n   ...\r\n\r\n   rom[grp*2048 + romchip*256 + pc++]();\r\n   pc %= 256; \/\/ wrap back to beginning of the ROM\r\n\r\n   \/\/ delayed group select\r\n   if (dgs &gt;  0) dgs++;\r\n   if (dgs == 3) { grp=dlygrp; dgs=0; }\r\n   ...\r\n<\/pre>\n<p>The lines using the &#8220;dgs&#8221; variable are the only additions. When a &#8220;delayed group select&#8221; microinstruction is executed, it saves (n) like before; but it also sets its state to 1=on.<\/p>\n<p>After that instruction finishes, the &#8220;if (dgs &#8230;) dgs++;&#8221; increments the state to 2=pending. The next instruction executes normally.<\/p>\n<p>After that one, we do dgs++ again and get to 3=act. At that point we set the group to the saved value ((n) aka dlygrp) and turn things off again (dgs=0 (=off)).<\/p>\n<p>There are probably other ways to do the same but this one works. I&#8217;ve tried it with the original microcode and [R\/S]. I&#8217;ve tried my usual test cases (just basics: key in a digit, ENTER, key in another, +, [A] (ie 1\/x), etc).<\/p>\n<p>I&#8217;ve left the existing code alone. That was working. Some of it is probably redundant now and there may be some strange bits that got added to offset the &#8220;delayed select group&#8221; not working as expected. (There is code to trigger a group change if you set s0 to 1. That might only be there because a &#8220;1 -&gt; s0&#8221; happened to follow a &#8220;delayed select group&#8221; somewhere.)<\/p>\n<p>The same approach could be used for other emulators written in C or Java &#8211; javascript ports pretty well to those. The code is licensed under GPL so feel free to use any of the above in any other emulator.<\/p>\n<p>I&#8217;ve updated my HP65 emulator to version 1.01 (Menu \/ About), and refitted the original microcode and the above tweaks. As usual, you can access the emulator from my <a href=\"http:\/\/www.sydneysmith.com\/wordpress\/hp65\/\">HP65<\/a> page.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was wrong. The HP-65 microcode available on the internet works fine. See HP-65 R\/S. It&#8217;s the emulators that are broken (slightly). See HP-65 Delayed Select Group. Here&#8217;s how I fixed mine:<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,5,39,47,20],"tags":[37,40,32],"_links":{"self":[{"href":"https:\/\/www.sydneysmith.com\/wordpress\/wp-json\/wp\/v2\/posts\/1454"}],"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=1454"}],"version-history":[{"count":7,"href":"https:\/\/www.sydneysmith.com\/wordpress\/wp-json\/wp\/v2\/posts\/1454\/revisions"}],"predecessor-version":[{"id":1463,"href":"https:\/\/www.sydneysmith.com\/wordpress\/wp-json\/wp\/v2\/posts\/1454\/revisions\/1463"}],"wp:attachment":[{"href":"https:\/\/www.sydneysmith.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sydneysmith.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sydneysmith.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}