File: TECOVT.MA of Disk: V50/Source/Source-Listing-MAC-2
(Source file text) 

/6G VT52 Support for OS/8 TECO V50

/MEMORY NOTES: (only applies if scope is VT52 or VT100)

/MEMORY		'MEMSIZ'	big	res	vt	BATCH

/  8K		0		no	no	no	?
/ 12K		1		yes	no	no	no	no	no
/ 16K		2		yes	no	yes	yes	no	no
/ 20K		3		yes	yes	yes	yes	no	yes
/ 24K		4		yes	yes	yes	yes	yes	yes

/Notes:

/	big	means text buffer and Q-register space get bigger
/	res	means overlays are resident and get extended error msgs
/	vt	means get VT support


	/JR 2-Oct-78	for Mario
	/JR 4-Oct-78	Put in type-ahead feature
	/JR 23-Nov-78	Convert to MACREL
	/JR 25-Dec-78	Fix ^S, ^C trapping, fix scope classification
	/SR 06-JAN-79	Added CIF4B, CIF4C, removed CDF4D, CDF4E
	/SR 02-Mar-79	read with no wait plus miscellaneous
	/SR 09-Mar-79	Fixed 16K bug
	/SR 08-Apr-79	Made 'update factor' default to 1 not 4

	.EXTERN	P,ZZ,N,NFLG,CTLWR,OUTDNE,ERR34
	.EXTERN	CHKCTR,TYIRET,GETQX,VPATCH,VT52D,VT52KL
	.GLOBAL	SWATCH,VTCHKC,VTYI,VTGETQ,VTCCB
	.GLOBAL	CUR
	.EXTERN	TPUTX,CHKCTC,NCOM

	CUR=	30	/Define field of VT52 routine

	VSIZE=	30	/Number of lines on screen
	HSIZE=	120	/Number of characters per line on screen

	TTYBEG=	3000	/Start of type ahead buffer for Teco
	TTYEND=	3600	/End of type ahead buffer

	CHKBUF=	TTYEND	/Define match buffer for screen alignment

	MAPST=	4000		/Start of phantom screen
	MAPSZ=	VSIZE^HSIZE	/Size of phantom screen
	MAPND=	MAPST+MAPSZ	/End of phantom screen


	.ASECT	VWATCH

	FIELD	1	/Set the field now

	*10
BXR,	0
CXR,	0

	*20
MAPCOL,	0		/Current logical column in phantom screen
MAPPTR,	0		/Absolute pointer to current char pos in phantom screen
CURLIN,	0		/Video line where cursor goes
CURCOL,	0		/Video column where cursor goes
CURPTR,	0		/Absolute ptr to char pos under cursor in phantom screen
VIDLIN,	0		/Physical cursor position on terminal
VIDCOL,	0		/Physical cursor colunm on terminal
TXTPTR,	0		/Pointer to Teco's text buffer during text expansion
CHRCNT,	0		/One's complement count of chars to do in text buffer
PTRTXT,	0		/Text buffer pointer
LINBAS,	0		/Absolute addr of start of current line in map
LINPTR,	0
UNDERC,	0		/-addr of P for flagging saving of cursor parameters
CRSLIN,	0		/Line we are currently putting cursor at
LINCNT,	0		/Logical line being processed
RTMOST,	0		/Saves rightmost col used in current line
CHKLIN,	0		/Line current being used for screen alignment matching
CHKFLG,	0		/Flag set -1 if a line has been saved
ZZSAV,	0
PSAV,	0
CHKPTR,	0
OUTCHR,	0		/Points to current output routine during text expansion
OUTBLN,	0		/Points to current line blanking routine
NWATCH,	20		/User selected cursor line number
INITFL,	0		/Set -1 if state of screen is known
CNT,	0
PTR,	0
OFFSET,	0		/Char offset of current screen alignment attempt
LOFFST,	0		/Line offset of current screen alignment attempt

/REGISTERS FOR VT ROUTINES
TYIFLG,	0		/Keyboard wait flag
KCHAR,	0		/Temp input character
TTYINP,	TTYBEG		/TA Input pointer
TTYOUP,	TTYBEG		/TA Output pointer
QSAVE,	0		/GETQ Address temp
DPOLL,	KBDCHK		/Indirect for POLL
TIMOUT,	0

POLL,	0		/Check keyboard (specially XOF)
	DCA	TIMOUT	/Reset timout
	DCA	TYIFLG	/Enable ^O and ^S check
LPOLL,	JMS I	DPOLL
	TAD	VTM20	/Was it XOF-3?
	SZA CLA
	JMP I	POLL	/No, go back to update screen
	ISZ	TIMOUT	/We hope to get a XON
	JMP	LPOLL	/Not yet! Approx. 400msec timout
	JMP I	POLL

/SOME CONSTANTS
VT177,	177
VT77,	77
VTM20,	-20
VT40,	40
VTM3,	-3
VT7,	7
VT4,	4


	/Type ahead routines for Teco

	*200

VTCHKC,	JMS	KBDCHK	/See if key hit
	CIF CDF		/Nothing special, just exit
	JMP I	(CHKCTR	/*K* AC set to 14 if ^O hit

VTYILP,	IAC		/Set keyboard wait flag
VTYI,	DCA	TYIFLG	/Clear flag first time through
	JMS	TAHCHK	/Check if input pending
	JMP	3$	/Type-ahead buffer is empty
	DCA	TYIFLG	/Clear flag again
	TAD	TTYOUP	/Bump output pointer now
	TAD	(-TTYEND+1 /Mod size of buffer
	SNA
	TAD	(TTYBEG-TTYEND
	TAD	(TTYEND
	DCA	TTYOUP
	TAD I	TTYOUP	/Pick up char
2$:	CIF CDF		/Exit with it
	JMP I	(TYIRET

3$:	CDF	0
	TAD I	(ETFLAG	/Check if want read with no wait
	CDF	30
	AND	VT40
	SNA CLA
	JMP	VTYILP	/No, loop
	DCA	TYIFLG	/Yes
	STA		/Should really check that echo is off ***
	JMP	2$

TAHCHK,	0		/Check for input
	JMS	KBDCHK	/Sample keyboard flag
	CLA		/Clear ^O if any
	TAD	TTYINP	/See if anything in buffer
	CIA
	TAD	TTYOUP
	CLL
	SZA CLA
	ISZ	TAHCHK	/Skp if yes
	JMP I	TAHCHK	/First return if no (Link cleared)


	/Catch type ahead chars

KBDCHK,	0
	KSF		/*M8 CHANGED TO 'CLA'
	JMP I	KBDCHK	/Just exit if no flag	
	JMS	CTCTST	/Test for ^C hit
	CLA
	TAD	TYIFLG	/Test if called during keyboard wait
	SZA CLA		/Skp if no
	JMP	NOTOUT	/Else handle normally
	JMS	CTCTST	/Test keyboard
	TAD	(3-17	/Test if ^O
	SZA
	TAD	(17-23	/Test if ^s
	SZA CLA		/Skp if either
	JMP	NOTOUT
KBDCTO,	TAD	TTYINP	/Clear type ahead buffer if ^O
	DCA	TTYOUP
	JMS	CTCTST	/Re test keyboard
	JMP I	KBDCHK	/Return

NOTOUT,	KRB		/Reload char
	AND	VT177
	DCA	KCHAR
	TAD	TTYINP	/Bump pointer first, see if room
	TAD	(-TTYEND+1
	SNA
	TAD	(TTYBEG-TTYEND
	TAD	(TTYEND
	CIA
	TAD	TTYOUP
	SNA
	JMP	NOROOM	/No room, sound bell to warn him
	CIA
	TAD	TTYOUP	/Fix ptr
	DCA	TTYINP	/And update it
	TAD	KCHAR	/Now store char
	DCA I	TTYINP
	JMP	KBDCHK+1 /Check keyboard again and exit
NOROOM,	TAD	VT7	/Send ^G
	TSF
	JMP	.-1
	TLS
	JMP I	KBDCHK	/Return AC=7 So what


CTCTST,	0
	KRS		/Sample keyboard
	AND	VT177
	TAD	VTM3	/Test if ^C
	SZA
	JMP I	CTCTST	/Return if no with remainder in AC
KBDCTC,	CDF		/Test if ET flagging ^C intercept
	CLA STL RAR
	TAD I	(ETFLAG
	SPA		/Skp if was set
	JMP	CCTRAP	/Else trap normally
	DCA I	(ETFLAG
	CDF	CUR
	JMP	NOTOUT	/Buffer the ^C char
CCTRAP,	CLA
	TAD	TTYINP	/Clear buffer if ^C abort
	DCA	TTYOUP
	CIF CDF		/Branch out to Teco's ^C trap
	JMP I	(ERR34


VTCCB,	0		/Check if input pending (^] command)
	DCA	TYIFLG	/We want to service XON/XOF
	JMS	TAHCHK	/Is typahead buffer filled?
	STL STA		/No give back -1
	CIF CDF 0	/Yes give back 0
	JMP I	(NCOM	/As numeric argument

VTGETQ,	DCA	QSAVE	/Save addr passed in AC
	KSF
	JMP	OKGETQ	/Jmp if no interrupt
	KRS		/Fudge, don't allow ^C trap since it may not be safe
	AND	VT177
	TAD	VTM3
	SZA CLA		/Skp to pretend we didn't really see it
	JMS	KBDCHK	/Ok, buffer the character
	CLA
OKGETQ,	CDF	20
	TAD I	QSAVE	/Now get word from q reg space
	CIF CDF		/Ready to exit now
	JMP I	(GETQX+4/Done

	PAGE


/change these to -4 (or any other value) to cause VT support
/to delay this many characters if there is type-ahead

FUDKNT,	-1		/ORIGINAL VALUE
FUDCNT,	-1		/COUNTER

	/VT52 scope "WATCH" processor proper

SWATCH,	CLA CLL
	CDF		/Make copies of buffer info
	TAD I	(P
	DCA	PSAV
	TAD I	(ZZ
	DCA	ZZSAV
	ISZ I	(NFLG	/Skp if numeric arg passed
	JMP	2$	/Jmp if no, use old arg
	TAD I	(N	/Pick up arg
	SKP
2$:	TAD	NWATCH	/Pick up old arg
	SPA		/Skp if just setting number of lines to watch
	JMP	DOSTUF	/Go do real work
	DCA	NWATCH	/Save the arg
VTEXIT,	CIF CDF
	JMP I	(CTLWR	/And exit
DOSTUF,	CLL CMA
	SNA		/Skp if should erase top N lines of screen
	JMP	2$	/Else just update screen
	CDF	CUR	/Reset df now
	TAD	(-VSIZE	/Force in range [1,VSIZE]
	SZL
	CLA
	TAD	(VSIZE
	CIA
	DCA	VCNT	/Set line counter
	TAD	(MAPST-1 /Set auto XR to phantom screen
	DCA	BXR
L$:	TAD	(-HSIZE	/Reset col count
	DCA	HCNT
1$:	DCA I	BXR	/Forget the location in the map
	ISZ	HCNT
	JMP	1$
	ISZ	VCNT	/For all lines requested
	JMP	L$
	STA		/Forget last cursor position
	DCA	VIDLIN
	CIF CDF		/Get ready to exit
	DCA I	(OUTDNE	/Fake out: pretend we didn't see Teco's output
	JMP I	(CTLWR	/Now exit
2$:	TAD I	(OUTDNE	/Clear init flag if any output done
	SZA CLA
	DCA	INITFL
	CDF	CUR
	JMS I	(TAHCHK	/See if anything in type ahead buffer
	JMP	3$
	ISZ	FUDCNT
	JMP	VTEXIT	/Don't update if count not zero
	TAD	FUDKNT
	DCA	FUDCNT	/Reset count
3$:	TAD	PSAV	/Set ptr to text
	DCA	PTRTXT
	TAD	PSAV	/Save for comparison during text expansion
	CIA		/(anything for a machine cycle)
	DCA	UNDERC
	JMS I	(BAKUPL	/Do a 0L
	TAD	ZZSAV	/Calculate ones complement of size to end of buffer
	CMA
	TAD	PTRTXT
	DCA	CHRCNT	/Save count for text expansion routine
	TAD	PTRTXT	/Set pointer for expansion routine
	DCA	TXTPTR
	TAD	(FCHAR	/Setup dummy routines for counting lines only
	DCA	OUTCHR
	TAD	(FBLNK
	DCA	OUTBLN
	DCA	LINCNT	/Clear line counter
	DCA	MAPCOL	/Clear current map column
	TAD	(MAPST	/Set base addr of line in map
	DCA	LINBAS
	DCA	RTMOST	/Clear rightmost col to be safe **
	JMS I	(DOTEXT	/Go expand the text now
	TAD	NWATCH	/Get specified cursor line number
	SNA
	TAD	(20	/If zero, assume 16.
	TAD	(-VSIZE	/Force in range [1,VSIZE]
	SMA
	CLA
	TAD	(VSIZE
	DCA	NWATCH	/Update it
	TAD	(-VSIZE	/See if cursor will fit towards bottom of screen
	TAD	LINCNT
	TAD	NWATCH
	SMA
	CLA
	CIA
	TAD	NWATCH	/Take MAX(NWATCH,calculated NWATCH)
	DCA	CRSLIN	/Save it away
	JMP I	(DOLCNT	/Jmp to do line counting now
VCNT,	0
HCNT,	0
	PAGE


	/Count lines to top of screen and save one for screen alignment

DOLCNT,	DCA	CHKFLG	/Init flag to buffer unknown
	DCA	LINCNT	/Clear line counter
	DCA	RTMOST	/Clear rightmost col count for first line
LINES,	TAD	(FCHAR	/Reset output routine to flush chars
	DCA	OUTCHR
	TAD	PTRTXT	/Make copy of current line in buffer
	DCA	LINPTR
	TAD	LINPTR	/See if beginning of buffer
	SNA CLA
	JMP I	(TRYMOV	/Jmp if yes, no more lines to count
	IAC		/Do a -1L
	JMS I	(BAKUPL
	TAD	LINPTR	/Calc ones complement of line size just got
	CMA
	TAD	PTRTXT
	DCA	CHRCNT
	TAD	CHRCNT	/See if line is large enough to warrent saving
	TAD	(17
	SMA SZA CLA
	JMP	DONTSV	/Jmp if too small
	TAD	(CHKBUF	/Else set ptr to match buffer
	DCA	CHKPTR
	TAD	CHKFLG	/See if match buffer is already used
	SZA CLA
	JMP	DONTSV	/Jmp if yes
	ISZ	CHKFLG	/Else flag it just being used now
	TAD	(CCHAR	/Set output routine to save the line in buffer
	DCA	OUTCHR
	TAD	(-HSIZE	/Set count to clear buffer
	DCA	CNT
	TAD	(CHKBUF-1
	DCA	BXR	/Set ptr to blank buffer
	TAD	VT40
	DCA I	BXR	/Blank whole buffer
	ISZ	CNT
	JMP	.-3
DONTSV,	TAD	PTRTXT	/Setup pointer to the line to do
	DCA	TXTPTR
	TAD	LINCNT	/Save current line count incase multi line overflow
	DCA	SAVCNT
	JMS I	(DOTEXT	/Expand the text in one line
	TAD	CHKFLG
	SPA SNA CLA	/Skp if we just filled the buffer
	JMP	NOCSAV	/Else dont save its line count
	TAD	LINCNT	/Save check line number for screen alignment later
	DCA	CHKLIN
	STA		/Flag buffer is used now
	DCA	CHKFLG
NOCSAV,	TAD	CRSLIN	/See if passed top of screen yet
	CIA
	TAD	LINCNT
	SPA CLA
	JMP	LINES	/Loop if no
	TAD	SAVCNT	/Restore line just before overflow
	DCA	LINCNT
	JMP I	(TRYMOV	/Now attempt to align the screen and map
SAVCNT,	0


DPATCH,	0
	CDF	10
	TAD	(CIF CDF CUR
	DCA I	(VT52D+1
	TAD	XXXJMP
	DCA I	(VT52D+2
	TAD	(VTGETQ
	DCA I	(VT52D+3
	JMP I	DPATCH
XXXJMP,	GETQX+3&177+5600	/JMP I .+1
	PAGE


	/Now try to align the buffered line with the map
	/to get an accurate move offset for up/down scrolling

TRYMOV,	TAD	INITFL	/See if screen known 
	SPA CLA		/Skp if no, no point trying to align it
	TAD	CHKFLG	/See if anything was saved in match buffer
	SMA CLA		/Skp if yes
	JMP I	(OUTPUT	/Else just do output
	TAD	CHKLIN	/Calculate line number of saved line
	CIA
	TAD	LINCNT
	DCA	CHKLIN
	TAD	CHKLIN	/Calculate offset in map of saved line
	CLL RTL		/*4
	TAD	CHKLIN	/*5
	CLL RTL		/*20.
	CLL RTL		/*80.
	DCA	CHKBAS	/Save base offset of the saved line
	DCA	OFFSET	/Clear present alignment offset
	DCA	LOFFST	/Clear offset in lines too
	JMP	TRYGO	/Jmp into alignment loop
NXTRY,	TAD	OFFSET	/Try to align screen down
	CIA
	JMS	TRYMAT
	JMP I	(DNMOVE	/Jmp to move screen down if got a match
	TAD	OFFSET	/Try to align screen up
TRYGO,	JMS	TRYMAT
	JMP I	(UPMOVE	/Jmp to move screen up if got a match
	ISZ	LOFFST	/Tally another line offset
	TAD	OFFSET
	TAD	(HSIZE	/Bump char offset magnitude too
	DCA	OFFSET
	XX=	VSIZE%2
	TAD	LOFFST	/See if too far out
	TAD	(-XX
	SPA CLA		/Skp if yes
	JMP	NXTRY	/Else try another set of moves
	JMP I	(OUTPUT	/Quit trying to match, go do output


	/Compare the buffered line with a spot in the map
	/CALL+1 = JMP to do up or down scroll and map move as case may be

TRYMAT,	0
	TAD	CHKBAS	/Add base of saved line relative to map
	TAD	(MAPST-1 /Make absolute
	DCA	BXR
	JMS	POLL	/Sample keyboard for type ahead
	TAD	BXR	/Range check pointer for [MAPST-1,MAPND-1]
	TAD	(-MAPND
	CLL
	TAD	(MAPSZ
	SNL CLA		/Skp if in range
	JMP	NOMTCH	/Else off screen, just exit
	TAD	(CHKBUF-1 /Point at match buffer
	DCA	CXR
	TAD	(-HSIZE	/Set count for size of line
	DCA	CNT
MATLUP,	TAD I	CXR	/Compare chars
	CIA
	TAD I	BXR
	SZA CLA		/Skp if same
	JMP	NOMTCH	/Exit if not
	ISZ	CNT
	JMP	MATLUP	/Iterate
	TAD	OFFSET	/See if any offset
	SNA CLA
	JMP I	(OUTPUT	/Jmp if none, just do output
	TAD	LOFFST	/Setup alignment count to shift screen by
	CIA
	DCA	CNT
	JMP I	TRYMAT	/Jmp to CALL+1 to move screen
NOMTCH,	ISZ	TRYMAT	/Skp screen align branch
	JMP I	TRYMAT
CHKBAS,	0

	PAGE


	/Align the screen downward if offset was negative

DNMOVE,	DCA	LINCNT	/Set cursor to [0,0]
	DCA	MAPCOL
	JMS I	(SETCUR	/Do the cursor sequence
DNLUP,	TAD	(REVSEQ	/Now shift screen down with reverse LF sequence
	JMS I	(PUTCH
	ISZ	CNT
	JMP	DNLUP	/The correct number of times
	TAD	(MAPND-1-HSIZE /Set pointers for phantom screen move
	DCA	BXR	/Target pointer
	TAD	OFFSET
	CIA
	TAD	BXR
	DCA	CXR	/Source pointer
	TAD	(-HSIZE-HSIZE /XR offset
	JMP	ADJGO	/Go do adjustment now

REVSEQ,	33;111;0	/Escape sequence for reverse line feed: $I
LFSEQ,	12;0		/Phony escape sequence for normal LF

	/Align screen upward with normal LF if offset was positive

UPMOVE,	TAD	(VSIZE-1 /Set cursor to bottom line
	DCA	LINCNT
	DCA	MAPCOL
	JMS I	(SETCUR	/Do position sequence
UPLUP,	TAD	(LFSEQ	/Use a simple line feed
	JMS I	(PUTCH
	ISZ	CNT
	JMP	UPLUP
	TAD	(MAPST-1 /Set XR for phantom screen move
	DCA	BXR	/Target ptr
	TAD	BXR
	TAD	OFFSET
	DCA	CXR
ADJGO,	DCA	XOFFSET	/Set xr offset now
	TAD	(-VSIZE	/Set number of lines to shift
	TAD	LOFFST
	JMS	MAPADJ	/Go do them
	TAD I	CXR	/Instruction to shift a char
	TAD	LOFFST	/Now number of lines remaining to blank
	CIA
	JMS	MAPADJ	/Do them
	TAD	VT40	/Instruction to blank a char
	JMP I	(OUTPUT	/Now do the output

MAPADJ,	0
	DCA	LCNT	/Store line count passed in AC
	TAD I	MAPADJ	/Get proper source char inst passed in call+1
	ISZ	MAPADJ
	DCA	MINST
MLOOP,	TAD	(-HSIZE	/Set count for one line
	DCA	CNT
MINST,	HLT		/"TAD I CXR" or "TAD VT40"
	DCA I	BXR	/Store the char
	ISZ	CNT
	JMP	MINST
	JMS	POLL	/Now poll the damn keyboard
	TAD	BXR	/Adjust XR's
	TAD	XOFFSET
	DCA	BXR
	TAD	CXR
	TAD	XOFFSET
	DCA	CXR
	ISZ	LCNT	/Do next line
	JMP	MLOOP
	JMP I	MAPADJ
LCNT,	0
XOFFSET,0


	/Do -NL in Teco's text buffer
	/Entery AC = N

BAKUPL,	0
	CMA
	DCA	BLCNT	/Set count of lines to skip
	JMP	BAKGO	/Go into loop
BAKLP,	TAD	BLPTR
	DCA	PTRTXT	/Update pointer to text
BAKGO,	CLL STA		/Peek at prev char
	TAD	PTRTXT
	DCA	BLPTR
	SNL		/Unless at start of buffer
	JMP I	BAKUPL
	CDF	10
	TAD I	BLPTR	/Look at the char
	CDF	CUR
	AND	VT177
	TAD	(-15	/Range check in interval [12,14]
	CLL
	TAD	(3
	SZL CLA		/Skp if not LF, VT, or FF
	ISZ	BLCNT	/Decrement counter if is LF, VT or FF
	JMP	BAKLP	/Repeat if not LF or count not done
	JMP I	BAKUPL
BLPTR,	0
BLCNT,	0

	PAGE



	/Routine to actually do screen update after attempted alignment

OUTPUT,	TAD	(VTCHAR	/Setup for actual terminal output now
	DCA	OUTCHR
	TAD	(VTBLNK
	DCA	OUTBLN
	DCA	RTMOST	/Initialize rightmost col to zero
	DCA	LINCNT	/Clear line counter too
	TAD	ZZSAV	/Calculate a char counter
	CMA
	TAD	LINPTR	/From saved cursor line (0L) to Z
	DCA	CHRCNT
	TAD	LINPTR
	DCA	TXTPTR
	TAD	(MAPST	/Set pointer to map for redundant output avoidance
	DCA	LINBAS
	TAD	INITFL	/See if screen state known
	SPA CLA
	JMP	KNWSCR	/Jmp if screen not corrupted
	DCA	MAPCOL	/Zero out map if screen is unknown
	STA
	DCA	VIDLIN
	DCA I	(MAPST	/Force a non blank char so screen will be cleared
	TAD	(MAPND	/Blank whole screen and map
	JMS I	OUTBLN
	EOSSEQ
	DCA	VIDCOL	/Set video col to zero
KNWSCR,	TAD	(VSIZE-1
	DCA	CURLIN
	TAD	(HSIZE-1
	DCA	CURCOL
	JMS I	(DOTEXT	/Go do the real output now!
	TAD	CURLIN	/Position terminal cursor now
	DCA	LINCNT
	TAD	CURCOL
	DCA	MAPCOL
	JMS I	(SETCUR
	CLL STA		/Peek at char in front of P
	TAD	PSAV
	DCA	PTMP
	SNL CLA		/Skp if not at beginning of buffer
	JMP	OUTFIN	/Just exit if no
	CDF	10	/See if cursor is under a CR
	TAD I	PTMP
	CDF	CUR
	AND	VT177	/Only 7 bits
	TAD	(-15
	SZA CLA
	JMP	OUTFIN	/Jmp if no
	TAD	(CURSEQ	/Else do fancy down arrow sequence
	JMS I	(PUTCH	/Also moves cursor back under arrow
	DCA I	CURPTR	/Zap char in map since we clobbered it on screen
OUTFIN,	STA		/Flag screen is known now
	DCA	INITFL
	CIF CDF		/Now indicate that no output done
	DCA I	(OUTDNE	/By zeroing flag in Teco's print routine
	JMP I	(CTLWR	/Jmp back to a POPJ in Teco to exit
PTMP,	0

CURSEQ,	33;106;153;33;107;33;104;0	/Esc seq to flag cursor at CR: $F k $G $D

	PAGE


	/Main routine to expand text

DOTEXT,	0
	JMP I	(EXPGO	/Jmp into expansion loop

	/Handle line overflows here

OVFCHK,	CDF	10	/Get into text buffer field
	TAD I	TXTPTR
	CDF	CUR
	AND	VT177
	TAD	(-15	/See if overflow on cr
	SNA CLA
	JMP I	(GETBYT	/Ignore if yes
	CDF		/See if allowing line overflows
	TAD I	(ETFLAG
	AND	(400
	CDF	CUR
	SNA CLA		/Skp if should truncate to terminal width
	JMP	DOVFL
	ISZ	TXTPTR	/Eat char
	TAD	MAPCOL
	TAD	(-HSIZE+1
	SMA CLA
	JMP I	(CHKCUR
	TAD	(OVSEQ	/Now send out right arrow escape sequence
	JMS I	OUTCHR
	JMP I	(CHKCUR	/Go check cursor position
DOVFL,	STA
	TAD	CHRCNT
	DCA	CHRCNT	/Fake out char counter to reget char
	TAD	MAPCOL
	DCA	RTMOST
	DCA	MAPCOL
	JMS I	(NEWLIN	/Goto new line
	TAD	(OVSEQ	/Now send right arrow escape sequence
	JMS I	OUTCHR
	TAD	VT40
	JMS I	OUTCHR
	JMP I	(CHKCUR	/Now go check if at cursor

CHKEOS,	TAD	ZZSAV	/See if end of screen now
	CIA
	TAD	TXTPTR
	SZA CLA
	JMP	EORETN
	TAD	MAPCOL
	TAD	(-HSIZE+1
	SMA SZA CLA
	JMP	EORETN
	TAD	(EOBSEQ	/Send out special end of buffer char
	JMS I	OUTCHR
EORETN,	TAD	(MAPND	/Pass pointer to end of map
	JMS I	OUTBLN	/Blank rest of screen now
	EOSSEQ
	JMP I	DOTEXT	/Return

OVSEQ,	33;106;150;33;107;0	/Esc seq to flag line overflow: $F h $G
EOBSEQ,	33;106;141;33;107;0	/Esc seq to flag end of buffer: $F a $G
EOSSEQ,	33;112;0		/Esc seq to clear to end of screen: $J

	PAGE


	/Continuation of text expansion routine

LOOP,	JMS	POLL	/See if key struck
	TAD	MAPCOL	/See if about to overflow line
	TAD	(-HSIZE+2
	SMA CLA
	JMP I	(OVFCHK	/Jmp if yes
GETBYT,	CDF	10	/Get a char from Teco's text buffer
	TAD I	TXTPTR
	ISZ	TXTPTR	/Bump the pointer now
	CDF	CUR
	AND	VT177	/Save only 7 bits
	DCA	TXTCHR
	TAD	TXTCHR
	TAD	(-177
	SNA
	JMP	GOTDEL	/Flag del as ^? since it doesn't echo otherwise
	TAD	(177-40
	SPA		/Skp if normal printing char, usual case
	JMP	TRYCTL	/Else check type of control char
	TAD	VT40	/Restore char
OUTPTC,	JMS I	OUTCHR	/Send it out
CHKCUR,	TAD	TXTPTR	/Test if char is at cursor now
	TAD	UNDERC
	SNA CLA		/Skp if not under cursor, normal case
	JMP	SAVECP	/Else save its position information for later
EXCONT,	ISZ	CHRCNT	/Now bump count
	JMP	LOOP	/Loop for more
	JMP I	(CHKEOS	/Exit to check out end of screen status if done

SAVECP,	TAD	MAPPTR	/Else save line and column for cursor postion later
	DCA	CURPTR
	TAD	LINCNT
	DCA	CURLIN
	TAD	MAPCOL
	DCA	CURCOL
	TAD	MAPCOL
	TAD	(-HSIZE	/See if col is legal
	SPA CLA
	JMP	EXCONT	/Jmp if ok
	TAD	(HSIZE-1 /Fix it if no
	DCA	CURCOL
	JMP	EXCONT	/Continue

TRYCTL,	TAD	(40-15	/See if CR
	SNA
	JMP	GOTCR	/Jmp if yes
	CLL		/See if carraige control LF, VT, FF
	TAD	(15-11
	SNA
	JMP	GOTTAB	/Jmp if happens to be TAB
	SNL		/Skp if carraige control
	JMP	GOTCTL	/Else its a control char requiring flagging
GOTCRG,	CIA		/Form line feed count
	STL RAL
	DCA	CRGCNT
	JMS I	(NEWLIN	/Emit a new line
	ISZ	CRGCNT	/Bump count
	JMP	.-2	/Iterate
	JMP	CHKCUR	/Go check cursor position
GOTDEL,	TAD	(136	/Flag del as ^? since it normally doesn't print
	JMS I	OUTCHR
	TAD	VT77
	JMP	OUTPTC
GOTCTL,	CLA
	CDF 0
	TAD I	(ETFLAG	/Check if image mode
	CDF CUR
	RAR
	SZL CLA
	JMP	IMGCTL	/Yes, don't convert
	TAD	TXTCHR
	TAD	(-33	/Now test if ESC requiring special flagging
	SNA CLA		/Not usually
	JMP	GOTESC	/Jmp if yes, echo as $
	TAD	(136	/Send out a ^ to flag it
	JMS I	OUTCHR
	TAD	(100	/Make character visible
IMGCTL,	TAD	TXTCHR
	JMP	OUTPTC	/Go output it
GOTESC,	TAD	VT4	/Echo $
	JMP	OUTPTC-1	/* ADD VT40
GOTTAB,	TAD	VT40	/Simulate tabs
	JMS I	OUTCHR
	TAD	MAPCOL
	AND	VT7
	SNA CLA		/Skp if more to go
	JMP	CHKCUR	/Else back to loop
	TAD	MAPCOL
	TAD	(-HSIZE+1
	SPA CLA		/Skp if over line
	JMP	GOTTAB	/Else room to continue
	JMP	CHKCUR
GOTCR,	TAD	MAPCOL
	CIA
	TAD	RTMOST
	SMA CLA
	JMP	EXPGO
	TAD	MAPCOL
	DCA	RTMOST
EXPGO,	TAD	LINBAS
	DCA	MAPPTR
	DCA	MAPCOL	/Reset column counter on CR
	JMP	CHKCUR	/Check if cursor is here

CRGCNT,	0
TXTCHR,	0

	PAGE


	/Handle a new line

NEWLIN,	0
	TAD	MAPCOL
	DCA	SAVCOL
	JMS	POLL	/Sample keyboard for type ahead
	TAD	LINBAS	/Pass end of line in map to clear up to
	TAD	(HSIZE
	JMS I	OUTBLN	/Do clear end of line sequence
	CLNSEQ
	DCA	RTMOST	/Zero rightmost col
	TAD	LINBAS
	TAD	(HSIZE
	DCA	LINBAS	/Update base of current line in map
	ISZ	LINCNT	/Tally another line done
	TAD	SAVCOL
	SNA CLA
	JMP	NLDONE
	TAD	LINCNT
	TAD	(-VSIZE
	SMA CLA
	JMP	NLDONE
	DCA	MAPCOL
	TAD	LINBAS	/Point MAPPTR at beginning of new line
	DCA	MAPPTR	/For OUTCHR matching
LEADBL,	TAD	VT40
	JMS I	OUTCHR
	TAD	MAPCOL
	CIA
	TAD	SAVCOL
	SZA CLA
	JMP	LEADBL
NLDONE,	TAD	SAVCOL
	DCA	MAPCOL
	TAD	MAPCOL	/Now point MAPPTR at proper loc in new line
	TAD	LINBAS
	DCA	MAPPTR
	TAD	LINCNT	/Test if off end of screen
	TAD	(-VSIZE
	SPA CLA		/Skp if yes
	JMP I	NEWLIN
	JMP I	(EORETN	/Exit from text expansion
SAVCOL,	0


	/Blank the remainder of a line and map
	/Blanking is only done if map has any non blank chars in it
	/Entry AC = limit addr to stop at in map

VTBLNK,	0
	DCA	BLNKCT	/Save limit addr
	TAD	MAPCOL	/**back up mapcol
	DCA	HOLDMC
	TAD	RTMOST	/Take MAX(RTMOST,MAPCOL)
	CIA
	TAD	MAPCOL
	SPA
	CLA
	TAD	RTMOST
	DCA	MAPCOL
	STA
	TAD	MAPCOL
	TAD	LINBAS	/Add to base of current line in map
	DCA	BXR	/Setup auto xr
	TAD	BLNKCT	/Calculate a counter
	CIA
	TAD	BXR
	DCA	BLNKCT
	DCA I	(PUTCH	/Use subroutine entry as a flag
	JMP	BLNKGO	/Jmp into loop
BLNKLP,	TAD I	BXR	/Examine a char in map
	TAD	(-40
	SZA CLA		/Skp if blank already, the usual case
	JMP	NOTBLN	/Jmp if no, put space there now
BLNKGO,	ISZ	BLNKCT	/Test count
	JMP	BLNKLP	/Go again
	TAD	HOLDMC	/Now restore mapcol
	DCA	MAPCOL
	ISZ	VTBLNK	/Skip past escape seq
	JMP I	VTBLNK
NOTBLN,	TAD	BXR	/Save pointer to map
	DCA	BLNKPT
	TAD	VT40	/Now store space there
	DCA I	BLNKPT
	TAD I	(PUTCH	/See if already cleared to eol
	SZA CLA		/Skp if no
	JMP	BLNKGO	/Else continue scanning
	JMS I	(SETCUR	/First set cursor to end of line
	TAD I	VTBLNK	/Now pick up terminal clearing seq
	JMS I	(PUTCH	/Send it out
	JMP	BLNKGO	/Continue
BLNKPT,	0
BLNKCT,	0
HOLDMC,	0


	/Do actual output if updating screen
	/Does no output if char matches map
	/Slightly convoluted flow to minimize execution time

VTCHAR,	0
	CIA
	TAD I	MAPPTR	/See if corresponds to map
	SZA		/Skp if yes, usual case
	JMP	VTSEND	/Else must send it
VTDONE,	ISZ	MAPCOL	/Always tally new line column
	ISZ	MAPPTR	/And bump absolute map pointer
	JMP I	VTCHAR
VTSEND,	CIA		/Restore char to ac
	TAD I	MAPPTR
	DCA I	MAPPTR	/Now save in map
	JMS I	(SETCUR	/Set cursor if necessary
	TAD I	MAPPTR	/Get char back
	JMS I	(PUTCH	/Send out char
	ISZ	VIDCOL	/Tally new video column now
	JMP	VTDONE	/Now exit

CLNSEQ,	33;113;0	/Escape seq to clear to end of line: $K

	PAGE


	/Phony output routne used to buffer a line in match buffer
	/For later use in vertical screen alignment

CCHAR,	0
	ISZ	MAPCOL
	DCA I	CHKPTR	/Save char in check buffer
	TAD I	CHKPTR	/See if special escape or not
	ISZ	CHKPTR
	AND	(7600
	SNA CLA		/Skp if yes
	JMP I	CCHAR	/Else return
	DCA	CHKFLG	/Say match buffer not valid
	TAD	(FCHAR	/Reset output routine to flush chars
	DCA	OUTCHR
	JMP I	CCHAR	/Return

FCHAR,	0
	CLA
	ISZ	MAPCOL
	JMP I	FCHAR

FBLNK,	0
	CLA		/Clear ending addr passed in ac
	ISZ	FBLNK	/Skip past escape seq ptr in CALL+1
	TAD	(FCHAR	/Reset output routine to flush chars now
	DCA	OUTCHR
	JMP I	FBLNK	/Done


	/Cursor setting routine
	/If LINCNT and MAPCOL are the same as VIDLIN and VIDCOL,
	/No positioning is done.  otherwise an escape seq is emitted.

SETCUR,	0
	TAD	LINCNT	/Save map line
	DCA	VRTCHR	/In case vertical position necessary
	TAD	LINCNT
	CIA
	TAD	VIDLIN	/Compare video and map cursor positions
	SZA CLA
	JMP	DOSETC	/Jmp if different, set cursor
	TAD	(VSIZE	/If same, set vertical out of range to avoid VT52 flicker
	DCA	VRTCHR
	TAD	MAPCOL	/Now compare column
	CIA
	TAD	VIDCOL
	SNA CLA
	JMP I	SETCUR	/No need at all if same
DOSETC,	TAD	(YSEQ	/Send out direct addr seq
	JMS I	(PUTCH
	TAD	VRTCHR
	TAD	VT40	/Make non control code out of it
	JMS I	(PUTCH
	TAD	MAPCOL
	TAD	VT40
	JMS I	(PUTCH
	TAD	LINCNT	/Now make video and map correspond
	DCA	VIDLIN
	TAD	MAPCOL
	DCA	VIDCOL
	JMP I	SETCUR	/Return
YSEQ,	33;131;0	/Escape seq for direct cursor addressing: $Y
VRTCHR,	0


	/Print a char or an escape seq passed in AC
	/If AC GE 200, assumes an escape seq addr, else a 7 bit char

PUTCH,	0
	TAD	(7600	/Test if addr of escape seq or real char
	CLL
	TAD	(200
	SNL		/Skp if normal char
	JMP	DOSEQ	/Else to sequence
	JMS	PCH
	JMP I	PUTCH
DOSEQ,	DCA	SEQPTR	/Save pointer to escape sequence
DOSEQL,	TAD I	SEQPTR
	ISZ	SEQPTR
	SNA
	JMP I	PUTCH	/End of seq delimited by null
	JMS	PCH
	JMP	DOSEQL
SEQPTR,	0

	/Low level routine to print a char on terminal

PCH,	0
TRYKBD,	KSF
	JMP	TRYTSF
	DCA	PCHCHR
	JMS	POLL
	TAD	PCHCHR
TRYTSF,	TSF		/Multi-8 changes this too 'SKP'
	JMP	TRYKBD	/OK, we don't have to check keyboard
	TLS
	CLA
	JMP I	PCH
PCHCHR,	0

	PAGE


	/Once only setup routine

	/If monitor scope flag not set, won't run VWATCH
	/If GT 16K, cause overlays to run in field 4 and run VWATCH
	/If lt 16K, do not run VWATCH
	/If eq 16K and no BATCH, swap overlays and run VWATCH
	/If eq 16K and batch running, don't run VWATCH

	.EXTERN	MEMSIZ
	.EXTERN	CDF4A,CDF4B,CDF4C,CDF4F,CIF4A,CIF4B,CIF4C,HCDF1,CIF4E
	.EXTERN	ETFLAG,SRHLEN,TWLVKP,VTSUP,L16K
	.GLOBAL	CORTST

	BIPCCL=	7777	/OS/8 batch flag word in field 0
	SCOPWD=	7726	/HLT-CLA HLT scope flag word in field 1

CORTST,	0
	CDF	0	/Enter with AC = "MEMSIZ" from Teco
	DCA I	(MEMSIZ	/Store it for Teco
	TAD I	(BIPCCL	/BATCH ACTIVE ?
	RAL
	SMA CLA
	JMP	NOBATC	/NO, KEEP MEMSIZ AS IS
	STA
	TAD I	(MEMSIZ
	DCA I	(MEMSIZ	/REDUCE MEMSIZ FOR RESIDENT BATCH
NOBATC,	TAD I	(MEMSIZ	/Now test the size
	SNA
	JMP	NOVT	/Jmp if only 8k (12K BATCH)
	ISZ	CORTST
	CLL RAR
	SNA
	JMP	NOVT	/Jmp if only 12k (16K BATCH)
	RAR		/See if exactly 16K (20K BATCH)
	SZA CLA
	JMP	OKMOVE	/Jmp if no
	TAD	(CLA	/Reduce Teco to 12k
	DCA I	(TWLVKP
	DCA I	(L16K	/Don't allow "/"
OKMOVE,	TAD	(CLL STA RTL
	DCA I	(VTSUP
	CIF CDF	10	/Ok, reset fields
	TAD I	(SCOPWD	/Test if scope or not
	AND	(200
	SNA CLA
	JMP	TECRTN	/Jmp out if no, don't run VWATCH
	JMS I	(VTSTUF	/Now go check out type of vt terminal and Multi-8
VTMOVL,	TAD I	VTAD1	/Relocate VWATCH routine to field 3 now
	CDF	30
	DCA I	VTAD1
	CDF	10
	ISZ	VTAD1
	ISZ	VTCNT
	JMP	VTMOVL
	TAD	(VPATCH-1 /Set up pointer to patch list
	DCA	BXR
PATLP1,	TAD I	BXR	/See if more
	SNA
	JMP	SETFIN	/Jmp if no
	DCA	PTR	/Save pointer
	TAD I	BXR	/Get counter
	DCA	CNT
PATLUP,	TAD I	BXR	/Get a word
	CDF	0
	DCA I	PTR	/Store in Teco
	CDF	10
	ISZ	PTR
	ISZ	CNT
	JMP	PATLUP	/Do this segment
	JMP	PATLP1	/Now get next segment
SETFIN,	JMS I	(DPATCH	/*M8 CHANGED TO 'CLA'
	CDF	0
	TAD	(1000	/Now set VWATCH bit in etflag
	TAD I	(ETFLAG
	DCA I	(ETFLAG
	TAD	(-40	/And shorten search buffer to preserve "CTLW" routine
	DCA I	(SRHLEN
	CLL STA RTL	/-3
	TAD I	(MEMSIZ	/See if gt 16K now
	SPA CLA
	JMP	ONLY16	/Jmp if only 16K (20K BATCH), force overlays to swap
	TAD	(CDF 40	/Force overlays to load into field 4
	DCA I	(CDF4A
	TAD	(CDF 40
	DCA I	(CDF4B
	TAD	(CDF 40
	DCA I	(CDF4C
	TAD	(CIF 40
	DCA I	(CIF4B
	TAD	(CIF CDF 40
	DCA I	(CIF4C
	TAD	(CIF CDF 40
	DCA I	(CIF4E
	TAD	(CDF 40
	CDF	10
	DCA I	(CDF4F
	TAD	(CIF 40
	DCA I	(CIF4A
	TAD	(CDF 40
	DCA I	(HCDF1
	JMP	TECRTN
ONLY16,	TAD	(CLA	/Zap overlay space test
	DCA I	(TWLVKP
TECRTN,	CIF CDF
	JMP I	CORTST	/All set, return to Teco core size routine
VTAD1,	0
VTCNT,	-CORTST		/Move up to this once only code

NOVT,	CIF CDF	0
	DCA I	(L16K	/Don't allow "/"
	JMP I	CORTST
	PAGE


	/TERMINAL CLASSIFICATION VIA ESACPE SEQUENCES
	/IF DESPERATE FOR ROOM, CAN REMOVE THIS

	.EXTERN	VT05A,VT05B,VT05C

VTSUBT,	0		/GETS THIRD CHAR OF TERMINAL ESC SEQ
VTM8,	0		/=1 IF UNDER MULTI8
VTSTUF,	0
	6254		/SKIP ON MULTI8
	SKP CLA
	CLA IAC		/SET 1 FOR MULTI8
	DCA	VTM8
	CLA CLL IAC RAL	/GIOT 2
	6770		/TURN ECHO OFF
	CLA
	TAD	(33	/SEND WHAT-ARE-YOU ESCAPE SEQ
	JMS	CPCH
	TAD	(132
	JMS	CPCH
	JMS	CGCH	/GET CHAR OR TIME OUT
	TAD	(-33
	SZA CLA
	JMP	NOGDVT	/JMP IF NOT A VALID ESC SEQ
	JMS	CGCH
	TAD	(-57	/'/' SHOULD FOLLOW
	SZA CLA
	JMP	NOGDVT	/JMP IF NOT VT5X OR VT100
	JMS	CGCH	/CLASSIFY TYPE OF VT5X
VTNOT,	DCA	VTSUBT	/SAVE VT5X TYPE FOR LATER USE
	CLA STL IAC RAL	/GIOT 3
	6770		/TURN ECHO ON AGAIN
	CLA
	TAD	VTSUBT	/WAS IT VT5X?
	SNA CLA
	JMP I	(TECRTN	/NO, DON'T ENABLE VWATCH
	TAD	VTM8	/ARE WE UNDER MULTI8?
	SNA CLA
	JMP I	VTSTUF	/NO, BUT VALID VT52 OR VT100
	TAD	(NOP
	DCA I	(KBDCHK+1	/DISABLE 'KBDCHK'
	DCA I	(VT52KL	/DISABLE TYPEAHEAD HOOKS
	TAD	(NOP
	DCA I	(SETFIN	/AND ANOTHER ONE
	TAD	(NOP
	DCA I	(TRYKBD	/DISABLE KEYBOARD CHECK IN VWATCH PRINT
	JMP I	VTSTUF


CGCH,	0
	CLL STA RTL	/-3
	DCA	CNT
CGCHL,	KSF
	JMP	FIG8
	KRB
	AND	VT177
	JMP I	CGCH
FIG8,	ISZ	CPCH
	JMP	CGCHL
	ISZ	CNT
	JMP	CGCHL
NOGDVT,	TAD	(10	/RUBOUT THE CRAPPY CHAR IF NOT VALID TERMINAL
	JMS	CPCH
	TAD	VT40
	JMS	CPCH
	TAD	(10
	JMS	CPCH
	CDF		/NOW DO MODS TO TECO, ASSUMING A VT05
	TAD	O7200
	DCA I	(VT05A
	TAD	(32
	DCA I	(VT05B
	TAD	(36
	CDF	10
	DCA I	(VT05C
	JMP	VTNOT	/RETURN WITHOUT VWATCH ENABLED

CPCH,	0
	TLS
	TSF
	JMP	.-1
O7200,	7200
	JMP I	CPCH

	PAGE