Prev: B5E3 Up: Map Next: B5FF
B5F7: Read one or more keyboard half-rows
The IN does the selecting on its own: IN A,($FE) puts A on the top half of the address bus, so each 0 bit in A selects one half-row, and A=0 reads all eight at once -- which is how the "press any key" waits call it. Callers also pass $7E, $99, $BD and so on to read several rows in one go. The CPL turns the Spectrum's active-low keys the right way up, and the AND keeps the five that mean anything.
The OUT before it is not needed for the read. It writes A to port A*256+$FD, which nothing on a 48K Spectrum answers because address line 0 is high. A 128K machine's paging port would decode it whenever A has bit 7 clear, and 0, $7E and $7F are all among the values callers pass.
Input
A the half-rows to select, a 0 bit for each
A on exit, the five key bits, 1 for pressed
read_port B5F7 OUT ($FD),A not what selects the row: see above
B5F9 IN A,($FE) reads port A*256+$FE
B5FB CPL set bits now mean pressed keys; keep the five
B5FC AND $1F
B5FE RET
Prev: B5E3 Up: Map Next: B5FF