Classic Computer Magazine Archive COMPUTE! ISSUE 19 / DECEMBER 1981 / PAGE 80

Programming The RESET Key The Easy Way

Richard Cornelius, Wichita, KS

On the Apple Computer the RESET key, to most users, is a magical key that provides an instant means to get out of any program. Usually a person begins to modify the RESET function only after learning machine language. Here is a method of making the RESET key do anything (well, almost anything) that you want it to do on an Apple II Plus, and you don't need any knowledge of machine language.

First let's examine what the RESET key does. When the RESET key is pressed the currently running program is interrupted; the screen display is set to text page 1; output to the screen is set to NORMAL; the text window is set to the complete screen; the cursor is moved to the bottom of the page; a beep sounds; accessory I/O is shut down; and then the computer looks in locations 1010 and 1011 in memory to see where it should go next for instructions. When the computer is turned on, the contents of these two locations are automatically set such that when RESET is pressed the computer is returned to immediate mode in BASIC. Changing these locations to make the computer go to different places for instructions involves only POKEs to positions 1010 and 1011 and a CALL-1169.

Where should the computer be sent? Starting at position 768 there is some room that is reserved for short machine language programs, and that is where we shall send it. (Don't worry — you don't need to know any machine language.) POKEs to seven bytes are used to make the RESET key run a BASIC program starting at the second line of code. When the first line of the program makes the program jump around the second line, then the second line will only be executed when RESET is pressed.

The program will help you understand how the RESET key can be used to execute any BASIC statements that can be put into a program. When the program is RUN, statements 110 through 190 are jumped over so that lines 200 through 260 are the first statements in the program that perform any tasks. These lines fix the RESET key so that the computer will go to line 110 when the RESET key is pressed. The length of the very first statement is critical. As long as it has a three-digit number after the GOTO, the RESET key will operate as desired. Changes in the length of the statement will likely mean that the RESET key will send the computer to some nonsense location. Placing a REM statement (or any other statement) before line 100 will have the same effect. Modifying the DATA statement in line 230 to accommodate changes in the length of that first statement is not difficult, but, unless you understand what to do, you had better not make any changes.

Lines 270 through 310 constitute a dummy BASIC program to show that the program is being RUN. Statements 110 through 190 tell the computer what to do when the RESET key is pressed. Lines 140 through 190 can be changed to make the RESET key do whatever you want it to do. In this example, the program is simply rerun from the beginning, but you can make lines 140-190 do whatever you wish. Lines 120 and 130 should not be changed since they fix up some things that are undone by the short machine language program that is POKEd in, but omit line 130 if you don't have a disk drive. If you should want to "turn off" the changes to the RESET key so that it behaves normally, simply POKE 1010,3: POKE 1011,244: POKE 1012,69 if you have no disk drive or POKE 1010,191: POKE 1011,157: POKE 1012,56 if you do have a disk drive.

For those who don't wish to stray from BASIC, this short program contains all that is needed to make the RESET key do almost anything. Take an existing program and add it starting at line 280 to the program. In lines 140-190, put statements that you wish to be executed when the RESET key is pressed. You can thus program the RESET key in BASIC without knowing any machine language. For those who are interested in straying just a little from BASIC, the final paragraphs explain the details of what is happening.

Positions 1010 and 1011 (hex 3F2 and 3F3) contain the low and high bytes of the location that the RESET key makes the computer jump to after it performs a fixed set of operations. The POKEs in statement 210 change this location from 40383 (hex 9DBF) to 768 (hex 300). Before the computer performs this jump, it looks at the "power-up" byte, position 1012 (hex 3F4), to see whether the value at this location equals an exclusive OR of the value in position 1011 (hex 3F3) with the constant 165. If the values correspond properly, the computer believes that it has not just been turned on and it executes a jump to the specified location. If the values do not properly correspond, the computer thinks that it has just been turned on, and it will attempt to reboot the disk if a disk controller card is present. The CALL-1169 in statement 210 properly sets this power-up byte.

The DATA statement in line 230, coupled with the POKE statement in the FOR...NEXT loop in lines 240 through 260, puts a very short machine language program into memory. This program is shown below:

0300- A9 0A    LDA  #$0A
0302- 85 67    STA  $67
0304- 4C 66 D5 JMP  $D566

The first two statements in this program place the value 10 (hex 0A) into location 103 (hex 67). Position 103 is the low byte (and position 104 is the high byte) of the starting location of the current BASIC program. The first statement in the program is "100 GOTO 200" and occupies 9 bytes: 2 bytes for the location of the next line, 2 bytes for the statement number, 1 byte for the GOTO token, 3 bytes for the digits of the number 200, and 1 byte for a terminating 0. Normally location 103 would contain the value 1, so adding 9 to this value makes the computer think that the BASIC program begins at the second line. To see that this works, enter the BASIC program and then POKE 103,10. If you list the program after this POKE, the list will begin with line 20. POKEing 103 with the value 1 will restore the program to begin with statement 10.

The final line in this machine language program jumps to 54630 (hex D566) where the RUN routine in firmware Applesoft BASIC resides. Since the value in location 103 (hex 67) has been changed, the RUN command executes the BASIC program starting at line 110. Once the program is running, the section that can only be accessed by the RESET key, the value in location 103 is changed back to its standard value so that the RUN command in line 190 will RUN the program starting with the first line of the BASIC program.

Many variations on this general scheme are possible. By making the RESET key RUN statements of BASIC code, changing the RESET key function becomes an easy adaptation to add to any BASIC program.

100 GOTO 200
120 POKE 103, 1
130 CALL 1002
140 HOME
150 VTAB 3
180 FOR PAUSE = 1 TO 2000 : NEXT
190 RUN
210 POKE 1010, 0 : POKE 1011, 3:
    CALL -1169
230 DATA 169, 10, 133, 103, 76, 102, 213
240 FOR SPOT = 768 TO 774
260 NEXT
280 HOME
290 VTAB 3
310 GOTO 310