;******************************************************************************
;SuperMouse function Entry
;
;
;
;******************************************************************************
.z80
mscdat	equ	prntdp		;data from mouse
mousts	equ	prntcp		;status from mouse
maxfnc	equ	21		;number of functions supported by mouse
;
mscfnc:
	push	af
	ld	a,c		;get mouse function number
	cp	maxfnc
	ret	nc		;function is not supported
	push	hl
	ld	hl,mscjtb
	sla	c
	ld	b,0
	add	hl,bc
	ld	c,(hl)
	inc	hl
	ld	b,(hl)
	pop	hl
	pop	af
	call	romsw
;
noperation:
	ret
mscjtb:
	dw	mscinq		;c = 0 inquire mouse available
	dw	mscint		;c = 1 mouse initialization
	dw	mscrng		;c = 2 mouse coord range
	dw	mscxy		;c = 3 mouse x,y coordinate
	dw	msctrm		;c = 4 terminate the mouse
	dw	msckym		;c = 5 mouse key mode
	dw	msckyp		;c = 6 mouse key programming
	dw	mscmov		;c = 7 mouse move
	dw	mscica		;c = 8 inquire cursor attributes and status
	dw	mscihc		;c = 9 initialize hardware cursor
	dw	mscshc		;c = 10 set hardware cursor attributes
	dw	mscmhc		;c = 11 move hardware cursor
	dw	mschcc		;c = 12 inquire hardware cursor coordinates
	dw	mscthc		;c = 13 terminate hardware cursor
	dw	noperation	;c = 14
	dw	noperation	;c = 15
	dw	noperation	;c = 16
	dw	noperation	;c = 17
	dw	modst		;c = 18 check modem input buffer ready
	dw	ttyin1		;c = 19 get a character from modem input buffer
	dw	mrcv_vect	;c = 20 set modem rcv_int_vector
;
;	Set modem RCV interrput vector to user handle routine
;	accepts:  hl = user subroutine address
;	returns:  none
;
mrcv_vect:
	ld	a,h
	or	l
	jr	nz,mrc_skip
	call	mrx_rset
	ei	
	ret
;
mrc_skip:
	ld	(mrx_idr+1),hl
	ei			;keep the system interrupt flag enable
	ret
mscinq:
	ld	a,(havhcr)	;get hardware cursor flag
	ld	b,a
	ld	a,(havmsc)	;get mouse flag
	ret
;
;******************************************************************************
;SuperMouse Interrupt Entry
;	Mouse Systems Corp Mouse routine
;
;	Mouse interrupt routine
;
;	Recives data from dart interrupt and builds xy coordinate and 
;	passes it to cursor handler.
;
;******************************************************************************
;	defs	16
;moustk	equ	$
;	defw	0		;user stack
;
intmsc:
	
	ld	(moustk),sp	;save user stack
	ld	sp,moustk	;load local stack
	push	af		;save all user registers
	push	bc
	push	de
	push	hl	
	in	a,(mscdat)	;get the byte
	ld	b,a		;save the mouse byte
	ld	hl,digcnt
	ld	a,(hl)		;get mouse count
	cp	5		;test if first byte of segquence
	jr	nz,digit1	;if not then test if last two bytes
	ld	a,b
	and	0f8h		;test if legit first byte of sequence
	cp	80h
	jp	nz,digtdn	;if not equal then exit wait for first byte
	dec	(hl)		;decrement mouse count
	ld	a,b
	cpl			;make a colsure =1
	and	7		;mask off the switch bits
	jr	z,digit0	;if none then exit
	jp	digswt
digit0:
	xor	a
	ld	(digsts),a	;clear switch status byte
	jp	digtdn
digitd:
	ld	hl,digcnt
	dec	(hl)
	jp	digtdn
;find delta  value
;for 2nd , third,fourth,fifth bytes
digit1:
	ld	a,b
	ld	e,b
	ld	d,0
	and	80h
	jr	z,digt10
	ld	d,0ffh		;de=delta value
digt10:
	ld	a,(hl)
	ld	hl,mscbrn	;point at branch
	sla	a		;mpy by two
	ld	c,a
	ld	b,0
	add	hl,bc
	ld	c,(hl)
	inc	hl
	ld	b,(hl)
	push	bc
	ret
mscfst:
	ld	(tmous1),de	;save  delta x1
	jr	digitd
msc2nd:
	ld	(tmous2),de	;save delta y1
	jr	digitd
msc3rd:
	ld	hl,(tmous1)
	add	hl,de
	ld	(tmous1),hl	;add  in second delta x
	jr	digitd
msc4th:
	ld	hl,(tmous2)
	add	hl,de		;hl=delta y
	ld	de,(gcury)	;get the current y coordinate
	add	hl,de
	ld	a,h
	and	a
	call	m,msczro
	ld	(gcury),hl
	ld	de,240
	xor	a
	sbc	hl,de
	call	nc,mscycl
	ld	hl,(tmous1)
	ld	de,(gcurx)
	add	hl,de
	ld	a,h
	and	a
	call	m,msczro
	ld	(gcurx),hl	;now have the new x,y coordinates
	ld	de,640
	xor	a
	sbc	hl,de
	call	nc,mscxcl
	ld	hl,digcnt
	ld	(hl),5		;reinit count
	ld	a,0ffh
	ld	(mscsts),a	;set data avail flag
digtdn:
	pop	hl		;restore all user registers
	pop	de
	pop	bc
	pop	af
	ld	sp,(moustk)	;restore user stack pointer.
	ei
	reti
;xclip routine
mscxcl:
	ld	hl,639
	ld	(gcurx),hl
	ret
;yclip routine
mscycl:
	ld	hl,239
	ld	(gcury),hl
	ret
msczro:
	ld	hl,0
	ret	
tmous1:	dw	0
tmous2:	dw	0
mscbrn:
	dw	0
	dw	msc4th
	dw	msc3rd
	dw	msc2nd
	dw	mscfst
	page
;******************************************************************************
;digswt
;	digitizer switch handling routine
;
;	entry	a = xxxxxsss
;		where x=don't care
;		      s=switch bit
;
;	exit	from interrupt level
;	uses	all
;******************************************************************************
digswt:	
	ld	hl,digsts
	ld	b,a		;save  switch contents
	ld	a,(mscmode)	;get mouse key mode
	or	a
	jr	z,msmode0	;go to mouse key mode 0
	ld	(hl),b		;update switch status
	jr	digswd		;skip input
msmode0:
	ld	a,(hl)		;get the switch status byte
	and	a
	ld	(hl),b		;update switch status
 	jr	nz,resdig	;reset mouse tracking flag
	ld	a,b
	ld	c,0
	cp	4
	jr	z,digswl
	cp	2
	jr	z,digswm
digswr:	inc	c
digswm:	inc	c
digswl:	ld	b,0
	ld	hl,mkytab
	add	hl,bc
	ld	b,(hl)
	call	storkb
	jr	z,digswd
resdig:	ld	a,5
	ld	(digcnt),a	;disable mouse tracking but input char
;
digswd:
	jp	digtdn		;don't handle the switch
;
; special receive condition for mouse
;
srcmsc:	ld	(moustk),sp	;save user stack
	ld	sp,moustk	;set local stack
	push	af
	ld	a,30h		;reset error condition
	out	(mousts),a	;send out to control reg
srcms1:	in	a,(mousts)	;read mouse status
	and	01		;receive ready
	jr	z,srcms2	;receive is empty
	in	a,(mscdat)	;get data
	nop			;delay a little bit
	jr	srcms1
;
srcms2:	ld	a,10h		;reset ext status interrupt latch
	out	(mousts),a
	ld	a,5
	ld	(digcnt),a	;reset mouse sequence
	pop	af
	ld	sp,(moustk)	;restore user stack
	ei
	reti
;
;mouse default configuration
;
havhcr:	db	0		;hardware cursor flag
;
;end of tpcimous.mac



