************
* Program ECHO (6811)
*
* Writes an explanation to the terminal.  Accepts input from the
* terminal, stores it and echoes to the terminal until either S7 on
* the I/O box is brought high or the number of characters is $FF.
*
* The entered text is then displayed on the terminal.
*
* Note the following Buffalo ROM utilities;
*
*  INPUT  reads char from terminal and returns in ACCA.  Returns 00
*     if no character.
*  OUT_CHAR outputs ACCA to the terminal
*  OUT_CR_LF outputs newline character
*  OUT_STRING outputs the string at adr specified by X.  Characters
*     are successively output until char $04 is encountered.
*
* P. H. Anderson, MSU, 24 Oct 91; 19 Jan 93
*************

PSCT		EQU $C000
DSCT		EQU $D000
IDSCT		EQU $D300

STACKTP 	EQU $0045
REG_BASE	EQU $1000

INPUT		EQU $E387
OUT_CHAR	EQU $E3B3
OUT_CR_LF	EQU $E4ED
OUT_STRING	EQU $E4FA

PORTB		EQU $04 	* output
PORTC		EQU $03 	* input
DDRC		EQU $07

	ORG PSCT

	LDS #STACKTP
	LDY #REG_BASE		* to allow indexed addressing

	CLR DDRC,Y

	LDX #MESSAGE_1		* play intro msg
	JSR OUT_STRING

	LDAA $FF
	STAA MEM_BYTES_LEFT	* 255 bytes
	LDX #MESS_STORE 	* beginning of storage

GET
	BRSET PORTC,Y %10000000 DONE
*if S7 at logic one - done
	JSR INPUT		* get the character
	TSTA
	BEQ GET			* loop until S7 or there is a char

	STAA 00,X		* save the character
	JSR OUT_CHAR		* echo it
	INX			* next address
	DEC MEM_BYTES_LEFT
	BNE GET

DONE
* terminate the string with char $04 and play it
	LDAA #$04
	STAA 00,X

	LDX #MESS_STORE
	JSR OUT_STRING		* play the string
	JSR OUT_CR_LF		* final new line

	SWI

	ORG DSCT

MEM_BYTES_LEFT	RMB 1
MESS_STORE	RMB $FF

	ORG IDSCT

MESSAGE_1
	FCC 'Entries from the terminal will be read, echoed and stored.'
	FCB $0D 	* new_line
	FCC 'To exit, bring S7 to logic 1.'
	FCB $0D, $04	* new_line and terminating character
