NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF BIN/TELNET
*--------------------------------------
				.INB INC/MACROS.I
				.INB INC/A2OSX.I
				.INB INC/ETH.I
				.INB INC/LIBTCPIP.I
				.INB INC/NET.TELNET.I				
*--------------------------------------
TIMEOUT.MAX		.EQ 100					10 sec.
BUFSIZE			.EQ 256
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPIPCfgPtr		.BS 2
ZPBufPtr		.BS 2
ZPBufLen		.BS 2
hBuf			.BS 1
hSocket			.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 #64					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.LIBTCPIP		.DA LIBTCPIP
L.SA.LOCAL		.DA SA.LOCAL
L.SA.REMOTE		.DA SA.REMOTE
L.SA.REMOTE.AD	.DA SA.REMOTE+S.SOCKADDR.ADDR
L.MSG.IPKO		.DA MSG.IPKO
L.MSG.USAGE		.DA MSG.USAGE
L.MSG.UNKNOWN	.DA MSG.UNKNOWN
L.MSG.HOSTOK	.DA MSG.HOSTOK
L.MSG.SKTKO		.DA MSG.SKTKO
L.MSG.SKTOK		.DA MSG.SKTOK
L.MSG.SKTERR	.DA MSG.SKTERR
L.MSG.IOERR		.DA MSG.IOERR
L.MSG.USER		.DA MSG.USER
				.DA 0
*--------------------------------------
CS.INIT			>LDYA L.LIBTCPIP
				>SYSCALL LoadLib
				bcs .9
				sta hLIBTCPIP

.9				rts
*--------------------------------------
CS.RUN			>LIBCALL hLIBTCPIP,LIBTCPIP.GETCFG	is TCPIP loaded ?
				>STYA ZPIPCfgPtr
				
				lda (ZPIPCfgPtr)		Configured ?
				bmi CS.RUN.IPOK
				
				>LDYA L.MSG.IPKO
				>SYSCALL puts
				lda #E.SYN
				sec				
				rts
				
CS.RUN.IPOK		ldy #S.PS.ARGC
				lda (pPs),y
				cmp #1
				bcc .9
				
				ldy #S.IPCFG.IP+3
				ldx #3
				
.1				lda (ZPIPCfgPtr),y
				sta SA.LOCAL+S.SOCKADDR.ADDR,x
				dey
				dex
				bpl .1
				
				lda #TIMEOUT.MAX
				sta TimeOut

.2				>PUSHW L.SA.REMOTE.AD
				lda #1
				>SYSCALL ArgV
				>PUSHYA
				>LIBCALL hLIBTCPIP,LIBTCPIP.HST.GETBYNAME
				bcc CS.RUN.HOSTOK
				
				>SLEEP
				lda  TimeOut
				bne .2

				lda #1
				>SYSCALL ArgV
				>PUSHYA
				>PUSHBI 2
				>LDYA L.MSG.UNKNOWN
				>SYSCALL printf
				bra .99
				
.9				>LDYA L.MSG.USAGE
				>SYSCALL puts

.99				lda #E.SYN
				sec				
				rts

CS.RUN.HOSTOK	lda #2
				>SYSCALL ArgV
				bcs CS.RUN.PORTOK
				
				>SYSCALL atoi
				>STYA SA.REMOTE+S.SOCKADDR.PORT
				
CS.RUN.PORTOK	lda #1
				>SYSCALL ArgV
				>PUSHYA
				
				>PUSHW SA.REMOTE+S.SOCKADDR.PORT

				ldx #3
				
.1				>PUSHB SA.REMOTE+S.SOCKADDR.ADDR,x
				dex
				bpl .1
				
				>PUSHBI 8
				>LDYA L.MSG.HOSTOK
				>SYSCALL printf
		
CS.RUN.OPENSKT	>PUSHBI 0				no protocol
				lda #S.SOCKET.T.STREAM
				>LIBCALL hLIBTCPIP,LIBTCPIP.Socket
				bcs .9

				sta hSocket

				>PUSHW L.SA.LOCAL
				lda hSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.Bind
				bcc .2
				
.9				>LDYA L.MSG.SKTKO
				>SYSCALL puts
				lda #E.SYN
				sec				
				rts
				
.2				>PUSHW L.SA.REMOTE
				lda hSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.Connect
				bcs .9
				
				>PUSHBI 0
				>LDYA L.MSG.SKTOK
				>SYSCALL printf
				
				>LDYAI BUFSIZE
				>SYSCALL getmem
				bcs .9
				>STYA ZPBufPtr
				stx hBuf

CS.RUN.LOOP		>SLEEP

				lda hSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.EOF
				bcs .99
				
				tay
				bne .2					EOF, no char

				>PUSHWI BUFSIZE
				>PUSHW ZPBufPtr
				lda hSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.Read
.99				bcs .9
				
				>PUSHYA					len
				>PUSHW ZPBufPtr
				ldy #S.PS.hStdOut
				lda (pPs),y
				>SYSCALL fwrite
				bcs .11
				
.2				ldy #S.PS.hStdIn
				lda (pPs),y

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

				>PUSHWI BUFSIZE			
				>PUSHW ZPBufPtr
				ldy #S.PS.hStdIn
				lda (pPs),y
				>SYSCALL fread
.11				bcs CS.RUN.IOERR
				
.3				>STYA ZPBufLen
				lda (ZPBufPtr)
				cmp #$14					Ctrl-T
				beq CS.RUN.USER
				
				>PUSHW ZPBufLen
				>PUSHW ZPBufPtr
				lda hSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.Write

.9				bcs CS.RUN.SKTERR
				
				jmp CS.RUN.LOOP
*--------------------------------------
CS.RUN.SKTERR	pha
				>PUSHA
				>PUSHBI 1
				>LDYA L.MSG.SKTERR
				>SYSCALL printf
				pla
				sec
				rts
*--------------------------------------
CS.RUN.IOERR	pha
				>PUSHA
				>PUSHBI 1
				>LDYA L.MSG.IOERR
				>SYSCALL printf
				pla
				sec
				rts
*--------------------------------------
CS.RUN.USER		>LDYA L.MSG.USER
				>SYSCALL puts
				lda #0
				sec
				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 hSocket
				beq .1

				>LIBCALL hLIBTCPIP,LIBTCPIP.Shutdown
				
.1				lda hBuf
				beq .2
				
				>SYSCALL freemem
				
.2				lda hLIBTCPIP
				beq .8
				>SYSCALL UnloadLib
				
.8				clc
				rts
*--------------------------------------
CS.END
LIBTCPIP		.AZ "libtcpip"
hLIBTCPIP		.BS 1
MSG.IPKO		.AZ "TCP/IP Not Loaded/Configured."
MSG.USAGE		.AZ "Usage : TELNET <ip|host> [port]"
MSG.UNKNOWN		.AZ "%s: Unknown host\r\n"
MSG.HOSTOK		.AZ "Connecting to %d.%d.%d.%d:%D (%s)..."
MSG.SKTKO		.AZ "Failed to Open Socket."
MSG.SKTOK		.AZ "Connected\r\n(Exit key is Ctrl-T)\r\n"
MSG.SKTERR		.AZ "Socket Error : $%h\r\n"
MSG.IOERR		.AZ "I/O Error : $%h\r\n"
MSG.USER		.AZ "User interrupt."
*--------------------------------------
SA.LOCAL		.DA #AF.INET			S.SOCKADDR.AF
				.BS 1
				.BS 4					S.SOCKADDR.ADDR
				.BS 2					S.SOCKADDR.PORT
*--------------------------------------
SA.REMOTE		.DA #AF.INET			S.SOCKADDR.AF
				.BS 1
				.BS 4					S.SOCKADDR.ADDR
				.DA TCP.PORT.TELNET
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
DS.END
				.ED
*--------------------------------------
MAN
SAVE USR/SRC/BIN/TELNET.S
ASM
