// EarsUtil.Sr

// Last modified October 20, 1979  3:06 PM by Taft

//contains procedures used by both EARSTRANSMIT and EARSDIR

get "altofilesys.d"

structure SB:
	[ cch	byte
	ch↑0,255	byte
	]

structure FUNFA:
	[
	fun	byte;
	fa byte;
	]

structure FDA:
	[
	track	bit	13;
	sector	bit	3;
	] 

structure OF:
	[ 
	macfp	word;
	version	word = fileid	word = dests	word;
	sn1	word;
	sn2	word;
	wmode	word;
	wf	word;
	macpos	word;
	pos	word;
	bphint	word;
	macbi 	word = macbp	word;
	formatted	word = clearance	word;
	fda	@FDA;
	rgda	word = rvrgbp	word;
	last	word;
	] 

get "q.df"
get "ears.df"
get "dir.df"
get "vm.df"
get "PupEftp.Decl"
get "rn1.df"
get "font.df"

//incoming procedures
external	[
	RealDA
	move
	movec
	]

//outgoing procedures
external	[
	lfm
	chknilfunfa
	macfe
	makecfa
	makefp
	]

//incoming statics
external	[
	mpfunfd
	mpCrockLrec2
	]

//local manifest
manifest	[
	nilfunfa= -1
	]



//-=-=-=-=-=-=-=-=-=-
let lfm(fs,fe)= valof
//-=-=-=-=-=-=-=-=-=-

[
let funfa= (lv(fs>>FS.rvmpfefunfa))!fe
let fun= funfa<<FUNFA.fun
let fa= funfa<<FUNFA.fa
test fun ls maxfun ifso
	[ let fd = mpfunfd ! fun
	let fdh = (lv (fd >> FD.fdh))
	resultis (lv (fdh >> FDH.ampFaLrec2)) ! fa
	] 
ifnot	resultis mpCrockLrec2 ! (fun-maxfun)
]

//-=-=-=-=-=-=-=-=-=-
and chknilfunfa(fs,fe) = valof
//-=-=-=-=-=-=-=-=-=-
//returns true if funfa is nil
[
let funfa= (lv(fs>>FS.rvmpfefunfa))!fe
resultis (funfa eq nilfunfa)
]

//-=-=-=-=-=-=-=-=-=-
and macfe(fs)= valof
//-=-=-=-=-=-=-=-=-=-
//returns number of last non-nil fe plus 1
//returns 0 if all nil
[
let mac=0
for ife=0 to 15 do
	unless chknilfunfa(fs,ife) do mac = ife + 1
resultis mac
]


//-=-=-=-=-==-===-=-==
and makecfa(cfa,fp,pagen,da;numargs na) be 
//-=-=-=-==-=-=-==--=-=
//makes cfa a CFA with @FP = fp, .pageNumber=pagen, .da=da
//pagen and da default to beginning of file
[
switchon na into
	[
	case 2: pagen=0
	case 3: da=RealDA(fp>>FP.leaderVirtualDa)
	]
move(fp,lv(cfa>>CFA.fp),lFP)
cfa>>CFA.fa.da = da
cfa>>CFA.fa.pageNumber = pagen
cfa>>CFA.fa.charPos = 0
]

//-=-=-=-=-=-=-=-=-=-
and makefp(tfp,fs,fe) be
//-=-=-=-=-=-=-=-=-=-=

[
movec(tfp,tfp+4,0)
tfp>>FP.serialNumber.word1= (lv(fs>>FS.rvmpfesn1))!fe
tfp>>FP.serialNumber.word2= (lv(fs>>FS.rvmpfesn2))!fe
tfp>>FP.version= 1
]