// pjinit.sr

// Last modified November 2, 1979  8:53 PM by Taft

get "BRAVO1.DF";
get "AltoFileSys.D";
get "HEAP.DF";
get "DISPLAY.DF";
get "MEASURE.DF";
get "OM.DF";
get "vm.Df";
get "State.Df";
get "FONT.Df";
get "SELECT.Df";


// Incoming Procedures

external [
	ult;
	errhlta
	move;
	array
	hpinit
	addbp
// 	dumpcore
	ugt
	movec
	array1;
	initom;
	chuckinit;
	findomspace;
	hplay;
	chuckinitselect;
	initsb;
	lastproc
	FtyOpen
	ReadVec;
	umax
	RealDA
	swat
	hpalloca
	EnuStu
	InputStuAb
	activateomseq
	fnalloc
	OutSims
	EnuSimInit
	deallocfn
	deactivateomseq
	MyFrame
	chuckinitformat
	chuckinitdoc
	chuckinitdisplay
	InitCalc
	VirtualDA
	removefont
	flushfn
	puts
	FindMacPos;
	underline
	DAYTIME
	InitVrldsys
	createdocm
	updatedisplay
	selectwholeww
	flushvm
	invalidatewindow
	Init2Rls
	hpcompact;
	CreateVrldsys
	CacheInit
	InitMsg
	fillinfdd
	getbin
	SetRegionW
	SetRegionSys
	FddlMaxInit
	CwFddl;
	FCkUserOf
	errhlt
	initvm
	InitFdh;
	ReadDiskKd;
	WriteDiskKd;
	initkbd
	];


// Incoming Statics

external [
	freet;
	freee;
	mpfunsb;
	rgcdlblank;
	rgmaccp;
	macww;
	vpzone;
	mpfnof;
	fillInDA;
	eofDA;
	DCread;
	dnbp;
	cominstream
	pzone
	stackroot
	tsmacro
	vmtb
	rgbp
	rgvpa
	rglastused
	rgbs
	macbp
	lrutime
	dnfn
	macfn
	cvmfree
	rgnchlast
	vdeltafp
	begofopsys;
	USERNAME;
	mpfnsb;
	SavedDiskKd;
	diskKd;
	vup;
	macfda;
	curfda;
	vcwaitfd;
// 	vputmsbomseq;
// 	vputomfixedstor;
// 	vcfreemin;
	vmeasurestatus;
	voverlay;
// 	vputsbomseq;
	vkcb;
	fpDiskDescriptor;
	fpSysLog;
	vFNoLog;
	vCfaEndRes;
	OsVersion;
	ozone
	ozonel
	vfdd0
	vwaCode
	vcaCode
	vbbs
	fpSysDir
	selmain
	vtodstart
	vcpagehc
	vbttotd
	vbttoti
	mpWwWwd
	vllDcb
	macmn
	mpmnom
	cpscrt
	vcache
	vbp
	rgmpbifb
	vrlwsys
	fpUserCm
	vSkfd
	vfddfirst
	vfontFixed;
	vfddlFixed;
	vcwFontFixed;
	crefKd
	diskBTsize
	nTracks
	nSectors
	nDisks
	nHeads
	defaultVersionsKept
	vswitch
	cfaSysDirEnd
	vyorig
	vdcborig
	vwaitproc
	daPgn1Kd;
	relocTable
	];

// Outgoing Procedures

external [
	pjinit2;
	PreJuntaInit
	];

// Outgoing Statics

external [
	SavedUsername;
	vfIniting
	vmacFrScrInit
	]; 

// Local Statics


manifest [ 
	vcNewest = 2; vcNewestOrNew = 4
	docuserfirst = 7
	risyspast = 3;	// com.df
	cwOhdSv = 14+32+#300	// 14 for SV.H
				// 32 for SV.BLV
				// #300 for SV.page0
	globalSwitches=1	//From Executive -- body is ASCII codes
	] 

structure BLV:
	[ blank	word	26
	startOfStatics	word
	endOfStatics	word
	startOfCode	word
	afterLastCodeWord	word
	endCode	word
	relocTable	word
	] 

structure UPE[		//User param format: 0 terminates
	type bit 12
	length bit 4
	]


// P J I N I T
//
let pjinit2(stacktop) be
[ vfIniting = true
let rgPa = vec lrgPa
let bbs = vbbs
rv #335 = stacktop-stackl;
let thpstart = lastproc; let thpend = nil;
let stm = lastproc;
crefKd = 0
test bbs >> BBS.fInstalled ifnot
	[
	thpend = thpstart + 9000
// hpbuf for compaction !!
	freet = thpend+hpbuf+10;
	freee = (rv #335)-10;
	bbs >> BBS.pArrayMax = freee
	] 
ifso	[ ReadVec(lv (bbs >> BBS.aCfa),bbs >> BBS.cwStm,stm)
	freet = bbs >> BBS.pArrayF;
	freee = bbs >> BBS.pArrayMax;
	thpstart = thpstart+stm >> STM.lnmod+10
	thpend = freet-(hpbuf+10)
	] 
pzone = array(zonel); // a heap just for fndir and fnopsys
initvm(array1);
for i = 1 to 15 do
	addbp(array);
hpinit(thpstart,thpend,4,pzone);
vup = true;
tsmacro = false;
macfda = 0;
curfda = 0;
vcwaitfd = 0;
vFNoLog = true
chuckinitformat(array1);
// chuckinitdisplay uses values set in chuckinitformat!!!!
chuckinitdoc(array1);
chuckinitdisplay(array1);
chuckinitselect(array1);
InitCalc(array);
vcache = CacheInit(10, 10, array)
InitMsg(array)
vSkfd = array1(6,0)

// have we got a da or Virtual da in vCfaEndRes?
if OsVersion gr 4 then
	(vCfaEndRes >> CFA.fa.da) = RealDA(vCfaEndRes >> CFA.fa.da)
test bbs >> BBS.fInstalled ifso
	[ movec(rgPa,rgPa+lrgPa-1,swat)
	rgPa ! ipaA = array; rgPa ! ipaH = hpalloca;
	EnuStu(stm,MyFrame()+ofsetParamF,dtyNil,ipaAb,InputStuAb)
// 	let rgfn = table [ fnscr; fntsmanifest ]
// 	for i = 0 to 1 do
// 		[ 
	let of = mpfnof ! fntsmanifest
	of >> OF.pos = 0
	let rgda = lv (of >> OF.rgda)
	movec(rgda+1,rgda+of >> OF.macfp+1,fillInDA)
	for mn = 1 to macmn-1 do
		[ (mpmnom ! mn) >> OM.ov = 0
		(mpmnom ! mn) >> OM.cref = 0
		] 
	ozonel = 0
	ozone = lastproc

// 	cpscrt = (mpfnof ! fnscr) >> OF.macpos
	let macBiScrInit = (mpfnof ! fnscr) >> OF.macbi
	let fb = getbin(fnscr,macBiScrInit-1)
	fb >> FB.macfr = vmacFrScrInit
	(rgbs ! vbp) << BS.dirty = true
	let mpbifb = rgmpbifb ! fnscr
	movec(mpbifb+macBiScrInit,mpbifb+maxbiscr-1,-1)

// 		] 
// 	vfdd0 >> FDD.rgcc = 0
// 	let cwInc = (stm >> STM.lnmod & (-2))
// 	let hpMinNew = (lastproc+1) & (-2);
// 	let hpMaxNew = (freet-(hpbuf+10+2450)) & (-2) // 2450 for user.cm (sigh)
// 	hpcompact(false,hpMinNew,hpMaxNew)
// 	vup = true
// 	freet = freet-2450
	] 
ifnot	[ let tvec = vec 10;
	let cfa = vec lCFA; move(fpDiskDescriptor,cfa,lFP)
	cfa >> CFA.fa.pageNumber = 0; cfa >> CFA.fa.charPos = 0;
	cfa >> CFA.fa.da = RealDA(cfa >> CFA.fp.leaderVirtualDa); 
	ReadVec(cfa,1,tvec);
	daPgn1Kd = cfa >> CFA.fa.da;
	initom();
	voverlay = true;
// 	activateomseq("JV",0,0)
// 	unless open(fndir,"SysDir",false,true,false,fpSysDir) do
// 		errhlt("sdr");
	] 
// vputomfixedstor = 2210+(lnovhdr+5);
// unless bbs >> BBS.fInstalled then
// 	deactivateomseq("JV","JV")
unless ozonel eq 0 then errhlt("ozl")
] 

and PreJuntaInit(bbs, blv, upe, cfaEndRes, vecCfa) be
[
vcaCode = blv >> BLV.startOfCode
vwaCode = cwOhdSv+(1+blv >> BLV.endOfStatics-blv >> BLV.startOfStatics)
let relocTableBLV = blv>>BLV.relocTable
if relocTableBLV!0 gr relocTable!0 then errhlta(250)
relocTable!0 = relocTableBLV!0
for i = 1 to relocTableBLV!0 do relocTable!i = (relocTableBLV-1)!(2*i)
vbbs = bbs
vswitch = -1
if (upe >> UPE.type eq globalSwitches) & (upe >> UPE.length gr 1) then
	vswitch = upe ! 1
if ((vswitch % #40) eq $i) then
	vbbs >> BBS.fInstalled = false
cfaSysDirEnd = vecCfa
move(fpSysDir,cfaSysDirEnd,lFP)
vyorig = 0;
vdcborig = #420;
// vdcbsys = rv vdcborig;
rv vdcborig = 0;
voverlay = false;
initkbd();
// begofopsys = stackroot-osmachinecodel+1;
// movestack(init,begofopsys-1);
vwaitproc = 0;
// junta();
diskBTsize = diskKd >> KDH.diskBTsize;
nTracks = diskKd >> KDH.nTracks;
nSectors = diskKd >> KDH.nSectors;
nHeads = diskKd >> KDH.nHeads;
nDisks = diskKd >> KDH.nDisks;
defaultVersionsKept = diskKd >> KDH.defaultVersionsKept;
]