// G I N I T
// GYPSY CHANGES !!

get "BRAVO.DF";
get "HEAP.DF";
get "DISPLAY.DF";
get "CHAR.DF";
get "GINN.DF"; // **

// Incoming Procedures

external [
	CREATES;
	OPENS;
	ult;
	errhlt;
	bravocom;
	qinit;
	OPENAFILE;
	READVEC;
	CLOSEAFILE;
	updatedisplay;
	move;
	ActOnPages;
	WritePages;
	trims;
	initoverlay;
	junta
	array
	movestack
	swat;
	initkbd;
	addbp
	hpinit
	opens
	creates
	hplay;
	ugt
	setmacfp;
	DisableInterrupts;
	EnableInterrupts;
// **
	invalidatedisplay
	readfile1
	setsel
	marks
	deallocfn
	];

// Incoming Statics

external [
	freet;
	KEYS;
	freee;
	rgsbcom;
	rgsbopand;
	rgdllast;
	rgmaxdl;
	rgcdlblank;
	rgmaccp;
	rgupdate;
	macww;
	vpzone;
	rgcplast;
	rgdoc;
	BITTABLE;
	mpfnof;
	fillInDA;
	eofDA;
	DCread;
	dnbp;
	cominstream
	macbp
	ozone
	pzone
	vup
	LASTSN1
	LASTSN2
	lastSN
	diskBitTable
	diskBTsize
	vdcbsys;
	vdcborig;
	vyorig
	SavedFinish
	SavedKeyProcess;
	stackroot;
	Saved335
// **
	ddoc
	selection
	fdebug
	hdebug
	ozonel
	];

// Outgoing Procedures

external [
	init;
	initsb;
	stperm;
	markcc;
	loadcore;
	dumpcore;
// **
	swaton
	swatoff
	];

// Outgoing Statics

external [ 
	begofopsys;
	topofheap;
	tsmacro
	rgdirty
	vwheel
	sbstate
	sbtermesc
	vmessage
	bbbbb // ** S P Y
	] 

// Local Statics

static [ 
	begofopsys;
	topofheap;
	tsmacro
	rgdirty
	vwheel
	sbstate
	sbtermesc
	vmessage
	bbbbb // ** S P Y
	] 

manifest [ 
	DISKRO = 0;
	maxcpage = 128;
	AdrAdrFinish=#353 //Holds address of finish code
	KeyProcess=12
	interruptVector = #500
	] 

// I N I T
//
let init() be
[
vyorig = 0;
vdcborig = #420;
vdcbsys = rv vdcborig;
rv vdcborig = 0;

initkbd();
begofopsys = stackroot-osmachinecodel+1;
movestack(init,begofopsys-1);
junta();

initoverlay();
rv #335 = begofopsys-stackl;
hpinit(junta,(rv #335)-1-hpbuf,4,pzone);
vup = true;
tsmacro = false;
unless opens(fndir,1,#100000,#144,#10000,false,true) do
	errhlt("sdr");

(mpfnof ! fndir) >> OF.wf = true;
creates(fnopsys,"$OPSYS",true,false);
dumpcore(fnopsys, begofopsys, (#176777-begofopsys+1) lshift 1);
setmacfp(fnopsys,((mpfnof ! fnopsys) >> OF.macpos) << PCD.p+2);

freee = #176777;
freet = rv #335;
if freet << odd then freet = freet + 1 // **
let stackloc = array(stackl);
movestack(init,stackloc+stackl-1); // ,lv pstack,... if any
rv #335 = stackloc;

while macbp ls 8 do addbp(array)

// ** bbbbb = array(#2500) ; // ** S P Y

qinit();

// ** GYPSY moved top of heap expansion from here to QINIT.SR

// ** initsb();

// **
invalidatedisplay(ddoc, rgmaccp ! ddoc-1, -1)
readfile1("GYPSY.DIRECTORY", ddoc, 0) // overlays
rgdirty ! ddoc = false
updatedisplay()
setsel(selection,0,-1)
marks(true) ; // first call on marks
// **

bravocom();
counterjunta();
] 

// ** GYPSY deleted initsb

// S T P E R M
//
and stperm(st, alloc) = valof
	[
	let n = (st >> lh + 2) rshift 1 ;
	let p = alloc(n) ;
	move(st, p, n) ;
	resultis p ;
	]
// M A R K C C
// catalogue no.
and markcc(rgcc,chfirst,chlast,mark) be
for cc = chfirst to chlast do
	rgcc ! cc = (rgcc ! cc) % (#100000 rshift mark);
// D U M P C O R E 
//
and dumpcore(fn,ca,bytec) be
[ let of = mpfnof ! fn;
unless of >> OF.wf do errhlt("wfd");
let cpage = bytec << PCD.p+1;
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 errhlt("aop");
;
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,ca) be
[ let rgca = vec maxcpage;
let macpos = (mpfnof ! fn) >> OF.macpos;
let cpage = macpos << PCD.p+1
let numcharslast = macpos << PCD.rc
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);
unless ActOnPages(rgca-1,rgda,fileid,1,cpage,DCread,0,0,0,0) eq cpage do
	errhlt("ncp");
move(dnbp ! bpbuff,ca,(numcharslast+1) rshift 1);
] 

and counterjunta() be
	[
	rv vdcborig = 0;
	for fn = minuserfn to maxfn-1 do // **
		if mpfnof ! fn ne -1 then deallocfn(fn)
	hplay(-ozonel, true) // **
	if ugt(topofheap, begofopsys-stackl-1) then
		[ // **
		let smaller = topofheap-(begofopsys-stackl-1)
		if smaller << odd then smaller = smaller+1
		hplay(smaller, false)
		]
	@#335=ozone;
	movestack(counterjunta, begofopsys-1)
	loadcore(fnopsys, begofopsys);
	rv LASTSN1 = lastSN ! 0;
	rv LASTSN2 = lastSN ! 1;
	move(diskBitTable, BITTABLE, diskBTsize);
	move(begofopsys, osmachinecode, osmachinecodel);
	movestack(counterjunta,stackroot)
	@#335=Saved335
	rv vdcborig = vdcbsys;
	DisableInterrupts()
	@AdrAdrFinish=SavedFinish
	@(interruptVector+KeyProcess+1) = SavedKeyProcess
	EnableInterrupts()
	finish
	]

and swaton(otherloc, threekeys; numargs N) be
	[
	rv #453 = rv #453 % #400
	if N then rv otherloc = rv otherloc % #400
	vwheel = true
	hdebug = true
	fdebug = threekeys
	]

and swatoff() be rv #453 = rv #453 & #77377