

CMOUSE	EQU	1BH
DMOUSE	EQU	1AH
VSIZEX	EQU	256	;X SIZE	SCREEN
VSIZEY	EQU	256	;Y SIZE	SCREEN

INTERUPT:
	CALL	READ_M
	CALL	C,MCORECT
	RET

;READING MOUSE
; HL - X COORD
; DE - Y COORD
;  A - BUTTONS
;      D0 - LEFT
;      D1 - RIGHT

MS_READ:
	LD	HL,(PIX_X)
	LD	DE,(PIX_Y)
	LD	A,(MB)
	RET

;INITIALIZING COM PORT
MS_INIT:
	DI
	LD	A,85
	OUT	(10H),A
	LD	A,45
	OUT	(10H),A
	LD	A,0
	OUT	(CMOUSE),A
	LD	A,1
	OUT	(CMOUSE),A
	LD	A,0
	OUT	(CMOUSE),A
	LD	A,3
	OUT	(CMOUSE),A
	LD	A,41H
	OUT	(CMOUSE),A
	LD	A,4
	OUT	(CMOUSE),A
	LD	A,47H
	OUT	(CMOUSE),A
	LD	A,5
	OUT	(CMOUSE),A
	LD	A,0E0H
	OUT	(CMOUSE),A
	EI
	RET

READ_M:
	IN	A,(CMOUSE)
	RRCA
	RET	NC
	IN	A,(DMOUSE)
	LD	L,A
	BIT	6,A
	CCF
	RET	Z
TST_01:
	IN	A,(CMOUSE)
	RRCA
	JP	NC,TST_01
	IN	A,(DMOUSE)
	LD	E,A
	BIT	6,A
	CCF
	RET	NZ
TST_02:	IN	A,(CMOUSE)
	RRCA
	JP	NC,TST_02
	IN	A,(DMOUSE)
	LD	D,A
	BIT	6,A
	CCF
	RET	NZ
	LD	A,E
	AND	03FH
	LD	E,A
	LD	A,L
	AND	003H
	RRCA
	RRCA
	OR	E
	LD	E,A
	LD	A,D
	AND	03FH
	LD	D,A
	LD	A,L
	AND	00CH
	RRCA
	RRCA
	RRCA
	RRCA
	OR	D
	LD	D,A
	LD	A,L
	RLCA
	RLCA
	RLCA
	RES	6,A
	JR	NC,STBU
	SET	6,A
STBU:	RLCA
	RLCA
	AND	03H
	LD	(MB),A
	LD	A,E
	LD	(MX),A
	LD	A,D
	LD	(M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