//moverlay.bcpl

external [
// OS
	Allocate; Free; CallSwat

// Overlay package
	OverlayNpages; OverlayFirstPn; ReadOverlay; ReleaseOverlay
	GeneratePresentOverlays; OverlayCoreAddr; LockPendingCode

// MDISP
	MakeDispZoneAvail; ReUseDispSpace; DisplayOff

// Defined here
	UserReadOverlay; SwappedOut; KillOverlays; OverlayZone
	OverlayFlushed
]

manifest [ PageSize = 256 ]

static [
	OverlayZone = 0; OverlayFlushed = true
]

//"OverlayFlushed" is used to avoid multiple display rebuilding.
//When the display is off, display update is delayed so that
//only a single painting of each line will be required.

let  UserReadOverlay(od) = valof
[	let Np = OverlayNpages(od)
	let Pn = OverlayFirstPn(od)
	unless OverlayZone ne 0 do OverlayZone = MakeDispZoneAvail()
	let Ca = Allocate(OverlayZone, Np*PageSize)
	ReadOverlay(Pn, Ca, Np); OverlayFlushed = false
	resultis Ca
]


and KillOverlays() be
[	unless OverlayZone ne 0 do return
	LockPendingCode()
	GeneratePresentOverlays(KillOverlays1)
	if DisplayOff then [ OverlayFlushed = true; return ]
	ReUseDispSpace(); OverlayZone = 0
]


and KillOverlays1(od) be
[	let Ca = OverlayCoreAddr(od)
	unless ReleaseOverlay(od, true) do CallSwat()
	Free(OverlayZone, Ca)
]


and SwappedOut() be CallSwat()