| Routines

00654: THE 'KEYBOARD SCANNING' SUBROUTINE

This very important subroutine is called by both the main keyboard subroutine (KEYBOARD) and the INKEY$ routine (S_INKEY). In all instances the E register is returned with a value in the range of 0 to 39, the value being different for each of the forty keys of the keyboard, or the value 255, for no-key. The D register is returned with a value that indicates which single shift key is being pressed. If both shift keys are being pressed then the D and E registers are returned with the values for the CAPS SHIFT and SYMBOL SHIFT keys respectively. If no key is being pressed then the DE register pair is returned holding 65535. The zero flag is returned reset if more than two keys are being pressed, or neither key of a pair of keys is a shift key.
Output
D | Shift key pressed (24 or 39), or 255 if no shift key pressed
E | Other key pressed (0 to 39), or 255 if no other key pressed
F | Zero flag reset if an invalid combination of keys is pressed
KEY_SCAN | 00654 | LD L,47 | The initial key value for each line will be 47, 46,..., 40. (Eight lines.)
| 00656 | LD DE,65535 | Initialise DE to 'no-key'.
| 00659 | LD BC,65278 | C=port address, B=counter.
Now enter a loop. Eight passes are made with each pass having a different initial key value and scanning a different line of five keys. (The first line is CAPS SHIFT, Z, X, C, V.)
KEY_LINE | 00662 | IN A,(C) | Read from the port specified.
| 00664 | CPL | A pressed key in the line will set its respective bit, from bit 0 (outer key) to bit 4 (inner key).
| 00665 | AND 31
| 00667 | JR Z,KEY_DONE | Jump forward if none of the five keys in the line are being pressed.
| 00669 | LD H,A | The key-bits go to the H register whilst the initial key value is fetched.
| 00670 | LD A,L
KEY_3KEYS | 00671 | INC D | If three keys are being pressed on the keyboard then the D register will no longer hold 255 - so return if this happens.
| 00672 | RET NZ
KEY_BITS | 00673 | SUB 8 | Repeatedly subtract 8 from the present key value until a key-bit is found.
| 00675 | SRL H
| 00677 | JR NC,KEY_BITS
| 00679 | LD D,E | Copy any earlier key value to the D register.
| 00680 | LD E,A | Pass the new key value to the E register.
| 00681 | JR NZ,KEY_3KEYS | If there is a second, or possibly a third, pressed key in this line then jump back.
KEY_DONE | 00683 | DEC L | The line has been scanned so the initial key value is reduced for the next pass.
| 00684 | RLC B | The counter is shifted and the jump taken if there are still lines to be scanned.
| 00686 | JR C,KEY_LINE
Four tests are now made.
| 00688 | LD A,D | Accept any key value which still has the D register holding 255, i.e. a single key pressed or 'no-key'.
| 00689 | INC A
| 00690 | RET Z
| 00691 | CP 40 | Accept the key value for a pair of keys if the D key is CAPS SHIFT.
| 00693 | RET Z
| 00694 | CP 25 | Accept the key value for a pair of keys if the D key is SYMBOL SHIFT.
| 00696 | RET Z
| 00697 | LD A,E | It is however possible for the E key of a pair to be SYMBOL SHIFT - so this has to be considered.
| 00698 | LD E,D
| 00699 | LD D,A
| 00700 | CP 24
| 00702 | RET | Return with the zero flag set if it was SYMBOL SHIFT and 'another key'; otherwise reset.