// ResistNet.bcpl -- Net and Pin structure operations

// Last modified by McCreight June 25, 1984  10:49 AM

get "Resist.decl"

external
[
// outgoing procedures
SummarizeBadNets
PinsNet; PinsNetSymbol; PinsNetString; PinsIC; CreateNet

// incoming procedures
EnumerateSymbolTable
ObjectsString; ObjectsSymbol; SymbolsObject; DefineSymbol
Wss; Puts; PutTemplate; ExpandTemplate; DisplayOhms; DisplayOhmsCode
Free; Usc

// incoming statics
sysZone; maxNPins
]


// ---------------------------------------------------------------------------
let SummarizeBadNets(outStream, symbolTable, InTolerance) be
// ---------------------------------------------------------------------------
[
let args = vec 3
args!0 = outStream; args!1 = InTolerance; args!2 = false
EnumerateSymbolTable(symbolTable, stNet, CheckForBadNet, args)
]

// ---------------------------------------------------------------------------
and CheckForBadNet(net, args) be
// ---------------------------------------------------------------------------
[
let outStream, InTolerance = args!0, args!1
let foundBad = false
let pin = net>>Net.firstPin
until pin>>Pin.nextPin eq 0 do
   [
   unless pin>>Pin.measuredOhmsCode eq unknownCode % InTolerance(pin) do
      [ foundBad = true; break ]
   pin = pin>>Pin.nextPin
   ]

if foundBad then
   [
   unless args!2 do Wss(outStream, "*n*n; Summary of suspect nets:*n")
   args!2 = true
   pin = net>>Net.firstPin
   PutTemplate(outStream, "*n; $S: expected resistance: $P, measured:",
    ObjectsString(net), DisplayOhmsCode, pin>>Pin.expectedOhmsCode)
   let nodeNum = 0
   until pin>>Pin.nextPin eq 0 do
      [
      let pinNumber = nil
      let ic = PinsIC(pin, lv pinNumber)
      if nodeNum rem 4 eq 0 then Wss(outStream, "*n;")
      let pinName = ExpandTemplate("$S.$D", ObjectsString(ic), pinNumber)
      PutTemplate(outStream, "   $S:", pinName)
      for i = pinName>>String.length to 6 do Puts(outStream, $*s)
      DisplayOhmsCode(outStream, pin>>Pin.measuredOhmsCode)
      Free(sysZone, pinName)
      pin = pin>>Pin.nextPin
      nodeNum = nodeNum+1
      ]
   Puts(outStream, $*n)
   ]
]

// ---------------------------------------------------------------------------
and CreateNet(st, string) = valof
// ---------------------------------------------------------------------------
[
let net = SymbolsObject(DefineSymbol(st, string, stNet, 0, lenNet))
net>>Net.firstPin = lv net>>Net.dummyPin
net>>Net.dummyPin.nextPin = 0
resultis net
]

// ---------------------------------------------------------------------------
// and EnumerateNetsPins(net, Proc, arg) be
// ---------------------------------------------------------------------------
// Calls Proc(pin, arg) for each pin in net
// [
// let pin = net>>Net.firstPin
// until pin>>Pin.nextPin eq 0 do
//    [
//    Proc(pin, arg)
//    pin = pin>>Pin.nextPin
//    ]
// ]

// ---------------------------------------------------------------------------
and PinsNet(pin) = valof
// ---------------------------------------------------------------------------
[
if pin>>Pin.nextPin eq 0 resultis pin - offset Net.dummyPin/16
pin = pin>>Pin.nextPin
] repeat

// ---------------------------------------------------------------------------
and PinsNetSymbol(pin) = ObjectsSymbol(PinsNet(pin))
// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------
and PinsNetString(pin) = ObjectsString(PinsNet(pin))
// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------
and PinsIC(pin, lvPinNumber; numargs na) = valof
// ---------------------------------------------------------------------------
// Returns the IC that pin is part of, and stores the pin number of pin
// into @lvPinNumber
[
let pinNumber = 0
   [ pin = pin-lenPin; pinNumber = pinNumber+1 ] repeatuntil
      Usc(pin>>Pin.nextPin, maxNPins) le 0
if na gr 1 then @lvPinNumber = pinNumber
resultis pin - offset IC.stopPin/16
]