Intfp
Listing index
Main
Routine:        Keyb    
Function:       Detect a keypress and update the clock
Called by:      Almset, Clkset, Input, Main, Pb3, Pc, Wait
Calls:          Clock
Entry:          None
Exit:           A holds keycode
Preserved:      IX,IY,E


KEYB    CALL    CLOCK
; Update the clock each time Keyb is called.
        LD      C,8
        LD      D,0
        LD      L,255
; Scan the keyboard matrix.
KEYB0   IN      A,(C)
        OR      224
        CP      255
        JR      Z,KEYB3
; If no key in row C is pressed, then go to the next row, otherwise find out which key was pressed, by rotating until a '0' is found.
        LD      B,5
KEYB1   RR      A
        JR      C,KEYB2
        LD      H,A
        LD      A,255
        CP      L
; Check that this is the first key detected. If more than one key is pressed then jump to Keyb7, i.e disregard them all.
        JR      NZ,KEYB7
        LD      L,D
        LD      A,H
KEYB2   INC     D
        DJNZ    KEYB1
KEYB3   LD      A,5
        ADD     D
        LD      D,A
        LD      A,12
        INC     C
        CP      C
        JR      NZ,KEYB0
; Return to Keyb0 to scan a new row if they have not all been scanned already.
        LD      A,(KEYLST)
        CP      L
        JR      NZ,KEYB4
; Check that the currently pressed key is not the same as the previous key. If it is then return 255, i.e no key pressed. This prevents auto-repeating.
        LD      A,255
        RET
KEYB4   LD      A,L
        LD      (KEYLST),A
; Store the new keynumber and wait for 3/50 of a second. This effectively debounces the key.
        IN      A,(6)
        LD      D,A
KEYB5   IN      A,(6)
        SUB     D
        JP      P,KEYB6
        NEG
KEYB6   CP      3
        JR      C,KEYB5
        LD      A,255
        CP      L
        RET     Z
; Return if no keys were pressed, otherwise add the keynumber to the start address of the list of keycodes and fetch the keycode.
        LD      BC,KEYS
        LD      H,0
        ADD     HL,BC
        LD      A,(HL)
        CP      16
; Check that the pressed key was not Rst, if it was then jump to Keyb7 and return 255, i.e no key pressed. A Rst keypress requires special treatment and is detected by the subroutine Reset.
        JR      Z,KEYB7
        RET
KEYB7   LD      A,255
        RET
 
Intfp
Listing index
Main