	PAGE	DEFINITIONS
ENTRY:CHANNEL	EQU	0
ENTRY:KEY	EQU	1
ENTRY:NODE	EQU	5
ENTRY:SIZE	EQU	6


NETTCB:CLOSEFLAG	EQU	TCB:SIZE
NETTCB:OPCRFLAG	EQU	NETTCB:CLOSEFLAG+1
NETTCB:NODENUM	EQU	NETTCB:OPCRFLAG+1
NETTCB:ENTRYPTR	EQU	NETTCB:NODENUM+1
NETTCB:FREESIZE	EQU	NETTCB:ENTRYPTR+2
NETTCB:FREESPACE	EQU	NETTCB:FREESIZE+2
NETTCB:KEYPTR	EQU	NETTCB:FREESPACE+2
NETTCB:SCBLKPTR	EQU	NETTCB:KEYPTR+2
NETTCB:SCBLK	EQU	NETTCB:SCBLKPTR+2
NETTCB:SIZE	EQU	NETTCB:SCBLK+SCBLK:END
	PAGE	FUNCTION MANAGER...
FUNCTIONINIT	LDX	#SOCKETMBURY
	STX	SDNETSOCKETMBURY


FUNCTIONMANAGER	STS	GOODSTACK	TO GET BACK TO BASIC WITH...
	JSR	GETSOCKETFORFNREC
	BCS	GOBACK	NO BODY TO TALK TO!
	JSR	RECBYTE
	CMPA	#IOSYSCALL
	BEQ	DOSYSCALL
	JSR	RELEASESOCKET
GOBACK	CLC
	RTS

DOSYSCALL	JSR	RECBYTE
	PSHA
	JSR	RECBYTE
	ANDA	#$7F
	TAB
	LDX	CURTCB
	LDX	NETTCB:SCBLKPTR,X
	STAA	1,X
	PULB
	STAB	0,X
	SUBA	#2
	BEQ	GOTBLOCK	NO MORE SCBLOCK IN LINK
	INX
	INX
	TAB
	CLRA
	JSR	RECBLOCK	GET THE REST

GOTBLOCK	LDX	CURTCB
	LDX	NETTCB:SCBLKPTR,X
	LDAB	#SCBLK:END
	SUBB	SCBLK:WLEN,X
	BEQ	ALLOCATEBUFS

CLEARITLOOP	CLR	SCBLK:END-1,X
	DEX
	DECB
	BNE	CLEARITLOOP

ALLOCATEBUFS	LDX	CURTCB
	LDAA	NETTCB:FREESPACE,X
	LDAB	NETTCB:FREESPACE+1,X
	STAA	NETTCB:SCBLK+SCBLK:WRBUF,X
	STAB	NETTCB:SCBLK+SCBLK:WRBUF+1,X
	ADDB	NETTCB:SCBLK+SCBLK:WRLEN+1,X
	ADCA	NETTCB:SCBLK+SCBLK:WRLEN,X
	STAA	NETTCB:SCBLK+SCBLK:RDBUF,X
	STAB	NETTCB:SCBLK+SCBLK:RDBUF+1,X
	LDAA	NETTCB:FREESIZE,X
	LDAB	NETTCB:FREESIZE+1,X
	SUBB	NETTCB:SCBLK+SCBLK:WRLEN+1,X
	SBCA	NETTCB:SCBLK+SCBLK:WRLEN,X
	BCS	NOSPACE
	SUBB	NETTCB:SCBLK+SCBLK:RDLEN+1,X
	SBCA	NETTCB:SCBLK+SCBLK:RDLEN,X
	BCS	NOSPACE

	LDX	CURTCB
	LDAA	NETTCB:SCBLK,X
	ASLA
	STAA	TEMPX+1
	LDAA	#FIRSTBRANCHAD/256
	STAA	TEMPX
	LDX	TEMPX
	LDX	FIRSTBRANCHAD\256,X
	JMP	0,X

FIRSTBRANCHAD	EQU	*
	FDB	OPCR	:OPEN
	FDB	OPCR	:CREATE
	FDB	CLOSECALL	:CLOSE
	FDB	CHANNELOP	:RENAME
	FDB	GETWRBUF	:DELETE
	FDB	CHANGETOOPEN	:LOAD
	FDB	CHANGETOOPEN	:CHAIN
	FDB	CHANGETOCREATE	:CREATLOG
	FDB	CLOSECALL	:CLOSELOG
	FDB	GETWRBUF	:DISKDEFAULT
	FDB	CHANNELOP	:READA
	FDB	CHANNELOP	:READB
	FDB	CHANNELOP	:WRITEA
	FDB	CHANNELOP	:WRITEB
	FDB	CHANNELOP	:CONTROL
	FDB	CHANNELOP	:STATUS
	PAGE	SCBLK IS TOO BIG...
NOSPACE	LDAA	NETTCB:SCBLK+SCBLK:OPCODE,X
	ASLA
	STAA	TEMPX+1
	LDAA	#TOOBIGBRANCH/256
	STAA	TEMPX
	LDX	TEMPX
	LDX	TOOBIGBRANCH\256,X
	JMP	0,X

TOOBIGBRANCH	EQU	*

	FDB	EATWRBUF	:OPEN
	FDB	EATWRBUF	:CREATE
	FDB	EATCAPWRBUF	:CLOSE
	FDB	EATCAPWRBUF	:RENAME
	FDB	EATWRBUF	:DELETE
	FDB	EATWRBUF	:LOAD
	FDB	EATWRBUF	:CHAIN	HOW DID WE GET THIS?
	FDB	EATWRBUF	:CREATELOG
	FDB	EATWRBUF	:CLOSELOG
	FDB	EATWRBUF	:DISKDEFAULT
	FDB	READATOOBIG	:READA
	FDB	READBTOOBIG	:READB
	FDB	WRITETOOBIG	:WRITEA
	FDB	WRITETOOBIG	:WRITEB
	FDB	EATCAPWRBUF	:CONTROL
	FDB	EATCAPWRBUF	:STATUS
	PAGE	OPEN/CREATE CALLS
OPCR	JSR	ALLOCATECHAN	GET ME A NEW CHANNEL #
	BCC	OPCR2
	LDX	#ILLEGALENTRY	WHO WILL GET SOME FUNNY ERROR

OPCR2	STX	TEMPX
	LDX	CURTCB
	LDAA	TEMPX
	LDAB	TEMPX+1
	STAA	NETTCB:ENTRYPTR,X
	STAB	NETTCB:ENTRYPTR+1,X
	DEC	NETTCB:OPCRFLAG,X
	LDX	TEMPX
	LDAA	ENTRY:CHANNEL,X
	LDX	CURTCB
	STAA	NETTCB:SCBLK+SCBLK:PARAMS,X
	JMP	GETWRBUF

OPCRWRAPUP	TSTA
	BNE	OPCROOPS
	TSTB
	BNE	OPCROOPS
	LDX	CURTCB
	LDX	NETTCB:ENTRYPTR,X
	RPT	ENTRY:KEY
	INX
	LDAB	#4
	CLRA
	JSR	XMITBLOCK
	LDX	CURTCB
	LDAA	NETTCB:NODENUM,X
	LDX	NETTCB:ENTRYPTR,X
	STAA	ENTRY:NODE,X

OPCRWU2	JSR	RELEASESOCKET
	LDX	CURTCB
	CLR	NETTCB:OPCRFLAG,X
	JMP	FUNCTIONMANAGER


OPCROOPS	EQU	*
	CLRA
	JSR	XMITBYTE	ZERO CAPABILITY
	CLRA
	JSR	XMITBYTE	ZERO CAPABILITY
	CLRA
	JSR	XMITBYTE	ZERO CAPABILITY
	CLRA
	JSR	XMITBYTE	ZERO CAPABILITY

	LDX	CURTCB
	LDX	NETTCB:ENTRYPTR,X
	LDAA	0,X
	INX		ASSERT ENTRY:KEY=1
	JSR	DEALLOCATECHAN
	BRA	OPCRWU2
	PAGE	CLOSE CALL
CLOSECALL	LDX	CURTCB
	INC	NETTCB:CLOSEFLAG,X
	JMP	CHANNELOP

CLOSEWRAPUP	LDX	CURTCB
	LDAA	NETTCB:SCBLK+SCBLK:PARAMS,X	=CHANNEL #
	CLR	NETTCB:CLOSEFLAG,X
	LDX	NETTCB:KEYPTR,X
	JSR	DEALLOCATECHAN
	JSR	RELEASESOCKET
	JMP	FUNCTIONMANAGER
	PAGE	FUNNY CALLS
CHANGETOOPEN	LDAA	#SYSCALL:OPEN
	SK2
CHANGETOCREATE	LDAA	#SYSCALL:CREATE
	LDX	CURTCB
	STAA	NETTCB:SCBLK+SCBLK:OPCODE,X
	JMP	OPCR
	PAGE	NORMAL CALLS
CHANNELOP	BSR	CHANNELOPSB
GETWRBUF	BSR	GETWRBUFSB
	JMP	DOIT

CHANNELOPSB	LDX	CURTCB
	LDX	NETTCB:KEYPTR,X
	LDAB	#4
	CLRA
	JSR	RECBLOCK
	LDX	CURTCB
	LDX	NETTCB:KEYPTR,X
	JSR	CONVERTKEYTOCHAN
	LDAA	0,X
	LDX	CURTCB
	STAA	NETTCB:SCBLK+SCBLK:PARAMS,X
	RTS

GETWRBUFSB	LDX	CURTCB
	LDAA	NETTCB:SCBLK+SCBLK:WRLEN,X
	LDAB	NETTCB:SCBLK+SCBLK:WRLEN+1,X
	LDX	NETTCB:SCBLK+SCBLK:WRBUF,X
	JMP	RECBLOCK
	PAGE	DO THE SYSCALL
DOIT	LDX	CURTCB
	LDX	NETTCB:SCBLKPTR,X
	JSR	$FB
	BCS	OOPS
	LDX	#0
OOPS	STX	TEMPX
	LDAA	TEMPX
	LDAB	TEMPX+1
	PSHB
	PSHA
	BSR	XMITRDBUF
	CLRA
	JSR	XMITBYTE
	CLRA
	JSR	XMITBYTE
	TSX
	LDAA	0,X	GET MSB OF ERROR
	JSR	XMITBYTE
	TSX
	LDAA	1,X	GET LSB OF ERROR
	JSR	XMITBYTE
	PULA
	PULB
	LDX	CURTCB
	TST	NETTCB:OPCRFLAG,X
	BNE	JOPCRWU
	TST	NETTCB:CLOSEFLAG,X
	BNE	JCLOSEWU
	JSR	RELEASESOCKET
	JMP	FUNCTIONMANAGER	NEXT!

JOPCRWU	JMP	OPCRWRAPUP
JCLOSEWU	JMP	CLOSEWRAPUP

XMITRDBUF	LDX	CURTCB
	LDX	NETTCB:SCBLK+SCBLK:RPLEN,X
	BEQ	DONTXMIT
	LDX	CURTCB
	LDAA	NETTCB:SCBLK+SCBLK:RPLEN,X
	JSR	XMITBYTE
	LDX	CURTCB
	LDAA	NETTCB:SCBLK+SCBLK:RPLEN+1,X
	JSR	XMITBYTE

	LDX	CURTCB
	LDAA	NETTCB:SCBLK+SCBLK:RPLEN,X
	LDAB	NETTCB:SCBLK+SCBLK:RPLEN+1,X
	LDX	NETTCB:SCBLK+SCBLK:RDBUF,X
	JMP	XMITBLOCK
DONTXMIT	CLC
	RTS
	PAGE	READ ASCII REQUEST TOO LARGE
READATOOBIG	LDX	CURTCB
	LDAA	NETTCB:SCBLK+SCBLK:PARAMS+1,X	LINE MODE?
	BNE	LMREADATB	YES, JUST SHORTEN LINE
	JMP	READBTOOBIG	NO, DO IT IN CHUNKS

LMREADATB	LDX	CURTCB
	LDAA	NETTCB:FREESIZE,X
	LDAB	NETTCB:FREESIZE+1,X
	STAA	NETTCB:SCBLK+SCBLK:RDLEN,X
	STAB	NETTCB:SCBLK+SCBLK:RDLEN+1,X
	JSR	CHANNELOPSB
	JSR	EATWRBUFSB
	JMP	DOIT
	PAGE	READ BINARY TOO LARGE

READBTOOBIG	LDX	CURTCB
	LDX	NETTCB:SCBLK+SCBLK:WRLEN,X
	BEQ	READBREALLYTB
	JSR	CHANNELOPSB
	JSR	EATWRBUF	WHY IS THERE A WRITE BUFFER?
	LDX	CURTCB
	CLR	NETTCB:SCBLK+SCBLK:WRLEN,X
	CLR	NETTCB:SCBLK+SCBLK:WRLEN+1,X
	LDD	NETTCB:FREESPACE,X
	STD	NETTCB:SCBLK+SCBLK:RDBUF,X
	BRA	READBRTB2

READBREALLYTB	JSR	CHANNELOPSB
READBRTB2	LDX	CURTCB
	CLR	NETTCB:SCBLK+SCBLK:RPLEN,X
	CLR	NETTCB:SCBLK+SCBLK:RPLEN+1,X
	LDAA	NETTCB:SCBLK+SCBLK:RDLEN,X
	LDAB	NETTCB:SCBLK+SCBLK:RDLEN+1,X
	PSHB
	PSHA
READBLOOP	PULA
	PULB
	LDX	CURTCB
	SUBB	NETTCB:SCBLK+SCBLK:RPLEN+1,X
	SBCA	NETTCB:SCBLK+SCBLK:RPLEN,X
	BNE	READBLOOP2
	TSTB
	BEQ	WRDONEJ
READBLOOP2	PSHD
	CMPA	NETTCB:FREESIZE,X
	BGT	RDUSEFS
	BLT	RDUSELEFT
	CMPB	NETTCB:FREESIZE,X
	BHI	RDUSEFS
RDUSELEFT	STD	NETTCB:SCBLK+SCBLK:RDLEN,X
	BRA	DOREADB

RDUSEFS	LDD	NETTCB:FREESIZE,X
	BRA	RDUSELEFT

DOREADB	LDX	NETTCB:SCBLKPTR,X
	JSR	$FB
	BCS	READBOOPS

	LDX	CURTCB
	LDD	NETTCB:SCBLK+SCBLK:RPLEN,X
	BNE	DOREADB2
	TSTA
	BEQ	READBLOOP	DON'T XMIT A ZERO COUNT!

DOREADB2	PSHB
	JSR	XMITBYTE
	PULA
	JSR	XMITBYTE

	LDX	CURTCB
	LDD	NETTCB:SCBLK+SCBLK:RPLEN,X
	LDX	NETTCB:SCBLK+SCBLK:RDBUF,X
	JSR	XMITBLOCK
	BRA	READBLOOP

WRDONEJ	JMP	WRDONE

READBOOPS	STX	TEMPX
	LDD	TEMPX
	PSHD
	LDX	CURTCB
	LDD	NETTCB:SCBLK+SCBLK:RPLEN,X
	BNE	READBOOPS3
	TSTA
	BEQ	READBOOPS2
READBOOPS3	EQU	*
	PSHB
	JSR	XMITBYTE
	PULA
	JSR	XMITBYTE
	LDX	CURTCB
	LDD	NETTCB:SCBLK+SCBLK:RPLEN,X
	LDX	NETTCB:SCBLK+SCBLK:RDBUF,X
	JSR	XMITBLOCK

READBOOPS2	CLRA
	JSR	XMITBYTE
	CLRA
	JSR	XMITBYTE
	PULA
	JSR	XMITBYTE
	PULA
	JSR	XMITBYTE
	JMP	WRWRAPUP
	PAGE	WRITE TOO BIG
WRITETOOBIG	LDX	CURTCB
	LDX	NETTCB:SCBLK+SCBLK:RDLEN,X
	BEQ	WRITEREALLYTB
	LDX	CURTCB
	CLR	NETTCB:SCBLK+SCBLK:RDLEN,X
	CLR	NETTCB:SCBLK+SCBLK:RDLEN+1,X
	JMP	ALLOCATEBUFS

WRITEREALLYTB	LDX	CURTCB
	LDD	NETTCB:SCBLK+SCBLK:WRLEN,X
	PSHD
	CLR	NETTCB:SCBLK+SCBLK:WRLEN,X
	CLR	NETTCB:SCBLK+SCBLK:WRLEN+1,X
	JSR	CHANNELOPSB	GET CHANNEL NUMBER

WRITELOOP	LDX	CURTCB
	PULD
	SUBD	NETTCB:SCBLK+SCBLK:WRLEN,X
	BNE	WRITELOOP2
	TSTB
	BEQ	WRDONE

WRITELOOP2	PSHD
	CMPA	NETTCB:FREESIZE,X
	BGT	WRUSEFS
	BLT	WRUSELEFT
	CMPB	NETTCB:FREESIZE+1,X
	BHI	WRUSEFS

WRUSELEFT	STD	NETTCB:SCBLK+SCBLK:WRLEN,X
	BRA	DOWRITE

WRUSEFS	LDD	NETTCB:FREESIZE,X
	BRA	WRUSELEFT

DOWRITE	JSR	GETWRBUFSB	GET THE STUFF OFF THE LINK
	LDX	CURTCB
	LDX	NETTCB:SCBLKPTR,X
	JSR	$FB
	BCC	WRITELOOP

*	ERROR!!!
	STX	TEMPX
	PULD
	LDX	CURTCB
	SUBD	NETTCB:SCBLK+SCBLK:WRLEN,X
	BNE	WRERR
	TSTB
	BEQ	GIVEERROR
WRERR	STD	TEMPX+2
	LDD	TEMPX
	PSHD
	LDD	TEMPX+2

WREATWR	PSHD
	JSR	RECBYTE
	PULD
	SUBB	#1
	SBCA	#0
	BNE	WREATWR
	TSTB
	BNE	WREATWR
	BRA	GIVEERROR2

GIVEERROR	LDD	TEMPX
	PSHD

GIVEERROR2	CLRA
	JSR	XMITBYTE
	CLRA
	JSR	XMITBYTE
	PULA
	JSR	XMITBYTE
	PULA
	JSR	XMITBYTE

WRWRAPUP	JSR	RELEASESOCKET
	JMP	FUNCTIONMANAGER

WRDONE	CLR	TEMPX
	CLR	TEMPX+1
	BRA	GIVEERROR
	PAGE	ERROR RECOVERY ROUTINES
EATCAPWRBUF	RPT	4
	JSR	RECBYTE

EATWRBUF	BSR	EATWRBUFSB
	CLRA
	JSR	XMITBYTE
	CLRA
	JSR	XMITBYTE

	LDAA	#ERR:SCBLKTOOBIG/256
	JSR	XMITBYTE
	LDAB	#ERR:SCBLKTOOBIG&$FF
	JSR	XMITBYTE

	JSR	RELEASESOCKET
	JMP	FUNCTIONMANAGER

EATWRBUFSB	LDX	CURTCB
	LDD	NETTCB:SCBLK+SCBLK:WRLEN,X
	BNE	EATLOOP
	TSTA
	BNE	EATLOOP
	RTS

EATLOOP	PSHD
	JSR	RECBYTE
	PULD
	SUBB	#1
	SBCA	#0
	BNE	EATLOOP
	TSTB
	BNE	EATLOOP
	RTS
	PAGE	MULTI-TASKING SUBROUTINES
*
*
*	ALLOCATE CHANNEL
*
*
ALLOCATECHAN	LDX	#CHANTABLESEMAPHORE
	JSR	ALLOCATESEMA

	LDX	#CHANNELTABLE

FINDCHANL	TST	ENTRY:CHANNEL,X
	BEQ	NOCHANNELS

	LDAA	ENTRY:KEY,X
	ORAA	ENTRY:KEY+1,X
	ORAA	ENTRY:KEY+2,X
	ORAA	ENTRY:KEY+3,X
	BEQ	FOUNDCHAN

	RPT	ENTRY:SIZE
	INX
	BRA	FINDCHANL

NOCHANNELS	LDX	#CHANTABLESEMAPHORE
	JSR	DEALLOCATESEMA
	SEC
	RTS

FOUNDCHAN	STX	TEMPX
	LDD	TEMPX
	PSHD
	JSR	GENKEY
	STX	TEMPX+2
	STD	TEMPX+4
	PULD
	STD	TEMPX
	LDX	TEMPX
	LDD	TEMPX+2
	STD	ENTRY:KEY,X
	LDD	TEMPX+4
	STD	ENTRY:KEY+2,X

	LDX	#CHANTABLESEMAPHORE
	JSR	DEALLOCATESEMA
	LDX	TEMPX
	CLC
	RTS
	PAGE
*
*	CONVERT KEY TO CHANNEL
*
*	(X)->4 BYTE KEY   RETURNS (X)->ENTRY
*
CONVERTKEYTOCHAN	EQU	*

	STX	TEMPX
	LDD	0,X
	LDX	#CHANNELTABLE

CONVERTLOOP	CMPA	ENTRY:KEY,X
	BNE	NEXTCONV
	CMPB	ENTRY:KEY+1,X
	BNE	NEXTCONV
	STX	TEMPX+2
	LDX	TEMPX
	LDD	2,X
	LDX	TEMPX+2
	CMPA	ENTRY:KEY+2,X
	BNE	NEXTCONV1
	CMPB	ENTRY:KEY+3,X
	BNE	NEXTCONV1
	CLC
	RTS

NEXTCONV1	STX	TEMPX+2
	LDX	TEMPX
	LDD	0,X
	LDX	TEMPX+2

NEXTCONV	RPT	ENTRY:SIZE
	INX
	TST	0,X
	BNE	CONVERTLOOP
	SEC
	RTS
	PAGE
*
*	CONVERT CHANNEL # IN A TO A ENTRY POINTER
*	IN X.  DOES NOT USE TEMPX, +1
*
CONVERTTOENTRY	DECA
	ASLA		*2
	TAB
	ASLA		*4
	ABA		*6	ASSERT ENTRY:SIZE=6
	LDAB	#CHANNELTABLE/256
	ADDA	#CHANNELTABLE&$FF
	ADCB	#0
	STAB	TEMPX+2
	STAA	TEMPX+3
	LDX	TEMPX+2
	RTS
	PAGE
*
*	DEALLOCATE CHANNEL
*		(A)=CHANNEL
*		(X)->CAPABILITY
*
DEALLOCATECHAN	STX	TEMPX
	BSR	CONVERTTOENTRY
	STX	TEMPX+2
	LDD	ENTRY:KEY,X
	LDX	TEMPX
	CMPA	0,X
	BNE	BADCAPABILITY
	CMPB	1,X
	BNE	BADCAPABILITY
	LDD	2,X
	LDX	TEMPX+2
	CMPA	ENTRY:KEY+2,X
	BNE	BADCAPABILITY
	CMPB	ENTRY:KEY+3,X
	BNE	BADCAPABILITY
	
*	IS THIS A FREE CHANNEL?

	LDAA	ENTRY:KEY,X
	ORAA	ENTRY:KEY+1,X
	ORAA	ENTRY:KEY+2,X
	ORAA	ENTRY:KEY+3,X
	BEQ	BADCAPABILITY

	CLR	ENTRY:KEY,X
	CLR	ENTRY:KEY+1,X
	CLR	ENTRY:KEY+2,X
	CLR	ENTRY:KEY+3,X
	CLR	ENTRY:NODE,X

	CLC
	RTS

BADCAPABILITY	SEC
	RTS
	PAGE
*
*	ALLOCATE SEMAPHORE
*
ALLOCATESEMA	CLC
	ROR	0,X
	BCS	MINE
	JSR	SDOS+SDOS:WAITEVENT
	BRA	ALLOCATESEMA	MAKE SURE...

MINE	RTS

*
*	DEALLOCATE SEMAPHORE
*
DEALLOCATESEMA	INC	0,X
	RTS
	PAGE	NET INTERFACE ROUTINES
XMITBYTE	LDX	CURTCB
	TAB
	LDAA	NETTCB:NODENUM,X
	JSR	SDNETXMITBYTE	WHO KNOWS...
	RTS


XMITBLOCK	STX	TEMPX
	STD	TEMPX+2
	LDX	TEMPX+2
	BEQ	XMITRTS

	PSHD
	LDD	TEMPX
	PSHD

XMITLOOP	TSX
	LDX	0,X
	LDAA	0,X
	INX
	STX	TEMPX
	TSX
	LDAB	TEMPX
	STAB	0,X
	LDAB	TEMPX+1
	STAB	1,X
	BSR	XMITBYTE

	TSX
	LDX	2,X
	DEX
	STX	TEMPX
	BEQ	XMITBLOCKEND
	LDD	TEMPX
	TSX
	STD	2,X
	BRA	XMITLOOP

XMITBLOCKEND	RPT	4
	INS
XMITRTS	RTS

RECBYTE	LDX	CURTCB
	LDAA	NETTCB:NODENUM,X
	JSR	SDNETRECBYTE
	TBA
	NOP	FOR BREAKPOINTING
	RTS

RECBLOCK	PSHD
	STX	TEMPX
	LDD	TEMPX
	PSHD

	TSX
	LDX	2,X
	BEQ	XMITBLOCKEND

RECBLOCKLOOP	BSR	RECBYTE
	TSX
	LDX	0,X
	STAA	0,X
	INX
	STX	TEMPX
	LDD	TEMPX
	TSX
	STD	0,X
	LDX	2,X
	DEX
	STX	TEMPX
	BEQ	XMITBLOCKEND
	LDD	TEMPX
	TSX
	STD	2,X
	BRA	RECBLOCKLOOP

RELEASESOCKET	RTS
	NOP
	NOP

GETSOCKETFORFNREC	JSR	SDNETPOLL
	BEQ	NOTHINGTODO	YES, NOBODY IS READY
	LDAA	NRB:NODENUM,X
	LDX	CURTCB
	STAA	NETTCB:NODENUM,X
	CLC
	RTS

NOTHINGTODO	SEC
	RTS
	PAGE	SOCKET DIED!
SOCKETMBURY	LDAA	NRB:NODENUM,X
	PSHA
	JSR	SDNETOPENNODE	REOPEN THE GUY...
	PULA
	LDX	CURTCB
	STAA	NETTCB:NODENUM,X

	LDAA	#CHANNELTABLE/256
	LDAB	#CHANNELTABLE&$FF
	STAA	NETTCB:ENTRYPTR,X
	STAB	NETTCB:ENTRYPTR+1,X

	LDAA	#SYSCALL:CLOSE
	STAA	NETTCB:SCBLK,X
	LDAA	#3
	STAA	NETTCB:SCBLK+1,X

GETTHIS	LDX	CURTCB
	LDAA	NETTCB:NODENUM,X
	LDX	NETTCB:ENTRYPTR,X
	CMPA	ENTRY:NODE,X
	BNE	GETNEXT

	LDAA	ENTRY:CHANNEL,X
	LDX	CURTCB
	STAA	NETTCB:SCBLK+SCBLK:PARAMS,X
	LDX	NETTCB:SCBLKPTR,X
	JSR	$FB	HELLO SDOS
	BCS	*+2	I DON'T CARE WHAT ERROR I GET
	LDX	CURTCB
	LDX	NETTCB:ENTRYPTR,X
	CLR	ENTRY:KEY,X
	CLR	ENTRY:KEY+1,X
	CLR	ENTRY:KEY+2,X
	CLR	ENTRY:KEY+3,X
	CLR	ENTRY:NODE,X	FREE!

GETNEXT	TST	ENTRY:CHANNEL,X	DONE?
	BEQ	ALLDONE
	LDX	CURTCB
	LDD	NETTCB:ENTRYPTR,X
	ADDB	#ENTRY:SIZE
	ADCA	#0
	STD	NETTCB:ENTRYPTR,X
	BRA	GETTHIS

ALLDONE	LDS	GOODSTACK
	CLC
	RTS	BACK TO THE USER...
	PAGE	VARIABLES, ETC.
IOSYSCALL	EQU	$E5

ILLEGALENTRY	FCB	$F0,0,0,0,0
CHANNELTABLE	EQU	*
	RPT	NCHANNELS
	FCB	1+(*-CHANNELTABLE)/6,0,0,0,0,0
	FCB	0,0,0,0,0,0	END OF TABLE


GOODSTACK	RMB	2
CHANTABLESEMAPHORE	FCB	1	NOT BUSY...
CURTCB	FDB	PSUEDOTCB


PSUEDOTCB	RMB	TCB:SIZE	SINCE I CAN'T USE USER'S TCB
	FCB	0	:CLOSEFLAG
	FCB	0	:OPCRFLAG
	FCB	0	:NODENUM
	FDB	CHANGED	:ENTRYPTR
	FDB	FREESIZE
	FDB	FREESPACE
	FDB	PSUEDOKEY	:KEYPTR
	FDB	*+2
	RMB	SCBLK:END
PSUEDOKEY	RMB	4

FREESIZE	EQU	$800
FREESPACE	RMB	FREESIZE
	END
