{"id":1423,"date":"2015-12-12T22:21:19","date_gmt":"2015-12-12T22:21:19","guid":{"rendered":"http:\/\/www.sydneysmith.com\/wordpress\/?p=1423"},"modified":"2017-05-14T00:43:26","modified_gmt":"2017-05-14T00:43:26","slug":"hp-65-rtn","status":"publish","type":"post","link":"https:\/\/www.sydneysmith.com\/wordpress\/1423\/hp-65-rtn\/","title":{"rendered":"HP-65 RTN"},"content":{"rendered":"<p>The RTN key does a return from a subroutine. It also stops a main program and, if nothing&#8217;s running at all, it will take you to the &#8220;top of memory marker&#8221; (&#8220;00 00&#8221; in w\/prgm mode).<\/p>\n<p>Here&#8217;s how it does all that:<!--more--><\/p>\n<p>00752 &#8211; 01365. Get a step and switch(prog_code)<br \/>\n01052 &#8211; 02351. switch again but with access to rom 4 now<br \/>\n02052 &#8211; 02223. Figure out it is a RTN and need to run it<br \/>\n02240 &#8211; 02247. Which case is it?<br \/>\n02334 &#8211; 02336. In a main program.<\/p>\n<pre>\r\n00752 pointer advance\r\n00753 disptoggle\r\n00754 c &lt;-&gt; m            ; C= 10000000000221 M =00000000000000\r\n00755 shiftl a[w]        ; A= 00000009990000\r\n00756 0 -&gt; s11           ; S= ........8...\r\n00757 0 -&gt; f5\r\n00760 if s11 = 0\r\n00761 goto 00757\r\n00757 0 -&gt; f5            ; S= ........8..b\r\n00760 if s11 = 0\r\n00761 goto 00757\r\n00762 0 -&gt; f5\r\n00763 rom 2\r\n01364 buffer -&gt; rom address\r\n01365 goto 01000\r\n\r\n01052 goto 01347         ; [RTN]\r\n01347 rom 4\r\n02350 buffer -&gt; rom address\r\n02351 goto 02000\r\n\r\n02052 goto 02164\r\n02164 p - 1 -&gt; p         ; P= 11\r\n02165 p - 1 -&gt; p         ; P= 10\r\n02166 p - 1 -&gt; p         ; P= 9\r\n02167 if p # 8\r\n02170 goto 02305\r\n02305 if c[m] = 0\r\n02306 goto 02220\r\n02220 if p # 9\r\n02221 goto 02120\r\n02222 if s3 = 0          ; if not just keying into prgm mem\r\n02223 goto 02240         ; go run it\r\n;\r\n; RTN0:\r\n; Do a RTN step\r\n02240 if s8 = 0          ; if not running\r\n02241 goto 02332         ; go position to \"00 00\" instead\r\n02242 if c[s] = 0        ; got a subroutine RTN\r\n02243 goto 02210\r\n02244 c - 1 -&gt; c[s]      ; C= 00000000000221\r\n02245 if c[s] = 0\r\n02246 goto 02334         ; RTN in main prog (tidy up and stop)\r\n; else c[s] was &gt; 1. go back to main prog (goto 02254)\r\n;\r\n; RTN3:\r\n; RTN from main program\r\n02334 0 -&gt; c[ms]         ; clear prefixes AND subroutine level\r\n02335 c &lt;-&gt; m            ; C= 00000000000000 M =00000000000221\r\n02336 rom 1              ; tidy up INC clear the running flag\r\n\r\n; tidy up\r\n00737 1 -&gt; f3\r\n00740 clearstatus        ; S= ............\r\n00741 goto 00620\r\n00620 if s8 = 0\r\n00621 goto 00531\r\n; display:\r\n00531 ...\r\n00777 goto 00726         ; wait loop\r\n<\/pre>\n<p>if s8 = 0 we&#8217;re not running and we should end up at &#8220;00 00&#8221;.<\/p>\n<pre>\r\n; RTN1:\r\n; RTN when not running (from the keyboard)\r\n02332 if s9 = 0\r\n02333 goto 02250\r\n; else continues into RTN from main program\r\n02250 search for label\r\n02251 jsb 02127 (=MCIRC0 = wait until done)\r\n02252 1 -&gt; f7\r\n02253 jsb 02047\r\n02047 return\r\n02254 search for label\r\n02255 jsb 02127 (=MCIRC0 = wait until done)\r\n02256 goto 02334\r\n; 02334 is RTN from main program\r\n<\/pre>\n<p>It looks like two searches take us to &#8220;00 00&#8221;.<br \/>\nThe seeming NOP of &#8220;jsb 02047 : 02047 return&#8221; has a side effect if f7 is set. It copies the bottom 6 bits of the jsb address into &#8220;the buffer&#8221;. That means the second search looks for program code 047. That is a reserved instruction. It shouldn&#8217;t be present in memory. <\/p>\n<p>s9 = 1 skips the go &#8220;00 00&#8221; process. It seems to get set when doing a subroutine but it needs to be explored further.<\/p>\n<p>The other end of the RTN is the GSB. On a HP-65, GSB is inferred &#8211; you just use [A] or [B] etc in a program to mean GoSuB A or B etc. (Interestingly on a HP-67, if you press [A] or [B] etc in W\/PRGM mode, the one key gets entered as &#8220;31 22 11&#8221; (f GSB A) or &#8220;31 22 12&#8221; (f GSB B) etc; so the principle carried forward.) You can see an [A] pressed from the keyboard and then a [B] run from within a program, in <a href=\"http:\/\/www.sydneysmith.com\/products\/gss-hp65\/downloads\/65-rtn-rtn.txt\" target=\"_blank\">65-rtn-rtn.txt<\/a><\/p>\n<p>[A] from the keyboard gives:<\/p>\n<pre>\r\n01316 key -&gt; rom\r\n01036 goto 01351 <\/pre>\n<p>[B] in the program gives:<\/p>\n<pre>\r\n01364 buffer -&gt; rom address\r\n01365 goto 01000\r\n01034 goto 01351 <\/pre>\n<p>Clearly the scan code for [A] is 036 and for [B], 034.<br \/>\nThe common bit afterwards is:<\/p>\n<pre>\r\n01351 rom 4\r\n02352 0 -&gt; f5                            ; S= ...........b\r\n02353 if c[m] = 0\r\n02354 goto 02371\r\n02371 0 -&gt; c[ms]\r\n02372 if s8 = 0                          ; if not running\r\n02373 goto 02102 <\/pre>\n<p>It checks that there aren&#8217;t any prefixes (f f-1 STO RCL LBL DSP g) in the &#8220;if c[m] = 0&#8221; step. Then it clears the sign &#8220;bit&#8221; in C and clears any prefixes (perhaps something branches here with some set).<\/p>\n<p>The thing that determines what comes next isn&#8217;t &#8220;was it a key press?&#8221; nor &#8220;are we already in a subroutine?&#8221;. It is simply &#8220;are we running?&#8221; (if s8 = 0).<\/p>\n<p>If we aren&#8217;t already running, it goes to:<\/p>\n<pre>\r\n02102 if s9 = 0\r\n02103 goto 02302\r\n\r\n02302 search for label\r\n02303 goto 02106\r\n02106 c + 1 -&gt; c[s]      ; C= 10000000000221\r\n02107 clearstatus        ; S= ............\r\n02110 1 -&gt; s10           ; S= ..........a.\r\n02111 jsb 02376 <\/pre>\n<p>That causes a &#8220;search for label&#8221; and sets c[s] to 1 (as shown).<\/p>\n<p>The alternate, if we were already running, is:<\/p>\n<pre>\r\n02374 mark and search\r\n02375 clearstatus           ; S= ............ <\/pre>\n<p>That uses the other &#8220;search&#8221; microinstruction. This one leaves the current pointer in place and adds a secondary pointer. After this, it is the secondary pointer that moves &#8211; not the normal one. Note that c[s] remains zero if we are in a subroutine (one level down) instead of the main routine (top level).<\/p>\n<p>The HP-65 only supports two levels of subroutine. The doco refers to the top level as the &#8220;main routine&#8221; and the lower level (called from inside the main routine) as the &#8220;subroutine&#8221;. You can&#8217;t call another level below that. The manual shows a call to another layer (eg LBL B, &#8230;, C, &#8230;, RTN) as equivalent to a &#8220;GTO C&#8221;. There isn&#8217;t a tertiary pointer to move around and c[s] is only ever 1 (in the main routine) or 0 (in the subroutine).<\/p>\n<p>Regardless of whether it was [A] from the keyboard or [B] within a program, the process continues on from microinstruction 02376 which sets the &#8220;running&#8221; flag (s8 to 1). Don&#8217;t get confused by the &#8220;jsb 02376&#8221;. It never returns to the instruction after that as, like user program memory, the microcode only supports one return address. It is effectively a goto 02376 back into common code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The RTN key does a return from a subroutine. It also stops a main program and, if nothing&#8217;s running at all, it will take you to the &#8220;top of memory marker&#8221; (&#8220;00 00&#8221; in w\/prgm mode). Here&#8217;s how it does all that:<\/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],"tags":[37,32],"_links":{"self":[{"href":"https:\/\/www.sydneysmith.com\/wordpress\/wp-json\/wp\/v2\/posts\/1423"}],"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=1423"}],"version-history":[{"count":6,"href":"https:\/\/www.sydneysmith.com\/wordpress\/wp-json\/wp\/v2\/posts\/1423\/revisions"}],"predecessor-version":[{"id":1478,"href":"https:\/\/www.sydneysmith.com\/wordpress\/wp-json\/wp\/v2\/posts\/1423\/revisions\/1478"}],"wp:attachment":[{"href":"https:\/\/www.sydneysmith.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sydneysmith.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sydneysmith.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}