NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF BIN/TERM
*--------------------------------------
				.INB INC/MACROS.I
				.INB INC/A2OSX.I
				.INB INC/ETH.I
				.INB INC/LIBTCPIP.I
				.INB INC/NET.TELNET.I
*--------------------------------------
TIMEOUT.MAX		.EQ 200					20 sec.
BUFSIZE			.EQ 256
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPDevName		.BS 2
ZPBufPtr		.BS 2
ZPBufLen		.BS 2
hBuf			.BS 1
hDev			.BS 1
TimeOut			.BS 1
ZS.END
				.ED
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp (.1,x)
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					BIN Layout Version 1
				.DA #S.PS.F.EVENT		S.PS.F
				.DA #0
				.DA CS.END-CS.START		Code Size (without Constants)
				.DA DS.END-DS.START		Data Segment Size
				.DA #32					Stack Size
				.DA #ZS.END-ZS.START	Zero Page Size
				.DA 0
*--------------------------------------
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT		
				.DA	CS.QUIT
L.MSG.USAGE		.DA MSG.USAGE
L.MSG.DEVOK		.DA MSG.DEVOK
				.DA 0
*--------------------------------------
CS.INIT			clc
				rts
*--------------------------------------
CS.RUN			lda #1
				>SYSCALL ArgV
				bcs .9
				>STYA ZPDevName
				
				>PUSHWZ					auxtype
				>PUSHBI 0				type				>PUSHBI O.RDWR
				>PUSHBI O.RDWR
				>LDYA ZPDevName
				>SYSCALL fopen
				bcs .99
				
				stx hDev
				>LDYA L.MSG.DEVOK
				>SYSCALL puts

				>LDYAI BUFSIZE
				>SYSCALL getmem
				bcs .99
				
				>STYA ZPBufPtr
				stx hBuf

				bra CS.RUN.LOOP

.9				>LDYA L.MSG.USAGE
				>SYSCALL puts

				lda #E.SYN
				sec
.99				rts

CS.RUN.LOOP		>SLEEP

				lda hDev
				>SYSCALL feof
				bcs .99
				tay
				bne .2					EOF, no char

				>PUSHWI BUFSIZE
				>PUSHW ZPBufPtr
				lda hDev
				>SYSCALL fRead
				bcc .1
.99				rts
				
.1				>PUSHYA					len
				>PUSHW ZPBufPtr
				ldy #S.PS.hStdOut
				lda (pPs),y
				>SYSCALL fwrite
				bcs .9

.2				ldy #S.PS.hStdIn
				lda (pPs),y

				>SYSCALL feof
				bcs .9
				tay
				bne CS.RUN.LOOP			EOF = true, no char from STDIN

				>PUSHWI BUFSIZE			
				>PUSHW ZPBufPtr
				ldy #S.PS.hStdIn
				lda (pPs),y
				>SYSCALL fread
.3				bcs .9

				>STYA ZPBufLen
				lda (ZPBufPtr)
				cmp #$14				Ctrl-T
				beq .9

				>PUSHW ZPBufLen
				>PUSHW ZPBufPtr
				lda hDev
				>SYSCALL fWrite
				bcs .9

				jmp CS.RUN.LOOP

.9				rts				
*--------------------------------------
CS.DOEVENT		lda (pEvent)
				bpl .9					is it a TIMER event?
				lda TimeOut
				beq .9
				dec TimeOut
.9				sec						do not discard TIMER event
				rts
*--------------------------------------
CS.QUIT			lda hDev
				beq .1

				>SYSCALL fclose

.1				lda hBuf
				beq .2

				>SYSCALL freemem

.2				clc
				rts
*--------------------------------------
CS.END
MSG.USAGE		.AZ "Usage : TERM device "
MSG.DEVOK		.AZ "Connected (Exit key is Ctrl-T)"
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
DS.END
				.ED
*--------------------------------------
MAN
SAVE USR/SRC/BIN/TERM.S
ASM
