//
// Simple reader for type B overlays
// last edited August 1, 1978  12:37 PM
//

	get "streams.d"
	get "altofilesys.d"

external	// defined here
[	LoadOverlay	// (cfa, base, limit[, zone]) -> end/0, updates cfa
]

external	// O.S.
[	DefaultArgs
	CreateDiskStream
	JumpToFa
	ReadBlock
	GetCompleteFa
	Closes
	Usc
]


let LoadOverlay(cfa, base, limit, zone; numargs na) = valof
[	DefaultArgs(lv na, 1, 0)
	let st = CreateDiskStream(lv cfa>>CFA.fp, ksTypeReadOnly, wordItem, 0, 0, zone)
	JumpToFa(st, lv cfa>>CFA.fa)
	ReadBlock(st, base, 20b)
	let org = base+20b
	let count = ((base!4+377b) & -400b)-20b
	if Usc(org+count, limit) gr 0 then
	[ Closes(st)
	  resultis 0
	]
	ReadBlock(st, org, count)
	let p = org+base!1+1
	let plim = p+2*p!-1
	while p ne plim do
	[ @(p!0) = p!1+org
	  p = p+2
	]
	GetCompleteFa(st, cfa)
	Closes(st)
	resultis base+base!4
]