*********
* Program SER_TEST (6811)
*
* Illustrates how to use serial port.
* Transmits a string at 1200 baud, 8 bit, no parity,
* 1 start, 1 stop bit.
*
* Receives a string.
*
* Requires improved error checking in the receive routine.
* Timeout, frame error, overrun and noise error.
*
*
* P. H. Anderson, MSU, 8 Jan 91; 15 Jan 93
**********
PSCT	EQU $C000
DSCT	EQU $D000
IDSCT	EQU $D300

STACKTP 	EQU $0045

REG_BASE	EQU $1000

PORTD	EQU $03
DDRD	EQU $09
SPCR	EQU $28
BAUD	EQU $2B
SCCR1	EQU $2C
SCCR2	EQU $2D
SCSR	EQU $2E
SCDR	EQU $2F

	ORG PSCT

	LDS #STACKTP

	LDY #REG_BASE

M1
	LDX #MSG_SEND		 * send the msg again and again
	BSR SETUP_DATA
	BSR ENABLE_TX
	BSR SEND_MSG
	BSR DISABLE_TX

* now receive a message
	LDX #MSG_RCV	* beginning of storage
	LDAA $0A	* last char
	STAA LAST_CHAR
	LDAA #79	* max size of string
	STAA LAST_CHAR
	BSR ENABLE_RX
	BSR RCV_MSG
	BSR DISABLE_RX
	SWI

SETUP_DATA
* configures to send data
* PORTD - no action required
* DDRD - no action

	BCLR SPCR,Y %00100000	* DWOM, wired or disabled

	BSET BAUD,Y %00110011	* 9600 prescale, 1200 baud
	BCLR BAUD,Y %01000100	* X011X011

	BCLR SCCR1,Y %00011000	* char length 8, disable WAKE

	BCLR SCCR2,Y %11111111	* disable all ints

	RTS

ENABLE_TX
	BSET SCCR2,Y %00001000	* TE = 1
	RTS

ENABLE_RX
	BSET SCCR2,Y %00000100	* RE = 1
	RTS

DISABLE_TX
	BCLR SCCR2,Y %00001000
	RTS

DISABLE_RX
	BCLR SCCR2,Y %00000100
	RTS

SEND_CHAR
* sends character in A
SC_1
	BRCLR SCSR,Y %10000000 SC_1	* loop if TDRE == 0
	STAA SCDR,Y			* output the data
	RTS

SEND_MSG
* transmits msg pointed to by X, stops when encounters \0 ($00)
	PSHA
SM_1
	LDAA 00,X
	BEQ DONE		* if NULL
	BSR SEND_CHAR
	INX
	BRA SM_1
DONE
	PULA
	RTS

RCV_CHAR
* returns char in A, error conditions in B
* must improve error checking

RC_1
	BRCLR SCSR,Y %00100000 RC_1	* loop until at one
	LDAB SCSR,Y	* get error FE, NF, OR
	ANDB #$0F	* isolate errors
	LDAA SCDR,Y	* get the byte
	RTS

RCV_MSG
* stores characters in location beginning at X.
* returns if specified character is encountered or if max number
* of characters.
* string is terminated with null.
	CLR NUM_CHARS	* no chars received yet
RM_1
	BSR RCV_CHAR	* no error checking
	STAA 00,X	* save
	CMPA LAST_CHAR	* is it the last
	BEQ RM_2
	INX		* next loc
	INC NUM_CHARS
	LDAA NUM_CHARS
	CMPA MAX_CHARS	* if at max size
	BEQ RM_2
	BRA RM_1
RM_2
	CLR 00,X	* add null char
	RTS

	ORG DSCT

LAST_CHAR	RMB 1
MAX_CHARS	RMB 1
NUM_CHARS	RMB 1
MSG_RCV		RMB 80

	ORG IDSCT

MSG_SEND
	FCC 'Now is the time for all good men to come to the aid'
	FCB $0A $0D
	FCC 'of their party'
	FCB $0A $0D
	FCB $00
