Engineering:Keystroke programming

From HandWiki

Keystroke programming describes a specific way of programming by which each keystroke on a device or application is recorded in some way and then played back so that the recorded key-presses can be repeated multiple times. Keystroke programming is most commonly but not exclusively found in programmable calculators, but there are keystroke-programmable software applications, too, for example Vim (text editor)[1]

Example: TI Calculators

The TI-58C

The Texas Instruments TI-58/TI-58C/TI-59 calculators were programmable, and program storage took the form of a sequence of codes which (mostly) corresponded to the position of keys on the calculator keypad. Here is a simplified diagram of the basic key layout:

  x1 x2 x3 x4 x5
1x Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Brown
2x Template:TI-5x/Keys/Yellow Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Yellow
3x Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Brown
4x Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Brown
5x Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Yellow
6x Template:TI-5x/Keys/Brown Template:TI-5x/Keys/White Template:TI-5x/Keys/White Template:TI-5x/Keys/White Template:TI-5x/Keys/Yellow
7x Template:TI-5x/Keys/Brown Template:TI-5x/Keys/White Template:TI-5x/Keys/White Template:TI-5x/Keys/White Template:TI-5x/Keys/Yellow
8x Template:TI-5x/Keys/Brown Template:TI-5x/Keys/White Template:TI-5x/Keys/White Template:TI-5x/Keys/White Template:TI-5x/Keys/Yellow
9x Template:TI-5x/Keys/Brown Template:TI-5x/Keys/White Template:TI-5x/Keys/White Template:TI-5x/Keys/White Template:TI-5x/Keys/Yellow

Each key is assigned a two-digit code; for most (but not all) keys, the tens digit comes from the row number (as indicated down the left of the table) and the units digit comes from the column number (as indicated across the top of the table).

Now consider a very simple program, which adds 2 to the number being displayed, and then stops. The program is entered starting from program location 000, and looks like this:

Location Contents Meaning Comments
000 85 +
001 02 2
002 95 = compute result
003 91 R/S stop program

To use this program, you would enter the number n to be operated on, press Template:TI-5x/Keys/Brown to ensure the current program location was 000, then Template:TI-5x/Keys/Brown to start execution. When the program stops (which should happen quite quickly for such a simple program), the display will be showing the number n + 2.

You can see that the codes 85, 95 and 91 correspond to the positions of the keys labelled Template:TI-5x/Keys/Yellow, Template:TI-5x/Keys/Yellow and Template:TI-5x/Keys/Brown on the grid above, but the code for the Template:TI-5x/Keys/White is not 83 as you would expect from the grid position, but 02. This last code was chosen to be more easily understandable by a human trying to read the program. There is no row 0, so the codes 00 .. 09 are used to represent the keys Template:TI-5x/Keys/White .. Template:TI-5x/Keys/White.

Context-Dependent Interpretations

These TI calculators allowed for addressing up to 100 data memory locations, numbered 00 to 99 (some models had a lower limit than this). A keystroke sequence like Template:TI-5x/Keys/Brown Template:TI-5x/Keys/White Template:TI-5x/Keys/White (store the currently-displayed number into memory 42) would be encoded as 42 42. In this case the first 42 is the key code for the Template:TI-5x/Keys/Brown key, but the second 42 is not a key code, but a memory register number. The calculator knows to interpret it as such because that's what has to follow the code for the Template:TI-5x/Keys/Brown key.

Program addresses were 3 digits, potentially allowing programs to consist of up to 1000 steps, numbered 000 to 999 (though in fact none of the models produced allowed for this maximum). A branch instruction, for example Template:TI-5x/Keys/Brown Template:TI-5x/Keys/White Template:TI-5x/Keys/White Template:TI-5x/Keys/White (unconditional branch to location 345) would be encoded as 61 03 45. In this case the hundreds digit of the branch destination is in the units digit of the second instruction byte (after the opcode), with a zero tens digit; and the remaining digits are in the third instruction byte.

A code sequence like 61 11, on the other hand, represents the keystrokes Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Brown, which is a branch to the location marked by the symbolic label Template:TI-5x/Keys/Brown. The calculator can tell this is a symbolic branch, rather than a branch to a numbered location, because the tens digit of the byte immediately following the opcode is not zero.

Most of the keys on the keypad can be used as symbolic labels. For example, sequences like Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Brown and Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Brown are legitimate, provided that Template:TI-5x/Keys/Brown and Template:TI-5x/Keys/Brown have in fact been defined as labels somewhere in the program.

Modifier Keys

Most of the keys had multiple functions, depending on whether Template:TI-5x/Keys/Brown or Template:TI-5x/Keys/Yellow had been pressed beforehand. Template:TI-5x/Keys/Brown invoked the "inverse" (whatever that meant) of the following function, and was recorded in a program as a separate keystroke (code 22), while Template:TI-5x/Keys/Yellow invoked a completely different function which was written on the keypad faceplate just above the key. For example, pressing Template:TI-5x/Keys/Yellow Template:TI-5x/Keys/Brown invoked the Template:TI-5x/Keys/Alt function, while Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Yellow Template:TI-5x/Keys/Brown (or Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Alt) gave you the arcsine.

The code for this alternate function is derived from that of the original key function by adding 5 to the units digit (without carry to the tens digit). Thus, the key codes corresponding to the position of the Template:TI-5x/Keys/Yellow key itself (21 and 26) are never used as opcodes. Here is the table of the codes produced with the Template:TI-5x/Keys/Yellow prefix:

  x6 x7 x8 x9 x0
1x Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt
2x   Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Yellow
3x Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt
4x Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt
5x Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt
6x Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt
7x Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt
8x Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt
9x Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt

Merged Keystrokes

The Template:TI-5x/Keys/Alt key allows for indirection: wherever a reference to a memory register or a program location is allowed, this key can be used to fetch the actual value from a memory register. For example, the key sequence Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Alt Template:TI-5x/Keys/White Template:TI-5x/Keys/White means to fetch the value in memory register 10, and use that value in turn as the number of a memory register whose contents are to be fetched as the current operand. However, in this case the Template:TI-5x/Keys/Alt cannot be encoded as code 40, because the code sequence 43 40 would be interpreted as Template:TI-5x/Keys/Brown Template:TI-5x/Keys/White Template:TI-5x/Keys/White, not Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Alt.

But, because of the use of the 00 .. 09 codes for the Template:TI-5x/Keys/White .. Template:TI-5x/Keys/White keys, the codes 62-64, 72-74, 82-84 and 92 are available for other uses. So most of these are reallocated to represent op Template:TI-5x/Keys/Alt sequences:

  x2 x3 x4
6x Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt
7x Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Alt
8x   Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt Template:TI-5x/Keys/Alt
9x Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Brown    

Thus, the key sequence Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Alt Template:TI-5x/Keys/White Template:TI-5x/Keys/White would be encoded as 73 10.

Note the use of code 92 for Template:TI-5x/Keys/Brown Template:TI-5x/Keys/Brown (return from subroutine); this was not necessary for reasons of ambiguity, but was done to save space with such a common sequence.

See also

References