
;Maustreiber fЃr Grafiktablett K6405 

		masm
		.model tiny

STX		EQU	2
ETX		EQU	3
HT      	EQU	9
LF      	EQU   	10
FF      	EQU   	12
CR      	EQU   	13
EOF     	EQU   	26
EOS		EQU	24h
DC1		EQU	11h
DC3		EQU	13h

my_id		EQU	0e4h		;0c0h..0ffh fЃr Anw. frei
prog_id		EQU	38h
return_code	EQU	19

;       MSDOS-Funktionen

_OUTSTR 	EQU   	9h
_SETINTVEC  	EQU   	25h
_GETVER 	EQU   	30h
_GETINTVEC 	EQU   	35h
_TERMINATE	EQU	4Ch
_OPEN_FILE	EQU	3Dh

;       MSDOS-Macro

MSDOS    	MACRO  	name
         	IF  	&name
         	MOV    	AH,&name
         	ENDIF
         	INT    	21H
         	ENDM

		
		.code
  		org	100h
		
		
start:		jmp	begin

oldint:		dw	0,0
oldint2fh:	dw	0,0
int_cont:	dw	0,0

counter:	db	0
byte_1:		db	0
byte_2:		db	0
byte_3:		db	0
byte_4:		db	0
byte_5:		db	0
x_abs :		dw	0
y_abs : 	dw	0

last_1:		db	0
last_2:		db	0	
last_3:		db	0
last_4:		db	0
last_5:		db	0
last_x_abs:	dw	0
last_y_abs:	dw	0

x_rel :		db	0
y_rel :		db 	0

port_adr:	dw	0

isr:		push	ds
		push	ax
		mov	ax,word ptr cs:oldint
		mov	ds,ax
		dec 	byte ptr ds:016dh
		dec	byte ptr ds:016fh
		pop	ax
		pop	ds

		push	ax
		push	ds
		push	es
		push	bx
		push	cx
		push	dx
		push	di
		push	si
		push	bp

		mov	ax,word ptr cs:oldint
		mov	ds,ax
		cld
		mov	dx,word ptr cs:port_adr
		in	al,dx
		mov	ah,al
		add	dx,5
		in	al,dx
		sub	dx,5
		test	al,2
		mov	al,ah
		jz	not_lost
got_lost:	mov	word ptr cs:counter,0
		jmp	reset_pic
not_lost:	and	al,01111111b
		test	al,40h
		jnz	first_byte

next_bytes:	cmp	byte ptr cs:counter,1
		jz	second_byte
		cmp	byte ptr cs:counter,2
		jz	third_byte
		cmp	byte ptr cs:counter,3
		jz	fourth_byte
		cmp	byte ptr cs:counter,4
		jz	fifth_byte 
		jmp	reset_pic

first_byte:	mov	byte ptr cs:byte_1,al
		mov	byte ptr cs:counter,1
		jmp	reset_pic
second_byte:	mov	byte ptr cs:byte_2,al
		inc	byte ptr cs:counter
		jmp	reset_pic
third_byte:	mov	byte ptr cs:byte_3,al
		inc	byte ptr cs:counter
		jmp	reset_pic
fourth_byte:	mov	byte ptr cs:byte_4,al
		inc	byte ptr cs:counter
		jmp	reset_pic
fifth_byte:	mov	byte ptr cs:byte_5,al
		inc	byte ptr cs:counter
		jmp	convert


reset_pic:	mov	al,20h
		push	dx
		mov	dx,word ptr ds:051ah
		out	dx,al
		pop	dx
		stc
		jmp	end_of_isr
		
end_of_isr:	pop	bp
		pop	si
		pop	di
		pop	dx
		pop	cx
		pop	bx
		pop	es
		pop	ds
		pop	ax

		push	ds
		push	ax
		mov	ax,word ptr cs:oldint
		mov	ds,ax
		inc	byte ptr ds:16dh
		inc	byte ptr ds:16fh
		pop	ax
		pop	ds
		
		iret

convert:	mov	byte ptr cs:counter,0
		xor	ax,ax
		mov	ah,byte ptr cs:byte_3
		shr	ax,1
		shr	ax,1
		and	ah,00001111b
		mov	bl,byte ptr cs:byte_2
		and	bl,00111111b
		or	al,bl
		mov	word ptr cs:x_abs,ax
		xor	ax,ax
		mov	ah,byte ptr cs:byte_5
		shr	ax,1
		shr	ax,1
		and	ah,00001111b
		mov	bl,byte ptr cs:byte_4
		and	bl,00111111b
		or	al,bl
		mov	word ptr cs:y_abs,ax

		mov	ax,word ptr cs:x_abs
		sub	ax,word ptr cs:last_x_abs
		jns	x_rel_pos
x_rel_neg:	neg	ax
		shr	ax,1
;		shr	ax,1
		neg	ax
		jmp	x_copy_sign
x_rel_pos:	shr	ax,1
;		shr	ax,1
x_copy_sign:	cmp	ax,0
		jns	store_x_rel
		or	al,10000000b
store_x_rel:	mov	byte ptr cs:x_rel,al
		
		mov	ax,word ptr cs:last_y_abs
		sub	ax,word ptr cs:y_abs
		jns	y_rel_pos
y_rel_neg:	neg	ax
		shr	ax,1
;		shr	ax,1
		neg	ax
		jmp	y_copy_sign
y_rel_pos:	shr	ax,1
;		shr	ax,1
y_copy_sign:	cmp	ax,0
		jns	store_y_rel
		or	al,10000000b
store_y_rel:	mov	byte ptr cs:y_rel,al

store_position:	mov	cx,9
		push	ds
		push	es
		push	cs
		pop	ax
		mov	ds,ax
		mov	es,ax
		lea	si,byte_1
		lea	di,last_1
		rep	movsb
		pop	es
		pop	ds

build_byte1:	mov	al,0
		mov	bl,byte ptr cs:y_rel
		shl	bl,1
		rcl	al,1
		shl	bl,1
		rcl	al,1
		mov	bl,byte ptr cs:x_rel
		shl	bl,1
		rcl	al,1
		shl	bl,1
		rcl	al,1
		or	al,01000000b
		test	byte ptr cs:byte_1,00000100b
		jz	no_botton
		or	al,00100000b
no_botton:	mov	byte ptr ds:01d9h,al
		mov	byte ptr ds:0509h,1
		
build_byte2:	mov	al,byte ptr cs:x_rel
		and	al,00111111b
		mov	byte ptr ds:01dah,al
		inc	byte ptr ds:0509h
		
build_byte3:	mov	al,byte ptr cs:y_rel
		and	al,00111111b

		mov	word ptr cs:int_cont,1300h
		mov	word ptr cs:int_cont+2,ds
jmp_microsoft:	jmp	dword ptr cs:int_cont


int2fh:		cmp	ax,my_id*100h+prog_id
		jnz	old2fh
		mov	ax,return_code
		iret
old2fh:		jmp	dword ptr cs:[oldint2fh]


toggle_snd:	push	ax
		in	al,61h
		xor	al,00000011b
		out	61h,al
		pop	ax
		ret

;------------------------------------------------------------------------------

begin:		push	cs
		pop	ds			; ds:=aktuelles Segm.

check_inst:	mov	ax,my_id*100h+prog_id	; Programm schon resident ?
		int	2fh
		cmp	ax,return_code
		jne	check_msoft		; nein => Progr. installieren
		lea	dx,msg2			
		jmp	exit

check_msoft:	mov	al,0ch			; MS-Maustr. an com1: ?
		push	ax
		msdos	_getintvec
		pop	ax
		lea	si,search_str
		mov	di,56eh
		mov	cx,21
		rep	cmpsb
		je	save_vector
		mov	al,0bh			; MS-Maustr. an com2: ?
		push	ax
		msdos	_getintvec
		pop	ax
		lea	si,search_str
		mov	di,56eh
		mov	cx,21
		rep	cmpsb
		je	save_vector
		lea	dx,msg3			; nicht gefunden !
		jmp	exit

save_vector:	push	ax
		mov	word ptr oldint,es	; es:=Maustr.-segm.
		mov	word ptr oldint+2,bx
		mov	ax,word ptr es:505h
		mov	word ptr ds:port_adr,ax
		jmp	install

install:	mov	ax,word ptr cs:oldint
		mov	es,ax
patch_mssoft:	mov	byte ptr es:08b1h,0c3h  ; 

		call	set_baudrate

		lea 	dx,isr
		pop	ax
		msdos   _setintvec

		mov	ax,352fh
		int	21h
		mov	word ptr ds:oldint2fh,bx
		mov	word ptr oldint2fh+2,es
		lea	dx,int2fh
		mov	ax,252fh	
		int	21h

		cmp	word ptr ds:port_adr,3f8h
		jne	on_com2

on_com1:	lea	dx,msg1			; Meldung
		msdos   _outstr
		lea	dx,msg4
		msdos	_outstr
		lea	dx,msg5
		msdos	_outstr
		jmp	tsr

on_com2:	mov	byte ptr ds:msg4_1,'2'
		lea	dx,msg1
		msdos	_outstr
		lea	dx,msg4
		msdos	_outstr
		lea	dx,msg6
		msdos	_outstr
		jmp	tsr

tsr:		lea	dx,begin		; resident beenden	
		int	27h


exit:		push	dx
		lea	dx,msg1
		msdos   _outstr
		pop	dx
		msdos	_outstr
		call	error_bell
		mov	al,0
		msdos	_terminate

set_baudrate:	push	ax
		push	bx
		push	cx
		push	dx
		mov	dx,word ptr cs:port_adr	; umstellen der
		mov	bx,dx
		add	dx,3h			; Baudrate auf 9600
		mov	al,80h
		out	dx,al
		xchg	bx,dx
		mov	al,12
		out	dx,al
		xor 	al,al
		inc	dx
		out	dx,al
		xchg	bx,dx
		mov	al,00011110b
		out	dx,al
		mov	dx,word ptr cs:port_adr 
		add	dx,1
		mov	al,1
		out	dx,al
		pop	dx
		pop	cx
		pop	bx
		pop	ax
		ret


time_loop:	push	cx
		xor	cx,cx
t_l:		nop
		loop	t_l
		pop	cx
		ret		

error_bell:	call	toggle_snd
		call	time_loop
		call	toggle_snd
		ret	


msg1:		db	CR,LF,CR,LF
		db	'ЙННННННННННННННННННННННННННННННННННННННННННННННННННННННННННННН»',CR,LF
		db	'є  SoftTronix presents : A Microsoftmouse Emulator.           є',CR,LF
		db	'ЗДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДДД¶',CR,LF  
		db	'є           Software Mousedriverinterface  Version 1.0        є',CR,LF
		db	'є           for Graphic Drafting Board K6405                  є',CR,LF
		db	'є                                                             є',CR,LF
		db      'є  Copyright (C): SoftTronix, 1991. Written for the CSF.      є',CR,LF
		db	'ИНННННННННННННННННННННННННННННННННННННННННННННННННННННННННННННј',CR,LF
		db	EOS

msg2:		db	'Error: Driver already installed !' 
		db	CR,LF,CR,LF,EOS

msg3:		db	'Error: Microsoft Mousedriver V6.26 you have to install first !'
		db	CR,LF,CR,LF,EOS

msg4:		db	'Driver successful installed on COM'
msg4_1:		db	'1: !',CR,LF,EOS

msg5:		db	'( Port address is 3F8 , IRQ number is 4 )',CR,LF,CR,LF,EOS

msg6:		db	'( Port address is 2F8 , IRQ number is 3 )',CR,LF,CR,LF,EOS

search_str:	db	'*** This is Copyright .....'


stop:		end	start	
