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

DevID			.BS 1
ProbeCnt		.BS 1
TimeOut			.BS 1
hFrame			.BS 1

ZPPtr1			.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.MCAST	.DA MSG2.MCAST
L.MSG2.NETRANGE	.DA MSG2.NETRANGE
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.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
L.NBP.Browse	.DA NBP.Browse
				.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.NETCFG.DevID
				lda (pETKCFG),y
				bne CS.RUN.SETUP

				>LDYA L.MSG.NODEV
				>SYSCALL PutS
				lda #MLI.E.NODEV
				sec
				rts
				
CS.RUN.SETUP	sta DevID

				lda (pETKCFG)
				bmi .7
				
				bne .1
				
				jsr CS.RUN.EtcFiles

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

.7				jsr CS.RUN.DUMP

				>PUSHW L.NBP.Browse
				>LIBCALL hLIBETALK,LIBETALK.NBP.Lookop

.8				lda #0
				sec
CS.RUN.SETUP.RTS
				rts
*--------------------------------------
CS.RUN.DUMP		>LEA.G DCB.NIC
				
				ldx #S.IOCTL.S.GETDCB
				jsr CS.RUN.IOCTL
				bcs CS.RUN.SETUP.RTS

				>LEA.G DIB
				
				ldx #S.IOCTL.S.GETDIB
				jsr CS.RUN.IOCTL
				bcs CS.RUN.SETUP.RTS
*--------------------------------------				
CS.RUN.DUMPNIC	>PUSHW L.MSG1.DEV
				
				>PUSHB DevID
				
				tax
				lda FDs.pLO-1,x
				clc
				adc #S.FD.DEV
				tay
				lda FDs.pHI-1,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.DUMPETKCFG
*--------------------------------------
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.DUMPETKCFG
				>PUSHW L.MSG2
				>PUSHBI 0
				>SYSCALL PrintF
				
				lda (pETKCFG)
				beq .11

				>LDYA L.MSG2.C
				bra .10

.11				>LDYA L.MSG2.U

.10				>SYSCALL PutS

				>PUSHW L.MSG2.ADDR
				
				ldy #S.ETKCFG.NetID+1
				lda (pETKCFG),y
				>PUSHA
				dey
				lda (pETKCFG),y
				>PUSHA
				ldy #S.ETKCFG.NodID
				lda (pETKCFG),y
				>PUSHA
				>PUSHBI 3
				>SYSCALL PrintF
				
				>PUSHW L.MSG2.ROUTER
				ldy #S.ETKCFG.NetID+1
				lda (pETKCFG),y
				>PUSHA
				dey
				lda (pETKCFG),y
				>PUSHA
				ldy #S.ETKCFG.Router
				lda (pETKCFG),y
				>PUSHA
				>PUSHBI 3
				>SYSCALL PrintF
				
				>PUSHW L.MSG2.MCAST
				
				ldy #S.ETKCFG.MCAddress+1
				
.1				>PUSHB (pETKCFG),y
				iny
				cpy #S.ETKCFG.MCAddress+7
				bcc .1
				
				>PUSHBI 6
				>SYSCALL PrintF
				
				>PUSHW L.MSG2.NETRANGE
				ldy #S.ETKCFG.NetStart+1
				lda (pETKCFG),y
				>PUSHA
				dey
				lda (pETKCFG),y
				>PUSHA
				ldy #S.ETKCFG.NetEnd+1
				lda (pETKCFG),y
				>PUSHA
				dey
				lda (pETKCFG),y
				>PUSHA
				>PUSHBI 4
				>SYSCALL PrintF

				>PUSHW L.MSG2.OBJECT
				lda pETKCFG
				clc
				adc #S.ETKCFG.Object
				tay
				lda pETKCFG+1
				adc /S.ETKCFG.Object
				>PUSHYA
				lda pETKCFG
				clc
				adc #S.ETKCFG.Zone
				tay
				lda pETKCFG+1
				adc /S.ETKCFG.Zone
				>PUSHYA
				>PUSHBI 4
				>SYSCALL PrintF

				rts
*--------------------------------------
CS.RUN.PROBE	>LDYA L.MSG.PROBE
				>SYSCALL PutS

				ldy #S.ETKCFG.MAC+5
				ldx #5

.20				lda (pETKCFG),y
				sta AARP.PRB.SHA,x
				dey
				dex
				bpl .20
				
				lda #$FF
				sta AARP.PRB.SPA+1
				sta AARP.PRB.TPA+1

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

				lda A2osX.T16
				eor A2osX.T16+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.G DEV.IOCTL+S.IOCTL.BUFPTR
				>LDYAI AARP.PRB.LEN
				>STYA.G DEV.IOCTL+S.IOCTL.BYTECNT
				
				ldx #IOCTL.WRITE
				jsr CS.RUN.IOCTL.X
				
				>SLEEP
				dec ProbeCnt
				bne .3
				
				lda AARP.PRB.SPA+2
				ldy #S.ETKCFG.NetID
				sta (pETKCFG),y
				sta DDP.GetNetInfo+S.DDP2.SRC.NET+1
				
				lda AARP.PRB.SPA+1
				iny
				sta (pETKCFG),y
				sta DDP.GetNetInfo+S.DDP2.SRC.NET
				
				lda AARP.PRB.SPA+3
				ldy #S.ETKCFG.NodID
				sta (pETKCFG),y
				sta DDP.GetNetInfo+S.DDP2.SRC.NOD
				
				lda (pETKCFG)
				inc
				sta (pETKCFG)

				rts
*--------------------------------------
CS.RUN.GETNETINFO
				>LDYA L.DDP.GetNetInfo
				>STYA.G DEV.IOCTL+S.IOCTL.BUFPTR
				>LDYAI DDP.GetNetInfo.LEN
				>STYA.G DEV.IOCTL+S.IOCTL.BYTECNT
				
				ldx #IOCTL.WRITE
				jsr CS.RUN.IOCTL.X
				bcs .9
				
				lda #TIMEOUT.MAX
				sta TimeOut
				
.1				>SLEEP

				lda (pETKCFG)
				bmi .8

				dec TimeOut
				bne .1
				
				sec
				rts
				
.8				>LDYA L.MSG.PROBE.OK
				>SYSCALL PutS

.9				rts
*--------------------------------------
CS.RUN.IOCTL	>STYA.G DEV.IOCTL+S.IOCTL.BUFPTR

				txa
				>STA.G DEV.IOCTL+S.IOCTL.S

				ldx #IOCTL.STATUS

CS.RUN.IOCTL.X	>PUSHB DevID
				txa
				>PUSHA

				>PUSHEA.G DEV.IOCTL
				>SYSCALL IOCTL
				rts
*--------------------------------------
CS.RUN.EtcFiles	>LDYAI 256
				>SYSCALL GetMem
				bcs .9

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

				ldy #S.ETKCFG.Object
				lda (pETKCFG),y
				bne .2

				lda pETKCFG
				clc
				adc #S.ETKCFG.Object+1
				tay
				lda pETKCFG+1
				adc /S.ETKCFG.Object+1
				
				>PUSHYA				
				>PUSHW L.CFG.DefaultHost
				>PUSHW A2osX.T16
				>PUSHBI 2
				>SYSCALL SPrintF
				
				lda #10
				ldy #S.ETKCFG.Object
				sta (pETKCFG),y
				
.2				lda CFG.hBuf1
				>SYSCALL FreeMem
				
.9				rts
*--------------------------------------
CFG.Read.PrintF pha
				>PUSHW L.MSG.CFG
				pla
				>PUSHYA
				>PUSHBI 2
				>SYSCALL PrintF
				rts
*--------------------------------------
CFG.Read.HOSTNAME
				lda pETKCFG
				clc
				adc #S.ETKCFG.Object
				sta ZPPtr1
				lda pETKCFG+1
				adc /S.ETKCFG.Object
				sta ZPPtr1+1

				>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

				iny
				sta (ZPPtr1),y
				cpy #32
				bne .2
				
.8				tya
				sta (ZPPtr1)

				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		.CZ "Usage : ETCONFIG\r\n"
*--------------------------------------
MSG.NODEV		.CZ "EtherTalk not bound to any device."
*--------------------------------------
MSG1.DEV		.CS "NIC Device Configuration :\r\n"
				.CS	" Device ID   : %d\r\n"
				.CS " Device Name : %s\r\n"
				.CS " Device Type : %S\r\n"
				.CZ " HW Address  : %02h:%02h:%02h:%02h:%02h:%02h\r\n"
MSG1.DEV.ARP	.CZ " ARP Offload : %d\r\n"
MSG1.DEV.IP		.CS " IP Offload  : %d\r\n"
				.CZ " Link Status : "
MSG1.LINK.OK	.CZ "OK"
MSG1.LINK.KO	.CZ "Media Disconnected"
MSG1.LINKSPEED	.CZ " Link Speed  : 1"
MSG1.DPLX.FD	.CZ " Mbit/s,Full Duplex"
MSG1.DPLX.HD	.CZ " Mbit/s,Half Duplex"
*--------------------------------------
MSG2			.CZ "EtherTalk Configuration : "
MSG2.C			.CZ "Configured"
MSG2.U			.CZ "Not Configured"
MSG2.ADDR		.CZ " Network/Node : %D/%d\r\n"
MSG2.ROUTER		.CZ " Router       : %D/%d\r\n"
MSG2.MCAST		.CZ " Multicast @  : %h:%h:%h:%h:%h:%h\r\n"
MSG2.NETRANGE	.CZ " Netrange     : %D/%D\r\n"
MSG2.OBJECT		.CZ " Object.Zone  : %S@%S\r\n"
*--------------------------------------
MSG.CFG			.CZ "ETCONFIG:Reading %s..."
MSG.CFG.KO		.CZ "Not Found. [%h]\r\n"
MSG.CFG.OK		.CZ "OK."
*--------------------------------------
HOSTNAME		.AZ "${ROOT}etc/hostname"
*--------------------------------------
CFG.DefaultHost	.CZ "a2osx-%H"
*--------------------------------------
MSG.PROBE		.CZ "ETCONFIG:Acquiring NODE..."
MSG.PROBE.KO	.CZ "ETCONFIG:Time Out [%h].\r\n"
MSG.PROBE.OK	.CZ "ETCONFIG:Success."
*--------------------------------------
CFG.hCfgFile	.BS 1
CFG.hBuf1		.BS 1
*--------------------------------------
AARP.PRB		.HS 090007FFFFFF		S.ETH.DSTMAC
				.BS 6
				.DA /AARP.PRB.DEND-AARP.PRB.LLC
				.DA #AARP.PRB.DEND-AARP.PRB.LLC
AARP.PRB.LLC
				.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
				.HS 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
AARP.PRB.DEND	.EQ *
				.BS AARP.PRB+60-*
AARP.PRB.LEN	.EQ 60
*--------------------------------------
DDP.GetNetInfo	.HS 090007FFFFFF		S.ETH.DSTMAC
				.BS 6
				.DA /DDP.GetNetInfo.DEND-DDP.GetNetInfo.LLC
				.DA #DDP.GetNetInfo.DEND-DDP.GetNetInfo.LLC
DDP.GetNetInfo.LLC
				.HS AA
				.HS AA
				.HS 03
				.HS 08.00.07
				.HS 80.9B				S.ETH.SNAP.PROTO
*--------------------------------------
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
*--------------------------------------
NBP.Browse		.PS "="
				.PS "="
				.PS "*"
				.HS 00
*--------------------------------------				
				.DUMMY
				.OR 0
DS.START
DIB				.BS S.DIB
DEV.IOCTL		.BS S.IOCTL
DCB.NIC			.BS S.DCB.NIC
DS.END			.ED
*--------------------------------------
MAN
SAVE usr/src/bin/etconfig.s
ASM
