NEW
PREFIX
AUTO 4,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF SBIN/NETWORKD
*--------------------------------------
LIB.MAX			.EQ 4
*--------------------------------------
				.INB INC/MACROS.I
				.INB INC/A2OSX.I
				.INB INC/MLI.I
				.INB INC/MLI.E.I
				.INB INC/NIC.I
				.INB INC/ETH.I
				.INB INC/LIB.NET.I
*--------------------------------------
* Zero Page Segment, up to 32 bytes
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
pNETCFG			.BS 2
pBuf			.BS 2
hNetworkFile	.BS 1
hLineBuf		.BS 1
hFrameIn		.BS 1
ZS.END			.ED
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp (.1,x)
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					BIN Layout Version 1
				.DA #S.PS.F.EVENT
				.DA #0
				.DA CS.END-CS.START		Code Size (without Constants)
				.DA DS.END-DS.START		Data SegmentSize
				.DA #64					Stack Size
				.DA #ZS.END-ZS.START	Zero Page Size
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------				
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT
				.DA	CS.QUIT
L.MSG.RUNNING	.DA MSG.RUNNING
L.MSG.DEV.OK	.DA MSG.DEV.OK
L.MSG.DEV.KO	.DA MSG.DEV.KO
L.MSG.LIB		.DA MSG.LIB
L.MSG.EXEC		.DA MSG.EXEC
L.DEVNAME		.DA DEVNAME
L.IOCTL			.DA IOCTL
L.DCB.NIC		.DA DCB.NIC
L.ETCNETWORK	.DA ETCNETWORK
				.DA 0
*--------------------------------------
CS.INIT			lda DevID
				bne .99

				jsr CS.INIT.DEV
				bcs .9

				jsr CS.INIT.LIBS
				bcs .9
				
				jsr CS.INIT.CONF
				
.9				rts

.99				>LDYA L.MSG.RUNNING
				>SYSCALL puts
				
				lda #0
				sec
				rts
*--------------------------------------
CS.INIT.DEV		>PUSHBI 0
				>LDYA L.DEVNAME
				>SYSCALL open
				bcc .8

				inc DEVNAME+8
				lda DEVNAME+8
				cmp #'8'
				bne CS.INIT.DEV

				>LDYA L.MSG.DEV.KO
				>SYSCALL puts
				
				lda #MLI.E.NODEV
				sec
				rts
				
.8				sta DevID

				>PUSHW L.DEVNAME
				>PUSHBI 2
				>LDYA L.MSG.DEV.OK
				>SYSCALL printf
				
				>LDYA L.DCB.NIC
				>STYA IOCTL+S.IOCTL.BUFPTR
				lda #S.IOCTL.STATCODE.GETDCB
				sta IOCTL+S.IOCTL.STATCODE

				>PUSHW L.IOCTL
				>PUSHBI IOCTL.STATUS
				lda DevID
				>SYSCALL IOCTL
				rts
*--------------------------------------
CS.INIT.LIBS	lda LibCnt
.1				inc
				>SYSCALL ArgV
				bcs .8

				phy
				pha
				
				>PUSHYA
				>PUSHBI 2
				>LDYA L.MSG.LIB
				>SYSCALL printf
				
				pla
				ply
				>SYSCALL LoadLib
				bcs CS.INIT.LIBS.9
				
				ldy LibCnt
				sta hLIBs,y
				
				tay
				ldx #LIBNET.GETCFG
				jsr A2osX.LIBCALL
				>STYA pNETCFG

				lda DevID
				ldy #S.NETCFG.DevID
				sta (pNETCFG),y

				iny						S.NETCFG.DevFlags
				lda DCB.NIC+S.DCB.NIC.FLAGS
				sta (pNETCFG),y
				
				ldx #5
				ldy #S.NETCFG.MAC+5
				
.2				lda DCB.NIC+S.DCB.NIC.MAC,x
				sta (pNETCFG),y
				dey
				dex
				bpl .2
				
				inc LibCnt
				lda LibCnt
				cmp #LIB.MAX
				bne .1
				
.8				clc

CS.INIT.LIBS.9	rts				
*--------------------------------------
CS.INIT.CONF	>LDYAI 64
				>SYSCALL GetMem
				bcs CS.INIT.LIBS.9

				>STYA pBuf
				stx hLineBuf
				
				>PUSHWI 0
				>PUSHBI S.FI.T.TXT
				>PUSHBI	O.RDONLY+O.TEXT
				
				>LDYA L.ETCNETWORK
				>SYSCALL fopen
				bcs .98
				
				sta hNetworkFile
				
.1				>PUSHWI 64
				>PUSHW pBuf
				lda hNetworkFile
				>SYSCALL fgets	
				bcs .80

				>PUSHW pBuf
				>PUSHBI 2
				>LDYA L.MSG.EXEC
				>SYSCALL printf

				>PUSHBI 0				S.PS.F.HOLD+S.PS.F.NOHUP
				>LDYA pBuf
				>SYSCALL execL
				bra .1
				
.98				pha
				jsr .81
				pla
				sec
				rts
				
.80				lda hNetworkFile
				>SYSCALL fclose
				
.81				lda hLineBuf
				>SYSCALL FreeMem
				
				clc
				rts
*--------------------------------------
CS.RUN			>PUSHW L.IOCTL
				>PUSHBI IOCTL.READ
				lda DevID
				>SYSCALL IOCTL
				bcs .8					No Frame
				
				sta hFrameIn
				
				ldx #0
				
.1				ldy hLIBs,x
				beq .2
				
				>PUSHW L.IOCTL
				lda hFrameIn
				
				phx
				ldx #LIBNET.INCOMING
				jsr A2osX.LIBCALL
				plx
				bcc CS.RUN
				
				inx
				bra .1

.2				lda hFrameIn
				>SYSCALL freemem
				
				bra CS.RUN
				
.8				clc
				rts
*--------------------------------------
CS.DOEVENT		lda (pEvent)
				bpl .9
				
				ldx #0
				
.1				ldy hLIBs,x
				beq .9
				
				phx
				ldx #LIBNET.PULSE
				lda (pEvent)
				jsr A2osX.LIBCALL
				plx
				
				inx
				bra .1
				
.9				sec						never discard TIME event
				rts
*--------------------------------------
CS.QUIT			ldx #0
				
.1				lda hLIBs,x
				beq .7
				
				phx
				>SYSCALL UnLoadLib
				plx
				
				inx
				bra .1
				
.7				lda DevID
				beq .8
				
				pha
				>PUSHWI 0
				>PUSHBI IOCTL.CLOSE
				pla
				>SYSCALL IOCTL				
				
.8				clc
				rts
*--------------------------------------
CS.END
*--------------------------------------
MSG.RUNNING		.AZ "NETWORKD:Already loaded, exiting."
MSG.DEV.KO		.AZ "NETWORKD:No Device Found, exiting."
MSG.DEV.OK		.AZ "NETWORKD:Bound To Device : %s\r\n"
MSG.LIB			.AZ "NETWORKD:Loading %s ...\r\n"
MSG.EXEC		.AZ "NETWORKD:Running %s ...\r\n"
ETCNETWORK		.AZ "${BOOT}ETC/NETWORK"
*--------------------------------------
DevID			.BS 1
LibCnt			.BS 1
hLIBs			.BS LIB.MAX+1			for ending 0 if MAX libs
DEVNAME			.AZ "/DEV/ETH1"
IOCTL			.BS S.IOCTL
DCB.NIC			.BS S.DCB.NIC
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
DS.END			.ED
*--------------------------------------
MAN
SAVE USR/SRC/SBIN/NETWORKD.S
ASM
