Prev: ECF3 Up: Map Next: ED6D
ED4D: Keyscan
Scans all eight keyboard half-rows in turn. A half-row with exactly one key down yields that key's packed definition; anything more ambiguous makes the routine return early with Z clear.
Used by the routine at define_a_key.
Output
D Key half-row number in bits 0..2, key in bits 3 and up, the same packing the key definition table uses, so the result can be stored straight into it. $FF if no key was down. Only meaningful when Z is set
F Z set when at most one key was identified, so D can be trusted. Z clear when the scan was ambiguous: two half-rows active, or two keys in one half-row
redefine_keyscan ED4D LD DE,$FF2F D = flag/counter? (255 to start), E = initial key and row counters (47 to start)
ED50 LD BC,$FEFE Set B to $FE (initial keyboard half-row selector) and C to $FE (keyboard port number)
Start loop.
ka_loop ED53 IN A,(C) Read port $<BC>
ED55 CPL Complement the value returned to change it from active-low to active-high
ED56 AND $1F Discard any non-key flags
ED58 JR Z,redefine_keyscan_1 Jump to next iteration if no keys were pressed
Keys were pressed.
ED5A INC D Only $FF, meaning no key found yet, leaves Z set; anything else means a second half-row is active so give up
ED5B RET NZ
ED5C LD H,A Copy key flags to H
ED5D LD A,E Copy key and row counters to A
redefine_keyscan_0 ED5E SUB $08 Decrement key counter bitfield in A
ED60 SRL H Shift a key bit out of H into the carry flag
ED62 JR NC,redefine_keyscan_0 Loop until we hit a set bit (at least one must be set since the zero case is handled earlier)
ED64 RET NZ Return if additional bits are set
ED65 LD D,A Set return value in D
redefine_keyscan_1 ED66 DEC E Decrement E
ED67 RLC B Rotate the half-row selector ($FE -> $FD -> $FB -> .. -> $7F)
ED69 JR C,ka_loop ...loop until the zero bit shifts out (eight iterations)
ED6B CP A Set Z
ED6C RET
Prev: ECF3 Up: Map Next: ED6D