// INITOM  MODULE 

get "CHAR.DF"
get "BRAVO1.DF"
get "AltoFileSys.d"
get "OM.DF"
get "vm.df"

// Incoming Procedures

external [
	ActOnPages
	move
	movec
	gets
	stput
	dirlkup
	opens
	setmacfp
	open
	array
	pjinit1
	errhlta
	normalizefilesb
	RealDA
	lastproc
	];

// Incoming Statics

external [
	mpfnof
	vversion
	vsn1
	vsn2
	vda
	dnbp
	fillInDA
	eofDA
	DCread
	];

// Outgoing Procedures

external [
	initom
	readsbsubsys
	opensavefile
	];

// Outgoing Statics

external [
	ozone
	ozonel
	mpmnom
	macmn
	vCfaEndRes
	];


// Local Statics

static	[
	ozone
	ozonel
	mpmnom
	macmn
	vCfaEndRes
	];

// Local Structures

structure SV:
	[ startingaddress	word
	length		word
	];

// Local manifests

manifest	[
	cda = 500
	]


// I N I T O M
//
let initom() be
	[
	mpmnom = array(maxmn+1);
	movec(mpmnom,mpmnom+maxmn+1,0);	
	let tom = array(lnom);
	mpmnom ! mnfixedstor = tom;
	movec(tom,tom+lnom-1,0);
	tom >> OM.ffixedstor = true;
	tom >> OM.fp = -1;
	tom >> OM.mn = mnfixedstor;
	macmn = 1;
	opensavefile();
	ozone = lastproc;
	ozonel = 0;
	]

// O P E N S A V E F I L E
//
and opensavefile() be
	[
// 	let tsbsubsys = vec 50;
// 	tsbsubsys ! 0 = 0;
// 	readsbsubsys(tsbsubsys);
// 	let tnsbsubsys = vec 50;
// 	tnsbsubsys ! 0 = 0;
// 	normalizefilesb(tsbsubsys,tnsbsubsys);		
// 	unless dirlkup(tnsbsubsys) do errhlt("dir");
// 	let da = RealDA(vCfaEndRes>>CFA.fp.leaderVirtualDa)
// 	opens(fnom,vCfaEndRes>>CFA.fp.version,vCfaEndRes>>CFA.fp.serialNumber.word1,vCfaEndRes>>CFA.fp.serialNumber.word2,da,true,true);
// 	let ofom = mpfnof ! fnom;
// 	ofom >> OF.wf = false;
// 	let tpfnrgda = lv(ofom >> OF.rgda);
// 	let tpfnfileid = lv(ofom >> OF.fileid);
	let tnumcharslast = 0;
// 	ActOnPages(0,tpfnrgda,tpfnfileid,0,1,DCread,lv tnumcharslast,0,dnbp ! bpbuff);
// 	let tsv = dnbp ! bpbuff;
// 	let tfpdsk = 1;
	let fptrOm = array(lFP)
	move(vCfaEndRes,fptrOm,lFP)
	let fid = vec 3
	fid ! 0 = fptrOm >> FP.version
	move(lv (fptrOm >> FP.serialNumber),fid+1,2)
	let pgn = vCfaEndRes>>CFA.fa.pageNumber;
	let cpg = (vCfaEndRes>>CFA.fa.charPos eq 0) ? 1,2
// 	let tcfpdsk = tsv >> SV.length+1;
	let trgda = vec cda;
	movec(trgda,trgda+pgn-1,eofDA);
	movec(trgda+pgn+1,trgda+cda-1,fillInDA);
ckmove:	trgda ! pgn = vCfaEndRes>>CFA.fa.da;
	let pgnlast = nil;
		[ pgnlast = ActOnPages(0,trgda,fid,pgn,pgn+cpg-1,DCread,lv tnumcharslast,0,dnbp ! bpbuff);
		if tnumcharslast ne #1000 do break;
		let tov = dnbp ! bpbuff;
		let tom = array(lnom);
		movec(tom,tom+lnom-1,0);
		tom >> OM.fp = pgn+cpg-2;
		unless tom >> OM.fp eq tov >> OV.fp then errhlta(56)
		tom >> OM.fptr = fptrOm;
		tom >> OM.aFa.pageNumber = tom >> OM.fp+1;
		tom >> OM.aFa.da = trgda ! (tom >> OM.aFa.pageNumber);
		tom >> OM.aFa.charPos = 0;
		tom >> OM.cword = tov >> OV.lnmod;
		macmn = macmn+1;
		if macmn ge maxmn then errhlta(57);
setom:	tom >> OM.mn = macmn;
		mpmnom ! macmn = tom;
		pgn = pgn+cpg-1;
		cpg = ((tov >> OV.lnmod+#377) << lh)+1;
		] repeat
// 	setmacfp(fnom,pgnlast);
// 	move(trgda,lv ((mpfnof ! fnom) >> OF.rgda),pgnlast+2)
	macmn = macmn+1
	]

// R E A D S B S U B S Y S
//
// and readsbsubsys(sb) be
// 	[
// 	if mpfnof ! fncom eq -1 then open(fncom,"COM.CM",false,false);
// 	let tich = 0;
// 		[ let tchar = gets(fncom);
// 		if (tchar eq chcr) % (tchar eq chsp) % (tchar eq $/) then break;
// 		stput(sb,tich,tchar);
// 		tich = tich+1;
// 		] repeat
// 	sb >> SB.siz = tich;
// 	]