; new key input
; *************************************************************
; * inkey2$(state)
; *************************************************************
FUNCTION	INKEY2$	STATE
REM	; returns key
REM	; 
DEF	STATE	BYTE
STATE	FCB	0
SHIFTKEY	FCB	0
LASTKEY	FCB	0
TEMPC	FCB	0
TEMPD	FCB	0
KEYCHK	FCB	254,253,251,247,239,223,191,127
KEYSIN	FCB	0,0,0,0,0,0,0,0
KEYTBL	FCB	111,48,48	;0
	FCB	63,13,13	;enter
	FCB	126,64,19	;@ !!
	FCB	95,56,40	;8 (
	FCB	125,72,104	;H h
	FCB	123,80,112	;P p
	FCB	119,88,120	;X x
; index 1
	FCB	111,49,33	;1 !
	FCB	95,57,41	;9 )
	FCB	63,12,92	;clear \
	FCB	126,65,97	;A a
	FCB	125,73,105	;I i
	FCB	123,81,113	;Q q
	FCB	119,89,121	;Y y
; index 2
	FCB	111,50,34	;2 "
	FCB	95,58,42	;: *
	FCB	63,3,3	;break
	FCB	126,66,98	;B b
	FCB	125,74,106	;J j
	FCB	123,82,114	;R r
	FCB	119,90,122	;Z z
; index 3
	FCB	111,51,35	;3 #
	FCB	95,59,43	; +
	FCB	119,94,95	;up _
	FCB	126,67,99	;C c
	FCB	125,75,107	;K k
	FCB	123,83,115	;S s
	FCB	0,0,0	;unknown
; index4
	FCB	111,52,36	;4 $
	FCB	95,44,60	;, <
	FCB	119,10,91	;dn [
	FCB	126,68,100	;D d
	FCB	125,76,108	;L l
	FCB	123,84,116	;T t
	FCB	0,0,0	;unknown
; index 5
	FCB	111,53,37	;5 %
	FCB	95,45,61	;- =
	FCB	119,8,21	;left
	FCB	126,69,101	;E e
	FCB	125,77,109	;M m
	FCB	123,85,117	;U u
	FCB	0,0,0	;unknown
; index 6
	FCB	111,54,38	;6 &
	FCB	95,46,62	;. >
	FCB	119,9,93	; right ]
	FCB	126,70,102	; F f
	FCB	125,78,110	;N n
	FCB	123,86,118	;V v
	FCB	0,0,0	;unknown
;index 7
	FCB	111,55,39	;7 `
	FCB	63,1,1	;shift adjust hack
	FCB	119,32,32	;space
	FCB	95,47,63	;/ ?
	FCB	126,71,103	;G g
	FCB	125,79,111	;O o
	FCB	123,87,119	;W w
; ok code
INKEY2$	LDA	STATE
	BEQ	KEYS
	RTS
;	lbra	checkforkey	; future update
; just a key press check
KEYS	LDX	#KEYCHK
	LDB	#7
	LDY	#KEYSIN
GRABKEY	LDA	B,X
	STA	$FF02
	LDA	$FF00
	ANDA	#127
	STA	B,Y
	DECB
	BPL	GRABKEY
; if no key exit
	CLRB
NOKEYQ	LDA	B,Y
	CMPA	#127
	BNE	KEYSNEXT
	INCB
	CMPB	#8
	BLO	NOKEYQ
PASS	LDB	LASTKEY
	CMPB	#1
	BEQ	CLEARLASTKEY
	CLRA
	CLRB
	RTS	;return no key press
CLEARLASTKEY	CLR	LASTKEY
	CLRB
	CLRA
	RTS
; *************************************************************
KEYSNEXT	CLR	TEMPC	;tempc index counter
KEYLP0	LDX	#KEYTBL
	LDA	TEMPC
	LDB	#21
	MUL
	LEAX	D,X	;ready
	CLR	TEMPD
KEYLP1	LDB	TEMPC	;what index of 8?
	LDA	B,Y	;index value keyin
	CMPA	0,X
	BEQ	YUPKEY
	LEAX	3,X	;advance
	INC	TEMPD
	LDA	TEMPD
	CMPA	#8
	BHS	KEYLP2
	BRA	KEYLP1
KEYLP2	INC	TEMPC
	LDA	TEMPC
	CMPA	#8
	BHS	PASS
	BRA	KEYLP0
; shift key or not?
YUPKEY	LDB	SHIFTKEY
	LEAX	1,X
	LDA	B,X
	CMPA	#1
	BEQ	SHIFTADJ
	STA	LASTKEY
	CLRA
	LDB	LASTKEY
	RTS
KEYDN	CLRA
	LDB	#1
	STB	LASTKEY
	RTS
SHIFTADJ	LDB	LASTKEY
	CMPB	#1
	BEQ	KEYDN
	INC	SHIFTKEY
	LDB	SHIFTKEY
	CMPB	#2
	BLO	KEYDN
	CLR	SHIFTKEY
	CLRA
	LDB	#1
	STB	LASTKEY
	RTS
ENDFUNCTION
;
; ******************************************************************************************************************************
; * joystickfire( button) returns state on b
; ******************************************************************************************************************************
FUNCTION	JOYSTICKFIRE	BUTTON
DEF	BUTTON	BYTE
REM	; returns left fire buttons on 1,2 state      result in b register
REM	; returns right fire buttons on 3,4 state   result in b register
BUTTON	FCB	0
KEYCHK	FCB	254,253,251,247,239,223,191,127
KEYSIN	FCB	0,0,0,0,0,0,0,0
JOYSTICKFIRE	LDX	#KEYCHK
	LDB	#7
	LDY	#KEYSIN
GRABKEY	LDA	B,X
	STA	$FF02
	LDA	$FF00
	ANDA	#127
	STA	B,Y
	DECB
	BPL	GRABKEY
	LDA	BUTTON
	LBEQ	NOBUTTON
	CMPA	#1
	BEQ	CHECKLEFT1
	CMPA	#2
	BEQ	CHECKLEFT2
	CMPA	#3
	BEQ	CHECKRIGHT1
	CMPA	#4
	BEQ	CHECKRIGHT2
NOBUTTON	CLRB
	RTS
CHECKLEFT1	LDX	#KEYSIN
	CLRB
LOOPL1	LDA	B,X
	BITA	#2
	BNE	NOBUTTON
	INCB
	CMPB	#8
	BLO	LOOPL1
	LDB	#1
	RTS	;button is pressed
CHECKLEFT2	LDX	#KEYSIN
	CLRB
LOOPL2	LDA	B,X
	BITA	#8
	BNE	NOBUTTON
	INCB
	CMPB	#8
	BLO	LOOPL2
	LDB	#1
	RTS	;button is pressed
CHECKRIGHT1	LDX	#KEYSIN
	CLRB
LOOPL3	LDA	B,X
	BITA	#1
	BNE	NOBUTTON
	INCB
	CMPB	#8
	BLO	LOOPL3
	LDB	#1
	RTS	;button is pressed
CHECKRIGHT2	LDX	#KEYSIN
	CLRB
LOOPL4	LDA	B,X
	BITA	#4
	BNE	NOBUTTON
	INCB
	CMPB	#8
	BLO	LOOPL4
	LDB	#1
	RTS	;button is pressed
ENDFUNCTION
