NEW
  AUTO 3,1
*--------------------------------------
CURSOR.BLINK.SPEED	.EQ 2
*--------------------------------------
				.DUMMY
				.OR ZPDRV
ZPIOCTL			.BS 2
ZPDCBPtr		.BS 2
ZPBufPtr		.BS 2
ZPCount			.BS 2
ZPBufBaseL1		.BS 2
ZPBufBaseL2		.BS 2
ZPScrBaseL1		.BS 2
ZPScrBaseL2		.BS 2

ZPTmpWord		.BS 2
ZPTmpBool		.BS 1
ZPTmpChar		.BS 1
*--------------------------------------
bActive			.BS 1
bCsiInNum		.BS 1
CsiParamCnt		.BS 1
CsiParams		.BS 4
				.ED
*--------------------------------------
DRV.TERM		cld
				jmp (.1,x)
.1				.DA DRV.TERM.STATUS
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA DRV.TERM.CONTROL
				.DA A2osX.BADCALL
				.DA DRV.TERM.OPEN
				.DA DRV.TERM.CLOSE
				.DA DRV.TERM.READ
				.DA DRV.TERM.WRITE
*--------------------------------------
DRV.TERM.STATUS	jsr DRV.TERM.GetIOCTLBufCntDCB
								
				ldy #S.IOCTL.STATCODE
				lda (ZPIOCTL),y
				beq .1
				
				cmp #S.IOCTL.STATCODE.GETDIB
				bne .3
				
				ldy #S.DIB-1
				.HS 2C					bit abs
.1				ldy #3

.2				lda DRV.TERM.DIB,y
				sta (ZPBufPtr),y
				dey
				bne .2
				
				ldy #S.DCB.TTY.STATUS
				lda (ZPDCBPtr),y
				sta (ZPBufPtr)
				
				clc
				rts
				
.3				cmp #S.IOCTL.STATCODE.EOF
				bne .9

				ldy #S.DCB.TTY.STATUS
				lda (ZPDCBPtr),y
				and #S.DIB.S.OPENED
				beq DRV.TERM.OPEN.E
				
				jsr DRV.TERM.CURBLNK
				
				ldy #S.DCB.TTY.OUTTAIL
				lda (ZPDCBPtr),y
				iny
				sec
				eor (ZPDCBPtr),y		OUTHEAD
				bne .8 

				bit bActive
				bpl .7
				bit	OPENAPPLE
				bmi .7
				
				bit KBD
				bmi .8

.7				lda #$ff
				.HS 2C					BIT ABS
.8				lda #0
				clc
				rts
				
.9				lda #MLI.E.BADCTL
				sec
				rts
*--------------------------------------
DRV.TERM.OPEN	tax						DEV.ID in A

				jsr DRV.TERM.GetDCB

				ldy #S.DCB.TTY.STATUS
				lda (ZPDCBPtr),y
				bit #S.DIB.S.OPENED
				beq DRV.TERM.OPEN.1
				
DRV.TERM.OPEN.E	lda #MLI.E.OPEN
				sec
				rts
				
DRV.TERM.OPEN.1	txa				

				ldx #0
.2				ldy A2osX.SCRNDEVS,x
				beq .3
				
				inx
				cpx #K.SCR.MAX
				bne .2
				
				lda #E.OOH
				sec
				rts
				
.3				sta A2osX.SCRNDEVS,x

				ldy #S.DCB.TTY.DEVID
				sta (ZPDCBPtr),y
				
				ldy #S.DCB.TTY.STATUS
				lda (ZPDCBPtr),y
				ora #S.DIB.S.OPENED
				ora DRV.TERM.DIB+S.DIB.S
				sta (ZPDCBPtr),y
				
				jsr RESET
*--------------------------------------
DRV.TERM.CONTROL
				jsr DRV.TERM.GetDCB
				
				ldy #S.DCB.TTY.DEVID
				lda (ZPDCBPtr),y
				sta A2osX.ASCREEN

				sta SETTEXT

				jsr DRV.TERM.SCRCPY
				
				lda #0

				ldy #S.DCB.TTY.bCURON
				sta (ZPDCBPtr),y

				ldy #S.DCB.TTY.bTITLE
				sta (ZPDCBPtr),y
				
				clc
				rts
*--------------------------------------
DRV.TERM.CLOSE	jsr DRV.TERM.GetDCB

				ldy #S.DCB.TTY.STATUS
				lda (ZPDCBPtr),y
				bit #S.DIB.S.OPENED
				beq .9
				
				lda DRV.TERM.DIB+S.DIB.S
				sta (ZPDCBPtr),y
				
				ldx #0
				ldy #S.DCB.TTY.DEVID
				
.1				lda A2osX.SCRNDEVS,x
				cmp (ZPDCBPtr),y
				bne .2

				stz A2osX.SCRNDEVS,x
				
.2				inx
				cpx #K.SCR.MAX
				bne .1
				
				clc
				rts
				
.9				lda #MLI.E.IO
				sec
				rts
*--------------------------------------
DRV.TERM.READ	jsr DRV.TERM.GetIOCTLBufCntDCB
				
				bit bActive				is screen active?
				bpl .6					no....go check flush...
				
				jsr DRV.TERM.CURBLNK

				lda	OPENAPPLE
				bpl .1
				
				lda KBD
				bmi .6					Open apple key, not for us...

				jsr DRV.TERM.Title.ON
				bra .6
				
.1				jsr DRV.TERM.Title.OFF

				lda KBD
				bpl .6
				sta KBDSTROBE

				and #$7F

				ldx KeyRemapped
.2				cmp KeyRemapped,x
				beq .3
				dex 
				bne .2
				jsr Char.Out.Put		no remap....send char....
				bra .7					and flush
				
.3				lda KeyRemappedIdx,x
				tax
				
.4				lda KeyRemappedTbl,x
				beq .7					end of remap....flush
				jsr Char.Out.Put
				inx
				bra .4
	
.6				ldy #S.DCB.TTY.OUTTAIL
				lda (ZPDCBPtr),y
				iny						
				cmp (ZPDCBPtr),y		OUTHEAD
				
				beq .9					no char in output buffer...
				
.7				stz .8+1
				
.70				inc ZPCount
				bne .71
				inc ZPCount+1
				beq .8
				
.71				jsr Char.Out.Get
				bcs .8
				
				ldy .8+1
				sta (ZPBufPtr),y
				iny
				sty .8+1
				bra .70
				
.8				lda #$ff				SELF MODIFIED
				ldy #S.IOCTL.BYTECNT
				sta (ZPIOCTL),y
				iny
				lda #0
				sta (ZPIOCTL),y
				
				clc
				rts
				
.9				lda	#E.NODATA
*				sec
				rts
*--------------------------------------
DRV.TERM.WRITE	jsr DRV.TERM.GetIOCTLBufCntDCB

				jsr DRV.TERM.CUROFF
				
.1				inc ZPCount
				bne .2
				inc ZPCount+1
				beq FS.8
				
.2				ldy #S.DCB.TTY.MODE
				lda (ZPDCBPtr),y
				tax
				
				jsr .3

				inc ZPBufPtr
				bne .1
				inc ZPBufPtr+1
				bra .1

.3				jmp (.4,x)

.4				.DA DRV.TERM.COUT
				.DA DRV.TERM.ESC
				.DA DRV.TERM.CSI
				.DA DRV.TERM.IAC
				.DA DRV.TERM.G0
				.DA DRV.TERM.G1
*--------------------------------------
DRV.TERM.COUT	lda (ZPBufPtr)

				cmp #IAC
				bne .1
				
				lda #S.DCB.TTY.MODE.IAC
				jmp DRV.TERM.SETMODE
				
.1				cmp #C.DEL
				beq DRV.TERM.COUT.DEL
				cmp #C.SPACE
				bcs .8
				
				ldx #CtrlChars.Cnt-1

.2				cmp CtrlChars,x
				beq .3
				dex
				bpl .2
				clc
				rts
				
.3				txa
				asl
				tax
				jmp (.4,x)

.4				.DA ENQ
				.DA BS
				.DA LF
				.DA	DRV.TERM.COUT.FF
				.DA DRV.TERM.COUT.CR
				.DA DRV.TERM.COUT.SO
				.DA DRV.TERM.COUT.SI
				.DA DRV.TERM.COUT.FS
				.DA DRV.TERM.COUT.ESC
				
.8				jsr SetCharAtCurPos
*--------------------------------------
DRV.TERM.COUT.FS
				ldy #S.DCB.TTY.CH
				lda (ZPDCBPtr),y
				cmp #79
				bne .1

				ldy #S.DCB.TTY.bLINEWRAP
				lda (ZPDCBPtr),y
				bpl FS.8
				
				ldy #S.DCB.TTY.CH
				lda #0
				sta (ZPDCBPtr),y
				
				jmp LF
				
.1				inc
				sta (ZPDCBPtr),y
				
FS.8			clc
				rts
*--------------------------------------
DRV.TERM.COUT.DEL
				ldy #S.DCB.TTY.CH
				lda (ZPDCBPtr),y
				beq .1
				dec
				sta (ZPDCBPtr),y
				bra .3

.1				iny						S.DCB.TTY.CV
				lda (ZPDCBPtr),y
				beq FS.8
				
				dec
				sta (ZPDCBPtr),y

				dey
				lda #79
				sta (ZPDCBPtr),y		S.DCB.TTY.CH
				
.3				lda #$20
				jmp SetCharAtCurPos
*--------------------------------------
DRV.TERM.COUT.FF
				lda #0
				ldy #S.DCB.TTY.CV
				sta (ZPDCBPtr),y
				dey
				sta (ZPDCBPtr),y
				jmp DRV.TERM.CLRSCR
*--------------------------------------
DRV.TERM.COUT.CR
				lda #0
				ldy #S.DCB.TTY.CH
				sta (ZPDCBPtr),y
				clc
				rts
*--------------------------------------
DRV.TERM.COUT.SO
				sec
				.HS 90					BCC
*--------------------------------------
DRV.TERM.COUT.SI
				clc
				ldy #S.DCB.TTY.bG0G1
				ror
				sta (ZPDCBPtr),y
				clc
				rts
*--------------------------------------
DRV.TERM.COUT.ESC
				lda #S.DCB.TTY.MODE.ESC
				bra DRV.TERM.SETMODE
*--------------------------------------
DRV.TERM.ESC	lda (ZPBufPtr)			y = #S.DCB.TTY.MODE

				ldx #EscCodes.Cnt-1
				
.2				cmp EscCodes,x
				beq .3
				dex
				bpl .2
				
				lda #0
				bra DRV.TERM.SETMODE

.3				phx
				jsr .4
				plx
				lda EscModes,x
				bra DRV.TERM.SETMODE
				
.4				txa
				asl
				tax
				jmp (.5,x)
				
.5				.DA Scroll.Dn			M
				.DA Scroll.Up			D
				.DA RESET				c
				.DA DRV.TERM.SETMODE.RTS	[
				.DA DRV.TERM.SETMODE.RTS	(
				.DA DRV.TERM.SETMODE.RTS	)
*--------------------------------------			
DRV.TERM.SETMODE		
				ldy #S.DCB.TTY.MODE
				sta (ZPDCBPtr),y
				ldy #S.DCB.TTY.INBUFFER
				lda #S.DCB.TTY.INBUFFER
				sta (ZPDCBPtr),y
				clc
DRV.TERM.SETMODE.RTS				
				rts
*--------------------------------------
DRV.TERM.CSI	ldy #S.DCB.TTY.INBUFFER
				lda (ZPDCBPtr),y
				inc
				sta (ZPDCBPtr),y
				tay
				
				lda (ZPBufPtr)
				sta (ZPDCBPtr),y
				cmp #64					End of Seq ?
				bcs DRV.TERM.CSI.Exec
				
				cpy #S.DCB.TTY.INBUFFER.MAX-1	buffer full?
				bne .8
				
				jmp RESET.MODE
				
.8				clc
				rts

DRV.TERM.CSI.Exec
				stz CsiParamCnt
				stz bCsiInNum
				
				ldy #S.DCB.TTY.INBUFFER			EscSeq Ptr
				
.10				stz ZPTmpWord
				stz ZPTmpWord+1

.1				iny
				lda (ZPDCBPtr),y
				
				cmp #'0'
				bcc .2
				cmp #'9'+1
				bcs .2

				dec bCsiInNum
				
				and #$0F
				pha
				
				lda ZPTmpWord
				ldx ZPTmpWord+1
				
				asl ZPTmpWord	    param=param*10
				rol ZPTmpWord+1
				asl ZPTmpWord
				rol ZPTmpWord+1
				
				clc
				adc ZPTmpWord
				sta ZPTmpWord
				txa
				adc ZPTmpWord+1
				sta ZPTmpWord+1
				asl ZPTmpWord
				rol ZPTmpWord+1

				pla
				clc
				adc ZPTmpWord
				sta ZPTmpWord
				bcc .1
				inc ZPTmpWord+1
				bra .1

.2				bit bCsiInNum
				bpl .3

				stz bCsiInNum
				lda #255
				ldx ZPTmpWord+1
				bne .21

				lda ZPTmpWord
.21				ldx CsiParamCnt
				sta CsiParams,x
				inc CsiParamCnt

.3				lda (ZPDCBPtr),y
				cmp #';'
				beq .10

				pha
				
				jsr RESET.MODE
				
				pla
				
				ldx #CsiCodes.Cnt-1

.4				cmp CsiCodes,x
				beq .5
				dex
				bne .4
				
				clc
				rts

.5				txa
				asl
				tax
				jmp (.8,x)

.8				.DA Csi.LWEnable		h
				.DA Csi.LWDisable		l
				.DA Csi.DispAttr		m
				.DA Csi.Query			n
				.DA Csi.Scroll			r
				.DA Csi.EraseScreen		J
				.DA Csi.EraseLine		K
				.DA	Csi.Home			H
*--------------------------------------
Csi.LWEnable	sec
				.HS 90					BCC 
*--------------------------------------
Csi.LWDisable	clc
				ldx CsiParamCnt
				beq .8
				dex
				bne .8

				lda CsiParams
				eor #7
				bne .8

				ror
				ldy #S.DCB.TTY.bLINEWRAP
				sta (ZPDCBPtr),y
				
.8				clc
				rts
*--------------------------------------
Csi.DispAttr	ldx CsiParamCnt
				bne .1
				
				jmp RESETATTR

.1				lda CsiParams-1,x
				bne .2
				
				jsr RESETATTR
				bra .7

.2				eor #7
				bne .7

.3				ldy #S.DCB.TTY.bNORMAL
				sta (ZPDCBPtr),y
				
.7				dex
				bne .1

.8				clc
				rts
*--------------------------------------
Csi.Query		ldx CsiParamCnt
				beq .8
				dex
				bne .8

				lda CsiParams
				cmp #6
				bne .8
				
				lda #C.ESC
				jsr Char.Out.Put
				
				lda #'['
				jsr Char.Out.Put
				
				ldy #S.DCB.TTY.CV
				lda (ZPDCBPtr),y
				inc
				jsr Decimal.Out
				
				lda #';'
				jsr Char.Out.Put
			
				ldy #S.DCB.TTY.CH
				lda (ZPDCBPtr),y
				inc
				jsr Decimal.Out 
				
				lda #'R'
				jsr Char.Out.Put
				
.8				clc
				rts
*--------------------------------------
Csi.Scroll		ldy CsiParamCnt
				bne .1
				
				lda #0
				ldx #23
				bra .8
				
.1				cpy #2
				bne .9
				
				lda CsiParams
				dec
				ldx CsiParams+1
				dex
				
.8				ldy #S.DCB.TTY.SCROLLTOP
				sta (ZPDCBPtr),y
				iny						S.DCB.TTY.SCROLLBOT
				txa
				sta (ZPDCBPtr),y
				
.9				clc
				rts
*--------------------------------------
Csi.EraseScreen	ldx CsiParamCnt

				bne .1
				jmp DRV.TERM.CLRSCR.DN
				
.1				lda CsiParams
				cmp #1
				bne .2
				jmp DRV.TERM.CLRSCR.UP

.2				cmp #2
				bne .9
				jmp DRV.TERM.CLRSCR

.9				clc
				rts
*--------------------------------------
Csi.EraseLine	ldx CsiParamCnt

				bne .1
				jmp CLREOL
				
.1				lda CsiParams
				cmp #1
				bne .2
				jmp CLRSOL

.2				cmp #2
				bne .9
				jmp CLRLINE

.9				clc
				rts
*--------------------------------------
Csi.Home		lda CsiParamCnt
				bne .1
				
				ldy #S.DCB.TTY.CH
				sta (ZPDCBPtr),y
				iny						S.DCB.TTY.CV
				sta (ZPDCBPtr),y
				clc
				rts
				
.1				ldx #1
				lda CsiParams
				beq .2
				
				ldx #24
				cmp #24
				bcs .2
				
				tax
				
.2				dex
				txa
				ldy #S.DCB.TTY.CV
				sta (ZPDCBPtr),y
				
				dec CsiParamCnt
				beq Csi.Home.8
				
				ldx #1
				lda CsiParams+1
				beq .3
				
				ldx #80
				cmp #80
				
				bcs .3
				
				tax
				
.3				dex
				txa
				ldy #S.DCB.TTY.CH
				sta (ZPDCBPtr),y
				
Csi.Home.8		clc
				rts
*--------------------------------------
DRV.TERM.G0		ldy #S.DCB.TTY.bG0ALT
				.HS 2C					BIT ABS
DRV.TERM.G1		ldy #S.DCB.TTY.bG1ALT

				lda (ZPBufPtr)
				cmp #'0'
				beq .1
				clc
				
.1				ror				
				sta (ZPDCBPtr),y
				lda #0
				jmp DRV.TERM.SETMODE
*--------------------------------------
DRV.TERM.IAC	ldy #S.DCB.TTY.INBUFFER
				lda (ZPDCBPtr),y
				inc
				sta (ZPDCBPtr),y
				tay
				
				lda (ZPBufPtr)
				sta (ZPDCBPtr),y
				
				cpy #S.DCB.TTY.INBUFFER+1	CMD ?
				beq Csi.Home.8			yes, wait for additional bytes

				ldy #S.DCB.TTY.INBUFFER+1
				lda (ZPDCBPtr),y		get back CMD
				cmp #SB
				bne .1					not a SB/SE....stop with CMD/SUBCMD

				lda (ZPBufPtr)
				cmp #SE
				bne Csi.Home.8			wait for ending SE....keep bIACMode
				bra COUT.IAC.SB
				
				ldy #S.DCB.TTY.INBUFFER+1
				lda (ZPDCBPtr),y		get back CMD
				
.1				cmp #IAC
				beq COUT.IAC.SB.9
				
				sec
				sbc #WILL
				bcc COUT.IAC.SB.9
				asl
				tax

				ldy #S.DCB.TTY.INBUFFER+2
				lda (ZPDCBPtr),y			Get SUBCMD in A

				jmp (.8,x)

.8				.DA COUT.IAC.WILL
				.DA COUT.IAC.WONT
				.DA COUT.IAC.DO
				.DA COUT.IAC.DONT
*--------------------------------------
COUT.IAC.SB		ldy #S.DCB.TTY.INBUFFER+2
				lda (ZPDCBPtr),y			Get SUBCMD in A

				cmp #TELOPT.TTYPE
				bne COUT.IAC.SB.9
				
				iny
				lda (ZPDCBPtr),y
				cmp #SB.SEND
				bne COUT.IAC.SB.9
				
				ldx #0
				
.1				lda SB.IS.TTYPE,x
				jsr Char.Out.Put
				inx
				cpx #SB.IS.TTYPE.LEN
				bne .1

COUT.IAC.SB.9	bra COUT.IAC.EXIT
*--------------------------------------
COUT.IAC.WILL	ldx #WILLDO.CNT-1

.1				cmp WILLDO,x
				beq .7
				dex
				bpl .1
				
				ldx #WILLDONT.CNT-1

.2				cmp WILLDONT,x
				beq .8
				dex
				bpl .2
				
				bra COUT.IAC.EXIT
				
.7				ldx #DO
				.HS 2C					BIT ABS
				
.8				ldx #DONT

				bra COUT.IAC.SendAX
*--------------------------------------
COUT.IAC.DO		ldx #DOWILL.CNT-1

.1				cmp DOWILL,x
				beq .7
				dex
				bpl .1
				
				ldx #DOWONT.CNT-1

.2				cmp DOWONT,x
				beq .8
				dex
				bpl .2
				
				bra COUT.IAC.EXIT
				
.7				ldx #WILL
				.HS 2C					BIT ABS
				
.8				ldx #WONT
*--------------------------------------
COUT.IAC.SendAX
				pha						push CMD
				phx
				lda #IAC
				jsr Char.Out.Put
				pla
				jsr Char.Out.Put
				pla
				jsr Char.Out.Put
				
COUT.IAC.WONT
COUT.IAC.DONT

COUT.IAC.EXIT
*--------------------------------------
RESET.MODE		lda #0
				ldy #S.DCB.TTY.MODE
				sta (ZPDCBPtr),y
				clc
				rts
*--------------------------------------
RESET			ldy #1
				
.1				iny
				lda RESET.VALUES-2,y		
				sta (ZPDCBPtr),y
				cpy #S.DCB.TTY.INBUFFER
				bne .1
				
				jmp DRV.TERM.CLRSCR
*--------------------------------------
RESETATTR		lda #$80
				ldy #S.DCB.TTY.bNORMAL
				sta (ZPDCBPtr),y
				clc
				rts
*--------------------------------------
ENQ				ldx #0

.1				lda ENQ.String,x

				jsr Char.Out.Put
				inx
				cpx ENQ.String.Len
				bne .1
				
.8				clc
				rts
*--------------------------------------
BS 				ldy #S.DCB.TTY.CH
				lda (ZPDCBPtr),y
				beq .1

				dec
				sta (ZPDCBPtr),y
				clc
				rts

.1				ldy #S.DCB.TTY.CV
				lda (ZPDCBPtr),y
				beq .2

				dec
				sta (ZPDCBPtr),y

				lda #79
				dey
				sta (ZPDCBPtr),y

.2				clc
				rts
*--------------------------------------
LF				ldy #S.DCB.TTY.CV
				lda (ZPDCBPtr),y
				ldy #S.DCB.TTY.SCROLLBOT
				cmp (ZPDCBPtr),y
				beq SCROLL.UP
				
				inc
				ldy #S.DCB.TTY.CV
				sta (ZPDCBPtr),y
				
				clc
				rts
*--------------------------------------
SCROLL.UP		ldy #S.DCB.TTY.SCROLLTOP
				lda (ZPDCBPtr),y

				tax

.1				jsr DRV.TERM.SETUP.L1X

				inx
				
				jsr DRV.TERM.COPY.XtoL1

				txa
				ldy #S.DCB.TTY.SCROLLBOT
				cmp (ZPDCBPtr),y
				bne .1
*--------------------------------------
CLRLINE			ldy #0					Start
				ldx #80					End
				bra CLR

CLRSOL			ldy #S.DCB.TTY.CH
				lda (ZPDCBPtr),y
				tax						End
				ldy #0					Start
				
				bra CLR

CLREOL			ldy #S.DCB.TTY.CH
				lda (ZPDCBPtr),y
				tay						Start
				ldx #80					End

CLR				stx .3+1				Save End
				phy
				
				ldy #S.DCB.TTY.CV
				lda (ZPDCBPtr),y
				tax
				
				ply
				
.1				phy
				
				lda #" "
				jsr SetCharAtYX			X unmodified
				
				ply
				iny
.3				cpy #$ff				Self Modified
				bne .1
				
				clc
				rts
*--------------------------------------
SCROLL.DN		ldy #S.DCB.TTY.SCROLLBOT
				lda (ZPDCBPtr),y

				tax
				
.1				jsr DRV.TERM.SETUP.L1X

				dex
				
				jsr DRV.TERM.COPY.XtoL1

				txa
				ldy #S.DCB.TTY.SCROLLTOP
				cmp (ZPDCBPtr),y
				bne .1			

				clc
				rts
*--------------------------------------
DRV.TERM.CURBLNK
				bit bActive
				bpl .9
				
				lda A2osX.TIMER16
				and #CURSOR.BLINK.SPEED
				ldy #S.DCB.TTY.bCURON
				eor (ZPDCBPtr),y
				bne DRV.TERM.CURBLNK.1

.9				rts

DRV.TERM.CUROFF	bit bActive
				bpl DRV.TERM.CURBLNK.RTS
				
				ldy #S.DCB.TTY.bCURON
				lda (ZPDCBPtr),y
				beq	DRV.TERM.CURBLNK.RTS
				
DRV.TERM.CURBLNK.1				
				lda (ZPDCBPtr),y
				bne DRV.TERM.CURBLNK.OFF

				jsr GetCharAtCurPos
				and #$80
				eor #" "
				bra DRV.TERM.CURBLNK.SET
				
DRV.TERM.CURBLNK.OFF
				jsr GetCharAtCurPos
				
DRV.TERM.CURBLNK.SET
				pha
				ldy #S.DCB.TTY.CV
				lda (ZPDCBPtr),y
				tax
				dey
				lda (ZPDCBPtr),y
				tay						CH
				
				jsr DRV.TERM.SETUP.L1X.SCR
				
				pla
				jsr SetCharAtY.SCR
				
				ldy #S.DCB.TTY.bCURON
				lda (ZPDCBPtr),y
				eor #CURSOR.BLINK.SPEED
				sta (ZPDCBPtr),y
DRV.TERM.CURBLNK.RTS
				rts
*--------------------------------------
Decimal.Out		stz ZPTmpWord
				stz ZPTmpWord+1
				
				ldx #8
				sed
				
				tay
				
.1				tya
				asl
				tay
				lda ZPTmpWord
				adc ZPTmpWord
				sta ZPTmpWord
				lda ZPTmpWord+1
				adc ZPTmpWord+1
				sta ZPTmpWord+1
				dex
				bne .1
				
				cld
				
				stz ZPTmpBool			No leading 0
				
				lda ZPTmpWord+1
				and #$0f
				beq .2
				
				dec ZPTmpBool			non zero, print everything

				jsr Char.Out.Put30
				
.2				lda ZPTmpWord
				lsr
				lsr
				lsr
				lsr
				bne .3
				
				bit ZPTmpBool			Print this digit ?
				bpl .4
				
.3				jsr Char.Out.Put30
				
.4				lda ZPTmpWord
				and #$0f
*--------------------------------------
Char.Out.Put30	ora #$30

Char.Out.Put	pha						save char

				ldy #S.DCB.TTY.OUTHEAD
				lda (ZPDCBPtr),y
				
				pha						save actual HEAD
				inc
				
				cmp #S.DCB.TTY.OUTBUFFER.MAX
				bne .1
				
				lda #S.DCB.TTY.OUTBUFFER

.1				dey						OUTTAIL
				
				cmp (ZPDCBPtr),y		HEAD+1 = TAIL ?
				beq .9					CS, full!
				
				iny
				sta (ZPDCBPtr),y		new head
				
				ply						old head
				pla
				sta (ZPDCBPtr),y
				clc
				rts
				
.9				pla
				pla
				rts
*--------------------------------------
Char.Out.Get	ldy #S.DCB.TTY.OUTTAIL
				lda (ZPDCBPtr),y
				iny						OUTHEAD
				cmp (ZPDCBPtr),y
				beq .9					CS
				
				tay
				lda (ZPDCBPtr),y
				pha
				
				tya
				inc
				cmp #S.DCB.TTY.OUTBUFFER.MAX
				bne .1
				
				lda #S.DCB.TTY.OUTBUFFER

.1				ldy #S.DCB.TTY.OUTTAIL
				sta (ZPDCBPtr),y
				
				pla
				clc
				
.9				rts				
*--------------------------------------
DRV.TERM.GetIOCTLBufCntDCB
				>STYA ZPIOCTL

				ldy #S.IOCTL.BUFPTR
				lda (ZPIOCTL),y
				sta ZPBufPtr
				iny 
				lda (ZPIOCTL),y
				sta ZPBufPtr+1

				iny						#S.IOCTL.BYTECNT
				
				lda (ZPIOCTL),y
				eor #$ff
				sta ZPCount
				iny
				lda (ZPIOCTL),y
				eor #$ff
				sta ZPCount+1
*--------------------------------------
DRV.TERM.GetDCB	ldy #S.FD.DEV.DCBPTR
				lda (pFD),y
				sta ZPDCBPtr
				iny
				lda (pFD),y
				sta ZPDCBPtr+1

				ldy #S.DCB.TTY.DEVID
				lda (ZPDCBPtr),y
				cmp A2osX.ASCREEN
				beq .1					CS

				clc
.1				ror bActive
				rts
*--------------------------------------
DRV.TERM.Title.ON
				ldy #S.DCB.TTY.bTITLE
				lda (ZPDCBPtr),y
				bmi DRV.TERM.Title.RTS
				
				lda #$ff
				sta (ZPDCBPtr),y
				
				ldy #S.DCB.TTY.DEVID
				lda (ZPDCBPtr),y
				tax
				lda Dev.Table-1,x
				clc
				adc #S.FD.DEV
				tay
				lda Dev.Table,x
				adc /S.FD.DEV

				>STYA ZPTmpWord
				
				ldx #0
				jsr DRV.TERM.SETUP.L1X.SCR
				
				ldy #0
				
.1				lda (ZPTmpWord),y
				beq .3
				cmp #$40
				bcc .2
				
				cmp #$60
				bcs .2
				
				and #$1F				remap UPPERCASE

.2				phy
				jsr SetCharAtY.SCR
				ply
				iny
				bne .1
				
.3				lda #C.SPACE

.4				phy
				jsr SetCharAtY.SCR
				ply
				iny
				cpy #80
				bne .4
DRV.TERM.Title.RTS
				rts

DRV.TERM.Title.OFF
				ldy #S.DCB.TTY.bTITLE
				lda (ZPDCBPtr),y
				bpl DRV.TERM.Title.RTS
				
				lda #$0
				sta (ZPDCBPtr),y
				jmp DRV.TERM.LINE0CPY
*--------------------------------------
RESET.VALUES	.DA #0					S.DCB.TTY.MODE
				.DA #0					S.DCB.TTY.CH
				.DA #0					S.DCB.TTY.CV
				.DA #0					S.DCB.TTY.SCROLLTOP
				.DA #23					S.DCB.TTY.SCROLLBOT
				.DA #0					S.DCB.TTY.bCURON
				.DA #$80				S.DCB.TTY.bNORMAL
				.DA #$80				S.DCB.TTY.bLINEWRAP
				.DA #0					S.DCB.TTY.bTITLE
				.DA #0					S.DCB.TTY.bG0G1
				.DA #0					S.DCB.TTY.bG0ALT
				.DA #$80				S.DCB.TTY.bG1ALT
				.DA #S.DCB.TTY.OUTBUFFER	S.DCB.TTY.OUTTAIL
				.DA #S.DCB.TTY.OUTBUFFER	S.DCB.TTY.OUTHEAD
				.DA #S.DCB.TTY.INBUFFER	S.DCB.TTY.INBUFFER			
*--------------------------------------
CtrlChars		.DA #C.ENQ,#C.BS,#C.LF,#C.FF,#C.CR,#C.SO,#C.SI,#C.FS,#C.ESC
CtrlChars.Cnt	.EQ *-CtrlChars
*--------------------------------------
EscCodes		.AZ "MDc[()"
EscCodes.Cnt	.EQ *-EscCodes
EscModes		.DA #0,#0,#0,#S.DCB.TTY.MODE.CSI,#S.DCB.TTY.MODE.G0,#S.DCB.TTY.MODE.G1
CsiCodes		.AS "hlmnrJKH"
CsiCodes.Cnt	.EQ *-CsiCodes
*--------------------------------------
KeyRemapped		.HS 05080A0B0D15		L,D,U,CR,R
KeyRemappedIdx	.HS 050004080C0F
KeyRemappedTbl	.EQ *
KeyRemappedLEFT	.HS 1B5B4400			esc[D
KeyRemappedDOWN	.HS 1B5B4200			esc[B
KeyRemappedUP	.HS 1B5B4100			esc[A
KeyRemappedCR	.HS 0D0A00				crlf
KeyRemappedRGHT	.HS 1B5B4300			esc[C
*--------------------------------------
WILLDO			.DA #TELOPT.BINARY,#TELOPT.SGA
WILLDO.CNT		.EQ *-WILLDO
WILLDONT		.DA #TELOPT.TSPEED,#TELOPT.LINEMODE,#TELOPT.STATUS
WILLDONT.CNT	.EQ *-WILLDONT
DOWILL			.DA #TELOPT.BINARY,#TELOPT.SGA,#TELOPT.TTYPE,#TELOPT.NAWS
DOWILL.CNT		.EQ *-DOWILL
DOWONT			.DA #TELOPT.ECHO,#TELOPT.NEWENVIRON,#TELOPT.XDISPLOC,#TELOPT.TSPEED,#TELOPT.LFLOW
DOWONT.CNT		.EQ *-DOWONT
SB.IS.TTYPE		.DA #IAC,#SB,#TELOPT.TTYPE,#SB.IS
ENQ.String		.AS "xterm"
ENQ.String.Len	.EQ *-ENQ.String
				.DA #IAC,#SE
SB.IS.TTYPE.LEN	.EQ *-SB.IS.TTYPE
SB.IS.NAWS		.DA #IAC,#SB,#TELOPT.NAWS,#SB.IS
				.DA 80
				.DA 24
				.DA #IAC,#SE
SB.IS.NAWS.LEN	.EQ *-SB.IS.NAWS
*--------------------------------------
DRV.TERM.DIB	.DA #S.DIB.S.WRITE+S.DIB.S.READ
				.DA #0,#0,#0
				>PSTR "A2osX VT100 TERM"
				.DA #S.DIB.T.CHAR
				.DA #0
				.DA K.VER
*--------------------------------------
MAN
SAVE USR/SRC/SYS/KERNEL.S.TERM
LOAD USR/SRC/SYS/KERNEL.S
ASM
