// TInit.Sr

// Last modified November 2, 1979  7:45 PM by Taft


get "BRAVO1.DF";
get "HEAP.DF";
get "DISPLAY.DF";
get "CHAR.DF";
get "MEASURE.DF";
get "AltoFileSys.D";
get "OM.DF";
get "SysDefs.d";
get "VM.Df";
get "State.Df";


// Incoming Procedures

external	[
	ult;
	errhlta
	CommandProcessor;
	chuckinit;
	move;
	ActOnPages;
	WritePages;
	trims;
	array
	movestack
	swat;
	initkbd;
	addbp
	hpinit
	opens
	hplay;
	ugt
	setmacfp;
	sbwsize;
	getchar;
	endofkeystream;
	initom;
	chuckinitselect;
	findomspace;
	CallersFrame;
	Junta;
	pjinit1;
	CounterJunta;
	movec;
	OutSims;
	EnuSimInit;
	fnalloc;
	creates;
	updatedisplay;
	selectwholeww;
	activateomseq;
	PreJuntaInit
	LoadRam
	InitBcplRuntime
	SwappedOut
	];


// Incoming Statics

external	[
	freet;
	freee;
	vpzone;
	mpfnof;
	fillInDA;
	eofDA;
	DCread;
	dnbp;
	voverlay;
	macbp
	ozone
	pzone
	vup
	Saved335
	vdcborig;
	SavedFinish
	SavedKeyProcess;
	vwaitproc;
	vyorig;
	vmeasurestatus;
	diskKd;
	buffer
	lvUserFinishProc
	vfn
	ozonel
	fpSysDir
	vcwFontFixed
	vfontFixed
	vfddlFixed
	vfIniting
	vllDcb
	selmain
	tsread
	vbttoti;
	vbttotd;
	vcpagehc;
	vtodstart;
	ReadCalendar;
//	BravoRamImage
	RamImage
	relocTable
	];


// Outgoing Procedures

external	[
	init;
	initsb;
	stperm;
	markcc;
// 	dumpcore;
	counterjunta;
	SeekTo
//	bravoFinish;
	doDc
	];


// Outgoing Statics

external	[
	begofopsys;
	vkcb;
	macfda;
	curfda;
	vcwaitfd;
	vcfreemin;
	vputomfixedstor;
// 	vputsbomseq;
// 	vputmsbomseq;
	SavedDiskKd;
	vRtcVertInt
	vcaCode
	vwaCode
	vbbs
	cfaSysDirEnd
	vswitch
	]


// Local Statics

static	[
	begofopsys;
	vkcb;
	macfda;
	curfda;
	vcwaitfd;
	vcfreemin;
	vputomfixedstor;
// 	vputsbomseq;
// 	vputmsbomseq;
	SavedDiskKd;
	vRtcVertInt
	vcaCode
	vwaCode
	vbbs
	cfaSysDirEnd
	vswitch
	OsGetFrame
	]


// Local manifests

manifest	[
	DISKRO = 0;
	maxcpage = 128;
	AdrAdrFinish=#353 //Holds address of finish code
	KeyProcess=13
	interruptVector = #500
	ParityProcess=1
	evenmask = #177776
	nextDiskCommand = #521;
	DCseekOnly=#44002
	keystreaml = 100; // LOCAL IN XKBD.SR TOO!!!
	IN = 16;
	OUT = 17;
	FIRST = 15;
	LIMIT = 18;
//	pGetFrame = #370
	]


// I N I T

let init(blv,upe,cfaEndRes) be
[
let bbs = table [ 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0 ]
let rtc = table [ 0; 0 ]
let vecCfa = table [ 0; 0; 0; 0; 0; 0; 0; 0 ]
vbttoti = table [ 0; 0 ];
vbttotd = table [ 0; 0 ];
vcpagehc = 0;
vtodstart = table [ 0; 0 ];
ReadCalendar(vtodstart);
vRtcVertInt = rtc
let mcOK = false
if LoadRam(RamImage) eq 0 then [ mcOK = true; InitBcplRuntime() ]
PreJuntaInit(bbs, blv, upe, cfaEndRes, vecCfa)
Junta((mcOK? levFilePointers, levBcpl),pjinit)
]


// P J I N I T

and pjinit() be
[
// rv #335 = begofopsys-stackl;
let stacktop = CallersFrame()
// vputmsbomseq = "GIJQTLK";
// vputsbomseq = "GIJQT";
// @lvUserFinishProc = bravoFinish
pjinit1(stacktop);
let baseInit = pjinit1
// Here we change all the statics for initialization procedures to point to
// SwappedOut, mainly so that they won't confuse Swat's symbolic
// address printouts.
let relocTableLcl = relocTable  // 'cause its static gets clobbered!
for i = 1 to relocTableLcl!0 do @(relocTableLcl!i) = SwappedOut
let shrinkamount = baseInit-(vpzone >> ZONE.min- (vup ? 0,hpbuf));
shrinkamount = shrinkamount+vcwFontFixed;
if shrinkamount << odd then shrinkamount = shrinkamount+1;
hplay(shrinkamount,true);
unless ozonel eq 0 then errhlta(50)
vfontFixed = baseInit;
ozone = baseInit+vcwFontFixed
vfddlFixed = 0;
vfIniting = false
updatedisplay();
@#420 = vllDcb
selectwholeww(selmain,2);
CommandProcessor();
CounterJunta(PostJunta);
]

// P O S T J U N T A
and PostJunta() be
[ finish
]


// I N I T   M C

// and InitMc() be
// [
// if LoadRam(RamImage) eq 0 then InitBcplRuntime();
// 
// if LoadRam(BravoRamImage, false) ne 0 then	// load, don't boot
// 	errhlta(250);	// < 0 => hardware error; > 0 => bad mc constants
// fix up GetFrame code to use ram:
// OsGetFrame = rv pGetFrame;
// rv pGetFrame = BravoGetFrame;
// ] // end InitMc


// B R A V O F I N I S H
// 
// and bravoFinish() be
// [
// rv pGetFrame = OsGetFrame;
// (mpfnof ! fnDiskDesc)>>OF.wf = true
// dumpcore(fnDiskDesc,diskKd,(lKDHeader+diskBTsize) lshift 1)
// ]


// D U M P C O R E
//
// and dumpcore(fn,ca,bytec) be
// [ let of = mpfnof ! fn;
// unless of >> OF.wf do errhlta(51);
// let cpage = bytec << PCD.p+1;
// if of >> OF.macfp le cpage then setmacfp(fn,cpage+2);
// of = mpfnof ! fn;
// let numcharslast = bytec << PCD.rc ;
// let rgda = lv (of >> OF.rgda);
// let fileid = lv (of >> OF.fileid);
// if (rgda ! 1 eq fillInDA) % (rgda ! 1 eq eofDA) then
// 	if ActOnPages(0,rgda,fileid,0,0,DCread,0,0,dnbp ! bpbuff,0) then errhlta(52);
// let rgca = vec maxcpage+1;
// for i = 0 to cpage-2 do
// 	[ rgca ! i = ca;
// 	ca = ca+#400;
// 	]
// rgca ! (cpage-1) = dnbp ! bpbuff;
// move(ca,dnbp ! bpbuff,(numcharslast+1) rshift 1);
// if numcharslast eq #1000 then
// 	[ numcharslast = 0;
// 	rgca ! cpage = dnbp ! bpbuff;
// 	cpage = cpage+1;
// 	]
// WritePages(rgca-1,rgda,fileid,1,cpage,0,0,numcharslast,0);
// of >> OF.macpos = bytec;
// trims(fn);
// ]

// L O A D C O R E
//
// and loadcore(fn,fp,cwords,ca) be
// [ fp = fp+1
// let rgca = vec maxcpage;
// let cbytes = cwords lshift 1
// let cpage = (cbytes+#777) << PCD.p;
// let numcharslast = (cbytes << PCD.rc);
// if numcharslast eq 0 then numcharslast = #1000
// for i = 0 to cpage-2 do
// 	[ rgca ! i = ca;
// 	ca = ca+#400;
// 	]
// rgca ! (cpage-1) = dnbp ! bpbuff;
// let of = mpfnof ! fn;
// let rgda = lv (of >> OF.rgda);
// let fileid = lv (of >> OF.fileid);
// let fplastread = ActOnPages(rgca-fp,rgda,fileid,fp,fp+cpage-1,DCread,0,0,0,0);	
// unless fplastread eq fp+cpage-1 do errhlt("ncp");
// move(dnbp ! bpbuff,ca,(numcharslast+1) rshift 1);
// ]

// C J F I N I S H
//
// and cjfinish() be
// 	[
// 	errhlt = boot+4;
// 	rv vdcborig = 0;
// 	move(mpfnof ! fnopsys,ozone,ofsiz+129);
// 	mpfnof ! fnopsys = ozone;
// 	@#335=ozone+ofsiz+129;
// 	movestack(counterjunta, begofopsys-1)
// 	loadcore(fnopsys,0,((mpfnof ! fnopsys) >> OF.macpos+1)rshift 1,begofopsys);
// 	move(diskKd,SavedDiskKd,lKDHeader+diskKd >> KD.diskBTsize);
// 	diskKd = SavedDiskKd;
// 	diskKd>>KD.bitTableChanged = true;
//	rv LASTSN1 = lastSN ! 0;
//	rv LASTSN2 = lastSN ! 1;
//	move(diskBitTable, BITTABLE, diskBTsize);
// 	move(begofopsys, osmachinecode, osmachinecodel);
// 	movestack(counterjunta,stackroot)
// 	USERNAME = SavedUsername;
// 	@#335=Saved335
// 	rv vdcborig = vdcbsys;
// 	rv vdcborig = vdcbsys;
// 	let tmax = KEYS ! LIMIT-KEYS !FIRST
// 	let tvec = vec keystreaml;
// 	let i = 0;
// 	until endofkeystream() do
// 		[ tvec ! i = getchar();
// 		i = i+1;
// 		]
// 	if i ge tmax then i = tmax-1
// 	DisableInterrupts()
// 	@AdrAdrFinish=SavedFinish
// 	@(interruptVector+KeyProcess) = SavedKeyProcess
// 	@(interruptVector+ParityProcess) = SavedParityProcess
// 	until endofkeystream() do
// 		PutItem(getchar());
//	KEYS ! OUT = KEYS ! FIRST;
//	KEYS ! IN = KEYS ! FIRST+i
//	move(tvec,KEYS ! FIRST,i);
// 	EnableInterrupts()
// 	finish
// 	]

// S E E K T O
//
and SeekTo(da) be
[ doDc(da,DCseekOnly,0);
]

// D O D C
//
and doDc(da,dc,label) be
[ movec(vkcb,vkcb+lnkcb-1,0);
vkcb >> KCB.headerAddress = lv (vkcb >> KCB.header);
vkcb >> KCB.labelAddress = label;
vkcb >> KCB.dataAddress = dnbp ! bpbuff;
if da eq eofDA % da eq fillInDA then errhlta(54);
vkcb >> KCB.diskAddress = da;
vkcb >> KCB.command = dc;
if (rv nextDiskCommand) ne 0 then errhlta(53)
rv nextDiskCommand = vkcb;
]