//
// Make Junta easier to use
// last edited August 10, 1978  1:01 PM
//

	get "altofilesys.d"
	get "sysdefs.d"
	get "disks.d"

external	// defined here
[	EasyJunta	// (lev, proc(savedData)[, data, lData, lSysZone])
]


external	// OS stuff
[	Junta
	ActOnDiskPages
	InitializeZone
	MoveBlock; SetBlock
	DefaultArgs
	MyFrame
		// statics
	sysDisk
	fpSysFont
	sysZone; lvSysZone
	sysFont
]


manifest
[	lvCodeTop = #335
	maxp = 100b	// max pages for sysfont
	BFSstack = 1200b	// stack needed by ActOnDiskPages
	lArgs = 4	// number of args to EasyJunta to save
	frameGuard = 1	// # of words below caller's frame smashed by getframe
]


static
[	savedData
	lSavedData
]


let EasyJunta(lev, proc, data, lData, lSysZone; numargs na) be
[	DefaultArgs(lv na, -2, 0, 0, 0)
	savedData = @lvCodeTop
	MoveBlock(savedData, lv proc, lArgs)
	MoveBlock(savedData+lArgs, data, lData)
	lSavedData = lArgs+lData
	@lvCodeTop = savedData+lSavedData
	Junta(lev, junta1)
]

and junta1() be
[	let lfont = readfile(fpSysFont)
	let frame = MyFrame()
	let font = frame-lfont-frameGuard
	let data = font-lSavedData
	for i = (lSavedData+lfont)-1 by -1 to 0 do
	   data!i = savedData!i	// transfer saved data and font
	sysFont = font+2
	@lvCodeTop = savedData
	let lzone = savedData!3
	let zone = data-lzone
	junta2!2 = junta2!2 + (frame-zone)	// increase junta2's frame size
	junta2(zone, lzone, data)
]

and junta2(zone, lzone, data) be
[	if lzone ne 0 then
	 [ InitializeZone(zone, lzone)
	   sysZone = zone
	   @lvSysZone = zone
	 ]
	(data!0)(data+lArgs)
	// Shouldn't return, but if it does ...
	finish
]

and readfile(fp) = valof
// Returns length in words
[	let CAs, DAs = vec maxp, vec maxp+1
	for i = 0 to maxp do
	   CAs!i = @lvCodeTop + (i-1) lshift 8
	CAs!0 = @lvCodeTop	// write page 1 over leader
	SetBlock(DAs+1, fillInDA, maxp)
	DAs!0 = fp>>FP.leaderVirtualDa
	let np = (lv fp - @lvCodeTop - BFSstack) rshift 8
	let numChars = nil
	let lastp = ActOnDiskPages(sysDisk, CAs, DAs, fp, 0, np, DCreadD, lv numChars)
	resultis (lastp-1) lshift 8 + (numChars+1) rshift 1
]