NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR $2000
				.TF drv/pppssc.drv
*--------------------------------------
DBG				.EQ 1
HWFC			.EQ 0
SWFC			.EQ 1		
INBUF.SIZE		.EQ 2048
*--------------------------------------
				.INB inc/macros.i
				.INB inc/a2osx.i
				.INB inc/kernel.i
				.INB inc/mli.e.i
				.INB inc/com.i
				.INB inc/com.6551.i
				.INB inc/nic.i
				.INB inc/eth.i
				.INB inc/net.tcpip.i
				.INB inc/libtcpip.i
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPArgsPtr		.BS 2
ZPPtr1			.BS 2
ZPPtr2			.BS 2
ZPPtr3			.BS 2

ZPBufLen		.BS 2
FCS				.BS 2

DWORD			.BS 4

hETCFILE		.BS 1
b7D				.BS 1
bLCP			.BS 1
bIPCP			.BS 1
Len				.BS 1

ZS.END			.ED
*--------------------------------------
*			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.ETCFILE		.DA ETCFILE
L.MSG.ETCFILE	.DA MSG.ETCFILE
L.MSG.UNEXPECTED	.DA MSG.UNEXPECTED
L.MSG.SYNTAX	.DA MSG.SYNTAX
L.MSG.SSCInit	.DA MSG.SSCInit
L.MSG.LCPInit	.DA MSG.LCPInit
L.MSG.IPCPInit	.DA MSG.IPCPInit
L.MSG.LocalIP	.DA MSG.LocalIP
L.MSG.RemoteIP	.DA MSG.RemoteIP
				.DO DBG=1
L.MSG.DUMPBUF	.DA MSG.DUMPBUFS
				.DA MSG.DUMPBUFR
L.MSG.HEXDUMP	.DA MSG.HEXDUMP
				.FIN
L.LCP.ConfReq	.DA LCP.ConfReq
L.LCP.EchoRep	.DA LCP.EchoRep
L.IPCP.ConfReq	.DA IPCP.ConfReq
L.IN.Buf		.DA IN.Buf
L.IN.Bufp1		.DA IN.Buf+1
L.OUT.Buf		.DA OUT.Buf
J.LCP			.DA LCP.IN.ConfReq
				.DA LCP.IN.ConfAck
				.DA LCP.IN.ConfNak
				.DA LCP.Discard
				.DA LCP.Discard
				.DA LCP.Discard
				.DA LCP.Discard
				.DA LCP.Discard
				.DA LCP.IN.EchoReq
				.DA LCP.Discard
				.DA LCP.Discard
				.DA LCP.Discard
				.DA LCP.Discard
				.DA LCP.Discard
				.DA LCP.Discard
J.IPCP			.DA IPCP.IN.ConfReq
				.DA IPCP.IN.ConfAck
				.DA IPCP.IN.ConfNak
				.DA IPCP.Discard
				.DA IPCP.Discard
				.DA IPCP.Discard
				.DA IPCP.Discard
				.DA 0					End Of Reloc Table
*--------------------------------------
Dev.Detect		>STYA ZPArgsPtr

				>LDYA L.MSG.DETECT
				>SYSCALL PutS
				stz ZPPtr1

				lda #$C1
				sta ZPPtr1+1

.1				and #$0f
				tay
				lda A2osX.S,y
				bne .3

				ldx #DEVSIG.Length-1

.2				ldy DEVSIG.Offset,x
				lda (ZPPtr1),y
				cmp DEVSIG.Value,x
				bne .3

				dex
				bpl .2

				bra .4

.3				inc FD.DEV.NAME+3
				inc ZPPtr1+1			no match, try next slot....
				lda ZPPtr1+1
				cmp #$C8
				bne .1

				>LDYA L.MSG.DETECT.KO
				>SYSCALL PutS

				lda #MLI.E.NODEV		Not Found in any slot, exiting
				sec
				rts

.4				lda ZPPtr1+1
				asl
				asl
				asl
				asl
				clc
				adc #$8F
				sta DEVSLOTn08F
*--------------------------------------
				jsr SSC.Init
				bcs .9

				jsr PPP.ETCFILE
				bcs .9

				jsr PPP.Init
				bcs .9

				>PUSHW L.MSG.DETECT.OK
				>PUSHW L.FD.DEV.NAME
				>PUSHBI 2
				>SYSCALL PrintF
				bcs .9

				>PUSHWI DRV.END
				>PUSHWI DRV.CS.END-DRV.CS.START
				>PUSHWI DRV.CS.START
				>LDYA L.DRV.CS.START
				>SYSCALL InsDrv
.9				bcs .99

				>STYA FD.DEV+S.FD.DEV.DRVPTR

				>PUSHW L.FD.DEV
				>PUSHW L.FD.DEV.NAME
				>SYSCALL MKDev
				bcs .99

				lda FD.DEV.NAME+3
				and #$0F
				tax
				lda #A2osX.S.NIC
				sta A2osX.S,x

*				clc

				rts

.99				pha
				jsr SSC.Close
				pla

*				sec

Dev.Detect.RTS	rts
*--------------------------------------
PPP.Init		>LDYA L.MSG.LCPInit
				>SYSCALL PutS
				
				>LDYA L.LCP.EchoRep		Pre calculate EchoRep FCS
				jsr PPP.FCS16.Set

				>LDYA L.LCP.ConfReq
				jsr PPP.FCS16.Set

				.DO DBG=1
				>LDYA L.LCP.ConfReq
				ldx #0
				jsr DumpBuf.PPP
				.FIN

				>LDYA L.LCP.ConfReq
				jsr SSC.WritePPP
				bcs .9

.1				jsr SSC.ReadPPP
				bcs .9

				tya
				beq .1

				sty ZPBufLen
				stz ZPBufLen+1

				.DO DBG=1
				>LDYA L.IN.Buf
				ldx #2
				jsr DumpBuf.PPP
				.FIN

				jsr PPP.IN.JUMP
				bcs .9

				bit bLCP
				bpl .1
				
				>LDYA L.MSG.IPCPInit
				>SYSCALL PutS
				
				jsr IPCP.OUT.ConfReq
				bcs .9
				
.2				jsr SSC.ReadPPP
				bcs .9

				tya
				beq .2

				sty ZPBufLen
				stz ZPBufLen+1
				
				.DO DBG=1
				>LDYA L.IN.Buf
				ldx #2
				jsr DumpBuf.PPP
				.FIN

				jsr PPP.IN.JUMP
				bcs .9

				bit bIPCP
				bpl .2
				
*				clc
				rts

.9				jsr SSC.Close
				
				lda #MLI.E.IO
*				sec
				rts
*--------------------------------------
PPP.IN.JUMP		lda IN.Buf+3
				cmp #$21
				bne LCP.Discard

				lda IN.Buf+2
				cmp #$C0
				bne .1

				lda IN.Buf+4
				beq LCP.Discard

				cmp #16
				bcs LCP.Discard

				asl
				tax
				jmp (J.LCP-2,x)

.1				cmp #$80
				bne LCP.Discard

				lda IN.Buf+4
				beq LCP.Discard

				cmp #8
				bcs LCP.Discard

				asl
				tax
				jmp (J.IPCP-2,x)
*--------------------------------------
LCP.Discard
IPCP.Discard
				clc
				rts
*--------------------------------------
LCP.IN.ConfReq	ldy #0

.1				lda IN.Buf,y			FF03C021
				sta OUT.Buf,y
				iny
				cpy #4
				bne .1

				lda #2					Configure-Ack
				sta OUT.Buf,y

				iny						ID

				lda IN.Buf,y
				sta OUT.Buf,y

				iny						Len MSB

				lda IN.Buf,y
				sta OUT.Buf,y
				sta	ZPBufLen+1

				iny						Len LSB

				lda IN.Buf,y
				sta OUT.Buf,y
				sta	ZPBufLen

				sec
				sbc #4					minus LEN LEN ... FCS FCS
				tax

				iny						First option ID

.2				lda IN.Buf,y
				cmp #6					ACCM ?
				bne .7

				phy
				phx

				ldx #4

.3				lda IN.Buf+2,y
				sta DWORD-1,x
				iny
				dex
				bne .3

*				ldx	#0

				ldy #0
				
.4				lda #1

.5				and DWORD,y
				bne .6

				stz ACCM.OUT,x

.6				inx
				asl
				bne .5
				
				iny
				cpy #4
				bne .4


				plx
				ply

.7				jsr LCP.Ack.Copy
				bne .2

				lda ZPBufLen
				clc
				adc #6
				sta ZPBufLen
				bcc .8

				inc ZPBufLen+1

.8				>LDYA L.OUT.Buf
				jsr PPP.FCS16.Set

				.DO DBG=1
				>LDYA L.OUT.Buf
				ldx #0
				jsr DumpBuf.PPP
				.FIN

				>LDYA L.OUT.Buf
				jmp SSC.WritePPP
*--------------------------------------
LCP.Ack.Copy	txa
				sec
				sbc IN.Buf+1,y

				pha

				ldx IN.Buf+1,y

.1				lda	IN.Buf,y
				sta OUT.Buf,y
				iny
				dex
				bne .1

				plx
				rts
*--------------------------------------
LCP.Ack.Next	txa
				sec
				sbc IN.Buf+1,y
				tax

				tya
				clc
				adc IN.Buf+1,y
				tay

				rts
*--------------------------------------
LCP.IN.ConfAck	sec
				ror bLCP

				clc
				rts
*--------------------------------------
LCP.IN.ConfNak

				clc
				rts
*--------------------------------------
LCP.IN.EchoReq	.DO DBG=1
				>LDYA L.LCP.EchoRep
				ldx #0
				jsr DumpBuf.PPP
				.FIN

				>LDYA L.LCP.EchoRep
				jmp SSC.WritePPP
*--------------------------------------
IPCP.IN.ConfReq	ldy #0

.1				lda IN.Buf,y			FF038021
				sta OUT.Buf,y
				iny
				cpy #4
				bne .1

				lda #2					Configure-Ack
				sta OUT.Buf,y

				iny						ID

				lda IN.Buf,y
				sta OUT.Buf,y

				iny						Len MSB

				lda IN.Buf,y
				sta OUT.Buf,y
				sta	ZPBufLen+1

				iny						Len LSB

				lda IN.Buf,y
				sta OUT.Buf,y
				sta	ZPBufLen

				sec
				sbc #4					minus LEN LEN ... FCS FCS
				tax

				iny						First option ID

.2				lda IN.Buf,y
				cmp #3					IP ADDR ?
				bne .7

				phy
				phx

				>PUSHW L.MSG.RemoteIP

				ldx #0

.3				lda IN.Buf+2,y
				sta DCB.GW,x
				>PUSHA
				iny
				inx
				cpx #4
				bne .3
				
				>PUSHBI 4
				>SYSCALL PrintF

				plx
				ply

.7				jsr LCP.Ack.Copy
				bne .2

				lda ZPBufLen
				clc
				adc #6
				sta ZPBufLen
				bcc .8

				inc ZPBufLen+1

.8				>LDYA L.OUT.Buf
				jsr PPP.FCS16.Set

				.DO DBG=1
				>LDYA L.OUT.Buf
				ldx #0
				jsr DumpBuf.PPP
				.FIN
				
				>LDYA L.OUT.Buf
				jmp SSC.WritePPP
*--------------------------------------
IPCP.IN.ConfAck	sec
				ror bIPCP

*				clc
				rts
*--------------------------------------
IPCP.IN.ConfNak	ldy #10
				ldx #0

				>PUSHW L.MSG.LocalIP

.1				lda IN.Buf,y
				sta IPCP.ConfReq,y
				sta DCB.IP,x
				>PUSHA
				inx
				iny
				cpy #14
				bne .1

				>PUSHBI 4
				>SYSCALL PrintF
*--------------------------------------
IPCP.OUT.ConfReq
				>LDYA L.IPCP.ConfReq
				jsr PPP.FCS16.Set

				.DO DBG=1
				>LDYA L.IPCP.ConfReq
				ldx #0
				jsr DumpBuf.PPP
				.FIN

				>LDYA L.IPCP.ConfReq
				jmp SSC.WritePPP
*--------------------------------------
SSC.Init		>LDYA L.MSG.SSCInit
				>SYSCALL PutS
				
				jsr SSC.Close

				ldy #0

.10				lda SSC.STATUS-$8F,x
				bit #SSC.STATUS.nDCD+SSC.STATUS.nDSR	true when 0
				bne .20
				
				iny
				bne .10
				
*				bra .9

.20				lda #SSC.CTL.1S+SSC.CTL.8D+SSC.CTL.CLKINT+SSC.CTL.B115200
				sta SSC.CTL-$8F,x

				lda #SSC.CMD.NOP+SSC.CMD.TE+SSC.CMD.RIRQDIS+SSC.CMD.DTR
				sta SSC.CMD-$8F,x

				ldy #0

.1				lda SSC.STATUS-$8F,x
				bit #SSC.STATUS.nDCD+SSC.STATUS.nDSR	true when 0
				beq .2
				
				iny
				bne .1

.9				lda #MLI.E.IO
				sec
				rts

.2				lda SSC.DATA-$8F,x			discard any garbage byte

				.DO SWFC=1
				lda #C.XOFF
				sta SSC.DATA-$8F,x
				.FIN

				.DO HWFC=1
				lda #SSC.CMD.NOP+SSC.CMD.RIRQDIS+SSC.CMD.DTR
				sta SSC.CMD-$8F,x
				.FIN

				clc
				rts
*--------------------------------------
SSC.Close		ldx DEVSLOTn08F
				stz SSC.RESET-$8F,x

				lda #SSC.CMD.RIRQDIS
				sta SSC.CMD-$8F,x		Applewin Bug ???

				rts
*--------------------------------------
SSC.ReadPPP		php
				sei

				ldx DEVSLOTn08F

				.DO SWFC=1
				lda #C.XON
				sta SSC.DATA-$8F,x
				.FIN

				.DO HWFC=1
				lda #SSC.CMD.NOP+SSC.CMD.TE+SSC.CMD.RIRQDIS+SSC.CMD.DTR
				sta SSC.CMD-$8F,x
				.FIN

				stz b7D

.10				ldy #0

.1				lda SSC.STATUS-$8F,x
				bit #SSC.STATUS.nDCD	true when 0
				bne .9

				bit #SSC.STATUS.RDRF	incoming char?
				bne .3

				iny
				bne .1

.8	
				.DO SWFC=1
				lda #C.XOFF
				sta SSC.DATA-$8F,x
				.FIN

				.DO HWFC=1
				lda #SSC.CMD.NOP+SSC.CMD.RIRQDIS+SSC.CMD.DTR
				sta SSC.CMD-$8F,x
				.FIN

				plp
				clc
				rts

.3				lda SSC.DATA-$8F,x

				bit b7D
				bmi .4

				cmp #$7D
				bne .40

				ror b7D
				bra .1

.4				stz b7D
				eor #%00100000
				
.40				cmp #$FF
				bne .10

				sta IN.Buf
				ldy #1

				stz b7D

.5				lda #SSC.STATUS.RDRF

.50				bit SSC.STATUS-$8F,x	incoming char?
				beq .50

				lda SSC.DATA-$8F,x
				bit b7D
				bmi .6

				cmp #$7D
				bne .7

				ror b7D
				bra .5

.6				stz b7D
				eor #%00100000

				sta IN.Buf,y
				iny
				bra .5

.7				cmp #$7E
				beq .8

				sta IN.Buf,y
				iny
				bne .5

.9				lda	#MLI.E.IO
				plp
				sec
.99				rts
*--------------------------------------
SSC.WritePPP	>STYA ZPPtr1

				ldy #7
				lda (ZPPtr1),y			Len LO
				clc
				adc #6
				sta ZPPtr3

				php
				sei

				ldx DEVSLOTn08F

				lda SSC.STATUS-$8F,x
				bit #SSC.STATUS.nDCD	true when 0
				bne .9

				.DO HWFC=1
				lda #SSC.CMD.NOP+SSC.CMD.TE+SSC.CMD.RIRQDIS+SSC.CMD.DTR
				sta SSC.CMD-$8F,x
				.FIN
				
				lda #SSC.STATUS.TDRE

.1				bit SSC.STATUS-$8F,x 	Register Empty ?
				beq .1

				lda #$7E
				sta SSC.DATA-$8F,x

.2				lda (ZPPtr1)

				cmp #$7D
				beq .5

				cmp #$7E
				beq .5

				cmp #32
				bcs .6
				
				tay
				lda ACCM.OUT,y
				beq .53
				
.5				lda #SSC.STATUS.TDRE
				
.51				bit SSC.STATUS-$8F,x 	Register Empty ?
				beq .51

				lda #$7D
				sta SSC.DATA-$8F,x
				
				lda #SSC.STATUS.TDRE
				
.52				bit SSC.STATUS-$8F,x 	Register Empty ?
				beq .52				

				lda (ZPPtr1)
				eor #%00100000
				bra .6
				
.53				lda (ZPPtr1)

.6				sta SSC.DATA-$8F,x
				
				inc ZPPtr1
				bne .60
				
				inc ZPPtr1+1
				
.60				dec ZPPtr3
				bne .2

				lda #SSC.STATUS.TDRE

.7				bit SSC.STATUS-$8F,x 	Register Empty ?
				beq .7

				lda #$7E
				sta SSC.DATA-$8F,x

				lda #SSC.STATUS.TDRE
				
.8				bit SSC.STATUS-$8F,x 	Register Empty ?
				beq .8
				
				.DO HWFC=1
				lda #SSC.CMD.NOP+SSC.CMD.RIRQDIS+SSC.CMD.DTR
				sta SSC.CMD-$8F,x
				.FIN

				plp
				clc
				rts

.9				lda	#MLI.E.IO

				plp
				sec
				rts
*--------------------------------------
				.DO DBG=1
DumpBuf.PPP		>STYA ZPPtr1

				stz ZPPtr2
				stz ZPPtr2+1

				ldy #7
				lda (ZPPtr1),y			Len LO
				clc
				adc #6
				sta ZPPtr3

				dey
				lda (ZPPtr1),y			Len HI
				adc #0
				sta ZPPtr3+1

				>PUSHW L.MSG.DUMPBUF,x
				>PUSHW ZPPtr3
				>PUSHBI 2
				>SYSCALL PrintF
				bcs .9

.1				>PUSHW L.MSG.HEXDUMP
				>PUSHW ZPPtr2

				ldy #0

.2				>PUSHB (ZPPtr1),y
				iny
				cpy #16
				bne .2

				>PUSHBI 18
				>SYSCALL PrintF
				bcs .9

				lda ZPPtr1
				adc #16
				sta ZPPtr1
				lda ZPPtr1+1
				adc #0
				sta ZPPtr1+1

				lda ZPPtr2
				adc #16
				sta ZPPtr2
				bcs .8
				lda ZPPtr2+1
				adc #0
				sta ZPPtr2+1

				lda ZPPtr3
				sec
				sbc #16
				sta ZPPtr3
				lda ZPPtr3+1
				sbc #0
				sta ZPPtr3+1
				bcc .9

				ora ZPPtr3
				bne .1

.8				clc

.9				rts
				.FIN
*--------------------------------------
PPP.FCS16.Check	sec
				.HS 90					BCC
PPP.FCS16.Set	clc
				php

				>STYA ZPPtr1

				lda #$ff
				sta FCS
				sta FCS+1

				ldy #7
				lda (ZPPtr1),y			Len LO
				clc
				adc #4					add FF 00 XX XX but dont include FCS FCS
				eor #$ff
				sta ZPPtr2

				dey
				lda (ZPPtr1),y			Len HI
				adc #0
				eor #$ff
				sta ZPPtr2+1

				ldy #0

.1				inc ZPPtr2
				bne .2

				inc ZPPtr2+1
				beq .7

.2				lda (ZPPtr1),y
				iny
				bne .3

				inc ZPPtr1+1

.3				eor FCS
				tax

				lda FCS.LO,x
				eor FCS+1
				sta FCS

				lda FCS.HI,x
				sta FCS+1

				bra .1

.7				plp
				bcc .8					CC = set

				lda FCS
				eor #$ff
				eor (ZPPtr1),y
				bne .9

				iny

				lda FCS+1
				eor #$ff
				eor (ZPPtr1),y
				bne .9

				clc
				rts

.8				lda FCS
				eor #$ff
				sta (ZPPtr1),y

				iny

				lda FCS+1
				eor #$ff
				sta (ZPPtr1),y

.9				rts
*--------------------------------------
				.INB usr/src/drv/pppssc.drv.s.e
*--------------------------------------
CS.END
*--------------------------------------
DEVSIG.Offset	.HS 05070B0C
DEVSIG.Value	.HS 38180131
DEVSIG.Length	.EQ DEVSIG.Value-DEVSIG.Offset
MSG.DETECT		.CZ "PPP Driver For Apple SSC."
MSG.DETECT.OK	.CZ "PPP Installed As Device : %s\r\n"
MSG.DETECT.KO	.CZ "No SSC Found."
ETCFILE			.AZ "${ROOT}etc/ppp.conf"
*--------------------------------------
MSG.ETCFILE		.CZ "Executing %s File...\r\n"
MSG.UNEXPECTED	.CZ "\r\nERR:Unexpected reply From Modem\r\n{%s}\r\n"
MSG.SYNTAX		.CZ "\r\nERR:Bad Syntax In ppp.conf\r\n{%s}\r\n"
MSG.SSCInit		.CZ "Connecting..."
MSG.LCPInit		.CZ "LCP Negotiation..."
MSG.IPCPInit	.CZ "IPCP Negotiation..."
MSG.LocalIP		.CZ "Local IP is %d.%d.%d.%d\r\n"
MSG.RemoteIP	.CZ "Remote IP is %d.%d.%d.%d\r\n"
				.DO DBG=1
MSG.DUMPBUFS	.CZ ">>Send:%H\r\n"
MSG.DUMPBUFR	.CZ "Recv<<:%H\r\n"
MSG.HEXDUMP		.CZ "%H:%h %h %h %h %h %h %h %h:%h %h %h %h %h %h %h %h\r\n"
				.FIN
*--------------------------------------
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 "eth1"
*--------------------------------------
LCP.ConfReq		.HS FF03
				.HS C021				LCP
LCP.ConfReq.DATA
				.HS 01					Configure-Request
				.HS FF					ID
				.DA /LCP.ConfReq.LEN,#LCP.ConfReq.LEN
*				.HS 01.04				MRU
*				.DA /1400,#1400
				.DO SWFC=1
				.HS 02.06.000A0000		ACCM
				.ELSE
				.HS 02.06.00000000		ACCM
				.FIN
				.HS 05.06.EA4C2B41		MAGIC
*				.HS 07.02
*				.HS 08.02
LCP.ConfReq.LEN	.EQ *-LCP.ConfReq.DATA
			.BS 2						FCS
*--------------------------------------
IPCP.ConfReq	.HS FF03
				.HS 8021				IPCP
IPCP.ConfReq.DATA
				.HS 01					Configure-Request
				.HS FF					ID
				.DA /IPCP.ConfReq.LEN,#IPCP.ConfReq.LEN
*				.HS 02.06.00.2D.0F.00	VJ
				.HS 03.06.00.00.00.00	IP Address
IPCP.ConfReq.LEN	.EQ *-IPCP.ConfReq.DATA
			.BS 2						FCS
*--------------------------------------
IN.Buf			.BS 256
OUT.Buf			.BS 256
*--------------------------------------
				.INB usr/src/drv/pppssc.drv.s.d
*--------------------------------------
MAN
SAVE usr/src/drv/pppssc.drv.s
ASM
