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
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 #16					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.DEVNAME		.DA DEVNAME
L.MSG.DEV.OK	.DA MSG.DEV.OK
L.MSG.DEV.KO	.DA MSG.DEV.KO
L.MSG.LIB		.DA MSG.LIB
L.IOCTL			.DA IOCTL
L.DCB.NIC		.DA DCB.NIC
L.ETCNETWORK	.DA ETCNETWORK
				.DA 0
*--------------------------------------
CS.INIT			jsr CS.INIT.DEV
				bcs .9

				jsr CS.INIT.LIBS
				bcs .9
				
				jsr CS.INIT.CONF
				
.9				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 .9
				
				ldy LibCnt
				sta hLIBs,y
				inc LibCnt
				
				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
				
				lda LibCnt
				cmp #LIB.MAX
				bne .1
				
.8				clc

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

				>STYA pBuf
				stx hLineBuf
				
				>PUSHWI 0
				>PUSHBI S.FI.T.TXT
				>PUSHBI	O.RDONLY
				
				>LDYA L.ETCNETWORK
				>SYSCALL fopen
				bcs .9
				
				sta hNetworkFile
				
.1				>PUSHWI 63
				>PUSHW pBuf
				lda hNetworkFile
				>SYSCALL fgets	
				bcs .8
				
				>PUSHBI 0				PS Flags
				>LDYA pBuf
				>SYSCALL execL
				bra .1
				
.8				lda hNetworkFile
				>SYSCALL fclose
				
				clc
.9				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 .7
				
				>PUSHW L.IOCTL
				lda hFrameIn
				
				phx
				ldx #LIBNET.INCOMING
				jsr A2osX.LIBCALL
				plx
				bcc .8
				
				inx
				bra .1

.7				lda hFrameIn
				>SYSCALL freemem
				
.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				>PUSHWI 0
				>PUSHBI IOCTL.CLOSE
				lda DevID
				>SYSCALL IOCTL				
				
				clc
				rts
*--------------------------------------
CS.END
*--------------------------------------
DEVNAME			.AZ "/DEV/ETH1"
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"
DevID			.BS 1
LibCnt			.BS 1
hLIBs			.BS LIB.MAX+1			for ending 0 if MAX libs
IOCTL			.BS S.IOCTL
DCB.NIC			.BS S.DCB.NIC
*ETCNETWORK		.AZ "${ROOT}ETC/NETWORK"
ETCNETWORK		.AZ "../ETC/NETWORK"
hNetworkFile	.BS 1
hLineBuf		.BS 1
hFrameIn		.BS 1
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
DS.END			.ED
*--------------------------------------
MAN
SAVE USR/SRC/SBIN/NETWORKD.S
ASM
