#PIC V1.0 (c)1997 J.Petroglou	LIST FILE
#file: dh2:IDE/APICforAMINET/APIC/examples/mouse.src
#date: Sun Dec 14 20:43:54 1997
#pic : PIC16C54
ADDR CODE	 SRCLINE SOURCECODE

0000             000001 ;PC mouse adapter for Amiga computers
0000             000002 ;
0000             000003 ; use serial Pc mouse on Amiga Computers without Software driver !!
0000             000004 ;
0000             000005 ; 03.01.96
0000             000006 ;
0000             000007 ;This Program converts the microsoft and the mouse system format to amiga ones,
0000             000008 ;three mouse buttons are supported (mouse system).
0000             000009 ;Only hardware, it`s like an original Amiga mouse
0000             000010 ;The Xtal frequenzy is 11.0592 Mhz, the TXD line from mouse is with a 20K
0000             000011 ;resistor directly with the PIC 16C54 Port connected. Only three other components
0000             000012 ;are needed for the voltage conversion: ICL7660 (voltage converter from Harris 
0000             000013 ;semiconductor,INTERSIL) and two capacitors with 10µF. The PCB is about 3*4.5cm.
0000             000014 ;My PC mouse needs -5V on TXD,5V on RTS and ground. 
0000             000015 ;The adapter recognizes mouse protocoll changes automatic. I think it`s easy
0000             000016 ;to include the Logitech protokoll but I don`t know it.
0000             000017 ;
0000             000018 ; see our Amiga Pic Tools Home Page:
0000             000019 ;
0000             000020 ; http://linux.rz.fh-hannover.de/~duesterb/
0000             000021 ; (Pic Simulator, Pic Progger)
0000             000022 ;
0000             000023 ;Dirk Düsterberg, duesterb@unixserv.rz.fh-hannover.de
0000             000024 ;
0000             000025 ;Jahnstr.9
0000             000026 ;31860 Emmerthal
0000             000027 ;Germany
0000             000028 ;
0000             000029 ;
0000             000030 ;thanks to Joannis Petroglou, who made this with his wonderful Tools possibel!
0000             000031 
0000             000032 
0000             000033 
0000             000034 ; Microsoft is a registered trademark of Microsoft Corp.
0000             000035 ; Mouse Systems is  registered trademark of MSC Technologies, Inc.
0000             000036 ; Microchip is a registered trademark of Microchip Technology
0000             000037 ; Logitech is a registered trademark too
0000             000038 ; This source is copyrighted to Dirk Düsterberg, no trademark
0000             000039 
0000             000040 
0000             000041 
0000             000042 
0000             000043 
0000             000044 
0000             000045 	list p=PIC16C54, r=hex, s=off
0000             000046 
01FF             000047 	RESET	start
0000             000048 
0000             000049 
0000             000050 	CBLOCK 08h
0000             000051 	  trycnt			;synctrys, which format?
0000             000052 	  loopcnt			;loop counter
0000             000053 	  bitcnt			;bit counter
0000             000054 	  serbuf			;serial buffer
0000             000055 	  RBbuf				;Port buffer
0000             000056 	ENDC
0000             000057 
0000             000058 	CBLOCK	10h
0000             000059 	  byte0				;first received byte
0000             000060 	  byte1				;second received byte and byte 4 (mouse system)
0000             000061 	  byte2				;third received byte and byte 5 (mouse system)
0000             000062 	ENDC
0000             000063 
0000             000064 RA	=	5
0000             000065 RB	=	6
0000             000066 
0000             000067 
0000             000068 
0000             000069 #define	RXD	RA,1			;RXD input, bit 1 from Port A
0000             000070 
0000             000071 #define	r_b	RBbuf,2			;right mouse button
0000             000072 #define	m_b	RBbuf,4			;middle mouse button
0000             000073 #define	l_b	RBbuf,6			;left mouse button
0000             000074 
0000             000075 #define	H	RBbuf,0			;Horizontal Pulses
0000             000076 #define	HQ	RBbuf,5			;Horizontal Quadrature Pulses
0000             000077 #define	V	RBbuf,1			;Vertical Pulses
0000             000078 #define	VQ	RBbuf,7			;Vertical Quadrature Pulses
0000             000079 
0000             000080 
0000             000081 #define	c	03,0
0000             000082 #define	z	03,2
0000             000083 
0000             000084 
0000 0C00        000085 start	movlw	0
0001 0006        000086 	tris	RB
0002             000087 
0002 0CFF        000088 	movlw	0ffh
0003 0005        000089 	tris	RA
0004             000090 
0004 006C        000091 	clrf	RBbuf			;make butoons unpressed
0005 00EC        000092 	decf	RBbuf,f
0006             000093 
0006 020C        000094 	movf	RBbuf,w			;use Port buffer to prevent read from Port
0007 0026        000095 	movwf	RB
0008             000096 
0008 0068        000097 	clrf	trycnt,f
0009             000098 
0009             000099 
0009             000100 
0009             000101 
0009             000102 
0009             000103 
0009             000104 
0009             000105 
0009             000106 
0009             000107 
0009             000108 
0009             000109 ;this is the routine for the microsoft format (3 bytes with 2 buttons)
0009             000110 
0009             000111 
0009             000112 
0009 0068        000113 micro	clrf	trycnt,f
000A 0C04        000114 mic	movlw	.4
000B 0088        000115 	subwf	trycnt,w
000C 0603        000116 	btfsc	c
000D 0A48        000117 	goto	mouse			;more than 3 trys? jump!
000E             000118 	
000E 0971        000119 	call	rcb			;receive the first byte
000F 02A8        000120 	incf	trycnt,f
0010             000121 	
0010 020B        000122 	movf	serbuf,w
0011 0FC0        000123 	xorlw	11000000b		;invert bit 6 and 7 from working register
0012 0EC0        000124 	andlw	11000000b		;clr bit 0 to 5 (buttons and coord.)
0013 0743        000125 	btfss	z			;skip if w is zero
0014 0A0A        000126 	goto	mic			;if not zero -> no match
0015             000127 
0015 020B        000128 	movf	serbuf,w		;this is the first received byte
0016 0030        000129 	movwf	byte0
0017             000130 
0017 058C        000131 	bsf	m_b			;middle button not supported
0018             000132 
0018 068B        000133 	btfsc	serbuf,4		;mov buttons
0019 044C        000134 	bcf	r_b
001A 078B        000135 	btfss	serbuf,4
001B 054C        000136 	bsf	r_b
001C             000137 
001C 06AB        000138 	btfsc	serbuf,5
001D 04CC        000139 	bcf	l_b
001E 07AB        000140 	btfss	serbuf,5
001F 05CC        000141 	bsf	l_b
0020             000142 
0020 0971        000143 	call	rcb			;receive second byte
0021             000144 
0021             000145 	
0021 0C80        000146 	movlw	10000000b
0022 01AB        000147 	xorwf	serbuf,f		;invert bit 7 from buffer
0023 020B        000148 	movf	serbuf,w
0024 0EC0        000149 	andlw	11000000b		;clr bit 0 to 5 (coord.)
0025 0743        000150 	btfss	z				;skip if w is zero
0026 0A48        000151 	goto	mouse			;if not zero -> no match -> mouse format
0027             000152 		
0027             000153 	
0027 0071        000154 	clrf	byte1
0028             000155 
0028 0710        000156 	btfss	byte0,0			;mov x6 and x7 coord. to byte1
0029 04D1        000157 	bcf	byte1,6
002A 0610        000158 	btfsc	byte0,0
002B 05D1        000159 	bsf	byte1,6
002C             000160 
002C             000161 
002C 0730        000162 	btfss	byte0,1			;mov x6 and x7 coord. to byte1
002D 04F1        000163 	bcf	byte1,7
002E 0630        000164 	btfsc	byte0,1
002F 05F1        000165 	bsf	byte1,7
0030             000166 
0030             000167 
0030             000168 
0030 020B        000169 	movf	serbuf,w		;set rest of x coord.
0031 0131        000170 	iorwf	byte1,f
0032             000171 
0032             000172 
0032             000173 
0032 0971        000174 	call	rcb			;receive third byte
0033             000175 
0033             000176 
0033 0C80        000177 	movlw	10000000b
0034 01AB        000178 	xorwf	serbuf,f		;invert bit 7 from buffer
0035 020B        000179 	movf	serbuf,w
0036 0EC0        000180 	andlw	11000000b		;clr bit 0 to 5 (coord.)
0037 0743        000181 	btfss	z			;skip if w is zero
0038 0A48        000182 	goto	mouse			;if not zero -> no match -> mouse format
0039             000183 
0039 0072        000184 	clrf	byte2
003A             000185 
003A             000186 
003A             000187 
003A 0750        000188 	btfss	byte0,2			;mov y6 and y7 coord. to byte2
003B 04D2        000189 	bcf	byte2,6
003C 0650        000190 	btfsc	byte0,2
003D 05D2        000191 	bsf	byte2,6
003E             000192 
003E             000193 
003E             000194 
003E 0770        000195 	btfss	byte0,3			;mov y6 and y7 coord. to byte2
003F 04F2        000196 	bcf	byte2,7
0040 0670        000197 	btfsc	byte0,3
0041 05F2        000198 	bsf	byte2,7
0042             000199 
0042             000200 
0042             000201 
0042 020B        000202 	movf	serbuf,w		;set rest of y coord.
0043 0132        000203 	iorwf	byte2,f
0044             000204 
0044 0272        000205 	comf	byte2,f			;fix microsoft to mouse protokoll
0045 02B2        000206 	incf	byte2,f			;(up and down exchanged)
0046             000207 
0046 0068        000208 	clrf	trycnt
0047 0A09        000209 	goto	micro
0048             000210 
0048             000211 
0048             000212 
0048             000213 ;this is the routine for the mouse system format (5 bytes with 3 buttons)
0048             000214 
0048             000215 
0048 0C06        000216 mouse	movlw	.6
0049 0088        000217 	subwf	trycnt,w
004A 0603        000218 	btfsc	c
004B 0A09        000219 	goto	micro			;more than 5 trys? jump micro!
004C             000220 
004C 0971        000221 	call	rcb			;get first of five bytes from serial mouse
004D             000222 
004D 02A8        000223 	incf	trycnt,f		;one more try
004E             000224 
004E             000225 ;test format #10000lmr (left, middle, right) 
004E             000226 
004E 020B        000227 	movf	serbuf,w
004F 0F80        000228 	xorlw	10000000b		;invert bit 7 from working register
0050 0EF8        000229 	andlw	11111000b		;clr bit 0 to 2 (buttons)
0051 0743        000230 	btfss	z			;skip if w is zero
0052 0A48        000231 	goto	mouse			;if not zero -> no match
0053             000232 	
0053 020B        000233 	movf	serbuf,w
0054 0030        000234 	movwf	byte0
0055             000235 
0055             000236 
0055             000237 
0055 0971        000238 	call	rcb			;byte1, X-Axis movement data
0056 020B        000239 	movf	serbuf,w
0057 0031        000240 	movwf	byte1
0058             000241 
0058 0971        000242 	call	rcb			;byte2, Y-Axis movement data
0059 020B        000243 	movf	serbuf,w
005A 0032        000244 	movwf	byte2
005B             000245 	
005B 0971        000246 	call	rcb			;byte3, X-Axis movement data	
005C 020B        000247 	movf	serbuf,w
005D 0031        000248 	movwf	byte1
005E             000249 
005E 0971        000250 	call	rcb			;byte4, Y-Axis movement data
005F 020B        000251 	movf	serbuf,w
0060 0032        000252 	movwf	byte2
0061             000253 
0061             000254 
0061             000255 
0061 0710        000256 	btfss	byte0,0			;convert to Amiga
0062 044C        000257 	bcf	r_b
0063 0610        000258 	btfsc	byte0,0
0064 054C        000259 	bsf	r_b
0065             000260 
0065             000261 
0065 0730        000262 	btfss	byte0,1
0066 048C        000263 	bcf	m_b
0067 0630        000264 	btfsc	byte0,1
0068 058C        000265 	bsf	m_b
0069             000266 
0069             000267 
0069 0750        000268 	btfss	byte0,2
006A 04CC        000269 	bcf	l_b
006B 0650        000270 	btfsc	byte0,2
006C 05CC        000271 	bsf	l_b
006D             000272 
006D             000273 
006D 020C        000274 	movf	RBbuf,w
006E 0026        000275 	movwf	RB			;use Port buffer to prevent read from Port
006F             000276 
006F 0068        000277 	clrf	trycnt			
0070             000278 
0070 0A48        000279 	goto	mouse
0071             000280 
0071             000281 
0071             000282 
0071             000283 
0071             000284 
0071             000285 
0071             000286 
0071             000287 ;receive routine, receive 8bits
0071             000288 ;loop is done until byte is received
0071             000289 
0071             000290 
0071             000291 
0071             000292 
0071 0625        000293 rcb	btfsc	RXD
0072 0A74        000294 	goto	rcshift			;startbit ?
0073 0A71        000295 	goto	rcb
0074             000296 
0074 0C08        000297 rcshift	movlw	.8
0075 002A        000298 	movwf	bitcnt
0076             000299 
0076 0982        000300 	call	whbit			;wait half bit (middle bit position)
0077             000301 
0077 098E        000302 r_it	call	wbit			;wait bit delay
0078             000303 
0078 0725        000304 	btfss	RXD			;move RXD into c
0079 0403        000305 	bcf	c
007A 0625        000306 	btfsc	RXD
007B 0503        000307 	bsf	c
007C             000308 
007C 032B        000309 	rrf	serbuf,f		;rotate c in serbuf
007D 02EA        000310 	decfsz	bitcnt
007E 0A77        000311 	goto	r_it			;decrement bit counter (8 bits)
007F 026B        000312 	comf	serbuf			;invert serbuf (RS232 -> TTL)
0080 0982        000313 	call	whbit			;wait a half bit to get out from data area
0081 0800        000314 	retlw	0			;back
0082             000315 
0082             000316 
0082             000317 
0082             000318 
0082             000319 
0082             000320 
0082             000321 
0082             000322 
0082             000323 ;wait routine for one and one half bit for 1200 baud
0082             000324 
0082             000325 
0082 0C7D        000326 whbit	movlw	.125
0083 0029        000327 	movwf	loopcnt			;1200
0084 0000        000328 do_hbit	nop
0085 0000        000329 	nop
0086 0000        000330 	nop
0087 0000        000331 	nop
0088 0000        000332 	nop
0089 0000        000333 	nop
008A 0000        000334 	nop
008B 02E9        000335 	decfsz	loopcnt
008C 0A84        000336 	goto	do_hbit
008D 0800        000337 	retlw	0
008E             000338 
008E             000339 
008E             000340 
008E             000341 
008E             000342 
008E             000343 ;this routine is one bit long, it converts the axe counter to a quadrature
008E             000344 ;modulation, every change on V,VQ,H or HQ means a mouse move
008E             000345 
008E             000346 
008E 0C2D        000347 wbit	movlw	.45
008F 0029        000348 	movwf	loopcnt			;quad mod is done while bit waiting
0090             000349 
0090 0211        000350 x_axe	movf	byte1,w
0091 0643        000351 	btfsc	z
0092 0AA8        000352 	goto	x_axe0			;byte1 = 0, nothing to do
0093 06F1        000353 	btfsc	byte1,7
0094 0ABB        000354 	goto	_right			;jump if bit
0095 0000        000355 	nop
0096 0000        000356 	nop
0097 0000        000357 	nop
0098 0000        000358 	nop
0099 0000        000359 	nop
009A 00F1        000360 left	decf	byte1,f			;decrement byte 1
009B 060C        000361 	btfsc	H
009C 0AA1        000362 	goto	leftx1			;jmp to x1 if H set
009D 06AC        000363 	btfsc	HQ
009E 0AA6        000364 	goto	left10
009F 050C        000365 	bsf	H			;set H if H and HQ clear
00A0 0AB6        000366 	goto	lback1
00A1 06AC        000367 leftx1	btfsc	HQ			
00A2 040C        000368 	bcf	H			;clr H if H set and HQ set
00A3 07AC        000369 	btfss	HQ
00A4 05AC        000370 	bsf	HQ			;set HQ if H set and HQ clear
00A5 0AB8        000371 	goto	lback3
00A6 04AC        000372 left10	bcf	HQ			;clear HQ if H clear and HQ set
00A7 0AB7        000373 	goto	lback2
00A8 0000        000374 x_axe0	nop
00A9 0000        000375 	nop
00AA 0000        000376 	nop
00AB 0000        000377 	nop
00AC 0000        000378 	nop
00AD 0000        000379 	nop
00AE 0000        000380 	nop
00AF 0000        000381 	nop
00B0 0000        000382 	nop
00B1 0000        000383 	nop
00B2 0000        000384 	nop
00B3 0000        000385 	nop
00B4 0000        000386 	nop
00B5 0000        000387 	nop
00B6 0000        000388 lback1	nop
00B7 0000        000389 lback2	nop
00B8 020C        000390 lback3	movf	RBbuf,w
00B9 0026        000391 	movwf	RB			;use Port buffer to prevent read from Port
00BA 0AD3        000392 	goto	y_axe
00BB             000393 
00BB 0271        000394 _right	comf	byte1,f			;negate byte1
00BC 02B1        000395 	incf	byte1,f
00BD             000396 
00BD 0000        000397 	nop
00BE 0000        000398 	nop
00BF             000399 
00BF 00F1        000400 right	decf	byte1,f
00C0 06AC        000401 	btfsc	HQ
00C1 0AC6        000402 	goto	right1x			;jmp to 1x if HQ set
00C2 060C        000403 	btfsc	H
00C3 0ACB        000404 	goto	right01
00C4 05AC        000405 	bsf	HQ			;set HQ if H and HQ clear
00C5 0ACD        000406 	goto	rback1
00C6 060C        000407 right1x	btfsc	H			
00C7 04AC        000408 	bcf	HQ			;clrb H if H and HQ set
00C8 070C        000409 	btfss	H
00C9 050C        000410 	bsf	H			;setb H if H clear and HQ set
00CA 0ACF        000411 	goto	rback3
00CB 040C        000412 right01	bcf	H			;clr H if HQ clear and H set
00CC 0ACE        000413 	goto	rback2
00CD             000414 
00CD 0000        000415 rback1	nop
00CE 0000        000416 rback2	nop
00CF 020C        000417 rback3	movf	RBbuf,w
00D0 0026        000418 	movwf	RB			;use Port buffer to prevent read from Port
00D1 0271        000419 	comf	byte1,f
00D2 02B1        000420 	incf	byte1,f
00D3             000421 
00D3             000422 	
00D3             000423 
00D3             000424 
00D3 0212        000425 y_axe	movf	byte2,w
00D4 0643        000426 	btfsc	z
00D5 0AEB        000427 	goto	y_axe0			;byte2 = 0, nothing to do
00D6 06F2        000428 	btfsc	byte2,7
00D7 0AFE        000429 	goto	_up			;jump if bit
00D8 0000        000430 	nop
00D9 0000        000431 	nop
00DA 0000        000432 	nop
00DB 0000        000433 	nop
00DC 0000        000434 	nop
00DD 00F2        000435 down	decf	byte2,f
00DE 06EC        000436 	btfsc	VQ
00DF 0AE4        000437 	goto	down1x			;jmp to 1x if VQ set
00E0 062C        000438 	btfsc	V
00E1 0AE9        000439 	goto	down01
00E2 05EC        000440 	bsf	VQ			;set VQ if V and VQ clear
00E3 0AF9        000441 	goto	dback1
00E4 062C        000442 down1x	btfsc	V			
00E5 04EC        000443 	bcf	VQ			;clrb V if V and VQ set
00E6 072C        000444 	btfss	V
00E7 052C        000445 	bsf	V			;setb V if V clear and VQ set
00E8 0AFB        000446 	goto	dback3
00E9 042C        000447 down01	bcf	V			;clr V if VQ clear and V set
00EA 0AFA        000448 	goto	dback2
00EB 0000        000449 y_axe0	nop
00EC 0000        000450 	nop
00ED 0000        000451 	nop
00EE 0000        000452 	nop
00EF 0000        000453 	nop
00F0 0000        000454 	nop
00F1 0000        000455 	nop
00F2 0000        000456 	nop
00F3 0000        000457 	nop
00F4 0000        000458 	nop
00F5 0000        000459 	nop
00F6 0000        000460 	nop
00F7 0000        000461 	nop
00F8 0000        000462 	nop
00F9 0000        000463 dback1	nop
00FA 0000        000464 dback2	nop
00FB 020C        000465 dback3	movf	RBbuf,w
00FC 0026        000466 	movwf	RB			;use Port buffer to prevent read from Port
00FD 0B16        000467 	goto	done
00FE             000468 
00FE 0272        000469 _up	comf	byte2,f
00FF 02B2        000470 	incf	byte2,f
0100 0000        000471 	nop
0101 0000        000472 	nop
0102             000473 
0102 00F2        000474 up	decf	byte2,f
0103 062C        000475 	btfsc	V
0104 0B09        000476 	goto	up_x1			;jmp to x1 if V set
0105 06EC        000477 	btfsc	VQ
0106 0B0E        000478 	goto	up_10
0107 052C        000479 	bsf	V			;set V if V and VQ clear
0108 0B10        000480 	goto	uback1
0109 06EC        000481 up_x1	btfsc	VQ			
010A 042C        000482 	bcf	V			;clr V if V set and VQ set
010B 07EC        000483 	btfss	VQ
010C 05EC        000484 	bsf	VQ			;set VQ if V set and VQ clear
010D 0B12        000485 	goto	uback3
010E 04EC        000486 up_10	bcf	VQ			;clear VQ if V clear and VQ set
010F 0B11        000487 	goto	uback2
0110 0000        000488 uback1	nop
0111 0000        000489 uback2	nop
0112 020C        000490 uback3	movf	RBbuf,w
0113 0026        000491 	movwf	RB			;use Port buffer to prevent read from Port
0114 0272        000492 	comf	byte2,f
0115 02B2        000493 	incf	byte2,f
0116             000494 	
0116             000495 	
0116             000496 
0116 02E9        000497 done	decfsz	loopcnt
0117 0A90        000498 	goto	x_axe
0118 0800        000499 	retlw	0	


Used Symbols
-----------------------------------------
trycnt                           00000008
loopcnt                          00000009
bitcnt                           0000000A
serbuf                           0000000B
RBbuf                            0000000C
byte0                            00000010
byte1                            00000011
byte2                            00000012
RA                               00000005
RB                               00000006
start                            00000000
micro                            00000009
mic                              0000000A
mouse                            00000048
rcb                              00000071
rcshift                          00000074
r_it                             00000077
whbit                            00000082
do_hbit                          00000084
wbit                             0000008E
x_axe                            00000090
left                             0000009A
leftx1                           000000A1
left10                           000000A6
x_axe0                           000000A8
lback1                           000000B6
lback2                           000000B7
lback3                           000000B8
_right                           000000BB
right                            000000BF
right1x                          000000C6
right01                          000000CB
rback1                           000000CD
rback2                           000000CE
rback3                           000000CF
y_axe                            000000D3
down                             000000DD
down1x                           000000E4
down01                           000000E9
y_axe0                           000000EB
dback1                           000000F9
dback2                           000000FA
dback3                           000000FB
_up                              000000FE
up                               00000102
up_x1                            00000109
up_10                            0000010E
uback1                           00000110
uback2                           00000111
uback3                           00000112
done                             00000116


Used Defines
-----------------------------------------
c                                03,0 
H                                RBbuf,0 
V                                RBbuf,1 
z                                03,2 
HQ                               RBbuf,5 
VQ                               RBbuf,7 
l_b                              RBbuf,6 
m_b                              RBbuf,4 
RXD                              RA,1 
r_b                              RBbuf,2 


PROGRAM MEMORY USAGE TABLE:	'-' = not used  'X' = used

0000 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX
0040 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX
0080 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX
00C0 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX
0100 : XXXXXXXXXXXXXXXX XXXXXXXXX------- ---------------- ----------------
0140 : ---------------- ---------------- ---------------- ----------------
0180 : ---------------- ---------------- ---------------- ----------------
01C0 : ---------------- ---------------- ---------------- ----------------

Program Memory Words Used:  0281
Program Memory Words Free:  0231

Errors: 0
