PR#3
PREFIX /A2OSX.SRC
NEW
INC 1
AUTO 6
				.LIST OFF	
*--------------------------------------
* IN : PULLW = PTR to S.SOCKET template
* OUT : 
*	YA=PTR to new S.SOCKET
*   X hSocket	128<x<255
*--------------------------------------
SKT.NEW			>PULLW ZPTmpPtr1

				ldy #S.SOCKET.SRC.PORT+1
				lda (ZPTmpPtr1),y
				dey
				ora (ZPTmpPtr1),y
				bne .1
				
				jsr GetDynPort
				sta (ZPTmpPtr1),y
				txa
				iny
				sta (ZPTmpPtr1),y
				
.1				>LDYA L.SKT.TABLE
				>STYA ZPTmpPtr2

				stz TmpOffset		to keep track of any free slot
				
				ldx #0
				
.2				lda (ZPTmpPtr2)
				beq .4
				cmp (ZPTmpPtr1)
				bne .5
				
				ldy #S.SOCKET.SRC.ADDR
				
.3				lda (ZPTmpPtr1),y
				cmp (ZPTmpPtr2),y
				bne .5
				iny
				cpy #S.SOCKET.DST.PORT+2
				bne .3

				lda #ERR.SKT.BUSY
				sec
				rts
				
.4				bit TmpOffset
				bmi .5					Already found an empty slot
				>LDYA ZPTmpPtr2
				>STYA ZPTmpPtr3
				txa
				ora #$80
				sta TmpOffset
				
.5				lda ZPTmpPtr2
				clc
				adc #S.SOCKET
				sta ZPTmpPtr2
				bcc .6
				inc ZPTmpPtr2+1
.6				inx
				cpx #K.SKTTABLE.SIZE
				bne .2
				
				bit TmpOffset			Did we found an empty slot ?
				bpl .9
				
				ldy #S.SOCKET-1
.7				lda (ZPTmpPtr1),y
				sta (ZPTmpPtr3),y
				dey
				bpl .7
				
				ldx TmpOffset
				>LDYA ZPTmpPtr3
				clc
				rts
				
.9				lda #ERR.SKT.OOS
				sec
				rts
*--------------------------------------
SKT.ACCEPTA		jsr SKT.GetA
				bcs .9
				ldy #S.SOCKET.SO
				lda (ZPPtrSKT),y
				and #S.SOCKET.SO.ACCEPTCONN
				bne .99
				
				ldy #S.SOCKET.LQ.HEAD
				lda (ZPPtrSKT),y
				ldy #S.SOCKET.LQ.TAIL
				cmp (ZPPtrSKT),y		Queue Empty
				beq .9					CC
				pha
				inc
				cmp #S.SOCKET.LQ.MAX
				bne .1
				lda #0
.1				sta (ZPPtrSKT),y
				pla
				clc
				adc #S.SOCKET.LQ
				tay
				lda (ZPPtrSKT),y
				clc
				rts
				
.99				lda #ERR.SKT.BAD
				sec
.9				rts
*--------------------------------------
SKT.SENDA
				sec
				rts
*--------------------------------------
SKT.SEND		>PULLB hSocket
				pha
				>PULLW ZPDataPtr
				>PULLW ZPDataLen
				
				pla
				jsr SKT.GetA
				bcs .9
			
				cpx #S.SOCKET.SOCK.DGRAM
				beq SKT.SEND.UDP
				
				cpx #S.SOCKET.SOCK.STREAM
				bne .99
				jmp SKT.SEND.TCP
				
.99				sec
.9				rts
*--------------------------------------
SKT.SEND.UDP	jsr NEW.UDP.FRAME
				bcs .9
				stx hFrame1
				
				lda #S.UDP-2
				clc
				adc ZPDataLen
				sta (ZPFrameBase1)
				sta ZPFrameLen1
				
				lda /S.UDP-2
				adc ZPDataLen+1
				ldy #1
				sta (ZPFrameBase1),y
				sta ZPFrameLen1+1				
				
				ldy #S.SOCKET.SRC.PORT
				lda (ZPPtrSKT),y
				tax
				iny
				lda (ZPPtrSKT),y
 				ldy #S.UDP.SRCPORT
				sta (ZPFrameBase1),y
				iny
				txa
				sta (ZPFrameBase1),y
				
				ldy #S.SOCKET.DST.ADDR
				ldx #4
				
.1				lda (ZPPtrSKT),y
				pha
				iny
				dex
				bne .1
				
				ldy #S.IP.DST+3
				ldx #4
				
.2				pla
				sta (ZPFrameBase1),y
				dey
				dex
				bne .2
				
				ldy #S.SOCKET.DST.PORT
				lda (ZPPtrSKT),y
				tax
				iny
				lda (ZPPtrSKT),y
 				ldy #S.UDP.DSTPORT
				sta (ZPFrameBase1),y
				iny
				txa
				sta (ZPFrameBase1),y
				
				lda #S.UDP
				clc
				adc ZPFrameBase1
				sta ZPTmpPtr1
				lda /S.UDP
				adc ZPFrameBase1+1
				sta ZPTmpPtr1+1
				
				ldy #0
.3				jsr DecDataLen
				beq .4
				lda (ZPDataPtr),y
				sta (ZPTmpPtr1),y
				iny
				bne .3
				inc ZPDataPtr
				inc ZPTmpPtr1
				bra .3
				
.4				jmp SEND.UDP.FRAME.I
.9				rts
*--------------------------------------
SKT.SEND.TCP
				bra *
				sec
				rts
*--------------------------------------
SKT.RCVDA		jsr SKT.GetA
				bcs .9
				
				ldy #S.SOCKET.DQ.TAIL
				lda (ZPPtrSKT),y
				tax
				
				ldy #S.SOCKET.DQ.HEAD
				cmp (ZPPtrSKT),y
				beq .99
				
				inc
				cmp #S.SOCKET.DQ.MAX
				bne .1
				lda #0
				
.1				ldy #S.SOCKET.DQ.TAIL
				sta	(ZPPtrSKT),y
				
				txa
				clc
				adc #S.SOCKET.DQ
				tay
				lda (ZPPtrSKT),y
				clc
				rts
				
.99				sec
.9				rts
*--------------------------------------
SKT.CLOSEA		jsr SKT.GetA
				bcs .9
				cpx #S.SOCKET.SOCK.DGRAM
				
				beq .8

				ldy #S.SOCKET.SO
				lda (ZPPtrSKT),y
				bit #S.SOCKET.SO.ACCEPTCONN
				
				beq *
				
				
.8				lda #0
				sta (ZPPtrSKT)
				clc
.9				rts
*--------------------------------------
SKT.GETTABLE	>LDYA L.SKT.TABLE
				clc
				rts
*--------------------------------------
SKT.GetA		stz ZPPtrSKT
				
				and #$7f				Strip off msb
				lsr
				ror ZPPtrSKT
				lsr
				ror ZPPtrSKT
				lsr
				ror ZPPtrSKT
				pha
				
				lda ZPPtrSKT
				adc L.SKT.TABLE
				sta ZPPtrSKT
				pla
				adc L.SKT.TABLE+1
				sta ZPPtrSKT+1
				
				lda (ZPPtrSKT)
				beq .9
				tax
				>LDYA ZPPtrSKT
				rts						CC
				
.9				lda #ERR.SKT.BAD
				sec
				rts
*--------------------------------------
* ZPPtrSKT -> actual socket
* A = hSocket
*--------------------------------------
SKT.AddToQueueL	pha
				ldy #S.SOCKET.LQ.HEAD
				lda (ZPPtrSKT),y
				tax
				inc 
				cmp #S.SOCKET.LQ.MAX
				bne .1
				lda #0
.1				ldy #S.SOCKET.LQ.TAIL
				cmp (ZPPtrSKT),y
				beq .9					Queue full!!
				
				ldy #S.SOCKET.LQ.HEAD
				sta (ZPPtrSKT),y
				txa
				clc
				adc #S.SOCKET.LQ
				tay
				pla
				sta (ZPPtrSKT),y
			
				clc
				rts
				
.9				pla
				sec
				rts
*--------------------------------------
SKT.AddToQueueD	ldy #S.SOCKET.DQ.HEAD
				lda (ZPPtrSKT),y
				tax
				inc 
				cmp #S.SOCKET.DQ.MAX
				bne .1
				lda #0
.1				ldy #S.SOCKET.DQ.TAIL
				cmp (ZPPtrSKT),y
				beq .9					Queue full!!
				
				ldy #S.SOCKET.DQ.HEAD
				sta (ZPPtrSKT),y
				txa
				clc
				adc #S.SOCKET.DQ
				tay
				lda hFrame1
				sta (ZPPtrSKT),y
				clc
				rts
				
.9				sec
				rts
*--------------------------------------
SKT.AddToQueueS
				clc
				rts
				
.9				sec
				rts
*--------------------------------------
MAN
SAVE LIB/LIBTCPIP.S.SKT
LOAD LIB/LIBTCPIP.S
ASM
