NEW
  AUTO 3,1
*--------------------------------------
CURSOR.BLINK.SPEED	.EQ 2
*--------------------------------------
				.DUMMY
				.OR ZPDRV
ZPIOCTL			.BS 2
ZPDCBPtr		.BS 2
ZPBufPtr		.BS 2
ZPBufBase		.BS 2

ZPBufBaseL1		.BS 2
ZPBufBaseL2		.BS 2
ZPScrBaseL1		.BS 2
ZPScrBaseL2		.BS 2
*--------------------------------------
ZPCount			.BS 2

ZPTmpBool		.BS 1
ZPTmpChar		.BS 1

bActive			.BS 1
CsiHeader		.BS 1
CsiParamCnt		.BS 1
CsiParams		.BS 4
				.ED
*--------------------------------------
TERMX			.DA TERMX.STATUS
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA TERMX.CONTROL
				.DA A2osX.BADCALL
				.DA TERMX.OPEN
				.DA TERMX.CLOSE
				.DA TERMX.READ
				.DA TERMX.WRITE
*--------------------------------------
TERMX.STATUS	beq .1

				cmp #S.IOCTL.STATCODE.GETDIB
				bne .3

				ldy #S.DIB-1
				.HS 2C					bit abs
.1				ldy #3

				sta CLRWRITEAUX

.2				lda TERMX.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

				jsr TERMX.ISOPENED
				beq TERMX.OPEN.E

				jsr TERMX.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
				.HS 2C					BIT ABS
TERMX.OPEN.E	lda #MLI.E.OPEN
				sec
				rts
*--------------------------------------
TERMX.OPEN		tax
				jsr TERMX.ISOPENED
				bne TERMX.OPEN.E

TERMX.OPEN.1	txa
				ldx #0

.2				ldy DevMgr.SCRNDEVS,x
				beq .3

				inx
				cpx #K.TTY.MAX+1
				bne .2

				lda #E.OOH
				sec
				rts

.3				sta DevMgr.SCRNDEVS,x

				sta (ZPDCBPtr)			#S.DCB.TTY.DEVID

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

				sta SETTEXT

				jsr TERMX.SCRCPY
				lda #0

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

				iny						#S.DCB.TTY.bTITLE
				sta (ZPDCBPtr),y
				clc
				rts
*--------------------------------------
TERMX.CLOSE		jsr TERMX.ISOPENED	
				beq .9

				lda TERMX.DIB+S.DIB.S
				sta (ZPDCBPtr),y
				ldx #0

.1				lda DevMgr.SCRNDEVS,x
				cmp (ZPDCBPtr)			#S.DCB.TTY.DEVID
				bne .2

				stz DevMgr.SCRNDEVS,x
				clc
				rts
				
.2				inx
				cpx #K.TTY.MAX+1
				bne .1

.9				lda #MLI.E.IO
				sec
				rts
*--------------------------------------
TERMX.ISOPENED	ldy #S.DCB.TTY.STATUS
				lda (ZPDCBPtr),y
				bit #S.DIB.S.OPENED
				rts
*--------------------------------------
TERMX.READ		bit bActive				is screen active?
				bpl .6					no....go check flush...
				jsr TERMX.CURBLNK

				lda	OPENAPPLE
				bpl .1

				jsr TERMX.Title.ON

				bra .6					Open apple key, not for us...

.1				jsr TERMX.Title.OFF

				lda KBD
				sta A2osX.RANDOM16
				bpl .6
				sta KBDSTROBE

				and #$7F
				cmp #C.ESC
				bne .10
				
				lda #3

.10				ldx #KeyRemapped.cnt-1

.2				cmp KeyRemapped,x
				beq .3

				dex 
				bpl .2

				cmp #C.CR
				bne .21

				jsr Char.Out.Put
				ldy #S.DCB.TTY.bCRLF
				lda (ZPDCBPtr),y
				bpl .7

				lda #C.LF
.21				jsr Char.Out.Put		no remap....send char....
				bra .7					and flush

.3				lda KeyRemapped.Tbl,x
				pha
				lda #C.ESC
				jsr Char.Out.Put
				lda #'['
				jsr Char.Out.Put
				pla
				jsr Char.Out.Put
				bra .7
	
.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 CLRWRITEAUX
				sta (ZPBufPtr),y
				sta SETWRITEAUX
				iny
				sty .8+1
				bra .70

.8				lda #$ff				SELF MODIFIED
				
				sta CLRWRITEAUX
				
				ldy #S.IOCTL.BYTECNT
				sta (ZPIOCTL),y
				iny
				lda #0
				sta (ZPIOCTL),y
				clc
				rts

.9				lda	#E.NODATA
*				sec
				rts
*--------------------------------------
TERMX.WRITE		jsr TERMX.CUROFF

.1				inc ZPCount
				bne .2

				inc ZPCount+1
				beq .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 TERMX.COUT
				.DA TERMX.ESC
				.DA TERMX.CSI
				.DA TERMX.IAC
				.DA TERMX.G0
				.DA TERMX.G1

.8				clc
				rts
*--------------------------------------
TERMX.COUT		jsr TERM.GetBufByte

				cmp #IAC
				bne .1

				lda #S.DCB.TTY.MODE.IAC
				jmp TERMX.SETMODE

.1				cmp #C.DEL
				beq TERMX.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 TERMX.COUT.BS
				.DA TERMX.COUT.LF
				.DA	TERMX.COUT.FF
				.DA TERMX.COUT.CR
				.DA TERMX.COUT.SO
				.DA TERMX.COUT.SI
				.DA TERMX.COUT.FS
				.DA TERMX.COUT.ESC
*--------------------------------------
.8				jsr SetCharAtCurPos
*--------------------------------------
TERMX.COUT.FS	ldy #S.DCB.TTY.CH
				lda (ZPDCBPtr),y
				inc
				sta (ZPDCBPtr),y
				cmp #80
				bcc TERMX.COUT.BS.8

				ldy #S.DCB.TTY.bLINEWRAP
				lda (ZPDCBPtr),y
				bpl TERMX.COUT.BS.8
				
				jsr TERMX.COUT.CR
				
				jmp TERMX.COUT.LF.1
*--------------------------------------
TERMX.COUT.BS	ldy #S.DCB.TTY.CH
				lda (ZPDCBPtr),y
				beq TERMX.COUT.BS.1

				dec
				sta (ZPDCBPtr),y

TERMX.COUT.BS.8	clc
				rts

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

				dec
				sta (ZPDCBPtr),y

				lda #79
				dey
				sta (ZPDCBPtr),y

.2				clc
				rts
*--------------------------------------
TERMX.COUT.DEL	ldy #S.DCB.TTY.CH
				lda (ZPDCBPtr),y
				beq .1

				dec
				sta (ZPDCBPtr),y
				bra .3

.1				ldy #S.DCB.TTY.CV
				lda (ZPDCBPtr),y
				beq TERMX.COUT.BS.8
				dec
				sta (ZPDCBPtr),y

				ldy #S.DCB.TTY.CH
				lda #79
				sta (ZPDCBPtr),y		S.DCB.TTY.CH
.3				lda #C.SPACE
				jmp SetCharAtCurPos
*--------------------------------------
TERMX.COUT.FF	jsr HOME
				jmp TERMX.CLRSCR
*--------------------------------------
TERMX.COUT.CR	lda #0
				ldy #S.DCB.TTY.CH
				sta (ZPDCBPtr),y
				clc
				rts
*--------------------------------------
TERMX.COUT.SO	sec
				.HS 90					BCC
*--------------------------------------
TERMX.COUT.SI	clc
				ldy #S.DCB.TTY.bG0G1
				ror
				sta (ZPDCBPtr),y
				clc
				rts
*--------------------------------------
TERMX.COUT.ESC	lda #S.DCB.TTY.MODE.ESC
				bra TERMX.SETMODE
*--------------------------------------
TERMX.ESC		jsr TERM.GetBufByte		y = #S.DCB.TTY.MODE

				ldx #EscCodes.Cnt-1
.2				cmp EscCodes,x
				beq .3
				dex
				bpl .2

				bra TERMX.SETMODE0

.3				phx
				jsr .4
				plx
				lda EscModes,x
				bra TERMX.SETMODE

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

.5				.DA SaveCurPos			7
				.DA RestoreCurPos		8
				.DA Scroll.Dn			M
				.DA Scroll.Up			D
				.DA RESET				c
				.DA TERMX.SETMODE.RTS	[
				.DA TERMX.SETMODE.RTS	(
				.DA TERMX.SETMODE.RTS	)
*--------------------------------------			
TERMX.SETMODE0	lda #0
TERMX.SETMODE	ldy #S.DCB.TTY.MODE
				sta (ZPDCBPtr),y
				ldy #S.DCB.TTY.INBUFFER
				lda #S.DCB.TTY.INBUFFER
				sta (ZPDCBPtr),y
				clc
TERMX.SETMODE.RTS
				rts
*--------------------------------------
TERMX.CSI		ldy #S.DCB.TTY.INBUFFER
				lda (ZPDCBPtr),y
				inc
				sta (ZPDCBPtr),y
				tay
				
				jsr TERM.GetBufByte
				
				sta (ZPDCBPtr),y
				cmp #64					End of Seq ?
				bcs TERMX.CSI.Exec
				cpy #S.DCB.TTY.INBUFFER.MAX-1	buffer full?
				bne .8

				jsr RESET.MODE

.8				clc
				rts

TERMX.CSI.Exec	stz CsiHeader
				stz CsiParamCnt

				lda #S.DCB.TTY.INBUFFER+1		EscSeq Ptr
				clc
				adc ZPDCBPtr
				sta ZPPtr2

				lda /S.DCB.TTY.INBUFFER+1
				adc ZPDCBPtr+1
				sta ZPPtr2+1

				lda (ZPPtr2)
				cmp #'?'
				bne .2

				sta CsiHeader

.1				jsr SHARED.NextCharPtr2

.2				jsr MATH.Dec2ACC32
				bcs .5

				jsr SHARED.AddYToPtr2
				lda #255
				ldx ACC32+1
				bne .3

				lda ACC32

.3				ldx CsiParamCnt
				sta CsiParams,x
				inc CsiParamCnt

				lda (ZPPtr2)
				cmp #';'
				beq .1

.5				jsr RESET.MODE

				lda (ZPPtr2)
				ldx #CsiCodes.Cnt-1

.6				cmp CsiCodes,x
				beq .7
				
				dex
				bpl .6
				
				clc
				rts

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

.8				.DA Csi.SetMode			h
				.DA Csi.ResetMode		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.SetMode		sec
				.HS 90					BCC 
*--------------------------------------
Csi.ResetMode	clc

				ldx CsiParamCnt
				beq .99
				
				dex
				bne .99
				
				ldx CsiHeader

				lda CsiParams
				php

				cmp #20					bCRLF
				bne .1

				txa
				bne .98

				ldy #S.DCB.TTY.bCRLF
				bra .8
				
.1				cmp #7
				bne .98

				cpx #'?'
				bne .98

				ldy #S.DCB.TTY.bLINEWRAP

.8				plp
				ror
				sta (ZPDCBPtr),y
				clc
				rts

.98				plp
.99				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 TERMX.CLRSCR.DN
.1				lda CsiParams
				cmp #1
				bne .2
				jmp TERMX.CLRSCR.UP

.2				cmp #2
				bne .9
				jmp TERMX.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 Csi.Home.8
				jmp CLRLINE
*--------------------------------------
Csi.Home		lda CsiParamCnt
				beq HOME

				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
*--------------------------------------
HOME			lda #0
				ldy #S.DCB.TTY.CV
				sta (ZPDCBPtr),y

				ldy #S.DCB.TTY.CH
				sta (ZPDCBPtr),y
				clc
				rts
*--------------------------------------			
TERMX.G0		ldx #$80
				.HS 2C					BIT ABS
TERMX.G1		ldx #$40

				ldy #S.DCB.TTY.bG0G1ALT

				jsr TERM.GetBufByte
				cmp #'0'
				beq .1
				
				txa
				eor #$ff
				and (ZPDCBPtr),y
				bra .8
				
.1				txa
				ora (ZPDCBPtr),y
.8				sta (ZPDCBPtr),y

				jmp TERMX.SETMODE0
*--------------------------------------
TERMX.IAC		ldy #S.DCB.TTY.INBUFFER
				lda (ZPDCBPtr),y
				inc
				sta (ZPDCBPtr),y
				tay
				
				jsr TERM.GetBufByte
				
				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

				jsr TERM.GetBufByte

				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
				rts
*--------------------------------------
RESET			ldy #S.DCB.TTY.MODE
.1				lda RESET.VALUES-S.DCB.TTY.MODE,y
				sta (ZPDCBPtr),y
				iny
				cpy #S.DCB.TTY.INBUFFER+1
				bne .1
				jmp TERMX.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
*--------------------------------------
TERMX.COUT.LF	ldy #S.DCB.TTY.bCRLF
				lda (ZPDCBPtr),y
				bpl TERMX.COUT.LF.1
				jsr TERMX.COUT.CR

TERMX.COUT.LF.1	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
*--------------------------------------
CLRSOL			ldy #S.DCB.TTY.CH		End
				lda (ZPDCBPtr),y
				cmp #80
				bcc CLRLINE.1

CLRLINE			lda #80					End

CLRLINE.1		ldy #0					Start
				bra CLR

CLREOL			ldy #S.DCB.TTY.CH
				lda (ZPDCBPtr),y
				tay						Start
				cpy #80
				bcs CLR.8

				lda #80					End

CLR				sty .1+1				Save Start
				sta .2+1				Save End

				ldy #S.DCB.TTY.CV
				lda (ZPDCBPtr),y
				tax

.1				ldy #$ff				SELF MODIFIED
.2				cpy #$ff				SELF MODIFIED
				bcs CLR.8

				lda #" "
				jsr SetCharAtYX			X unmodified

				inc .1+1
				bra .1

CLR.8			clc
				rts
*--------------------------------------
SaveCurPos		ldy #S.DCB.TTY.CH

				jsr .1
				
				iny
				
.1				lda (ZPDCBPtr),y
				iny
				sta (ZPDCBPtr),y
				rts
*--------------------------------------
RestoreCurPos	jsr TERMX.CUROFF
				
				ldy #S.DCB.TTY.CV.SAVE
			
				jsr .1
				
				dey

.1				lda (ZPDCBPtr),y
				dey
				sta (ZPDCBPtr),y
				rts
*--------------------------------------
SCROLL.UP		ldy #S.DCB.TTY.SCROLLTOP
				lda (ZPDCBPtr),y

				tax

.1				jsr TERMX.SETUP.L1X

				inx
				jsr TERMX.COPY.XtoL1

				txa
				ldy #S.DCB.TTY.SCROLLBOT
				cmp (ZPDCBPtr),y
				bne .1
				bra CLRLINE
*--------------------------------------
SCROLL.DN		ldy #S.DCB.TTY.SCROLLBOT
				lda (ZPDCBPtr),y

				tax

.1				jsr TERMX.SETUP.L1X

				dex
				jsr TERMX.COPY.XtoL1

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

				clc
				rts
*--------------------------------------
TERMX.CURBLNK	bit bActive
				bpl .9

				lda DevMgr.Timer
				and #CURSOR.BLINK.SPEED
				ldy #S.DCB.TTY.bCURON
				eor (ZPDCBPtr),y
				bne TERMX.CURBLNK.1

.9				rts

TERMX.CUROFF	bit bActive
				bpl TERMX.CURBLNK.RTS
				ldy #S.DCB.TTY.bCURON
				lda (ZPDCBPtr),y
				beq	TERMX.CURBLNK.RTS

TERMX.CURBLNK.1	lda (ZPDCBPtr),y
				bne TERMX.CURBLNK.OFF

				jsr GetCharAtCurPos
				bcs TERMX.CURBLNK.RTS	Out of screen

				and #$80
				eor #" "

TERMX.CURBLNK.SET
				jsr SetCharAtY.SCR
				ldy #S.DCB.TTY.bCURON
				lda (ZPDCBPtr),y
				eor #CURSOR.BLINK.SPEED
				sta (ZPDCBPtr),y
				rts
				
TERMX.CURBLNK.OFF
				jsr GetCharAtCurPos
				bcc TERMX.CURBLNK.SET	if not Out of screen
TERMX.CURBLNK.RTS
				rts
*--------------------------------------
Decimal.Out		jsr MATH.A2STR10NP

				ldx #0
.1				lda A2osX.NumStrBuf,x
				beq TERMX.CURBLNK.RTS
				inx
				jsr Char.Out.Put
				bra .1
*--------------------------------------
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
*--------------------------------------
TERMX.SETUP.L1X	lda ZPBufBase
				clc
				adc BUF.BASEL,x
				sta ZPBufBaseL1
				
				lda ZPBufBase+1
				adc BUF.BASEH,x
				sta ZPBufBaseL1+1

				bit bActive
				bpl TERMX.SETUP.L1X.8

TERMX.SETUP.L1X.SCR
				lda SCR.BASEL,x
				sta ZPScrBaseL1
				txa
				lsr
				and #$3
				ora #4
*				lda SCR.BASEH,x
				sta ZPScrBaseL1+1
TERMX.SETUP.L1X.8
				rts
*--------------------------------------
TERMX.CLRSCR.DN	ldy #S.DCB.TTY.CV
				sta (ZPDCBPtr),y
				tax
				bra TERMX.CLRSCR.1

TERMX.CLRSCR.UP	ldx #0
				ldy #S.DCB.TTY.CV
				sta (ZPDCBPtr),y
				tay
				bra TERMX.CLRSCR.2

TERMX.CLRSCR	ldx #0

TERMX.CLRSCR.1	ldy #24

TERMX.CLRSCR.2	sty .1+1

				sta SET80STORE
.1				cpx #$ff				SELF MODIFIED
				beq .9
				jsr TERMX.SETUP.L1X
				lda #" "

				bit bActive
				bpl .4
				sta SETPAGE2
				jsr .7

				sta CLRPAGE2
				jsr .7

.4				ldy #79
				
.5				sta (ZPBufBaseL1),y
				dey
				bpl .5

				inx
				bra .1
.7				ldy #39
.8				sta (ZPScrBaseL1),y
				dey
				bpl .8			
.9				rts
*--------------------------------------
TERMX.Title.ON	ldy #S.DCB.TTY.bTITLE
				lda (ZPDCBPtr),y
				bmi TERMX.Title.RTS

				lda #$ff
				sta (ZPDCBPtr),y

				jsr TERM.GetDevName
				
				ldx #0
				jsr TERMX.SETUP.L1X.SCR

				ldy #0

.1				jsr SHARED.TXTPTR.GetNext
				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
				
TERMX.Title.RTS	rts

TERMX.Title.OFF	ldy #S.DCB.TTY.bTITLE
				lda (ZPDCBPtr),y
				bpl TERMX.Title.RTS
				lda #$0
				sta (ZPDCBPtr),y
*--------------------------------------
TERMX.LINE0CPY	ldx #0
				.HS 2C					BIT ABS
TERMX.SCRCPY	ldx #23

				sta SET80STORE

.1				lda ZPBufBase
				clc
				adc BUF.BASEL,x
				sta .80+1

				lda ZPBufBase+1
				adc BUF.BASEH,x
				sta .80+2
				jsr TERMX.SETUP.L1X.SCR

				phx

				ldx #78

				sta SETPAGE2

				ldy #39

				jsr .8

				ldx #79
				sta CLRPAGE2

				ldy #39

				jsr .8

				plx
				dex
				bpl .1
				rts
				
.8				ldy #39

.80				lda $ffff,x				SELF MODIFIED

				sta (ZPScrBaseL1),y
				dex
				dex
				dey
				bpl .80

				rts
*--------------------------------------
TERMX.COPY.XtoL1
				lda ZPBufBase
				clc
				adc BUF.BASEL,x
				sta ZPBufBaseL2

				lda ZPBufBase+1
				adc BUF.BASEH,x
				sta ZPBufBaseL2+1

				ldy #79

.1				lda (ZPBufBaseL2),y
				sta (ZPBufBaseL1),y 
				dey
				bpl .1

				bit bActive
				bpl .8

				lda SCR.BASEL,x
				sta ZPScrBaseL2
*				lda SCR.BASEH,x
				txa
				lsr
				and #$3
				ora #4
				sta ZPScrBaseL2+1
				sta SET80STORE
				sta SETPAGE2
				jsr .6
				sta CLRPAGE2

.6				ldy #39
.7				lda (ZPScrBaseL2),y
				sta (ZPScrBaseL1),y 
				dey
				bpl .7

.8				rts
*--------------------------------------
SetCharAtCurPos	pha
				ldy #S.DCB.TTY.CV
				lda (ZPDCBPtr),y
				tax
				ldy #S.DCB.TTY.CH
				lda (ZPDCBPtr),y
				tay
				pla
				cpy #80
				bcs SetCharAtYX.8
*--------------------------------------
SetCharAtYX		ror A2osX.RANDOM16+1
				cmp #$40
				bcc .1
				cmp #$60
				bcs .1
				and #$1F				remap UPPERCASE

.1				phy

				ldy #S.DCB.TTY.bNORMAL
				ora (ZPDCBPtr),y
				sta ZPTmpChar

				ldy #S.DCB.TTY.bG0G1	Select Active Font
				lda (ZPDCBPtr),y
				clc
				bpl .2
				sec

.2				lda #$80
				bcc .21
				lsr

.21				iny						#S.DCB.TTY.bG0G1ALT
				and (ZPDCBPtr),y
				beq .3					not Graphic mode

				lda ZPTmpChar
				cmp #$E0				Normal lowercase ?
				bcc .3

				tay
				lda REMAP.E0.FF-$E0,y  
				sta ZPTmpChar

.3				jsr TERMX.SETUP.L1X
				ply
				lda ZPTmpChar
				sta (ZPBufBaseL1),y 

				bit bActive
				bpl SetCharAtYX.8

SetCharAtY.SCR	pha
				tya
				lsr
				tay
				pla
				sta SET80STORE
				bcs .2
				sta SETPAGE2
.2				sta (ZPScrBaseL1),y
				sta CLRPAGE2

SetCharAtYX.8	rts
*--------------------------------------
GetCharAtCurPos	ldy #S.DCB.TTY.CV
				lda (ZPDCBPtr),y
				cmp #24
				bcs .9					Out of screen

				tax
				jsr TERMX.SETUP.L1X

				ldy #S.DCB.TTY.CH
				lda (ZPDCBPtr),y
				cmp #80
				bcs .9					Out of screen

				tay
				lda (ZPBufBaseL1),y 
				cmp #$20
				bcs .8
	
*				clc	
				adc #40					remap $00-$1F uppercase
.8				clc

.9				rts
*--------------------------------------
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 "78MDc[()"
EscCodes.Cnt	.EQ *-EscCodes
EscModes		.DA #0,#0,#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 080A0B15			Left,Down,Up,Right
KeyRemapped.Cnt	.EQ *-KeyRemapped
KeyRemapped.Tbl	.HS 44424143			esc[D,esc[B,esc[A,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 "vt100"
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
*--------------------------------------
RESET.VALUES	.DA #0					MODE
				.DA #0					CH
				.DA #0					CH.SAVE
				.DA #0					CV
				.DA #0					CV.SAVE
				.DA #0					SCROLLTOP
				.DA #23					SCROLLBOT
				.DA #0					bCURON
				.DA #0					bTITLE

				.DA #$80				bNORMAL
				.DA #$80				bLINEWRAP
				.DA #$80				bCRLF

				.DA #0					bG0G1
				.DA #0					bG0G1ALT

				.DA #S.DCB.TTY.OUTBUFFER	OUTTAIL
				.DA #S.DCB.TTY.OUTBUFFER	OUTHEAD
				.DA #S.DCB.TTY.INBUFFER	INBUFFER			
*--------------------------------------
TERMX.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
*--------------------------------------
*					` a b c d e f g h i j k l m n o
REMAP.E0.FF		.HS 5B4142434445464748495FA0DF544e4f
*					p q r s t u v w x y z { | } ~ DEL
				.HS 50DF5253545556575F595a5b5c5d5e5f
*--------------------------------------
SCR.BASEL		.HS	00.80.00.80.00.80.00.80.28.A8.28.A8.28.A8.28.A8.50.D0.50.D0.50.D0.50.D0
*SCR.BASEH		.HS	04.04.05.05.06.06.07.07.04.04.05.05.06.06.07.07.04.04.05.05.06.06.07.07
*--------------------------------------
BUF.BASEL		.DA #0
				.DA #80
				.DA #160
				.DA #240
				.DA #320
				.DA #400
				.DA #480
				.DA #560
				.DA #640
				.DA #720
				.DA #800
				.DA #880
				.DA #960
				.DA #1040
				.DA #1120
				.DA #1200
				.DA #1280
				.DA #1360
				.DA #1440
				.DA #1520
				.DA #1600
				.DA #1680
				.DA #1760
				.DA #1840
BUF.BASEH		.DA /0
				.DA /80
				.DA /160
				.DA /240
				.DA /320
				.DA /400
				.DA /480
				.DA /560
				.DA /640
				.DA /720
				.DA /800
				.DA /880
				.DA /960
				.DA /1040
				.DA /1120
				.DA /1200
				.DA /1280
				.DA /1360
				.DA /1440
				.DA /1520
				.DA /1600
				.DA /1680
				.DA /1760
				.DA /1840
*--------------------------------------
MAN
SAVE USR/SRC/SYS/KERNEL.S.TERMX
LOAD USR/SRC/SYS/KERNEL.S
ASM
