#dontinclude
; include file
; universal joystick 1 player code



; *************************************************************************************
; * universaljoystick()     single player coded joystick (doesnt matter which one you want)
; *************************************************************************************
function	universaljoystick
rem	universaljoystick()
rem	waits for first button press clr $15c to re-assign
rem	uses only l,r from coco.  $15a=y $15b=x $15c=device $15d=buttons

universaljoystick_joyy1	equ	$15a	;y
universaljoystick_joyx1	equ	$15b	;x
universaljoystick_joyd	equ	$15c	;device
universaljoystick_joyb	equ	$15d	;buttons roll over
universaljoystick_keychk	fcb	254,253,251,247,239,223,191,127

def	universaljoystick_setb1	byte
def	universaljoystick_setb2	byte
def	universaljoystick_keysin	string	8
def	universaljoystick_button	byte

universaljoystick
	lda	universaljoystick_joyd	;check if we have a device
	beq	universaljoystick_lookjoy
	cmpa	#4
	bhi	universaljoystick_lookjoy
	cmpa	#4
	lbls	universaljoystick_updatejoy
universaljoystick_lookjoy
	ldx	#universaljoystick_keychk
	ldb	#7
	ldy	#universaljoystick_keysin
universaljoystick_grabkey
	lda	b,x
	sta	$ff02
	lda	$ff00
	anda	#127
	sta	b,y
	decb
	bpl	universaljoystick_grabkey
	lda	#4
	sta	universaljoystick_button
universaljoystick_grabkey2
	lda	universaljoystick_button
	cmpa	#1
	lbeq	universaljoystick_checkleft1
	cmpa	#2
	lbeq	universaljoystick_checkleft2
	cmpa	#3
	lbeq	universaljoystick_checkright1
	cmpa	#4
	lbeq	universaljoystick_checkright2
universaljoystick_nobtn
	dec	universaljoystick_button
	lda	universaljoystick_button
	bne	universaljoystick_grabkey2
	rts
universaljoystick_checkleft1
	ldx	#universaljoystick_keysin
	clrb
universaljoystick_loopl1
	lda	b,x
	bita	#2
	bne	universaljoystick_nobtn
	incb
	cmpb	#8
	blo	universaljoystick_loopl1
	ldb	#1
	stb	$15c
	rts
universaljoystick_checkleft2
	ldx	#universaljoystick_keysin
	clrb
universaljoystick_loopl2
	lda	b,x
	bita	#8
	bne	universaljoystick_nobtn
	incb
	cmpb	#8
	blo	universaljoystick_loopl2
	ldb	#1
	stb	$15c
	rts	;button is pressed
universaljoystick_checkright1
	ldx	#universaljoystick_keysin
	clrb
universaljoystick_loopl3
	lda	b,x
	bita	#1
	bne	universaljoystick_nobtn
	incb
	cmpb	#8
	blo	universaljoystick_loopl3
	ldb	#2
	stb	$15c
	rts	;button is pressed
universaljoystick_checkright2
	ldx	#universaljoystick_keysin
	clrb
universaljoystick_loopl4
	lda	b,x
	bita	#4
	bne	universaljoystick_nobtn
	incb
	cmpb	#8
	blo	universaljoystick_loopl4
	ldb	#2
	stb	$15c
	rts	;button is pressed
universaljoystick_updatejoy
	lda	universaljoystick_joyd
	beq	universaljoystick_nodev
	cmpa	#1
	lbeq	universaljoystick_leftinput1
	cmpa	#2
	lbeq	universaljoystick_rightinput1
	rts	;add more later
universaljoystick_nodev
	lda	#31
	sta	$15a
	sta	$15b
	jsr	universaljoystick_sndon
; ok buttons
	clr	$15d
	rts
; left input
; lda	$ff23
; anda	#247
; sta	$ff23	;kill sound
; setup for left y
universaljoystick_leftinput1
	lda	$ff03
	anda	#247
	adda	#8
	sta	$ff03
	lda	$ff01
	anda	#247
	adda	#8
	sta	$ff01
	clrb
universaljoystick_joyly
	stb	$ff20
	lda	$ff00
	bpl	universaljoystick_joylyd	;done
	addb	#$04
	cmpb	#$fc
	beq	universaljoystick_joylyd
	bra	universaljoystick_joyly
universaljoystick_joylyd
	lsrb
	lsrb
	stb	universaljoystick_joyy1
; x axis
	lda	$ff01
	anda	#247
	sta	$ff01	;cleared mux bit
	clrb
universaljoystick_joylx
	stb	$ff20
	lda	$ff00
	bpl	universaljoystick_joylxd	;done
	addb	#$04
	cmpb	#$fc
	beq	universaljoystick_joylxd
	bra	universaljoystick_joylx
universaljoystick_joylxd
	lsrb
	lsrb
	stb	universaljoystick_joyx1
; turn sound back on
	jsr	universaljoystick_sndon
; ok buttons
	clr	$15d
	rts
; right input
; lda	$ff23
; anda	#247
; sta	$ff23	;kill sound
; setup for right y
universaljoystick_rightinput1
	lda	$ff03
	anda	#247
	sta	$ff03
	lda	$ff01
	anda	#247
	adda	#8
	sta	$ff01
	clrb
universaljoystick_joyry
	stb	$ff20
	lda	$ff00
	bpl	universaljoystick_joyryd	;done
	addb	#$04
	cmpb	#$fc
	beq	universaljoystick_joyryd
	bra	universaljoystick_joyry
universaljoystick_joyryd
	lsrb
	lsrb
	stb	universaljoystick_joyy1
; x axis
	lda	$ff01
	anda	#247
	sta	$ff01	;cleared mux bit
	lda	$ff03
	anda	#247
	sta	$ff03
	clrb
universaljoystick_joyrx
	stb	$ff20
	lda	$ff00
	bpl	universaljoystick_joyrxd	;done
	addb	#$04
	cmpb	#$fc
	beq	universaljoystick_joyrxd
	bra	universaljoystick_joyrx
universaljoystick_joyrxd
	lsrb
	lsrb
	stb	universaljoystick_joyx1
; turn sound back on
	jsr	universaljoystick_sndon
; ok buttons
	clr	$15d
	rts
; turn sound back on
universaljoystick_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
;



; ******************************************************************************************************************************
; * joystickfire( button) returns state on b
; ******************************************************************************************************************************
function	universaljoystick_fire	universaljoystick_fire_button
def	universaljoystick_fire_button	byte
rem	universaljoystick_fire()
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

universaljoystick_fire_keychk	fcb	254,253,251,247,239,223,191,127
def	universaljoystick_fire_keysin	string	8

universaljoystick_fire
	ldx	#universaljoystick_fire_keychk
	ldb	#7
	ldy	#universaljoystick_fire_keysin
universaljoystick_fire_grabkey
	lda	b,x
	sta	$ff02
	lda	$ff00
	anda	#127
	sta	b,y
	decb
	bpl	universaljoystick_fire_grabkey
	lda	universaljoystick_fire_button
	lbeq	universaljoystick_fire_nobutton
	cmpa	#1
	beq	universaljoystick_fire_checkleft1
	cmpa	#2
	beq	universaljoystick_fire_checkleft2
	cmpa	#3
	beq	universaljoystick_fire_checkright1
	cmpa	#4
	beq	universaljoystick_fire_checkright2
universaljoystick_fire_nobutton
	clrb
	rts
universaljoystick_fire_checkleft1
	ldx	#universaljoystick_fire_keysin
	clrb
universaljoystick_fire_loopl1
	lda	b,x
	bita	#2
	bne	universaljoystick_fire_nobutton
	incb
	cmpb	#8
	blo	universaljoystick_fire_loopl1
	ldb	#1
	rts	;button is pressed
universaljoystick_fire_checkleft2
	ldx	#universaljoystick_fire_keysin
	clrb
universaljoystick_fire_loopl2
	lda	b,x
	bita	#8
	bne	universaljoystick_fire_nobutton
	incb
	cmpb	#8
	blo	universaljoystick_fire_loopl2
	ldb	#1
	rts	;button is pressed
universaljoystick_fire_checkright1
	ldx	#universaljoystick_fire_keysin
	clrb
universaljoystick_fire_loopl3
	lda	b,x
	bita	#1
	bne	universaljoystick_fire_nobutton
	incb
	cmpb	#8
	blo	universaljoystick_fire_loopl3
	ldb	#1
	rts	;button is pressed
universaljoystick_fire_checkright2
	ldx	#universaljoystick_fire_keysin
	clrb
universaljoystick_fire_loopl4
	lda	b,x
	bita	#4
	bne	universaljoystick_fire_nobutton
	incb
	cmpb	#8
	blo	universaljoystick_fire_loopl4
	ldb	#1
	rts	;button is pressed
endfunction





