NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF sbin/networkd
*--------------------------------------
LIB.MAX			.EQ 4
DBG				.EQ 2
*--------------------------------------
				.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
hEtcNetwork		.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
				.DO DBG=1
				.DA #128				Stack Size
				.ELSE
				.DA #64					Stack Size
				.FIN
				.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.MSG.OK		.DA MSG.OK
L.MSG.ERR		.DA MSG.ERR
L.DEVNAME		.DA DEVNAME
L.IOCTL			.DA IOCTL
L.DCB.NIC		.DA DCB.NIC
L.ETCNETWORK	.DA ETCNETWORK
				.DO DBG=1
L.MSG.DBG		.DA MSG.DBG
L.MSG.DBG.REJECT	.DA MSG.DBG.REJECT
				.FIN
				.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		>PUSHW L.DEVNAME
				>PUSHBI 0
				>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.MSG.DEV.OK
				>PUSHW L.DEVNAME
				>PUSHBI 2
				>SYSCALL PrintF
				
				>LDYA L.DCB.NIC
				>STYA IOCTL+S.IOCTL.BUFPTR
				lda #S.IOCTL.S.GETDCB
				sta IOCTL+S.IOCTL.S

				>PUSHB DevID
				>PUSHBI IOCTL.STATUS
				>PUSHW L.IOCTL
				>SYSCALL IOCTL
				rts
*--------------------------------------
CS.INIT.LIBS	lda LibCnt

.1				inc
				>SYSCALL ArgV
				bcs .8

				phy
				pha
				>PUSHW L.MSG.LIB
				pla
				pha
				>PUSHYA
				>PUSHBI 2
				>SYSCALL PrintF
				
				pla
				ply
			
				>SYSCALL LoadLib
				jsr CS.RUN.CheckErr
				bcs CS.INIT.LIBS.9

				ldy LibCnt
				sta hLIBs,y
				tay
				ldx #LIBNET.GETCFG
				jsr A2osX.LIBCALL
				bcs CS.INIT.LIBS.9

				>STYA pNETCFG

				jsr CS.INIT.SETUP.NETCFG

				>PUSHW pNETCFG

				ldx LibCnt
				ldy hLIBs,x

				ldx #LIBNET.SETCFG
				jsr A2osX.LIBCALL
				bcs CS.INIT.LIBS.9
				
				inc LibCnt
				lda LibCnt
				cmp #LIB.MAX
				bne .1

.8				clc

CS.INIT.LIBS.9	rts
*--------------------------------------
CS.INIT.SETUP.NETCFG

				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
				
				rts
*--------------------------------------
CS.INIT.CONF	>LDYAI 64
				>SYSCALL GetMem
				bcs CS.INIT.LIBS.9

				>STYA pBuf
				stx hLineBuf
				
				>PUSHW L.ETCNETWORK
				>PUSHBI	O.RDONLY+O.TEXT
				>PUSHBI S.FI.T.TXT
				>PUSHWZ
				>SYSCALL FOpen
				bcs .98

				sta hEtcNetwork

.1				>PUSHB hEtcNetwork
				>PUSHW pBuf
				>PUSHWI 64
				>SYSCALL FGetS
				bcs .80

				>PUSHW L.MSG.EXEC
				>PUSHW pBuf
				>PUSHBI 2
				>SYSCALL PrintF

				>PUSHW pBuf
				>PUSHBI 0				S.PS.F.HOLD
				>SYSCALL ExecL
				jsr CS.RUN.CheckErr
				>SLEEP
				bra .1

.98				pha
				jsr .81
				pla
				sec
				rts

.80				lda hEtcNetwork
				>SYSCALL FClose

.81				lda hLineBuf
				>SYSCALL FreeMem
*				clc
				rts
*--------------------------------------
CS.RUN			>PUSHB DevID
				>PUSHBI IOCTL.READ
				>PUSHW L.IOCTL
				
				>SYSCALL IOCTL
				bcs .8					No Frame

				sta hFrameIn
				
				.DO DBG=1
				>SYSCALL GetMemPtr
				>STYA pBuf
				
				jsr CS.RUN.FILTER
				bcs .23
				
				>PUSHW L.MSG.DBG

				ldy #0
				
.22				>PUSHB (pBuf),y
				iny
				cpy #54
				bne .22
				
				>PUSHBI 54
				>SYSCALL PrintF
.23				.FIN

				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				.DO DBG=1
				jsr CS.RUN.FILTER
				
				bcs .33
				
				>LDYA L.MSG.DBG.REJECT
				>SYSCALL puts
.33				.FIN
				lda hFrameIn
				>SYSCALL FreeMem
				jmp CS.RUN

.8				clc
				rts
*--------------------------------------
				.DO DBG=1
CS.RUN.FILTER	lda (pBuf)
				cmp #$ff
				beq .9
				dec
				beq .9
				ldy #S.ETH.EII.TYPE+1
				lda (pBuf),y
				bne .9
				
				clc
				rts
				
.9				sec
				rts
				.FIN
*--------------------------------------
CS.RUN.CheckErr	pha

				bcs .1
				>LDYA L.MSG.OK
				>SYSCALL PutS
				pla
				rts

.1				>PUSHW L.MSG.ERR
				pla
				pha
				>PUSHA
				>PUSHBI 1
				>SYSCALL PrintF
				pla
				sec
				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

				>PUSHA
				>PUSHBI IOCTL.CLOSE
				>PUSHWZ
				>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..."
MSG.EXEC		.AZ "NETWORKD:Running %s..."
MSG.OK			.AZ "[OK]"
MSG.ERR			.AZ "[%h]\r\n"
ETCNETWORK		.AZ "${BOOT}etc/network"
				.DO DBG=1
MSG.DBG			.AS "FRM ETH:%h%h%h%h%h%h.%h%h%h%h%h%h.%h%h\r\n"
				.AS "    IP :%h%h.%h%h.%h%h.%h%h.%h [%d] (%h%h) %d.%d.%d.%d>%d.%d.%d.%d\r\n"
				.AZ "    TCP:%D>%D %h%h%h%h %h%h%h%h %h %b [%h%h] (%h%h) %h%h\r\n"
MSG.DBG.REJECT	.AZ "Rejected"
				.FIN
*--------------------------------------
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
