; universal joystick 1 player code
; *************************************************************************************
; * universaljoy1()     single player coded joystick (doesnt matter which one you want)
; *************************************************************************************
FUNCTION	UNIVERSALJOY1
REM	; universaljoy1()  waits for first button press clr $15c to re-assign
REM	; uses only l,r from coco.  $15a=y $15b=x $15c=device $15d=0 use joystickfire
JOYY1	EQU	$15A	;y
JOYX1	EQU	$15B	;x
JOYD	EQU	$15C	;device
JOYB	EQU	$15D	;buttons roll over
SETB1	FCB	0
SETB2	FCB	0
KEYCHK	FCB	254,253,251,247,239,223,191,127
KEYSIN	FCB	0,0,0,0,0,0,0,0
BUTTON	FCB	0
UNIVERSALJOY1	LDA	JOYD	;check if we have a device
	BEQ	LOOKJOY
	CMPA	#4
	BHI	LOOKJOY
	CMPA	#4
	LBLS	UPDATEJOY
LOOKJOY	LDX	#KEYCHK
	LDB	#7
	LDY	#KEYSIN
GRABKEY	LDA	B,X
	STA	$FF02
	LDA	$FF00
	ANDA	#127
	STA	B,Y
	DECB
	BPL	GRABKEY
	LDA	#4
	STA	BUTTON
GRABKEY2	LDA	BUTTON
	CMPA	#1
	LBEQ	CHECKLEFT1
	CMPA	#2
	LBEQ	CHECKLEFT2
	CMPA	#3
	LBEQ	CHECKRIGHT1
	CMPA	#4
	LBEQ	CHECKRIGHT2
NOBTN	DEC	BUTTON
	LDA	BUTTON
	BNE	GRABKEY2
	RTS
CHECKLEFT1	LDX	#KEYSIN
	CLRB
LOOPL1	LDA	B,X
	BITA	#2
	BNE	NOBTN
	INCB
	CMPB	#8
	BLO	LOOPL1
	LDB	#1
	STB	$15C
	RTS
CHECKLEFT2	LDX	#KEYSIN
	CLRB
LOOPL2	LDA	B,X
	BITA	#8
	BNE	NOBTN
	INCB
	CMPB	#8
	BLO	LOOPL2
	LDB	#1
	STB	$15C
	RTS	;button is pressed
CHECKRIGHT1	LDX	#KEYSIN
	CLRB
LOOPL3	LDA	B,X
	BITA	#1
	BNE	NOBTN
	INCB
	CMPB	#8
	BLO	LOOPL3
	LDB	#2
	STB	$15C
	RTS	;button is pressed
CHECKRIGHT2	LDX	#KEYSIN
	CLRB
LOOPL4	LDA	B,X
	BITA	#4
	BNE	NOBTN
	INCB
	CMPB	#8
	BLO	LOOPL4
	LDB	#2
	STB	$15C
	RTS	;button is pressed
UPDATEJOY	LDA	JOYD
	BEQ	NODEV
	CMPA	#1
	LBEQ	LEFTINPUT1
	CMPA	#2
	LBEQ	RIGHTINPUT1
	RTS	;add more later
NODEV	LDA	#31
	STA	$15A
	STA	$15B
	JSR	SNDON
; ok buttons
	CLR	$15D
	RTS
; left input
; LDA	$FF23
; ANDA	#247
; STA	$FF23	;kill sound
; setup for left y
LEFTINPUT1	LDA	$FF03
	ANDA	#247
	ADDA	#8
	STA	$FF03
	LDA	$FF01
	ANDA	#247
	ADDA	#8
	STA	$FF01
	CLRB
JOYLY	STB	$FF20
	LDA	$FF00
	BPL	JOYLYD	;done
	ADDB	#$04
	CMPB	#$FC
	BEQ	JOYLYD
	BRA	JOYLY
JOYLYD	LSRB
	LSRB
	STB	JOYY1
; x axis
	LDA	$FF01
	ANDA	#247
	STA	$FF01	;cleared mux bit
	CLRB
JOYLX	STB	$FF20
	LDA	$FF00
	BPL	JOYLXD	;done
	ADDB	#$04
	CMPB	#$FC
	BEQ	JOYLXD
	BRA	JOYLX
JOYLXD	LSRB
	LSRB
	STB	JOYX1
; turn sound back on
	JSR	SNDON
; ok buttons
	CLR	$15D
	RTS
; right input
; LDA	$FF23
; ANDA	#247
; STA	$FF23	;kill sound
; setup for right y
RIGHTINPUT1	LDA	$FF03
	ANDA	#247
	STA	$FF03
	LDA	$FF01
	ANDA	#247
	ADDA	#8
	STA	$FF01
	CLRB
JOYRY	STB	$FF20
	LDA	$FF00
	BPL	JOYRYD	;done
	ADDB	#$04
	CMPB	#$FC
	BEQ	JOYRYD
	BRA	JOYRY
JOYRYD	LSRB
	LSRB
	STB	JOYY1
; x axis
	LDA	$FF01
	ANDA	#247
	STA	$FF01	;cleared mux bit
	LDA	$FF03
	ANDA	#247
	STA	$FF03
	CLRB
JOYRX	STB	$FF20
	LDA	$FF00
	BPL	JOYRXD	;done
	ADDB	#$04
	CMPB	#$FC
	BEQ	JOYRXD
	BRA	JOYRX
JOYRXD	LSRB
	LSRB
	STB	JOYX1
; turn sound back on
	JSR	SNDON
; ok buttons
	CLR	$15D
	RTS
; turn sound back on
SNDON	CLRB
; sound pop hopefully gone
;	LDA	$FF23
;	ANDA	#247
;	ADDA	#8
;	STA	$FF23
	LDA	$FF03
	ANDA	#247
	ADDA	#8
	STA	$FF03
	LDA	$FF01
	ANDA	#247
	STA	$FF01
	RTS
ENDFUNCTION
;
;
; *************************************************************************************
; * joysticks()     grabs both and returns
; *************************************************************************************
FUNCTION	JOYSTICKS
REM	; joysticks()  grabs both joystick ports from coco $15a,$15b for left and $15c,15d$ for right
REM	; important! (requires inkey$) data= x0000000 has buttons press encoded [and values 127]
JOYY1	EQU	$15A	;y
JOYX1	EQU	$15B	;x
JOYY2	EQU	$15C	;y
JOYX2	EQU	$15D	;x
SETB1	FCB	0
SETB2	FCB	0
; LDA	$FF23
; ANDA	#247
; STA	$FF23	;kill sound
JOYSTICKS	JSR	LEFTINPUT1
	JSR	RIGHTINPUT1
; sound pop hopefully gone
;	LDA	$FF23
;	ANDA	#247
;	ADDA	#8
;	STA	$FF23
	LDA	$FF03
	ANDA	#247
	STA	$FF03
	LDA	$FF01
	ANDA	#247
	STA	$FF01
	RTS
; left input
LEFTINPUT1	; setup for left y
	LDA	$FF03
	ANDA	#247
	ADDA	#8
	STA	$FF03
	LDA	$FF01
	ANDA	#247
	ADDA	#8
	STA	$FF01
	CLRB
JOYLY	STB	$FF20
	LDA	$FF00
	BPL	JOYLYD	;done
	ADDB	#$04
	CMPB	#$FC
	BEQ	JOYLYD
	BRA	JOYLY
JOYLYD	LSRB
	LSRB
	STB	JOYY1
; x axis
	LDA	$FF01
	ANDA	#247
	STA	$FF01	;cleared mux bit
	CLRB
JOYLX	STB	$FF20
	LDA	$FF00
	BPL	JOYLXD	;done
	ADDB	#$04
	CMPB	#$FC
	BEQ	JOYLXD
	BRA	JOYLX
JOYLXD	LSRB
	LSRB
	STB	JOYX1
; ok buttons
	CLR	SETB1
	CLR	SETB2
	LDD	$FFFE
	ADDD	$FFFF
	CMPA	#167
	BEQ	COCOJLB3	;joystick buttons from 3
; for left input
	LDA	$FF00
	BITA	#2
	BNE	NOPEB1
	LDB	#128
	STB	SETB1
NOPEB1	BITA	#1
	LBNE	CCLE1	;exit
	LDB	#128
	STB	SETB2
CCLE1	LDA	JOYY1
	ADDA	SETB1
	STA	JOYY1
	LDA	JOYX1
	ADDA	SETB2
	STA	JOYX1
	RTS
COCOJLB3	LDA	$0152
	BITA	#2
	BNE	CCL3
	LDB	#128
	STB	SETB1
CCL3	BITA	#8
	BNE	CCLE1
	LDB	#128
	STB	SETB2
	BRA	CCLE1
; end of that
; right input
RIGHTINPUT1	LDA	$FF03
	ANDA	#247
	STA	$FF03
	LDA	$FF01
	ANDA	#247
	ADDA	#8	
	STA	$FF01
	CLRB
JOYRY	STB	$FF20
	LDA	$FF00
	BPL	JOYRYD	;done
	ADDB	#$04
	CMPB	#$FC
	BEQ	JOYRYD
	BRA	JOYRY
JOYRYD	LSRB
	LSRB
	STB	JOYY2
; x axis
	LDA	$FF01
	ANDA	#247
	STA	$FF01	;cleared mux bit
	LDA	$FF03
	ANDA	#247
	STA	$FF03
	CLRB
JOYRX	STB	$FF20
	LDA	$FF00
	BPL	JOYRXD	;done
	ADDB	#$04
	CMPB	#$FC
	BEQ	JOYRXD
	BRA	JOYRX
JOYRXD	LSRB
	LSRB
	STB	JOYX2
	CLR	SETB1
	CLR	SETB2
; ok buttons
	LDD	$FFFE
	ADDD	$FFFF
	CMPA	#167
	BEQ	COCOJRB3	;joystick buttons from 3
; nope regular coco
	LDA	$FF00
	BITA	#2
	BNE	NOPEB2
	LDB	#128
	STB	SETB2
NOPEB2	BITA	#1
	BNE	CCRE1	;exit
	LDB	#128
	STB	SETB1
CCRE1	LDA	JOYY2
	ADDA	SETB1
	STA	JOYY2
	LDA	JOYX2
	ADDA	SETB2
	STA	JOYX2
	RTS
; right
COCOJRB3	LDA	$0152
	BITA	#1
	BNE	CCR3
	LDB	#128
	STB	SETB1
CCR3	BITA	#4
	BNE	CCRE1
	LDB	#128
	STB	SETB2
	BRA	CCRE1
ENDFUNCTION
