NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF bin/term
*--------------------------------------
				.INB inc/macros.i
				.INB inc/a2osx.i
*--------------------------------------
TIMEOUT.MAX		.EQ 200					20 sec.
BUFSIZE			.EQ 256
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPBufPtr		.BS 2
ZPDataLen		.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

				>PUSHYA
				>PUSHBI O.RDWR
				>PUSHBI 0				type
				>PUSHWZ					auxtype
				>SYSCALL FOpen
				bcs .99

				sta 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
CS.RUN.RTS		rts
*--------------------------------------
CS.RUN.LOOP		>SLEEP

				lda hDev
				>SYSCALL FEOF
				bcs .9
				tay
				bne .1					EOF, no char

				lda hDev
				jsr CS.Read
				bcs CS.RUN.RTS

				>STYA ZPDataLen
				
				ldy #S.PS.hStdOut
				lda (pPS),y
				jsr CS.Write
				bcs .9

.1				ldy #S.PS.hStdIn
				lda (pPS),y

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

				ldy #S.PS.hStdIn
				lda (pPS),y
				jsr CS.Read
				bcs .9

				>STYA ZPDataLen
				
				lda (ZPBufPtr)
				eor #$14				Ctrl-T
				clc
				beq .9					exit with A=0,CC

				lda hDev
				jsr CS.Write
				bcc CS.RUN.LOOP
				
.9				rts
*--------------------------------------
CS.Read			>PUSHA
				>PUSHW ZPBufPtr
				>PUSHWI BUFSIZE
				>SYSCALL FRead
				rts
*--------------------------------------
CS.Write		>PUSHA
				>PUSHW ZPBufPtr
				>PUSHW ZPDataLen
				>SYSCALL FWrite
				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
