A2osX/LIB/LIBTCPIP.S.txt
2016-01-10 23:17:49 +01:00

517 lines
11 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.

PR#3
PREFIX /A2OSX.SRC
NEW
INC 1
AUTO 6
.LIST OFF
.OP 65C02
.OR $2000
.TF /A2OSX.BOOT/LIB/LIBTCPIP.O
*--------------------------------------
.INB INC/MACROS.I
.INB INC/A2OSX.I
.INB INC/LIBTCPIP.I
*--------------------------------------
ZPFrameBase1 .EQ ZPLIB
ZPFrameLen1 .EQ ZPLIB+2
ZPFramePtr1 .EQ ZPLIB+4
ZPPtrIP .EQ ZPLIB+6
ZPPtrMAC .EQ ZPLIB+8
ZPPtrDNS .EQ ZPLIB+10
ZPTmpPtr1 .EQ ZPLIB+12
ZPTmpPtr2 .EQ ZPLIB+14
*--------------------------------------
* 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
.DA 0
.DA 0
.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
.1 .DA LIB.LOAD
.DA LIB.UNLOAD
.DA SET.IPCFG
.DA GET.IPCFG
.DA NEW.ICMP.FRAME
.DA NEW.TCP.FRAME
.DA NEW.UDP.FRAME
.DA NEW.IP.FRAME
.DA NEW.ARP.FRAME
.DA NEW.ETH.FRAME
.DA SEND.ICMP.FRAME
.DA SEND.TCP.FRAME
.DA SEND.UDP.FRAME
.DA SEND.IP.FRAME
.DA SEND.ARP.FRAME
.DA SEND.ETH.FRAME
.DA ARP.CLEAR
.DA ARP.QUERY
.DA ARP.ADD
.DA ARP.GETCACHE
.DA DNS.CLEAR
.DA DNS.QUERY
.DA DNS.ADD
.DA DNS.GETCACHE
.DA UDP.LISTENER
.DA TCP.LISTENER
.DA ADD.LISTENER
.DA REM.LISTENER
*--------------------------------------
L.IPCFG .DA IPCFG
L.ARP.CACHE .DA ARP.CACHE
L.DNS.CACHE .DA DNS.CACHE
.DA 0
*--------------------------------------
LIB.LOAD lda bFirstLoad
bne .8
jsr ARP.CLEAR
jsr DNS.CLEAR
jsr DYN.CLEAR
inc bFirstLoad
.8 clc
rts
*--------------------------------------
LIB.UNLOAD clc
rts
*--------------------------------------
* PULLW = PTR to S.IPCFG
*--------------------------------------
SET.IPCFG >PUSHBI K.PROTOID.IP
>SYSCALL SYS.SetNetCfg
bcs GET.IPCFG.RTS
*--------------------------------------
* grab a copy on the fly for lib function
* OUT :
* Y,A = PTR to S.IPCFG
*--------------------------------------
GET.IPCFG lda #K.PROTOID.IP
>SYSCALL SYS.GetNetCfgA
bcs GET.IPCFG.RTS
>STYA ZPTmpPtr1
ldy #S.IPCFG
.1 dey
lda (ZPTmpPtr1),y
sta IPCFG,y
tya
bne .1
ldx #S.IPCFG.HDEV
lda IPCFG,x
>SYSCALL SYS.GetDevByIDA
>STYA NetDevJmp+1
ldx #DEVMGR.GETINFO
jsr NetDevJmp
>STYA ZPTmpPtr2
ldy #S.DEVINFO.FLAGS
lda (ZPTmpPtr2),y
sta DevFlags
and #S.DEVINFO.FLAGS.ARPOFFLOAD
beq .2
>LDYA ZPTmpPtr1
ldx #DEVMGR.SETIPCFG
jsr NetDevJmp
.2 >LDYA ZPTmpPtr1
clc
GET.IPCFG.RTS rts
*--------------------------------------
* Out:
* Y,A = Frame PTR
* X = hMem
*--------------------------------------
NEW.ICMP.FRAME jsr NEW.IP.FRAME
bcs .9
ldy #S.IP.PROTOCOL
lda #S.IP.PROTOCOL.ICMP
sta (ZPFrameBase1),y
>LDYA ZPFrameBase1
clc
.9 rts
*--------------------------------------
* Out:
* Y,A = Frame PTR
* X = hMem
*--------------------------------------
NEW.TCP.FRAME jsr NEW.IP.FRAME
bcs .9
ldy #S.IP.PROTOCOL
lda #S.IP.PROTOCOL.TCP
sta (ZPFrameBase1),y
>LDYA ZPFrameBase1
clc
.9 rts
*--------------------------------------
* Out:
* Y,A = Frame PTR
* X = hMem
*--------------------------------------
NEW.UDP.FRAME jsr NEW.IP.FRAME
bcs .9
ldy #S.IP.PROTOCOL
lda #S.IP.PROTOCOL.UDP
sta (ZPFrameBase1),y
>LDYA ZPFrameBase1
clc
.9 rts
*--------------------------------------
* Out:
* Y,A = Frame PTR
* X = hMem
*--------------------------------------
NEW.IP.FRAME jsr NEW.ETH.FRAME
bcs .9
ldy #S.ETH.ETHERTYPE
lda /S.ETH.ETHERTYPE.IP
sta (ZPFrameBase1),y
iny
lda #S.ETH.ETHERTYPE.IP
sta (ZPFrameBase1),y
ldy #S.IP.V.IHL.DSCP.ECN
lda #$45
sta (ZPFrameBase1),y
iny
lda #$0
sta (ZPFrameBase1),y
ldy #S.IP.IDENTIFICATION
sta (ZPFrameBase1),y
inc
iny
sta (ZPFrameBase1),y
dec
ldy #S.IP.FRAGMENT.FLAGS
sta (ZPFrameBase1),y
iny
sta (ZPFrameBase1),y
ldy #S.IP.TTL
lda #K.IP.TTL
sta (ZPFrameBase1),y
>LDYA ZPFrameBase1
clc
.9 rts
*--------------------------------------
* Out:
* Y,A = Frame PTR
* X = hMem
*--------------------------------------
NEW.ARP.FRAME jsr NEW.ETH.FRAME
bcs .9
ldy #S.ETH.ETHERTYPE
lda /S.ETH.ETHERTYPE.ARP
sta (ZPFrameBase1),y
iny
lda #S.ETH.ETHERTYPE.ARP
sta (ZPFrameBase1),y
ldy #S.ARP.HTYPE
lda #0
sta (ZPFrameBase1),y
iny
lda #1
sta (ZPFrameBase1),y
iny
lda #8
sta (ZPFrameBase1),y
iny
lda #0
sta (ZPFrameBase1),y
iny
lda #6
sta (ZPFrameBase1),y
iny
lda #4
sta (ZPFrameBase1),y
>LDYA ZPFrameBase1
clc
.9 rts
*--------------------------------------
* Out:
* Y,A = Frame PTR
* X = hMem
*--------------------------------------
NEW.ETH.FRAME >PUSHWI K.ETH.FRAME.LEN
>PUSHBI S.MEM.F.INIT0
>SYSCALL SYS.GetMem
bcs .9
>STYA ZPFrameBase1
clc
.9 rts
*--------------------------------------
* In:
* Out:
*--------------------------------------
SEND.ICMP.FRAME >PULLW ZPFrameBase1
>PULLW ZPFrameLen1
>PUSHW ZPFrameLen1
>PUSHWI S.ICMP.TYPE
jsr ComputeChecksum
phy
ldy #S.ICMP.CHECKSUM
sta (ZPFrameBase1),y
iny
pla
sta (ZPFrameBase1),y
bra SEND.IP.FRAME.I
*--------------------------------------
* In:
* Out:
*--------------------------------------
SEND.TCP.FRAME >PULLW ZPFrameBase1
>PULLW ZPFrameLen1
bra SEND.IP.FRAME.I
*--------------------------------------
* In:
* Out:
*--------------------------------------
SEND.UDP.FRAME >PULLW ZPFrameBase1
>PULLW ZPFrameLen1
SEND.UDP.FRAME.I
ldy #S.UDP.LENGTH+1
lda ZPFrameLen1
sec
sbc #S.IP
sta (ZPFrameBase1),y
dey
lda ZPFrameLen1+1
sbc /S.IP
sta (ZPFrameBase1),y
lda #0 SET.UDP.CHECKSUM
ldy #S.UDP.CHECKSUM (0 = Disable UDP Checksum)
sta (ZPFrameBase1),y
iny
sta (ZPFrameBase1),y
bra SEND.IP.FRAME.I
*--------------------------------------
* In:
* Out:
*--------------------------------------
SEND.IP.FRAME >PULLW ZPFrameBase1
>PULLW ZPFrameLen1
SEND.IP.FRAME.I ldx #S.IPCFG.IP
ldy #S.IP.SRC
.10 lda IPCFG,x
sta (ZPFrameBase1),y
inx
iny
cpy #S.IP.SRC+4
bne .10
jsr ARP.RESOLVE
bcs .9
ldy #S.IP.TOTAL.LENGTH+1
lda ZPFrameLen1
sec
sbc #S.ETH
sta (ZPFrameBase1),y
dey
lda ZPFrameLen1+1
sbc /S.ETH
sta (ZPFrameBase1),y
stz IP.CHECKSUM RESET.IP.CHECKSUM
stz IP.CHECKSUM+1
stz IP.CHECKSUM+2
* stz IP.CHECKSUM+3 Optimized for IP Header
ldy #S.IP.V.IHL.DSCP.ECN
.1 lda (ZPFrameBase1),y
tax
iny
lda (ZPFrameBase1),y
iny
clc
adc IP.CHECKSUM
sta IP.CHECKSUM
txa
adc IP.CHECKSUM+1
sta IP.CHECKSUM+1
bcc .2
inc IP.CHECKSUM+2
*.2 bne .3 Optimized for IP Header
* inc IP.CHECKSUM+3 Optimized for IP Header
.2 cpy #S.IP
bne .1
lda IP.CHECKSUM
clc
adc IP.CHECKSUM+2
eor #$FF
ldy #S.IP.HDR.CHECKSUM+1
sta (ZPFrameBase1),y
dey
lda IP.CHECKSUM+1
* adc IP.CHECKSUM+3 Optimized for IP Header
adc #0 Optimized for IP Header
eor #$FF
sta (ZPFrameBase1),y
bra SEND.ETH.FRAME
.9 rts
*--------------------------------------
* In:
* Out:
*--------------------------------------
SEND.ARP.FRAME >PULLW ZPFrameBase1
SEND.ARP.FRAME.I
>LDYAI S.ARP default ARP Frame length
>STYA ZPFrameLen1
*--------------------------------------
* In:
* Out:
*--------------------------------------
SEND.ETH.FRAME >PUSHW ZPFrameLen1
>PUSHW ZPFrameBase1
ldx #DEVMGR.NET.SEND
jsr NetDevJmp
rts
*--------------------------------------
UDP.LISTENER >PULLW ZPFrameBase1
>PULLW ZPFrameLen1
sec
rts
*--------------------------------------
TCP.LISTENER >PULLW ZPFrameBase1
>PULLW ZPFrameLen1
sec
rts
*--------------------------------------
ADD.LISTENER sec
rts
*--------------------------------------
REM.LISTENER sec
rts
*--------------------------------------
DYN.CLEAR
*--------------------------------------
DYN.GET
*--------------------------------------
DYN.RELEASE
*--------------------------------------
.INB LIB/LIBTCPIP.S.ARP
.INB LIB/LIBTCPIP.S.DNS
*--------------------------------------
* PRIVATE
*--------------------------------------
*--------------------------------------
ToLowerCaseA cmp #'A'
bcc .1
cmp #'Z'+1
bcs .1
adc #$20
.1 rts
*--------------------------------------
* In:
* PULLW = Start Offset
* PULLW = End Offset
* Out:
* Y,A = CheckSum
*--------------------------------------
ComputeChecksum >PULLA
clc
adc ZPFrameBase1
sta ZPTmpPtr1
>PULLA
adc ZPFrameBase1+1
sta ZPTmpPtr1+1
>PULLA
clc
adc ZPFrameBase1
sta TmpOffset
>PULLA
adc ZPFrameBase1+1
sta TmpOffset+1
stz IP.CHECKSUM RESET.IP.CHECKSUM
stz IP.CHECKSUM+1
stz IP.CHECKSUM+2
stz IP.CHECKSUM+3
ldy #1
.1 lda (ZPTmpPtr1),y
clc
adc IP.CHECKSUM
sta IP.CHECKSUM
lda (ZPTmpPtr1)
adc IP.CHECKSUM+1
sta IP.CHECKSUM+1
bcc .3
inc IP.CHECKSUM+2
bne .3
inc IP.CHECKSUM+3
.3 lda ZPTmpPtr1
clc
adc #2
sta ZPTmpPtr1
bcc .4
inc ZPTmpPtr1+1
.4 cmp TmpOffset
bne .1
lda ZPTmpPtr1+1
cmp TmpOffset+1
bne .1
lda IP.CHECKSUM
clc
adc IP.CHECKSUM+2
eor #$FF
tay
lda IP.CHECKSUM+1
adc IP.CHECKSUM+3
eor #$FF
rts
*--------------------------------------
NetDevJmp jmp *
*--------------------------------------
CS.END
*--------------------------------------
bFirstLoad .DA #0
Status .BS 1
DevFlags .BS 1
DNSMessageID .BS 2
DNSDynamicPort .BS 2
DNSHostTTL .BS 4
DNSHostName .BS K.DNS.MAXLEN+1
DNSHostNameLen .BS 1
IP.CHECKSUM .BS 4
IPCFG .BS S.IPCFG
ARP.CACHE .BS K.ARPCACHE.SIZE*S.ARPCACHE
DNS.CACHE .BS K.DNSCACHE.SIZE*S.DNSCACHE
UDP.TABLE .BS K.UDPTABLE.SIZE*S.LISTENER
TCP.TABLE .BS K.TCPTABLE.SIZE*S.LISTENER
DYNPORT.UDP.LAST .BS 2
DYNPORT.TCP.LAST .BS 2
DYNPORT.UDP .BS K.DYNPORT.MAX*4
DYNPORT.TCP .BS K.DYNPORT.MAX*4
*TmpCounter .BS 2
TmpOffset .BS 2
*--------------------------------------
MAN
SAVE LIB/LIBTCPIP.S
ASM