A2osX/LIB/LIBTCPIP.S.SKT.txt
2018-10-21 22:54:07 +02:00

1459 lines
28 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
*/--------------------------------------
* # Socket
* Create a new socket
* ## C
* `hFD socket(short int type, short int protocol);`
* ## ASM
* **In:**
* `>PUSHB protocol`
* `lda type`
* `>LIBCALL hLIBTCPIP,LIBTCPIP.socket`
* ## RETURN VALUE
* CC: A = hSOCKET
* CS: A = EC
*\--------------------------------------
SKT.Socket tax type (RAW,DGRAM,SEQPACKET,STREAM)
>PULLA get protocol (RAW)
SKT.Socket.I jmp (J.SKT.Socket,x)
SKT.Socket.RAW ldy #S.SOCKET socket.size
bra SKT.Socket.DSOCK
SKT.Socket.DGRAM
lda #S.IP.PROTOCOL.UDP
ldy #S.SOCKET socket.size
bra SKT.Socket.DSOCK
SKT.Socket.SEQPACKET
lda #S.IP.PROTOCOL.TCP
ldy #S.SOCKET.TCP socket.size
SKT.Socket.DSOCK
stx FD.DSOCK+S.SOCKET.T save type
sta FD.DSOCK+S.SOCKET.PROTO ICMP,UDP,TCP
lda #0 Y = socket.size
>SYSCALL GetMem0
bcs .9
>STYA ZPTmpPtr1
stx FD.DSOCK+S.FD.DSOCK.HSKT
jsr SKT.Socket.FindFree
bcs .9
ldy #FD.DSOCK.SIZE-1
.1 lda FD.DSOCK,y
sta (ZPTmpPtr1),y
dey
bpl .1
txa
clc
.9 rts
SKT.Socket.STREAM
lda #S.IP.PROTOCOL.TCP
ldy #S.SOCKET.TCP
stx FD.SSOCK+S.SOCKET.T save type
sta FD.SSOCK+S.SOCKET.PROTO ICMP,UDP,TCP
lda #0 Y = socket.size
>SYSCALL GetMem0
bcs .9
>STYA ZPTmpPtr1
stx FD.SSOCK+S.FD.SSOCK.HSKT
jsr SKT.Socket.FindFree
bcs .9
ldy #FD.SSOCK.SIZE-1
.1 lda FD.SSOCK,y
sta (ZPTmpPtr1),y
dey
bpl .1
txa
clc
.9 rts
SKT.Socket.FindFree
ldy #0
.1 lda SKT.TABLE,y
beq .2
iny
cpy #K.SKTTABLE.SIZE
bne .1
txa
>SYSCALL freemem
lda #E.OOH
sec
rts
.2 txa x = hFD
sta SKT.TABLE,y
clc
rts
*/--------------------------------------
* # Bind
* bind a name to a socket
* ## C
* `int bind(hFD fd, const struct sockaddr *addr);`
* ## ASM
* **In:**
* `>PUSHW addr`
* `lda fd`
* `>LIBCALL hLIBTCPIP,LIBTCPIP.socket`
* ## RETURN VALUE
* CC: A = hSOCKET
* CS: A = EC
*\--------------------------------------
SKT.bind >SYSCALL GetMemPtr
>STYA ZPPtrSKT
jsr SKT.PullLocAddr
lda SKT.LOC.ADDR+S.SOCKADDR.PORT
bne .1
ldx SKT.LOC.ADDR+S.SOCKADDR.PORT+1
bne .1
jsr GetDynPort
sta SKT.LOC.ADDR+S.SOCKADDR.PORT
stx SKT.LOC.ADDR+S.SOCKADDR.PORT+1
.1 jsr SKT.FindMatchingLoc
bcc .9
jsr SKT.SetLocAddr
clc
rts
.9 lda #ERR.SKT.BUSY
sec
rts
*/--------------------------------------
* # Connect
* Iinitiate a connection on a socket
* ## C
* `int connect(hFD fd, const struct sockaddr *addr);`
* ## ASM
* **In:**
* `>PUSHW addr`
* `lda fd`
* `>LIBCALL hLIBTCPIP,LIBTCPIP.socket`
* ## RETURN VALUE
* CC: A = hSOCKET
* CS: A = EC
*\--------------------------------------
SKT.connect >SYSCALL GetMemPtr
>STYA ZPPtrSKT
jsr SKT.PullRemAddr
ldy #S.SOCKET.T
lda (ZPPtrSKT),y
tax
jmp (J.SKT.connect,x)
SKT.connect.RAW
SKT.connect.DGRAM
jsr SKT.SetRemAddr
clc
rts
SKT.connect.STREAM
>LDYAI K.TCP.WSIZE
>SYSCALL getmem
bcs SKT.connect.99
txa
ldy #S.SOCKET.SQ.hInMem
sta (ZPPtrSKT),y
>LDYAI K.TCP.WSIZE
>SYSCALL getmem
bcs SKT.connect.99
txa
ldy #S.SOCKET.SQ.hOutMem
sta (ZPPtrSKT),y
SKT.connect.SEQPACKET
ldy #S.SOCKET.TCP.S
lda (ZPPtrSKT),y
bne SKT.connect.9
jsr SKT.SetRemAddr
lda #K.TCP.WSIZE
ldy #S.SOCKET.TCP.INFREE
sta (ZPPtrSKT),y
ldy #S.SOCKET.TCP.OUTFREE
sta (ZPPtrSKT),y
lda /K.TCP.WSIZE
ldy #S.SOCKET.TCP.INFREE
sta (ZPPtrSKT),y
ldy #S.SOCKET.TCP.OUTFREE
sta (ZPPtrSKT),y
ldy #S.SOCKET.TCP.OUTNEXTSEQ
.2 lda A2osX.TIMER16-S.SOCKET.TCP.OUTNEXTSEQ,y
sta (ZPPtrSKT),y
iny
cpy #S.SOCKET.TCP.OUTNEXTSEQ+4
bne .2
lda #S.TCP.OPTIONS.SYN
jsr TCP.OUT.SendOptA Send SYN
bcs .9
ldy #S.SOCKET.TCP.S
lda #S.SOCKET.TCP.S.SYNSENT
sta (ZPPtrSKT),y
clc
.9 rts
SKT.connect.9 lda #ERR.SKT.BAD
sec
SKT.connect.99 rts
*/--------------------------------------
* # Listen
* Listen for connections on a socket
* ## C
* `int listen(hFD fd);`
* ## ASM
* **In:**
* `lda fd`
* `>LIBCALL hLIBTCPIP,LIBTCPIP.listen`
* ## RETURN VALUE
* CS: A = EC
*\--------------------------------------
SKT.listen >SYSCALL GetMemPtr
>STYA ZPPtrSKT
ldy #S.SOCKET.T
lda (ZPPtrSKT),y
cmp #S.SOCKET.T.SEQPACKET+1
bcc .9
ldy #S.SOCKET.TCP.S
lda (ZPPtrSKT),y
bne .9
lda #S.SOCKET.TCP.S.LISTEN
sta (ZPPtrSKT),y
ldy #S.SOCKET.O
lda #S.SOCKET.O.ACCEPTCONN
sta (ZPPtrSKT),y
clc
rts
.9 lda #ERR.SKT.BAD
sec
rts
*/--------------------------------------
* # Accept
* Accept a connection on a socket
* ## C
* `hFD Accept(hFD fd);`
* ## ASM
* **In:**
* `lda fd`
* `>LIBCALL hLIBTCPIP,LIBTCPIP.accept`
* ## RETURN VALUE
* A = hSocket
*\--------------------------------------
SKT.Accept >SYSCALL GetMemPtr
>STYA ZPPtrSKT
ldy #S.SOCKET.O
lda (ZPPtrSKT),y
and #S.SOCKET.O.ACCEPTCONN
beq .99
ldy #S.SOCKET.HQ.TAIL
lda (ZPPtrSKT),y
ldy #S.SOCKET.HQ.HEAD
cmp (ZPPtrSKT),y Queue Empty
beq .9 CS
pha save tail...
inc
cmp #S.SOCKET.HQ.MAX
bne .1
lda #0
.1 ldy #S.SOCKET.HQ.TAIL
sta (ZPPtrSKT),y
pla
clc
adc #S.SOCKET.HQ
tay
lda (ZPPtrSKT),y
clc
rts
.99 lda #ERR.SKT.BAD
sec
.9 rts
*/--------------------------------------
* # Shutdown
* Close socket
* ## C
* `int shutdown(int fd);`
* ## ASM
* **In:**
* `lda fd`
* `>LIBCALL hLIBTCPIP,LIBTCPIP.shutdown`
* ## RETURN VALUE
*\--------------------------------------
SKT.shutdown >SYSCALL GetMemPtr
>STYA ZPPtrSKT
cmp #S.SOCKET.T.STREAM
beq .10 TCP
cmp #S.SOCKET.T.RAW+1
bcs .9 no #S.SOCKET.T.DGRAM, nor #S.SOCKET.T.RAW
.1 ldy #S.SOCKET.HQ.TAIL
lda (ZPPtrSKT),y
ldy #S.SOCKET.HQ.HEAD
cmp (ZPPtrSKT),y
beq .8 No frame in Queue, close..
pha save tail...
clc
adc #S.SOCKET.HQ
tax
pla
inc
cmp #S.SOCKET.HQ.MAX
beq .2
lda #0
.2 ldy #S.SOCKET.HQ.TAIL
sta (ZPPtrSKT),y
txa
tay
lda (ZPPtrSKT),y
>SYSCALL FreeMem
bra .1
.8 lda #0
sta (ZPPtrSKT)
clc
rts
.9 lda #ERR.SKT.BAD
sec
.99 rts
.10 ldy #S.SOCKET.O
lda (ZPPtrSKT),y
bit #S.SOCKET.O.ACCEPTCONN
bne * TO DO: Listening socket
ldy #S.SOCKET.TCP.S
lda (ZPPtrSKT),y
cmp #S.SOCKET.TCP.S.CLWAIT
bne .11
lda #S.SOCKET.TCP.S.LASTACK
bra .12
.11 lda #S.SOCKET.TCP.S.FINWT1
.12 sta (ZPPtrSKT),y
ldy #S.SOCKET.TCP.O
lda (ZPPtrSKT),y
ora #S.TCP.OPTIONS.FIN+S.TCP.OPTIONS.ACK
sta (ZPPtrSKT),y
jmp TCP.OUT Try to push FIN/ACK
*/--------------------------------------
* # Read (STREAM)
* ## C
* `int skt.read(hFD fd, void *buf, int count);`
* ## ASM
* **In:**
* `>PUSHWI count`
* `>PUSHW buf`
* `lda fd`
* `>LIBCALL hLIBTCPIP,LIBTCPIP.skt.read`
* ## RETURN VALUE
* CC: Y,A = bytes read
* CS: A = EC
*\--------------------------------------
SKT.Read >SYSCALL GetMemPtr
>STYA ZPPtrSKT
>PULLW ZPDataInPtr !!!DataOut trashed when sending ACK in TCP.OUT
>PULLW ZPDataInLen !!!use DataInPtr/Len
ldy #S.SOCKET.T
lda (ZPPtrSKT),y
cmp #S.SOCKET.T.STREAM
bne .90 bad skt
ldy #S.SOCKET.O
lda (ZPPtrSKT),y
and #S.SOCKET.O.ACCEPTCONN
bne .90 Bad Skt
ldy #S.SOCKET.TCP.S
lda (ZPPtrSKT),y
cmp #S.SOCKET.TCP.S.ESTBLSH
bcc .92 Not yet established...no data
cmp #S.SOCKET.TCP.S.FINWT2
bcs .91 closing...IO err
jsr SKT.GetDataFromSktIn
rts if CS, No data...
.90 lda #ERR.SKT.BAD
sec
rts
.91 lda #MLI.E.IO
sec
rts
.92 lda #0
sec
.99 rts
*/--------------------------------------
* # Write (STREAM)
* ## C
* `int skt.write(hFD fd, const void *buf, int count);`
* ## ASM
* **In:**
* `>PUSHWI count`
* `>PUSHW buf`
* `lda fd`
* `>LIBCALL hLIBTCPIP,LIBTCPIP.skt.write`
* ## RETURN VALUE
* CC: Y,A = bytes written
* CS: A = EC
*\--------------------------------------
SKT.Write >SYSCALL GetMemPtr
>STYA ZPPtrSKT
>PULLW ZPDataInPtr
>PULLW ZPDataInLen
ldy #S.SOCKET.T
lda (ZPPtrSKT),y
cmp #S.SOCKET.T.STREAM
bne .99
ldy #S.SOCKET.O
lda (ZPPtrSKT),y
and #S.SOCKET.O.ACCEPTCONN
bne .99
ldy #S.SOCKET.TCP.S
lda (ZPPtrSKT),y
cmp #S.SOCKET.TCP.S.ESTBLSH
bne .91
jsr SKT.AddDataToSktOut will call SKT.GetTCB
bcs .90
jsr TCP.OUT.I SKT.GetTCB called, try to push, ignore error....
clc
rts
.90 lda #0 no data transfered
sec
rts
.91 lda #MLI.E.IO
sec
rts
.99 lda #ERR.SKT.BAD
sec
rts
*/--------------------------------------
* # Recv (RAW,DGRAM,SEQPACKET)
* # RecvFrom (RAW,DGRAM,SEQPACKET)
* ## C
* `hMem recv(hFD fd);`
* `hMem recvfrom(hFD fd, struct sockaddr *addr);`
* ## ASM
* **In:**
* `>PUSHW addr` (RecvFrom)
* `lda fd`
* `>LIBCALL hLIBTCPIP,LIBTCPIP.skt.Recv`
* ## RETURN VALUE
* CC: A = hMem
* CS: A = EC
*\--------------------------------------
SKT.RecvFrom sec
.HS 90 BCC
SKT.Recv clc
php
>SYSCALL GetMemPtr
>STYA ZPPtrSKT
ldy #S.SOCKET.T
lda (ZPPtrSKT),y
cmp #S.SOCKET.T.STREAM
beq .99
ldy #S.SOCKET.HQ.TAIL
lda (ZPPtrSKT),y
tax
ldy #S.SOCKET.HQ.HEAD
cmp (ZPPtrSKT),y
beq .90
inc
cmp #S.SOCKET.HQ.MAX
bne .1
lda #0
.1 ldy #S.SOCKET.HQ.TAIL
sta (ZPPtrSKT),y
txa
clc
adc #S.SOCKET.HQ
tay
lda (ZPPtrSKT),y
plp
bcc .9
pha
>SYSCALL GetMemPtr
>STYA ZPFrameInPtr
>PULLW ZPTmpPtr1 addr
ldy #S.IP.SRC
.2 lda (ZPFrameInPtr),y
sta SA.REMOTE+S.SOCKADDR.ADDR-S.IP.SRC,y
iny
cpy #S.IP.SRC+4
bne .2
ldy #S.IP.PROTOCOL
lda (ZPFrameInPtr),y
cmp #S.IP.PROTOCOL.ICMP
bne .3
ldy #S.ICMP.IDENTIFIER
bra .7
.3 cmp #S.IP.PROTOCOL.TCP
beq .6
cmp #S.IP.PROTOCOL.UDP
bne .8
.6 ldy #S.TCPUDP.SRCPORT
.7 lda (ZPFrameInPtr),y
sta SA.REMOTE+S.SOCKADDR+1
iny
lda (ZPFrameInPtr),y
sta SA.REMOTE+S.SOCKADDR
.8 lda #S.SOCKADDR-1
.80 lda SA.REMOTE,y
sta (ZPTmpPtr1),y
dey
bpl .80
pla
clc
.9 rts
.99 plp
lda #ERR.SKT.BAD
sec
rts
.90 plp
lda #0 no data
sec
rts
*/--------------------------------------
* # Send (RAW,DGRAM,SEQPACKET)
* # SendTo (RAW,DGRAM,SEQPACKET)
* ## C
* `int skt.send(hFD fd, const void *buf, int count);`
* `int skt.sendto(hFD fd, const void *buf, int count, const struct sockaddr *addr);`
* ## ASM
* **In:**
* `>PUSHW addr` (SendTo)
* `>PUSHWI count`
* `>PUSHW buf`
* `lda fd`
* `>LIBCALL hLIBTCPIP,LIBTCPIP.skt.send`
* ## RETURN VALUE
* CC: Y,A = bytes written
* CS: A = EC
*\--------------------------------------
SKT.SendTo sec
.HS 90 BCC
SKT.Send clc
php
>SYSCALL GetMemPtr
>STYA ZPPtrSKT
plp
bcc .1
jsr SKT.PullRemAddr
.1 >PULLW ZPDataInPtr
>PULLW ZPDataInLen
ldy #S.SOCKET.T
lda (ZPPtrSKT),y
tax
jmp (J.SKT.send,x)
*--------------------------------------
SKT.Send.RAW ldy #S.SOCKET.PROTO
lda (ZPPtrSKT),y
tax
>LDYA ZPDataInLen
jsr FRM.NewIP
bcs SKT.Send.9
bra SKT.Send.DGRAM.8
*--------------------------------------
SKT.Send.DGRAM ldx #S.IP.PROTOCOL.UDP
>LDYA ZPDataInLen
jsr FRM.NewIP
bcs SKT.Send.9
jsr SKT.SetFrameOutTCPUDPPorts
SKT.Send.DGRAM.8
jsr SKT.SetFrameOutDstIP
jsr SKT.CopyDataInToOut
jmp FRM.SendIP
*--------------------------------------
SKT.Send.STREAM lda #ERR.SKT.BAD
sec
SKT.Send.9 rts
*--------------------------------------
SKT.Send.SEQPACKET
bra *
*/--------------------------------------
* # GetTable
* Get socket table
* ## C
* `void * gettable();`
* ## ASM
* **In:**
* `>LIBCALL hLIBTCPIP,LIBTCPIP.skt.GetTable`
* ## RETURN VALUE
*\--------------------------------------
SKT.GetTable >LDYA L.SKT.Table
clc
rts
*--------------------------------------
* PRIVATE
*--------------------------------------
SKT.PullLocAddr >PULLW ZPTmpPtr1
ldy #S.SOCKADDR-1
.1 lda (ZPTmpPtr1),y
sta SKT.LOC.ADDR,y
dey
bpl .1
rts
*--------------------------------------
SKT.PullRemAddr >PULLW ZPTmpPtr1
ldy #S.SOCKADDR-1
.1 lda (ZPTmpPtr1),y
sta SKT.REM.ADDR,y
dey
bpl .1
rts
*--------------------------------------
SKT.SetLocAddr ldy #S.SOCKET.LOC.ADDR
.1 lda SKT.LOC.ADDR+S.SOCKADDR.ADDR-S.SOCKET.LOC.ADDR,y
sta (ZPPtrSKT),y
iny
cpy #S.SOCKET.LOC.PORT+2
bne .1
rts
*--------------------------------------
SKT.SetRemAddr ldy #S.SOCKET.REM.ADDR
.1 lda SKT.REM.ADDR+S.SOCKADDR.ADDR-S.SOCKET.REM.ADDR,y
sta (ZPPtrSKT),y
iny
cpy #S.SOCKET.REM.PORT+2
bne .1
rts
*--------------------------------------
SKT.FindMatchingLocRem
ldx #0
.1 lda SKT.TABLE,x
beq .8
phx
>SYSCALL GetMemPtr
>STYA ZPTmpPtr1
plx
ldy #0
.2 lda (ZPTmpPtr1),y
bne .4
ldy #S.SOCKET.LOC.PORT This Socket is bound to 0.0.0.0,check only LOC port
.3 lda (ZPTmpPtr1),y
.4 cmp SKT.LOC.ADDR+S.SOCKADDR.ADDR-S.SOCKET.LOC.ADDR,y
bne .8
iny
cpy #S.SOCKET.REM.ADDR
bne .3
lda SKT.REM.ADDR+S.SOCKADDR.ADDR
cmp #$ff FF.FF.FF.FF, Broadcast ?
bne .5
ldy #S.SOCKET.O
lda (ZPTmpPtr1),y
and #S.SOCKET.O.BROADCAST
beq .9 this socket does not accept broadcast
ldy #S.SOCKET.REM.PORT Boradcast, check port only
.5 lda (ZPTmpPtr1),y
cmp SKT.REM.ADDR+S.SOCKADDR.ADDR-S.SOCKET.REM.ADDR,y
bne .9 wrong remote host, exit....
iny
cpy #S.SOCKET.REM.PORT+2
bne .5
>LDYA ZPTmpPtr1 x = SKT.TABLE index
clc
rts
.8 inx
cpx #K.SKTTABLE.SIZE
bne .1
.9 sec
rts
*--------------------------------------
SKT.FindMatchingLoc
ldy #S.SOCKET.LOC.PORT+1
phy
ldx #0
.1 lda SKT.TABLE,x
beq .7
phx
>SYSCALL GetMemPtr
>STYA ZPTmpPtr1
plx
ply
phy
.2 lda (ZPTmpPtr1),y
cmp SKT.LOC.ADDR+S.SOCKADDR.ADDR-S.SOCKET.LOC.ADDR,y
bne .7
dey
cpy #S.SOCKET.LOC.ADDR-1
bne .2
ply
>LDYA ZPTmpPtr1 x = SKT.TABLE index
clc
rts
.7 inx
cpx #K.SKTTABLE.SIZE
bne .1
ply
* sec
.9 rts
*--------------------------------------
* ZPPtrSKT -> actual socket
* A = hSocket or hFrame
*--------------------------------------
SKT.AddToQueueA sta .2+1
ldy #S.SOCKET.HQ.HEAD
lda (ZPPtrSKT),y
tax
inc
cmp #S.SOCKET.HQ.MAX
bne .1
lda #0
.1 ldy #S.SOCKET.HQ.TAIL
cmp (ZPPtrSKT),y
beq .9 Queue full!!
ldy #S.SOCKET.HQ.HEAD
sta (ZPPtrSKT),y
txa
clc
adc #S.SOCKET.HQ
tay
.2 lda #$ff
sta (ZPPtrSKT),y
clc
rts
.9 sec
rts
*--------------------------------------
* From TCP.IN
* Src : ZPDataInPtr/ZPDataInLen
* Dst : hInMem/INHEAD
*--------------------------------------
SKT.AddDataToSktIn
ldx SKT.TmpCache+S.SOCKET.TCP.INFREE
cpx ZPDataInLen
lda SKT.TmpCache+S.SOCKET.TCP.INFREE+1
sbc ZPDataInLen+1
bcc .9 Not enough room in Q
ldy #S.TCP.SEQNUM+3
ldx #3
.10 lda (ZPFrameInPtr),y
eor SKT.TmpCache+S.SOCKET.TCP.INLASTSEQNUM,x
bne .9
dey
dex
bpl .10
ldy #S.SOCKET.SQ.hInMem
lda (ZPPtrSKT),y
>SYSCALL GetMemPtr
>STYA ZPTmpPtr1 Make ZPTmpPtr1=buffer base
lda ZPTmpPtr1
clc
adc SKT.TmpCache+S.SOCKET.TCP.INHEAD
sta ZPTmpPtr2
lda ZPTmpPtr1+1
adc SKT.TmpCache+S.SOCKET.TCP.INHEAD+1
sta ZPTmpPtr2+1 Make ZPTmpPtr2=base+Tail
lda ZPDataInLen
eor #$FF
tax
lda ZPDataInLen+1
eor #$FF
pha
ldy #0
.1 inx
bne .2
pla
inc
beq .8
pha
.2 lda (ZPDataInPtr),y
sta (ZPTmpPtr2)
iny
bne .3
inc ZPDataInPtr+1
.3 inc ZPTmpPtr2
bne .4
inc ZPTmpPtr2+1
.4 inc SKT.TmpCache+S.SOCKET.TCP.INHEAD
bne .1
inc SKT.TmpCache+S.SOCKET.TCP.INHEAD+1
lda SKT.TmpCache+S.SOCKET.TCP.INHEAD+1
cmp /K.TCP.WSIZE
bne .1
stz SKT.TmpCache+S.SOCKET.TCP.INHEAD+1
lda ZPTmpPtr1 Dst Ptr=Buffer Base
sta ZPTmpPtr2
lda ZPTmpPtr1+1
sta ZPTmpPtr2+1
bra .1
.9 sec
rts
.8 lda SKT.TmpCache+S.SOCKET.TCP.INFREE
sec
sbc ZPDataInLen
sta SKT.TmpCache+S.SOCKET.TCP.INFREE
lda SKT.TmpCache+S.SOCKET.TCP.INFREE+1
sbc ZPDataInLen+1
sta SKT.TmpCache+S.SOCKET.TCP.INFREE+1
lda SKT.TmpCache+S.SOCKET.TCP.INUSED
clc
adc ZPDataInLen
sta SKT.TmpCache+S.SOCKET.TCP.INUSED
lda SKT.TmpCache+S.SOCKET.TCP.INUSED+1
adc ZPDataInLen+1
sta SKT.TmpCache+S.SOCKET.TCP.INUSED+1
clc
lda SKT.TmpCache+S.SOCKET.TCP.INLASTSEQNUM+3
adc ZPDataInLen
sta SKT.TmpCache+S.SOCKET.TCP.INLASTSEQNUM+3
lda SKT.TmpCache+S.SOCKET.TCP.INLASTSEQNUM+2
adc ZPDataInLen+1
sta SKT.TmpCache+S.SOCKET.TCP.INLASTSEQNUM+2
bcc .80
inc SKT.TmpCache+S.SOCKET.TCP.INLASTSEQNUM+1
bne .80
inc SKT.TmpCache+S.SOCKET.TCP.INLASTSEQNUM
.80 clc
rts
*--------------------------------------
* From SKT.Read
* In:
* Src : hInMem/INTAIL
* Dst : ZPDataOutPtr/ZPDataOutLen
* Out:
* Y,A = bytes read
*--------------------------------------
SKT.GetDataFromSktIn
jsr SKT.GetTCB
lda SKT.TmpCache+S.SOCKET.TCP.INUSED
ora SKT.TmpCache+S.SOCKET.TCP.INUSED+1
bne .10
sec
rts A=0,NO DATA
.10 ldy #S.SOCKET.SQ.hInMem
lda (ZPPtrSKT),y
>SYSCALL GetMemPtr
>STYA ZPTmpPtr1 Make ZPTmpPtr1=buffer base
lda ZPTmpPtr1
clc
adc SKT.TmpCache+S.SOCKET.TCP.INTAIL
sta ZPTmpPtr2
lda ZPTmpPtr1+1
adc SKT.TmpCache+S.SOCKET.TCP.INTAIL+1
sta ZPTmpPtr2+1
lda ZPDataInLen
eor #$FF
tax
lda ZPDataInLen+1
eor #$FF
pha
stz ZPTmpPtr3 Reset byte counter
stz ZPTmpPtr3+1
ldy #0
.1 inx Check if room left in dest buffer
bne .2
pla
inc
beq .8
pha
.2 lda SKT.TmpCache+S.SOCKET.TCP.INTAIL
eor SKT.TmpCache+S.SOCKET.TCP.INHEAD
bne .3
lda SKT.TmpCache+S.SOCKET.TCP.INTAIL+1
eor SKT.TmpCache+S.SOCKET.TCP.INHEAD+1
beq .7 no more data....
.3 lda (ZPTmpPtr2)
pha
inc SKT.TmpCache+S.SOCKET.TCP.INTAIL
bne .4
inc SKT.TmpCache+S.SOCKET.TCP.INTAIL+1
lda SKT.TmpCache+S.SOCKET.TCP.INTAIL+1
cmp /K.TCP.WSIZE
bne .4
stz SKT.TmpCache+S.SOCKET.TCP.INTAIL+1
lda ZPTmpPtr1
sta ZPTmpPtr2
lda ZPTmpPtr1+1
sta ZPTmpPtr2+1
bra .5
.4 inc ZPTmpPtr2
bne .5
inc ZPTmpPtr2+1
.5 pla get back data....
.6 sta (ZPDataInPtr),y
iny
bne .61
inc ZPDataInPtr+1
.61 inc ZPTmpPtr3
bne .1
inc ZPTmpPtr3+1
bra .1
.7 pla discard byte counter HI
.8 lda ZPTmpPtr3 Do we have transfered something?
ora ZPTmpPtr3+1
beq .9
lda SKT.TmpCache+S.SOCKET.TCP.INFREE
clc
adc ZPTmpPtr3
sta SKT.TmpCache+S.SOCKET.TCP.INFREE
lda SKT.TmpCache+S.SOCKET.TCP.INFREE+1
adc ZPTmpPtr3+1
sta SKT.TmpCache+S.SOCKET.TCP.INFREE+1
lda SKT.TmpCache+S.SOCKET.TCP.INUSED
sec
sbc ZPTmpPtr3
sta SKT.TmpCache+S.SOCKET.TCP.INUSED
lda SKT.TmpCache+S.SOCKET.TCP.INUSED+1
sbc ZPTmpPtr3+1
sta SKT.TmpCache+S.SOCKET.TCP.INUSED+1
lda SKT.TmpCache+S.SOCKET.TCP.OUTACKNUM+3
clc
adc ZPTmpPtr3
sta SKT.TmpCache+S.SOCKET.TCP.OUTACKNUM+3
lda SKT.TmpCache+S.SOCKET.TCP.OUTACKNUM+2
adc ZPTmpPtr3+1
sta SKT.TmpCache+S.SOCKET.TCP.OUTACKNUM+2
bcc .80
inc SKT.TmpCache+S.SOCKET.TCP.OUTACKNUM+1
bne .80
inc SKT.TmpCache+S.SOCKET.TCP.OUTACKNUM
.80 jsr SKT.StoreTCB
ldy #S.SOCKET.TCP.O
lda (ZPPtrSKT),y
ora #S.TCP.OPTIONS.ACK
sta (ZPPtrSKT),y
jsr TCP.OUT.I SKT.GetTCB already called, Try to ACK read DATA
>LDYA ZPTmpPtr3
clc
rts
.9
* lda #0 NO DATA
sec
rts
*--------------------------------------
* From SKT.Write.TCP
* Src : ZPDataInPtr/ZPDataInLen
* Dst : hOutMem/OUTHEAD
*--------------------------------------
SKT.AddDataToSktOut
jsr SKT.GetTCB
ldx SKT.TmpCache+S.SOCKET.TCP.OUTFREE
cpx ZPDataInLen
lda SKT.TmpCache+S.SOCKET.TCP.OUTFREE+1
sbc ZPDataInLen+1
bcc .9 Not enough room in Q
ldy #S.SOCKET.SQ.hOutMem
lda (ZPPtrSKT),y
>SYSCALL GetMemPtr
>STYA ZPTmpPtr1 Make ZPTmpPtr1=buffer base
lda ZPTmpPtr1
clc
adc SKT.TmpCache+S.SOCKET.TCP.OUTHEAD
sta ZPTmpPtr2
lda ZPTmpPtr1+1
adc SKT.TmpCache+S.SOCKET.TCP.OUTHEAD+1
sta ZPTmpPtr2+1 Make ZPTmpPtr2=base+Tail
lda ZPDataInLen
eor #$FF
tax
lda ZPDataInLen+1
eor #$FF
pha
ldy #0
.1 inx
bne .2
pla
inc
beq .8
pha
.2 lda (ZPDataInPtr),y
sta (ZPTmpPtr2)
iny
bne .3
inc ZPDataInPtr+1
.3 inc ZPTmpPtr2
bne .4
inc ZPTmpPtr2+1
.4 inc SKT.TmpCache+S.SOCKET.TCP.OUTHEAD
bne .1
inc SKT.TmpCache+S.SOCKET.TCP.OUTHEAD+1
lda SKT.TmpCache+S.SOCKET.TCP.OUTHEAD+1
cmp /K.TCP.WSIZE
bne .1
stz SKT.TmpCache+S.SOCKET.TCP.OUTHEAD+1
lda ZPTmpPtr1 Dst Ptr=Buffer Base
sta ZPTmpPtr2
lda ZPTmpPtr1+1
sta ZPTmpPtr2+1
bra .1
.9 sec
rts
.8 lda SKT.TmpCache+S.SOCKET.TCP.OUTFREE
sec
sbc ZPDataInLen
sta SKT.TmpCache+S.SOCKET.TCP.OUTFREE
lda SKT.TmpCache+S.SOCKET.TCP.OUTFREE+1
sbc ZPDataInLen+1
sta SKT.TmpCache+S.SOCKET.TCP.OUTFREE+1
lda SKT.TmpCache+S.SOCKET.TCP.OUTUSED
clc
adc ZPDataInLen
sta SKT.TmpCache+S.SOCKET.TCP.OUTUSED
lda SKT.TmpCache+S.SOCKET.TCP.OUTUSED+1
adc ZPDataInLen+1
sta SKT.TmpCache+S.SOCKET.TCP.OUTUSED+1
lda SKT.TmpCache+S.SOCKET.TCP.OUTTOSEND
clc
adc ZPDataInLen
sta SKT.TmpCache+S.SOCKET.TCP.OUTTOSEND
lda SKT.TmpCache+S.SOCKET.TCP.OUTTOSEND+1
adc ZPDataInLen+1
sta SKT.TmpCache+S.SOCKET.TCP.OUTTOSEND+1
jsr SKT.StoreTCB
rts
*--------------------------------------
* From TCP.OUT
* Src : hOutMem/OUTTAIL
* Dst : ZPDataOutPtr/ZPDataOutLen
*--------------------------------------
SKT.GetDataFromSktOut
ldy #S.SOCKET.SQ.hOutMem
lda (ZPPtrSKT),y
>SYSCALL GetMemPtr
>STYA ZPTmpPtr1 Make ZPTmpPtr1=buffer base
lda ZPTmpPtr1
clc
adc SKT.TmpCache+S.SOCKET.TCP.OUTTAILNEXT
sta ZPTmpPtr2 Make ZPTmpPtr2=base+nexttail
lda ZPTmpPtr1+1
adc SKT.TmpCache+S.SOCKET.TCP.OUTTAILNEXT+1
sta ZPTmpPtr2+1
lda ZPDataOutLen
eor #$FF
tax
lda ZPDataOutLen+1
eor #$FF
pha
ldy #0
.1 inx
bne .2
pla
inc
beq .8
pha
.2 lda (ZPTmpPtr2)
sta (ZPDataOutPtr),y
iny
bne .3
inc ZPDataOutPtr+1
.3 inc ZPTmpPtr2
bne .4
inc ZPTmpPtr2+1
.4 inc SKT.TmpCache+S.SOCKET.TCP.OUTTAILNEXT
bne .1
inc SKT.TmpCache+S.SOCKET.TCP.OUTTAILNEXT+1
lda SKT.TmpCache+S.SOCKET.TCP.OUTTAILNEXT+1
cmp /K.TCP.WSIZE
bne .1
stz SKT.TmpCache+S.SOCKET.TCP.OUTTAILNEXT+1
lda ZPTmpPtr1 Dst Ptr=Buffer Base
sta ZPTmpPtr2
lda ZPTmpPtr1+1
sta ZPTmpPtr2+1
bra .1
.8 lda SKT.TmpCache+S.SOCKET.TCP.OUTTOSEND
sec
sbc ZPDataOutLen
sta SKT.TmpCache+S.SOCKET.TCP.OUTTOSEND
lda SKT.TmpCache+S.SOCKET.TCP.OUTTOSEND+1
sbc ZPDataOutLen+1
sta SKT.TmpCache+S.SOCKET.TCP.OUTTOSEND+1
clc
rts
*--------------------------------------
* From TCP.IN
* Src : A,X
* Dst : hOutMem/OUTTAIL
*--------------------------------------
SKT.AckDataToSktOut
ldy #S.TCP.ACKNUM+3 Get new ACK number from FrameIn...
ldx #3 Substract old ACK from socket
sec
.1 lda (ZPFrameInPtr),y
pha
sbc SKT.TmpCache+S.SOCKET.TCP.OUTSEQNUM,x
sta TmpDWord,x
pla ....and Set SEQ = new SEQ
sta SKT.TmpCache+S.SOCKET.TCP.OUTSEQNUM,x
dey
dex
bpl .1
lda TmpDWord+3
ldx TmpDWord+2
bne .2
tay
beq .8
.2 clc
adc SKT.TmpCache+S.SOCKET.TCP.OUTTAIL
sta SKT.TmpCache+S.SOCKET.TCP.OUTTAIL
txa
adc SKT.TmpCache+S.SOCKET.TCP.OUTTAIL+1
and /K.TCP.WSIZE-1
sta SKT.TmpCache+S.SOCKET.TCP.OUTTAIL+1
lda SKT.TmpCache+S.SOCKET.TCP.OUTFREE
clc
adc TmpDWord+3
sta SKT.TmpCache+S.SOCKET.TCP.OUTFREE
lda SKT.TmpCache+S.SOCKET.TCP.OUTFREE+1
adc TmpDWord+2
sta SKT.TmpCache+S.SOCKET.TCP.OUTFREE+1
lda SKT.TmpCache+S.SOCKET.TCP.OUTUSED
sec
sbc TmpDWord+3
sta SKT.TmpCache+S.SOCKET.TCP.OUTUSED
lda SKT.TmpCache+S.SOCKET.TCP.OUTUSED+1
sbc TmpDWord+2
sta SKT.TmpCache+S.SOCKET.TCP.OUTUSED+1
.8 rts
*--------------------------------------
SKT.GetTCB ldy #S.SOCKET.TCP-S.SOCKET
.1 dey
lda (ZPPtrSKT),y
sta SKT.TmpCache,y
cpy #S.SOCKET
bne .1
rts
*--------------------------------------
SKT.StoreTCB ldy #S.SOCKET.TCP-S.SOCKET
.1 dey
lda SKT.TmpCache,y
sta (ZPPtrSKT),y
cpy #S.SOCKET
bne .1
rts
*--------------------------------------
SKT.Destroy ldy #S.SOCKET.SQ.hOutMem
lda (ZPPtrSKT),y
beq .1
>SYSCALL FreeMem
.1 ldy #S.SOCKET.SQ.hInMem
lda (ZPPtrSKT),y
beq .2
>SYSCALL FreeMem
.2 lda #0
sta (ZPPtrSKT)
clc
rts
*--------------------------------------
SKT.SetFrameOutDstIP
ldy #S.SOCKET.REM.ADDR
ldx #4
.1 lda (ZPPtrSKT),y
pha
iny
dex
bne .1
ldy #S.IP.DST+3
ldx #4
.2 pla
sta (ZPFrameOutPtr),y
dey
dex
bne .2
rts
*--------------------------------------
SKT.SetFrameOutTCPUDPPorts
ldy #S.SOCKET.LOC.PORT
lda (ZPPtrSKT),y
tax
iny
lda (ZPPtrSKT),y
ldy #S.TCPUDP.SRCPORT
sta (ZPFrameOutPtr),y
iny
txa
sta (ZPFrameOutPtr),y
ldy #S.SOCKET.REM.PORT
lda (ZPPtrSKT),y
tax
iny
lda (ZPPtrSKT),y
ldy #S.TCPUDP.DSTPORT
sta (ZPFrameOutPtr),y
iny
txa
sta (ZPFrameOutPtr),y
rts
*--------------------------------------
SKT.CopyDataInToOut
lda ZPDataInPtr+1
pha
lda ZPDataOutPtr+1
pha
lda ZPDataInLen
eor #$ff
tax
lda ZPDataInLen+1
eor #$ff
pha
ldy #0
.1 inx
bne .2
pla
inc
beq .9
pha
.2 lda (ZPDataInPtr),y
sta (ZPDataOutPtr),y
iny
bne .1
inc ZPDataInPtr+1
inc ZPDataOutPtr+1
bne .1
.9 pla
sta ZPDataOutPtr+1
pla
sta ZPDataInPtr+1
rts
*--------------------------------------
MAN
SAVE /A2OSX.SRC/LIB/LIBTCPIP.S.SKT
LOAD /A2OSX.SRC/LIB/LIBTCPIP.S
ASM