8K Machine Code Keyboard Scanning

See all articles from SYNC v3 n1
Author(s): Sidney Belman
Publication: SYNC
Pub Details: SYNC v3 n1 (Volume 3, Issue 1)
Date: January/February 1983
Pages: 70-71

Bernard Puerzer’s program for scanning the ZX80 keyboard (“Machine Code Keyboard Scanning Program,” SYNC 1:4) was written for the 4K ROM. His program served as the occasion to give the same ability to read the keyboard to 8K ROM users. This required adaptation since the programs are not automatically transferable. One of the problems was to get around the differences in the way that the index registers are used. This program avoids that problem. Further, it adds a WAIT loop which keeps the screen dark after ENTER is pressed and then prints the keyboard entry following ENTER. This loop is in addresses 20023 to 20026. It requires that ENTER be read first in addresses 20002 to 20016.

The port of entry from the keyboard is IN A,(FE) at address 20014. The keyboard enters as groups of 5 keys determined by which bit of A is off, i.e., equals zero. Thus, LD A,254 (before IN A,254) allows keys “Shift to V” to enter. Table 1 describes this situation. The key enters A with values as shown in the last column. Note that the values are from the periphery to the center. A binary representation of each number will show how the 0 bit shifts.

The group values are stored in address 25000, and the key values in address 25001. Addresses 20030-20064 calculate the address containing the codes for the keys which are stored in HL and the codes are transferred to address 25005 by addresses 20065 to 20068. Address 20069 is the RET. The codes are stored in addresses 20070 to 20109, in identical order as given by Puerzer. My address 20070 = his 17458, etc. I prefer to use 184 (inverse S) for Shift and 179 (inverse N) for ENTER.

To enter the code you can use the loader program in Listing 1. To run the program I use this routine:

200 LET K   USR 20000
210 PRINT CHR$ (PEEK 25005)
220 STOP
230 GOTO 200

I enter GOTO 200, press ENTER, and then press the desired key. CONT will repeat the program.

Table 1.

247   3   1 to 5        62, 61,
                        59, 55,
                        47
251   2   Q to T        same
253   1   A to G        same
254   0   Shift
            to V        same
239   4   0 to 6        same
223   5   P to Y        same
191   6   N/L to H      same
127   7   Space
            to B        same

Figure 1. Machine code Loading Program. (From Bernard Puerzer’s article.)

  5 LET MARK=0
10 PRINT "ENTER STARTING ADDRESS"
20 INPUT A
30 PRINT A; " (1 sp.) "; PEEK (A)
40 INPUT B
60 IF B>255 THEN GO TO 130
70 POKE A,B
80 PRINT A; " (1 sp.) "; PEEK (A)
90 LET MARK=MARK +1
100 IF MARK < 10 THEN GO TO 110
105 CLS
107 LET MARK = 0
109 PRINT A; " (1 sp.) "; PEEK (A)
110 LET A = A+1
120 GO TO 30
130 STOP

Figure 2. 8K Keyboard Scanning Program.

Address  Hex  Decimal  Comments
20000 0E 14 LD C, 1
20001 1 1
20002 6 6 LD B,191 entry for N/L
20003 BF 191
20004 18 24 JR 4
20005 4 4
20006 CB 203 RLC B
20007 00 0
20008 0E 14 LD C, 0
20009 0 0
20010 78 120 LD A,B group entry code
20011 32 50 LD (25000), A
20012 A8 168
20013 61 97
20014 DB 219 IN A, (254) port for keyboard
20015 FE 254
20016 FE 254 CP 63
20017 3F 63
20018 28 40 JR Z -14 loop to 20006
20019 F2 242
20020 81 129 ADD A,C
20021 FE 254 CP 63
20022 3F 63
20023 28 40 JR Z-21 loop until next key
20024 EB 235
20025 91 145 SUB C
20026 32 50 LD (25001),A store key value
20027 A9 169
20028 61 97
20029 00 0 NOP
20030 21 33 LD HL,20069 start calc. address
20031 65 101
20032 4E 78
20033 11 17 LD DE,5
20034 05 5
20035 00 0
20036 3A 58 LD A,(25000)
20037 A8 168
20038 61 97
20039 06 6 LD B,254
20040 FE 254
20041 18 24 JR 3
20042 03 3
20043 CB 203 RLC B
20044 00 0
20045 19 25 ADD HL,DE
20046 B8 184 CP B
20047 20 32 JR NZ -6
20048 FA 250
20049 11 17 LD DE,1
20050 01 1
20051 00 0
20052 3A 58 LD A, (25001)
20053 A9 169
20054 61 97
20055 C6 198 ADD A,192
20056 C0 192
20057 06 6 LD B,127
20058 7F 127
20059 CB 203 RLC B
20060 00 0
20061 19 25 ADD HL, DE
20062 B8 184 CP B
20063 20 32 JR NZ -6
20064 FA 250
20065 7E 126 LD A,(HL) code into A
20066 32 50 LD(25005),A
20067 AD 173
20068 61 97
20069 C9 201 RET
20070 Enter codes given by Puerzer
starting with his address 17458.
Keep the same sequence, but
disregard the addresses given.
See Figure 3.
20109 Last entry

Figure 3. Table from Puerzer’s Article.

17458   1
17459   63    17469   60    17478   28    17488   231
17460   61    17470   42    17479   37    17489   49
17461   40    17471   55    17480   36    17490   48
17462   59    17472   57    17481   35    17491   47
17463   38    17473   29    17482   34    17492   45
17464   56    17474   30    17483   53    17493   0
17465   41    17475   31    17484   52    17494   155
17466   43    17476   32    17485   46    17495   50
17467   44    17476   32    17486   58    17496   51
17468   54    17477   33    17487   62    17497   39

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.