	WITH	WI=105,DE=51
	TABS	20,30,50,60,70,80
	IFUND	BASEAD
BASEAD	EQU	$6A00
	FIN
	ORG	BASEAD
	IFUND	SDOS
SDOS	EQU	$7480
	FIN
	PAGE	NET: OPEN AND CREATE
NET:OPEN	EQU	*
NET:CREATE	EQU	*
	STS	NET:USERSTACK
	TST	NET:NETINITED	IS THE SD NET READY TO GO YET?
	BNE	NET:OPEN2
	LDX	#MASTERNODE	=?
	JSR	SDNETOPEN
	INC	NET:NETINITED	MARK IT AS OPENED
NET:OPEN2	JSR	NET:PARSEFORNODE
	STAA	NET:NODENUM
	STAB	NET:NAMELEN
	STX	NET:NAMEPTR

	LDX	SDOS+SDOS:IOBLOCKPTR
	STX	NET:FROM
	LDAB	SCBLK:WLEN,X
	ANDB	#$7F
	CLRA
	LDX	#NET:SCBLK
	STX	NET:TO
	JSR	NET:COPYBYTES

	LDX	NET:NAMEPTR
	STX	NET:SCBLK+SCBLK:WRBUF
	LDAA	NET:NAMELEN
	STAA	NET:SCBLK+SCBLK:WRLEN+1
	CLR	NET:SCBLK+SCBLK:WRLEN

	JSR	NET:GETSOCKETFORNETXMIT
	LDAA	#NET:IOSYSCALL
	JSR	NET:XMITBYTE

	LDX	#NET:SCBLK
	LDAB	SCBLK:WLEN,X
	ANDB	#$7F
	CLRA
	JSR	NET:XMITBLOCK

	LDX	NET:SCBLK+SCBLK:WRBUF
	LDAA	NET:SCBLK+SCBLK:WRLEN
	LDAB	NET:SCBLK+SCBLK:WRLEN+1
	JSR	NET:XMITBLOCK

	LDX	#NET:SCBLK
	STX	NET:SCBLKPTR
	JSR	NET:GETREPLY
	BCS	*+2	IGNORE ERROR NOW
	LDAA	NET:SCBLK+SCBLK:RPLEN
	LDAB	NET:SCBLK+SCBLK:RPLEN+1
	LDX	SDOS+SDOS:IOBLOCKPTR
	STAA	SCBLK:RPLEN,X
	STAB	SCBLK:RPLEN+1,X	UPDATE USER'S SCBLK

	LDX	SDOS+SDOS:IOCBPOINTER
	LDAA	NET:NODENUM
	STAA	IOCB:NETNODENUM,X
	STX	NET:ENTRYPTR

	JSR	NET:RECBYTE
	LDX	NET:ENTRYPTR
	STAA	IOCB:CHANCAP+0,X
	JSR	NET:RECBYTE
	LDX	NET:ENTRYPTR
	STAA	IOCB:CHANCAP+1,X
	JSR	NET:RECBYTE
	LDX	NET:ENTRYPTR
	STAA	IOCB:CHANCAP+2,X
	JSR	NET:RECBYTE
	LDX	NET:ENTRYPTR
	STAA	IOCB:CHANCAP+3,X

NET:OPCRWRAPUP	JSR	NET:RELEASESOCKET
	LDX	NET:LASTERROR
	BEQ	NET:DOANOKRTS1
	JMP	NET:ERRORRTN

NET:DOANOKRTS1	OKRTS
	PAGE	MAIN NET FUNCTIONS
NET:READA	EQU	*
NET:READB	EQU	*
NET:WRITEA	EQU	*
NET:WRITEB	EQU	*
NET:CONTROL	EQU	*
NET:STATUS	EQU	*
NET:CLOSE	EQU	*

	STS	NET:USERSTACK
	LDX	SDOS+SDOS:IOBLOCKPTR

NET:ALL	STX	NET:SCBLKPTR
	LDX	SDOS+SDOS:IOCBPOINTER
	STX	NET:ENTRYPTR	POINTS TO (CHANNEL,CAPABILITY)
	LDAA	IOCB:NETNODENUM,X
	STAA	NET:NODENUM
	JSR	NET:GETSOCKETFORNETXMIT
	LDAA	#NET:IOSYSCALL
	JSR	NET:XMITBYTE
	LDX	NET:SCBLKPTR
	LDAB	SCBLK:WLEN,X
	ANDB	#$7F
	CLRA
	JSR	NET:XMITBLOCK

	LDX	NET:ENTRYPTR
	LDAA	IOCB:CHANCAP,X
	JSR	NET:XMITBYTE
	LDX	NET:ENTRYPTR
	LDAA	IOCB:CHANCAP+1,X
	JSR	NET:XMITBYTE
	LDX	NET:ENTRYPTR
	LDAA	IOCB:CHANCAP+2,X
	JSR	NET:XMITBYTE
	LDX	NET:ENTRYPTR
	LDAA	IOCB:CHANCAP+3,X
	JSR	NET:XMITBYTE

	LDX	NET:SCBLKPTR
	LDAA	SCBLK:WLEN,X
	ANDA	#$7F
	CMPA	#8
	BLT	NET:JGETREP	NO NEED TO NETXMIT WRBUF
	LDAA	SCBLK:WRLEN,X
	LDAB	SCBLK:WRLEN+1,X
	LDX	SCBLK:WRBUF,X
	JSR	NET:XMITBLOCK

NET:JGETREP	JSR	NET:GETREPLY
	JSR	NET:RELEASESOCKET
	LDX	NET:LASTERROR
	BEQ	NET:DOANOKRTS3
	JMP	NET:ERRORRTN

NET:DOANOKRTS3	OKRTS

	PAGE	NET:RENAME

NET:RENAME	EQU	*
	STS	NET:USERSTACK
	JSR	NET:PARSEFORNODE
	STAB	NET:NAMELEN
	STX	NET:NAMEPTR

*
*	CMPA	NET:NODENUM
*	BNE	WRONG NODE!!!
*
	LDX	#NET:RENAMESCBLK
	STX	NET:TO
	LDX	SDOS+SDOS:IOBLOCKPTR
	STX	NET:FROM

	LDAB	SCBLK:WLEN,X
	ANDB	#$7F
	CLRA
	JSR	NET:COPYBYTES

	LDX	NET:NAMEPTR
	STX	NET:RENAMESCBLK+SCBLK:WRBUF
	LDAA	NET:NAMELEN
	STAA	NET:RENAMESCBLK+SCBLK:WRLEN+1
	CLR	NET:RENAMESCBLK+SCBLK:WRLEN

	LDX	#NET:RENAMESCBLK
	JSR	NET:ALL
	BCS	NET:REN2
	LDX	#0

NET:REN2	STX	NET:LASTERROR
	LDAA	NET:RENAMESCBLK+SCBLK:RPLEN
	LDAB	NET:RENAMESCBLK+SCBLK:RPLEN+1
	LDX	SDOS+SDOS:IOBLOCKPTR
	STAA	SCBLK:RPLEN,X
	STAB	SCBLK:RPLEN+1,X
	LDX	NET:LASTERROR
	BEQ	NET:DOANOKRTS2
	JMP	NET:ERRORRTN

NET:DOANOKRTS2	OKRTS
	PAGE	GET THE REPLY BUFFER
NET:GETREPLY	CLR	NET:REPLYLEN
	CLR	NET:REPLYLEN+1
	LDX	NET:SCBLKPTR
	LDX	SCBLK:RDBUF,X
	STX	NET:REPLYPTR

NET:RECEIVEREPLY	JSR	NET:RECBYTE
	PSHA
	JSR	NET:RECBYTE
	TSTA
	PULB		DOESN'T AFFECT CC
	BNE	NET:RR2
	TSTB
	BEQ	NET:GETERROR	ALL DONE!

NET:RR2	PSHA
	PSHB
	ADDA	NET:REPLYLEN+1
	ADCB	NET:REPLYLEN
	STAA	NET:REPLYLEN+1
	STAB	NET:REPLYLEN

	PULA
	PULB
	PSHB
	PSHA
	LDX	NET:REPLYPTR
	JSR	NET:RECBLOCK
	PULA
	PULB
	ADDB	NET:REPLYPTR+1
	ADCA	NET:REPLYPTR
	STAA	NET:REPLYPTR
	STAB	NET:REPLYPTR+1
	BRA	NET:RECEIVEREPLY

NET:GETERROR	JSR	NET:RECBYTE
	STAA	NET:LASTERROR
	JSR	NET:RECBYTE
	STAA	NET:LASTERROR+1

	LDX	NET:SCBLKPTR
	LDAA	SCBLK:WLEN,X
	ANDA	#$7F
	CMPA	#$E
	BLT	NET:GERTS
	LDAA	NET:REPLYLEN
	LDAB	NET:REPLYLEN+1
	STAA	SCBLK:RPLEN,X
	STAB	SCBLK:RPLEN+1,X
NET:GERTS	RTS
	PAGE	NET:DELETE

NET:DELETE	STS	NET:USERSTACK
	JSR	NET:PARSEFORNODE
	STAA	NET:NODENUM
	STAB	NET:NAMELEN
	STX	NET:NAMEPTR

	LDX	SDOS+SDOS:IOBLOCKPTR
	STX	NET:FROM
	LDAB	SCBLK:WLEN,X
	ANDB	#$7F
	CLRA
	LDX	#NET:SCBLK
	STX	NET:TO
	STX	NET:SCBLKPTR
	JSR	NET:COPYBYTES

	LDX	NET:NAMEPTR
	STX	NET:SCBLK+SCBLK:WRBUF
	LDAA	NET:NAMELEN
	STAA	NET:SCBLK+SCBLK:WRLEN+1
	CLR	NET:SCBLK+SCBLK:WRLEN

	JSR	NET:GETSOCKETFORNETXMIT
	LDAA	#NET:IOSYSCALL
	JSR	NET:XMITBYTE

	LDX	#NET:SCBLK
	LDAB	SCBLK:WLEN,X
	ANDB	#$7F
	CLRA
	JSR	NET:XMITBLOCK
	LDX	NET:SC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	#9
	BHI	NET:NOTRIGHT
	PSHA

	LDAA	NET:NODENUM
	ASLA		*2
	TAB
	ASLA		*4
	ASLA		*8
	ABA		*10
	PULB
	ABA
	STAA	NET:NODENUM
	DEC	NET:NAMELEN
	BNE	NET:GETNUM

NET:NOTRIGHT	LDX	#ERR:BADFILENAME
	JMP	NET:ERRORRTN

NET:NAMETOOBIG	LDX	#ERR:NAMETOOBIG
	JMP	NET:ERRORRTN

NET:NAMETOOSMALL	LDX	#ERR:NAMETOOSMALL
	JMP	NET:ERRORRTN

NET:DONE	LDX	NET:NAMEPTR
	LDAB	NET:NAMELEN
	DECB
	LDAA	NET:NODENUM
	BEQ	NET:NOTRIGHT
	OKRTS

NET:FOLDA	CMPA	#'A+32
	BLT	NET:FOLDRTS
	CMPA	#'Z+32
	BGT	NET:FOLDRTS
	SUBA	#32
NET:FOLDRTS	RTS
	PAGE	NET INTERFACE ROUTINES
NET:XMITBYTE	TAB
	LDAA	NET:NODENUM
	JSR	SDNETNET:XMITBYTE	WHO KNOWS...
	RTS


NET:XMITBLOCK	STX	NET:FROM
	STAA	NET:COUNT
	STAB	NET:COUNT+1
	LDX	NET:COUNT
	BEQ	NET:XMITRTS

NET:XMITLOOP	LDX	NET:FROM
	LDAA	0,X
	INX
	STX	NET:FROM
	BSR	NET:XMITBYTE

	LDX	NET:COUNT
	DEX
	STX	NET:COUNT
	BNE	NET:XMITLOOP
NET:XMITRTS	RTS

NET:RECBYTE	TAB
	LDAA	NET:NODENUM
	JSR	SDNETNET:RECBYTE
	RTS

NET:RECBLOCK	STX	NET:TO
	STAA	NET:COUNT
	STAB	NET:COUNT+1

	LDX	NET:COUNT
	BEQ	NET:RECRTS

NET:RECBLOCKLOOP	BSR	NET:RECBYTE
	LDX	NET:TO
	STAA	0,X
	INX
	STX	NET:TO
	LDX	NET:COUNT
	DEX
	STX	NET:COUNT
	BNE	NET:RECBLOCKLOOP

NET:RECRTS	RTS

NET:GETSOCKETFORNETXMIT	RTS	
	RMB	2	FOR PATCHING...

NET:RELEASESOCKET	RTS
	RMB	2

NET:SOCKETMBURY	JSR	NET:RESET	BACK TO THE BEGINNING, BECAUSE
	LDS	NET:USERSTACK
	JSR	SDOS+SDOS:ERROR	THE NODE WE'RE TALKING TO DIED!
	FDB	ERR:HE.DIED
	PAGE	DON'T LOOK...
NET:COPYBYTES	STAA	NET:COUNT
	STAB	NET:COUNT+1
	LDX	NET:COUNT
	BEQ	NET:COPYBYTESRTS

NET:COPYLOOP	LDX	NET:FROM
	LDAA	0,X
	INX
	STX	NET:FROM

	LDX	NET:TO
	STAA	0,X
	INX
	STX	NET:TO

	LDX	NET:COUNT
	DEX
	STX	NET:COUNT
	BNE	NET:COPYLOOP

NET:COPYBYTESRTS	RTS
	PAGE	NET RESET
NET:RESET	CLR	NET:NETINITED
	CLC
	RTS	REAL TOUGH!
	PAGE	NET: DCB
*
*
*	DCB FOR NET:
*
*
NET:DONEFLAG	FCB	1	WASTED FLAG...
NET:LASTERROR	FDB	0	MY ERROR CODE
NET:NAME	FDB	NETNAMESTR
NET:NEXTDCB	RMB	2	WHO KNOWS WHERE...
NET:DRIVER	FDB	NET:JUMPT


NETNAMESTR	EQU	*
	FCC	'NET:'
	FCB	0
	PAGE	SYSCALL JUMP TABLE
NET:JUMPT	EQU	*

	FDB	NET:OPEN
	FDB	NET:CLOSE
	FDB	NET:READA
	FDB	NET:READB
	FDB	NET:WRITEA
	FDB	NET:WRITEB
	FDB	NET:CREATE
	FDB	NET:RENAME
	FDB	NET:DELETE
	FDB	NET:CONTROL
	FDB	NET:STATUS
	FDB	NET:RESET
	PAGE	VARIABLES
*	SCRATCH VARIABLES

NET:IOSYSCALL	EQU	$E5	WHY NOT?
ERR:NAMETOOBIG	EQU	ERR:BADFILENAME
ERR:NAMETOOSMALL	EQU	ERR:BADFILENAME	FOR NOW
NET:NAMELEN	RMB	1
NET:NAMEPTR	RMB	2
NET:NODENUM	RMB	1
NET:REPLYLEN	RMB	2
NET:REPLYPTR	RMB	2
NET:FROM	RMB	2
NET:TO	RMB	2
NET:COUNT	RMB	2
NET:NETINITED	RMB	1
NET:USERSTACK	RMB	2	SAVED FOR A RAINY DAY...

*	SCRATCH SYSCALL BLOCKS

NET:SCBLK	RMB	$E
NET:RENAMESCBLK	RMB	$E
NET:SCBLKPTR	RMB	2
	PAGE	CHANNEL CAPABILITY TABLE
*	WHAT FOLLOWS IS AN ALTERNATIVE USEAGE OF IOCBS...
IOCB:NETNODENUM	EQU	4	
IOCB:CHANCAP	EQU	5

NET:ENTRYPTR	RMB	2

	PAGE	ERROR RETURN
NET:ERRORRTN	STX	NET:ERRORCODE
	JSR	SDOS+SDOS:ERROR
NET:ERRORCODE	EQU	*
	FDB	0	FOR NOW
	END
