Prev: 8B81 Up: Map Next: 8BFB
8B93: Scan the whole keyboard, debounced
Used by the routine at GET_KEY.
Reads all eight half-rows with BC = $FEFE and RLC B, building a key map at KEYS_LAST_SEEN and comparing it against the previous one so that only changes count. It calls DEBOUNCE_DELAY first, which is where nearly all of the game's idle time goes: about 7.4ms per scan.
SCAN_KEYBOARD 8B93 PUSH HL
8B94 PUSH IX
8B96 PUSH BC
8B97 CALL DEBOUNCE_DELAY Debounce: about 7.4ms
8B9A LD (NEW_KEY),BC No new key yet; HL = the keyboard as last seen, IX = the masks, BC = the first half-row
8B9E LD HL,KEYS_LAST_SEEN
8BA1 LD IX,KEY_STATE
8BA5 LD BC,$FEFE
SCAN_KEYBOARD_0 8BA8 IN A,(C) Read a half-row, with the keys that do not count masked out
8BAA AND $1F
8BAC OR (IX+$00)
8BAF PUSH AF Any key down now that was up last time?
8BB0 CPL
8BB1 AND (HL)
8BB2 CPL
8BB3 JR Z,SCAN_KEYBOARD_1
8BB5 LD (NEW_KEY),BC Yes: remember which half-row and which bits
8BB9 LD (NEW_KEY),A
SCAN_KEYBOARD_1 8BBC POP AF Remember this half-row for next time, and on to the next
8BBD LD (HL),A
8BBE INC HL
8BBF INC IX
8BC1 RLC B
8BC3 JR C,SCAN_KEYBOARD_0
8BC5 LD BC,(NEW_KEY) No new key at all: A = 0
8BC9 LD A,B
8BCA OR C
8BCB JR Z,SCAN_KEYBOARD_6
8BCD LD A,$FB Key number = half-row times five plus bit
SCAN_KEYBOARD_2 8BCF ADD A,$05
8BD1 RRC B
8BD3 JR C,SCAN_KEYBOARD_2
8BD5 DEC A
SCAN_KEYBOARD_3 8BD6 INC A
8BD7 RRC C
8BD9 JR C,SCAN_KEYBOARD_3
8BDB LD C,A
8BDC LD B,$00 Look it up in KEY_MAP, or KEY_MAP_SHIFTED if either shift is held
8BDE LD HL,KEY_MAP
8BE1 LD A,$FE
8BE3 IN A,($FE)
8BE5 AND $01
8BE7 JR Z,SCAN_KEYBOARD_4
8BE9 LD A,$7F
8BEB IN A,($FE)
8BED AND $02
8BEF JR NZ,SCAN_KEYBOARD_5
SCAN_KEYBOARD_4 8BF1 LD HL,KEY_MAP_SHIFTED
SCAN_KEYBOARD_5 8BF4 ADD HL,BC
8BF5 LD A,(HL)
SCAN_KEYBOARD_6 8BF6 POP BC
8BF7 POP IX
8BF9 POP HL
8BFA RET
Worth knowing when driving the game from a script: at uncapped emulation speed a key press and release can straddle a scan and be missed entirely, and the game's own ENTER still gets through, so a command comes out as a bare WAIT. Type at realtime speed.
Prev: 8B81 Up: Map Next: 8BFB