
;[BEGIN]
;//MODULE: INTMOUSE
;//CREATE: 19-05-1998	AUTHOR:	Denis Parinov
;//UPDATE: 24-10-1999	DNS	Restore	module

CMOUSE	EQU	#1B
DMOUSE	EQU	#1A
Y_PORT	EQU	#89
VPAGE	EQU	#C9

; MOUSE	SOFTWARE SPECIFICATION
;---------------------------------------------
; COMMAND 00h (INITIALIZATION)
;
;  RETURN: NC -	MOUSE PRESENT
;	    C -	MOUSE ABSENT
;---------------------------------------------
; COMMAND 01h (SHOW MOUSE CURSOR)
;
;  RETURN: NC -	DONE
;	    C -	MOUSE ON SCREEN
;---------------------------------------------
; COMMAND 02h (HIDE MOUSE CURSOR)
;
;  RETURN: NC -	DONE
;	    C -	NONE MOUSE
;---------------------------------------------
; COMMAND 03h (READ MOUSE STATE)
;
;  RETURN: HL -	X COORD
;	   DE -	Y COORD
;	    A -	BUTTONS	D2-D0 (MIDDLE,RIGHT,LEFT)
;---------------------------------------------
; COMMAND 04h (GOTO MOUSE CURSOR)
;
;   INPUT: HL -	X COORD
;	   DE -	Y COORD
;---------------------------------------------
; COMMAND 05h (RESERVED)
;---------------------------------------------
; COMMAND 06h (RESERVED)
;---------------------------------------------
; COMMAND 07h (SET VERT. BOUNDS)
;
;   INPUT: HL -	Y MIN
;	   DE -	Y MAX
;---------------------------------------------
; COMMAND 08h (SET HORZ. BOUNDS)
;
;   INPUT: HL -	X MIN
;	   DE -	X MAX
;---------------------------------------------
; COMMAND 09h (LOAD CURSOR)
;
;   INPUT: IX -	CURSOR IMAGE
;	    H -	HEIGHT CURSOR
;	    L -	WIDTH CURSOR
;	    D -	Y HOT SPOT
;	    E -	X HOT SPOT
;	    B =	0
;---------------------------------------------
; COMMAND 0Ah (SET CURSOR IN TEXT MODES)
;
;   INPUT: H - AND SIMBOL MASK
;	   L - XOR SIMBOL MASK
;	   D - AND ATTRIBUT MASK
;	   E - XOR ATTRIBUT MASK
;	   B = 0
;---------------------------------------------
; COMMAND 0Bh (RETURN CURSOR)
;
;   INPUT: IX -	CURSOR IMAGE BUFFER
;  OUTPUT:  H -	HEIGHT CURSOR
;	    L -	WIDTH CURSOR
;	    D -	Y HOT SPOT
;	    E -	X HOT SPOT
;---------------------------------------------
; COMMAND 0Ch (RESERVED)
;---------------------------------------------
; COMMAND 0Dh (RESERVED)
;---------------------------------------------
; COMMAND 0Eh (GET SENSETIVE)
;  OUTPUT: H - VERTICAL	SENSETIVE
;	   L - HORIZONTAL SENSETIVE
;---------------------------------------------
; COMMAND 0Fh (SET SENSETIVE)
;   INPUT: H - VERTICAL	SENSETIVE
;	   L - HORIZONTAL SENSETIVE
;---------------------------------------------
; COMMAND 80h (MOUSE HARDWARE INTERRUPT)
;---------------------------------------------
; COMMAND 81h (CHANGE VIDEO MODE)
;   INPUT: A - MODE
;---------------------------------------------
; COMMAND 82h (RESERVED)
;---------------------------------------------
; COMMAND 83h (MOUSE REFRESH)
;---------------------------------------------
; ERRORS:  0 - NO ERROR
;	   1 - COMMAND NOT PRESENT
;	   2 - DEVICE ABSENT
;	   3 - CURSOR ON (UZHE)
;	   4 - CURSOR OFF (UZHE)
;	   5 - CURSOR IMAGE VERY BIG

INTMOUS	BIT	7,C
	JP	NZ,EMOUSE
	INC	C
	DEC	C
	JP	Z,MS_INIT
	DEC	C
	JP	Z,MS_SHOW
	DEC	C
	JP	Z,MS_HIDD
	DEC	C
	JP	Z,MS_READ
	DEC	C
	JP	Z,MS_GOTO
	DEC	C
	JP	Z,MS_RESR
	DEC	C
	JP	Z,MS_RESR
	DEC	C
	JP	Z,MS_VERT
	DEC	C
	JP	Z,MS_HORZ
	DEC	C
	JP	Z,MS_CURS
	DEC	C
	JP	Z,MS_MASK
	DEC	C
	JP	Z,MS_BCUR
	DEC	C
	JP	Z,MS_RESR
	DEC	C
	JP	Z,MS_RESR
	DEC	C
	JP	Z,MS_GSEN
	DEC	C
	JP	Z,MS_SENT
MS_RESR	LD	A,1
	SCF 
	RET 

EMOUSE	RES	7,C
	INC	C
	DEC	C
	JP	Z,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	D,B
	RET 

M_MODE	LD	(MODE),A
	OR	A
	JR	Z,UNKMODE
	CP	1
	JR	Z,UNKMODE
	CP	2
	JR	Z,S320256
	CP	3
	JR	Z,S640256
	BIT	7,A
	JR	Z,UNKMODE
	RES	7,A
	CP	1
	JR	Z,S320256
	CP	2
	JR	Z,S640256
UNKMODE	XOR	A
	SCF 
	RET 

S320256
	LD	HL,0	;MIN X
	LD	DE,319	;MAX X
	CALL	MS_HORZ
	LD	HL,0	;MIN X
	LD	DE,255	;MAX X
	CALL	MS_VERT
	XOR	A
	RET 

S640256
	LD	HL,0	;MIN X
	LD	DE,639	;MAX X
	CALL	MS_HORZ
	LD	HL,0	;MIN X
	LD	DE,255	;MAX X
	CALL	MS_VERT
	XOR	A
	RET 

;Mouse Interrupt
M_INT	IN	A,(PAGE3)
	LD	B,A
	IN	A,(Y_PORT)
	LD	C,A
	PUSH	BC
	CALL	REFRESH	;Refresh mouse
	CALL	CONTROL
	POP	BC
	LD	A,B
	OUT	(PAGE3),A
	LD	A,C
	OUT	(Y_PORT),A
	RET 

M_REFR	CALL	CONTROL
	AND	A
	RET 

CONTROL	CALL	READ_M
	RET	NC
	CALL	MCORECT
	LD	A,#00
	LD	(REDY+1),A
	RET 

REFRESH	LD	A,#01
	OR	A
	RET	NZ
REDY	LD	A,#00
;	OR	A
;	RET	NZ
	CALL	RESTORE
	LD	HL,(PIX_X)
	LD	DE,(PIX_Y)
	CALL	MOUSE
	LD	A,#FF
	LD	(REDY+1),A
	RET 

MODE	DB	#03

PIX_X	DW	160
PIX_Y	DW	128

MX	DB	#00
MY	DB	#00
MB	DB	#00
MB_OLD	DB	#00
MIN_X	DW	0
MAX_X	DW	319
MIN_Y	DW	0
MAX_Y	DW	255
SENSEXY
SENSE_X	DB	0
SENSE_Y	DB	0

XHOT_SP	DW	0
YHOT_SP	DW	0

ANDXORS	DW	#FF00
ANDXORA	DW	#FF77

M_XSIZE	DB	10
M_YSIZE	DB	14
SKIPXF	DW	0
REALXS	DW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