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

ZPCnt			.BS 2

ZPBufBL1		.BS 2
ZPBufBL2		.BS 2
ZPScrBL1		.BS 2
ZPScrBL2		.BS 2
*--------------------------------------
bActive			.BS 1
ZPDevID			.BS 1

ZPTmpBool		.BS 1
ZPTmpChar		.BS 1

CsiH			.BS 1
CsiPCnt			.BS 1
CsiP			.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	tya
				beq .1

				cmp #S.IOCTL.S.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.S
				lda (ZPDCBPtr),y
				sta (ZPBufPtr)
				clc
				rts

.3				cmp #S.IOCTL.S.EOF
				bne .9

				jsr TERMX.ISOPENED
				beq TERMX.OPEN.E

				jsr TERMX.CBLNK
				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		jsr TERMX.ISOPENED
				bne TERMX.OPEN.E

				ldx #0

.2				jsr SHARED.GetScrnDevX
				beq .3

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

				lda #E.OOH
				sec
				rts

.3				lda ZPDevID

				sta CLRWRITEAUX
				sta A2osX.SCRNDEVS,x
				sta SETWRITEAUX

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

				ldy #S.DCB.TTY.S
				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 CLRWRITEAUX
				sta A2osX.ASCREEN
				sta SETWRITEAUX
				
				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				jsr SHARED.GetScrnDevX
				cmp (ZPDCBPtr)			#S.DCB.TTY.DEVID
				bne .2

				sta CLRWRITEAUX
				stz A2osX.SCRNDEVS,x
				sta SETWRITEAUX
				clc
				rts
				
.2				inx
				cpx #K.TTY.MAX+1
				bne .1

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

				bit	OPENAPPLE
				bpl .1

				jsr TERMX.Title.ON

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

.1				jsr TERMX.Title.OFF

				lda KBD

				bpl .6
				sta KBDSTROBE

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

.10				ldx #3

.2				cmp KeyMap,x
				beq .3

				dex 
				bpl .2

				cmp #C.CR
				bne .21

				jsr TERMX.COUT
				ldy #S.DCB.TTY.bCRLF
				lda (ZPDCBPtr),y
				bpl .7

				lda #C.LF
.21				jsr TERMX.COUT		no remap....send char....
				bra .7					and flush

.3				lda KeyMap.Tbl,x
				pha
				lda #C.ESC
				jsr TERMX.COUT
				lda #'['
				jsr TERMX.COUT
				pla
				jsr TERMX.COUT
				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 ZPCnt
				bne .71

				inc ZPCnt+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 ZPCnt
				bne .2

				inc ZPCnt+1
				beq .8

.2				ldy #S.DCB.TTY.M
				lda (ZPDCBPtr),y
				tax
				jsr .3

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

.3				jmp (.4,x)

.4				.DA TERMX.OUT
				.DA TERMX.ESC
				.DA TERMX.CSI
				.DA TERMX.IAC
				.DA TERMX.G0
				.DA TERMX.G1

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

				cmp #IAC
				bne .1

				lda #S.DCB.TTY.M.IAC
				jmp TERMX.SETM

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

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

				dec
				sta (ZPDCBPtr),y

TERMX.OUT.BS.8	clc
				rts

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

				dec
				sta (ZPDCBPtr),y

				lda #79
				ldy #S.DCB.TTY.CH
				sta (ZPDCBPtr),y

.2				clc
				rts
*--------------------------------------
TERMX.OUT.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.OUT.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.OUT.FF	jsr HOME
				jmp TERMX.CLRSCR
*--------------------------------------
TERMX.OUT.CR	lda #0
				ldy #S.DCB.TTY.CH
				sta (ZPDCBPtr),y
				clc
				rts
*--------------------------------------
TERMX.OUT.SO	sec
				.HS 90					BCC
*--------------------------------------
TERMX.OUT.SI	clc
				ldy #S.DCB.TTY.bG0G1
				ror
				sta (ZPDCBPtr),y
				clc
				rts
*--------------------------------------
TERMX.OUT.ESC	lda #S.DCB.TTY.M.ESC
				bra TERMX.SETM
*--------------------------------------
TERMX.ESC		jsr TERM.GetBufByte		y = #S.DCB.TTY.M

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

				bra TERMX.SETM0

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

.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.RTS			[
				.DA TERMX.RTS			(
				.DA TERMX.RTS			)
*--------------------------------------			
TERMX.SETM0		lda #0
TERMX.SETM		ldy #S.DCB.TTY.M
				sta (ZPDCBPtr),y
				ldy #S.DCB.TTY.INBUF
				lda #S.DCB.TTY.INBUF
				sta (ZPDCBPtr),y
				clc
				rts
*--------------------------------------
TERMX.CSI		ldy #S.DCB.TTY.INBUF
				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.INBUF.MAX-1	buffer full?
				bne .8

				jsr RESET.MODE

.8				clc
				rts

TERMX.CSI.Exec	stz CsiH
				stz CsiPCnt

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

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

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

				sta CsiH

.1				jsr SHARED.NextCP2

.2				jsr MATH.Dec2ACC32
				bcs .5

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

				lda ACC32

.3				ldx CsiPCnt
				sta CsiP,x
				inc CsiPCnt

				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.SetM			h
				.DA Csi.ResetM			l
				.DA Csi.DispAttr		m
				.DA Csi.Query			n
				.DA Csi.Scroll			r
				.DA Csi.CUU				A
				.DA Csi.CUD				B
				.DA Csi.CUF				C
				.DA Csi.CUB				D
				.DA Csi.CLRSCR			J
				.DA Csi.CLRL			K
				.DA	Csi.Home			H
*--------------------------------------
Csi.SetM		sec
				.HS 90					BCC 
*--------------------------------------
Csi.ResetM		clc

				ldx CsiPCnt
				beq .99
				
				dex
				bne .99
				
				ldx CsiH

				lda CsiP
				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 CsiPCnt
				bne .10
				
				jmp RESETATTR

.10				ldx #0

.1				lda CsiP,x
				bne .2

				jsr RESETATTR
				bra .7

.2				eor #7
				bne .7

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

.7				inx
				dec CsiPCnt
				bne .1

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

				lda CsiP
				cmp #6
				bne .8
				lda #C.ESC
				jsr TERMX.COUT
				lda #'['
				jsr TERMX.COUT
				ldy #S.DCB.TTY.CV
				lda (ZPDCBPtr),y
				inc
				jsr DecOut
				lda #';'
				jsr TERMX.COUT
			
				ldy #S.DCB.TTY.CH
				lda (ZPDCBPtr),y
				inc
				jsr DecOut 
				lda #'R'
				jsr TERMX.COUT
.8				clc
				rts
*--------------------------------------
Csi.Scroll		ldy CsiPCnt
				bne .1
				lda #0
				ldx #23
				bra .8
.1				cpy #2
				bne .9
				lda CsiP
				dec
				ldx CsiP+1
				dex
.8				ldy #S.DCB.TTY.SCROLLTOP
				sta (ZPDCBPtr),y
				iny						S.DCB.TTY.SCROLLBOT
				txa
				sta (ZPDCBPtr),y
.9				clc
				rts
*--------------------------------------
Csi.CUU			sec
				.HS 90					BCC
Csi.CUD			clc
				ldy #S.DCB.TTY.CV
				bra Csi.CU
				
Csi.CUF			clc
				.HS B0					BCS
Csi.CUB			sec
				ldy #S.DCB.TTY.CH

Csi.CU			lda CsiPCnt
				beq .8
				
				dec
				bne .8
				
				lda (ZPDCBPtr),y
				bcs .1
				
				adc	CsiP
				sta (ZPDCBPtr),y
				
				clc
				rts
				
.1				sbc	CsiP
				sta (ZPDCBPtr),y
				
.8				clc
				rts
*--------------------------------------
Csi.CLRSCR		ldx CsiPCnt
				beq .8

.1				lda CsiP
				beq .8
				
				cmp #1
				bne .2

				jmp TERMX.CLRSCR.DN

.2				cmp #2
				bne .9

				jmp TERMX.CLRSCR.UP

.8				jmp TERMX.CLRSCR

.9				clc
				rts
*--------------------------------------
Csi.CLRL		ldx CsiPCnt

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

.2				cmp #2
				bne Csi.Home.8
				jmp CLRLINE
*--------------------------------------
Csi.Home		lda CsiPCnt
				beq HOME

				ldx #1
				lda CsiP
				beq .2

				ldx #24
				cmp #24
				bcs .2

				tax

.2				dex
				txa
				ldy #S.DCB.TTY.CV
				sta (ZPDCBPtr),y
				dec CsiPCnt
				beq Csi.Home.8

				ldx #1
				lda CsiP+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.SETM0
*--------------------------------------
TERMX.IAC		ldy #S.DCB.TTY.INBUF
				lda (ZPDCBPtr),y
				inc
				sta (ZPDCBPtr),y
				tay
				
				jsr TERM.GetBufByte
				
				sta (ZPDCBPtr),y
				cpy #S.DCB.TTY.INBUF+1	CMD ?
				beq Csi.Home.8			yes, wait for additional bytes

				ldy #S.DCB.TTY.INBUF+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.INBUF+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.INBUF+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.INBUF+2
				lda (ZPDCBPtr),y			Get SUBCMD in A

				cmp #TN.O.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 TERMX.COUT
				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 TERMX.COUT
				pla
				jsr TERMX.COUT
				pla
				jsr TERMX.COUT
*--------------------------------------
COUT.IAC.WONT
*--------------------------------------
COUT.IAC.DONT
*--------------------------------------
COUT.IAC.EXIT
*--------------------------------------
RESET.MODE		lda #0
				ldy #S.DCB.TTY.M
				sta (ZPDCBPtr),y
				rts
*--------------------------------------
RESET			ldy #S.DCB.TTY.M
.1				lda RESET.VALUES-S.DCB.TTY.M,y
				sta (ZPDCBPtr),y
				iny
				cpy #S.DCB.TTY.INBUF+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 TERMX.COUT
				inx
				cpx #ENQ.String.Len
				bne .1
.8				clc
				rts
*--------------------------------------
TERMX.OUT.LF	ldy #S.DCB.TTY.bCRLF
				lda (ZPDCBPtr),y
				bpl TERMX.OUT.LF.1

				jsr TERMX.OUT.CR

TERMX.OUT.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.CBLNK		bit bActive
				bpl .9

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

.9				rts

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

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

				jsr GetCharAtCurPos
				bcs TERMX.RTS			Out of screen

				and #$80
				eor #" "

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

TERMX.RTS		rts
*--------------------------------------
DecOut			jsr MATH.A2STR10NP

				ldx #0
.1				lda FOUTBuf,x
				beq TERMX.RTS
				inx
				jsr TERMX.COUT
				bra .1
*--------------------------------------
TERMX.COUT		pha						save char

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

.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.OUTBUF.MAX
				bne .1
				lda #S.DCB.TTY.OUTBUF

.1				ldy #S.DCB.TTY.OUTTAIL
				sta (ZPDCBPtr),y
				pla
				clc
.9				rts
*--------------------------------------
TERMX.SETUP.L1X	lda ZPDCBPtr
				clc
				adc BUF.BL,x
				sta ZPBufBL1
				
				lda ZPDCBPtr+1
				adc BUF.BH,x
				sta ZPBufBL1+1

				bit bActive
				bpl TERMX.SETUP.L1X.8

TERMX.SETUP.L1X.SCR
				lda SCR.BL,x
				sta ZPScrBL1
				lda SCR.BH,x
				sta ZPScrBL1+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

.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 (ZPBufBL1),y
				dey
				bpl .5

				inx
				bra .1

.7				ldy #39
.8				sta (ZPScrBL1),y
				dey
				bpl .8			
.9				rts
*--------------------------------------
TERMX.Title.ON	ldy #S.DCB.TTY.bTITLE
				lda (ZPDCBPtr),y
				bmi TERMX.RTS2

				lda #$ff
				sta (ZPDCBPtr),y

				lda (ZPDCBPtr)			#S.DCB.TTY.DEVID
				tax
				jsr TERM.GetDevName
				>STYA TXTPTR
				
				ldx #0
				jsr TERMX.SETUP.L1X.SCR

				ldy #0

.1				jsr SHARED.TXTPTRgn
				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.RTS2		rts

TERMX.Title.OFF	ldy #S.DCB.TTY.bTITLE
				lda (ZPDCBPtr),y
				bpl TERMX.RTS2

				lda #$0
				sta (ZPDCBPtr),y
*--------------------------------------
TERMX.LINE0CPY	ldx #0
				.HS 2C					BIT ABS
TERMX.SCRCPY	ldx #23

.1				lda ZPDCBPtr
				clc
				adc BUF.BL,x
				sta .80+1

				lda ZPDCBPtr+1
				adc BUF.BH,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 (ZPScrBL1),y
				dex
				dex
				dey
				bpl .80

				rts
*--------------------------------------
TERMX.COPY.XtoL1
				lda ZPDCBPtr
				clc
				adc BUF.BL,x
				sta ZPBufBL2

				lda ZPDCBPtr+1
				adc BUF.BH,x
				sta ZPBufBL2+1

				ldy #79

.1				lda (ZPBufBL2),y
				sta (ZPBufBL1),y 
				dey
				bpl .1

				bit bActive
				bpl .8

				lda SCR.BL,x
				sta ZPScrBL2
				lda SCR.BH,x
				sta ZPScrBL2+1

				sta SETPAGE2
				jsr .6
				sta CLRPAGE2

.6				ldy #39
.7				lda (ZPScrBL2),y
				sta (ZPScrBL1),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		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 (ZPBufBL1),y 

				bit bActive
				bpl SetCharAtYX.8

SetCharAtY.SCR	pha
				tya
				lsr
				tay
				pla

				bcs .2
				sta SETPAGE2
				sta (ZPScrBL1),y
				rts

.2				sta CLRPAGE2
				sta (ZPScrBL1),y

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 (ZPBufBL1),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.M.CSI,#S.DCB.TTY.M.G0,#S.DCB.TTY.M.G1
CsiCodes		.AS "hlmnrABCDJKH"
CsiCodes.Cnt	.EQ *-CsiCodes
*--------------------------------------
KeyMap			.HS 080A0B15			Left,Down,Up,Right
KeyMap.Tbl		.HS 44424143			esc[D,esc[B,esc[A,esc[C
*--------------------------------------
WILLDO			.DA #TN.O.BINARY,#TN.O.SGA,#TN.O.ECHO
WILLDO.CNT		.EQ *-WILLDO
WILLDONT		.DA #TN.O.TSPEED,#TN.O.LINEMODE,#TN.O.STATUS
WILLDONT.CNT	.EQ *-WILLDONT
DOWILL			.DA #TN.O.BINARY,#TN.O.SGA,#TN.O.TTYPE,#TN.O.NAWS
DOWILL.CNT		.EQ *-DOWILL
DOWONT			.DA #TN.O.ECHO,#TN.O.NEWENV,#TN.O.XDISPLOC
				.DA #TN.O.TSPEED,#TN.O.LFLOW,#TN.O.AUTH
DOWONT.CNT		.EQ *-DOWONT
*--------------------------------------
SB.IS.TTYPE		.DA #IAC,#SB,#TN.O.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,#TN.O.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.OUTBUF	OUTTAIL
				.DA #S.DCB.TTY.OUTBUF	OUTHEAD
				.DA #S.DCB.TTY.INBUF	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.BL			.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.BH			.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.BL			.DA #S.DCB.TTY+0
				.DA #S.DCB.TTY+80
				.DA #S.DCB.TTY+160
				.DA #S.DCB.TTY+240
				.DA #S.DCB.TTY+320
				.DA #S.DCB.TTY+400
				.DA #S.DCB.TTY+480
				.DA #S.DCB.TTY+560
				.DA #S.DCB.TTY+640
				.DA #S.DCB.TTY+720
				.DA #S.DCB.TTY+800
				.DA #S.DCB.TTY+880
				.DA #S.DCB.TTY+960
				.DA #S.DCB.TTY+1040
				.DA #S.DCB.TTY+1120
				.DA #S.DCB.TTY+1200
				.DA #S.DCB.TTY+1280
				.DA #S.DCB.TTY+1360
				.DA #S.DCB.TTY+1440
				.DA #S.DCB.TTY+1520
				.DA #S.DCB.TTY+1600
				.DA #S.DCB.TTY+1680
				.DA #S.DCB.TTY+1760
				.DA #S.DCB.TTY+1840
BUF.BH			.DA /S.DCB.TTY+0
				.DA /S.DCB.TTY+80
				.DA /S.DCB.TTY+160
				.DA /S.DCB.TTY+240
				.DA /S.DCB.TTY+320
				.DA /S.DCB.TTY+400
				.DA /S.DCB.TTY+480
				.DA /S.DCB.TTY+560
				.DA /S.DCB.TTY+640
				.DA /S.DCB.TTY+720
				.DA /S.DCB.TTY+800
				.DA /S.DCB.TTY+880
				.DA /S.DCB.TTY+960
				.DA /S.DCB.TTY+1040
				.DA /S.DCB.TTY+1120
				.DA /S.DCB.TTY+1200
				.DA /S.DCB.TTY+1280
				.DA /S.DCB.TTY+1360
				.DA /S.DCB.TTY+1440
				.DA /S.DCB.TTY+1520
				.DA /S.DCB.TTY+1600
				.DA /S.DCB.TTY+1680
				.DA /S.DCB.TTY+1760
				.DA /S.DCB.TTY+1840
*--------------------------------------
MAN
SAVE usr/src/sys/kernel.s.termx
LOAD usr/src/sys/kernel.s
ASM
