NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF bin/etconfig
*--------------------------------------
				.INB inc/macros.i
				.INB inc/a2osx.i
				.INB inc/kernel.i
				.INB inc/mli.i
				.INB inc/mli.e.i
				.INB inc/nic.i
				.INB inc/eth.i
				.INB inc/lib.net.i
				.INB inc/libetalk.i
*--------------------------------------
TIMEOUT.MAX		.EQ 200					20 sec.
PROBE.MAX		.EQ 10
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
pETKCFG			.BS 2
ZPFrameBase		.BS 2
ZPFramePtr		.BS 2
BufPtr			.BS 2

ProbeCnt		.BS 1
TimeOut			.BS 1
hFrame			.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.LIBETALK		.DA LIBETALK
L.MSG.USAGE		.DA MSG.USAGE
L.MSG.NODEV		.DA MSG.NODEV
L.MSG1.DEV		.DA MSG1.DEV
L.MSG1.DEV.ARP	.DA MSG1.DEV.ARP
L.MSG1.DEV.IP	.DA MSG1.DEV.IP
L.MSG1.LINK.OK	.DA MSG1.LINK.OK
L.MSG1.LINK.KO	.DA MSG1.LINK.KO
L.MSG1.LINKSPEED .DA MSG1.LINKSPEED
L.MSG1.DPLX.FD	.DA MSG1.DPLX.FD
L.MSG1.DPLX.HD	.DA MSG1.DPLX.HD
L.MSG2			.DA MSG2
L.MSG2.C		.DA MSG2.C
L.MSG2.U		.DA MSG2.U
L.MSG2.ADDR		.DA MSG2.ADDR
L.MSG2.ROUTER	.DA MSG2.ROUTER
L.MSG2.OBJECT	.DA MSG2.OBJECT
L.MSG.CFG		.DA MSG.CFG
L.MSG.CFG.KO	.DA MSG.CFG.KO
L.MSG.CFG.OK	.DA MSG.CFG.OK
L.HOSTNAME		.DA HOSTNAME
L.CFG.DefaultHost	.DA  CFG.DefaultHost
L.ETKCFG		.DA ETKCFG
L.ETKCFG.OBJECT	.DA ETKCFG+S.ETKCFG.OBJECT
L.IOCTL			.DA IOCTL
*--------------------------------------
L.MSG.PROBE		.DA MSG.PROBE
L.MSG.PROBE.KO	.DA MSG.PROBE.KO
L.MSG.PROBE.OK	.DA MSG.PROBE.OK
L.AARP.PRB		.DA AARP.PRB
L.DDP.GetNetInfo .DA DDP.GetNetInfo
				.DA 0
*--------------------------------------
CS.INIT			>LDYA L.LIBETALK
				>SYSCALL LoadLib
				bcs .9
				sta hLIBETALK

.9				rts
*--------------------------------------
CS.RUN			>LIBCALL hLIBETALK,LIBETALK.GETCFG
				>STYA pETKCFG

				ldy #S.ETKCFG-1
.1				lda (pETKCFG),y
				sta ETKCFG,y
				dey
				bpl .1

				lda ETKCFG+S.NETCFG.DevID
				bne CS.RUN.SETUP
				>LDYA L.MSG.NODEV
				>SYSCALL PutS
				lda #MLI.E.NODEV
				sec
				rts
				
CS.RUN.SETUP	lda ETKCFG+S.NETCFG.STATUS
				bne .7
				
				jsr CFG.EtcFiles

				jsr CS.RUN.PROBE
				
				jsr CS.RUN.GETNETINFO

				lda #$80
				sta ETKCFG+S.NETCFG.STATUS
				>PUSHW L.ETKCFG
				>LIBCALL hLIBETALK,LIBETALK.SETCFG

.7				jsr CS.RUN.DUMP

.8				lda #0
				sec
CS.RUN.SETUP.RTS
				rts
*--------------------------------------
CS.RUN.DUMP		>PUSHB ETKCFG+S.NETCFG.DevID
				>PUSHEA.G DIB
				>SYSCALL GetDevStatus
				bcs CS.RUN.SETUP.RTS
				
				>LEA.G DCB.NIC
				>STYA IOCTL+S.IOCTL.BUFPTR
				lda #S.IOCTL.S.GETDCB
				sta IOCTL+S.IOCTL.S

				>PUSHB ETKCFG+S.NETCFG.DevID
				>PUSHBI IOCTL.STATUS
				>PUSHW L.IOCTL
				>SYSCALL IOCTL
				bcs CS.RUN.SETUP.RTS
				
CS.RUN.DUMPNIC	>PUSHW L.MSG1.DEV
				
				lda ETKCFG+S.NETCFG.DevID
				>PUSHA
				
				ldx ETKCFG+S.NETCFG.DevID
				lda Dev.Table-1,x
				clc
				adc #S.FD.DEV
				tay
				lda Dev.Table,x
				adc /S.FD.DEV

				>PUSHYA
				
				>PUSHEA.G DIB+S.DIB.IDS 

				ldy #DCB.NIC+S.DCB.NIC.MAC
				ldx #6
				
.1				lda (pData),y
				>PUSHA
				iny
				dex 
				bne .1

				>PUSHBI 11
				
				>SYSCALL PrintF
				
				>PUSHW L.MSG1.DEV.ARP
				
				>LDA.G DCB.NIC+S.DCB.NIC.FLAGS
				and #S.DCB.NIC.FLAGS.ARPOFFLOAD
				beq .2
				
				lda #1

.2				>PUSHA
				>PUSHBI 1	
				
				>SYSCALL PrintF

				>PUSHW L.MSG1.DEV.IP
				
				>LDA.G DCB.NIC+S.DCB.NIC.FLAGS
				and #S.DCB.NIC.FLAGS.IPOFFLOAD

				beq .3

				lda #1

.3				>PUSHA		
				>PUSHBI 1	
				
				>SYSCALL PrintF

				>LDA.G DCB.NIC+S.DCB.NIC.LINK
				and #S.DCB.NIC.LINK.OK
				bne CS.RUN.LINK
				>LDYA L.MSG1.LINK.KO
				>SYSCALL PutS
				bra CS.RUN.DUMPNET

CS.RUN.LINK		>LDYA L.MSG1.LINK.OK
				>SYSCALL PutS

				>PUSHW L.MSG1.LINKSPEED
				>PUSHBI 0
				>SYSCALL PrintF
				
				>LDA.G DCB.NIC+S.DCB.NIC.SPEED
				and #$0F
				tax
.4				phx
				lda #'0'
				>SYSCALL PutChar
				plx
				dex
				bne .4
				>LDA.G DCB.NIC+S.DCB.NIC.LINK
				and #S.DCB.NIC.LINK.FD
				bne .5
				>LDYA L.MSG1.DPLX.HD
				bra .6
.5				>LDYA L.MSG1.DPLX.FD
.6				>SYSCALL PutS

CS.RUN.DUMPNET	>PUSHW L.MSG2
				>PUSHBI 0
				>SYSCALL PrintF

				lda ETKCFG+S.ETKCFG.NodID
				beq .11
				>LDYA L.MSG2.C
				bra .10
.11				>LDYA L.MSG2.U
.10				>SYSCALL PutS

				>PUSHW L.MSG2.ADDR
				>PUSHW ETKCFG+S.ETKCFG.NetID
				>PUSHB ETKCFG+S.ETKCFG.NodID
				>PUSHBI 3
				>SYSCALL PrintF
				
				>PUSHW L.MSG2.ROUTER
				>PUSHW ETKCFG+S.ETKCFG.NetID
				>PUSHB ETKCFG+S.ETKCFG.Router
				>PUSHBI 3
				>SYSCALL PrintF
				
				>PUSHW L.MSG2.OBJECT
				>PUSHW L.ETKCFG.OBJECT
				>PUSHBI 2
				>SYSCALL PrintF
				lda #0					tell TSKMGR that all done ok, but 
				sec						we do not want to stay in memory
				rts
*--------------------------------------
CS.RUN.PROBE	>LDYA L.MSG.PROBE
				>SYSCALL PutS

				ldx #5

.20				lda ETKCFG+S.NETCFG.MAC,x
				sta AARP.PRB.SHA,x
				dex
				bpl .20
				
				lda #$FF
				sta AARP.PRB.SPA+2
				sta AARP.PRB.TPA+2

				lda #$FE
				sta AARP.PRB.SPA+1
				sta AARP.PRB.TPA+1

				lda A2osX.TIMER16
				eor A2osX.TIMER16+1
				bne .1

				inc
.1				cmp #$FE
				bcc .2
	
				eor #$80

.2				sta AARP.PRB.SPA+3
				sta AARP.PRB.TPA+3

				lda #PROBE.MAX
				sta ProbeCnt

.3				>LDYA L.AARP.PRB
				>STYA IOCTL+S.IOCTL.BUFPTR
				>LDYAI AARP.PRB.LEN
				>STYA IOCTL+S.IOCTL.BYTECNT
				
				>PUSHB ETKCFG+S.NETCFG.DevID
				>PUSHBI IOCTL.WRITE
				>PUSHW L.IOCTL
				>SYSCALL IOCTL
				
				>SLEEP
				dec ProbeCnt
				bne .3
				
				lda AARP.PRB.SPA+2
				sta ETKCFG+S.ETKCFG.NetID
				sta DDP.GetNetInfo+S.DDP2.SRC.NET
				
				lda AARP.PRB.SPA+1
				sta ETKCFG+S.ETKCFG.NetID+1
				sta DDP.GetNetInfo+S.DDP2.SRC.NET+1
				
				lda AARP.PRB.SPA+3
				sta ETKCFG+S.ETKCFG.NodID
				sta DDP.GetNetInfo+S.DDP2.SRC.NOD
				
*				clc

				rts
*--------------------------------------
CS.RUN.GETNETINFO
				>LDYA L.DDP.GetNetInfo
				>STYA IOCTL+S.IOCTL.BUFPTR
				>LDYAI DDP.GetNetInfo.LEN
				>STYA IOCTL+S.IOCTL.BYTECNT
				
				>PUSHW ETKCFG+S.NETCFG.DevID
				>PUSHBI IOCTL.WRITE
				>PUSHW L.IOCTL
				>SYSCALL IOCTL
				
				lda #TIMEOUT.MAX
				sta TimeOut
				
.1				>SLEEP

				dec TimeOut
				beq .9

				>PUSHW ETKCFG+S.NETCFG.DevID
				>PUSHBI IOCTL.READ
				>PUSHW L.IOCTL
				>SYSCALL IOCTL
				bcs .1
				
				sta hFrame
				>SYSCALL GetMemPtr
				>STYA ZPFrameBase
				
				
				
				lda hFrame
				>SYSCALL FreeMem
				
				>LDYA L.MSG.PROBE.OK
				>SYSCALL PutS
				rts
				
.9				sec				
CS.RUN.GETNETINFO.RTS
				rts
*--------------------------------------
CFG.EtcFiles	>LDYAI 256
				>SYSCALL GetMem
				bcs CS.RUN.GETNETINFO.RTS

				>STYA BufPtr
				stx CFG.hBuf1
				>LDYA L.HOSTNAME
				jsr CFG.Read.PrintF
				jsr CFG.Read.HOSTNAME

				lda ETKCFG+S.ETKCFG.OBJECT
				bne .4

				>LDYA L.ETKCFG.OBJECT
				iny
				bne .1
				inc
				
.1				>PUSHYA				
				>PUSHW L.CFG.DefaultHost
				>PUSHW A2osX.TIMER16
				>PUSHBI 2
				>SYSCALL SPrintF
				
				lda #10
				sta ETKCFG+S.ETKCFG.OBJECT
				
.4				lda CFG.hBuf1
				>SYSCALL FreeMem
				rts
*--------------------------------------
CFG.Read.PrintF pha
				>PUSHW L.MSG.CFG
				pla
				>PUSHYA
				>PUSHBI 2
				>SYSCALL PrintF
				rts
*--------------------------------------
CFG.Read.HOSTNAME
				>LDYA L.HOSTNAME
				jsr CFG.Read.Open
				bcs .9

				jsr CFG.Read.Result
				bcs .9
				
.1				jsr CFG.Read.GetS
				bcs .8
				
				lda (BufPtr)
				beq .1					empty line...
				cmp #'#
				beq .1					comment...
				
				ldy #0

.2				lda (BufPtr),y
				beq .8					end of name

				sta ETKCFG+S.ETKCFG.OBJECT+1,y
				iny
				cpy #32
				bne .2
				
.8				sty ETKCFG+S.ETKCFG.OBJECT

				clc
				.HS B0					BCS
.9				sec
				jmp CFG.Read.Close
*--------------------------------------
CFG.Read.Result	bcc .1
				pha
				>PUSHW L.MSG.CFG.KO
				pla
				>PUSHA
				>PUSHBI 1
				>SYSCALL PrintF
				sec
				rts
				
.1				>LDYA L.MSG.CFG.OK
				>SYSCALL PutS
				rts
*--------------------------------------
CFG.Read.Open	stz CFG.hCfgFile
				>PUSHYA
				>PUSHBI	O.RDONLY
				>PUSHBI S.FI.T.TXT
				>PUSHWZ
				>SYSCALL FOpen
				bcs .9

				sta CFG.hCfgFile
*				clc
.9				rts
*--------------------------------------
CFG.Read.Close	php
				pha

CFG.Read.Close2	lda CFG.hCfgFile
				beq .8

				>SYSCALL FClose

.8				pla
				plp
				rts
*--------------------------------------
CFG.Read.GetS	>PUSHB CFG.hCfgFile
				>PUSHW BufPtr
				>PUSHWI 255
				>SYSCALL FGetS
				rts
*--------------------------------------
CS.DOEVENT		lda (pEvent)
				bpl .9					is it a TIMER event?

				lda TimeOut
				beq .9

				dec TimeOut

.9				sec						do not discard TIMER event
				rts
*--------------------------------------
CS.QUIT			lda hLIBETALK
				beq .8
				>SYSCALL UnloadLib
.8				clc
				rts
*--------------------------------------
CS.END
LIBETALK		.AZ "libetalk"
hLIBETALK		.BS	1
*--------------------------------------
MSG.USAGE		.AZ "Usage : ETCONFIG\r\n"
*--------------------------------------
MSG.NODEV		.AZ "EtherTalk not bound to any device."
*--------------------------------------
MSG1.DEV		.AS "NIC Device Configuration :\r\n"
				.AS	" Device ID   : $%h\r\n"
				.AS " Device Name : %s\r\n"
				.AS " Device Type : %S\r\n"
				.AZ " HW Address  : %02h:%02h:%02h:%02h:%02h:%02h\r\n"
MSG1.DEV.ARP	.AZ " ARP Offload : %d\r\n"
MSG1.DEV.IP		.AS " IP Offload  : %d\r\n"
				.AZ " Link Status : "
MSG1.LINK.OK	.AZ "OK"
MSG1.LINK.KO	.AZ "Media Disconnected"
MSG1.LINKSPEED	.AZ " Link Speed  : 1"
MSG1.DPLX.FD	.AZ " Mbit/s,Full Duplex"
MSG1.DPLX.HD	.AZ " Mbit/s,Half Duplex"
*--------------------------------------
MSG2			.AZ "EtherTalk Configuration : "
MSG2.C			.AZ "Configured"
MSG2.U			.AZ "Not Configured"
MSG2.ADDR		.AZ " Network/Node : %D/%d\r\n"
MSG2.ROUTER		.AZ " Router       : %D/%d\r\n"
MSG2.OBJECT		.AZ " Object Name  : %S\r\n"
*--------------------------------------
MSG.CFG			.AZ "ETCONFIG:Reading %s..."
MSG.CFG.KO		.AZ "Not Found. [%h]\r\n"
MSG.CFG.OK		.AZ "OK."
*--------------------------------------
HOSTNAME		.AZ "${ROOT}etc/hostname"
*--------------------------------------
CFG.DefaultHost	.AZ "a2osx-%H"
*--------------------------------------
MSG.PROBE		.AZ "ETCONFIG:Acquiring NODE..."
MSG.PROBE.KO	.AZ "ETCONFIG:Time Out [%h].\r\n"
MSG.PROBE.OK	.AZ "ETCONFIG:Success."
*--------------------------------------
ETKCFG			.BS S.ETKCFG
CFG.hCfgFile	.BS 1
CFG.hBuf1		.BS 1
*--------------------------------------
AARP.PRB		.HS 090007FFFFFF		S.ETH.DSTMAC
AARP.PRB.SRCMAC	.BS 6
				.DA #0,#36				S.ETH.802.3.LENGTH
				.HS AA					S.ETH.802.2.DSTSAP
				.HS AA					S.ETH.802.2.SRCSAP
				.HS 03					S.ETH.802.2.CTRL
				.HS 00.00.00.80.F3		S.ETH.SNAP.PROTO
				.HS 00.01.80.9B.06.04
				.HS 0003				Probe
AARP.PRB.SHA	.BS 6
AARP.PRB.SPA	.BS 4
AARP.PRB.THA	.BS 6
AARP.PRB.TPA	.BS 4
				.BS AARP.PRB+60-*
AARP.PRB.LEN	.EQ 60
*--------------------------------------
DDP.GetNetInfo	.HS 090007FFFFFF		S.ETH.DSTMAC
				.BS 6
				.DA #0,#DDP.GetNetInfo.DEND-DDP.GetNetInfo.LLC
*--------------------------------------
DDP.GetNetInfo.LLC
				.HS AA
				.HS AA
				.HS 03
				.HS 08.00.07.80.9B
*--------------------------------------
DDP.GetNetInfo.DDP
				.DA #0,#DDP.GetNetInfo.DEND-DDP.GetNetInfo.DDP
				.DA	0					S.DDP2.CHECKSUM
				.DA 0					S.DDP2.DST.NET
				.DA 0					S.DDP2.SRC.NET
				.DA #$FF				S.DDP2.DST.NOD
				.DA #0					S.DDP2.SRC.NOD
				.DA #6					S.DDP2.DST.SKT
				.DA #6					S.DDP2.SRC.SKT
				.DA #6					S.DDP2.TYPE
*--------------------------------------
				.DA #5					ZIP.GetNetInfoReq
				.HS 00
				.HS 00.00.00.00
				.HS 00					Zone Len
DDP.GetNetInfo.DEND	.EQ *
				.BS DDP.GetNetInfo+60-*
DDP.GetNetInfo.LEN	.EQ 60
*--------------------------------------
IOCTL			.BS S.IOCTL
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
DIB				.BS S.DIB
DCB.NIC			.BS S.DCB.NIC
DS.END			.ED
*--------------------------------------
MAN
SAVE usr/src/bin/etconfig.s
ASM
