	.TITLE	VT200SET - Defines keys on VT-200 terminals
;
;	Written by Mark Northrup on 9/11/85
;		   Miller Brewing Company
;		   3939 West Highland Blvd.
;		   Milwaukee, WI 53201
;
;	This module was designed to down load pre-set commands to the VT-200
;	terminal. The first position in the file must be either a semi-colon
;	or the first digit of the function key number as defined in the "VT-220
;	Programmers Pocket Guide" page 39, and page 83 of the "VT-200 
;	Programmer Reference Manual. If the first character is not a
;	semi-colon the first three characters must have the form "nn/" where
;	nn=the function key value, i.e. Function key 6 has a value of 17.
;	After the "/" the command line that is to be displayed when that key
;	is pressed, this program translates the string into the hex pairs
;	required by the VT-200. A tilde (~) will be translated into a Carriage
;	return, all other symbols will be translated directly into their hex
;	pair equivalent (including semi-colons that are not in position 1).
;
	.MCALL	FSRSZ$,GTIM$S,FDBDF$,FDAT$A,FDRC$A,FDOP$A,OPEN$R,GET$,EXIT$S
	.MCALL	CLOSE$,NMBLK$,QIOW$S,MRKT$S,WTSE$S
;
;	Open VT200SETU.PRM get a record and process it
;
;	REGISTER	DEFINITION
;
;	  R0		ADDRESS OF FILE DESC. BLOCK
;	  R1		NUMBER OF BYTES TO BE CLEARED
;	  R2		ADDRESS OF BUFFER TO BE CLEARED
;	  R3		
;	  R4
;	  R5		Points to VARDAT that is used to initialize func. keys
;
BEGIN:	MOV	#LSTFDB,R0			;POINT TO FILE DESC.BLK
	MOV	#VARDAT,R5			;POINT TO THE VARIABLE OUTPUT AREA
	OPEN$R	R0				;OPEN OUTPUT FILE
10$:	CALL	CLNBUF				;BLANK OUT LSTBUF
	GET$	R0,#LSTBUF,#256.		;GET DESCRPTION
	CMP	F.ERR(R0),#1			;WAS IT OK?
	BNE	20$				;YES!
	CMPB	LSTBUF,#73			;IS IT A COMMENT?
	BEQ	10$				;YES GET NEXT RECORD
	CALL	CNVSTR				;CONVERT STRING TO PROPER CODE
	CMP	R5,#spilbf			;IS THE DATA TOO BIG?
	BGE	SIZERR				;YES NOTIFY USER OF THE ERROR
	BR	10$				;GET MORE WORK
20$:	CMPB	F.ERR(R0),#-10.			;IS IT EOF?
	BEQ	CPYEXT				;WE ARE DONE
	MOV	F.ERR(R0),R5			;WE ARE IN TROUBLE MOVE ERROR TO R5
	IOT					;BLOWUP
;
;	Insert end of record marker then send it to the user
;
CPYEXT:	DEC	R5				;BACK UP OVER THE LAST ;
	MOVB	ENDCON,(R5)+			;END OF RECORD CODE
	MOVB	ENDCON+1,(R5)+			;END OF RECORD CODE
	SUB	#CONSET,R5			;Get the record length
	QIOW$S	#IO.WVB,#5,#1,,,,<#CONSET,R5,#40> ;Set function keys
	CLOSE$
	EXIT$S
;
;	Too much data to put into vt200, notify user then try to recover
;
SIZERR:	QIOW$S	#IO.WVB,#5,#1,,,,<#MSG2,#MS2LEN,#40>
	QIOW$S	#IO.WVB,#5,#1,,,,<#LSTBUF,#256.,#40>
	MOV	#SPILBF-1,R5			;Point to the end of the buffer
	BR	CPYEXT				;and finish up 
	.sbttl	SUBROUTINE AREA
	.page
;
;	Zero out recieve buffer due to the variable length nature of the data
;	we are working with
;
;	REGISTER	DEFINITION
;
;	  R0		ADDRESS OF FILE DESC. BLOCK
;	  R1		NUMBER OF BYTES TO BE CLEARED
;	  R2		ADDRESS OF BUFFER TO BE CLEARED
;
CLNBUF:	MOV	#LSTEND-LSTBUF,R1		;NUMBER OF BYTES TO CLEAR
	ASR	R1				;DIVIDE BY TWO TO MAKE WORDS
	MOV	#LSTBUF,R2			;STARTING ADDESS TO BE ZEROED
10$:	CLR	(R2)+				;ZERO NEXT WORD
	SOB	R1,10$				;KEEP ON TRUCKIN
	RETURN
;
;	Verify that the string we are talking about is in the legal range
;	altho it still may be illegal, convert the string to hexadecimal
;	because that is what the 200 wants.
;
;	REGISTER	DEFINITION
;
;	   R0		Addres of input buffer from file
;	   R1		High order nibble of byte from the file buffer
;	   R2		Low order nible of byte from the file buffer
;	   R3		
;	   R4
;	   R5		Address of output buffer, to define function keys
;
CNVSTR:	MOV	R0,-(SP)			;SAVE FDB ADDRESS
;
;	VERIFY LEGAL KEY DEFINITION
;
	CMPB	LSTBUF,#60			;IS CHR #1 LE ASCII 0?
	BLE	ERRREC				;BAD RECORD
	CMPB	LSTBUF,#64			;IS CHR #1 GE ASCII 4?
	BGE	ERRREC				;BAD RECORD
	CMPB	LSTBUF+1,#60			;IS CHR #2 LT ASCII 0?
	BLT	ERRREC				;BAD RECORD
	CMPB	LSTBUF+1,#71			;IS CHR #2 GT ASCII 9?
	BGT	ERRREC				;BAD RECORD
	CMPB	LSTBUF+2,#57			;IS CHR #3 EQUAL ASCII /?
	BNE	ERRREC				;BAD RECORD
	MOVB	LSTBUF,(R5)+			;Copy 1st three characters to output buffer
	MOVB	LSTBUF+1,(R5)+			;Copy 1st three characters to output buffer
	MOVB	LSTBUF+2,(R5)+			;Copy 1st three characters to output buffer
;
;	Read the rest of the record and convert it to hex for the VT200 
;
	MOV	#LSTBUF+2,R0			;Point to the begining of file data
10$:	INC	R0				;Next byte
	TSTB	(R0)				;End of record?	
	BEQ	900$				;Yes
	CMPB	(R0),#'~			;Should we put in a Carriage return
	BNE	20$				;No do a regular hex convertion
	MOVB	#'0,(R5)+			;Move Hex version (0D) of Carriage return
	MOVB	#'D,(R5)+			;Move Hex version (0D) of Carriage return
	BR	10$				;Next character
;
;	Hex convertion routine
;
20$:	MOVB	(R0),R1				;Copy by byte to working reg.
	MOVB	(R0),R2				;Copy by byte to working reg.
	BIC	#^B1111111100001111,R1		;Mask out only the high order nibble
	BIC	#^B1111111111110000,R2		;Mask out only the low order nibble
	ASH	#-4,R1				;Make it an index into the hex table
	MOVB	HEXCON(R1),(R5)+		;Copy hex code into func. key def
	MOVB	HEXCON(R2),(R5)+		;Copy hex code into func. key def
	BR	10$				;Continue processing record
;
;	Return to main routine
;
900$:	MOVB	#';,(R5)+			;Mark the end of key definition
	MOV	(SP)+,R0			;Restore FDB ADDRESS
	RETURN					;NORMAL RETURN
;
;	We can not define that key, so we let the user know
;
ERRREC:	QIOW$S	#IO.WVB,#5,#1,,,,<#MSG1,#MS1LEN,#40>
	QIOW$S	#IO.WVB,#5,#1,,,,<#LSTBUF,#256.,#40>
	MOV	(SP)+,R0			;Restore FDB ADDRESS
	RETURN					;BAD EXIT BUT NOT SERIOUS
	.sbttl	Working storage and file definition area
	.page
	.NLIST BIN
DUMMY:	.WORD	0
	FSRSZ$	1
LSTFDB:	FDBDF$
	FDAT$A	R.VAR,FD.CR
	FDRC$A	,LSTBUF,256.
	FDOP$A	1,,FNAM,FO.RD
FNAM:	NMBLK$	VT200SETU,PRM
	.EVEN
MSG1:	.ASCII	<12> <15> /Invalid function key number ignoring this definition, continuing.../
	MS1LEN=.-MSG1
	.EVEN 
MSG2:	.ASCII	<12> <15> /Too much data for key definition buffer, passing what we can!/
	.ascii  <12> <15> /Last record to be analyzed is as follows:/
	MS2LEN=.-MSG2
	.EVEN 
LSTBUF:	.BLKW	128.
LSTEND:
ENDCON:	.ASCII	<33> !/! 			;ENDING CONSTANTS
	.EVEN
;
;	Table to be used by calculated offset to obtain hex value
;
HEXCON:	.ASCII	/0123456789ABCDEF/
CONSET:	.ASCII	<33> !P0;1|!			;CONSTANT FOR FUNC. KEY SETUP
;
;	Any way I calculated this number it came out to 557. bytes, this number
;	works so we used it!
;
VARDAT:	.BLKb	573.				;BUFFER FOR VARIABLE DATA
;
;	Because the maximum number of bytes allowed per record is 512 plus
;	three leading control characters, we must provide a spill area so we 
;	won't get a memory protect violation.
;
SPILBF:	.BLKb	515.				;Overflow bufer
	.even
	.END	BEGIN
