;Name:	Derek Marston and Kristin Kelly
;Date:  10/8/02
;Description:  This program will test the new
;	subroutine ProcessKey

Monitor 	EQU	0FF7Ch;
CmdReg		EQU	8800h;
PortA		EQU	1000h;
StatusReg	EQU	8800h
DataReg		EQU	8000h
SCIStatusReg	EQU	102Eh;
SCIDataReg	EQU	102Fh;
CharOut		EQU	0FFB8h;
StrOut		EQU	0FFC7h;
LineFeed	EQU	0FFC4h;

;Subroutines in EEPROM:
Config8251A             EQU	0B600h
InitializeVars	        EQU	0B603h
InstallInterruptVector  EQU    	0B606h
TransmitA	        EQU	0B609h
Receive		        EQU	0B60Ch
dspData		        EQU	0B60Fh
ProcessKey		EQU	0B612h
CopyBuff		EQU	0B615h

;put flags early in memory so they can
;be used with the direct address mode
	org	00h
RecFlag:	DS 	1t;	Define flag
dspFlag:	DS	1t;	Define flag
Data2SendFlag:	DS	1t;	Define flag
KBFlag:		DS	1t;	Define flag

	org	100h
	bra	main

RecBuff:	DS	19t; Define space for largest token
dspBuff:	DS	17t;	Define space for data
KBBuff:		DS	17t;	Define space for data
KBPtr:		DS	2t;

; Main is below.  IntrRoutine is needed in EEPROM,
; so we start it at an easy memory location, 100h

;Name:		IntrRoutine
;Input:		None
;Output:	None
;Destroys:	Nothing
;Desc:		Receive one byte

IntrRoutine:
	clr	RecFlag		;Reset Flag
	jsr	Receive		;read first byte
	cmpa	#0AAh		;see if it's AA
	bne	done		;if not, bad token:discarded
	ldx	#RecBuff	;Initialize x w/buffer
	jsr	Receive		;read second byte

	cmpa	#0BBh		;see if busy token
	beq	BusyHere	
	cmpa	#0FFh		;if not see if free token
	bne	done		;if not, token invalid
	
	jsr 	Receive		;Token is free?
	cmpa 	#0AAh		
	bne	done		;if not token is invalid
	ldaa	#0FFh		;if so, set free token flag
	staa	RecFlag
	bra	done
	
BusyHere:			;set up loop
	ldab	#18t		;load counter with 18 data bits
	staa	0,x		;store data to buffer
	inx			;next byte in buffer

GetRest:
	jsr	Receive		;get next byte
	staa	0,x		;store it
	inx
	decb
	bne	GetRest		;loop
	
	jsr	Receive		;get terminating #AA byte
	cmpa	#0AAh		;see if it is
	bne	done		;if not, discard token
	ldaa	#0BFh		;if it is, set token rec. flag
	staa	RecFlag
done:
	rti

main:
	jsr	Config8251A;	Configure USART
	jsr	InstallInterruptVector;
	jsr	InitializeVars;
	
WaitLoop:
	jsr	ProcessKey
	brclr	Data2SendFlag,0FFh,WaitLoop

	clr	Data2SendFlag
	ldx	#KBBuff
	jsr	CopyBuff
	jsr	dspData

;	ldx	#RecFlag
;	brclr	0,x,0Bh,WaitLoop;branch if flag clear
;	clr	RecFlag
	bra	WaitLoop
	jmp	Monitor;	End program