ZX81 Machine Code demo: how to read the keyboard
Posted: Thu Jan 28, 2021 10:03 am
It is easy to read the keyboard -- and specifically to look for specific key presses -- on the ZX81
Just call this and when it RETurns the A register will have the keycode. For example, if the user presses 'Y' then A will have the code for Y which is 62d (3Eh) per the ZX81 Basic manual
The ROM call 'DECODE' makes it easy to find out which specific key was pressed
The above code has a potential problem. It only works in SLOW mode -- if you call it in FAST mode it will hang the machine as the keys cannot be read in FAST mode, and the only way to exit the code is to press a key
Voila. This version will test the SLOW/FAST mode and go into SLOW mode if necessary and then go back to FAST mode before it RETurnsSo GETKEY is the more foolproof version. GETKEY won't hang if called from FAST mode, and it won't hang if called from the original (unimproved) ROM
Code: Select all
SCANKEY:LD A,(4025H);LAST_K
INC A
JR NZ,SCANKEY
GOTKEY: LD BC,(LAST_K)
LD A,C
INC A
JR Z,GOTKEY
CALL DECODE //call the ZX81 ROM routine
JR NC,GOTKEY
LD A,(HL) //retrieve the keycode
RET
The ROM call 'DECODE' makes it easy to find out which specific key was pressed
The above code has a potential problem. It only works in SLOW mode -- if you call it in FAST mode it will hang the machine as the keys cannot be read in FAST mode, and the only way to exit the code is to press a key
Voila. This version will test the SLOW/FAST mode and go into SLOW mode if necessary and then go back to FAST mode before it RETurns
Code: Select all
//ROM SUBROUTINES
DECODE EQU $07BD
ZX81ROM_FAST EQU $0F23
//
GETKEY: //this version works in SLOW mode or FAST mode
LD A,(CDFLAG) //test for SLOW mode
BIT 7,A
PUSH AF //save the result so we can restore it later
CALL Z,SLOW_ //if needed, call SLOW
SCANKEY:LD A,(4025H);LAST_K
INC A
JR NZ,SCANKEY
GOTKEY: LD BC,(LAST_K)
LD A,C
INC A
JR Z,GOTKEY
CALL DECODE //call the ZX81 ROM routine
JR NC,GOTKEY
POP AF //restore test result
LD A,(HL) //retrieve the keycode
CALL Z,ZX81ROM_FAST //if we started in FAST mode, go back to FAST mode
RET
SLOW_: SLOW_NEWROM EQU $0F2B //ROM routined 'SLOW' is at a different
SLOW_OLDROM EQU $0F28 //address for the two ZX81 ROM versions
CALL IS_OLDROM
JP Z,SLOW_NEWROM
JP SLOW_OLDROM
// RET comes from the ROM calls
IS_OLDROM: //THIS SUBROUTINE RET Z IF IMPROVED ROM
LD A,(5942)
CP $D9
RET