Apple II Machine Language: Accessing the keyboard

When writing a machine language routine to check the keyboard for a single character, make sure that the routine checks for the high bit at the keyboard, signifing valid data, before the routine attempts to read data there.
Here is an example of such a routine:
ORG            $300
KBD       EQU  $C000      ; location of keyboard data
KBDSTRB   EQU  $C010      ; location of keyboard strobe latch

ReadKey   BIT  KBD        ; test high bit of data reg.
         BPL  ReadKey    ; If not set retest till it is set
         LDA  KBD        ; Data at KBD is Valid (High Bit set)
         BIT  KBDSTRB    ; now clear the keyboard for the next char
         RTS             ; and we are done.

The act of striking a key will set the strobe bit automatically, so this
routine clears it before returning.

If you are testing for a particular keystroke, put the test in a seperate
routine. Here is an example of how that would be done:
         BIT  KBDSTRB     ; Clear out any data that is already at KBD
WaitForA  JSR  ReadKey     ; get a key from the keyboard
         CMP  #$C1        ; is it an A????

         BNE  WaitForA    ; Nope go get another
         RTS

These methods apply to all Apple II's: II, II Plus, IIe, IIe enhanced, and IIc.
Published Date: Feb 18, 2012