Keyboard Scanning from Machine Code

Author(s): Ian Logan
Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v3 n7 (Volume 3, Issue 7)
Date: July 1982
Pages: 6

One of the problems you must solve when writing machine code for a ZX80/1 with 8K ROM is deciding how best to scan the keyboard. Although there is a Keyboard Scanning routine at 02BB and a Keyboard Decode routine at 07BD, it is preferable on the whole not to use either of these directly.

In SLOW mode, the system variable LAST K (dec. 16421/2, hex 4025/6) holds the “key-value” of the last key pressed. Pressing a key forces an exit from the display routine after the Keyboard Scanning routine has detected that a key was pressed.

This Key-Value Demonstrator program shows the key-values for the ZX80/1’s different keys:

KEY-VALUE DEMONSTRATOR

  10 SCROLL
20 PRINT INKEY$;TAB 7;PEEK 16421+256*PEEK 16422
30 GOTO 10

SAMPLE RUN

        65535
1       65015
2       64503
3       63479
4       61431

You’ll see the key-value for no-key is 65535, for 1 is 65015, and so on.

Knowing these values in a machine code program in SLOW, you can read the system variable, LAST K, and match it against known key-values. Or you can stay in a loop until you press a certain key. This method works well, but you have to be careful with multiple keying.

To use this method, you must be sure the machine code program is in the SLOW mode. How can you select FAST and SLOW from machine code?

You can select SLOW by calling either 0F28 (old 8K ROM) or 0F2B (improved 8K ROM–new 8K ROMs should be improved). Select FAST by calling 0F20 (old ROM) or 0F23 (new ROM). Because some people may have old ROMs, I use this construction for FAST/SLOW selection to make my program ROM-independent:

START  LD HL,+NEXT    ;6 bytes
further on
       PUSH HL        ;push address
                       onto stack
       LD HL,(SLOW    ;SLOW=0CAC
        or FAST)       FAST=0CAF
       JP (HL)        ;call SLOW or
                       FAST
NEXT   ........       ;now proceed

This routine works by looking up the appropriate address in the “parameter table.” Note that calling SLOW is really “going to SLOW mode” if it is available and working.

The FAST/SLOW Keyboard Scanning program following is my preferred method of keyboard scanning. It is a copy of Sinclair’s method and so has the same “feel” as obtained when entering BASIC.

The machine coding is cunning as it uses an apparently never-exited loop:

04CF SLOW-DISP BIT 0,(HL)
               JR  Z,04CF,SLOW-
                   DISP

The answer is that in SLOW mode, bit 0 of (HL) becomes set whenever a key is pressed.

This machine code routine also caters to multiple-keying and sets the debounce mechanism to its correct value. Note the routine is set up to return the character code of the unshifted key being pressed. It is printed using a D7 instruction only for for convenience of the demonstration.

  10 REM 123456789012345678901234567890123456
20 REM NOW ENTER THE ROUTINE
30 LET A$="213B40CB7ECC2902CB4628FCED4B2540CBC0FDCB3B863EFF322740CDBD0730E04675D7C9"
40 FOR A=1 TO 36
50 POKE 16513+A,16*(CODE A$(A*2-1)-28)+CODE A$(A*2)-28
60 NEXT A
100 REM NOW CALL THE ROUTINE
110 SCROLL
120 RAND USR 16514
130 GOTO 100