// AItest.bcpl
// Alto - 1822 Interface Test Program
// L. Stewart last modified: January 4, 1981 5:00 PM

// This module contains the main loops

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

external //Incoming Procedures
[
InitWorld //from AIInit
Cleanup //from AIWorkb
SilentBoot
LoaduCode
PrintBuffers
Type
Update
Contents
SetInterrupts
CheckBuffers
GetBoxString
GetBoxNumber
PutBoxTemplate
WriteCStat
SetResults
MasterClear //from AIWork
ClearIWD
SetCSFctn
TestAll
TestBLZ
TestGather
TestInt
LoopEcho
Listen
TestScatter
HostRelay
TestMode
SetControl
Communicate //from AIComm
CAPEcho
]

external //Incoming Procedures from Packages
[
CallContextList //Context package
Block
Dismiss //Altotimer package
DisableInterrupts //Altotimer package
EnableInterrupts
]

external //OS procedures
[
Wss; Endofs; Gets
]
external //Outgoing Procedures
[
MenuProc
AutoUpdate
BlastProc
CProc
IProc
OProc
]

external //Incoming OS Statics
[
keys
]

external //Incoming Statics
[
iBuf //from AIWork
oBuf
BufSize
cb
oiHostRelay
oiTestMode
oiDiscard
cInterrupt
iInterrupt
oInterrupt
lastCSFctn
uCodeFname //from AIWorkb
PacketLength
PacketLoopCount
SBootVector
AutoUpdateSwitch
updateInterval
EditPacket
IMPCommMode //from AIComm
IMPImpNumber
IMPHostNumber
PRUHostNumber
]

external //Outgoing Statics (in AIWorkb.bcpl)
[
Q //Context Stuff
CtxZn
MPSpace
MPContext
AUSpace
AUContext
RxSpace
RxContext
BlastSpace
BlastContext
menuS //Display streams
menuStream
workS
menuSMap //Display bit maps
menuMap
workSMap
MenuData //tables for main menu
red //mouse buttons
yellow
blue
Blast //to unstick things
StopXo //Coordinates of the ’stop’ button
StopXc
StopYo
StopYc
Menu //Menu chain
Sel //index into menu chain
IMask //Interrupt stuff
OMask
CMask
IStack
OStack
CStack
]

static
[
Q
CtxZn
MPSpace
MPContext
AUSpace
AUContext
RxSpace
RxContext
BlastSpace
BlastContext
menuS
workS
menuStream
menuMap
menuSMap
workSMap
MenuData
red = false
yellow = false
blue = false
Blast = false
StopXo
StopXc
StopYo
StopYc
Menu
Sel
IMask
OMask
CMask
IStack
OStack
CStack
odcb=0
]


let Main() be
[
let v = vec 2 //Context Switching
Q = v
let v = vec 2 //main menu
menuS = v
let v = vec 2 //edit menu
menuStream = v

uCodeFname = "AIuCode.br................."
uCodeFname>>STRING.length = 10

InitWorld()

CallContextList(@Q) repeat
]

and MenuProc() be
[
let temp,char = nil,nil
Block()
unless Endofs(keys) do
[
char = Gets(keys)
switchon (char) into
[
case $D: case $d:
temp = @display
@display = odcb
odcb = temp
endcase
default:
]
]
Blast = false
if (Sel eq 0) do
[
Sel=ScanMenu(Menu,0,true)
if Sel do
[
cb>>CB.InputPost = 0
cb>>CB.OutputPost = 0
cb>>CB.ControlPost = 0
SetResults(false,false)
]
]
red = (Sel & mred) ne 0
yellow = (Sel & myellow) ne 0
blue = (Sel & mblue) ne 0
Sel = Sel & #377
switchon Sel into
[
case bQuit: Cleanup() ; endcase
case bSilentBoot: SilentBoot() ; endcase
case bLoaduCode: LoaduCode() ; endcase
case bMasterClear: MasterClear() ; endcase
case bClearIWD: ClearIWD() ; endcase
case bSetCSFctn: SetCSFctn() ; endcase
case bTestAll: TestAll() ; endcase
case bCAPEcho: CAPEcho() ; endcase
case bTestBLZ: TestBLZ() ; endcase
case bTestGather: TestGather() ; endcase
case bTestInt: TestInt() ; endcase
case bLoopEcho: LoopEcho() ; endcase
case bListen: Listen() ; endcase
case bEdit: EditPacket() ; endcase
case bTestScatter: TestScatter() ; endcase
case bType: Type() ; endcase
case bUpdate: Update() ; endcase
case bContents: Contents() ; endcase
case bCommunicate: Communicate() ; endcase
case bSetInterrupts: SetInterrupts(true) ; endcase

case bHostRelay:
[
HostRelay(not oiHostRelay)
Sel = 0
endcase
]

case bTestMode:
[
TestMode(not oiTestMode)
Sel = 0
endcase
]

case bCheckBuffers:
[
if CheckBuffers(true) then Wss(workS," Ok")
endcase
]

case buCodeFname:
[
GetBoxString(Menu!buCodeFname,uCodeFname)
Sel = 0
endcase
]

case bPacketLength:
[
GetBoxNumber(Menu!PacketLengthv,lv PacketLength)
PacketLength = (PacketLength gr BufSize) ? BufSize, PacketLength
endcase
]

case bHostNumber:
[
GetBoxNumber(Menu!bHostNumberv,IMPCommMode ? lv IMPHostNumber,lv PRUHostNumber)
endcase
]

case bImpNumber:
[
GetBoxNumber(Menu!bImpNumberv,lv IMPImpNumber)
endcase
]

case bNetMode:
[
IMPCommMode = not IMPCommMode
PutBoxTemplate(Menu!bNetModev, " $S", IMPCommMode ? "IMP", "PRU")
PutBoxTemplate(Menu!bHostNumberv, "#$6UF0O", IMPCommMode ? IMPHostNumber, PRUHostNumber)
endcase
]

case bLoopCount:
[
GetBoxNumber(Menu!LoopCountv,lv PacketLoopCount)
endcase
]

case bCommand:
[
GetBoxNumber(Menu!Commandv,lv lastCSFctn)
endcase
]

case bBootVector:
[
GetBoxNumber(Menu!BootVectorv,lv SBootVector)
endcase
]
]
if Sel do FlipBox(Menu!Sel) // unflip, really
Sel = 0
] repeat


and let AutoUpdate() be
[
until AutoUpdateSwitch do Block()
SetControl(doNothing)
WriteCStat(cb>>CB.ControlPost)
Dismiss(updateInterval*10) // tenths of seconds
] repeat

and let BlastProc() be
[
Dismiss(10)
let XCursor=@#424 //get cursor location
let YCursor=@#425
let key=(not @#177030) & #007
// compare cursor coords to absolute coords
if (key ne 0) & (XCursor ge StopXo) & (XCursor le StopXc) & (YCursor ge StopYo) & (YCursor le StopYc) do
[
FlipBox(Menu!bStop)
if select(StopXo,StopXc,StopYo,StopYc,key) then Blast = true
FlipBox(Menu!bStop)
]
] repeat

and let CProc() be
[
DisableInterrupts()
Wss(workS,"c")
cInterrupt = true
EnableInterrupts()
]

and let IProc() be
[
DisableInterrupts()
Wss(workS,"i")
iInterrupt = true
EnableInterrupts()
]

and let OProc() be
[
DisableInterrupts()
Wss(workS,"o")
oInterrupt = true
EnableInterrupts()
]