;
; GATHER ESCAPE CONTROL SEQUENCES
;  ENTER WITH TABLE @HL
;	DW @OUTPUT ROUTINE
;	DB 'CHAR'
;	DW ADDRESS
;	...
;	DB	-1
;
; IF CHAR>=80H
;	THEN @ADDRESS IS A WORD PARAMETER TO SET
;	ELSE @ADDRESS IS A ROUTINE TO EXEC
;		(WITH HL=PARM)
;
; STATE: 0-NORMAL
;	 1-WAITING FOR PARM
;	 2-REVCING PARM
;	 3-PARM COMPLETE
;
; PARM FORMAT: (ESC) [SPACES] [VALUE] [SPACES] 'CHAR'
;   DEFAULT VALUE=0
;
; ENTER WITH  CHAR IN 'A'
;
	NAM	PROCHA
	ENT	PROCHR,PROCHA
;
ESC	EQU	1BH
SPC	EQU	20H
;
;
PROCHA:	MOV	C,A	;SAVE CHARACTER
	MOV	A,M
	INX	H
	ORA	M	; 0=INACTIVE ; -1=ACTIVE
	MOV	A,C
	INX	H
	JNZ	PROCH1	; PROCHR IS ACTIVE
	CMP	M	; IS IT SWCHAR?
	INX	H	; POINT HL @ OUTPUT ROUTINE
	JNZ	PROCCA	; NO , OUTPUT CHARACTER
;
	DCX	H		; BACK-UP TO 2 BYTE OF SWITCH
	DCX	H		; CHANGE SWITCH TO...
	MVI	A,-1		; SIGNAL WE'RE OUT OF BUSINESS
	MOV	M,A
	DCX	H
	MOV	M,A
	RET
;
PROCH1:	INX	H
PROCHR:	CPI	ESC
	JNE	PROCC1
; INITIALIZE CTL SEQ.(ONLY ONE DEVICE AT A TIME!!)
	XCHG		; SAVE OUTPUT ROUTINE
	LXI	H,CSTATE
	MOV	A,M
	DCR	A
	MVI	M,0	; SET STATE TO 0
	MVI	A,ESC
	XCHG
	JZ	PROCCA	; SEND OUT ESC AS A CHARACTER
	XCHG
	INR	M	; INCREMENT STATE
;
	LXI	H,0
	SHLD	CPARM
	RET
; ELSE SEE WHAT STATE IS
PROCC1:	SHLD	DOCHR	; SAVE ADDRESS
	PUSH	PSW
	LDA	CSTATE
	ORA	A
	JNZ	PROCC2
; NORMAL OUTPUT
	POP	PSW
PROCCA:	MOV	E,M
	INX	H
	MOV	D,M
PROCCB:	XCHG
	PCHL		; OUTPUT CHAR (A)
;
PROCC2:	DCR	A
	JNZ	PROCC3
; WAITING FOR PARM
	POP	PSW
	CPI	SPC
	RZ		; STILL WAITING
PROC2A:	CPI	'0'
	JLT	GVAL
	CPI	'9'+1
	JLT	GDIGIT	; GOT FIRST DIGIT
	JMP	GVAL	; GOTO FINAL VALUE
; RCVING PARM
PROCC3:	DCR	A
	JNZ	PROCC4
	POP	PSW
	CPI	SPC
	JNE	PROC2A	; IF NOT SPC THEN TEST DIGIT
	MVI	A,3	; ELSE PARM DONE
	STA	CSTATE
	RET
; MUST BE WAITING FOR VALUE LETTER
PROCC4:	POP	PSW
	CPI	SPC
	REQ
	JMP	GVAL
;
; GOT A DIGIT
;
GDIGIT:	LHLD	CPARM
	MOV	D,H	; *10 ROUTINE
	MOV	E,L
	DAD	H	; *2
	DAD	H	; *4
	DAD	D	; *5
	DAD	H	; *10
;
	SUI	'0'
	ADD	L
	MOV	L,A
	JNC	GDIG1
	INR	H
GDIG1:	SHLD	CPARM
	RET
;
; GOT A LETTER / DO ACTION
;
GVAL:	LHLD	DOCHR	; SKIP OUTPUT
	INX	H
	INX	H
	MOV	B,A
	XRA	A
	STA	CSTATE	; CLEAR STATE
;
GVAL1:	MOV	A,M	; SEARCH FOR CHAR
	ANI	7FH	; CLEAR MSB
	CMP	B
	MOV	A,M	;RE-LOAD 'A'
	INX	H
	JEQ	GVAL2	; EXIT IF FOUND
	INX	H
	INX	H
	MOV	A,M
	CPI	-1
	JNE	GVAL1
; NOT FOUND (ECCH!!)
	RET
;
GVAL2:	MOV	E,M
	INX	H
	MOV	D,M	; DE:=ADDR
	ANI	80H	; IF MSB CLEAR 
	JNZ	GVAL3
; 		       ; THEN ACTION
	PUSH	D
	LHLD	CPARM
	RET
; 			ELSE DESCRIPTOR
GVAL3:	LHLD	CPARM
	XCHG
	MOV	M,E
	INX	H
	MOV	M,D
	RET
;
; VARIABLES
;
CSTATE:	DB	0	; STATE
CPARM:	DW	0	; PARM VALUE
DOCHR:	DW	0	; OUTPUT ROUTINE & TABLE
;
;
