A2osX/BIN/TELNET.S.txt
2018-09-28 17:24:44 +02:00

318 lines
6.0 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

NEW
PREFIX /A2OSX.BUILD
AUTO 4,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
*--------------------------------------
TIMEOUT.MAX .EQ 40 4 sec.
BUFSIZE .EQ 256
*--------------------------------------
ZPIPCfgPtr .EQ ZPBIN
ZPBufPtr .EQ ZPBIN+2
ZPBufLen .EQ ZPBIN+4
*--------------------------------------
* 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 Length To Relocate
.DA DS.END-DS.START Data Segment to Allocate
.DA #16 SS
.DA #6 ZP
.DA 0
*--------------------------------------
.1 .DA CS.INIT
.DA CS.RUN
.DA CS.DOEVENT
.DA CS.QUIT
L.LIBTCPIP .DA LIBTCPIP
L.Socket .DA Socket
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
lda (pPs)
ora #S.PS.F.EVENT Now accept events
sta (pPs)
clc
.9 rts
*--------------------------------------
CS.RUN >LIBCALL hLIBTCPIP,LIBTCPIP.GET.IPCFG is TCPIP loaded ?
>STYA ZPIPCfgPtr
lda (ZPIPCfgPtr) Configured ?
bmi CS.RUN.IPOK
>LDYA L.MSG.IPKO
>SYSCALL puts
lda #K.E.SYN
sec
rts
CS.RUN.IPOK ldy #S.PS.ARGC
lda (pPs),y
beq .9
jsr Init.Timeout
.2 >PUSHEA.G DST.IP
lda #1
>SYSCALL GetArg
>PUSHYA
>LIBCALL hLIBTCPIP,LIBTCPIP.HST.GETBYNAME
bcc CS.RUN.HOSTOK
>SLEEP
jsr Wait.Timeout
bcc .2
lda #1
>SYSCALL GetArg
>PUSHYA
>PUSHBI 2
>LDYA L.MSG.UNKNOWN
>SYSCALL printf
bra .99
.9 >LDYA L.MSG.USAGE
>SYSCALL puts
.99 lda #K.E.SYN
sec
rts
CS.RUN.HOSTOK ldy #S.PS.ARGC
lda (pPs),y
cmp #2
bcc CS.RUN.PORTOK
lda #2
>SYSCALL GetArg
>SYSCALL atoi
>STYA Socket.Dst.Port
CS.RUN.PORTOK lda #1
>SYSCALL GetArg
>PUSHYA
>PUSHW Socket.Dst.Port
ldy #DST.IP+3
ldx #3
.1 lda (pData),y
sta Socket.Dst.Addr,x
>PUSHA
dey
dex
bpl .1
>PUSHBI 8
>LDYA L.MSG.HOSTOK
>SYSCALL printf
CS.RUN.OPENSKT ldx #3
ldy #S.IPCFG.IP+3
.1 lda (ZPIPCfgPtr),y
sta Socket.Src.Addr,x
dey
dex
bpl .1
>PUSHW L.Socket
>LIBCALL hLIBTCPIP,LIBTCPIP.SKT.New
bcc .2
>LDYA L.MSG.SKTKO
>SYSCALL puts
lda #K.E.SYN
sec
.9 rts
.2 >STA.G hSocket
>LDYA L.MSG.SKTOK
>SYSCALL puts
>LDYAI BUFSIZE
>SYSCALL getmem
bcs .9
>STYA ZPBufPtr
txa
>STA.G hBuf
CS.RUN.LOOP >SLEEP
>PUSHWI BUFSIZE
>PUSHW ZPBufPtr
>LDA.G hSocket
>LIBCALL hLIBTCPIP,LIBTCPIP.SKT.Read
bcc .1
tay
beq .2 no char
jmp CS.RUN.SKTERR
.1 >PUSHYA len
>PUSHW ZPBufPtr
ldy #S.PS.hStdOut
lda (pPs),y
>SYSCALL write
bcs .11
.2 >PUSHWI BUFSIZE
>PUSHW ZPBufPtr
ldy #S.PS.hStdIn
lda (pPs),y
>SYSCALL read
bcc .3
tay
beq CS.RUN.LOOP no char from STDIN
.11 jmp CS.RUN.IOERR
.3 >STYA ZPBufLen
lda (ZPBufPtr)
cmp #3 Ctrl-C
beq CS.RUN.USER
>PUSHW ZPBufLen
>PUSHW ZPBufPtr
>LDA.G hSocket
>LIBCALL hLIBTCPIP,LIBTCPIP.SKT.Write
bcs CS.RUN.SKTERR
jmp CS.RUN.LOOP
.9 lda #0
sec
rts
*--------------------------------------
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 #3
sec
rts
*--------------------------------------
CS.DOEVENT lda (pEvent)
bpl .9 is it a TIMER event?
ldy #TimeOut
lda (pData),y
beq .9
dec
sta (pData),y
.9 sec do not discard TIMER event
rts
*--------------------------------------
CS.QUIT >LDA.G hSocket
beq .1
>LIBCALL hLIBTCPIP,LIBTCPIP.SKT.Close
.1 >LDA.G hBuf
beq .2
>SYSCALL freemem
.2 lda hLIBTCPIP
>SYSCALL UnloadLib
clc
rts
*--------------------------------------
Init.Timeout ldy #TimeOut
lda #TIMEOUT.MAX
sta (pData),y
rts
*--------------------------------------
Wait.TimeOut sec
ldy #TimeOut
lda (pData),y
beq .9
clc
.9 rts
*--------------------------------------
CS.END
LIBTCPIP .AZ "libtcpip.o"
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."
MSG.SKTERR .AZ "Socket Error : $%h\r\n"
MSG.IOERR .AZ "I/O Error : $%h\r\n"
MSG.USER .AZ "User interrupt."
*--------------------------------------
Socket .DA #S.SOCKET.T.STREAM
.BS 1
.BS 1
.BS 1
Socket.Src.Addr .BS 4
Socket.Src.Port .DA 0 Dynamic
Socket.Dst.Addr .BS 4
Socket.Dst.Port .DA TCP.PORT.TELNET
*--------------------------------------
.DUMMY
.OR 0
DS.START
DST.IP .BS 4
hBuf .BS 1
hSocket .BS 1
TimeOut .BS 1
DS.END
.ED
*--------------------------------------
MAN
SAVE /A2OSX.SRC/BIN/TELNET.S
ASM