//  RouteDaffodilB.bcpl
//  Route description of Daffodil Board part B (2 of 4)
//  Last modified June 16, 1983  9:49 AM by Diebert

get "route.defs"
external [ VCCPinPos; GNDPinPos ]

let VCCPinPos(icinst, pin, px, py, pInfo; numargs na) = valof
   [
   DefaultArgs(lv na, -4, lv na)
   @pInfo = noDisconnect + noReconnect + BottomLevel
   if (pin ls 1) % (pin gr 706) then resultis illegal

   if pin ls 28 do
      [ let group = (pin - 1) / 9
      let subpin = (pin - 1) rem 9
      let xoffset = nil
      test subpin ls 4
         ifso [ xoffset = 0; @py = ((subpin) * 10 * 4) + (19 * 4) ]
         ifnot [ xoffset = 4; @py = ((subpin - 4) * 10 * 4) + (14 * 4) ]
      @px = 8 * 4 + (group * 3 * 4) + xoffset
      resultis absolute ]

   if pin ls 32 do
      [ @px = 20 * 4; @py = ((pin - 28) * 10 * 4) + (19 * 4)
      resultis absolute ]

   if pin ls 37 do
      [ @px = 21 * 4; @py = ((pin - 32) * 10 * 4) + (14 * 4)
      resultis absolute ]

   if pin ls 127 do
      [ let group = (pin - 37) / 9
      let subpin = (pin - 37) rem 9
      let xoffset = nil
      test subpin ls 4
         ifso [ xoffset = 0; @py = ((subpin) * 10 * 4) + (19 * 4) ]
         ifnot [ xoffset = 4; @py = ((subpin - 4) * 10 * 4) + (14 * 4) ]
      @px = 26 * 4 + (group * 3 * 4) + xoffset
      resultis absolute ]

   if pin ls 131 do
      [ @px = 59 * 4; @py = ((pin - 127) * 10 * 4) + (19 * 4)
      resultis absolute ]

   if pin ls 136 do
      [ @px = 60 * 4; @py = ((pin - 131) * 10 * 4) + (14 * 4)
      resultis absolute ]

   if pin ls 235 do
      [ let group = (pin - 136) / 11
      let subpin = (pin - 136) rem 11
      let xoffset = nil
      test subpin ls 5
         ifso [ xoffset = 0; @py = ((subpin) * 40) + (9 * 4) ]
         ifnot [ xoffset = 4; @py = ((subpin - 5) * 40) + (4 * 4) ]
      @px = 65 * 4 + (group * 3 * 4) + xoffset
      if @py eq (4 * 4) then @py = 5 * 4
      resultis absolute ]

   if pin ls 240 do
      [ @px = 95 * 4; @py = ((pin - 235) * 10 * 4) + (9 * 4)
      if @py eq (4 * 4) then @py = 5 * 4
      resultis absolute ]

   if pin ls 246 do
      [ @px = 96 * 4; @py = ((pin - 240) * 10 * 4) + (4 * 4)
      if @py eq (4 * 4) then @py = 5 * 4
      resultis absolute ]

   if pin ls 323 do
      [ let group = (pin - 246) / 11
      let subpin = (pin - 246) rem 11
      let xoffset = nil
      test subpin ls 5
         ifso [ xoffset = 0; @py = ((subpin) * 10 * 4) + (9 * 4) ]
         ifnot [ xoffset = 4; @py = ((subpin - 5) * 10 * 4) + (4 * 4) ]
      @px = 101 * 4 + (group * 3 * 4) + xoffset
      if @py eq (4 * 4) then @py = 5 * 4
      resultis absolute ]


// Right Side

   if pin ls 359 do
      [ let group = (pin - 323) / 12
      let subpin = (pin - 323) rem 12
      let xoffset = nil
      test subpin ls 6
         ifso [ xoffset = 0; @py = ((subpin) * 10 * 4) + (59 * 4) ]
         ifnot [ xoffset = 4; @py = ((subpin - 6) * 10 * 4) + (64 * 4) ]
      @px = 8 * 4 + (group * 3 * 4) + xoffset
      resultis absolute ]

   if pin ls 365 do
      [ @px = 20 * 4; @py = ((pin - 359) * 10 * 4) + (59 * 4)
      resultis absolute ]

   if pin ls 371 do
      [ @px = 21 * 4; @py = ((pin - 365) * 10 * 4) + (64 * 4)
      resultis absolute ]

   if pin ls 395 do
      [ let group = (pin - 371) / 12
      let subpin = (pin - 371) rem 12
      let xoffset = nil
      test subpin ls 6
         ifso [ xoffset = 0; @py = ((subpin) * 10 * 4) + (59 * 4) ]
         ifnot [ xoffset = 4; @py = ((subpin - 6) * 10 * 4) + (64 * 4) ]
      @px = 26 * 4 + (group * 3 * 4) + xoffset
      resultis absolute ]

   if pin ls 437 do
      [ let group = (pin - 395) / 6
      let subpin = (pin - 395) rem 6
      let xoffset = nil
      test subpin ls 3
         ifso [ xoffset = 0; @py = ((subpin) * 20 * 4) + (77 * 4) ]
         ifnot [ xoffset = 4; @py = ((subpin - 3) * 20 * 4) + (74 * 4) ]
      if @py eq (117 * 4) then @py = 115 * 4
      @px = 33 * 4 + (group * 2 * 4) + xoffset
      resultis absolute ]

   if pin ls 467 do
      [ let group = (pin - 437) / 12
      let subpin = (pin - 437) rem 12
      let xoffset = nil
      test subpin ls 6
         ifso [ xoffset = 0; @py = ((subpin) * 10 * 4) + (64 * 4) ]
         ifnot [ xoffset = 4; @py = ((subpin - 6) * 10 * 4) + (59 * 4) ]
      @px = 47 * 4 + (group * 2 * 4) + xoffset
      resultis absolute ]

   if pin ls 509 do
      [ let group = (pin - 467) / 6
      let subpin = (pin - 467) rem 6
      let xoffset = nil
      test subpin ls 3
         ifso [ xoffset = 0; @py = ((subpin) * 20 * 4) + (77 * 4) ]
         ifnot [ xoffset = 4; @py = ((subpin - 3) * 20 * 4) + (74 * 4) ]
      if @py eq (117 * 4) then @py = 115 * 4
      @px = 52 * 4 + (group * 2 * 4) + xoffset
      resultis absolute ]

   if pin ls 539 do
      [ let group = (pin - 509) / 12
      let subpin = (pin - 509) rem 12
      let xoffset = nil
      test subpin ls 6
         ifso [ xoffset = 0; @py = ((subpin) * 10 * 4) + (64 * 4) ]
         ifnot [ xoffset = 4; @py = ((subpin - 6) * 10 * 4) + (59 * 4) ]
      @px = 66 * 4 + (group * 2 * 4) + xoffset
      resultis absolute ]

   if pin ls 581 do
      [ let group = (pin - 539) / 6
      let subpin = (pin - 539) rem 6
      let xoffset = nil
      test subpin ls 3
         ifso [ xoffset = 0; @py = ((subpin) * 20 * 4) + (77 * 4) ]
         ifnot [ xoffset = 4; @py = ((subpin - 3) * 20 * 4) + (74 * 4) ]
      if @py eq (117 * 4) then @py = 115 * 4
      @px = 71 * 4 + (group * 2 * 4) + xoffset
      resultis absolute ]

   if pin ls 587 do
      [ @px = 85 * 4; @py = ((pin - 581) * 10 * 4) + (64 * 4)
      resultis absolute ]

   if pin ls 611 do
      [ let group = (pin - 587) / 12
      let subpin = (pin - 587) rem 12
      let xoffset = nil
      test subpin ls 6
         ifso [ xoffset = 0; @py = ((subpin) * 40) + (59 * 4) ]
         ifnot [ xoffset = 4; @py = ((subpin - 6) * 40) + (64 * 4) ]
      @px = 86 * 4 + (group * 3 * 4) + xoffset
      resultis absolute ]

   if pin ls 617 do
      [ @px = 95 * 4; @py = ((pin - 611) * 10 * 4) + (59 * 4)
      resultis absolute ]

   if pin ls 623 do
      [ @px = 96 * 4; @py = ((pin - 617) * 10 * 4) + (64 * 4)
      resultis absolute ]

   if pin ls 707 do
      [ let group = (pin - 623) / 12
      let subpin = (pin - 623) rem 12
      let xoffset = nil
      test subpin ls 6
         ifso [ xoffset = 0; @py = ((subpin) * 10 * 4) + (59 * 4) ]
         ifnot [ xoffset = 4; @py = ((subpin - 6) * 10 * 4) + (64 * 4) ]
      @px = 101 * 4 + (group * 3 * 4) + xoffset
      resultis absolute ]

   resultis illegal


   ]


let FindCoordFromString(s, px, py, vop1, hop1; numargs na) = valof
//  This routine takes the string name of a board location and computes 
//    the internal co-ordinates x,y of that position.
//  It then adds vop1 and hop1 as the offsets assuming that pin 1 is
//    installed at x,y.
//  The result is placed in @px,@py.
//  The result is absolute if the pin is legal, and illegal otherwise.
//  vop1 is the +y column vertical long  way offset from pin 1; 
//  hop1 is the -x row  horizontal short way offset from pin 1, 
//  In a standard 16 pin DIP,
//    pin 2  vop1=4 and hop1=0,
//    pin 16 vop1=0 and hop1= - 12 
   [
   DefaultArgs(lv na, -3, 0, 0)
   let columny = -1
   let rowx = -1
   let next = 1
   let okflag = false

   let getlc(ss, idx) = valof
      [
      let cc = ss>>str.char↑idx
      if (cc ge $A) & (cc le $Z) then cc = cc - $A + $a
      resultis cc
      ]

   let c = getlc(s, next)
   if c eq $# then [ next = next + 1; c = getlc(s, next) ]

   if (c ls $a) % (c gr $e) then resultis illegal

   switchon c into
   [
   case $a:
      [ next = next + 1; c = getlc(s, next)
      if c ls $a % c gr $z then resultis illegal
      switchon c into 
         [
         case $a: [ rowx = 7 * 4; endcase ]
         case $d: [ rowx = 10 * 4; endcase ]
         case $g: [ rowx = 13 * 4; endcase ]
         case $j: [ rowx = 16 * 4; endcase ]
         case $l: [ rowx = 18 * 4; endcase ]
         case $m: [ rowx = 19 * 4; endcase ]
         case $p: [ rowx = 22 * 4; endcase ]
         case $r: [ rowx = 24 * 4; endcase ]
         case $s: [ rowx = 25 * 4; endcase ]
         case $v: [ rowx = 28 * 4; endcase ]
         case $z: [ rowx = 31 * 4; endcase ]
         default: resultis illegal
         ]

      columny = 0
      for i = (next+1) to s>>str.length do
         [
         let c = getlc(s, i)
         if (c ls $0) % (c gr $9) then resultis illegal
         columny = (10 * columny) + (c - $0)
         ]
      if (columny ls 8) % (columny gr 111) then resultis illegal
      endcase
      ]

   case $b:
      [
      next = next + 1; let cc = getlc(s, next)
      if (cc ls $a) % (cc gr $z) then resultis illegal

      columny = 0
      for i = (next+1) to s>>str.length do
         [
         let c = getlc(s, i)
         if (c ls $0) % (c gr $9) then resultis illegal
         columny = (10 * columny) + (c - $0)
         ]
      if (columny ls 8) % (columny gr 108) then resultis illegal
      if columny ls 51 do
         [ switchon cc into
            [
            case $c: [ rowx = 34 * 4; endcase]
            case $f: [ rowx = 37 * 4; endcase]
            case $i: [ rowx = 40 * 4; endcase]
            case $l: [ rowx = 43 * 4; endcase]
            case $o: [ rowx = 46 * 4; endcase]
            case $r: [ rowx = 49 * 4; endcase]
            case $u: [ rowx = 52 * 4; endcase]
            case $x: [ rowx = 55 * 4; endcase]
            case $z: [ rowx = 57 * 4; endcase]
            default: resultis illegal
            ]
         ]
      if columny ge 55 do
         [ okflag = false
         if (cc ge $b) & (cc le $o) do
           [ rowx = ((cc - $b) + 33) * 4 ; okflag = true ]
         if cc ge $u do
           [ rowx = ((cc - $u) + 52) * 4 ; okflag = true ]
         unless okflag do resultis illegal
         ]
      endcase
      ]

   case $c:
      [ next = next + 1; let cc = getlc(s, next)
      if (cc ls $a) % (cc gr $z) then resultis illegal

      columny = 0
      for i = (next+1) to s>>str.length do
         [
         let c = getlc(s, i)
         if (c ls $0) % (c gr $9) then resultis illegal
         columny = (10 * columny) + (c - $0)
         ]
      if columny ls 8 & cc eq $a then resultis illegal
      if columny le 0 then resultis illegal
      if columny gr 108 then resultis illegal
      if columny ls 51 do
         [ switchon cc into
            [
            case $a: [ rowx = 58 * 4; endcase]
            case $d: [ rowx = 61 * 4; endcase]
            case $f: [ rowx = 63 * 4; endcase]
            case $g: [ rowx = 64 * 4; endcase]
            case $j: [ rowx = 67 * 4; endcase]
            case $m: [ rowx = 70 * 4; endcase]
            case $p: [ rowx = 73 * 4; endcase]
            case $s: [ rowx = 76 * 4; endcase]
            case $v: [ rowx = 79 * 4; endcase]
            case $y: [ rowx = 82 * 4; endcase]
            default: resultis illegal
            ]
         ]
      if columny ge 55 do
         [ okflag = false
         if cc le $h do
           [ rowx = ((cc - $a) + 58) * 4 ; okflag = true ]
         if cc ge $n do
           [ rowx = ((cc - $n) + 71) * 4 ; okflag = true ]
         unless okflag do resultis illegal
         ]
      endcase
      ]

   case $d:
      [ next = next + 1; let cc = getlc(s, next)
      if (cc ls $a) % (cc gr $z) then resultis illegal

      columny = 0
      for i = (next+1) to s>>str.length do
         [
         let c = getlc(s, i)
         if (c ls $0) % (c gr $9) then resultis illegal
         columny = (10 * columny) + (c - $0)
         ]
      if cc eq $a & columny ge 108 then resultis illegal
      if cc eq $a & columny ls 55 then resultis illegal
      if cc eq $b & columny gr 50 then resultis illegal
      if columny le 0 then resultis illegal
      if columny gr 111 then resultis illegal
      switchon cc into
         [
         case $a: [ rowx = 84 * 4; endcase]
         case $b: [ rowx = 85 * 4; endcase]
         case $e: [ rowx = 88 * 4; endcase]
         case $h: [ rowx = 91 * 4; endcase]
         case $j: [ rowx = 93 * 4; endcase]
         case $k: [ rowx = 94 * 4; endcase]
         case $n: [ rowx = 97 * 4; endcase]
         case $p: [ rowx = 99 * 4; endcase]
         case $q: [ rowx = 100 * 4; endcase]
         case $t: [ rowx = 103 * 4; endcase]
         case $w: [ rowx = 106 * 4; endcase]
         case $z: [ rowx = 109 * 4; endcase]
         default: resultis illegal
         ]
      endcase
      ]

   case $e:
      [ next = next + 1; let cc = getlc(s, next)
      if (cc ls $a) % (cc gr $l) then resultis illegal

      columny = 0
      for i = (next+1) to s>>str.length do
         [
         let c = getlc(s, i)
         if (c ls $0) % (c gr $9) then resultis illegal
         columny = (10 * columny) + (c - $0)
         ]
      if columny le 0 then resultis illegal
      if columny gr 111 then resultis illegal
      switchon cc into
         [
         case $c: [ rowx = 112 * 4; endcase]
         case $f: [ rowx = 115 * 4; endcase]
         case $i: [ rowx = 118 * 4; endcase]
         case $l: [ rowx = 121 * 4; endcase]
         default: resultis illegal
         ]
      endcase
      ]

   default: resultis illegal
   ]

   if rowx ge (33 * 4) & rowx le (84 * 4) do
      [ if (columny ge 69) & (columny le 74) then resultis illegal
      if (columny ge 89) & (columny le 94) then resultis illegal ]
   if (columny gr 50) & (columny ls 55) then resultis illegal
   @px = rowx - hop1
   @py = (4 * 4) + (columny * 4) + vop1
   resultis absolute
   ]