Prev: F603 Up: Map Next: F67E
F640: Read Keyboard Controls
Scan the keyboard half-rows via Table_KeyboardPortMap and return the matching ASCII key code in A, or 00 if nothing is pressed.
Probe port FEFE first (caps/shift row with bit 00 forced).
Read_KeyboardControls F640 LD BC,$FEFE Load BC with keyboard port FEFE.
F643 LD HL,$F67E Point HL at Table_KeyboardPortMap.
F646 IN A,(C) Read the half-row.
F648 SET 0,A Force bit 00 set (ignore caps-shift alone).
F64A AND %00011111 Keep the five key bits.
F64C XOR %00011111 Invert so pressed keys are set.
F64E JR NZ,Read_KeyboardControls_Decode If any are pressed, decode at Read_KeyboardControls_Decode.
F650 LD DE,$0005 Load DE with 0005, then advance HL onto the next half-row's key codes.
F653 ADD HL,DE
F654 LD E,$06 Load E with 06 remaining half-rows.
Scan the remaining keyboard half-rows for a pressed key.
Read_KeyboardControls_HalfRowLoop F656 RLC B Rotate the port select in B left (next half-row).
F658 IN A,(C) Read that half-row.
F65A AND %00011111 Keep the five key bits.
F65C XOR %00011111 Invert so pressed keys are set.
F65E JR NZ,Read_KeyboardControls_Decode If any are pressed, decode at Read_KeyboardControls_Decode.
F660 INC HL Advance HL onto the next half-row's key codes.
F661 INC HL
F662 INC HL
F663 INC HL
F664 INC HL
F665 DEC E Decrease the half-row counter.
F666 JR NZ,Read_KeyboardControls_HalfRowLoop Loop at Read_KeyboardControls_HalfRowLoop if more half-rows remain.
Final half-row: force bit 01 set before testing.
F668 RLC B Rotate onto the last half-row port.
F66A IN A,(C) Read it.
F66C SET 1,A Force bit 01 set.
F66E AND %00011111 Keep / invert the key bits.
F670 XOR %00011111 Invert so pressed keys are set.
F672 JR NZ,Read_KeyboardControls_Decode If any are pressed, decode at Read_KeyboardControls_Decode.
F674 RET Return with A = 00 (no key).
Shift through the pressed-bit positions to pick the ASCII code at HL.
Read_KeyboardControls_Decode F675 SRL A Shift the pressed-bit mask right, then if that key is pressed, take its code at Read_KeyboardControls_Found.
F677 JR C,Read_KeyboardControls_Found
F679 INC HL Advance HL onto the next key's code.
F67A JR Read_KeyboardControls_Decode Jump to Read_KeyboardControls_Decode to keep decoding.
Return the ASCII code for the pressed bit position from the port map.
Read_KeyboardControls_Found F67C LD A,(HL) Fetch the ASCII key code at HL.
F67D RET Return.
Prev: F603 Up: Map Next: F67E