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
ZPTmpPtr1		.BS 2
ZPTmpPtr2		.BS 2
ZPTmpPtr3		.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 #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+1
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	jsr CS.RUN.PROBE

				jsr CS.RUN.DUMP

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

				>PUSHW L.IOCTL
				>PUSHBI IOCTL.STATUS
				lda ETKCFG+S.NETCFG.DevID
				>SYSCALL IOCTL
				
CS.RUN.DUMPNIC	ldy #DCB.NIC+S.DCB.NIC.MAC+5
				ldx #6
				
.1				lda (pData),y
				>PUSHA
				dey
				dex 
				bne .1

				>PUSHEA.G DIB+S.DIB.IDS 

				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
				
				lda ETKCFG+S.NETCFG.DevID
				>PUSHA
				>PUSHBI 11
				>LDYA L.MSG1.DEV
				>SYSCALL printf
				
				>LDA.G DCB.NIC+S.DCB.NIC.FLAGS
				and #S.DCB.NIC.FLAGS.ARPOFFLOAD
				beq .2
				
				lda #1
				
.2				>PUSHA
				>PUSHBI 1	
				>LDYA L.MSG1.DEV.ARP
				>SYSCALL printf
				
				>LDA.G DCB.NIC+S.DCB.NIC.FLAGS
				and #S.DCB.NIC.FLAGS.IPOFFLOAD

				beq .3

				lda #1
.3
				>PUSHA		
				>PUSHBI 1	
				>LDYA L.MSG1.DEV.IP
				>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
				
				>PUSHBI 0
				>LDYA L.MSG1.LINKSPEED
				>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	>PUSHBI 0
				>LDYA L.MSG2
				>SYSCALL printf
				lda ETKCFG+S.ETKCFG.NodID
				beq .11
				
				>LDYA L.MSG2.C
				bra .10
				
.11				>LDYA L.MSG2.U
				
.10				>SYSCALL puts

				>PUSHB ETKCFG+S.ETKCFG.NodID
				>PUSHW ETKCFG+S.ETKCFG.NetID

				>PUSHBI 3
				>LDYA L.MSG2.ADDR
				>SYSCALL printf
				
				>PUSHB ETKCFG+S.ETKCFG.Router
				>PUSHW ETKCFG+S.ETKCFG.NetID
				
				>PUSHBI 3
				>LDYA L.MSG2.ROUTER
				>SYSCALL printf
				
				>PUSHW L.ETKCFG.OBJECT

				>PUSHBI 2
				>LDYA L.MSG2.OBJECT
				>SYSCALL printf
				
				lda #0					tell TSKMGR that all done ok, but 
				sec						we do not want to stay in memory
				rts
*--------------------------------------
CS.RUN.PROBE	>PUSHBI 0
				>LDYA L.MSG.PROBE
				>SYSCALL printf

				ldx #5

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

*				lda A2osX.RANDOM16+1
*				lda #$06
				sta AARP.PRB.SPA+1
				sta AARP.PRB.TPA+1
				sta ETKCFG+S.ETKCFG.NetID+1

				lda A2osX.RANDOM16
				eor A2osX.RANDOM16+1
*				lda #$3A
				bne .1
				inc
				
.1				cmp #$FE
				bcc .2
	
				eor #$80
				bne .2
				inc
				
.2				sta AARP.PRB.SPA+3
				sta AARP.PRB.TPA+3
				sta ETKCFG+S.ETKCFG.NodID
				
				lda #PROBE.MAX
				>STA.G PROBE.CNT
				
.3				>LDYA L.AARP.PRB
				>STYA IOCTL+S.IOCTL.BUFPTR
				>LDYAI AARP.PRB.LEN
				>STYA IOCTL+S.IOCTL.BYTECNT
				>PUSHW L.IOCTL
				>PUSHBI IOCTL.WRITE
				lda ETKCFG+S.NETCFG.DevID
				>SYSCALL IOCTL
				
				>SLEEP
				
				>DEC.G PROBE.CNT
				bne .3
				
				>LDYA L.DDP.GetNetInfo
				>STYA IOCTL+S.IOCTL.BUFPTR
				>LDYAI DDP.GetNetInfo.LEN
				>STYA IOCTL+S.IOCTL.BYTECNT
				>PUSHW L.IOCTL
				>PUSHBI IOCTL.WRITE
				lda ETKCFG+S.NETCFG.DevID
				>SYSCALL IOCTL
				
				>LDYA L.MSG.PROBE.OK
				>SYSCALL puts
				
				clc
				rts
*--------------------------------------
CFG.EtcFiles	>LDYAI 256
				>SYSCALL GetMem
				bcc .1
				rts
				
.1				>STYA ZPTmpPtr1
				stx CFG.hBuf1
				
				>LDYA L.HOSTNAME
				jsr CFG.Read.PrintF
				jsr CFG.Read.HOSTNAME

				lda ETKCFG+S.ETKCFG.OBJECT
				bne .4

				>PUSHW A2osX.TIMER16
				>PUSHBI 2
				>PUSHW L.CFG.DefaultHost
				>LDYA L.ETKCFG.OBJECT
				>SYSCALL sprintf
				lda #11
				sta ETKCFG+S.ETKCFG.OBJECT
				
.4				>PUSHW L.ETKCFG
				>LIBCALL hLIBETALK,LIBETALK.SETCFG

				lda CFG.hBuf1
				>SYSCALL freemem
								
.9				rts				
*--------------------------------------
CFG.Read.PrintF >PUSHYA
				>PUSHBI 2
				>LDYA L.MSG.CFG
				>SYSCALL printf
				rts
*--------------------------------------
CFG.Read.HOSTNAME
				>LDYA L.HOSTNAME
				jsr CFG.Read.Open
				jsr CFG.Read.Result
				bcs .9
				
.1				jsr CFG.Read.GetS
				bcs .8
				
				lda (ZPTmpPtr1)
				beq .1					empty line...
				cmp #'#
				beq .1					comment...
				
				ldy #0

.2				lda (ZPTmpPtr1),y
				beq .8					end of name
				sta ETKCFG+S.ETKCFG.OBJECT,y
				
				iny
				cpy #32
				bne .2
				
.8				tya
				sta ETKCFG+S.ETKCFG.OBJECT
				
				clc
				.HS B0					BCS
.9				sec
				jmp CFG.Read.Close
*--------------------------------------
CFG.Read.Result	bcc .1
				>PUSHA
				>PUSHBI 1
				>LDYA L.MSG.CFG.KO
				>SYSCALL printf
				sec
				rts
				
.1				>LDYA L.MSG.CFG.OK
				>SYSCALL puts
				rts
*--------------------------------------
CFG.Read.Open	stz CFG.hCfgFile
				
				pha
				
				>PUSHWZ
				>PUSHBI S.FI.T.TXT
				>PUSHBI	O.RDONLY
				
				pla
				>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	>PUSHWI 255
				>PUSHW ZPTmpPtr1
				lda CFG.hCfgFile
				>SYSCALL fgets
				rts
*--------------------------------------
CS.DOEVENT		lda (pEvent)
				bpl .9					is it a TIMER event?
				
				>LDA.G TimeOut
				beq .9
				
				dec
				sta (pData),y
				
.9				sec						do not discard TIMER event
				rts
*--------------------------------------
CS.QUIT			lda hLIBETALK
				beq .8
				>SYSCALL UnloadLib
				
.8				clc
				rts
*--------------------------------------
Init.Timeout	ldy #TimeOut
				lda #TIMEOUT.MAX
				sta (pData),y
				rts
*--------------------------------------
Wait.TimeOut	ldy #TimeOut
				lda (pData),y
				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,#36
				.HS AA
				.HS AA
				.HS 03
				.HS 08.00.07.80.9B
				.DA #0,#6				S.DDP2.DATALEN
				.DA	0					S.DDP2.CHECKSUM
				.DA $FF00
				.DA $1234
				.DA #$FF
				.DA #$FF
				.DA #6					S.DDP2.DST.SKT
				.DA #0
				.DA #6					S.DDP2.TYPE
*--------------------------------------
				.DA #5					ZIP.GetNetInfoReg
				.HS 00.00.00.00
				.HS 00					Zone Len
				.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
PROBE.CNT		.BS 1
TimeOut			.BS 1
DS.END			.ED
*--------------------------------------
MAN
SAVE USR/SRC/SBIN/ETCONFIG.S
ASM
