0000-                  4       ;
0000-                  5       ; FROM THE BOOK '6502 GAMES', EXPECTS THE SAME HARDWARE AS DECRIBED WITHIN IT.
0000-                  6       ; THIS PART WAS NOT MEANT TO BE RUN DIRECTLY, SO I'VE JUST KEPT THE ADDRESS
0000-                  7       ; DEFINITIONS GIVEN IN THAT CHAPTER.
0000-                  8       ;
C000-                  9       VIA1    .EQ     $C000
CC00-                 10       VIA3    .EQ     $CC00
CC03-                 11       DDR3A   .EQ     VIA3+3      ; USING RC-ONE ADDRESS DECODING SCHEME, PLACING
CC02-                 12       DDR3B   .EQ     VIA3+2      ;  VIA3 AT $CC00 BY DEFAULT (INSTEAD OF $AC00)
CC01-                 13       PORT3A  .EQ     VIA3+1      ;  TO FIT RC-ONE ADDRESS DECODING THOUGH ORIGINAL
CC00-                 14       PORT3B  .EQ     VIA3        ;  CAN BE JUMPERED IF NEEDED/WANTED.
0000-                 15
0100-                 16               .OR     $0100       ; NOTE: BOOK PLACES $0100 INSIDE STACK, MEANING
0100-                 17               .TA     $0100       ;  THAT ROUTINE MAY EASILY GET OVERWRITTEN.
0100-A9 00            18 (  2) START   LDA     #0
0102-8D 03 CC         19 (  4)         STA     DDR3A       ; SET KEY STROBE PORT FOR INPUT
0105-A9 FF            20 (  2)         LDA     #$FF
0107-8D 02 CC         21 (  4)         STA     DDR3B       ; SET KEYS FOR OUTPUT
010A-                 22               .IN     getkey_routine.asm
010A-            I     1       ; 'GETKEY' KEYBOARD INPUT ROUTINE READS AND DEBOUNCES KEYBOARD. RETURNS WITH
010A-            I     2       ; KEY NUMBER IN ACCUMULATOR IF KEY DOWN. OPERATION: SENDS NUMBERS 0-F TO 74154
010A-            I     3       ; (4 TO 16 LINE DECODER), WHICH GROUNDS ONE SIDE OF KEYSWITCHES ONE AT A TIME.
010A-            I     4       ; IF A KEY IS DOWN, PA7 OF VIA #3 WILL BE GROUNDED, AND THE CURRENT VALUE
010A-            I     5       ; APPLIED TO THE 74154 BE THE KEY NUMBER. WHEN THE PROGRAM DETECTS A KEY CLOSE
010A-            I     6       ; CHECKS FOR KEY CLOSURE FOR 50 MS. TO ELIMINATE BOUNCE.
010A-            I     7       ; NOTE: IF NO KEY IS PRESSED, GETKEY WILL WAIT.
010A-            I     8       ;
010A-2C 01 CC    I     9 (  4) GETKEY  BIT     PORT3A      ; SEE IF KEY IS STILL DOWN FROM LAST KEY CLOSURE:
010D-            I    10                                   ;  KEYSTROBE IN 'N' STATUS BIT.
010D-10 FB       I    11 (2**)         BPL     GETKEY      ; IF YES, WAIT FOR KEY RELEASE
010F-A2 0F       I    12 (  2) RSTART  LDX     #15         ; SET KEY COUNTER TO 15
0111-8E 00 CC    I    13 (  4) NXTKEY  STX     PORT3B      ; OUTPUT KEY # TO 74154
0114-2C 01 CC    I    14 (  4)         BIT     PORT3A      ; SEE IF KEY DOWN: STROBE IN 'N'
0117-10 05       I    15 (2**)         BPL     BOUNCE      ;  IF YES, GO DEBOUNCE
0119-CA          I    16 (  2)         DEX                 ;   DECREMENT KEY #
011A-10 F5       I    17 (2**)         BPL     NXTKEY      ;  NO, DO NEXT KEY
011C-30 F1       I    18 (2**)         BMI     RSTART      ; START OVER
011E-8A          I    19 (  2) BOUNCE  TXA                 ; SAVE KEY NUMBER IN A
011F-A0 12       I    20 (  2)         LDY     #$12        ; OUTER LOOP CNT LOAD FOR DELAY OF 50 MS.
0121-A2 FF       I    21 (  2) LP1     LDX     #$FF        ; INNER 11 US. LOOP
0123-2C 01 CC    I    22 (  4) LP2     BIT     PORT3A      ; SEE IF KEY STILL DOWN
0126-30 E7       I    23 (2**)         BMI     RSTART      ; IF NOT, KEY NOT VALID, RESTART
0128-CA          I    24 (  2)         DEX
0129-D0 F8       I    25 (2**)         BNE     LP2         ; THIS LOOP USES 2115*5 US.
012B-88          I    26 (  2)         DEY
012C-D0 F3       I    27 (2**)         BNE     LP1         ; OUTER LOOP: TOTAL IS 50 MS.
012E-60          I    28 (  6)         RTS                 ; DONE: KEY IN A.
012F-            I    29       ;
012F-            I    30       ; SUBROUTINE 'INITKEY'
012F-            I    31       ; TAKES CARE OF INITIALIZING VIA #3 FOR USING WITH THE GETKEY ROUTINE FROM
012F-            I    32       ; THE CODE.
012F-            I    33       ;
012F-A9 00       I    34 (  2) INITKEY LDA     #0
0131-8D 03 CC    I    35 (  4)         STA     DDR3A       ; SET KEY STROBE PORT FOR INPUT
0134-A9 FF       I    36 (  2)         LDA     #$FF
0136-8D 02 CC    I    37 (  4)         STA     DDR3B       ; SET KEYS FOR OUTPUT
0139-60          I    38 (  6)         RTS
