; Driver MOUSE from PAINTER
;=======================================
JoyPort	equ	0	;0 - JoyPort2/ 1 - JoyPort1
;
.phase
	org	2A50h	
;----------------------------------------
L_2A50:		push	de
		push	hl
		ld	hl, 0F0Eh	; Ports	PSG 15,14
		ld	a, h
		di
		out	(0A0h),	a
		in	a, (0A2h)
		and	8Fh
  if JoyPort
		or	10h		; Bit 4 StrobJoyPort1 = 1
					; Bit 6	= 0  JoyPort = 1
  else
		or	60h		; Bit 5 StrobJoyPort2 = 1
					; Bit 6 = 1  JoyPort = 2 
  endif
		ld	d, a		; Save param-s JoyPort1
		ld	a, h
		out	(0A0h),	a
		ld	a, d
		out	(0A1h),	a	; Strob	JoyPort1 = 1 _/~
		ld	b, 0Fh		; Pause
loc_2A68:
		djnz	loc_2A68
		call	sub_2A8B	; Read X(7..4)
		call	sub_2A9E	; Read X(3..0)
		and	0Fh
		or	c
		ld	b, a		; B=X
		call	sub_2A8B	; Read Y(7..4)
		pop	hl
		ld	d, c
		ld	c, b		; C=X
		in	a, (0A2h)	; Read Y(3..0)
		ei
		ld	e, a
		and	0Fh
		or	d
		ld	b, a		; B=Y
		ld	a, e		; Bit5,4 - Buttons
		rla
		rla
		and	0C0h
		xor	0C0h		; A(7,6)=Buttons
		pop	de
		ret
;========================================
sub_2A8B:
  if	JoyPort
		res	4, d		; Strob Port1 = 0
  else
		res	5, d		; Strob Port2 = 0
  endif
		ld	a, l		; Strob	= 0
		out	(0A0h),	a	; Select PSG Port 14
		call	sub_2A9E
		add	a, a
		add	a, a
		add	a, a
		add	a, a
		ld	c, a
  if	JoyPort
		set	4, d		; Strob Port1 = 1
  else
		set	5, d		; Strob Port2 = 1
  endif
		ld	a, l
		out	(0A0h),	a
		ret
; =======================================
sub_2A9E:
		in	a, (0A2h)
		ld	e, a		; Read PSG Port	14
		ld	a, h
		out	(0A0h),	a	; Select PSG Port 15
		ld	a, d
		out	(0A1h),	a	; Set JoyStrob
		ld	a, e
		ret
;========================================
	end
