// CopyDiskInit.bcpl
// Copyright Xerox Corporation 1979, 1981
// Last modified November 4, 1981  12:03 AM by Boggs

get "CopyDisk.decl"
get "Streams.d"
get "AltoDefs.d"
get "SysDefs.d"

external
[
// outgoing procedures
BeforeJuntaInit; AfterJuntaInit

// incoming procedures
Allocate; Free; InitializeZone; AddToZone; Idle
MyFrame; SysErr; MoveBlock; Zero; Block
CreateDisplayStream; ShowDisplayStream; SetLmarg
CreateKeyboardStream; InvertLine; GetLinePos
UBlockEq; BlockEq; UPupChecksum; PupChecksum; GetFrame
InitCopyDiskNet; InitCopyDiskBfs; InitCopyDiskTfs
InitCopyDiskUtil; InitCopyDiskCmd; BootRunInit
LoadRam; InitBcplRuntime

// outgoing statics
model44Flag; bootFlag; debugFlag; checkFlag
ramFlag; writeProtectFlag; compressFlag
titleDsp; dsp; show; noShow
ctxQ; sysZone; controlLock

// incoming statics
sysFont; CopyDiskRamImage
]

static
[
model44Flag = false; bootFlag = false; checkFlag = true
debugFlag = false; allocatorDebug = false; ramFlag = true
writeProtectFlag = true; compressFlag = true
titleDsp; dsp; show; noShow
ctxQ; sysZone; controlLock

lastShownStream
]

manifest
[
stackLim = 335B

//display backgrounds
black = true
white = false
]

//----------------------------------------------------------------------------
let BeforeJuntaInit(blv, upe, cfa) be
//----------------------------------------------------------------------------
[
MoveBlock(cursorBitMap, table [ 167352B; 125252B; 105344B; 125204B; 167204B;
 0; 0; 0; 147352B; 122214B; 122114B; 122052B; 147352B; 0; 0; 0 ], 16)

// read the global switches
while upe!0 ne 0 do
   [
   let length = upe>>UPE.length
   if upe>>UPE.type eq globalSwitches then
      [
      let notFlag = false
      for i = 1 to length-1 do
         [
         switchon upe!i into
            [
            case $-:
               [ notFlag = not notFlag; loop ]
            case $A: case $a:
               [ allocatorDebug = not notFlag; endcase ]
            case $D: case $d:
               [ debugFlag = not notFlag; endcase ]
            case $4:
               [ model44Flag = not notFlag; endcase ]
            case $C: case $c:
               [ checkFlag = not notFlag; endcase ]
            case $R: case $r:
               [ ramFlag = not notFlag; endcase ]
            case $W: case $w:
               [ writeProtectFlag = not notFlag; endcase ]
            ]
         notFlag = false
         ]
      ]
   upe = upe + length
   ]

if ramFlag then
   [
   ramFlag = LoadRam(CopyDiskRamImage, true) eq 0
   if ramFlag then
      [
      // The next boot will really boot the machine.
      // The silent boot stuff to disable the trident is
      //  in CopyDiskFinish.  This way, if someone boots out
      //  of CopyDisk, the disk will boot on the first try
      //  rather than having to boot twice.
      // When it is time to disable the microcode, we set the
      //  boot locus vector to 177776b, and silent boot.
      BlockEq = UBlockEq
      PupChecksum = UPupChecksum
      InitBcplRuntime()
      ]
   ]

// set up a free storage zone
let freeBegin = @stackLim
let freeEnd = MyFrame() -1500
if freeEnd - freeBegin ls 0 then freeEnd = freeBegin + 77777b
@stackLim = freeEnd
sysZone = InitializeZone(freeBegin, freeEnd-freeBegin, SysErr,
 (allocatorDebug? SysErr, 0))

// initialization specific to boot or run file versions of CopyDisk
BootRunInit()
]

//----------------------------------------------------------------------------
and AfterJuntaInit() be
//----------------------------------------------------------------------------
[
CreateKeyboardStream()
let freeBegin = @stackLim
let freeEnd = MyFrame() -350
if freeEnd-freeBegin gr 20 then
   [
   @stackLim = freeEnd
   AddToZone(sysZone, freeBegin, freeEnd-freeBegin)
   ]
ctxQ = Allocate(sysZone, 2); ctxQ!0 = 0
Idle = Block

// set up the display
lastShownStream = MakeBar(white, 24)
ShowDisplayStream(lastShownStream, DSalone)
ShowDisplayStream(MakeBar(white, 1), DSbelow, lastShownStream)
let fontHeight = ((sysFont!-2)+1)&-2
let lw = lDCB + 28*fontHeight +1
show = CreateDisplayStream(1, Allocate(sysZone, lw), lw, 0, 28)
SetLmarg(show, 160)
noShow = CreateDisplayStream(1, Allocate(sysZone, lw), lw, 0, 28)
SetLmarg(noShow, 160)
CDShowDisplayStream(show)
CDShowDisplayStream(MakeBar(white, 8))
CDShowDisplayStream(MakeBar(black, 2))
lw = lw + 8*fontHeight
titleDsp = CreateDisplayStream(1, Allocate(sysZone, lw), lw, 0, 36)
SetLmarg(titleDsp, 48)
InvertLine(titleDsp, GetLinePos(titleDsp))
CDShowDisplayStream(titleDsp)
CDShowDisplayStream(MakeBar(black, 2))
dsp = CreateDisplayStream(15, Allocate(sysZone, 4000), 4000, 0, 34)
SetLmarg(dsp, 48)
CDShowDisplayStream(dsp)
CDShowDisplayStream(MakeBar(black, 1))

InitCopyDiskUtil()
InitCopyDiskNet()
InitCopyDiskCmd()
InitCopyDiskBfs()
InitCopyDiskTfs()
]

//----------------------------------------------------------------------------
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
]

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