// oinit2.sr

get "BRAVO1.DF";
get "DISPLAY.DF";
get "MSG.DF";
get "CALC.DF";
get "DOC.DF";
get "FORMAT.DF";
get "VM.DF";
get "ALTOFILESYS.D";
get "state.DF";

// Incoming Procedures

external	[
	array;
	array1
	errhlt;
	bravocom;
	movec;
	move;
	EtherReport;
	stcopy;
	stappend;
	stsize;
	slput;
	stput;
	stcompare;
	swat;
	slget;
	stget;
	tsflush;
	setreplay;
	hpalloca;
	hpfree;
	ult;
	getvch;
	creates;
	gets;
	puts;
	endofs;
	movestack;
	getfont
	stperm
	hplay
	qinitfcode;
	chuckinitparams;
	createfd;
	chuckreadcom;
	fnalloc;
	junta;
	ratio;
	initCnDcb;
	];

// Incoming Statics

external	[
	vrealx;
	vrealy;
	vrealz;
	vrealw;
	vintradix;
	cdigitpoint;
	frealmode;
	selmain;
	selarg;
	selaux;
	vselcaret;
	cursorstate;
	rgpctb;
	rgmaccp;
	macdoc;
	rgcfn;
	cpscrt;
	rgcpfirst;
	rgcpfirstnew;
	rgyfirst;
	dcb;
	vdcbblack;
	cdcb;
	vyorig;
	cnrgul;
	macdl;
	macww;
//	sbpast;
//	sbstate;
//	sbtermesc;
	rgcpfirstback;
	rgheightdback;
	avdcpfix;
	avdcpl;
	avheight;
	vwwback;
	vxleftmarg;
	vxrightmarg;
	vsncompact;
	fnts;
	mpfrfc;
	vformattedfile;
	rgmpbifc;
	rgmpbifb;
	parstd;
	parsacred;
	ttblsacred;
	parofbounds;
	rgfinfo;
	rgxw;
	msgtbl;
// 	mpfunmagi;
// 	mpfunfddfaulted;
	fddlrutimer;
	mtfirst;
// 	vmag;
	vvpargcc1;
	vvpargcc2;
	vtl;
	vdxleftmarg;
	vdxleftmargf;
	vdxrightmarg;
	vcuripar;
	vmapstate;
	vWaOfSiMac;
// 	vfddfaulted;
	vfwheel;
	vsgh;
	vsgd;
	mpDlLd;
	mpDlLdNew;
	vCnDcbFree;
	mpWwWwd;
	SDldNew;
	SDld;
	vbbs
	vybot
	vlt
	dcpendofdoc
	vmtb;
	rgbp;
	dnbp;
	rgvpa;
	rglastused;
	rgbs;
	macbp;
	lrutime;
	macfn;
	cvmfree;
	vdeltafp;
	rgnchlast;
	mpfnof;
	mpfnsb;
	vkcb;
	vfRemove;
	vdocPtblMac;
	vlptblMac;
	];

// Outgoing Procedures

external [
	chuckinitselect;
	chuckinitbravocom;
	chuckinitdoc;
	chuckinitdisplay;
	chuckinitformat;
	InitCalc;
	initvm
	addbp
	];

// Local structures

structure SW:
	[ ANYCOMCM bit 1;
	ANYREMCM bit 1;
	blank bit 14
	] 

// manifests that are needed from select.df
manifest
	[
	sell = (offset SEL.last)/16
	snone = 0
	ulmode1 = 2
	ulmode2 = 3
	uloff = 0
	]

// Local manifest

manifest
	[ 
	swloc = #1002;
	maxbuf = 5000;
	maxfun = 11
	] 

// B R A V O
// I N I T S E L E C T
// SPE catalogue no.

let chuckinitselect(alloc) be
[
selmain = alloc(sell);
selarg = alloc(sell);
selaux = alloc(sell);
vselcaret = alloc(sell);
vlt = array1(2,0)
selmain >> SEL.type = snone;
selarg >> SEL.type = snone;
selaux >> SEL.type = snone;
vselcaret >> SEL.type = snone;
selmain >> SEL.ulmode = ulmode1;
selarg >> SEL.ulmode = ulmode2;
// sbpast = alloc(50);
// sbpast ! 0 = 0;
cursorstate = -1
] 

// I N I T D O C
// catalogue no.
and chuckinitdoc(alloc) be
[
unless vbbs >> BBS.fInstalled then
	[
	rgpctb = alloc(maxdoc,-1);
	rgmaccp = alloc(maxdoc,0);
	] 
macdoc = 0;
rgcfn = alloc(maxfn,0);
cpscrt = 0;
dcpendofdoc = 7		// length of endmark & trailer in bravo.regions
vdocPtblMac = 0;
vlptblMac = 0;
] 

// I N I T D I S P L A Y

and chuckinitdisplay(alloc) be
[ SDld = alloc((maxdl+1)*lDld,0); SDldNew = alloc((maxdl+1)*lDld,0);
let SWwd = alloc((maxww+1)*lWwd,0)
let wwd = SWwd
mpWwWwd = alloc(maxww+1)
for ww = 0 to maxww do
	[ mpWwWwd ! ww = wwd
	wwd>>WWD.xdUd = xleftmargstd rshift 5;
// 	wwd>>WWD.mag = 100;
	wwd = wwd+lWwd
	] 

rgcpfirst = alloc((maxdl+1),0);
rgcpfirstnew = alloc((maxdl+1));
let dld = SDld
for dl = 0 to maxdl do
	[ dld>>DLD.xdLast = -1
	dld>>DLD.xdFirst = xaudleft
	dld>>DLD.ul = uloff
	] 

rgyfirst = alloc(maxww+1, vybot);

dcb = alloc(maxcdcb*dcbl+1,0);
vdcbblack = alloc(5);
if vdcbblack << odd then 
	vdcbblack = vdcbblack+1
vdcbblack ! 0 = 0;
vdcbblack ! 1 = #40000;
vdcbblack ! 2 = 0;
vdcbblack ! 3 = 303;
if dcb << odd then dcb = dcb+1;
vCnDcbFree = alloc(lCnDcb)
let tDcb = dcb
for n = 1 to maxcdcb-1 do
	[ tDcb>>DCB.next = tDcb+dcbl
	tDcb = tDcb>>DCB.next
	] 
tDcb>>DCB.next = 0
initCnDcb(vCnDcbFree,dcb,maxcdcb)
cdcb = 0;
vyorig = 0;
cnrgul = 0;

macdl = inidl;
macww = 0;


// sbstate = alloc(50);
// sbtermesc = alloc(15);

vfRemove = true;
] // end chuckinitdisplay

// I N I T F O R M A T 
// catalogue no.
and chuckinitformat(alloc) be
[
rgcpfirstback = alloc(maxl);
rgheightdback = alloc(maxl);
avdcpfix = alloc(avl);
avdcpl = alloc(avl);
avheight = alloc(avl);
avdcpfix >> AV.nom = 200;
avdcpfix >> AV.denom = 1;
avdcpl >> AV.nom = 30;
avdcpl >> AV.denom = 1;
avheight >> AV.nom = 16;
avheight >> AV.denom = 1;
vwwback = -1;

vxleftmarg = xleftmargstd;
vxrightmarg = xrightmargstd;

vsncompact = 0;
fnts = fntsmanifest;
// NEW FORMATTING STUFF !!!
// qinitfcode(alloc);
mpfrfc = 0;
vformattedfile = false;
qinitfcode(alloc);
unless vbbs >> BBS.fInstalled then
	[ rgmpbifc = alloc(maxfn,0);
	rgmpbifb = alloc(maxfn,0);
	] 
parstd = alloc(parovhd);
parstd >> PAR.xrightmarg = xrightmargstd;
parstd >> PAR.xleftmarg = xleftmargstd;
parstd >> PAR.xleftmargf = xleftmargstd;
parstd >> PAR.ypos = -1;
parstd >> PAR.spec = specstd;
parstd >> PAR.par = -1;
parstd >> PAR.nextpar = -1;
parstd >> PAR.fOldtab = true
parstd >> PAR.dxtb = dxtbStd;
parstd >> PAR.ykeep = 0;
parstd >> PAR.label = 0;
parsacred = alloc(parovhd);
ttblsacred = alloc(lnttblMax);
parofbounds = alloc(parovhd);
rgfinfo = alloc(maxcharperline);
rgxw = alloc(maxcharperline);
msgtbl = alloc(maxfmsg);
// mpfunmagi = alloc(maxfun,-1);
// mpfunfddfaulted = alloc(maxfun,-1);
fddlrutimer = 0;
// vmag = 0;
vvpargcc1 = 0;
vvpargcc2 = 0;
vtl = alloc((size TL)/16);
vtl >> TL.ww = -1;
vdxleftmarg = ratio(36,635,18);
vdxleftmargf = 0;
vdxrightmarg = 0;
vcuripar = alloc((size IPAR)/16);
vcuripar >> IPAR.doc = -1;
vmapstate = alloc(lmapstate);
vWaOfSiMac = -1;
// vfddfaulted = 0;
vfwheel = false;
vsgh = alloc(sgl,0)
vsgd = alloc(sgl,0)
] 

// I N I T C A L C
//
and InitCalc(alloc) be
	[
	vrealw = alloc(cwmaxreal);
	vrealz = alloc(cwmaxreal);
	vrealy = alloc(cwmaxreal);
	vrealx = alloc(cwmaxreal);
	movec(vrealw,vrealw+cwmaxreal-1,0);
	movec(vrealz,vrealz+cwmaxreal-1,0);
	movec(vrealy,vrealy+cwmaxreal-1,0);
	movec(vrealx,vrealx+cwmaxreal-1,0);
	cdigitpoint = 2;
	frealmode = frealfixed;
	vintradix = 10;
	]

// I N I T V M 
// catalogue no = SPE-84
and initvm(alloc) be
[ vmtb = alloc(vmtbmask+1,0);
rgbp = alloc(vmtbmask+1);
dnbp = alloc(maxbp);
rgvpa = alloc(maxbp,-1);
rglastused = alloc(maxbp);
rgbs = alloc(maxbp);
macbp = 0;
lrutime = 1;
macfn = 0;
cvmfree = vmtbmask+1;
 
vdeltafp = 1;
rgnchlast = alloc(maxbp,#1000);
unless vbbs >> BBS.fInstalled then
	[ mpfnof = alloc(maxfn,-1);
	mpfnsb = alloc(maxfn,0);
	] 
vkcb = alloc(lnkcb);
]

// A D D B P 
// catalogue no. = SPE-85
and addbp(alloc) = valof
[ if macbp eq maxbp then resultis false;
dnbp ! macbp = alloc(chperpage rshift 1);
rglastused ! macbp = 1;
rgvpa ! macbp = -1;
rgbs ! macbp = 0;
macbp = macbp+1;
resultis true;
]