// rninit.sr
// Carol Hankins
// March 26,1976

// Last modified October 25, 1979  6:54 PM by Taft

get "bravo1.df"
get "vm.df"
get "rn1.df"
get "doc.df"
get "msg.df"
get "com.df"

//incoming procedures
external
	[
	createdoce
	array
	errhlt
	getvp
	VpaGetOne
	VpaGetMany
	VpaGetScratchBin
	DirtyBp
	PutSbScr
	array1
	]

//incoming statics
external
	[
	vfb
	vbp
	cpscrt
	deltacp
	look1std
	look2std
	]

//outgoing procedures
external
	[
	Init1Rls
	Init2Rls
	AppendRegion
	]

//outgoing statics
external
	[
	rgrl
	vrlfsys
	vrldsys
	vrlwsys
	vcridpast2
	vrgridpast2
	]

static
	[
	rgrl
	vrlfsys
	vrldsys
	vrlwsys
	vcridpast2
	vrgridpast2
	]

let RlCreate(krl,undocfn,rimax,fRes) = valof
[

let trl = nil
let tbimax = nil

trl = array1(lrl,0)
trl>>RL.krl = krl
trl>>RL.doc = undocfn
trl>>RL.rimac = 0
trl>>RL.rimax = rimax
trl>>RL.fRes = fRes
test fRes ifso
	[ trl>>RL.rb = array(lrx+Lrd(krl)*rimax)
	InitRb(trl>>RL.krl,trl>>RL.rb,0,0,rimax)
	] 
ifnot	[ tbimax = IntCeilingDiv(rimax,DriMax(trl))
	trl>>RL.bimac = 0
	trl>>RL.bphint = 0
	trl>>RL.VpaGet = errhlt

	trl>>RL.mpbirifirst = array(tbimax + 1)
	trl>>RL.mpbivpa = array(tbimax)
	trl>>RL.mpbifcfirst = array(tbimax)
	] 

resultis trl

]


and IntCeilingDiv(int1,int2) = valof
[
resultis ( (int1+int2-1)/int2 )
]


and DriMax(rl) =  ( (256-lrx) / Lrd(rl>>RL.krl)  )

and Lrd(krl) = ((krl eq krlw) ? 3,2)

and AppendRegion(rl,unfccp,dunfccp,dfcmax) be
[

let trb = nil
test rl >> RL.fRes ifso
	trb = rl >> RL.rb
ifnot	[ let tbp = nil

	let tbimac = rl>>RL.bimac
	if tbimac gr 0 then trb = getvp((rl>>RL.mpbivpa) ! (tbimac-1) )
	tbp = vbp

	if tbimac eq 0 % trb>>RX.dri ge DriMax(rl) then
		[
		rl>>RL.mpbirifirst ! tbimac = rl>>RL.rimac
		rl>>RL.mpbifcfirst ! tbimac = unfccp
		rl>>RL.mpbivpa ! tbimac = VpaGetScratchBin()
		rl>>RL.bimac = rl>>RL.bimac + 1
		rl>>RL.VpaGet = valof
			[
			test rl>>RL.bimac eq 1
				ifso resultis VpaGetOne
				ifnot resultis VpaGetMany
			]
		trb = getvp( (rl>>RL.mpbivpa) ! (tbimac) )
		tbp = vbp
		InitRb(rl>>RL.krl,trb,rl>>RL.rimac,tbimac,DriMax(rl))
		] 
	DirtyBp(tbp)
	]

let trimac = rl>>RL.rimac
let riFirst = trb>>RX.rifirst
let fcFirst =  0
unless (rl >> RL.fRes) then
	[ let tbimac = rl>>RL.bimac
	fcFirst = rl>>RL.mpbifcfirst ! (tbimac-1)
	rl>>RL.mpbirifirst ! tbimac = trimac + 1
	] 

(trb+trb>>RX.brgdfcfirst) ! (trimac-riFirst ) =
		unfccp - fcFirst
(trb+trb>>RX.brgdfc) ! (trimac-riFirst ) =
		dunfccp 
if rl>>RL.krl eq krlw then
(trb+trb>>RX.brgdfcmax) ! (trimac-riFirst ) =
		dfcmax

trb>>RX.dri = trb>>RX.dri + 1
rl>>RL.rimac = trimac + 1


]


and FcGetRegionW(dfc) = valof
[

let tfc,asb = nil,nil

tfc = cpscrt
tfc = (tfc+1) logand #177776
dfc = (dfc logand #177776) + 1
if ( (tfc<<PCD.rc)+1+dfc ) gr #1000 then
	tfc = (tfc + #777) logand #177000

asb<<lh = tfc + dfc + 1 - cpscrt
deltacp = 0
PutSbScr(lv asb,look1std,look2std)
cpscrt = tfc + dfc + 1
resultis tfc + 1

]


and Init1Rls() be
[

//fcfirst is a variable necessary for EnRun and FInitCom1
vrlfsys = RlCreate(krlf,fnrlf,maxri,false)
rgrl = array(nrlmax)
rgrl ! 0 = vrlfsys

createdoce(docsys,10);
vrldsys = RlCreate(krld,0,30,true)
for i = 0 to 29 do
	AppendRegion(vrldsys,0,0)
vrlwsys = RlCreate(krlw,fnscr,6,false)
// to chuckinit
// vrgridpast2 = array(cridMax)
// vcridpast2 = 0

]

and Init2Rls() be
[
//vrlwsys is region for putting up variable strings
// there are 6 regions reserved, each of length 40
// last 2 are reserved at all times !!
for i = 0 to 7 do
	AppendRegion(vrlwsys,FcGetRegionW(40),0,40)
rgrl ! 1 = vrlwsys

]

and InitRb(krl,rb,riFirst,bi,driMax) be
[ rb>>RX.rifirst = riFirst
rb>>RX.dri = 0
rb>>RX.bi = bi
rb>>RX.brgdfcfirst = lrx
rb>>RX.brgdfc = lrx + driMax
if krl eq krlw then
	rb>>RX.brgdfcmax = lrx + 2*driMax
]