; Title:	ZX Spectrum Keyboard Routines
; Author:	Dean Belfield
; Created:	29/07/2011
; Last Updated:	29/07/2011
; Requires:
; Modinfo:

; Read the keyboard and return an ASCII character code
Read_Keyboard:		LD HL,Keyboard_Map		; Point HL at the keyboard list
			LD D,8				; This is the number of ports (rows) to check
			LD C,&FE			; B is always FEh for reading keyboard ports
Read_Keyboard_0:	LD B,(HL)			; Get the keyboard port address
			INC HL				; Increment to keyboard list of table
			IN A,(C)			; Read the row of keys in
			AND &1F				; We are only interested in the first five bits
			LD E,5				; This is the number of keys in the row
Read_Keyboard_1:	SRL A				; Shift A right; bit 0 sets carry bit
			JR NC,Read_Keyboard_2		; If the bit is 0, we've found our key
			INC HL				; Go to next table address
			DEC E				; Decrement key loop counter
			JR NZ,Read_Keyboard_1		; Loop around until this row finished
			DEC D				; Decrement row loop counter
			JR NZ,Read_Keyboard_0		; Loop around until we are done
			AND A				; Clear A (no key found)
Read_Keyboard_2:       	LD A,(HL)			; We've found a key at this point; fetch the character code!

Keyboard_Map:		DB &FE,"#","Z","X","C","V"
			DB &FD,"A","S","D","F","G"
			DB &FB,"Q","W","E","R","T"
			DB &F7,"1","2","3","4","5"
			DB &EF,"0","9","8","7","6"
			DB &DF,"P","O","I","U","Y"
			DB &BF,"#","L","K","J","H"
			DB &7F," ","#","M","N","B"