NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
*				.TF sys/pm/pm.vedrive
				.TF sys/pm.vedrive
*--------------------------------------
				.INB inc/macros.i
				.INB inc/io.i
				.INB inc/monitor.i
				.INB inc/mli.i
				.INB inc/mli.e.i
				.INB inc/nic.i
				.INB inc/nic.91c96.i
				.INB inc/nic.8900a.i
				.INB inc/nic.w5100.i
				.INB inc/eth.i
				.INB inc/net.tcpip.i
				.INB inc/net.dhcp.i
*--------------------------------------
ZPPtr1			.EQ	$0
ZPPtr2			.EQ	$2
len				.EQ 4
idx				.EQ 5
int				.EQ 6
ZPBufPtr		.EQ 8
ZPBufCnt		.EQ 10
ZPnCnt			.EQ 12
ZPCheckSum		.EQ 14
timerS			.EQ 16
timerC			.EQ 17
bRemoteMac		.EQ 18
bD1Size			.EQ	19
bD2Size			.EQ	19
Slotn0			.EQ 21
*--------------------------------------
CONF.FBUF		.EQ $4000
CONF.LBUF		.EQ $4400
CONF.FILEPATH	.EQ $4500
FRAMEBUF		.EQ $4600
*--------------------------------------
ADT.CMD.VSD		.EQ $C5					"E": Virtual Drive Command Envelope
ADT.CMD.PING	.EQ $D9					"Y": PING
DRV.EntryPoint	.EQ $BF41
*--------------------------------------
DRV.BLK.START	.EQ $D400
DRV.NIC.START	.EQ $D800
*--------------------------------------
DRV.NIC.SendARP	.EQ DRV.NIC.START
DRV.NIC.SendUDP	.EQ DRV.NIC.START+3
DRV.NIC.Rcvd	.EQ DRV.NIC.START+6
*--------------------------------------
DRV.ARPBuf		.EQ $DA00-S.UDP-5-S.ARP
DRV.UDPBuf		.EQ $DA00-S.UDP-5		Frame Buffer = S.UDP+5+512+1 bytes
DRV.UDPBuf.Env	.EQ DRV.UDPBuf+S.UDP
DRV.UDPBuf.Cmd	.EQ DRV.UDPBuf+S.UDP+1
DRV.UDPBuf.BlkL	.EQ DRV.UDPBuf+S.UDP+2
DRV.UDPBuf.BlkH	.EQ DRV.UDPBuf+S.UDP+3
DRV.UDPBuf.Sum	.EQ DRV.UDPBuf+S.UDP+4
DRV.UDPBuf.Data	.EQ DRV.UDPBuf+S.UDP+5
*--------------------------------------
DRV.InBuf		.EQ $DA00				Frame Buffer = 1536 bytes
*--------------------------------------
VEDRIVE.Init	>LDYAI VEDRIVE.MSG0
				jsr PrintFYA

				jsr VEDRIVE.CheckDRV
				bcc .1

				>LDYAI VEDRIVE.DRVKO
				jsr PrintFYA
				rts

.1				jsr VEDRIVE.ReadConf
				bcc .2

				>LDYAI VEDRIVE.CONFINV
				jsr PrintFYA
				rts

.2				jsr VEDRIVE.CheckHW
				bcc .3

				>LDYAI VEDRIVE.NICKO
				jsr PrintFYA
				rts

.3				jsr VEDRIVE.MACInit
				lda CONF.ADDRESS
				bne .4

				>LDYAI VEDRIVE.DHCP
				jsr PrintFYA

				jsr VEDRIVE.DHCPReq
				bcc .4

				>LDYAI VEDRIVE.DHCPKO
				jsr PrintFYA
				rts

.4				jsr VEDRIVE.IPInit

				jsr VEDRIVE.ARPReq
				bcc .5

				>LDYAI VEDRIVE.IPKO
				jsr PrintFYA

				rts

.5				ldx #11

.6				lda CONF.ADDRESS,x
				pha
				dex
				bpl .6

				>LDYAI VEDRIVE.IPOK
				jsr PrintFYA

				jsr VEDRIVE.Ping
				bcc .7

				>LDYAI VEDRIVE.SRVKO
				jsr PrintFYA
				rts

.7				>LDYAI VEDRIVE.SRVOK
				jsr PrintFYA

				jsr VEDRIVE.SetSlot
				bcc .8

				>LDYAI VEDRIVE.NOSLOT
				jsr PrintFYA
				rts

.8				pha						Push slot
				pha						2 times
				>LDYAI VEDRIVE.DEVOK
				jsr PrintFYA

				jsr VEDRIVE.Install
				bcc .9

				>LDYAI VEDRIVE.DRVIKO
				jsr PrintFYA
				rts

.9				>LDYAI VEDRIVE.DRVIOK
				jsr PrintFYA

				rts
*--------------------------------------
VEDRIVE.CheckDRV
				ldx #COPYRIGHT.LEN

				sec

.1				lda DRV.EntryPoint-1,x
				eor COPYRIGHT-1,x
				bne .9

				dex
				bne .1

				clc

.9				rts
*--------------------------------------
VEDRIVE.Ping	lda CONF.PORT
				sta ADT.REQ.UDP.DST

				lda CONF.PORT+1
				sta ADT.REQ.UDP.DST+1

				>LDYAI ADT.REQ
				>STYA ZPBufPtr

				>LDYAI ADT.REQ.LEN
				>STYA ZPBufCnt

				jsr VEDRIVE.SendUDP
				bcs .9

				jsr VEDRIVE.InitTimer

.1				jsr VEDRIVE.READ
				bcc .2

				jsr VEDRIVE.Wait
				bcc .1

				rts

.2				jsr VEDRIVE.CheckADTRep
				bcc .3

				jsr VEDRIVE.CheckARPFrame
				bra .1

.3				bit bD1Size
				bmi .30
				
				ldy #S.UDP+10+4+$25
				lda (ZPBufPtr),y
				sta CONF.D1TOTALBLK
				iny
				lda (ZPBufPtr),y
				sta CONF.D1TOTALBLK+1

.30				lda #5
				sta ADT.REQ.CMD
				
				lda #$C2
				sta ADT.REQ.SUM

				>LDYAI ADT.REQ
				>STYA ZPBufPtr

				>LDYAI ADT.REQ.LEN
				>STYA ZPBufCnt

				jsr VEDRIVE.SendUDP
				bcs .9

				jsr VEDRIVE.InitTimer

.4				jsr VEDRIVE.READ
				bcc .5

				jsr VEDRIVE.Wait
				bcc .4

				rts

.5				jsr VEDRIVE.CheckADTRep
				bcc .6

				jsr VEDRIVE.CheckARPFrame
				bra .4

.6				bit bD2Size
				bmi .60
				
				ldy #S.UDP+10+4+$25
				lda (ZPBufPtr),y
				sta CONF.D2TOTALBLK
				iny
				lda (ZPBufPtr),y
				sta CONF.D2TOTALBLK+1
.60				
*				clc
.9				rts
*--------------------------------------
VEDRIVE.CheckHW	ldx CONF.NIC
				jmp (.1,x)

.1				.DA GS.Init
				.DA U1.Init
				.DA U2.Init
*--------------------------------------
VEDRIVE.SendUDP	jsr VEDRIVE.IPChecksum
				jsr VEDRIVE.UDPChecksum

VEDRIVE.SendARP	ldx CONF.NIC
				jmp (.1,x)

.1				.DA GS.WRITE
				.DA U1.WRITE
				.DA U2.WRITE
*--------------------------------------
VEDRIVE.READ	>LDYAI FRAMEBUF
				>STYA ZPBufPtr

				ldx CONF.NIC
				jmp (.1,x)

.1				.DA GS.READ
				.DA U1.READ
				.DA U2.READ
*--------------------------------------
VEDRIVE.InitTimer
				lda VBL
				sta timerS
				stz timerC
				rts
*--------------------------------------
VEDRIVE.Wait	lda VBL
				tax
				eor timerS
				bpl .8

				stx timerS

				dec timerC
				bne .8

				sec
				rts

.8				clc
				rts
*--------------------------------------
* Find 2 free slots in DEVPTRS (D1 & D2)
*--------------------------------------
VEDRIVE.SetSlot	ldx #2					Starts at Slot1

.1				lda DEVPTRS,x			Drive1
				cmp DEVPTRS				pointing to S0D1 NODEV ?
				bne .2

				lda DEVPTRS+1,x
				cmp DEVPTRS+1
				bne .2

				lda DEVPTRS+16,x		Drive2
				cmp DEVPTRS
				bne .2

				lda DEVPTRS+17,x
				cmp DEVPTRS+1
				bne .2

				lda #DRV.EntryPoint
				sta DEVPTRS,x
				sta DEVPTRS+16,x
				lda /DRV.EntryPoint
				sta DEVPTRS+1,x
				sta DEVPTRS+17,x
				txa
				asl
				asl
				asl
				ora #$0D
				ldy DEVCNT
				iny
				sta DEVLST,y			add Drv1
				ora #$80
				iny
				sta DEVLST,y			add Drv2
				sty DEVCNT
				txa
				lsr						exit with A=SLOT
				clc
				rts

.2				inx
				inx
				cpx #16
				bne .1

				rts						sec from CPX
*--------------------------------------
VEDRIVE.Install	ldx #PATCH.SIZE

.1				lda PATCH-1,x
				sta DRV.EntryPoint-1,x
				dex
				bne .1

				bit RRAMWRAMBNK2
				bit RRAMWRAMBNK2

				>LDYAI DRV.BLK
				>STYA ZPPtr1

				lda #DRV.BLK.START
				sta ZPPtr2
				lda /DRV.BLK.START
				sta ZPPtr2+1

				lda #DRV.BLK.SIZE
				eor #$ff
				sta ZPnCnt
				
				lda /DRV.BLK.SIZE
				eor #$ff
				sta ZPnCnt+1

				jsr VEDRIVE.Move

				lda Slotn0
				sta DRV.BLK.Slotn0
				
				lda CONF.SLOT
				sta DRV.BLK.Slot0n

				ldx CONF.NIC
				lda .10,x
				sta ZPPtr1

				lda .10+1,x
				sta ZPPtr1+1

				lda .20,x
				eor #$ff
				sta ZPnCnt

				lda .20+1,x
				eor #$ff
				sta ZPnCnt+1

				lda #DRV.NIC.START
				sta ZPPtr2
				lda /DRV.NIC.START
				sta ZPPtr2+1

				jsr VEDRIVE.Move

				ldx #S.ARP

.7				lda ARP.REP-1,x
				sta DRV.ARPBuf-1,x
				dex
				bne .7

				ldx #S.UDP

.8				lda ADT.REQ-1,x
				sta DRV.UDPBuf-1,x
				dex
				bne .8

				lda #ADT.CMD.VSD
				sta DRV.UDPBuf.Env

				ldx #3
				
.80				lda CONF.D1TOTALBLK,x
				sta DRV.D1TotalBlks,x
				dex
				bpl .80

				bit RROMBNK1
				clc
				rts
*--------------------------------------
.10				.DA DRV.GS
				.DA DRV.U1
				.DA DRV.U2
*--------------------------------------
.20				.DA DRV.GS.SIZE
				.DA DRV.U1.SIZE
				.DA DRV.U2.SIZE
*--------------------------------------
VEDRIVE.Move	ldy #0

.1				inc ZPnCnt
				bne .2

				inc ZPnCnt+1
				beq .8

.2				lda (ZPPtr1),y
				sta (ZPPtr2),y
				iny
				bne .1

				inc ZPPtr1+1
				inc ZPPtr2+1
				bra .1
				
.8				rts				
*--------------------------------------
* ProDOS $BE41->$BE4B Patch for switching to BANK2 (10 bytes)
*--------------------------------------
COPYRIGHT		.AS "(C)APPLE  "
COPYRIGHT.LEN	.EQ *-COPYRIGHT
*--------------------------------------
PATCH			.PH DRV.EntryPoint
				bit RRAMWRAMBNK2
				jsr DRV.BLK.START+1
				bit RRAMWRAMBNK1
				rts
				.EP
PATCH.SIZE		.EQ *-PATCH
*--------------------------------------
				.INB usr/src/shared/x.printf.s
*--------------------------------------
				.INB usr/src/sys/pm.vedrive.s.cf
				.INB usr/src/sys/pm.vedrive.s.bk
				.INB usr/src/sys/pm.vedrive.s.ip
				.INB usr/src/sys/pm.vedrive.s.gs
				.INB usr/src/sys/pm.vedrive.s.u1
				.INB usr/src/sys/pm.vedrive.s.u2
*--------------------------------------
VEDRIVE.MSG0	.CZ "VEDRIVE (ADTPro Virtual Ethernet HD) Driver\r"
VEDRIVE.DRVKO	.CZ "VEDRIVE (Or other custom Driver) Already Installed.\r"
VEDRIVE.CONF	.CZ "Checking %S...\r"
VEDRIVE.CONFKO	.CZ "Error reading VEDRIVE.CONF file.\r"
VEDRIVE.CONFSYN	.CZ "Syntax error in VEDRIVE.CONF file.\r"
VEDRIVE.CONFINV	.CZ "Invalid VEDRIVE.CONF file.\r"
VEDRIVE.NICKO	.CZ "Hardware not detected.\r"
VEDRIVE.NICOK	.CZ "%S Rev. %d.%d Found At Slot %d.\r"
VEDRIVE.DHCP	.CZ "Querying DHCP...\r"
VEDRIVE.DHCPKO	.CZ "No response from DHCP.\r"
VEDRIVE.IPKO	.CZ "Unable to setup IP configuration.\r"
VEDRIVE.IPOK	.CZ "IP config : %d.%d.%d.%d/%d.%d.%d.%d GW=%d.%d.%d.%d\r"
VEDRIVE.SRV		.CZ "Contacting ADTPro Server..."
VEDRIVE.SRVKO	.CZ "No Response From ADTPro Server.\r"
VEDRIVE.SRVOK	.CZ "ADTPro Server Is Online.\r"
VEDRIVE.NOSLOT	.CZ "No ProDOS device slot available.\r"
VEDRIVE.DEVOK	.CZ "VEDRIVE Installed 2 devices at S%d,D1 & S%d,D2.\r"
VEDRIVE.DRVIKO	.CZ "Unable to install VEDRIVE.\r"
VEDRIVE.DRVIOK	.CZ "VEDRIVE Successfully Installed.\r"
*--------------------------------------
CONF.FILENAME	.AS "vedrive.conf"
CONF.FILENAME.L	.EQ *-CONF.FILENAME
*--------------------------------------
CONF.KW			.PS "nic"
				.PS "slot"
				.PS "mac"
				.PS "address"
				.PS "netmask"
				.PS "gateway"
				.PS "server"
				.PS "port"
				.PS "blksize1"
				.PS "blksize2"
				.HS 00
*--------------------------------------
CONF.KW.NIC		.PS "lancegs"
				.PS "uthernet"
				.PS "uthernet2"
				.HS 00
*--------------------------------------
CONF.NIC		.HS FF
CONF.SLOT		.HS 00
CONF.IPCFG		.EQ *
CONF.SRCMAC		.HS 000E3A123456
CONF.ADDRESS	.HS 00000000
CONF.MASK		.HS 00000000
CONF.GATEWAY	.HS 00000000
CONF.SERVER		.HS 00000000
CONF.PORT		.DA /1977,#1977
CONF.D1TOTALBLK	.DA 1600
CONF.D2TOTALBLK .DA 1600
*--------------------------------------
MLIGETPREFIX.P	.DA #1
				.DA CONF.FILEPATH
*--------------------------------------
MLIOPEN.P		.DA #3
				.DA CONF.FILEPATH
				.DA CONF.FBUF
				.BS 1					FILE#
*--------------------------------------
MLINEWLINE.P	.DA #3
				.BS 1
				.DA #$7F
				.DA #$0D
*--------------------------------------
MLIREAD.P		.DA #4
				.BS 1
				.DA CONF.LBUF
				.DA 255
				.BS 2
*--------------------------------------
MLICLOSE.P		.DA #1
				.BS 1
*--------------------------------------
ARP.REQ			.HS FFFFFFFFFFFF		S.ETH.DSTMAC
ARP.REQ.SRCMAC	.BS 6
ARP.REQ.ETYPE	.DA /S.ETH.EII.TYPE.ARP
				.DA #S.ETH.EII.TYPE.ARP
				.HS 0001.0800.06.04
ARP.REQ.OP		.DA /S.ARP.OPERATION.REQ
				.DA #S.ARP.OPERATION.REQ
ARP.REQ.SHA		.BS 6
ARP.REQ.SPA		.BS 4
ARP.REQ.THA		.BS 6
ARP.REQ.TPA		.BS 4
*--------------------------------------
ARP.REP			.EQ *
ARP.REP.DSTMAC	.BS 6
ARP.REP.SRCMAC	.BS 6
ARP.REP.ETYPE	.DA /S.ETH.EII.TYPE.ARP
				.DA #S.ETH.EII.TYPE.ARP
				.HS 0001.0800.06.04
ARP.REP.OP		.DA /S.ARP.OPERATION.REP
				.DA #S.ARP.OPERATION.REP
ARP.REP.SHA		.BS 6
ARP.REP.SPA		.BS 4
ARP.REP.THA		.BS 6
ARP.REP.TPA		.BS 4
*--------------------------------------
DHCP.DISC		.HS FFFFFFFFFFFF		DST MAC
DHCP.DISC.SRCMAC	.BS 6
				.DA /S.ETH.EII.TYPE.IP
				.DA #S.ETH.EII.TYPE.IP
DHCP.DISC.IP	.HS 4500
				.DA /DHCP.DISC.END-DHCP.DISC.IP,#DHCP.DISC.END-DHCP.DISC.IP
				.HS 0000
				.HS 0000
				.DA #64					TTL
				.DA #S.IP.PROTOCOL.UDP
				.BS 2					IP CHECKSUM
				.HS 00000000
				.HS FFFFFFFF
*--------------------------------------
DHCP.DISC.UDP	.DA #0,#UDP.PORT.DHCPC
				.DA #0,#UDP.PORT.DHCPS
				.DA /DHCP.DISC.END-DHCP.DISC.UDP,#DHCP.DISC.END-DHCP.DISC.UDP
				.BS 2					UDP CHECKSUM
*--------------------------------------
				.HS 01010600			OP,HTYPE,HLEN,HOPS
DHCP.DISC.XID	.HS	54328574
				.HS 0000				SECS
				.DA S.DHCP.FLAGS.BRDCST
				.HS	00000000			CIADDR
DHCP.DISC.YIADDR	.HS 00000000
				.HS 00000000			SIADDR
DHCP.DISC.GIADDR	.HS 00000000
DHCP.DISC.CHADDR	.HS 00000000.00000000.00000000.00000000
				.BS 64					SNAME
				.BS 128					FILE
				.HS 63825363			COOKIE
				.HS 3501				OPT
				.DA #S.DHCP.OPTIONS.DHCPDiscover
				.HS 37020103FF			37040103060FFF
DHCP.DISC.END	.EQ *
DHCP.DISC.LEN	.EQ *-DHCP.DISC
*--------------------------------------
DHCP.REQ		.HS FFFFFFFFFFFF		DST MAC
DHCP.REQ.SRCMAC	.BS 6
				.DA /S.ETH.EII.TYPE.IP
				.DA #S.ETH.EII.TYPE.IP
DHCP.REQ.IP		.HS 4500
				.DA /DHCP.REQ.END-DHCP.REQ.IP,#DHCP.REQ.END-DHCP.REQ.IP
				.HS 0000
				.HS 0000
				.DA #64					TTL
				.DA #S.IP.PROTOCOL.UDP
				.BS 2					IP CHECKSUM
				.HS 00000000
				.HS FFFFFFFF
*--------------------------------------
DHCP.REQ.UDP	.DA #0,#UDP.PORT.DHCPC
				.DA #0,#UDP.PORT.DHCPS
				.DA /DHCP.REQ.END-DHCP.REQ.UDP,#DHCP.REQ.END-DHCP.REQ.UDP
				.BS 2					UDP CHECKSUM
*--------------------------------------
				.HS 01010600			OP,HTYPE,HLEN,HOPS
DHCP.REQ.XID	.HS	54328574
				.HS 0000				SECS
				.DA S.DHCP.FLAGS.BRDCST
				.HS	00000000			CIADDR
DHCP.REQ.YIADDR	.HS 00000000
				.HS 00000000			SIADDR
				.HS 00000000			GIADDR
DHCP.REQ.CHADDR	.HS 00000000.00000000.00000000.00000000
				.BS 64					SNAME
				.BS 128					FILE
				.HS 63825363			COOKIE
				.HS 3501				OPT
				.DA #S.DHCP.OPTIONS.DHCPRequest
				.HS 3204
DHCP.REQ.OPT.REQIP	.BS 4
				.HS 3604
DHCP.REQ.OPT.SVRIP	.BS 4
				.HS	FF
DHCP.REQ.END	.EQ *
DHCP.REQ.LEN 	.EQ *-DHCP.REQ
*--------------------------------------
ADT.REQ
ADT.REQ.DSTMAC	.BS 6					DST MAC
ADT.REQ.SRCMAC	.BS 6
				.DA /S.ETH.EII.TYPE.IP
				.DA #S.ETH.EII.TYPE.IP
ADT.REQ.IP		.HS 4500
				.DA /ADT.REQ.END-ADT.REQ.IP,#ADT.REQ.END-ADT.REQ.IP
				.HS 0000
				.HS 0000
				.DA #64					TTL
				.DA #S.IP.PROTOCOL.UDP
				.BS 2					IP CHECKSUM
ADT.REQ.IP.SRC	.BS 4					SRC IP
ADT.REQ.IP.DST	.BS 4					DST IP
*--------------------------------------
ADT.REQ.UDP		.DA #$C0,#$00			SRC PORT
ADT.REQ.UDP.DST	.BS 2					DST PORT
				.DA /ADT.REQ.END-ADT.REQ.UDP,#ADT.REQ.END-ADT.REQ.UDP
				.BS 2					UDP CHECKSUM
*--------------------------------------
				.DA #ADT.CMD.VSD
ADT.REQ.CMD		.HS 030200				READ D1, BLK 2
ADT.REQ.SUM		.HS C4
ADT.REQ.END		.EQ *
ADT.REQ.LEN 	.EQ *-ADT.REQ
*--------------------------------------
MAN
SAVE usr/src/sys/pm.vedrive.s
ASM
