NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF DRV/UTHERNET.DRV
*--------------------------------------
				.INB INC/MACROS.I
				.INB INC/A2OSX.I
				.INB INC/MLI.E.I
				.INB INC/NIC.I
				.INB INC/NIC.8900A.I
				.INB INC/ETH.I
*--------------------------------------
ZPArgPtr		.EQ ZPDRV
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp Dev.Detect			cld,jmp abs=DRV
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					DRV Layout Version 1
				.DA 0
				.DA CS.END-CS.START			Code Length
				.DA 0
				.DA 0
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
L.MSG.DETECT	.DA MSG.DETECT
L.MSG.DETECT.OK	.DA MSG.DETECT.OK
L.MSG.DETECT.KO	.DA MSG.DETECT.KO
L.DRV.CS.START	.DA DRV.CS.START
L.FD.DEV		.DA FD.DEV
L.FD.DEV.NAME	.DA FD.DEV.NAME
L.SSCANF.MAC	.DA SSCANF.MAC
L.MAC0			.DA DCB+S.DCB.NIC.MAC
L.MAC1			.DA DCB+S.DCB.NIC.MAC+1
L.MAC2			.DA DCB+S.DCB.NIC.MAC+2
L.MAC3			.DA DCB+S.DCB.NIC.MAC+3
L.MAC4			.DA DCB+S.DCB.NIC.MAC+4
L.MAC5			.DA DCB+S.DCB.NIC.MAC+5
				.DA 0					End Of Reloc Table
*--------------------------------------
Dev.Detect		>STYA ARGS
				
				>LDYA L.MSG.DETECT
				>SYSCALL puts
				
				ldx #$70
				ldy #7

.1				lda A2osX.S,y		IO based detection, avoid scanning in Disk Controller IO!!!!
				bne .2
				
				lda /PP.ID
				sta PacketPagePTR+1,x
				lda #PP.ID
				sta PacketPagePTR,x

				lda PacketPageDATA+1,x
				cmp	/DEVID
				bne	.2
				
				lda PacketPageDATA,x
				cmp	#DEVID
				beq	.3
				
.2				dec FD.DEV.NAME+3
				txa
				sec
				sbc #$10
				tax
				dey
				bne .1
				
				>LDYA L.MSG.DETECT.KO
				>SYSCALL puts

				lda #MLI.E.NODEV
				sec
				rts
				
.3				stx DEVSLOTx0
				lda #A2osX.S.NIC
				sta A2osX.S,y
				
				lda /PP.REVISION		Get Revision# of ths chip...
				sta PacketPagePTR+1,x
				lda #PP.REVISION
				sta PacketPagePTR,x
				
				lda PacketPageDATA+1,x
				sta DIB+S.DIB.VERSION+1	set Hardware version
				lda PacketPageDATA,x
				sta DIB+S.DIB.VERSION
				
				jsr Dev.ParseArgs
				bcs .9
				
.8				>PUSHW L.FD.DEV.NAME
				>PUSHBI 2
				>LDYA L.MSG.DETECT.OK
				>SYSCALL printf
				
				>PUSHWI DRV.END
				>PUSHWI DRV.CS.END
				>PUSHWI DRV.CS.START
				>LDYA L.DRV.CS.START
				>SYSCALL InsDrv
				bcs .9
				>STYA FD.DEV+S.FD.DEV.DRVPTR
				
				>PUSHW L.FD.DEV.NAME
				>LDYA L.FD.DEV
				>SYSCALL MKDEV

.9				rts
*--------------------------------------
Dev.ParseArgs	>LDYA ARGS
				>STYA ZPArgPTR
				lda (ZPArgPTR)
				bne .1
				
				lda A2osX.RANDOM16
				eor A2osX.TIMER16
				sta DCB+S.DCB.NIC.MAC+3
				eor A2osX.RANDOM16+1
				sta DCB+S.DCB.NIC.MAC+4
				eor A2osX.TIMER16+1
				sta DCB+S.DCB.NIC.MAC+5
				clc
				rts
				
.1				>PUSHW L.MAC5
				>PUSHW L.MAC4
				>PUSHW L.MAC3
				>PUSHW L.MAC2
				>PUSHW L.MAC1
				>PUSHW L.MAC0
				>PUSHBI 12				6 x byte PTRs
				>PUSHW L.SSCANF.MAC
				>LDYA ZPArgPtr
				>SYSCALL sscanf
				bcc .8
				
				lda #E.SYN
				sec
.8				rts
*--------------------------------------
CS.END
ARGS			.BS 2
MSG.DETECT		.AZ "UtherNet/CS8900A Driver."
MSG.DETECT.OK	.AZ "UtherNet/CS8900A Installed As Device : %s\r\n"
MSG.DETECT.KO	.AZ "Hardware Not Found."
SSCANF.MAC		.AZ "%h:%h:%h:%h:%h:%h"
*--------------------------------------
FD.DEV			.DA #S.FD.T.CDEV
				.DA #0					HANDLER
				.DA #0					BUSID
				.DA #0					DEVID
				.DA 0					BUSPTR
				.BS 2					DRVPTR
				.DA 0					DCBPTR
				.DA 0					BUFPTR
FD.DEV.NAME		.AZ "ETH7"
*--------------------------------------
*			Driver Code
*--------------------------------------
ZPIOCTL			.EQ ZPDRV
ZPBufPtr		.EQ ZPDRV+2
Counter			.EQ ZPDRV+4
*--------------------------------------
DRV.CS.START	cld
				jmp (.1,x)
.1				.DA STATUS
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA OPEN
				.DA CLOSE
				.DA READ
				.DA WRITE
				.DA 0					end or relocation
*--------------------------------------
STATUS			>STYA ZPIOCTL

				ldy #S.IOCTL.STATCODE
				lda (ZPIOCTL),y
				beq .1
				
				cmp #S.IOCTL.STATCODE.GETDIB
				bne STATUS.DCB
				
				ldx #S.DIB-1
				.HS 2C					bit abs
.1				ldx #3

				ldy #S.IOCTL.BUFPTR
				lda (ZPIOCTL),y
				sta .3+1
				iny
				lda (ZPIOCTL),y
				sta .3+2
				
.2				lda DIB,x
.3				sta $ffff,x				SELF MODIFIED
				dex
				bpl .2
				
				clc
				rts

STATUS.DCB		cmp #S.IOCTL.STATCODE.GETDCB
				bne STATUS.9

				stz DCB+S.DCB.NIC.LINK
				
				ldx DEVSLOTx0
				
				lda /PP.LineST
				sta PacketPagePTR+1,x
				lda #PP.LineST
				sta PacketPagePTR,x

				lda PacketPageDATA+1,x
				lda PacketPageDATA,x
				and #PP.LineST.LinkOK
				beq .1
				
				lda #S.DCB.NIC.LINK.OK
				tsb DCB+S.DCB.NIC.LINK
	
				lda /PP.TestCTL
				sta PacketPagePTR+1,x
				lda #PP.TestCTL
				sta PacketPagePTR,x
				
				lda PacketPageDATA+1,x
				ldy PacketPageDATA,x
				and /PP.TestCTL.FDX
				beq .1
				
				lda #S.DCB.NIC.LINK.FD
				tsb DCB+S.DCB.NIC.LINK
				
.1				ldy #S.IOCTL.BUFPTR
				lda (ZPIOCTL),y
				sta .3+1
				iny
				lda (ZPIOCTL),y
				sta .3+2
				
				ldx #S.DCB.NIC-1
				
.2				lda DCB,x
.3				sta $ffff,x				SELF MODIFIED
				dex
				bpl .2

				clc
				rts
				
STATUS.9		lda #MLI.E.BADCTL
				sec
				rts				
*--------------------------------------
OPEN			lda #S.DIB.S.OPENED
				bit DIB+S.DIB.S
				bne .9
				
				jsr CLOSE

*				ldx DEVSLOTx0			Done by CLOSE

				lda /PP.RxCTL
				sta PacketPagePTR+1,x
				lda #PP.RxCTL
				sta PacketPagePTR,x
				
				lda /PP.RxCTL.LID+PP.RxCTL.RxOKA+PP.RxCTL.IA+PP.RxCTL.BroadcastA+PP.RxCTL.RuntA
				sta PacketPageDATA+1,x
				lda #PP.RxCTL.LID+PP.RxCTL.RxOKA+PP.RxCTL.IA+PP.RxCTL.BroadcastA+PP.RxCTL.RuntA
				sta PacketPageDATA,x

				lda /PP.MAC
				sta PacketPagePTR+1,x
				lda #PP.MAC
				sta PacketPagePTR,x

				>LDYA DCB+S.DCB.NIC.MAC
				>STYA PacketPageDATA,x
				>LDYAI PP.MAC+2
				>STYA PacketPagePTR,x
				>LDYA DCB+S.DCB.NIC.MAC+2
				>STYA PacketPageDATA,x
				>LDYAI PP.MAC+4
				>STYA PacketPagePTR,x
				>LDYA DCB+S.DCB.NIC.MAC+4
				>STYA PacketPageDATA,x
				
				lda /PP.LineCTL
				sta PacketPagePTR+1,x
				lda #PP.LineCTL
				sta PacketPagePTR,x

				lda /PP.LineCTL.LID+PP.LineCTL.SerRxOn+PP.LineCTL.SerTxOn+PP.LineCTL.10BaseT
				sta PacketPageDATA+1,x
				lda #PP.LineCTL.LID+PP.LineCTL.SerRxOn+PP.LineCTL.SerTxOn+PP.LineCTL.10BaseT
				sta PacketPageDATA,x
				
				lda #S.DIB.S.OPENED
				tsb DIB+S.DIB.S
				
				clc
				rts
				
.9				lda #MLI.E.OPEN
				sec
				rts
*--------------------------------------
CLOSE			ldx DEVSLOTx0
				
				lda /PP.SelfCTL
				sta PacketPagePTR+1,x
				lda #PP.SelfCTL
				sta PacketPagePTR,x
				
				lda /PP.SelfCTL.LID+PP.SelfCTL.RESET
				sta PacketPageDATA+1,x

				lda #PP.SelfCTL.LID+PP.SelfCTL.RESET
				sta PacketPageDATA,x
				
				lda /PP.SelfST
				sta PacketPagePTR+1,x
				lda #PP.SelfST
				sta PacketPagePTR,x
				
				ldy #0
				
.1				lda PacketPageDATA+1,x
				lda PacketPageDATA,x
				and #PP.SelfST.InitDone
				
				bne .8
				
				iny
				bne .1
				
.8				lda #S.DIB.S.OPENED
				trb DIB+S.DIB.S
				
				clc
				rts
*--------------------------------------
READ			php
				sei

				>STYA ZPIOCTL

				ldx DEVSLOTx0
				
				lda /PP.RxEvent
				sta PacketPagePTR+1,x
				lda #PP.RxEvent
				sta PacketPagePTR,x

				lda PacketPageDATA+1,x
				ldy PacketPageDATA,x
				and /PP.RxEvent.RxOK+PP.RxEvent.IA+PP.RxEvent.Brdcast+PP.RxEvent.Runt
				beq READWRITE.9

				lda RTDATA+1,x			discard RxStatus
				lda RTDATA,x
				
				lda RTDATA+1,x			get RxLength
				ldy #S.IOCTL.BYTECNT+1
				
				sta (ZPIOCTL),y
				pha
				lsr
				pha
				
				lda RTDATA,x
				dey
				sta (ZPIOCTL),y
				tay

				ror
				adc #0
				eor #$ff
				sta Counter

				pla
				adc #0
				eor #$ff
				sta Counter+1

				pla
				
				>SYSCALL2 getmem
				bcs READWRITE.99
				
				>STYA ZPBufPtr
				phx
				
				phy
				ldy #S.IOCTL.BUFPTR+1
				sta (ZPIOCTL),y
				dey
				pla
				sta (ZPIOCTL),y
				
				ldx DEVSLOTx0

				ldy #0

.2				inc Counter
				bne .3
				inc Counter+1
				beq .8
				
.3				lda RTDATA,x
				sta	(ZPBufPtr),y
				iny
				
				lda RTDATA+1,x
				sta	(ZPBufPtr),y
				iny
				bne .2
				inc ZPBufPtr+1
				bra .2
				
.8				pla						hMem

				plp
				clc
				rts
*--------------------------------------
READWRITE.9		lda #E.NODATA

READWRITE.99	plp
				sec
				rts
*--------------------------------------
WRITE			php
				sei
				
				>STYA ZPIOCTL

				ldx DEVSLOTx0
				
*				lda /PP.BufEvent		
*				sta PacketPagePTR+1,x
*				lda #PP.BufEvent		
*				sta PacketPagePTR,x

*				lda	PacketPageDATA+1,x
*				ldy	PacketPageDATA,x
*				and /PP.BufEvent.Rdy4Tx
*				beq .9

				lda /PP.TxCMD.LID+PP.TxCMD.TxStartFULL
				sta TxCMD+1,x
				lda #PP.TxCMD.LID+PP.TxCMD.TxStartFULL
				sta TxCMD,x
				
				ldy #S.IOCTL.BYTECNT+1
				lda (ZPIOCTL),y
				sta TxLength+1,x
				lsr
				pha

				dey
				lda (ZPIOCTL),y
				sta TxLength,x
				ror
				adc #0
				eor #$ff
				sta Counter

				pla
				adc #0
				eor #$ff
				sta Counter+1

				lda /PP.BusST		
				sta PacketPagePTR+1,x
				lda #PP.BusST		
				sta PacketPagePTR,x

				lda	PacketPageDATA+1,x
				ldy	PacketPageDATA,x
				and /PP.BusST.Rdy4TxNOW
				beq READWRITE.9
				
				ldy #S.IOCTL.BUFPTR
				lda (ZPIOCTL),y
				sta ZPBufPtr
				iny
				lda (ZPIOCTL),y
				sta ZPBufPtr+1
				
				ldx #5
				ldy #S.ETH.SRCMAC+5
				
.10				lda DCB+S.DCB.NIC.MAC,x
				sta (ZPBufPtr),y
				dey
				dex
				bpl .10

				ldx DEVSLOTx0

				ldy #0

.1				inc Counter
				bne .2
				inc Counter+1
				beq .8
				
.2				lda	(ZPBufPtr),y
				sta RTDATA,x
				iny
				bne .3
				inc ZPBufPtr+1
				
.3				lda	(ZPBufPtr),y
				sta RTDATA+1,x
				iny
				bne .1
				inc ZPBufPtr+1
				bne .1
				
.8				plp
				clc
				rts
*--------------------------------------
DRV.CS.END
DEVSLOTx0		.BS 1
*--------------------------------------
DIB				.DA #0
				.DA #0,#0,#0			size
				>PSTR "Uthernet/CS8900A"
				.DA #S.DIB.T.NIC
				.BS 1					Subtype
				.BS 2					Version
*--------------------------------------
DCB				.DA #S.DCB.T.NIC
				.BS 1					FLAGS
				.BS 1					LINK
				.DA #S.DCB.NIC.SPEED.10
				.HS 000E3A123456		MAC
				.BS 12					IP/MASK/GW
*--------------------------------------
DRV.END
MAN
SAVE USR/SRC/DRV/UTHERNET.DRV.S
ASM
