; keyboard funcs

KEY_Update:

        ld hl,keymap    ;3
        ld bc,#f782     ;3
        out (c),c       ;4
        ld bc,#f40e     ;3
        ld e,b          ;1
        out (c),c       ;4
        ld bc,#f6c0     ;3
        ld d,b          ;1
        out (c),c       ;4
        ld c,0          ;2
        out (c),c       ;4
        ld bc,#f792     ;3
        out (c),c       ;4
        ld a,#40        ;2
        ld c,#4a        ;2 44
loop    ld b,d          ;1
        out (c),a       ;4 select line
        ld b,e          ;1
        ini             ;5 read bits and write into KEYMAP
        inc a           ;1
        cp c            ;1
        jr c,loop       ;2/3 9*16+1*15=159
        ld bc,#f782     ;3
        out (c),c       ;4
        ret
		
.keymap
		ds 10,0
.keys
		ds 6,0
	
KEY_UpdateState:
	
	ld IX,keymap+9 		;get joystick	
	ld HL,keys
	xor A
	ld B,6
	
.key_loop
	rr (IX)
	jr c,no_key
	ld A,(HL)
	cp 2
	jr nc,key_pressed
	inc (HL)
.key_pressed
	xor A
	jr key_done
.no_key
	ld (HL),A
.key_done
	inc HL
	djnz key_loop
	ret