; 'GETKEY' KEYBOARD INPUT ROUTINE READS AND DEBOUNCES KEYBOARD. RETURNS WITH
; KEY NUMBER IN ACCUMULATOR IF KEY DOWN. OPERATION: SENDS NUMBERS 0-F TO 74154
; (4 TO 16 LINE DECODER), WHICH GROUNDS ONE SIDE OF KEYSWITCHES ONE AT A TIME.
; IF A KEY IS DOWN, PA7 OF VIA #3 WILL BE GROUNDED, AND THE CURRENT VALUE 
; APPLIED TO THE 74154 BE THE KEY NUMBER. WHEN THE PROGRAM DETECTS A KEY CLOSE
; CHECKS FOR KEY CLOSURE FOR 50 MS. TO ELIMINATE BOUNCE.
; NOTE: IF NO KEY IS PRESSED, GETKEY WILL WAIT.
;
GETKEY  BIT     PORT3A      ; SEE IF KEY IS STILL DOWN FROM LAST KEY CLOSURE:
                            ;  KEYSTROBE IN 'N' STATUS BIT.
        BPL     GETKEY      ; IF YES, WAIT FOR KEY RELEASE
RSTART  LDX     #15         ; SET KEY COUNTER TO 15
NXTKEY  STX     PORT3B      ; OUTPUT KEY # TO 74154
        BIT     PORT3A      ; SEE IF KEY DOWN: STROBE IN 'N'
        BPL     BOUNCE      ;  IF YES, GO DEBOUNCE
        DEX                 ;   DECREMENT KEY #
        BPL     NXTKEY      ;  NO, DO NEXT KEY
        BMI     RSTART      ; START OVER
BOUNCE  TXA                 ; SAVE KEY NUMBER IN A
        LDY     #$12        ; OUTER LOOP CNT LOAD FOR DELAY OF 50 MS.
LP1     LDX     #$FF        ; INNER 11 US. LOOP
LP2     BIT     PORT3A      ; SEE IF KEY STILL DOWN
        BMI     RSTART      ; IF NOT, KEY NOT VALID, RESTART
        DEX
        BNE     LP2         ; THIS LOOP USES 2115*5 US.
        DEY
        BNE     LP1         ; OUTER LOOP: TOTAL IS 50 MS.
        RTS                 ; DONE: KEY IN A.
;
; SUBROUTINE 'INITKEY'
; TAKES CARE OF INITIALIZING VIA #3 FOR USING WITH THE GETKEY ROUTINE FROM
; THE CODE.
;
INITKEY LDA     #0
        STA     DDR3A       ; SET KEY STROBE PORT FOR INPUT
        LDA     #$FF
        STA     DDR3B       ; SET KEYS FOR OUTPUT
        RTS