	.TITLE	PLOTER
	.IDENT	/BL1.0/
	.MCALL	$QIOW,$SSDEF,$ASSIGN,$GETDVI,$DVIDEF,$WAITFR,$TTDEF
	.MCALL	$QIO
$DVIDEF
$ttDEF
;
;	THIS IS A SET OF PLOTTING ROUTINES FOR THE DUMB TERMINAL WITH
;	RETROGRAPHICS. IT USES STANDARD PLOT 10 ESC SEQUENCES.
;	This set of routines works with most retrographics equipped
;	terminals and also Visual 102.  It does handshaking if 
;	hostsync is not enabled.  It tests to see if you have graphics
;	by entering graphics mode and requesting handshake.  If none avail
;	It marks it an non graphics terminal and will not use it all after
;	that.
;	This routine assumes that the terminal returns status
;	as 5 chars followed by a "carriage return" and no other terminators.
;
ESC=^O33			; Escape character
PLT.PT=^O34			; Point plot
PLT.VC=^O35			; Plot vector
PLT.AN=^O37			; Alphanumeric mode
PLT.OF=^O30			; Plotting mode off
	.psect	BUFCOM,NOEXE,OVR,GBL,WRT,SHR,LONG,REL,PIC
	.psect	$VARS,NOEXE,CON,LCL,WRT
TTCHAR:	.BYTE	4
;
;	DATA TABLES
;
CROSS:	.BYTE	ESC
	.ASCII	/"4g/			; Cross hair mode
TTNAM:	.ASCID	/TT:/
TTDVI:	.WORD	4,DVI$_DEVDEPEND	; Get device dependent char.
	.LONG	TTCHAR,0		; address of characteristics
EWBUF:	.ascii	<esc>/[f/<esc>/[2J/
	.BYTE	PLT.PT,ESC,^O14		; LOCK KB:,PT MODE,2*CLEAR+HOME,
	.BYTE	ESC,^O5			; REQUEST REPLY WHEN DONE
EWSIZ=.-EWBUF
ADME:	.BYTE	PLT.AN,PLT.OF		; Alphanumeric mode, Terminal mode
PLTST:	.BYTE	PLT.PT,ESC,5		; Go to plot mode + request status
TERM:	.LONG	0,0
IGET:	.BYTE	0
IOSTAT:	.BLKW	1
IOSIZE:	.BLKW	3
TTCHAN:	.BLKW	1			; I/O channel number
PCNT:	.BLKW	1			; CONTAINS PARAMETER COUNT
XHI:	.BYTE	0
XLO:	.BYTE	0
YHI:	.BYTE	0
YLO:	.BYTE	0
CNT:	.WORD	0			; NUMBER OF VECTORS (POINTS TO CONVERT)
PLTYPE:	.WORD	0			; CURRENT TYPE OF PLOT
TTSTAT:	.LONG	0
XPOS:	.LONG	0			; Current X
YPOS:	.LONG	0			; Current Y
CINTEN:	.LONG	1			; Current plot intensity
CHIGH:	.LONG	73.			; Characters/line
CWIDE:	.LONG	35.			; Characters/page
MVERT:	.LONG	779.			; Vertical size
MHOR:	.long	1023.			; Horizontal size
VRES:	.LONG	250.			; Vertical resolution
HRES:	.LONG	512.			; Horizontal resolution
BUFF:	.BLKB	256.			; DATA BUFFER FOR OUTPUT
RQBUF:	.BYTE	PLT.PT,PLT.AN			;4010 MOD,4010 CHAR MOD
	.BYTE	ESC,^O21				;REQUEST INPUT DATA
ADD1:	.BLKB	2				;LINE NUMBER REQUESTED
RQ100:	.BYTE	PLT.PT,PLT.AN			;4010 MOD,4010 CHAR MOD
	.ASCII	<ESC>/"0;/
INY:	.BLKB	20
TESTB:	.ASCIZ	<ESC>/"0;0;1b/
COUNTB:	.ASCIZ	/;153b/
TESTC:	.ASCIZ	<ESC>/"0;0;1c/
COUNTC:	.ASCIZ	/;128c/
;
;	DATA TABLE OF BITS TO PLOT
;
TABL:	.BYTE	^X00, ^X03, ^X0C, ^X0F
	.BYTE	^X30, ^X33, ^X3C, ^X3F
	.BYTE	^XC0, ^XC3, ^XCC, ^XCF
	.BYTE	^XF0, ^XF3, ^XFC, ^XFF
REVTAB:	.byte	^X00, ^X10, ^x08, ^x18
	.byte	^x04, ^x14, ^x0c, ^x1c
	.byte	^x02, ^x12, ^x0A, ^x1a
	.byte	^x06, ^x16, ^x0e, ^x1e
	.byte	^X01, ^X11, ^x09, ^x19
	.byte	^x05, ^x15, ^x0d, ^x1d
	.byte	^x03, ^x13, ^x0b, ^x1b
	.byte	^x07, ^x17, ^x0f, ^x1f
	.psect	PLOTER,EXE,LCL,CON,NOWRT
;
;	CALL ERASE	(THIS ERASES THE SCREEN + HOMES CURSOR)
;
.ENTRY	ERASE,^M<R2>
	BSBW	SCOP
	BITL	#TT$M_HOSTSYNC,TTCHAR
	BNEQ	10$					; Host sync ?
	$QIOW_S	EFN=#1,CHAN=TTCHAN,-
	IOSB=IOSTAT,-
	FUNC=#IO$_ttyreadpall!IO$M_NOECHO!IO$M_TRMNOECHO-
!IO$M_PURGE!IO$M_TIMED!IO$M_NOFORMAT-
	P1=BUFF,P2=#6,P3=#2,P5=#EWBUF,P6=#EWSIZ		; Erase screen
	BRB	20$
10$:	$QIOW_S	EFN=#1,CHAN=TTCHAN,-
	IOSB=IOSTAT,-
	FUNC=#IO$_WRITEVBLK!IO$M_NOFORMAT,-
	P1=EWBUF,P2=#EWSIZ-2				; Erase screen
20$:	BSBW	STCLR					; CLEAR STATUS
;
;	SET BACK TO Terminal mode + ENABLE KB:
;
ADMEND:	$QIO_S	CHAN=TTCHAN,FUNC=#IO$_WRITEVBLK!IO$M_NOFORMAT,-
	P1=ADME,P2=#2				; Return to Terminal mode
	RET
;
;	This routine is called to set up the I/O and check
;	if the terminal is 4010 compatible.  If not return to user!
;
SCOP:	TSTL	TTCHAN				; Is chan set up ?
	BEQL	10$				; No ?
	BLBC	TTSTAT,5$			; Not Graphics terminal ?
	RSB					; Normal return
5$:	RET					; Yes, exit from routine
10$:	$ASSIGN_S	DEVNAM=TTNAM,CHAN=TTCHAN
	CLRL	BUFF				; Clear to set up status
	$QIOW_S	EFN=#1,CHAN=TTCHAN,-
	IOSB=IOSTAT,-
	FUNC=#IO$_ttyreadpall!IO$M_NOECHO!IO$M_TRMNOECHO-
!IO$M_PURGE!IO$M_TIMED!IO$M_NOFORMAT-
		P1=BUFF,P2=#6, -
		P3=#2,P5=#PLTST,P6=#3	; Get plot status
	MOVAL	BUFF,R4				; Now check if buffer correct
	MOVB	(R4)+,R0			; First char
	BICB	#^x9F,R0			; Clear out undefined bits
	CMPB	#^x20,R0			; Check if correct
	BNEQ	20$				; Not correct status ?
	MOVL	(R4)+,R0			; Second char-5'th
	BICL	#^x9F9F9F9F,R0			; Clear extraneous bits
	CMPL	#^x20202020,R0			; Check x,y code
	BNEQ	20$				; Not ok ?
	BICB	#^x80,(R4)			; Clear undefined bits
	CMPB	#^O15,(R4)			; Last one "return"
	BNEQ	20$				; No ?
	MOVB	#-1,TTSTAT
	$GETDVI_S	EFN=#1,CHAN=TTCHAN,ITMLST=TTDVI	; Find out what device
	$WAITFR_S	EFN=#1
20$:	TSTB	TTSTAT				; Graphics terminal ?
	BNEQ	40$				; Yes ?
30$:	RET					; No, exit from routine
40$:	MOVL	#1,CINTEN			; Set up current intensity
	BITL	#TT$M_HOSTSYNC,TTCHAR		; ADM ?
	BNEQ	100$				; no ?
	RSB
100$:	MOVAL	TESTC,R0	; Rest of string
110$:	MOVAL	BUFF,R1		; Get buffer
120$:	MOVB	(R0)+,(R1)+	; Till done
	BNEQ	120$
	SUBL	#BUFF,R1	; String length
	$QIOW_S	EFN=#1,CHAN=TTCHAN,IOSB=IOSTAT,-
	FUNC=#IO$_READPROMPT!IO$M_NOECHO!IO$M_TRMNOECHO-
!IO$M_PURGE!IO$M_TIMED!IO$M_NOFORMAT-
	P1=BUFF,P2=#256,P3=#2,P5=#BUFF,P6=R1
	BLBS	IOSTAT,160$	; Input OK ??
	TSTB	IGET		; Check Input type
	BNEQ	140$		; Tried both ??
	INCB	IGET		; Try again
	MOVAL	TESTB,R0	; Rest of string
	BRW	110$
140$:	MOVB	#-1,IGET	; Kill input
	BRB	200$
160$:	BLBC	IGET,170$	; Retrographics ?
	MOVL	#293,VRES			; Vertical resolution
	MOVL	#768,HRES			; Horizontal resolution
	MOVL	#76,CWIDE			; Characters per width
	MOVL	#24,CHIGH			; Characters per height
	BRB	200$
170$:	MOVL	#240,VRES			; Vertical resolution
	MOVL	#640,HRES			; Horizontal resolution
	MOVL	#80,CWIDE			; Characters per width
	MOVL	#34,CHIGH			; Characters per height
200$:	$QIOW_S	EFN=#1,CHAN=TTCHAN,FUNC=#IO$_WRITEVBLK,-
	P1=ADME,P2=#2				; Return to Terminal mode
	RSB
;
;	CALL	SCTEST(ITERM,[IX],[IY],[INTEN],[CHIGH],[CWIDE],
;		[HEIGHT],[WIDTH],[VRESOL],[HRESOL])
;
.ENTRY	SCTEST,^M<R2,R3,R4,R5>
	CLRL	@4(AP)				; First clear status
	BSBW	SCOP
	CMPB	(AP),#2				; Number of params
	BLSS	15$				; No IX,IY desired ?
	BEQL	5$				; Only ITERM,IX requested ?
	TSTL	12(AP)				; IY requested
	BNEQ	7$				; Yes ?
5$:	TSTL	8(AP)				; Check on IX request
	BEQL	15$				; IX not requested ?
7$:	MOVL	#2,R5
10$:	BSBW	WAIT2
	$QIO_S	CHAN=TTCHAN,FUNC=#IO$_WRITEVBLK!IO$M_NOFORMAT,-
	P1=ADME,P2=#2				; Return to Terminal mode
	DECL	R5
	BLEQ	15$				; Skip next try
	MOVB	(R4)+,R0			; Get status word
	BICB	#^X9F,R0			; Clear extra bits	
	CMPB	#^X20,R0			; Is it correct ?
	BNEQ	10$				; No ?
	BSBW	XYCODE				; Get the coordinate
	BLSS	10$				; Bad data ?
	MOVL	R0,XPOS				; Transfer to variable
	BSBW	XYCODE				; Get the coordinate
	BLSS	10$				; Bad data ?
	MOVL	R0,YPOS				; Save it
	CMPB	(R4),#^O15			; Is last char line feed ?
	BNEQ	10$				; Not ?
15$:	MOVAL	TTSTAT,R0			; Input address
	MOVL	AP,R1				; Output address
	MOVZBL	(AP),R2				; Count
	TSTL	(R1)+
20$:	TSTL	(R1)				; Check to see if param
	BEQL	30$				; No ?
	MOVL	(R0)+,@(R1)+			; Move param
	BRB	40$
30$:	TSTL	(R1)+
	TSTL	(R0)+
40$:	SOBGTR	R2,20$				; Continue till done
	RET

;
;	CALL INTENS(IBRGHT)
;		IBRGHT	= -1	Dots toggled
;		IBRGHT	= 0	ZERO BRIGHTNESS, ERASE LINES
;		IBRGHT	= 1	NON ZERO BRIGHTNESS, DRAW LINES
;
.ENTRY	INTENS,^M<R2,R3,R4>
	BSBW	SCOP		; Graphics scope?
	MOVAL	BUFF,R4
	MOVB	#PLT.PT,(R4)+	; SET POINT MODE
	MOVB	#ESC,(R4)+	; ESCAPE
	BITL	#TT$M_HOSTSYNC,TTCHAR
	BNEQ	40$		; Host sync ?
	TSTB	(AP)		; Any params ?
	BEQL	10$		; None?
	MOVB	@4(AP),CINTEN	; Current intensity
	BEQL	20$		; NONE
10$:	MOVB	#^O141,(R4)+	; NON ZERO BRIGHTNESS
	BRW	90$
20$:	MOVB	#^O177,(R4)+	; ZERO BRIGHTNESS
	BRW	90$
40$:	MOVB	#^A\/\,(R4)+
	TSTB	(AP)		; Check params ?
	BEQL	70$		; None ?
	TSTB	@4(AP)		; Check input param
	BGTR	70$		; 1 or greater ?
	BEQL	60$		; Zero ?
	MOVB	#^A/2/,(R4)+	; Dots toggled (complemented)
	BRB	80$
60$:	MOVB	#^A/1/,(R4)+	; Dots turned off
	BRB	80$
70$:	MOVB	#^A/0/,(R4)+	; Turn on dots
80$:	MOVB	#^A/d/,(R4)+	; End of sequence
90$:	JMP	ADMRET
;
;
;	ROUTINE TO PLOT POINTS
;	CALL PTPL(IX,IY [,ISIZE] )
;
.ENTRY	PTPL,^M<R2,R3,R4>
	BSBW	SCOP
	BSBW	MULTI		; MULTIPLE SET UP
	BSBW	STCLR
	MOVB	#PLT.PT,PLTYPE	; SAVE POINT PLOT TYPE
	JMP	VECT1		; NOW DO PLOT
;
;	ROUTINE TO PLOT VECTORS
;	CALL VECT(IX,IY [,ISIZE])
;		ISIZE < 0 MEANS START VECTOR AT FIRST IX,IY
;		ISIZE > 0 START VECTOR AT LAST IX,IY PLOTTED
;
STGET:	TSTW	YHI		; Any status?
	BEQL	10$		; None?
	MOVB	YHI,(R4)+
	MOVB	YLO,(R4)+
	MOVB	XHI,(R4)+
	MOVB	XLO,(R4)+
10$:	RSB
;
;	CALL	VECT(IX,IY,IPTS)
;		Draws line from last IX,IY to current one or draws
;	series of lines from IX(1),IY(1).... to IX(IPTS),IY(IPTS)
;
.ENTRY	VECT,^M<R2,R3,R4>
	BSBW	SCOP
	BSBW	MULTI		; GET INPUT PARAM
	MOVB	#PLT.VC,PLTYPE	; VECTOR MODE
VECT1:	MOVB	PLTYPE,(R4)+	; SET MODE
	TSTW	(R3)		; TEST FIRST POINT
	BGEQ	1$		; POSITIVE IS OK
	BSBW	STCLR		; CLEAR OLD VALUE
1$:	TSTW	CNT		; CHECK IF NO OLD VECTOR
	BGEQ	2$		; NO
	MNEGW	CNT,CNT		; MAKE IT POSITIVE COUNT
	BSBW	STCLR		; CLEAR OUT OLD STATUS
2$:	BSBW	STGET		; RESTORE OLD STATUS
	BSBW	CONVXY		; CONVERT TO ESC SEQUENCE
	BSBW	WAIT
	TSTW	CNT		; ARE WE DONE?
	BGTR	VECT1		; Do more ?
	JMP	ADMEND

;
;	CALL PLCHAR(IX,IY,ARRAY,ISIZE)
;
CHERR:	RET
.ENTRY	PLCHAR,^M<R2,R3,R4>
	BSBW	SCOP
	CMPB	(AP),#3		; Good params?
	BLSS	CHERR		; no
	BSBW	MULTI1		; MULTIPLE SET UP
	MOVB	#^O35,(R4)+	; Enter vect mode
	CLRW	CNT		; ONLY 1 ADDRESS
	TSTL	R2		; X coord address
	BEQL	10$		; Not specified ?
	TSTL	R3		; Y coord address
	BEQL	10$		; Not specified ?
	CMPW	(R2),#-1	; CHECK IF NO COORDINATE SPEC
	BEQL	10$		; NO COORDINATE
	BSBW	STCLR
	BSBW	CONVXY		; PLOT 1 POINT
10$:	MOVL	12(AP),R0	; BUFFER ADDRESS FOR CHAR
	MOVZWL	(R0),R1		; Length from descriptor
	MOVL	4(R0),R0	; First byt from descriptor
	CMPB	(AP),#4		; Length specified?
	BLSS	30$		; No
	TSTL	16(AP)		; None?
	BEQL	30$		; No param ?
	TSTW	@16(AP)		; None?
	BLEQ	30$		; Yes, too short
	MOVZWL	@16(AP),R1	; NUMBER OF CHAR
30$:	MOVB	#^O37,(R4)+	; ALPHA MODE
40$:	MOVB	(R0)+,R2	; GET CHAR
	BEQL	60$		; IF ZERO WE ARE DONE
	BICW	#^O200,R2	; STRIP TO 7 BIT ASCII
	CMPW	R2,#^O40	; IS IT OK
	BGTR	50$		; YES
	CMPW	R2,#^O15	; IS IT GRAPHICS CONTROL?
	BLEQ	50$		; NO, ACCEPT IT
	MOVW	#^O40,R2	; NO
50$:	MOVB	R2,(R4)+	; SAVE IN BUFFER
	CMPL	R4,#BUFF+70.	; BUFFER FILLED?
	BGTRU	70$		; YES
	SOBGTR	R1,40$		; CONTINUE TILL DONE
60$:	CLRL	R1		; NO MORE CHAR
70$:	MOVL	R1,-(SP)	; Save R1
	MOVL	R0,-(SP)	; and R0
	BSBW	WAIT		; WRITE ALPHA + WAIT FOR END
	MOVL	(SP)+,R0	; restore
	MOVL	(SP)+,R1	; Restore
	TSTW	R1		; ARE WE DONE
	BGTR	30$		; NO, CONTINUE
	JMP	ADMEND

;
ERROR:	RET
;
;	THIS SECTION OUTPUTS THE PLOT + WAITS FOR DONE
;
WAIT:	BITL	#TT$M_HOSTSYNC,TTCHAR	; Check for host/sync
	BEQL	WAIT3		; Not present ?
	SUBL	#BUFF,R4	; NUMBER OF BYTES TO WRITE
	$QIOW_S	EFN=#1,CHAN=TTCHAN,-
	FUNC=#IO$_WRITEVBLK!IO$M_NOFORMAT,-
	P1=BUFF,P2=R4,P3=#2
	MOVAL	BUFF,R4		; RESET R4 TO START OF BUFFER
	RSB
WAIT2:	MOVAL	BUFF,R4		; RESET R4 TO START OF BUFFER
	MOVB	#PLT.PT,(R4)+	; Enter plot mode
WAIT3:	MOVB	#ESC,(R4)+	; ESCAPE
	MOVB	#5,(R4)+	; REQUEST REPLY WHEN DONE
	MOVL	#2,R0		; Timeout count
WAIT1:	SUBL	#BUFF,R4	; NUMBER OF BYTES TO WRITE
	$QIOW_S	EFN=#1,CHAN=TTCHAN,IOSB=IOSTAT,-
	FUNC=#IO$_ttyreadpall!IO$M_NOECHO!IO$M_TRMNOECHO!IO$M_PURGE-
!IO$M_TIMED!IO$M_NOFORMAT-
		P1=BUFF,P2=#6,P3=R0,P4=#TERM,P5=#BUFF,P6=R4
	MOVAL	BUFF,R4		; RESET R4 TO START OF BUFFER
	RSB
;
;	Output line and return
;
ADMRET:	MOVB	#PLT.AN,(R4)+	; Return from plot mode
	MOVB	#PLT.OF,(R4)+	; Turn off 4010 graphics
	SUBL	#BUFF,R4	; NUMBER OF BYTES TO WRITE
	$QIOW_S	EFN=#1,CHAN=TTCHAN,FUNC=#IO$_WRITEVBLK,-
	P1=BUFF,P2=R4
	RET

;
;
STCLR:	CLRW	XHI		; CLEAR OLD X STATUS
	CLRW	YHI		; ALSO Y
	RSB
;
;	SET UP ROUTINE TO CHECK PARAM
;
MULTI:
	CLRW	CNT		; Provisional count
	CMPB	(AP),#2		; CHECK ON # OF PARAM
	BLSS	MERR		; No params
	BEQL	MULTI1		; No count ?
	MOVW	@12(AP),CNT	; BUFFER SIZE
MULTI1:	MOVAL	BUFF,R4		; OUTPUT BUFFER
;	BSBW	SCOPE		; Is it graphics scope
;	BCS	5$		; No
	MOVL	4(AP),R2	; X COORDINATE BUFFER
	MOVL	8(AP),R3	; Y COORDINATE BUFFER
	RSB
MERR:	RET			; Bad call so ignore it
;
;	CALL	CURSOR(IX,IY,ICHAR)
;
.ENTRY	CURSOR,^M<R2,R3,R4,R5>
	BSBW	SCOP
	MOVB	#2,R5		; Number of passes
	CMPB	(AP),#3		; Sufficient params?
	BLSS	MERR		; No?
1$:	DECB	R5
	BLEQ	40$		; 2 errors in a row ?
	MOVAL	BUFF,R4
	CLRL	(r4)		; Clear for status
	CLRL	4(R4)		; ditto
	MOVB	#PLT.VC,(R4)+	; (GS) ENTER VECTOR MODE
;	BITL	#TT$M_HOSTSYNC,TTCHAR	; Check for host/sync
;	BEQL	10$		; Not present ?
;	MOVL	CROSS,(R4)+	; Set up crosshair mode
;	BRB	20$
10$:	MOVB	#ESC,(R4)+	; (ESC) NOW ENTER
	MOVB	#^O32,(R4)+	; (SUB) THE CURSOR MODE
20$:	MOVL	#128.,R0	; Will be timeout count
	BSBW	WAIT1		; GET THE CURSOR POSITION
	BLBC	IOSTAT,40$	; Not normal termination ?
	BICW	#^O200,(R4)	; STRIP OFF EXTRA BITS
	MOVB	(R4)+,@12(AP)	; TYPED CHAR TO USER
	BSBW	XYCODE		; DECODE THE X COORDINATE
	BLSS	1$		; Bad data ?
	MOVL	R0,@4(AP)
	BSBW	XYCODE		; DECODE THE Y COORDINATE
	BLSS	1$		; Bad data ?
	MOVL	R0,@8(AP)
	CMPB	(R4),#^O15	; Is last char Carriage ret ?
	BNEQ	1$		; Not ?
	JMP	ADMEND
40$:	MOVB	#-1,@12(AP)	; Timeout
	JMP	ADMEND
;
;
;
XYCODE:	CVTBL	(R4)+,R0
	BITB	#^X20,R0			; Test if bits ok
	BEQL	10$				; Not OK ?
	BITB	#^X40,R0			; Test if proper bit clear
	BNEQ	10$				; Not OK ?
	BICB2	#^XE0,R0
	CVTBL	(R4)+,R1
	BITB	#^X20,R1			; Test if bits ok
	BEQL	10$				; Not OK ?
	BITB	#^X40,R1			; Test if proper bit clear
	BNEQ	10$				; Not OK ?
	BICB2	#^XE0,R1
	ASHL	#5,R0,R0
	ADDL	R1,R0
	RSB
10$:	MOVL	#-1,R0				; Bad data
	RSB

;
;	SUBROUTINE TO CONVERT X,Y VALUES (  (R2),(R3)  ) AND PUT RESULT
;	INTO BUFFER (R4)
;	R0,R1 ARE DESTROYED
;
CONVXY:
10$:	MOVL	(R3)+,R0	; GET Y VALUE
	BGEQ	30$
	MNEGL	R0,R0		; TAKE ABSOLUTE VALUE OF R0
	BSBW	STCLR
	MOVB	#^O35,PLTYPE	; SET TO PLOT TYPE
	TSTL	(R2)		; IF POSITIVE LEAVE WELL ENOUGH ALONE
	BGEQ	20$		; OK
	MOVB	#^O34,PLTYPE	; SET TO POINT PLOT TYPE
20$:	MOVB	PLTYPE,(R4)+	; SET MODE
30$:	CMPL	R0,#779.	; VERTICAL TOO BIG?
	BLEQU	40$		; NO
	MOVL	#779.,R0	; YES, SET TO MAX
40$:	CLRL	R1
	EDIV	#32,R0,R0,R1	; SPLIT TO 2 DIGITS
	ADDB	#^O40,R0	; ADD ON BLANK
	CMPB	R0,YHI		; IS IT ALREADY SET
	BEQL	50$		; YES, SKIP OUTPUT
	MOVB	R0,(R4)+	; SET UP Y HIBITS
	CLRB	YLO		; MAKE SURE TO DO YLO
50$:	ADDB	#^O140,R1	; NOW IS LOW Y DIGIT
	CMPB	R1,YLO		; DO WE SKIP Y LO
	BEQL	60$		; YES
	MOVB	R1,(R4)+	; PUT YLO INTO BUFFER
60$:	MOVB	R0,YHI		; SAVE YHI
	MOVB	R1,YLO		; SAVE YLO
	MOVL	(R2)+,R0	; GET X
	BGEQ	70$		; NON NEGATIVE LEAVE IT SAME
	MNEGL	R0,R0		; TAKE ABSOLUTE VALUE
70$:	CMPL	R0,#1023	; IS IT TOO BIG?
	BLEQU	80$		; NO
	MOVL	#1023.,R0	; YES, SUBSTITUTE MAX VALUE
80$:	CLRL	R1		; SET UP DOUBLE WORD INTEGER
	EDIV	#32,R0,R0,R1	; SPLIT TO 2 DIGITS
	ADDB	#^O40,R0		; ADD ON BLANK
	CMPB	R0,XHI		; HAS IT CHANGED
	BEQL	110$		; NO
	CMPB	-1(R4),YLO	; WAS YLO PUT IN BUFF?
	BEQL	100$		; YES
	MOVB	YLO,(R4)+	; NO, INCLUDE IT NOW
100$:	MOVB	R0,(R4)+	; PUT XHI INTO BUFFER
110$:	ADDB	#^O100,R1	; NOW IS LOW X
	MOVB	R1,(R4)+	; PUT XLO INTO BUFFER (MANDATORY)
	MOVB	R0,XHI		; SAVE XHI
	MOVB	R1,XLO		; SAVE XLO
	DECW	CNT		; ARE WE DONE?
	BLEQ	120$		; YES
	CMPL	R4,#BUFF+70.	; TEST IF BUFFER IS FULL YET
	BGEQU	120$		; Done?
	BRW	10$		; Not yet
120$:	RSB

;
;	CALL	PLSTAT(IX,IY)
;		RETURNS STATUS IX,IY POSITION
;
PLSTAT::
	CMPB	(AP),#2		; Sufficient params?
	BLSS	20$
	MOVAL	XHI,R4
	MOVW	#2,R3
	BSBW	XYCODE
	MOVL	R0,@4(AP)
	BSBW	XYCODE
	MOVL	R0,@8(AP)
20$:	RET

;
;	Subroutine to read the ADM3A retrographics board
;
.ENTRY	GETSC,^M<R2,R3,R4,R5,R6,R7>
	TSTB	IGET		; Check input status
	BLSS	1$		; No input allowed
	BSBW	SCOP
	CMPB	(AP),#3		; Sufficient params?
	BGEQ	10$		; No?
1$:	RET
10$:	BITL	#TT$M_HOSTSYNC,TTCHAR; VT-100 ?
	BEQL	20$		; no ?
	BRW	100$
20$:	MOVZWL	@4(AP),R0	; Get half line address
	MOVL	#249,R1		; Max line #
	CMPL	R0,R1		; Too big ?
	BGTR	1$		; Yes
	SUBL	R0,R1
	ASHL	#1,R1,R1	; Make it even address
	ASHL	#-5,R1,R0	; SPLIT INTO 2 5 BIT ADDRESSES
	BICB2	#^O360,R0	; Strip extra bits
	BICB2	#^O340,R1
	BISB2	#^O100,R0	; Make it ascii
	BISB2	#^O100,R1
	MOVB	R0,ADD1		; Save in buffer
	MOVB	R1,ADD1+1
;
;	Now request readback
;
	MOVL	8(AP),R2	; Output buffer address
	MOVL	#2,R7		; Number of times through
30$:	$QIOW_S	EFN=#1,CHAN=TTCHAN,IOSB=IOSTAT,-
	FUNC=#IO$_READPROMPT!IO$M_NOECHO!IO$M_TRMNOECHO-
!IO$M_PURGE!IO$M_TIMED!IO$M_NOFORMAT-
	P1=BUFF,P2=#256,P3=#4,P5=#RQBUF,P6=#6
;
;	Return to Terminal mode
;
	$QIO_S	CHAN=TTCHAN,FUNC=#IO$_WRITEVBLK!IO$M_NOFORMAT,-
	P1=ADME,P2=#2		; Return to Terminal mode
	BLBC	IOSTAT,90$	; Abnormal termination ?
	BSBW	CNVLIN
	INCB	ADD1+1
	SOBGTR	R7,30$
90$:	RET
100$:	MOVZWL	@4(AP),R0	; Get line number
	CMPL	R0,#1023	; Check if too big
	BLEQ	105$		; Too big ?
	RET			; Yes !
105$:	CLRB	-(SP)		; Chock
110$:	CLRL	R1
	EDIV	#10,R0,R0,R1	; Get remainder
	ADDL	#^A/0/,R1	; Set up digit
	MOVB	R1,-(SP)	; Save digit
	TSTL	R0		; Check result
	BNEQ	110$		; Not done ?
	MOVAL	INY,R1		; Output address
120$:	MOVB	(SP)+,R0	; Get byte
	BEQL	130$		; Done ?
	MOVB	R0,(R1)+
	BRB	120$
130$:	MOVAL	COUNTC,R0	; Rest of string
	TSTB	IGET		; Check type of get
	BEQL	140$		; Retrographics ??
	MOVAL	COUNTB,R0	; Rest of string
140$:	MOVB	(R0)+,(R1)+	; Till done
	BNEQ	140$
	SUBL	#RQ100,R1	; String length
	$QIOW_S	EFN=#1,CHAN=TTCHAN,IOSB=IOSTAT,-
	FUNC=#IO$_READPROMPT!IO$M_NOECHO!IO$M_TRMNOECHO-
!IO$M_PURGE!IO$M_TIMED!IO$M_NOFORMAT-
	P1=BUFF,P2=#256,P3=#4,P5=#RQ100,P6=R1
	$QIO_S	CHAN=TTCHAN,FUNC=#IO$_WRITEVBLK!IO$M_NOFORMAT,-
	P1=ADME,P2=#2		; Return to Terminal mode
	BLBS	IOSTAT,200$	; Input good ??
	RET
200$:	MOVAL	BUFF,R2		; Input buffer address
	MOVL	8(AP),R3	; Output buffer address
	MOVL	@12(AP),R4	; Output buffer size (bytes)
	MOVL	#-4,R5		; Counts nibbles
210$:	MOVZBL	(R2)+,R0	; Get first byte
	DECW	IOSIZE		; Check number of bytes
	BLSS	400$		; Done ?
	CMPB	R0,#^A/#/	; String of zero bytes ??
	BNEQ	214$		; No ?
	CLRL	R6		; Will be count
211$:	MOVB	(R2)+,R0	; Get count
	SUBB	#^A/0/,R0	; Convert digit
	BLSS	213$		; Done ??
	CMPB	R0,#9		; Check max
	BGTR	213$	
	MULL2	#10,R6		; Shift over R6
	ADDL	R0,R6		; Now modified count
	DECW	IOSIZE		; Check number of bytes
	BLSS	400$		; Done ?
	BRB	211$
213$:	DECL	R2		; Points to correct byte
	TSTL	R6		; Count
	BEQL	400$		; Not ok ?
	BRB	217$		; Now store data
214$:	CMPB	R0,#^A/$/	; Is it string of zero bytes ?
	BNEQ	250$		; No
	MOVZBL	(R2)+,R6	; Get nother byte
	DECW	IOSIZE		; Check number of bytes
	BLSS	400$		; Done ?
	BICB2	#^O300,R6	; Now constitutes count
	BEQL	400$		; Error ?
217$:	CLRB	R0
220$:	BSBW	INSERT
	SOBGTR	R6,220$
	BRB	210$		; more
250$:	CMPB	R0,#^A/ /	; Is it non printable ?
	BLSS	400$		; Yes ?
	BICB2	#^O340,R0	; Now get bit field
	TSTB	IGET		; Test graphics type
	BEQL	260$		; Retrographics ?
	MOVB	L^REVTAB(R0),R0	; Now reverse bits
260$:	BSBW	INSERT
	BRB	210$		; Ok ?
400$:	RET
INSERT:	ASHL	#5,R1,R1	; Make room for extra bits
	BISB2	R0,R1		; Insert into word
	ADDL	#5,R5		; Next position
	BLSS	10$		; No ?
1$:	EXTZV	R5,#4,R1,R7	; Extract byte
	MOVB	L^TABL(R7),(R3)+	; Result to caller
	SOBGTR	R4,5$		; Output array not full ?
	RET			; Done
5$:	SUBL	#4,R5		; Number of bits ready - 4
	BGEQ	1$		; More to transfer ?
10$:	RSB			; Return for more data
;
;	ROUTINE TO CONVERT A LINE OF INPUT TO PLOT LINE
;	R3	= INPUT BUFFER ADDRESS
;	R2	= OUTPUT BUFFER
;	R4	= COUNT
;
CNVLIN: MOVAL	BUFF,R3				; Input buffer address
	MOVL	#64,R4				; MAX NUMBER OF BYTES TO INPUT
	MOVL	#6,R0				; MAX NUMBER OF CHAR TO SEARCH
1$:	CMPB	(R3)+,#^A/=/			; IS IT CORRECT FIRST CHARACTER
	BEQL	CNVT2				; YES
	SOBGTR	R0,1$				; NO
	BRB	CNVT3				; BLANK LINE
CNVT2:	MOVB	(R3)+,R0			; GET CHARACTER
	DECW	IOSIZE				; Check number of bytes
	BLSS	CNVT3				; Done ?
	CMPB	R0,#^A/;/			; IS IT END OF BUFF?
	BEQL	CNVT3				; YES
	CMPB	R0,#^A/#/			; IS IT ZERO COUNT?
	BNEQ	5$				; NO
	MOVZBL	(R3)+,R0			; GET COUNT
	BICB	#^O300,R0			; GET COUNT
	BEQL	CNVT2				; IT IS NFG
	ASHL	#1,R0,R0
2$:	CLRB	(R2)+				; Clear words by count
	DECL	R4
	BLEQ	CNVTEN
	SOBGTR	R0,2$				; Continue ?
	BRB	CNVT2
5$:	CMPB	R0,#^A/A/			; IS CHAR A TO F (HEX INPUT)?
	BGEQ	6$				; YES
	SUBB	#^A/0/,R0			; CONVERT 0 TO 9 TO BINARY
	BRB	7$
6$:	SUBL	#^O67,R0			; CONVERT A TO F TO BINARY
7$:	BICB	#^O360,R0
	MOVB	L^TABL(R0),(R2)+			; INTO OUTPUT BUFFER
11$:	SOBGTR	R4,CNVT2			; CONTINUE TILL DONE
	BRB	CNVTEN
CNVT3:	CLRB	(R2)+				; CLEAR REST OF BUFF
	SOBGTR	R4,CNVT3
CNVTEN:	RSB
	.END
