.ENABLE SUBSTITUTION
;
;	Build dynamic library loader and test programs
;
.ENABLE QUIET
.SETS BASTYP "BP2"
.SETS RESUIC "[4,1]"
.;	BP2PLS -- Create a 12.k BP2PLS containing all of the normal BASIC2
.;		  resident library plus the RMSSEQ resident library.
.;		  Note:  This version assumes BP2 V2.0!!!
.;	  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:
	;The F4P Plas Resident library is not yet updated for V4.1 F4P
	;Sorry....  See comments in command file if interested in
	;re-implementing these features.
.;	.ASK F4POTS CREATE F4PLAS (DYNAMIC F4P RESIDENT LIBRARY)
.;	.IFT F4POTS .ASK $FCS LINK THE F4PLAS RESIDENT LIBRARY TO FCSRES
	.setf f4pots
	.setf $fcs
	.ASK BP2OTS Create BP2PLS (Dynamic BP2 (V2.0) 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
.BP2:
;
; This section builds the necessary BP2 files.  The ZAP offsets are only for
; V2.0 of BP2
;
	.IFF BP2OTS .GOTO DEMO
.OPEN BP2T.MAC;1
.ENABLE DATA
	.TITLE BP2T
BP2RES:	.RAD50	/'BASTYP'PLS/
$INITM::

	MOV	#BP2RES,R0	; 12.K BASIC2+RMSSEQ RES
	CALL	$LDLIB		; LOAD IT
	JMP	$INITN		; JUMP TO NORMAL INIT CODE
	.END
.DISABLE DATA
.CLOSE
	MAC BP2T.OBJ;1=BP2T				! ASM NEW INIT MODULE
	PIP BINIT.OBJ;*/DE/NM				!
	LBR BINIT=LB:'RESUIC''BASTYP'OTS.OLB/EX:$BINIT	!
.OPEN ZAPBP2.CMD;1					! CHANGE NAME FROM
.ENABLE DATA						! $INITM TO $INITN
BINIT.OBJ;1/AB
232%
35556
232%
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 'BASTYP'RESBLD.CMD;1
.ENABLE DATA
;
LB:'BASTYP'PLS.TSK;1/-HD,MP:'BASTYP'PLS.MAP,SY:'BASTYP'PLS.STB;1= 'BASTYP'PLS/MP
STACK=0
PAR='BASTYP'PLS:120000:60000
/
.DISABLE DATA
.CLOSE
.OPEN 'BASTYP'PLS.ODL;1
.ENABLE DATA

	.ROOT	RM0X3X-RMSSQE-BP2RES
RM0X3X:	.FCTR	RMSIO-RMSCBL-RMSCOM-RMSIX0-RMEXTD-RMSEXC
RMSIO:	.FCTR	LB:'RESUIC'RMSLIB/LB:R0CACH:R0RLCH:R0MAPC:R0RWBF:R0UNLK
RMSCBL:	.FCTR	LB:'RESUIC'RMSLIB/LB:R0RTCB:R0ACBB:R0RTDB:R0MDAT
RMSCOM:	.FCTR	LB:'RESUIC'RMSLIB/LB:R0RMSE:R0IDPB:SAVRG:RQLCB:ARITH
RMSIX0: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TS/LB:$DFIX :$DINT :$DMUL :$DSCAL:$JCON1
	.END


.DISABLE DATA
.CLOSE

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

.DEMO:
;
; THIS SECTION BUILDS THE LOADER TASK AND THE DEMO TASKS
;
.ASK DEMO Build the loader task (LDLIB) or the simple demo tasks
.IFF DEMO .GOTO RUNEM
.BLD:	PIP LDLIBS.MAC/PU/NM,LDLIBT,LIBTST,TESTCL.FTN	! CLEAN UP
;
	.ASK LDR   Build the loader task (LDLIB)
	.ASK F4P   Build F4P demo tasks
	.ASK BP2   Build bp2 demo task
	.IFF LDR .GOTO SKIP1
	; 1) BUILD THE LOADER TASK
	@LDLIBCMD.CMD					! GO BUILD THE TASK
	; 1a)Build CKP
	@CKP
	INS LDLIB					! INSTALL IT
	INS CKP

.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
	.DATA EXIT
	.CLOSE
	.OPEN FOO.B2S
.ENABLE DATA
10	PRINT "Hi there!"
15	X=4.12345678901\Y=SIN(X)
17	INPUT "Who are you";A$
	PRINT A$;", you just got a transient BASIC-Plus-2 library to work"
	PRINT "Contratulations, and happy taskbuilding!"
20	END
.DISABLE DATA
.CLOSE
	.IFNINS BP2 INS $BASIC2/TASK=...BP2		! INSTALL IF NEEDED
	BP2 @FOOASM					! ASSEMBLE
	PIP FOOASM.CMD;*/DE/NM,FOO.B2S;*
.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]'BASTYP'PLS.STB
LIBR:	.FCTR 	LB:'RESUIC''BASTYP'OTS/LB
@LB:'RESUIC''BASTYP'IC1
@LB:'RESUIC'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


