	.TITLE	DRAW
/
/   8 JUL 77 (011; PDH) ENSURE THAT 'CLEAR' HAS BEEN CALLED
/  12 APR 77 (010; PDH) MAKE 13-BIT ADDRESS FOR 'CHRS .+2'
/   2 FEB 77 (009; PDH) UPDATE TO HANDLE 'TEXT' FILES
/   6 JUL 76 (008: PDH) CONVERT TO XVM/RSX V1A
/   5 JUL 76 (007: PDH) MODIFY FOR USE WITH DOS F4
/  19 MAY 76 (006: PDH) MODIFY TO RUN ON THE PDP-15 WITH THE VT15
/   9 JUL 74 (PDH) REMOVE INTERACTION-WITH-CLOUDY PROBLEM
/  25 JAN 74 (PDH) CORRECT PROBLEM WHEN ADDING ALREADY EXISTING FILE
/   5 DEC 73 (PDH) CORRECT DELETION OF NON-EXISTENT FILE
/  31 OCT 73 - PAUL HENDERSON
/
/  SUBROUTINE TO CONNECT AND DISCONNECT DISPLAY FILES.
/  CALLING SEQUENCE:
/
/	CALL DRAW (IVECT,FIRST,LAST)
/
/  WHERE: IVECT IS THE NAME OF AN ARRAY CONTAINING THE DISPLAY FILE,
/	FIRST IS THE ELEMENT NUMBER OF THE START OF THE DISPLAY FILE,
/	LAST IS THE ELEMENT NUMBER OF THE END OF THE DISPLAY FILE, OR
/	     ZERO IF THE DISPLAY FILE IS TO BE DELETED.
/
/  TO ASSEMBLE FOR USE WITH 'WATRAN', NO PARAMETERS ARE REQUIRED.
/  TO ASSEMBLE FOR DEC FORTRAN, THE SYMBOL
/F4=1	/ MUST BE DEFINED.
/
/  IF ASSEMBLED FOR WATRAN, A SEPARATE ENTRY POINT FOR ASSEMBLY
/  LANGUAGE CALLING PROGRAMS IS PROVIDED.  THE NAME OF THIS ENTRY
/  POINT IS 'DRAWS', AND CALLING SEQUENCE REQUIRED IS SIMILAR TO
/  THE CALLING SEQUENCE FROM DEC FORTRAN.  A VERY MUCH SIMPLIFIED
/  VERSION OF '.DA' IS CODED IN-LINE FOR THIS ENTRY POINT.
/
	.EJECT
/  'DRAW' HAS A QUITE LONG HISTORY, BEING ONE OF THE FIRST MEMBERS
/  OF THE UNIVERSITY OF WATERLOO GRAPHICS LIBRARY.  IT HAS GONE
/  THROUGH SEVERAL CHANGES OF PHILOSOPHY SINCE THE ORIGINAL VERSION,
/  WHICH WAS AN 8K ONLY MODEL.  THE PRESENT DISPLAY HARDWARE ALLOWS
/  DISPLAY FILES TO BE EXECUTED FROM ANY MEMORY BANK.  THE DISPLAY
/  CAN INCREMENT ITS ADDRESS COUNTER ACROSS A BANK BOUNDARY, BUT
/  CANNOT JUMP ACROSS (SUBROUTINE MODE).  THIS REQUIRES SPECIAL CARE
/  WHEN CHAINING FILES SO THAT THE NUMBER OF BANK BOUNDARY CROSSINGS
/  IS MINIMIZED.
/
/  THE VT15 DOES NOT ALLOW A DISPLAY FILE TO CROSS A BANK BOUNDARY.
/  'DRAW' WILL TRUNCATE SUCH FILES AT THE BOUNDARY CROSSING.
/
IDX=ISZ			/ INDEX POINTER, SKIP NOT EXPECTED
INC=ISZ			/ INCREMENT POSITIVE COUNTER
CHRI=     0		/ CHARACTER IMMEDIATE - SINGLE CHARACTER
CHRS=040000		/ CHARACTER STRING (MEMORY REFERENCE)
DJMP=600000
/
	.GLOBL	DRAW,DR.W,FILER,CLEAR,CLOUDY,C.SWTH,P.BASE
/
	.EJECT
/  NORMAL ENTRY FOR DEC FORTRAN, & SPECIAL ENTRY FOR WATRAN ASSEMBLY
/  LANGUAGE CALLS.
/
DRAWS	XX
	.IFDEF	F4
	.GLOBL	.DA
/
	JMS*	.DA
	JMP	.+4
IVCT
FRST
LST
DRAW=DRAWS
IVECT=IVCT
FIRST=FRST
LAST=LST
	.ENDC
	.IFUND	F4
	.GLOBL	DRAWS,.ARG,.ERROR
	LAC	DRAWS		/TRANSFER RETURN POINTER
	DAC	DRAW
	IDX	DRAWS		/POINT TO 1ST ARGUMENT
	LAC*	DRAWS		/GET POINTER TO MACRO CALLING ARGUMENT
	IDX	DRAWS		/INCREMENT TO NEXT ARGUMENT
	DAC	IVECT
	SPA
	LAC*	IVECT		/IN CASE OF DOUBLE INDIRECT
	DAC	IVECT
	LAC*	DRAWS
	IDX	DRAWS		/INCREMENT TO 3RD ARGUMENT
	DAC	FIRST
	SPA
	LAC*	FIRST		/IN CASE OF DOUBLE INDIRECT
	DAC	FIRST
	LAC*	DRAWS
	DAC	LAST
	SPA
	LAC*	LAST
	DAC	LAST
	JMP	ARGSIN		/ARGUMENTS ARE ALL IN NOW
/
ERR65	LAW	65		/ILLEGAL MODE IN MACRO SUBROUTINE ARGUMENT
	JMS*	.ERROR
	.ENDC
	.EJECT
	.IFUND	F4
/  SUBROUTINE TO VALIDATE MODE OF SUBROUTINE ARGUMENTS.  MODE BITS ARE
/  IN AC 0-3 ON ENTRY TO SUBROUTINE.  VALID MODES ARE SINGLE AND
/  DOUBLE INTEGER ONLY.  ALL OTHER MODES GIVE TERMINAL ERROR 65 (OCTAL).
/  CALLING SEQUENCE:
/
/	LAC	ARGUMENT	/POINTER TO DATA WITH MODE IN AC 0-3
/	JMS	VALID8
/	(RETURN POINT IF DOUBLE INTEGER)
/	(RETURN POINT IF SINGLE INTEGER)
/
VALID8	XX
	RAL			/SHIFT ILLEGAL MODE BITS TO LINK & AC0
	SPA!SZL!RAL		/DOUBLE INTEGER BIT TO AC0.  (SPA&SZL)
	JMP	ERR65		/NOT INTEGER MODE ARGUMENT
	SMA			/WILL BE -VE IF DOUBLE INTEGER
	IDX	VALID8		/INCREMENT RETURN ADDRESS FOR SINGLE
	JMP*	VALID8
/
DRAW	XX
	JMS*	.ARG
	JMP	.+4
IVECT
FIRST
LAST
	LAC	IVECT
	JMS	VALID8		/VALIDATE ARGUMENT MODES
	JMP	ERR65		/'IVECT' MUST BE SINGLE INTEGER
	LAC	FIRST
	JMS	VALID8
	IDX	FIRST		/POINT TO LEAST SIGNIFICANT HALF OF DOUBLE
	LAC	LAST
	JMS	VALID8
	IDX	LAST		/IFF DOUBLE INTEGER
	.ENDC
/
ARGSIN	LAC*	CLEAR		/ HAS 'CLEAR' BEEN CALLED?
	SNA			/ IF NOT, IT WILL BE CALLED HERE TO
	JMS*	CLEAR		/ PREVENT A MAJOR DISASTER (HUNG SYSTEM)
	LAC*	FILER
SBASIC	DAC	SBASIC		/START OF BASIC DISPLAY FILE
EBASIC	AAC	2		/END OF BASIC DISPLAY FILE
BEGINA	DAC*	SBASIC		/INSERT POINTER IN BASIC FILE
ENDA	IAC
BEGINB	DAC	EBASIC
ENDB	LAC	(KLEAR		/LINK 'DRAW' TO 'CLEAR'
BEGINC	DAC*	DR.W
ENDC	JMS	KLEAR		/TIE BASIC DISPLAY FILE TOGETHER
SHORTF	LAC	.+2
	DAC	ARGSIN		/SHORT-CIRCUIT THE SETUP ROUTINE
	JMP	.+1
	.EJECT
L.M1	LAW	-1
	TAD*	FIRST
	TAD	IVECT
	TAD*	P.BASE		/ RELOCATE TO REAL ADDRESS
	DAC	BEGINB		/START OF SPECIFIED DISPLAY FILE
L.M2	LAW	-2
	TAD*	LAST
	SAD	L.M2		/IS LAST=0?
	JMP	REMOVE		/YES.  REMOVE EXISTING DISPLAY FILE
	TAD	IVECT
	TAD*	P.BASE		/ ADD PARTITION BASE TO VIRTUAL ADDR
	DAC*	BEGINB		/INSERT POINTER IN NEW FILE
	IAC
	DAC	ENDB		/ END OF NEW DISPLAY FILE
	LAC	BEGINB
	IAC			/ POINT TO BEGINNING OF ACTUAL DISPLAY CODE
	AND	(060000		/ SAVE ONLY BANK BITS
	DAC	SHORTF		/ IN CASE WE NEED TO SHORTEN FILE
	XOR	ENDB
	AND	(060000		/ BEGINNING AND END OF DISPLAY FILE
	SNA			/ MUST BE IN SAME BANK
	JMP	INSERT		/ FILE IS OK.
	LAC*	P.BASE		/ GET BASE ADDRESS OF PARTITION
	AND	(700000		/ NEED ONLY UPPER ADDRESS BITS
	XOR	SHORTF
	XOR	(17777		/ FILE WILL BE TRUNCATED AT BANK BOUNDARY
	DAC	ENDB
	TCA			/ HOWEVER, WE MUST ENSURE ENOUGH ROOM
	TAD	BEGINB		/ (2 LOCATIONS) TO INSERT THE TRUNCATION.
	IAC
	SMA			/ IF NOT ENOUGH ROOM,
	JMP*	DRAW		/ SIMPLY RETURN.
/
	.EJECT
/  INSERT NEW DISPLAY FILE INTO CHAIN
/
INSERT	JMS	SEARCH		/FIND A LIKELY SPOT FOR THE NEW FILE
	CLC			/NEW FILE GOES HIGHEST IN MEMORY
	SNA			/EXISTING FILE RETURNS HERE WITH AC=0
	JMP*	DRAW		/***DON'T INSERT AGAIN!!!***
	INC*	C.SWTH		/INCREMENT FILE COUNTER
	JMS	LINK		/LINK END OF NEW FILE INTO CHAIN
/
/  NOW SEE IF WE HAVE A 'TEXT' FILE.  'TEXT' FILES REQUIRE SPECIAL CARE.
/  THEY ARE OF THE FORM:
/
/ IVECT	0			/ (BEGINB
/	INT!SCALE
/	LPON! . . . !ESCR
/	PTY	IY
/	PTX	IX
/	CHRI!10000 52		/ '*' (UNIQUE TO 'TEXT' FILE)
/	CHRS	.+2		/ INSERTED BY 'DRAW'
/	DJMP	PAST		/ INSERTED BY 'DRAW'
/	.ASCII	' . . . '
/	.ASCII	<15> ; .LOC .-1
/ PAST	DJMP*	.+1
/	(NEXT FILE		/ (ENDB
/
	LAC	BEGINB
	AAC	5
	DAC	BEGINC		/ POINT TO 'TEXT' IDENTIFIER
	LAC*	BEGINC
	SAD	(CHRI!10000 52
	SKP
	JMP	INSRT2		/ NOT A 'TEXT' FILE
	IDX	BEGINC		/ POINT TO 1ST WORD TO BE FILLED
	LAC	BEGINC
	AND	(17777
	TAD	(CHRS 2		 / GENERATE 'CHRS .+2
	DAC*	BEGINC		/ INSERT INTO FILE
	IDX	BEGINC		/ POINT TO 2ND WORD
	LAW	-1
	TAD	ENDB
	AND	(17777
	XOR	(DJMP		/ INSERT 'DJMP PAST'
	DAC*	BEGINC		/ INTO 2ND INTERNAL WORD
INSRT2	LAC	BEGINB
	DAC	BEGINC		/SUBROUTINE 'LINK' USES LOCATIONS
	LAC	ENDA		/ 'ENDB' & 'BEGINC'.
	DAC	ENDB
UNLINK	JMS	LINK		/LINK OLD CHAIN TO NEW LINK
	JMS*	CLOUDY		/NECESSARY FOR SYNCHRONIZATION
	JMP	.+2
	DUMMY			/ARGUMENT FOR 'CLOUDY'
	JMP*	DRAW
/
DUMMY	XX
	JMP*	DUMMY		/DUMMY SUBROUTINE EXECUTED FROM 'CLOUDY'
/
/  REMOVE EXISTING FILE FROM DISPLAY CHAIN
/
REMOVE	JMS	SEARCH		/SEARCH FOR SPECIFIED FILE
	SKP
	SZA!CLC			/NON-ZERO AC MEANS FILE NOT FOUND
	JMP*	DRAW		/FILE NOT FOUND - NO DIAGNOSTIC
	TAD*	C.SWTH		/FILE FOUND.  DECREMENT FILE COUNTER
	DAC*	C.SWTH		/AND UNLINK FROM CHAIN.
	LAC	ENDA		/MOVE 'ENDA' TO
	DAC	ENDB		/'ENDB' BEFORE IT GETS CLOBBBERED
	JMS	NXLINK		/POINT 'BEGINC' TO NEXT FILE
	JMP	UNLINK
	.EJECT
/  SUBROUTINE TO SEARCH THROUGH EXISTING DISPLAY FILES.  THIS SUBROUTINE
/  IS USED FOR BOTH INSERTION & DELETION OF FILES.  SUBROUTINE
/  RETURNS WHEN GOOD SPOT FOR INSERTION IS FOUND, OR WHEN SPECIFIED FILE
/  IS LOCATED, OR WHEN BEGINNING OF BASIC DISPLAY IS FOUND.
/
/  CALLING SEQUENCE:
/
/	JMS	SEARCH
/	(RETURN POINT IF LOCATION NOT FOUND)
/	(RETURN POINT IF LOCATION FOUND)
/
/  **NB** THIS WILL REQUIRE SOME RE-WORK FOR USE IN B/F.
/
SEARCH	XX
	LAC*	FILER		/INITIALIZE POINTER TO BEGINNING OF
	DAC	BEGINC		/BASIC DISPLAY FILE.
/
TRYNXT	JMS	NXLINK		/GET POINTERS TO NEXT LINK IN CHAIN
	SAD	SBASIC		/HAVE WE COME FULL CIRCLE IN SEARCH?
	JMP*	SEARCH		/YES.  EXIT.
	TCA
	TAD	BEGINB		/ (BEGINB-BEGINC)
	SMA!SZA			/SKIP ON (MINUS.OR.ZERO) AC
	JMP	TRYNXT		/NOT CORRECT ONE.  TRY NEXT DISPLAY FILE.
	IDX	SEARCH		/HAVE FOUND CORRECT SPOT.
	JMP*	SEARCH
/
NXLINK	XX
	LAC*	BEGINC		/FIRST WORD IN DISPLAY FILE POINTS TO
	IAC			/SECOND LAST WORD OF DISPLAY FILE.
	DAC	ENDA		/ POINT TO LAST WORD (ADDRESS OF 2ND WORD
	LAW	-1		/ OF NEXT DISPLAY FILE).
	TAD*	ENDA		/ GET POINTER TO ABSOLUTE BEGINNING OF
	DAC	BEGINC		/ NEXT LINK IN CHAIN.
	JMP*	NXLINK
	.EJECT
/  SUBROUTINE TO LINK IN END OF ONE DISPLAY FILE TO BEGINNING OF
/  THE NEXT ONE.
/
LINK	XX
	IDX	BEGINC		/ POINT TO 1ST DISPLAY CODE WORD
	LAW	-1
	TAD	ENDB
	DAC	ENDB		/ POINT TO SECOND LAST WORD OF NEW FILE
	AND	(17777		/ EXTRACT 13-BIT ADDRESS
	TAD	(DJMP* 1	/ GENERATE 'DJMP* LAST WORD'
	DAC*	ENDB
	IDX	ENDB		/ POINT TO LAST WORD
	LAC	BEGINC
	DAC*	ENDB		/ PUT IN ADDRESS OF NEXT LINK
	JMP*	LINK		/LINK NOW TIED IN.
/
/  SUBROUTINE TO TIE BASIC DISPLAY FILE ON ITSELF ON INITIAL CALL
/  TO 'DRAW' AND ALSO ON CALLS TO 'CLEAR'.
/
KLEAR	XX
	LAC	SBASIC		/PUT ARGUMENTS FOR SUBROUTINE 'LINK'
	DAC	BEGINC		/IN PROPER PLACE.
	LAC	EBASIC
	DAC	ENDB
	JMS	LINK		/TIE BASIC FILE ON ITSELF
	JMP*	KLEAR
	.END
