;
; +++++++++++++++++++++++++++++
; +++ KERMIT VT100 emulator +++
; +++++++++++++++++++++++++++++
;
;============= Begin of VT100 emulation ================
;
; Simple VT100 emulator - emulates subset of
; VT100 escape sequences:
;
;  1) ESC [ Pn A  -> Cursor up
;
;  2) ESC [ Pn B  -> Cursor down
;
;  3) ESC [ Pn C  -> Cursor forward
;
;  4) ESC [ Pn D  -> Cursor backwards
;
;  5) ESC [ row;col H  -> Position cursor
;
;  6) ESC [ Pn J  -> Erase in display
;
;  7) ESC [ Pn K  -> Erase line
;
;  8) ESC [ P1;P2;..,Pn m  -> Give attributes
;	Pn = 0 : Reset all
;	Pn = 4 : Underline
;	Pn = 7 : Invers
;	Pn =27 : Reset all
;
;  9) ESC [ Pn P  -> Delete character	[]
;
; 10) ESC [ Pn L  -> Insert line	[]
;
; 11) ESC [ Pn M  -> Delete line	[]
;
;=======================================================
;
; VT100 emulator storage
;
MaxEsc	equ	32		; .. buffer length

ESClen:
	db	0
ESCptr:
	dw	0
ESCbuf:
	ds	MaxEsc+1
ESCpar:
	ds	MaxEsc / 2
ESCtab:
	db	'HJKmABCDPLM'
ESCln	equ	$-ESCtab
ESCcom:
	dw	esc.H,esc.J,esc.K,esc..m
	dw	esc.A,esc.B,esc.C,esc.D
	dw	esc.P,esc.L,esc.M
;
; Get character from line and interpret it
; EXIT	Accu holds character
;
AuxESC:
	call	auxchr		; Get from line
	cp	esc		; Test ESCape
	ret	nz		; .. nope
	ld	a,(vt100)	; Test flag
	or	a
	ld	a,esc
	ret	z		; .. OFF
	call	..auxchr	; Get next
	cp	'['		; Test legal prefix
	jp	nz,ESCret	; .. nope, get \0
	ld	hl,ESCbuf
	ld	(ESCptr),hl	; Init escape buffer
	xor	a
	ld	(ESClen),a
ESCloop:
	call	..auxchr	; Get character
	call	IsAlpha		; Test alphabetical
	jr	nc,ESCbeg	; .. yeap, start
	call	StESC		; Save character
	jr	ESCloop
ESCbeg:
	ld	hl,ESCtab+ESCln-1
	ld	bc,ESCln
	cpdr			; Find in table
	jp	nz,ESCret	; .. not here, get \0
	ld	hl,ESCcom
	add	hl,bc		; Get index
	add	hl,bc
	ld	e,(hl)		; .. fetch address
	inc	hl
	ld	d,(hl)
	push	de
	call	ScanBuff	; Scan escape buffer
	pop	hl
	jp	(hl)		; .. go !
;
; Test character alphabetical
; ENTRY	Accu holds character
; EXIT	Carry set if not alphabetical
;
IsAlpha:
	cp	'A'		; Test range
	ret	c
	cp	'z'+1
	ccf
	ret	c
	cp	'a'
	ret	nc
	cp	'Z'+1
	ccf
	ret
;
; Test character digit
; ENTRY	Accu holds character
; EXIT	Carry set if not digit
;
IsDigit:
	cp	'0'		; test range
	ret	c
	cp	'9'+1
	ccf
	ret
;
; Store character into ESCape buffer
; ENTRY	Accu holds character
;
StESC:
	ld	b,a
	ld	a,(ESClen)
	cp	MaxEsc		; Test room
	ret	nc		; .. nope
	inc	a
	ld	(ESClen),a
	ld	hl,(ESCptr)
	ld	(hl),b		; Store character
	inc	hl
	ld	(hl),0		; .. clear entry
	ld	(ESCptr),hl
	ret
;
; Convert string to number
; ENTRY	Reg DE points to string starting with digit
; EXIT	Accu holds number
;
CnvNum:
	ld	c,0		; Clear entry
.Cnv:
	ld	a,c		; Get old character
	add	a,a		; * 2
	add	a,a		; * 4
	add	a,c		; * 5
	add	a,a		; *10
	ld	c,a
	ld	a,(de)
	sub	'0'		; Strip off ASCII
	add	a,c
	ld	c,a		; .. add digit
	ld	a,b
	cp	1		; Test any more
	ld	a,c
	ret	z
	dec	b
	inc	de
	ld	a,(de)
	call	IsDigit		; Test more
	jr	nc,.Cnv		; .. yeap
	ld	a,c
	dec	de		; .. fix pointer
	inc	b
	ret
;
; Scan escape buffer
;
ScanBuff:
	ld	a,(ESClen)
	ld	b,a
	xor	a
	ld	(ESClen),a	; Clear count
	inc	a
	ld	(ESCpar),a	; Init 1st parameter
	ld	(ESCpar+1),a	; .. 2nd, too
	dec	b
	inc	b		; .. test any
	ret	z		; .. nope
	ld	hl,ESCpar	; Init pointers
	ld	de,ESCbuf
ScanLoop:
	ld	a,(de)
	call	IsDigit		; Test digit
	jr	c,ScanNxt	; .. nope
	call	CnvNum		; .. get number
	ld	(hl),a
	inc	hl
	ld	a,(ESClen)
	inc	a		; Bump count
	ld	(ESClen),a
ScanNxt:
	inc	de
	djnz	ScanLoop
	ret
;
; Reset video attributes
;
ResAttr:
	ld	c,'u'		; No underline
	call	ESCprt
	ld	c,'q'		; .. no invers
;
; Print ESCape session
; ENTRY	Reg C holds escape control
;
ESCprt:
	push	bc
	push	bc
	ld	a,esc
	call	PutCon		; .. give escape
	pop	bc
	ld	a,c
	call	PutCon		; .. and code
	pop	bc
	ret
;
; Sequence ESC [ Pn P  -> Delete characters
; Maps to Pn times ESC N
;
esc.P:
	ld	c,'N'
	jr	CUxx
;
; Sequence ESC [ Pn L  -> Insert lines
; Maps to Pn times ESC L
;
esc.L:
	ld	c,'M'
	jr	CUxx
;
; Sequence ESC [ Pn M  -> Delete lines
; Maps to Pn times ESC M
;
esc.M:
	ld	c,'M'
	jr	CUxx
;
; Sequence ESC [ Pn D  -> Cursor backwards
; Maps to Pn times ESC D
;
esc.D:
	ld	c,'D'
	jr	CUxx
;
; Sequence ESC [ Pn C  -> Cursor forward
; Maps to Pn times ESC C
;
esc.C:
	ld	c,'C'
	jr	CUxx
;
; Sequence ESC [ Pn B  -> Cursor down
; Maps to Pn times ESC B
;
esc.B:
	ld	c,'B'
	jr	CUxx
;
; Sequence ESC [ Pn A  -> Cursor up
; Maps to Pn times ESC A
;
esc.A:
	ld	c,'A'
CUxx:
	ld	a,(ESCpar)	; Get Pn
	ld	b,a
CUloop:
	call	ESCprt		; Print session
	djnz	CUloop
ESCret:
	xor	a		; Return \0
	ret
;
; Sequence ESC [ Pn K  -> Erase line
; Maps to ESC K
;
esc.K:
	ld	c,'K'
Ex:
	call	ESCprt		; .. print
	jr	ESCret
;
; Sequence ESC [ Pn J  -> Erase in display
; Maps to ESC J
;
esc.J:
	ld	c,'J'
	jr	Ex
;
; Sequence ESC [ P1;P2;..,Pn m  -> Give attributes
; Maps to different ESCape versions:
; Pn = 0 : Reset all; ESC u and ESC q
; Pn = 4 : Underline; ESC r
; Pn = 7 : Invers;    ESC p
;
esc..m:
	ld	a,(ESClen)
	or	a		; Test any parameter
	jr	z,NoAttr	; .. no attributes
	ld	b,a
	ld	hl,ESCpar	; Init parameters
SGR.loop:
	push	hl
	ld	a,(hl)
	ld	c,'r'
	cp	4		; Test underline
	jr	z,SGR.do
	ld	c,'p'
	cp	7		; Test invers
	jr	z,SGR.do
	or	a		; Test reset
	push	af
	call	z,ResAttr	; .. reset it
	pop	af
	cp	27		; Test special
	call	z,ResAttr	; .. reset it
SGR.nxt:
	pop	hl
	djnz	SGR.loop
	jr	ESCret
SGR.do:
	call	ESCprt		; Print control
	jr	SGR.nxt
NoAttr:
	call	ResAttr		; Reset attributes
	jr	ESCret
;
; Sequence ESC [ row;col H  -> Position cursor
; Maps to ESC Y row-1+20h col-1+20h
;
esc.H:
	ld	c,'Y'
	call	ESCprt		; .. give prefix
	ld	a,(ESCpar)	; Get row
	add	a,31
	call	PutCon
	ld	a,(ESCpar+1)	; .. and col
	add	a,31
	call	PutCon
	jr	ESCret		; .. get more
;
; Get character from auxiliary port and log to file
;
..auxchr:
	call	.auxchr		; Get it
	ld	e,a		; .. save it
	ld	a,(alogfl)	; Get the log flag.
	or	a
	call	nz,logit	; Log the char if it is set.
	ld	a,e
	ret

