; 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
		
;do keyboard/joy mapping
KEY_DoMapping:
		ld IX,keymap
		bit 4,(IX+1)		;up key = F5
		jr z,KEY_DoMapping_DoUp
		bit 0,(IX+0)		;alt = up arrow
		jr z,KEY_DoMapping_DoUp
		jr KEY_DoMapping_noUp
.KEY_DoMapping_DoUp
		and %11111110
.KEY_DoMapping_noUp
		bit 6,(IX+1)		;down key = F2
		jr z,KEY_DoMapping_DoDn
		bit 2,(IX+0)		;alt = down arrow
		jr z,KEY_DoMapping_DoDn
		jr KEY_DoMapping_noDn
.KEY_DoMapping_DoDn
		and %11111101
.KEY_DoMapping_noDn
		bit 5,(IX+1)		;left key = F1
		jr z,KEY_DoMapping_DoLt
		bit 0,(IX+1)		;alt = left arrow
		jr z,KEY_DoMapping_DoLt
		jr KEY_DoMapping_noLt
.KEY_DoMapping_DoLt		
		and %11111011
.KEY_DoMapping_noLt
		bit 5,(IX+0)		;right key = F3
		jr z,KEY_DoMapping_DoRt
		bit 1,(IX+0)		;alt = right arrow
		jr z,KEY_DoMapping_DoRt
		jr KEY_DoMapping_noRt
.KEY_DoMapping_DoRt		
		and %11110111
.KEY_DoMapping_noRt
		bit 7,(IX+8)		;fire A = Z/W
		jr z,KEY_DoMapping_DoFireA
		bit 7,(IX+2)		;alt = control
		jr z,KEY_DoMapping_DoFireA
		jr KEY_DoMapping_noFireA
.KEY_DoMapping_DoFireA		
		and %11101111
.KEY_DoMapping_noFireA
		bit 7,(IX+7)		;fire B = X
		jr z,KEY_DoMapping_DoFireB
		bit 1,(IX+1)		;alt = copy
		jr z,KEY_DoMapping_DoFireB
		jr KEY_DoMapping_noFireB
KEY_DoMapping_DoFireB
		and	%11011111
.KEY_DoMapping_noFireB
		;or (IX+9)
		xor #FF

		ld B,A
		ld A,(IX+9)
		xor #FF
		or B

		ret
		


IF MMI_IsRecordMode		
		
;start recording keyboard
; HL = address to write
; A = bank		
.MMI_EnterRecordMode
	ld (MMI_RecordBank),A
	ld (MMI_RecordAddress),HL

	xor A
	ld (MMI_RecordLastCount),A
	ld (MMI_RecordLastData),A
	
	ld A,5
	ld (ForcePlayerMove),A
	
	ret
	
.MMI_RecordMode
;first of all , get keyboard
	call MMI_NoPlayerForce 
	ld B,A
	ld A,(MMI_RecordLastData)
	cp B
	jr z,MMI_RecordModeSameData
	
.MMI_RecordModeWriteData	
	push BC
;change bank
	ld B,#7F
	ld A,(MMI_RecordBank)
	out (C),A
	
;get address
	ld HL,(MMI_RecordAddress)
;write count
	ld A,(MMI_RecordLastCount)
	ld (HL),A
	inc HL
;write data
	ld A,(MMI_RecordLastData)
	ld (HL),A
	inc HL
;save pointer
	ld (MMI_RecordAddress),HL
;update dataset
	pop BC
	ld A,B
	ld (MMI_RecordLastData),A
	ld A,1
	ld (MMI_RecordLastCount),A	
	ret
.MMI_RecordModeSameData
	ld A,(MMI_RecordLastCount)
	inc A
	ld (MMI_RecordLastCount),A
	cp #FF	;max count ?
	jr z,MMI_RecordModeWriteData	;force writting !
	ret


ENDIF

;enter play mode
;A = bank
;HL = pointer to data
.MMI_EnterPlayMode
	ld (MMI_RecordBank),A
	ld (MMI_RecordAddress),HL

	xor A
	ld (MMI_RecordLastCount),A
	
;get first data	
	inc HL
	ld A,(HL)
	ld (MMI_RecordLastData),A
	
	ld A,6
	ld (ForcePlayerMove),A
	ret
	
.MMI_PlayMode
	ld A,(MMI_RecordBank)
	ld HL,(MMI_RecordAddress)
;setup bank
	ld B,#7F
	out (C),A

;set current command	
	ld A,(MMI_RecordLastData)
	ld (MMIVAR),A
;process next command ?	
	ld A,(MMI_RecordLastCount)
	inc A
	cp (HL)
	jr z,MMI_PlayModeProcessNext
	ld (MMI_RecordLastCount),A
	
	ret
.MMI_PlayModeProcessNext
;next command , pair of bytes
	inc HL
	inc HL
;count
	ld (MMI_RecordAddress),HL
;data
	inc HL
	ld A,(HL)
	ld (MMI_RecordLastData),A
;get next data
	xor A
	ld (MMI_RecordLastCount),A
	ret
	

.MMI_RecordLastCount
	db 0
.MMI_RecordLastData
	db 0
.MMI_RecordBank
	db 0
.MMI_RecordAddress
	dw 0
		
MMI:
		;di
		;call KEY_Update
		;ei
		xor A
		ld (FORCE_SPR),A
		
		ld A,(ForcePlayerMove)
		or A
		jr z,MMI_NoPlayerForce
		cp 1
		jp z,MMI_PlayerForceCentering
		cp 2
		jr z,MMI_PlayerForceIntro
		cp 3
		jr z,MMI_PlayerForceIntroB
		cp 4
		jp z,MMI_PlayerForceEnd
IF MMI_IsRecordMode
		cp 5		
		jp z,MMI_RecordMode
ENDIF
		cp 6
		jr z,MMI_PlayMode
		ret
		
.MMI_NoPlayerForce
		ld A,#FF
		call KEY_DoMapping
		ld (MMIVAR),A
		ret

.MMI_PlayerForceIntro		
		ld A,(INTRO_TIMER)
		dec A
		or A
		jr z,MMI_PlayerIntro_WaitTimer
		ld (INTRO_TIMER),A
		jr MMI_PlayerForceNoMove
		
.MMI_PlayerIntro_WaitTimer
		
.MMI_PlayerIntro_WaitTimerMove
		ld A,(SHIPX)
		inc A
		cp 30
		jr z,MMI_PlayerIntro_Stop
		ld (SHIPX),A
.MMI_PlayerIntro_WaitTimerNoMove
		ld B,231
		jr c,MMI_PlayerIntro_SprLoop
		xor A
.MMI_PlayerIntro_SprLoop
		dec B
		sub 6
		jr nc,MMI_PlayerIntro_SprLoop

		ld A,B
		ld (FORCE_SPR),A
		
		jr MMI_PlayerForceNoMove
		
.MMI_PlayerIntro_Stop
		ld A,3
		ld (ForcePlayerMove),A
		xor A
		ld (EXBYTE),A
		ld A,226
		ld (FORCE_SPR),A
		
.MMI_PlayerForceNoMove
		xor A
		ld (MMIVAR),A
		ret
		
.MMI_PlayerForceIntroB

		ld A,(FRAME)
		and 1
		push AF
		push AF
		
		ld B,228
		ld A,(SHIPX)
		sub 15
		jr c,MMI_PlayerForceIntroBSprDone
		dec B
		sub 10
		jr c,MMI_PlayerForceIntroBSprDone
		dec B
		
.MMI_PlayerForceIntroBSprDone
		pop AF
		jr z,MMI_PlayerForceIntroBPair
		ld A,5
		add B
		ld B,A
.MMI_PlayerForceIntroBPair		
		
		ld A,B
		ld (FORCE_SPR),A
		
		pop AF
		jr z,MMI_PlayerForceNoMove
		
		
		ld A,(SHIPX)
		dec A
		ld (SHIPX),A
		cp 10
		jr z,MMI_PlayerForceIntroEnd
		

		jr MMI_PlayerForceNoMove		
.MMI_PlayerForceIntroEnd
		xor A
		ld (ForcePlayerMove),A
		jr MMI_PlayerForceNoMove
	
.MMI_PlayerForceCentering
		ld B,0
		ld A,(SHIPX)
		cp 14
		jr z,MMI_PlayerForceCenteringXmoveOk
		jr c,MMI_PlayerForceCenteringXmoveRT
		set 2,B	;left
		jr MMI_PlayerForceCenteringXmoveOk		
.MMI_PlayerForceCenteringXmoveRT
		set 3,B ;right
.MMI_PlayerForceCenteringXmoveOk

		ld A,(SHIPY)
		cp 13
		jr z,MMI_PlayerForceCenteringYmoveOk
		jr c,MMI_PlayerForceCenteringYmoveRT
		set 0,B	;up
		jr MMI_PlayerForceCenteringYmoveOk		
.MMI_PlayerForceCenteringYmoveRT
		set 1,B ;down
.MMI_PlayerForceCenteringYmoveOk

		ld A,B
		ld (MMIVAR),A

		ret
		
.MMI_PlayerForceEnd
	xor A
	ld (MMIVAR),A

	ld A,(FRAME)
	and 1
	ret nz
	
	ld A,(INTRO_TIMER)
	dec A
	ret z
	ld (INTRO_TIMER),A
	
	ld HL,SHIPX
	inc (HL)
	

	
	ret
		
KEY_UpdateState:
	
	call MMI
	xor #FF
	ld (keymap+9),A
	
	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
		

