// BFSTestInit.bcpl
// Copyright Xerox Corporation 1979, 1982
// Last modified March 28, 1982  1:24 AM by Boggs

get "AltoDefs.d"
get "Streams.d"
get "SysDefs.d"

external
[
// outgoing procedures
BeforeJuntaInit; AfterJuntaInit

// incoming procedures
AfterJunta; Junta
BlockCheck; uBlockCheck; UpdateTitle

CreateKeyboardStream; CreateDisplayStream; ShowDisplayStream
InvertLine; GetLinePos; CharWidth; SetLmarg
InitializeZone; Allocate; MoveBlock; Zero; MyFrame
LoadRam; InitBcplRuntime

// outgoing statics
sysZone; dsp; label; data; eng; title

// incoming statics
sysFont; BFSTestMc
]

static
[
sysZone; dsp; label; data; eng; title
lastShownStream
]

manifest
[
stkLim = 335b
black = true
white = false
dspLines = 20
]

structure String [ length byte; char↑1,1 byte ]

//----------------------------------------------------------------------------
let BeforeJuntaInit() be
//----------------------------------------------------------------------------
[
let ramFlag = LoadRam(BFSTestMc, false) eq 0
if ramFlag then
   [
   InitBcplRuntime()
   BlockCheck = uBlockCheck
   ]
Junta((ramFlag? levBuffer, levBcpl), AfterJunta)
]

//----------------------------------------------------------------------------
and AfterJuntaInit() be
//----------------------------------------------------------------------------
[
// get the keyboard going again quickly
CreateKeyboardStream()

MoveBlock(cursorBitMap, table [ 0; 0; 0; 0; 161706b; 111011b; 111010b;
 161606b; 111001b; 111011b; 161006b; 0; 0; 0; 0; 0 ], 16)
eng = (table [ 61014b; 1401b ])()<<VERS.eng

// free storage
let freeBegin, freeEnd = @stkLim, MyFrame()-2000
@stkLim = freeEnd
if freeEnd-freeBegin ls 0 then freeBegin = freeEnd-77777b
sysZone = InitializeZone(freeBegin, freeEnd-freeBegin)

// display
lastShownStream = MakeBar(white, 24)
ShowDisplayStream(lastShownStream, DSalone)
ShowDisplayStream(MakeBar(white, 1), DSbelow, lastShownStream)
let lineWords = lDCB + 38*((sysFont!-2 +3) & -2)
title = CreateDisplayStream(1, Allocate(sysZone, lineWords), lineWords)
SetLmarg(title, 32)
UpdateTitle()
MyShowDisplayStream(MakeBar(black, 2))
MyShowDisplayStream(title)
MyShowDisplayStream(MakeBar(black, 1))
let dspSize = dspLines * lineWords
dsp = CreateDisplayStream(dspLines, Allocate(sysZone, dspSize), dspSize)
SetLmarg(dsp, 32)
MyShowDisplayStream(dsp)
MyShowDisplayStream(MakeBar(black, 1))

// public disk buffers used by everyone
label = Allocate(sysZone, 8)
data = Allocate(sysZone, 256)
]

//----------------------------------------------------------------------------
and MyShowDisplayStream(stream) be
//----------------------------------------------------------------------------
[
ShowDisplayStream(stream, DSbelow, lastShownStream)
lastShownStream = stream
]

//----------------------------------------------------------------------------
and MakeBar(background, nLines) = valof
//----------------------------------------------------------------------------
[
structure Bar: [ fdcb word; ldcb word; @DCB ]
manifest lenBar = size Bar/16
let bar = Allocate(sysZone, lenBar, false, true)
Zero(bar, lenBar)
bar>>Bar.fdcb = lv bar>>Bar.next
bar>>Bar.ldcb = lv bar>>Bar.next
bar>>Bar.background = background
bar>>Bar.width = 0
bar>>Bar.height = nLines
resultis bar
]