NEW
  AUTO 3,1
				.LIST OFF	
				.OP	65C02
				.OR $2000
				.TF drv/mouse.drv
*--------------------------------------
				.INB inc/macros.i
				.INB inc/a2osx.i
				.INB inc/kernel.i
				.INB inc/io.i
				.INB inc/mli.e.i
				.INB inc/xy.i
				.INB inc/xy.mouse.i
*--------------------------------------
ZPSlotPtr		.EQ ZPBIN
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp Dev.Detect			cld,jmp abs=DRV
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					DRV Layout Version 1
				.DA 0
				.DA CS.END-CS.START			Code Length
				.DA 0
				.DA 0
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
L.MSG.DETECT	.DA MSG.DETECT
L.MSG.DETECT.OK	.DA MSG.DETECT.OK
L.MSG.DETECT.KO	.DA MSG.DETECT.KO
L.DRV.CS.START	.DA DRV.CS.START
L.FD.DEV		.DA FD.DEV
L.FD.DEV.NAME	.DA FD.DEV.NAME
				.DA 0					End Of Reloc Table
*--------------------------------------
Dev.Detect		>LDYA L.MSG.DETECT
				>SYSCALL PutS

				stz ZPSlotPtr
				lda #$C1

.1				sta ZPSlotPtr+1
				ldx #DEVSIG.Value-DEVSIG.Offset-1

.2				ldy DEVSIG.Offset,x
				lda (ZPSlotPtr),y
				cmp DEVSIG.Value,x
				bne .3

				dex
				bpl .2

				bra .8

.3				lda ZPSlotPtr+1			no match, try next slot....
				inc
				cmp #$C8
				bne .1	

				>LDYA L.MSG.DETECT.KO
				>SYSCALL PutS

.9				lda #MLI.E.NODEV
				sec
				rts
				
.8				lda ZPSlotPtr+1
				sta GOMOUSE.LDX+2
				sta GOMOUSE.JMP+2
				and #$0F
				sta DEVSLOT0n
				tax

				ora #$30
				sta FD.DEV.NAME+3
				asl
				asl
				asl
				asl
				sta GOMOUSE.n0+1
				
				lda #A2osX.S.XY
				sta A2osX.S-1,x

				>PUSHW L.MSG.DETECT.OK
				>PUSHW L.FD.DEV.NAME
				>PUSHBI 2
				>SYSCALL PrintF
	
				>PUSHWI DRV.END
				>PUSHWI DRV.CS.END-DRV.CS.START
				>PUSHWI DRV.CS.START
				>LDYA L.DRV.CS.START
				>SYSCALL InsDrv
				bcs .99

				>STYA FD.DEV+S.FD.DEV.DRVPTR

				>PUSHW L.FD.DEV
				>PUSHW L.FD.DEV.NAME
				>SYSCALL MKDev

.99				rts
*--------------------------------------
CS.END
MSG.DETECT		.CZ "Apple Mouse Driver."
MSG.DETECT.OK	.CZ "Mouse Installed As Device : %s\r\n"
MSG.DETECT.KO	.CZ "No Mouse Found."
DEVSIG.Offset	.HS 05070B0CFB
DEVSIG.Value	.HS 38180120D6
*--------------------------------------
FD.DEV			.DA #S.FD.T.CDEV
				.DA #0					HANDLER
				.DA #0					BUSID
				.DA #0					DEVID
				.DA 0					BUSPTR
				.BS 2					DRVPTR
				.DA 0					DCBPTR
				.DA 0					BUFPTR
FD.DEV.NAME		.AZ "mou1"
*--------------------------------------
*			Driver Code
*--------------------------------------
ZPIOCTL			.EQ ZPDRV
ZPBufPtr		.EQ ZPDRV+2
ZPState			.EQ ZPDRV+4
*--------------------------------------
DRV.CS.START	cld
				jmp (.1,x)
.1				.DA STATUS
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA CONTROL
				.DA A2osX.BADCALL
				.DA OPEN
				.DA CLOSE
				.DA READ
				.DA A2osX.BADCALL
				.DA 0					end or relocation
*--------------------------------------
STATUS			>STYA ZPIOCTL

				ldy #S.IOCTL.BUFPTR
				lda (ZPIOCTL),y
				sta ZPBufPtr
				iny 
				lda (ZPIOCTL),y
				sta ZPBufPtr+1

				ldy #S.IOCTL.S
				lda (ZPIOCTL),y
				beq .1

				cmp #S.IOCTL.S.GETDIB
				bne STATUS.DCB

				ldy #S.DIB-1
				.HS 2C					bit abs

.1				ldy #3

.2				lda DIB,y
				sta (ZPBufPtr),y
				dey
				bpl .2

				clc
				rts

STATUS.DCB		
STATUS.9		lda #MLI.E.BADCTL
				sec
				rts
*--------------------------------------
CONTROL			>STYA ZPBufPtr			NON STANDARD

				php
				sei

				ldy #0
				ldx #0					set X
				jsr SETCLAMP

				ldy #4
				ldx #1					set Y
				jsr SETCLAMP

				ldy #HOMEMOUSE
				bra EXIT1
*--------------------------------------
OPEN			php
				sei
			
				lda #S.DIB.S.OPENED
				tsb DIB+S.DIB.S

				ldx DEVSLOT0n
				lda MOUSEMODx,x			keep if VBLInt already activated by kernel
				and #MOUSEMODx.INTVBL
				ora #MOUSEMODx.ACTIVE	Enable, Polling mode
				bra EXIT0
				
*				jsr GOMOUSE

*				ldy #INITMOUSE			Already called by IRQManager
*				jsr GOMOUSE				CLEAR $2000-3FFF AND MESS SETHIRES
*				plp
*				rts	
*--------------------------------------
CLOSE			php
				sei

				lda #S.DIB.S.OPENED
				trb DIB+S.DIB.S

				ldx DEVSLOT0n
				lda MOUSEMODx,x			keep if VBLInt already activated by kernel
				and #MOUSEMODx.INTVBL	Disable ALL but VBLInt if previously active

EXIT0			ldy #SETMOUSE

EXIT1			jsr GOMOUSE

				plp
				clc
				rts
*--------------------------------------
READ			>STYA ZPBufPtr			NON STANDARD

				stz ZPState				Reset Flags

				php
				sei
				
				sta IO.CLRPAGE2

*				ldy #READMOUSE			DONE BY IRQ MANAGER
*				jsr GOMOUSE

				ldx DEVSLOT0n
				lda MOUSESTSx,x
				and #MOUSESTSx.MOVED
				beq .1

				ldy #S.XY.X1

				lda MOUSEXLOx,x
				sta (ZPBufPtr),y
				iny
				lda MOUSEXHIx,x
				sta (ZPBufPtr),y
				iny
				lda MOUSEYLOx,x
				sta (ZPBufPtr),y
				iny
				lda MOUSEYHIx,x
				sta (ZPBufPtr),y

				lda #S.XY.S.MOVE
				tsb ZPState			

				stz bDblClick			Reset DblClick flag

.1				lda MOUSESTSx,x
				and #MOUSESTSx.DOWN+MOUSESTSx.LDOWN
				beq .8					no button state change:up & previously up

				cmp #MOUSESTSx.DOWN+MOUSESTSx.LDOWN
				bne .10

				lda ZPState				no button state change:down & previously down
				and #S.XY.S.MOVE
				beq .8
				
				sec
				ror bDrag
				
				lda #S.XY.S.DRAGSTART
				tsb ZPState			
				bra .8

.10				asl
				bcc .2					if CC, up & previously down

				lda #S.XY.S.DOWN		if CS, down & previously up
				tsb ZPState				button has been pressed
			
				ldy #S.XY.X1+3
				
.11				lda (ZPBufPtr),y		record pos if it is a drag start
				sta SrcDrag-S.XY.X1,y
				dey
				bpl .11
				
				bra .8
*--------------------------------------
.2				lda #S.XY.S.UP
				tsb	ZPState
				
				bit bDrag
				bmi	.4					

				lda #S.XY.S.CLK			no move since down...click!
				bit bDblClick
				bpl .3

				lda #S.XY.S.DCLK

				ldy #1
				sty bDblClick

.3				dec bDblClick

				tsb ZPState
				bra .8
*--------------------------------------
.4				lda #S.XY.S.DRAGEND
				tsb ZPState

				ldy #S.XY.X2+3

.5				lda SrcDrag-S.XY.X2,y
				sta (ZPBufPtr),y
				dey
				bpl .5
				
				stz bDrag
*--------------------------------------
.8				plp

				lda ZPState
				beq .9

				ldy #S.XY.S
				sta (ZPBufPtr),y
				clc
				rts

.9				lda	#MLI.E.EOF
				sec
				rts
*--------------------------------------
* PRIVATE
*--------------------------------------
SETCLAMP		lda (ZPBufPtr),y
				sta LOCLAMPLO

				iny
				lda (ZPBufPtr),y
				sta LOCLAMPHI

				iny
				lda (ZPBufPtr),y
				sta HICLAMPLO

				iny
				lda (ZPBufPtr),y
				sta HICLAMPHI

				txa
				ldy #CLAMPMOUSE 
*--------------------------------------
GOMOUSE
GOMOUSE.LDX		ldx $C100,y				SELF MODIFIED
				stx GOMOUSE.JMP+1
				ldx GOMOUSE.JMP+2
GOMOUSE.n0		ldy #$10				SELF MODIFIED
GOMOUSE.JMP		jmp $C100				SELF MODIFIED		
*--------------------------------------
DRV.CS.END
DEVSLOT0n		.BS 1
bDblClick		.BS 1
bDrag			.BS 1
SrcDrag			.BS 4
*--------------------------------------
DIB				.DA #0
				.DA #0,#0,#0
				.PS "Apple II Mouse"
				.BS 2
				.DA #S.DIB.T.XY
				.BS 1					Subtype
				.DA K.VER				Version
*--------------------------------------
DRV.END
*--------------------------------------
				.LIST ON
DRV.CS.SIZE		.EQ DRV.CS.END-DRV.CS.START
DRV.SIZE		.EQ DRV.END-DRV.CS.START
				.LIST OFF
MAN
SAVE usr/src/drv/mouse.drv.s
ASM
