mirror of
https://github.com/A2osX/A2osX.git
synced 2024-11-04 03:05:43 +00:00
367 lines
7.1 KiB
Plaintext
367 lines
7.1 KiB
Plaintext
NEW
|
||
PREFIX /A2OSX.BUILD
|
||
AUTO 4,1
|
||
.LIST OFF
|
||
.OP 65C02
|
||
.OR $2000
|
||
.TF /A2OSX.BUILD/BIN/PING
|
||
*--------------------------------------
|
||
.INB /A2OSX.BUILD/INC/MACROS.I
|
||
.INB /A2OSX.BUILD/INC/A2OSX.I
|
||
.INB /A2OSX.BUILD/INC/LIBTCPIP.I
|
||
*--------------------------------------
|
||
TIMEOUT.MAX .EQ 20 2 sec.
|
||
*--------------------------------------
|
||
ZPFrameBase .EQ ZPBIN
|
||
ZPFrameLen .EQ ZPBIN+2
|
||
ZPFramePtr1 .EQ ZPBIN+4
|
||
ZPIPCfgPtr .EQ ZPBIN+6
|
||
*--------------------------------------
|
||
* File Header (16 Bytes)
|
||
*--------------------------------------
|
||
CS.START cld
|
||
jmp (.1,x)
|
||
.DA #$61 6502,Level 1 (65c02)
|
||
.DA #1 BIN Layout Version 1
|
||
.DA 0
|
||
.DA CS.END-CS.START Code Length To Relocate
|
||
.DA DS.END-DS.START Data Segment to Allocate
|
||
.DA #16 SS
|
||
.DA #8 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.ICMP.Request .DA ICMP.Request
|
||
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.REPLY .DA MSG.REPLY
|
||
L.MSG.UNREACH .DA MSG.UNREACH
|
||
.DA 0
|
||
*--------------------------------------
|
||
CS.INIT >LDYA L.LIBTCPIP
|
||
>SYSCALL LoadLib.YA
|
||
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 PrintF.YA
|
||
lda #K.E.SYN
|
||
sec
|
||
rts
|
||
|
||
CS.RUN.IPOK ldy #S.PS.ARGC
|
||
lda (pPs),y
|
||
|
||
beq .9
|
||
|
||
jsr Init.Timeout
|
||
|
||
.2 >PUSHW pData DST.IP
|
||
lda #1
|
||
>SYSCALL GetArg.A
|
||
>PUSHYA
|
||
>LIBCALL hLIBTCPIP,LIBTCPIP.HST.GETBYNAME
|
||
bcc CS.RUN.HOSTOK
|
||
>SLEEP
|
||
jsr Wait.Timeout
|
||
bcc .2
|
||
|
||
lda #1
|
||
>SYSCALL GetArg.A
|
||
>PUSHYA
|
||
>LDYA L.MSG.UNKNOWN
|
||
bra .99
|
||
|
||
.9 >LDYA L.MSG.USAGE
|
||
.99 >SYSCALL PrintF.YA
|
||
lda #K.E.SYN
|
||
sec
|
||
rts
|
||
|
||
CS.RUN.HOSTOK lda #1
|
||
>SYSCALL GetArg.A
|
||
>PUSHYA
|
||
|
||
ldy #DST.IP+3
|
||
ldx #3
|
||
|
||
.1 lda (pData),y
|
||
sta Socket.Dst.Addr,x
|
||
>PUSHA
|
||
dey
|
||
dex
|
||
bpl .1
|
||
|
||
>LDYA L.MSG.HOSTOK
|
||
>SYSCALL PrintF.YA
|
||
|
||
CS.RUN.OPENSKT ldx #3
|
||
ldy #S.IPCFG.IP+3
|
||
|
||
.1 lda (ZPIPCfgPtr),y
|
||
sta Socket.Src.Addr,x
|
||
dey
|
||
dex
|
||
bpl .1
|
||
|
||
ldy #Identifier
|
||
lda A2OSX.RANDOM16
|
||
sta Socket.Dst.Port
|
||
sta (pData),y
|
||
iny
|
||
lda A2OSX.RANDOM16+1
|
||
sta Socket.Dst.Port+1
|
||
sta (pData),y
|
||
|
||
>PUSHW L.Socket
|
||
>LIBCALL hLIBTCPIP,LIBTCPIP.SKT.New
|
||
bcc .2
|
||
|
||
>LDYA L.MSG.SKTKO
|
||
>SYSCALL PrintF.YA
|
||
lda #K.E.SYN
|
||
sec
|
||
rts
|
||
|
||
.2 txa
|
||
ldy #hSocket
|
||
sta (pData),y
|
||
|
||
CS.RUN.SEND.ECHO
|
||
ldy #Identifier
|
||
lda (pData),y
|
||
sta ICMP.Request.ID
|
||
iny
|
||
lda (pData),y
|
||
sta ICMP.Request.ID+1
|
||
|
||
ldy #Sequence
|
||
sec
|
||
lda (pData),y
|
||
adc #0
|
||
sta (pData),y
|
||
sta ICMP.Request.SQ
|
||
|
||
iny
|
||
lda (pData),y
|
||
adc #0
|
||
sta (pData),y
|
||
sta ICMP.Request.SQ+1
|
||
|
||
jsr Init.Timeout
|
||
|
||
ldy #hSocket
|
||
>PUSHB (pData),y
|
||
>PUSHW L.ICMP.Request
|
||
>PUSHWI ICMP.RequestLen
|
||
|
||
>LIBCALL hLIBTCPIP,LIBTCPIP.SKT.Write
|
||
bcs CS.RUN.ERR
|
||
|
||
jsr Init.Timeout
|
||
|
||
CS.RUN.WAIT.REPLY
|
||
>SLEEP
|
||
>SYSCALL GetChar
|
||
bcs .1
|
||
cmp #3 Ctrl-C
|
||
beq .9
|
||
|
||
.1 ldy #hSocket
|
||
lda (pData),y
|
||
>LIBCALL hLIBTCPIP,LIBTCPIP.SKT.ReadA
|
||
bcs .2
|
||
|
||
ldy #hFrame
|
||
sta (pData),y
|
||
>SYSCALL GetMemPtr.A
|
||
>STYA ZPFrameBase
|
||
jsr CS.Print.REPLY
|
||
bcs CS.RUN.ERR
|
||
|
||
ldy #hFrame
|
||
lda (pData),y
|
||
>SYSCALL FreeMem.A
|
||
|
||
ldy #hFrame
|
||
lda #0
|
||
sta (pData),y
|
||
|
||
jmp CS.RUN.SEND.ECHO
|
||
|
||
.2 jsr Wait.Timeout
|
||
bcc CS.RUN.WAIT.REPLY
|
||
|
||
jsr CS.Print.UNREACH
|
||
bcs CS.RUN.ERR
|
||
|
||
jmp CS.RUN.SEND.ECHO
|
||
|
||
.9 lda #0
|
||
sec
|
||
CS.RUN.ERR 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.Print.UNREACH
|
||
ldy #DST.IP+3
|
||
>PUSHB (pData),y DST.IP+3
|
||
dey
|
||
>PUSHB (pData),y DST.IP+2
|
||
dey
|
||
>PUSHB (pData),y DST.IP+1
|
||
dey
|
||
>PUSHB (pData),y DST.IP
|
||
>LDYA L.MSG.UNREACH
|
||
>SYSCALL PrintF.YA
|
||
rts
|
||
*--------------------------------------
|
||
CS.Print.REPLY lda #TIMEOUT.MAX
|
||
sec
|
||
ldy #TimeOut
|
||
sbc (pData),y
|
||
|
||
stz TimeOut.ms
|
||
stz TimeOut.ms+1
|
||
|
||
tay
|
||
beq .3
|
||
|
||
.1 lda TimeOut.ms
|
||
clc
|
||
adc #100
|
||
sta TimeOut.ms
|
||
bcc .2
|
||
inc TimeOut
|
||
.2 dey
|
||
bne .1
|
||
|
||
.3 >PUSHW TimeOut.ms
|
||
ldy #S.IP.TTL
|
||
>PUSHB (ZPFrameBase),y
|
||
|
||
ldy #S.ICMP.SEQUENCE+1
|
||
>PUSHB (ZPFrameBase),y
|
||
dey
|
||
>PUSHB (ZPFrameBase),y
|
||
|
||
ldy #S.IP.SRC+3
|
||
>PUSHB (ZPFrameBase),y
|
||
dey
|
||
>PUSHB (ZPFrameBase),y
|
||
dey
|
||
>PUSHB (ZPFrameBase),y
|
||
dey
|
||
>PUSHB (ZPFrameBase),y
|
||
|
||
>LDYA L.MSG.REPLY
|
||
>SYSCALL PrintF.YA
|
||
rts
|
||
*--------------------------------------
|
||
CS.QUIT ldy #hFrame
|
||
lda (pData),y
|
||
beq .1
|
||
|
||
>SYSCALL FreeMem.A
|
||
|
||
.1 ldy #hSocket
|
||
lda (pData),y
|
||
|
||
>LIBCALL hLIBTCPIP,LIBTCPIP.SKT.CLOSEA
|
||
|
||
lda hLIBTCPIP
|
||
>SYSCALL UnloadLib.A
|
||
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 >CSTR "libtcpip.o"
|
||
MSG.IPKO >CSTR "TCP/IP Not Loaded/Configured.\r\n"
|
||
MSG.USAGE >CSTR "Usage : PING <ip|host>\r\n"
|
||
MSG.UNKNOWN >CSTR "%S: Unknown host\r\n"
|
||
MSG.HOSTOK >CSTR "PING %d.%d.%d.%d (%S)\r\n"
|
||
MSG.SKTKO >CSTR "Failed to Open Socket.\r\n"
|
||
MSG.UNREACH >CSTR "%d.%d.%d.%d: TimeOut/Host unreachable\r\n"
|
||
MSG.REPLY >CSTR "32 bytes from %d.%d.%d.%d, icmp_seq=%D, ttl=%d, time=%D ms\r\n"
|
||
hLIBTCPIP .BS 1
|
||
TimeOut.ms .BS 2
|
||
Socket .DA #S.SOCKET.SOCK.RAW
|
||
.BS 1
|
||
.DA #S.IP.PROTOCOL.ICMP
|
||
.BS 1
|
||
Socket.Src.Addr .BS 4
|
||
Socket.Src.Port .DA 0 Dynamic
|
||
Socket.Dst.Addr .BS 4
|
||
Socket.Dst.Port .DA 0 Identifier
|
||
*--------------------------------------
|
||
ICMP.Request .DA #S.ICMP.TYPE.ECHOREQ
|
||
.DA #0 CODE
|
||
.DA 0 CHECKSUM
|
||
ICMP.Request.ID .BS 2
|
||
ICMP.Request.SQ .BS 2
|
||
.AS "ABCDEFGHIJKLMNOP"
|
||
.AS "QRSTUVWXYZ012345"
|
||
ICMP.RequestLen .EQ *-ICMP.Request
|
||
*--------------------------------------
|
||
.DUMMY
|
||
.OR 0
|
||
DS.START
|
||
DST.IP .BS 4
|
||
hSocket .BS 1
|
||
hFrame .BS 1
|
||
Identifier .BS 2
|
||
Sequence .BS 2
|
||
TimeOut .BS 1
|
||
DS.END
|
||
.ED
|
||
*--------------------------------------
|
||
MAN
|
||
SAVE /A2OSX.SRC/BIN/PING.S
|
||
ASM
|