//   AIInit.bcpl
//   Alto - 1822 Interface Test Program
//   L. Stewart  last modified:  January 5, 1981  11:40 AM

//   This module contains initialization procedures

get "AIMenuDefs.d"
get "AITest.d"
get "AIMenuNames.d"

external                  //Incoming Procedures from packages
   [
   InitializeContext      //Context package
   Enqueue                //AltoQueue package
   InitializeInterrupt    //Interrupt package
   FindInterruptMask
   LoadRam                //LoadRam package
   PutTemplate            //Template package
   ]

external                  //Incoming OS procedures
   [
   GetFixed
   InitializeZone
   Allocate
   Zero
   ShowDisplayStream
   CreateDisplayStream
   Wss
   FixedLeft
   ]

external                  //Incoming procedures
   [
   PutBoxTemplate         //from AIWorkb
   Type
   BlinkBox
   Contents
   MenuProc               //from AITest
   AutoUpdate
   BlastProc
   CProc
   OProc
   IProc
   HostRelay              //from AIWork
   TestMode
   Receiver               //from AIComm
   MakeIMPNop
   MakePRUNop
   MakeIMPEcho
   MakePRUEcho
   MenuInitHelp           //from AIMenuTables
   EditMenuInit           //from AIEditMenuTables
   ]

external                  //Data files
   [
   RamImage               //default Packed Ram Image
   ]

external                  //Incoming OS Statics
   [
   sysFont
   dsp
   ]

external                  //Incoming Statics
   [
   SBootVector            //from AIWorkb
   updateInterval
   uCodeFname
   AutoUpdateSwitch
   PacketLoopCount
   PacketLength
   IMPCommMode            //from AIComm
   IMPImpNumber
   IMPHostNumber
   PRUHostNumber
   eMenuData              //from AIWorkb
   lastCSFctn             //from AIWork
   cb
   iBuf
   oBuf
   BufSize
   oiHostRelay
   oiTestMode
   oiDiscard
   CtxZn                  //from AITest
   Q
   Menu
   MenuData
   MPSpace
   MPContext
   AUSpace
   AUContext
   RxSpace
   RxContext
   BlastSpace
   BlastContext
   menuS
   menuSMap
   menuStream
   menuMap
   workS
   workSMap
   IStack
   OStack
   CStack
   CMask
   OMask
   IMask
   StopXo
   StopYo
   StopXc
   StopYc
   blue
   ]

external                  //Outgoing Procedures
   [
   SetupParamDisplay
   InitWorld
   InitBlastProc
   ]

let InitWorld() be
   [
   let switchVec = vec 20
   let fixed=nil
   CtxZn = GetFixed(ZoneSize)
   Q!0=0
   InitializeZone(CtxZn,ZoneSize)   //Build Processes
   MPSpace=Allocate(CtxZn,CtxSize)
   MPContext=InitializeContext(MPSpace,CtxSize, MenuProc)
   Enqueue(Q,MPContext)
   AUSpace=Allocate(CtxZn,CtxSize)
   AUContext=InitializeContext(AUSpace,CtxSize, AutoUpdate)
   Enqueue(Q,AUContext)
   RxSpace=Allocate(CtxZn,CtxSize)
   RxContext=InitializeContext(RxSpace,CtxSize, Receiver)
   Enqueue(Q,RxContext)
   BlastSpace=Allocate(CtxZn,200)
   BlastContext=InitializeContext(BlastSpace,200, BlastProc)
   Enqueue(Q,BlastContext)

   IStack = GetFixed(200)
   OStack = GetFixed(200)
   CStack = GetFixed(200)
   CMask = FindInterruptMask(1)
   InitializeInterrupt(CStack, 200, CMask, CProc)
   OMask = FindInterruptMask(CMask)
   InitializeInterrupt(OStack, 200, OMask, OProc)
   IMask = FindInterruptMask(OMask)
   InitializeInterrupt(IStack, 200, IMask, IProc)

   //initialize statics
   cb = GetFixed(20)
   Zero(cb,20)
   cb = (cb + 1) & #177776
   iBuf = GetFixed(BufSize) ; Zero(iBuf,BufSize)
   oBuf = GetFixed(BufSize) ; Zero(oBuf,BufSize)
   MenuData = MenuInitHelp()
   eMenuData = EditMenuInit()

   let length = MenuSize(MenuData)
   menuSMap = GetFixed(length)
   if menuSMap eq 0 then abort
   CreateMenuDisplayStream(MenuData,menuSMap,length,menuS)
   ShowDisplayStream(menuS,DSbelow,dsp)
   Menu = MenuData!0
   length = MenuSize(eMenuData)
   menuMap = GetFixed(length)
   CreateMenuDisplayStream(eMenuData,menuMap,length,menuStream)

   workSMap = GetFixed(MapSize)
   if workSMap eq 0 then abort
   workS = CreateDisplayStream(NLines,workSMap,MapSize,sysFont,38,DSnone)
   ShowDisplayStream(workS,DSbelow,menuS)

   SetupParamDisplay()

   InitBlastProc()

   BlinkBox(Menu!bLoaduCode,50)
   if LoadRam(RamImage,true) ne 0 do
      [
      Wss(dsp,"LoadRam Failed!*n")
      finish
      ]
   BlinkBox(Menu!bSilentBoot,50)
   AutoUpdateSwitch = true
   TestMode(oiTestMode)
   HostRelay(oiHostRelay)
   MakeIMPNop()
   MakeIMPEcho()
   MakePRUNop()
   MakePRUEcho()
   fixed = FixedLeft()
   // PutTemplate(workS,"FixedLeft $UD (10)*n",fixed)
   PutTemplate(workS,"Alto-1822 Test Program of January 4, 1981*n")
   PutTemplate(workS,"Maximum packet length #$6UF0O*n",BufSize)
   ]

and SetupParamDisplay() be
   [
   let oblue = blue
   PutBoxTemplate(Menu!BootVectorv,"#$6UF0O",SBootVector)
   PutBoxTemplate(Menu!Updatev,"#$6UF0O",updateInterval)
   PutBoxTemplate(Menu!buCodeFname,"  $S",uCodeFname)
   PutBoxTemplate(Menu!Commandv,"#$6UF0O",lastCSFctn)
   PutBoxTemplate(Menu!Interruptsv,"Off")
   PutBoxTemplate(Menu!bNetModev, "  $S", IMPCommMode ? "IMP", "PRU")
   PutBoxTemplate(Menu!bHostNumberv,"#$6UF0O",IMPCommMode ? IMPHostNumber, PRUHostNumber)
   PutBoxTemplate(Menu!bImpNumberv,"#$6UF0O",IMPImpNumber)
   blue = true
   Type()
   PutBoxTemplate(Menu!LoopCountv,"#$6UF0O", PacketLoopCount)
   PutBoxTemplate(Menu!PacketLengthv,"#$6UF0O",PacketLength)
   Contents()
   blue = oblue
   ]

and InitBlastProc() be
   [
	// define stop box coords
   let box=Menu!bStop
	let Xo=box>>BOX.xorigin
	let Xc=box>>BOX.xcorner
	let Yo=box>>BOX.yorigin
	let Yc=box>>BOX.ycorner

	// if dcb was specified, find absolute coords from dcb chain
	let top=FindDCB(box)		// returns # lines to the dcb
	let left=top ? 16*((box>>BOX.dcb)>>DCB.indentation),0
	StopXo=Xo+left
	StopYo=Yo+top
	StopXc=Xc+left
	StopYc=Yc+top
   ]