
; PicBasic Pro Compiler 2.40, (c) 1998, 2001 microEngineering Labs, Inc. All Rights Reserved. 
PM_USED			EQU	1

	INCLUDE	"16C745.INC"


; Define statements.
#define		CODE_SIZE		 8
#define		OSC		     24
#define		INTHAND		 usbint

RAM_START       		EQU	00020h
RAM_END         		EQU	0016Fh
RAM_BANKS       		EQU	00003h
BANK0_START     		EQU	00020h
BANK0_END       		EQU	0007Fh
BANK1_START     		EQU	000A0h
BANK1_END       		EQU	000EFh
BANK2_START     		EQU	00120h
BANK2_END       		EQU	0016Fh

R0              		EQU	RAM_START + 000h
R1              		EQU	RAM_START + 002h
R2              		EQU	RAM_START + 004h
R3              		EQU	RAM_START + 006h
R4              		EQU	RAM_START + 008h
R5              		EQU	RAM_START + 00Ah
R6              		EQU	RAM_START + 00Ch
R7              		EQU	RAM_START + 00Eh
R8              		EQU	RAM_START + 010h
T1              		EQU	RAM_START + 012h
FLAGS           		EQU	RAM_START + 014h
fsave           		EQU	RAM_START + 015h
GOP             		EQU	RAM_START + 016h
psave           		EQU	RAM_START + 017h
RM1             		EQU	RAM_START + 018h
RM2             		EQU	RAM_START + 019h
RR1             		EQU	RAM_START + 01Ah
RR2             		EQU	RAM_START + 01Bh
ssave           		EQU	RAM_START + 01Ch
_buffer          		EQU	RAM_START + 01Dh
_loopcnt         		EQU	RAM_START + 025h
_state           		EQU	RAM_START + 026h
wsave           		EQU	RAM_START + 050h
temp            		EQU	RAM_START + 051h
GPtemp          		EQU	RAM_START + 052h
BufferData      		EQU	RAM_START + 00100h
BufferDescriptor		EQU	RAM_START + 00108h
USB_Interface   		EQU	RAM_START + 0010Bh
EP0_start       		EQU	RAM_START + 0010Eh
USB_BTO_ERR     		EQU	RAM_START + 00110h
USB_BTS_ERR     		EQU	RAM_START + 00112h
USB_CRC16_ERR   		EQU	RAM_START + 00114h
USB_CRC5_ERR    		EQU	RAM_START + 00116h
USB_DFN8_ERR    		EQU	RAM_START + 00118h
USB_OWN_ERR     		EQU	RAM_START + 0011Ah
USB_PID_ERR     		EQU	RAM_START + 0011Ch
USB_WRT_ERR     		EQU	RAM_START + 0011Eh
bufindex        		EQU	RAM_START + 00120h
bytecounter     		EQU	RAM_START + 00121h
counter         		EQU	RAM_START + 00122h
dest_ptr        		EQU	RAM_START + 00123h
EP0_end         		EQU	RAM_START + 00124h
EP0_maxLength   		EQU	RAM_START + 00125h
hid_dest_ptr    		EQU	RAM_START + 00126h
hid_source_ptr  		EQU	RAM_START + 00127h
inner           		EQU	RAM_START + 00128h
IS_IDLE         		EQU	RAM_START + 00129h
outer           		EQU	RAM_START + 0012Ah
PIDs            		EQU	RAM_START + 0012Bh
RP_save         		EQU	RAM_START + 0012Ch
source_ptr      		EQU	RAM_START + 0012Dh
temp2           		EQU	RAM_START + 0012Eh
USB_address_pending		EQU	RAM_START + 0012Fh
USB_Curr_Config 		EQU	RAM_START + 00130h
USB_dev_req     		EQU	RAM_START + 00131h
USB_status_device		EQU	RAM_START + 00132h
USB_USTAT       		EQU	RAM_START + 00133h
USBMaskedErrors 		EQU	RAM_START + 00134h
USBMaskedInterrupts		EQU	RAM_START + 00135h
_PORTL           		EQU	PORTB
_PORTH           		EQU	PORTC
_TRISL           		EQU	TRISB
_TRISH           		EQU	TRISC
#define _LED             	_PORTA_2
#define _LEDG            	_PORTA_5
#define _PORTA_2         	PORTA, 002h
#define _PORTA_5         	PORTA, 005h
	INCLUDE	"USBMOUSE.MAC"
	INCLUDE	"PBPUSB14.LIB"

	GOTO?L	_start

	ASM?


; W, STATUS and PCLATH registers already saved, save FSR
usbint	movf	FSR, W
	movwf	fsave

; Check interrupt source and vector there
	movlw	high ServiceUSBInt
	movwf	PCLATH
	btfsc	PIR1, USBIF
	call	ServiceUSBInt

; Restore saved registers
	clrf	STATUS
        movf    fsave, W
        movwf   FSR
        movf    psave, W
        movwf   PCLATH
        swapf   ssave, W
        movwf   STATUS
        swapf   wsave, F
        swapf   wsave, W

        retfie                  ; Return from interrupt



	ENDASM?


	LABEL?L	_start	
	MOVE?CB	007h, ADCON1
	LOW?T	_LEDG
	USBINIT?	
	HIGH?T	_LED
	MOVE?CB	000h, _buffer
	MOVE?CB	000h, _buffer + 00001h
	MOVE?CB	000h, _buffer + 00002h
	MOVE?CB	000h, _buffer + 00003h
	MOVE?CB	000h, _buffer + 00005h
	MOVE?CB	000h, _buffer + 00006h
	MOVE?CB	000h, _buffer + 00007h
	MOVE?CB	000h, _buffer + 00008h

	LABEL?L	_movecursor	
	MOVE?CB	000h, _state
	LABEL?L	L00002	
	CMPGT?BCL	_state, 003h, L00003
	MOVE?CB	001h, _loopcnt
	LABEL?L	L00004	
	CMPGT?BCL	_loopcnt, 010h, L00005
	BRANCH?BCL	_state, 004h, L00001
	BRGOTO?L	_up
	BRGOTO?L	_right
	BRGOTO?L	_down
	BRGOTO?L	_left

	LABEL?L	L00001	

	LABEL?L	_up	
	MOVE?CB	000h, _buffer + 00001h
	UMINUS?CB	002h, _buffer + 00002h
	GOTO?L	_endgame

	LABEL?L	_down	
	MOVE?CB	000h, _buffer + 00001h
	MOVE?CB	002h, _buffer + 00002h
	GOTO?L	_endgame

	LABEL?L	_left	
	UMINUS?CB	002h, _buffer + 00001h
	MOVE?CB	000h, _buffer + 00002h
	GOTO?L	_endgame

	LABEL?L	_right	
	MOVE?CB	002h, _buffer + 00001h
	MOVE?CB	000h, _buffer + 00002h

	LABEL?L	_endgame	
	USBOUT?CBCL	001h, _buffer, 004h, _endgame
	NEXT?BCL	_loopcnt, 001h, L00004
	LABEL?L	L00005	
	NEXT?BCL	_state, 001h, L00002
	LABEL?L	L00003	
	GOTO?L	_movecursor

	END
