;; This example shows a bad method to read the keyboard and
;; joysticks.
;;
;; This example will work on the CPC and KC Compact, but not
;; on the CPC+.
;;
;; The following is assumed before executing of this algorithm:
;; - I/O port A of the PSG is set to input,
;; - PPI Port A is set to output

.read_matrix 
ld hl,matrix_buffer			; buffer to store matrix data

ld bc,&f40e					; PSG port A register
out (c),c
ld bc,&f6c0					; PSG operation: select register
out (c),c

ld bc,&f792					; PPI port A to input, 8255 will also clear
							; port outputs, therefore setting PSG operation to inactive
out (c),c

ld c,&40					; PSG operation: read register and initial keyboard line

.scan_key 
ld b,&f6 
out (c),c					;set matrix line & set PSG operation

ld b,&f4					;PPI port A (databus to/from PSG)
in a,(c)					;get matrix line data from PSG register 14

cpl							;invert data: 1->0, 0->1
							;if a key/joystick button is pressed bit will be "1"
							;keys that are not pressed will be "0"

ld (hl),a					;write line data to buffer
inc hl						;update position in buffer
inc c						;update line

ld a,c
and &0f
cp &0a						;scanned all rows?
jr nz,scan_key				;no loop and get next row

ld bc,&f782					; PPI port A to output, 8255 will also clear
							; port outputs, therefore setting PSG operation to inactive
out (c),c
ret

.matrix_buffer
defs 16