NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF sbin/networkd
*--------------------------------------
LIB.MAX			.EQ 4
DBG				.EQ 0
*--------------------------------------
				.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
pBuf			.BS 2
pNETCFG			.BS 2
hEtcNetwork		.BS 1
hBuf			.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 #32					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.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 .8

				>LDYAI 64
				>SYSCALL GetMem
				bcs .99

				>STYA pBuf
				stx hBuf
				
				>STYA IOCTL+S.IOCTL.BUFPTR	for CS.INIT.DEV
	
				jsr CS.INIT.DEV
				bcs .90

				lda LibCnt

.1				inc
				>SYSCALL ArgV
				bcs .2

				jsr CS.INIT.LIB
				bcs .90

				inc LibCnt
				lda LibCnt
				cmp #LIB.MAX
				bne .1
				
.2				jsr CS.INIT.CONF

.90				php
				pha
				lda hBuf
				>SYSCALL FreeMem
				pla
				plp
				
.99				rts

.8				>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
				
				lda #S.IOCTL.S.GETDCB
				sta IOCTL+S.IOCTL.S

				ldx #IOCTL.STATUS
*--------------------------------------
CS.IOCTL		lda DevID
				>PUSHA
				txa
				>PUSHA
				>PUSHW L.IOCTL
				>SYSCALL IOCTL
CS.IOCTL.RTS	rts
*--------------------------------------
* pBuf = DCB.NIC
*--------------------------------------
CS.INIT.LIB		phy
				pha
				>PUSHW L.MSG.LIB
				pla
				pha
				>PUSHYA
				>PUSHBI 2
				>SYSCALL PrintF
				
				pla
				ply
			
				>SYSCALL LoadLib
				jsr CS.RUN.CheckErr
				bcs CS.IOCTL.RTS

				ldy LibCnt
				sta hLIBs,y
				tay
				ldx #LIBNET.GETCFG
				jsr A2osX.LIBCALL
				bcs CS.IOCTL.RTS

				>STYA pNETCFG

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

				ldy #S.DCB.NIC.FLAGS
				lda (pBuf),y
	
				ldy #S.NETCFG.DevFlags
				sta (pNETCFG),y

				ldx #6
				ldy #S.DCB.NIC.MAC
				
.2				lda (pBuf),y
				pha
				iny
				dex
				bne .2

				ldx #6
				ldy #S.NETCFG.MAC+5

.3				pla
				sta (pNETCFG),y
				dey
				dex
				bne .3

				>PUSHW pNETCFG

				ldx LibCnt
				ldy hLIBs,x

				ldx #LIBNET.SETCFG
				jmp A2osX.LIBCALL
*--------------------------------------
CS.INIT.CONF	>PUSHW L.ETCNETWORK
				>PUSHBI	O.RDONLY+O.TEXT
				>PUSHBI S.FI.T.TXT
				>PUSHWZ
				>SYSCALL FOpen
				bcs .9

				sta hEtcNetwork

.1				>PUSHB hEtcNetwork
				>PUSHW pBuf
				>PUSHWI 63
				>SYSCALL FGetS
				bcs .8

				>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

.8				lda hEtcNetwork
				>SYSCALL FClose

*				clc

.9				rts
*--------------------------------------
CS.RUN			ldx #IOCTL.READ
				jsr CS.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

*				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

				ldx #IOCTL.CLOSE
				jsr CS.IOCTL
				
.8				clc
				rts
*--------------------------------------
CS.END
*--------------------------------------
MSG.RUNNING		.CZ "NETWORKD:Already loaded, exiting."
MSG.DEV.KO		.CZ "NETWORKD:No Device Found, exiting."
MSG.DEV.OK		.CZ "NETWORKD:Bound To Device : %s\r\n"
MSG.LIB			.CZ "NETWORKD:Loading %s..."
MSG.EXEC		.CZ "NETWORKD:Running %s..."
*--------------------------------------
MSG.OK			.CZ "[OK]"
MSG.ERR			.CZ "[%h]\r\n"
*--------------------------------------
ETCNETWORK		.AZ "${BOOT}etc/network"
				.DO DBG=1
MSG.DBG			.CS "FRM ETH:%h%h%h%h%h%h.%h%h%h%h%h%h.%h%h\r\n"
				.CS "    IP :%h%h.%h%h.%h%h.%h%h.%h [%d] (%h%h) %d.%d.%d.%d>%d.%d.%d.%d\r\n"
				.CZ "    TCP:%D>%D %h%h%h%h %h%h%h%h %h %b [%h%h] (%h%h) %h%h\r\n"
MSG.DBG.REJECT	.CZ "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
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
DS.END			.ED
*--------------------------------------
MAN
SAVE usr/src/sbin/networkd.s
ASM
