.ENABLE SUBSTITUTION
;
;	Build dynamic library loader and test programs
;
.ENABLE QUIET
.;	BP2RES -- Create a 12.k BP2RES containing all of the normal BASIC2
.;		  resident library plus the RMSSEQ resident library.
.;	  BP2T -- Replaces BP2 initialization module.  Will call LDLIBS to
.;		  load BP2RES, followed by a jump to old init module with a 
.;		  new name.
.;	LDLIBT -- Builds a central task to load resident libraries 
.;		  into PLAS regions.
.;	LDLIBS -- Subroutine to request a resident library to be 
.;		  loaded into a region and mapped to the task.
.;	LIBTST -- Example code section to create a dummy library
.;	TESTCL -- Example to request LIBTST to be loaded and executed.
.;     TESTLIB -- Example of building a program with a Fortran loadable
.;		  resident library(possibly linked with FCSRES).
.;	   F4T -- Replaces F4P initialization module. Will call LDLIBS to
.;		  load F4PLAS, followed by a jump to old init module with
.;		  a new name.
.;  F4PLAS.OLB -- A  F4POTS.OLB is created so that the initialization module
.;		  calls LDLIB to load F4PLAS resident library into a PLAS
.;		  region.
.;	F4PLAS -- A F4P resident library (optionally linked to FCSRES)
.;		  which is identical with a normal F4PRES with the exception
.;		  of the name and that the initialization entry is patched
.;		  to point to the modified initialization routine.
;
;
	.SETF $FCS					! ASSUME NO FCSRES
	.SETS OURUIC <UIC>				! SAVE FOR LATER
	.ASK REBLD REBUILD SOME COMPONENTS
	.IFT REBLD .GOTO BLDEM				! BUILD ALL TEST TASKS
	.GOTO RUNEM					! JUST RUN TASKS
.BLDEM:
	.ASK F4POTS CREATE F4PLAS (DYNAMIC F4P RESIDENT LIBRARY)
	.IFT F4POTS .ASK $FCS LINK THE F4PLAS RESIDENT LIBRARY TO FCSRES
	.ASK BP2OTS CREATE BP2RES (DYNAMIC BP2 AND RMS 12.K RESIDENT LIBRARY)
	.TESTFILE LDLIBS.OBJ
	.IF <FILERR> NE 1 PIP LDLIBS.OBJ;1/DE/NM	! USER SUBROUTINE
	.IF <FILERR> NE 1 MAC LDLIBS.OBJ;1=LDLIBS		! COMPILE IT
	.IFF F4POTS .GOTO BP2				! SKIP BUILDING OTS?
	PIP F4PLAS.OLB;*/DE/NM				! REMOVE OLD COPY
	.TESTFILE LB:[1,1]F4POTS.OLB
	.IF <FILERR> EQ 1 .GOTO OK1			! SKIP IF IT EXISTS
	.DISABLE QUIET					!
	; LB:[1,1]F4POTS.OLB DOES NOT EXIST, EXITING
	.EXIT						! BYE, BYE	
.OK1:	PIP F4PLAS.OLB=LB:[1,1]F4POTS.OLB		! COPY F4POTS.OLB
	LBR F4PLAS/RP=LDLIBS				! INSERT LOADER SUB
	LBR OTI.NEW=F4PLAS/EX:$OTI			! GET OTS INIT MODULE
							! ZAP OTI.NEW TO
.OPEN OTIZAP.CMD;1					!  CHANGE OTI$ TO OTJ$
.ENABLE DATA
OTI.NEW/AB
6%
37200
144%
60352
X
.DISABLE DATA
.CLOSE
	.IFNINS ZAP INS $ZAP				! INSTALL ZAP
	ZAP @OTIZAP.CMD					! ZAP INIT MODULE
	LBR F4PLAS.OLB/RP=OTI.NEW			! REPLACE INIT MODULE
	LBR F4PLAS.OLB/DG:OTI$				! DELETE GLOBAL REF
	MAC F4T.OBJ;1=F4T				! CREATE NEW INIT SUB
	LBR F4PLAS/RP=F4T				! INSERT IT
	PIP F4T.OBJ;*/DE/NM,OTI.NEW;*,OTIZAP.CMD;*	! CLEAN UP
;
; CREATE THE RESIDENT LIBRARY TASK ITSELF (JUST LIKE F4PRES)
;
	.ASK F4PLAS BUILD AND TRANSFER (TO LB:[1,1]) F4PLAS RESIDENT LIBRARY
	.IFF F4PLAS .GOTO BLD				! SKIP IF NO
	@F4PLAS.CMD					! BUILD IT
	SET /UIC=[1,1]					!
	PIP LB:=SY:'OURUIC'F4PLAS.TSK,F4PLAS.STB	! TRANSFER
	PIP LB:F4PLAS.*/PU				! PURGE
	SET /UIC='OURUIC'				! BACK TO START UIC
	.IFT BP2OTS .GOTO BP2
	.ASK BUILD BUILD TASKS NOW
	.IFF BUILD .GOTO RUNEM				! IF NO, SKIP
.BP2:
;
; THIS SECTION BUILDS THE NECESSARY BP2 FILES
;
	.IFF BP2OTS .GOTO NOTNOW
	MAC BP2T.OBJ;1=BP2T				! ASM NEW INIT MODULE
	PIP BINIT.OBJ;*/DE/NM				!
	LBR BINIT=LB:[2,1]BASIC2.OLB/EX:$BINIT
.OPEN ZAPBP2.CMD;1
.ENABLE DATA
BINIT.OBJ;1/AB
232%
35556
X
.DISABLE DATA
.CLOSE
	INS $ZAP					! INSTALL IT
	ZAP @ZAPBP2.CMD					! CHNG NAME TO $INITN
	REM ZAP						! CLEAN UP
	PIP ZAPBP2.CMD;1/DE/NM				!   "   "
.OPEN BP2RESBLD.CMD;1
.ENABLE DATA
;
LB:BP2RES.TSK;1/-HD,NL:,SY:BP2RES.STB;1= BP2RES/MP
STACK=0
PAR=BP2RES:120000:60000
/
.DISABLE DATA
.CLOSE
.OPEN BP2RES.ODL;1
.ENABLE DATA

	.ROOT	RM0X3X-RMSSQE-BP2RES
RM0X3X:	.FCTR	RMSIO-RMSCBL-RMSCOM-RMSIX0-RMEXTD-RMSEXC
RMSIO:	.FCTR	LB:[2,1]RMSLIB/LB:R0CACH:R0RLCH:R0MAPC:R0RWBF:R0UNLK
RMSCBL:	.FCTR	LB:[2,1]RMSLIB/LB:R0RTCB:R0ACBB:R0RTDB:R0MDAT
RMSCOM:	.FCTR	LB:[2,1]RMSLIB/LB:R0RMSE:R0IDPB:SAVRG:RQLCB:ARITH
RMSIX0:	.FCTR	LB:[2,1]RMSLIB/LB:R0RLSB:R0GETB
RMEXTD:	.FCTR	LB:[2,1]RMSLIB/LB:R0EXTD:R1RLBK:R1NXBK:R1WTLS
RMSEXC:	.FCTR	LB:[2,1]RMSLIB/LB:R0RSET:R0WATB:R0RMSA

RMSSQE:	.FCTR	LB:[2,1]RMSLIB/LB:R1CKEF:R1GET:R1GSET:R1GBLD-RMSQPT
RMSQPT:	.FCTR	LB:[2,1]RMSLIB/LB:R1PUT:R1PBLD:R1PSET:R1PUNR

BP2RES:	.FCTR	LB:[2,1]BASIC2/LB:$BER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B:[2,1]BASIC2/LB:$FTIME:$DTIME:$DCMP:$DDIV:$DEXP-BP2RTG
BP2RTG:	.FCTR	LB:[2,1]BASIC2/LB:$DINT:$DLOG:$DMUL:$DSCAL:$DXDD-BP2RTH
BP2RTH:	.FCTR	LB:[2,1]BASIC2/LB:$DXDI:$FINT:$FADDP:$FEXP:$FFIX-BP2RTI
BP2RTI:	.FCTR	LB:[2,1]BASIC2/LB:$FNEXT:$FXFI:$STFNS:$JCON1:$DCON1

	.END
.DISABLE DATA
.CLOSE

	TKB @BP2RESBLD					! BUILD MODIFIED RESLB
	PIP BP2RESBLD.CMD;*/DE/NM,BP2RES.ODL;*		! CLEAN UP
	SET /UIC=[1,1]					!	
	PIP LB:BP2RES.TSK;*/DE/NM,BP2RES.STB;*		! CLEAN UP FIRST
	PIP LB:=SY:'OURUIC'BP2RES.TSK,BP2RES.STB	! TRANSFER
	SET /UIC='OURUIC'				! BACK HOME
	PIP BP2RES.TSK;1/DE/NM,BP2RES.STB;1		! CLEAN UP

.NOTNOW:
	.ASK BUILD BUILD TASKS NOW
	.IFF BUILD .GOTO RUNEM				! IF NO, SKIP

;
; THIS SECTION BUILDS THE LOADER TASK AND THE DEMO TASKS
;
.BLD:	PIP LDLIBS.MAC/PU,LDLIBT,LIBTST,TESTCL.FTN	! CLEAN UP
;
	.ASK F4P BUILD F4P DEMO TASKS
	.ASK BP2 BUILD BP2 DEMO TASK
	.IFINS LDLIB .GOTO SKIP1
	; 1) BUILD THE LOADER TASK
	MAC LDLIBT.OBJ;1=[1,1]EXEMC/ML,'<UIC>'LDLIBT	!
.OPEN LDLIBBLD.CMD
.ENABLE DATA
LDLIB.TSK;1/SL/CP=LDLIBT
/
ASG=TI:2
PRI=80
WNDWS=1
.DISABLE DATA
.IFT $FCS .DATA LIBR=FCSRES:RO:7
.DATA //
.CLOSE
	TKB @LDLIBBLD					! BUILD IT
	INS LDLIB					! INSTALL IT
	PIP LDLIBBLD.CMD;*/DE/NM,LDLIBT.OBJ;*		! CLEAN UP

.SKIP1:	.IFF F4P .GOTO CHKBP2				! NO F4P TASKS
	; 2) CREATE A DUMMY RESIDENT LIBRARY TASK
	MAC LIBTST.OBJ;1=LIBTST				! ASSEMBLE
.OPEN LIBTSTBLD.CMD;1
.ENABLE DATA
LIBTST.TSK;1/-HD/-PI,NL:,SY:LIBTST.STB;1=LIBTST
/
UNITS=0
STACK=0
PAR=LIBTST:160000:20000
//
.DISABLE DATA
.CLOSE
	TKB @LIBTSTBLD					! PSEUDO RESLIB


	; 2) BUILD A TASK TO USE THE DUMMY RESIDENT LIBRARY
	;    BUILD USING STANDARD [1,1]F4POTS.OLB
	;
	PIP TESTCL.OBJ;*/DE/NM				! CLEAN UP
	F4P TESTCL.OBJ;1=TESTCL/NOLINE				!
.OPEN TESTCLBLD.CMD
.ENABLE DATA
TESTCL.TSK;1/CP/FP=TESTCL,LDLIBS,LIBTST.STB,[1,1]F4POTS/LB
/
WNDWS=1
//
.DISABLE DATA
.CLOSE
	TKB @TESTCLBLD					! BUILD TEST
	PIP TESTCLBLD.CMD;*/DE/NM,TESTCL.OBJ;*		! CLEAN UP


	; 3) DEMONSTRATE ABILITY TO BUILD WITH F4PLAS
	;
	PIP TESTLIB.OBJ;*/DE/NM				!
	F4P TESTLIB.OBJ=TESTLIB				!
.OPEN TESTLIB.BLD
.ENABLE DATA
TESTLIB.TSK;1/CP/FP=TESTLIB,[1,1]F4PLAS.STB,SY:'<UIC>'F4PLAS/LB
/
WNDWS=1
.DISABLE DATA
.IFT $FCS .DATA LIBR=FCSRES:RO:7
.ENABLE DATA
//
.DISABLE DATA
.CLOSE
	TKB @TESTLIB.BLD				! BUILD IT
	PIP TESTLIB.BLD;*/DE/NM,TESTLIB.OBJ;*		! CLEAN UP
.CHKBP2:
	.OPEN FOOASM.CMD;1
	.DATA OLD FOO
	.DATA COMPILE FOO.OBJ;1
	.DATA EXIT
	.CLOSE
	.IFNINS BP2 INS $BASIC2				! INSTALL IF NEEDED
	BP2 @FOOASM					! ASSEMBLE
	PIP FOOASM.CMD;*/DE/NM
.OPEN FOOBLD.CMD;1
.ENABLE DATA
SY:FOO.TSK;1/CP/FP=FOO/MP
UNITS = 14
ASG = TI:13
ASG = SY:5:6:7:8:9:10:11:12
EXTTSK= 952 
WNDWS=3
//
.DISABLE DATA
.CLOSE
.OPEN FOO.ODL;1
.ENABLE DATA
	.ROOT 	RESLOD-BASIC2-RMSROT-USER,RMSALL
USER:	.FCTR 	SY:FOO-LIBR
RESLOD:	.FCTR 	BP2T-LDLIBS-LB:[1,1]BP2RES.STB
LIBR:	.FCTR 	LB:[2,1]BASIC2/LB
@LB:[2,1]BP2IC1
@LB:[2,1]RMSRLS
	.END
.DISABLE DATA
.CLOSE
	TKB @FOOBLD					! TASKBUILD FOO
	PIP FOOBLD.CMD;*/DE/NM,FOO.ODL;*,FOO.OBJ;*
;
;
.RUNEM:.ASK RUN RUN THE TEST TASKS
	.IFF RUN .EXIT					! QUIT
	.ASK F4P RUN F4P TASKS
	.ASK BP2 RUN THE BP2 TEST TASK
	.IFNINS LDLIB INS LDLIB				! INSTALL LOADER
	.IFF F4P .GOTO RUNBP2				!
	SET /UIC=[1,1]					! MOVE TO LB:[1,1]
	PIP LB:=SY:'OURUIC'LIBTST.TSK,LIBTST.STB	! TRANSFER RESLIB
	SET /UIC='OURUIC'				! BACK HOME
	.DISABLE QUIET
	;
	; 1) NOW RUN TASK TO TEST LOADING A CODE SECTION 
	;    FROM THE SIMPLE LIBRARY WE CREATED EARLIER.
	;
	RUN TESTCL
	;
	.ENABLE QUIET
	SET /UIC=[1,1]					! MOVE TO LB:[1,1]
	PIP LB:LIBTST.TSK;*/DE/NM,LIBTST.STB;*		! REMOVE TEST RESLIB
	SET /UIC='OURUIC'				! BACK HOME
	.DISABLE QUIET					!
	;						!
	; 2) NOW RUN TASK TO TEST BUILDING WITH F4P RESIDENT
	;    LIBRARY LOADED INTO A REGION (F4PLAS).
	;
	RUN TESTLIB					!
	;
.RUNBP2:.IFF BP2 .EXIT
	.DISABLE QUIET
	;
	; NOW RUN THE TEST BP2 TASK
	;
	RUN FOO
	;
	
	.ENABLE QUIET


