// OutState.Sr
// last modified
// RML October 11, 1977  11:11 AM dump alternate Net Address
// RML December 23, 1977  4:26 PM save all Net addreses

// Last modified October 25, 1979  8:33 PM by Taft

get "BRAVO1.DF";
get "ALTOFILESYS.D";
get "OM.DF";
get "vm.DF";
get "St.DF";
get "State.DF";
get "MSG.DF"
get "FONT.DF"
get "doc.DF"
get "rn1.DF"

// Incoming Procedures

external [
	ult;
	MyFrame;
	errhlta
	move;
	sbwsize;
	mult;
	Lrd;
	];

// Incoming Statics

external [
	mpfnof;
	mpfnsb;
	macmn;
	mpmnom;
	mpfunfd;
// 	vfdd0
	vcwFontFixed;
	vfddfirst
	vfddShortFirst
	vmeasurestatus 
	vxleftmargstd 
	vxleftmargfstd 
	vfloppy 
	vmstransperpg 
	vmsskpertrk 
	vmsheadld 
	vpgpertrk 
	vdxleftmarg 
	vdxrightmarg 
	vdxleftmargf 
	vofsetstd 
	vldlnstd 
	vldhdrstd 
	vytop 
	vymid 
	vybot 
	vxrightmargstd 
	rgmpbifb 
	rgmpbifc 
	vvpampchci 
	rgpctb 
	rgmaccp 
	rgrl 
	vrlfsys 
	vrlwsys 
	vrldsys 
	vmacFrScrInit 
	vfbloclastpar
	mpCrockLrec2
	cpscrt
	vdxtbStd
// 	vSDD
	nTracks
	nSectors
	nHeads
	nDisks
	defaultVersionsKept
	daPgn1Kd;
//	vfEars
	vfDiablomode
	vslPrintBy
//	vsbEarsNetAddress
	vsbPressNetAddress
	vsbAltPressNetAddress
	look1std
	look2std
	] 

// Outgoing Procedures

external [
	OutSims;
	BStuFromSim;
	EnuSimInit;
	];

// Outgoing Statics


// Local Statics


manifest [ 
	pidVecStm = 2;
	pidCwMac = 4;
	pidCwMax = 3;
	] 


let OutSims(EnuSim,stmid,vecStm,cwMax) be
[ let cwMac = lStmHdr
vecStm >> OV.type = otyStm
vecStm >> STM.stmid = stmid
EnuSim(MyFrame()+ofsetParamF,BStuFromSim)
vecStm >> OV.lncode = cwMac-lStmHdr;
let bReltbl = cwMac; @(vecStm+bReltbl) = 0
vecStm >> OV.reltbl = bReltbl;
cwMac = cwMac+1; unless ult(cwMac,cwMax) then errhlta(239)
vecStm >> OV.lnmod = cwMac;
vecStm >> STM.cw = cwMac;
] 

and BStuFromSim(sim,cw,dty,ipa,sgrp,sver,fNoMod,fmOutSims,paramDty,paramIpa; numargs na) = valof
[ if na ls 10 then
	paramIpa = (ipa eq ipaAb) ? sim,simNil
let stm = fmOutSims ! pidVecStm
let stu = stm+fmOutSims ! pidCwMac
stu >> STU.cw = cw+lStuHdr
stu >> STU.dty = dty; stu >> STU.ipa = ipa; stu >> STU.sgrp = sgrp
stu >> STU.sver = sver; stu >> STU.fNoMod = fNoMod;
stu >> STU.simDest = paramIpa
stu >> STU.fSeen = false
let td = 0
switchon dty into
	[ 
case dtyRgb:
	errhlta(240)
	endcase;
case dtyMixed:
	unless na ge 9 then errhlta(241)
	td = paramDty >> PX.cw
	stu >> STU.cw = stu >> STU.cw+td
	move(paramDty,lv (stu >> STU.aSdm),td)
	endcase
default:	endcase
	] 
fmOutSims ! pidCwMac = fmOutSims ! pidCwMac+cw+lStuHdr+td;
unless ult(fmOutSims ! pidCwMac,fmOutSims ! pidCwMax) then
	errhlta(242)
move(sim,stu+lStuHdr+td,cw)
resultis stu-stm
] 

and EnuSimInit(fm,ProcSim) be
[ let tvec = vec 100; let tvec1 = vec 100;
let pxBli = tvec1
// Files - Of's 
OutRgMixed(lv mpfnof,maxfn,ofNil,SizOf,fm)

// Files - Formatting info
OutRgMixed(lv rgmpbifb,maxfn,0,BiMac,fm)
OutRgMixed(lv rgmpbifc,maxfn,0,BiMac,fm)

// Files - Names
OutRgMixed(lv mpfnsb,maxfn,0,SizSbFile,fm)

// Save file - Overlays
let mpMnBOm = tvec
let tiBli = 1
let fptr = (mpmnom ! 2) >> OM.fptr;
let tbFptr = BStuFromSim(fptr,lFP,dtyA,ipaA,sgrpInit,0,false,fm)
move(mpmnom,mpMnBOm,maxfn)
for mn = 0 to macmn-1 do
	[ let om = mpmnom ! mn
	if om eq omNil then loop
	pxBli ! tiBli = mn; tiBli = tiBli+1
	let tpxBli = table [ 2; (offset OM.fptr)/16 ] 
	om >> OM.fptr = tbFptr
	mpMnBOm ! mn = BStuFromSim(om,lnom,dtyMixed,ipaA,sgrpInit,0,false,fm,tpxBli)
	om >> OM.fptr = fptr
	] 
pxBli >> PX.cw = tiBli; 
let tb = BStuFromSim(mpMnBOm,macmn,dtyMixed,ipaA,sgrpInit,0,false,fm,pxBli)
BStuFromSim(lv tb,1,dtyB,ipaAb,sgrpInit,0,false,fm,0,lv mpmnom)
BStuFromSim(lv macmn,1,dtyA,ipaAb,sgrpInit,0,false,fm,0,lv macmn)

// Fonts
let mpFunBFd = tvec
move(mpfunfd,mpFunBFd,maxfun)
let tiBli = 1
let pxBli = tvec1
let pxBliFdd = table [ 2; (offset FDD.fddnext)/16 ]
let fdd = vfddfirst;
while fdd ne 0 do
	[ if fdd >> FDD.font ne 0 then errhlta(243)
	let lfdd = fddl+sbwsize(lv (fdd >> FDD.rvsbname))
	let dty = (fdd >> FDD.fddnext eq 0) ? dtyA,dtyMixed
	fdd >> FDD.b = BStuFromSim(fdd,lfdd,dty,ipaA,sgrpInit,0,false,fm,pxBliFdd)
	fdd = fdd >> FDD.fddnext
	] 
let stm = fm ! pidVecStm
fdd = vfddfirst;
	[ let stu = stm+fdd >> FDD.b; let stm = lv (stu >> STU.aSdm)
	let tfdd = stm+stm>>PX.cw
	fdd = fdd >> FDD.fddnext
	if fdd eq 0 then break
	tfdd >> FDD.fddnext = fdd >> FDD.b
	] repeat
for fun = 0 to maxfun-1 do
	[ let fd = vec 60; let tfd = mpfunfd ! fun
	if tfd eq fdnil then loop
// 	let maxmagi = tfd >> FD.maxmagi
// 	let fdh = (lv(tfd >> FD.rvrgfdd))+maxmagi;
// 	let cwFd = fdl+maxmagi+fdhl+sbwsize(lv (fdh >> FDH.rvsbname))
	let fdh = lv(tfd >> FD.fdh);
	let cwFd = fdl+sbwsize(lv (fdh >> FDH.rvsbname))
	if cwFd gr 60 then errhlta(244)
	move(mpfunfd ! fun,fd,cwFd)
	pxBli ! tiBli = fun; tiBli = tiBli+1
	let rgBfdd = lv (fd >> FD.fddLarge)
	let ofsetMap = (offset FD.fddLarge)/16
// 	let rgBfdd = lv (fd >> FD.rvrgfdd)
// 	let ofsetMap = (offset FD.rvrgfdd)/16
	let pxBli1 = vec 20; let iBli1 = 1
// 	for tn = -1 to maxmagi-1 by 1 do
	for tn = 0 to 1 do
		[ let fdd = rgBfdd ! tn;
		pxBli1 ! iBli1 = tn+ofsetMap; iBli1 = iBli1+1
// 		test fdd >> FDD.fddindirect eq 0 ifnot
// 			[ let bfddShort = vec fddlshort
// // 			bfddShort >> FDD.mag = fdd >> FDD.mag
// 			let fddIndirect = fdd >> FDD.fddindirect;
// 			if fddIndirect >> FDD.fddindirect ne 0 then
// 				errhlta(245)
// 			bfddShort >> FDD.fddindirect = fddIndirect >> FDD.b
// 			rgBfdd ! tn = BStuFromSim(bfddShort,fddlshort,dtyMixed,ipaA,sgrpInit,0,false,fm,table [ 2; 1] )
// 			] 
// 		ifso	
		rgBfdd ! tn = fdd >> FDD.b
		] 
	pxBli1 >> PX.cw = iBli1; 
	mpFunBFd ! fun = BStuFromSim(fd,cwFd,dtyMixed,ipaA,sgrpInit,0,false,fm,pxBli1)
	] 
pxBli >> PX.cw = tiBli; 
tb = BStuFromSim(mpFunBFd,maxfun,dtyMixed,ipaA,sgrpInit,0,false,fm,pxBli)
BStuFromSim(lv tb,1,dtyB,ipaAb,sgrpInit,0,false,fm,0,lv mpfunfd)
BStuFromSim(lv vcwFontFixed, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
// BStuFromSim(lv (vfdd0 >> FDD.b),1,dtyB,ipaAb,sgrpInit,0,false,fm,0,lv vfdd0)
BStuFromSim(lv (vfddfirst >> FDD.b),1,dtyB,ipaAb,sgrpInit,0,false,fm,0,lv vfddfirst)
let tb =  BStuFromSim(mpCrockLrec2,3,dtyA,ipaA,sgrpInit,0,false,fm)
BStuFromSim(lv tb,1,dtyB,ipaAb,sgrpInit,0,false,fm,0,lv mpCrockLrec2)

BStuFromSim(lv nTracks, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv nHeads, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv nDisks, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv nSectors, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv defaultVersionsKept, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv daPgn1Kd, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)

// User Parameters
BStuFromSim(lv vmeasurestatus, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv vxleftmargstd, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv vxleftmargfstd, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv vxrightmargstd, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv vdxtbStd, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv vfloppy, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv vmstransperpg, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv vmsskpertrk, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv vmsheadld, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv vpgpertrk, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv vdxleftmarg, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv vdxrightmarg, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv vdxleftmargf, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv vofsetstd, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv vldlnstd, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv vldhdrstd, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv vytop, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv vymid, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv vybot, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
//BStuFromSim(lv vfEars, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv vfDiablomode, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv look1std, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv look2std, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)

let tl = 1+(vslPrintBy>>SL.cch+1)/2;
let tb = BStuFromSim(vslPrintBy, tl, dtyA, ipaA, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv tb,1,dtyB,ipaAb,sgrpInit,0,false,fm,0,lv vslPrintBy)

//tl = (vsbEarsNetAddress>>SB.cch+2)/2;
//tb = BStuFromSim(vsbEarsNetAddress, tl, dtyA, ipaA, sgrpInit, 0, false, fm, 0)
//BStuFromSim(lv tb,1,dtyB,ipaAb,sgrpInit,0,false,fm,0,lv vsbEarsNetAddress)

tl = (vsbPressNetAddress>>SB.cch+2)/2;
tb = BStuFromSim(vsbPressNetAddress, tl, dtyA, ipaA, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv tb,1,dtyB,ipaAb,sgrpInit,0,false,fm,0,lv vsbPressNetAddress)

tl = (vsbAltPressNetAddress>>SB.cch+2)/2;
tb = BStuFromSim(vsbAltPressNetAddress, tl, dtyA, ipaA, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv tb,1,dtyB,ipaAb,sgrpInit,0,false,fm,0,lv vsbAltPressNetAddress)

// Scratch file
BStuFromSim(lv vmacFrScrInit, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv vfbloclastpar, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)
BStuFromSim(lv cpscrt, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)

// Command Processor
BStuFromSim(lv vvpampchci, 1, dtyA, ipaAb, sgrpInit, 0, false, fm, 0)

// Documents
OutRgMixed(lv rgpctb,maxdoc,-1,SizPctb,fm)
let tb =  BStuFromSim(rgmaccp,maxdoc,dtyA,ipaA,sgrpInit,0,false,fm)
BStuFromSim(lv tb,1,dtyB,ipaAb,sgrpInit,0,false,fm,0,lv rgmaccp)

// Regions
let rgBrl = vec nrlmax
move(rgrl,rgBrl,nrlmax)
for i = 0 to 2 do
	[ let nrl = i;
	let lvrl = selecton i into
		[ 
case 0:		lv vrlfsys
case 1:		lv vrlwsys
case 2:		lv vrldsys
		] 
	let rl = @lvrl
	let rlB = vec lrl; let bimac = rl >> RL.bimac
	move(rl,rlB,lrl)
	rlB >> RL.mpbirifirst = BStuFromSim(rl >> RL.mpbirifirst,bimac+1,dtyA,ipaA,sgrpInit,0,false,fm)
	rlB >> RL.mpbivpa = BStuFromSim(rl >> RL.mpbivpa,bimac,dtyA,ipaA,sgrpInit,0,false,fm)
	rlB >> RL.mpbifcfirst = BStuFromSim(rl >> RL.mpbifcfirst,bimac,dtyA,ipaA,sgrpInit,0,false,fm)
	let pxBliRl = table [ 4; 4; 5; 6 ]
	if rl >> RL.fRes then
		[ 
// GOOD ONLY FOR SYSDOC - 2 SHOULD BE Lrd(rl >> RL.krl)
		let cwRb = lrx+2*(rl >> RL.rimax)
		rlB >> RL.rb = BStuFromSim(rl >> RL.rb,cwRb,dtyA,ipaA,sgrpInit,0,false,fm)
		pxBliRl = table [ 5; 4; 5; 6; 11 ]
		]
	let tb =  BStuFromSim(rlB,lrl,dtyMixed,ipaA,sgrpInit,0,false,fm,pxBliRl)
	BStuFromSim(lv tb,1,dtyB,ipaAb,sgrpInit,0,false,fm,0,lvrl)
	unless i eq 3 then rgBrl ! i = tb
	] 
let pxBliRgRl = table [ 3; 0; 1 ]
let tb =  BStuFromSim(rgBrl,nrlmax,dtyMixed,ipaA,sgrpInit,0,false,fm,pxBliRgRl)
BStuFromSim(lv tb,1,dtyB,ipaAb,sgrpInit,0,false,fm,0,lv rgrl)
] 

// O U T R G M I X E D
//
and OutRgMixed(lvMpFooBar,fooMax,barNil,SizBar,fm) be
[ let mpFooBbar = vec 100; let pxBli = vec 100
let mpFooBar = @lvMpFooBar
move(mpFooBar,mpFooBbar,fooMax)
let tiBli = 1
for foo = 0 to fooMax-1 do
	[ let bar = mpFooBar ! foo
	if bar eq barNil then loop
	pxBli ! tiBli = foo; tiBli = tiBli+1
	mpFooBbar ! foo = BStuFromSim(bar,SizBar(foo),dtyA,ipaH,sgrpInit,0,false,fm)
	] 
pxBli >> PX.cw = tiBli; 
let tb =  BStuFromSim(mpFooBbar,fooMax,dtyMixed,ipaA,sgrpInit,0,false,fm,pxBli)
BStuFromSim(lv tb,1,dtyB,ipaAb,sgrpInit,0,false,fm,0,lvMpFooBar)
] 

// S I Z 
//
and SizOf(fn) = ofsiz+(mpfnof ! fn) >> OF.macfp
and BiMac(fn) = ((fn eq fnscr) ? maxbiscr,(mpfnof ! fn) >> OF.macbi)+1
and SizSbFile(fn) = sbwsize(mpfnsb ! fn)
and SizPctb(doc) = pctbovhd+mult(pcdsiz+1,(rgpctb ! doc) >> PCTB.maxpc)