// InState.Sr

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

// Incoming Procedures

external [
	move
	hpinit
	ult
	swat
	hpalloca
	array
	LoadCore
	movec
	errhlta
	MyFrame
	ugt
	];

// Incoming Statics

external [
	freee
	freet
	pzone
	];

// Outgoing Procedures

external [
//	InputStm;
	EnuStu;
	InputStuAb;
	StuToSim;
	FillInB;
	];

// Outgoing Statics


// Local Statics


manifest [ 
	pidRgPa = 1 // BEWARE, THIS PARAM IS IN PJINIT.SR
	] 


// let InputStm(cfaStm, vecDestStm, cwMax, arrayF, arrayMax, hpF, hpMac) be
// [ 
// let rgPa = vec lrgPa
// movec(rgPa,rgPa+lrgPa-1,swat)
// rgPa ! ipaA = array; rgPa ! ipaH = hpalloca;
// freee = arrayMax; freet = arrayF; hpinit(hpF,hpMac,4,pzone);
// LoadCore(cfaStm,vecDestStm,cwNil,cwMax)
// unless ult(vecDestStm >> STM.lnmod,cwMax) then errhlt("lnm")
// EnuStu(vecDestStm, MyFrame()+ofsetParamF, dtyNil, ipaAb, InputStuAb)
// ] 

// E N U S T U
//
let EnuStu(stm,fm,dty,ipa,Proc) be
[
let stu = lv (stm >> STM.aStu)
let pEnd = stm+stm >> STM.reltbl; unless @pEnd eq 0 then errhlta(246)
until stu eq pEnd do
	[
	if ((dty eq dtyNil) % (dty eq stu >> STU.dty)) & ((ipa eq ipaNil) % (ipa eq stu >> STU.ipa)) then
		Proc(stu,stm,fm)
	stu = stu+stu >> STU.cw
	if ugt(stu,pEnd) then errhlta(235)
	] 
] 


// I N P U T S T U A B
//
and InputStuAb(stu,stm,fmInputStm) be
[ unless stu >> STU.cw eq cwStuStat then errhlta(236)
StuToSim(stu,stm,fmInputStm ! pidRgPa)
] 

// S T U T O S I M 
//
and StuToSim(stu,stm,rgPa,simDestNoMod; numargs na) be
[ unless stu >> STU.sgrp eq sgrpInit & stu >> STU.sver eq 0 then
	errhlta(237)
let sdm = lv (stu >> STU.aSdm)
let ipa = stu >> STU.ipa
let dty = stu >> STU.dty
let cwStu = stu >> STU.cw
let simSrc = sdm
if dty eq dtyMixed then
	[ let pxBli = sdm;
	simSrc = sdm+pxBli >> PX.cw
	unless stu >> STU.fSeen then
		for ti = 1 to pxBli >> PX.cw-1 do
			FillInB(simSrc+(pxBli ! ti),stm,rgPa,simDestNoMod ! (pxBli ! ti))
	] 
if (dty eq dtyB) & (stu >> STU.fSeen eq false) then
	[ unless cwStu eq cwStuMin then errhlta(238)
	FillInB(sdm,stm,rgPa,@sdm)
	] 
stu >> STU.fSeen = true
if stu >> STU.fNoMod then return
let cwSim = stu+cwStu-simSrc
if (stu >> STU.simDest eq simNil) then
	stu >> STU.simDest = (rgPa ! ipa)(cwSim)
move(simSrc,stu >> STU.simDest,cwSim)
] 


// F I L L I N B
//
and FillInB(lvB,stm,rgPa,simDestNoMod) be
[ let stu = stm+@lvB
if stu >> STU.fNoMod then
	stu >> STU.simDest = simDestNoMod
StuToSim(stu,stm,rgPa,simDestNoMod)
@lvB = stu >> STU.simDest
]