//routeinit.bcpl

// Initialization and driver module

// last modified by Taft, September 22, 1979 7:11 PM

get "sysdefs.d"
get "streams.d"
get "altofilesys.d"
get "route.defs"

external
[
fpComCm
fpSysDir
sysDisk
dsp

OverlayScan
GenerateOverlays
OverlayNpages

LookUpEntries

LoadRam

InitBcplRuntime
RamImage

initCodeEnd

PrintTWPins
PrintHoles
MakeBuzzLists

overlayCore
]

static
[
Correcting = false
noroute = false
whereTW = false
buzzLists = false

nlFileName = empty

boardLocation = empty
boardCodeFileName = empty

wlNewFileName = empty
wlOldFileName = empty

ImplicitFile
ADFile
ErFile
CommentsFile
OldWlFile
OutFile

charTab

dontCareNet

StandardMetric

SilZone = 0
initCodeEnd

kf = empty

epvec
epsize

overlayCore
npBiggestOverlay
]


let OnceOnlyInitCode(blv,params,cfa) be
[ // sets up, also handles board type checking.
initCodeEnd = @#335

AddSpace(3000)
// leave for stack

@#420 = 0 //turn off display for speed

let e = vec 100
epvec = e
epsize = 0
AddDynamicEntry(lv AddEclTerminators) // in RouteTerm
AddDynamicEntry(lv LocateAvailTerms)
AddDynamicEntry(lv FreeTermStorage)

AddDynamicEntry(lv AssignTWNet)
// RouteTWNetAssign

AddDynamicEntry(lv ManhattanDistFn) // in Routec
AddDynamicEntry(lv EuclideanDistFn)

AddDynamicEntry(lv CheckUnusedPins) // in Routea

AddDynamicEntry(lv NLCompFn) // in Routeb
AddDynamicEntry(lv AssignNetNumber)

AddDynamicEntry(lv ReportRepairedPins) // in RouteCorrect
AddDynamicEntry(lv ReportNewCutPins)
AddDynamicEntry(lv PrintNewNet)

AddDynamicEntry(lv MultiWirePanic) // in RouteBackpanel

AddDynamicEntry(lv RouteEarlyNets) // in RouteNet
AddDynamicEntry(lv RouteNetIfExternal)
AddDynamicEntry(lv RouteNet)
AddDynamicEntry(lv ComputeLengths)

OverlayScan(lv (cfa>>CFA.fp), Allocate(SilZone, 200), 200, lv (cfa>>CFA.fa),
0, 0, 0, 0, sysDisk, epvec, epsize)
npBiggestOverlay = 0
GenerateOverlays(HowBig)
overlayCore = Allocate(SilZone, 256*npBiggestOverlay)

charTab = Allocate(SilZone, 256)
Zero(charTab, 256)
AddAttribute(upperCaseMask%letterMask, $A, $Z)
AddAttribute(lowerCaseMask%letterMask, $a, $z)
AddAttribute(digitMask, $0, $9)
AddAttribute(colonMask, ":")
AddAttribute(breakMask, " :,;*t*n")
AddAttribute(legalSpecialCharMask, "$#()’[]↑←?/\|+-=&**~")

let comcm = OpenFile("COM.CM",ksTypeReadOnly,charItem,0,fpComCm,0,SilZone)
if comcm eq 0 then CallSwat("Can’t open COM.CM")
let fn = vec 128
ReadNext(comcm,fn) //throw away name
let fnl = fn>>str.length
let i=1
while i le fnl do test fn>>str.char↑i eq $/
ifso [ i=i+1; break ]
ifnot i=i+1

while i le fnl do
[
switchon fn>>str.char↑i into
[
case $C:
case $c:
Correcting = true
endcase

case $M:
case $m:
doMultiWire = true
endcase

case $T:
case $t:
whereTW = true
endcase

case $B:
case $b:
buzzLists = true
endcase

case $R:
case $r:
doResistances = true
endcase

default:
]
i=i+1
]

StandardMetric = ManhattanDistFn

DefaultString(lv boardLocation, "c1")
DefaultString(lv boardCodeFileName, "routemlb.br")

let nlFileCount = 0
until Endofs(comcm) do
[
let filestring = vec 20
unless ReadNext(comcm,filestring) then break
unless IsFunnyParameter(filestring) do
[
NeedFile(filestring, 0, true) // input file
if nlFileCount eq 0 & nlFileName eq empty then
[
DefaultString(lv nlFileName, filestring)
let lastGoodChar = nlFileName>>str.length
for i=1 to nlFileName>>str.length do
[
if nlFileName>>str.char↑i eq $. then
lastGoodChar = i-1
]
nlFileName>>str.length = lastGoodChar
]
nlFileCount = nlFileCount+1
]
]
Closes(comcm)

if LoadRam(RamImage) eq 0 then InitBcplRuntime() // fire up microcode

SetupSpace()

dontCareNet = DefineNamee("Whatever", typeNet, NewNet)
dontCareNet>>net.hasBeenRouted = true
dontCareNet>>net.dontTerminate = true

SetupICclasses() // build icclass objects for all known IC classes

SetupBoard(boardCodeFileName)
// read in board code and plug into statics, then
//plug edge connectors and trace-wired nets into data structures

if whereTW then PrintTWPins()
if buzzLists then MakeBuzzLists()

if nlFileCount le 0 then finish

let ts = vec 30
test Correcting & (wlOldFileName eq empty)
ifso
[
ExpandTemplate(ts, "$S.wl", nlFileName)
DefaultString(lv wlOldFileName, ts)
ExpandTemplate(ts, "$S.wlNew", nlFileName)
DefaultString(lv wlNewFileName, ts)
]
ifnot
[
ExpandTemplate(ts, "$S.wl", nlFileName)
DefaultString(lv wlNewFileName, ts)
]

NeedFile(wlNewFileName)
NeedFile(nlFileName, ".inl")
NeedFile(nlFileName, ".re")
NeedFile(nlFileName, ".bp")
NeedFile(nlFileName, ".lc")
NeedFile(nlFileName, ".comments")

if Correcting then
[
NeedFile(wlOldFileName, "", false, false)
NeedFile(nlFileName, ".ad")
NeedFile("route.dpf")
]

if doMultiWire then
[
NeedFile(nlFileName, ".mw")
NeedFile(nlFileName, ".mh")
NeedFile(nlFileName, ".mhi")
]

if doResistances then
[
NeedFile(nlFileName, ".resist")
]

MapNamees(typeIcinst, NeedConnInstFiles)
FindOrMakeNeededFiles()

ErFile = GetFile(nlFileName,".re",charItem) //routing errors
PutTemplate(ErFile, "[0] = worst error severity*n")

ImplicitFile = GetFile(nlFileName,".inl", charItem) // implicit net list
PutTemplate(ImplicitFile, "; Implicitly generated wiring ...*n@")

CommentsFile = GetFile(nlFileName,".comments",charItem)
TruncateDiskStream(CommentsFile)
]

and DefaultString(sp, s) be
[
if @sp ne empty then Free(SilZone, @sp)
@sp = Allocate(SilZone, (s>>str.length rshift 1)+1)
MoveBlock(@sp, s, (s>>str.length rshift 1)+1)
]

and AddDynamicEntry(p) be
[
epvec!epsize = p
epsize = epsize+1
]

and HowBig(od) be
[
let npThisOverlay = OverlayNpages(od)
if npThisOverlay gr npBiggestOverlay then
npBiggestOverlay = npThisOverlay
]

and NeedConnInstFiles(icinst) be
[
unless Icclass(icinst)>>icclass.isConnector do return
let filename = vec 20
ExpandTemplate(filename, "$S-$S.nl", nlFileName, FindNameesString(icinst))
NeedFile(filename)
]

and AddAttribute(attributeMask, first, last) be
[
test first le 255
ifso for char=first to last do charTab!char = charTab!char%attributeMask
ifnot for i=1 to first>>str.length do AddAttribute(attributeMask, first>>str.char↑i,
first>>str.char↑i)
]


and ReadNext(stream,string) = valof
[
let ch = $*s
if Endofs(stream) then resultis false
until (Endofs(stream)%(ch gr $*s))do ch = Gets(stream)
if ch le $*s then resultis false

string>>str.length = 1
string>>str.char↑1 = ch

[
if Endofs(stream) then break
ch = Gets(stream)
if ch le $*s then break
let sl = string>>str.length+1
string>>str.char↑sl = ch
string>>str.length = sl
] repeat
resultis true
]



and IsFunnyParameter(string) = valof
[
let lastSlashPos = 0
for i=1 to string>>str.length do
if string>>str.char↑i eq $/ then lastSlashPos = i

if lastSlashPos eq 0 then resultis false

string>>str.length = lastSlashPos-1
switchon string>>str.char↑(lastSlashPos+1) into
[
case $h:
case $H:
heuristicWork = CSN(string)
endcase

case $m:
case $M:
StandardMetric = selecton string>>str.char↑1 into
[
case $M:
case $m:ManhattanDistFn

case $E:
case $e:EuclideanDistFn

default:ManhattanDistFn
]
endcase

case $E:
case $e:
exhaustThresh = CSN(string)
endcase

case $L:
case $l:
DefaultString(lv boardLocation, string)
endcase

case $B:
case $b:
DefaultString(lv boardCodeFileName, string)
endcase

case $C:
case $c:
Correcting = true
DefaultString(lv wlOldFileName, string)
endcase

case $F:
case $f:
DefaultString(lv nlFileName, string)
endcase

default:
]

resultis true
]

and CSN(string) = valof
[
let value = 0
for i=1 to string>>str.length do
[
let c = string>>str.char↑i
unless (charTab!c&digitMask)ne 0 do
[
CallSwat("*nIllegal number in command file.",
string)
finish
]
value = value*10+c-$0
]
resultis value
]

and NeedFile(fn, suffix, isInput, okToCreate; numargs na) be
[
DefaultArgs(lv na, -1, "", false, false)
let filename = vec 20
ExpandTemplate(filename, "$S$S", fn, suffix)
let fnwords = (filename>>str.length rshift 1)+1
let newkf = Allocate(SilZone, offset KF.string/16+fnwords)
MoveBlock(lv newkf>>KF.string, filename, fnwords)
newkf>>KF.input = isInput
newkf>>KF.oktocreate = na gr 3? okToCreate, not isInput
newkf>>KF.next = kf
kf = newkf
]

and FindOrMakeNeededFiles() be
[
let kfCount = 0
let kfIndex = kf
let kfRev = empty
// reversed list
until kfIndex eq empty do
[
kfCount = kfCount+1
let kfTemp = kfIndex>>KF.next
kfIndex>>KF.next = kfRev
kfRev = kfIndex
kfIndex = kfTemp
]
kf = kfRev

let names = Allocate(SilZone, kfCount)
let kfCount = 0
let kfIndex = kf
until kfIndex eq empty do
[
names!kfCount = lv kfIndex>>KF.string
kfCount = kfCount+1
kfIndex = kfIndex>>KF.next
]

let dvs = Allocate(SilZone, kfCount*lDV)
Zero(dvs, kfCount*lDV)

let q = OpenFile(0,0,0,0,fpSysDir,0,SilZone)
if q eq 0 then CallSwat("Can’t open SysDir")
LookUpEntries(q, names, dvs, kfCount, true)
Closes(q)

kfIndex = kf
kfCount = 0
until kfIndex eq empty do
[
test dvs!(kfCount*lDV) eq 0
ifnot
[
MoveBlock(lv kfIndex>>KF.fp,
lv dvs!(kfCount*lDV+offset DV.fp/16), lFP)
]
ifso
[
if not kfIndex>>KF.oktocreate then CallSwat("Can’t find input file ",
lv kfIndex>>KF.string)
let file = OpenFile(lv kfIndex>>KF.string,ksTypeReadWrite,
0,0,0,0,SilZone)
let cfa = vec lCFA
GetCompleteFa(file, cfa)
MoveBlock(lv kfIndex>>KF.fp, lv cfa>>CFA.fp, lFP)
Closes(file)
]
kfCount = kfCount+1
kfIndex = kfIndex>>KF.next
]
]

and AddSpace(stackNeeds) be
[
static [ malFormed = 0 ]
let Space = FixedLeft()
while Usc(Space, stackNeeds+20) gr 0 do
[
let grab = Space-stackNeeds
if Usc(grab, #77777) gr 0 then grab = #77777 //+ infinity
test SilZone eq 0
ifso SilZone = InitializeZone(GetFixed(grab), grab, SysErr, malFormed)
ifnot AddToZone(SilZone, GetFixed(grab), grab)

Space = FixedLeft()
]
]