//  RouteDaffodilD.bcpl
//  Route description of Daffodil Board part D (4 of 4)
//  Last modified June 15, 1983  11:54 AM by Diebert

get "route.defs"
external [ VCCPinPos; UpperLeft; LowerLeft; Right; PinGridPower; InArray; Center ]

manifest
   [
   firstDipPin = 1
   firstJ1Pin = firstDipPin + 5292
   firstJ2Pin = firstJ1Pin + 66
   firstJ3Pin = firstJ2Pin + 60
   firstJ4Pin = firstJ3Pin + 25
   firstJ5Pin = firstJ4Pin + 15
   firstJ6Pin = firstJ5Pin + 4
   firstJ7Pin = firstJ6Pin + 50
   firstJ8Pin = firstJ7Pin + 50
   firstGNDPin = firstJ8Pin + 26
   firstVCCPin = firstGNDPin + 826
   NextIndex  = firstVCCPin + 706
   ]
 manifest
   [
   GND = 0
   VCC = 1
   ]

let InArray(x, inc, min, max) = valof [
   if (x ls min) % (x gr max) then resultis false
   resultis ((x - min) rem inc) eq 0  ]

and UpperLeft(absX, absY, fstGnd, fstVcc, pPinNo, picclass) = valof
   [
   let Offset = nil
   let type = nil
   switchon (absX rem 300) into
      [
      case 0: 
         [ // GND first row
         test ((absY-1400) rem 1000) eq 0
            ifso [ type = GND; Offset = (absY-1400)/1000 ]
            ifnot [ type = VCC; Offset = (absY-1900)/1000 ]
         endcase ]
      case 100:
         [ // VCC first row
         test ((absY-1400) rem 1000) eq 0
            ifso [ type = VCC; Offset = (absY-1400)/1000 + 4]
            ifnot [ type = GND; Offset = (absY-1900)/1000 + 5]
         endcase ]
      default:
         [ CallSwat(" HELP!!!!") ]
      ]
   let grp = absX / 300
   switchon type into
      [
      case GND:
         [ @pPinNo = grp * 9 + Offset + fstGnd
         @picclass = MustFindNamee("GND",typeIcclass)
         resultis @pPinNo + firstGNDPin - 1
         endcase ]
      case VCC:
         [ @pPinNo = grp * 9 + Offset + fstVcc
         @picclass = MustFindNamee("VCC",typeIcclass)
         resultis @pPinNo + firstVCCPin - 1
         endcase ]
      ]
   ]


and LowerLeft(absX, absY, fstGnd, fstVcc, pPinNo, picclass) = valof
   [
   if absY eq 500 then absY = 400
   let Offset = nil
   let type = nil
   switchon (absX rem 300) into
      [
      case 0: 
         [ // GND first row
         test ((absY-400) rem 1000) eq 0
            ifso [ type = GND; Offset = (absY-400)/1000 ]
            ifnot [ type = VCC; Offset = (absY-900)/1000 ]
         endcase ]
      case 100:
         [ // VCC first row
         test ((absY-400) rem 1000) eq 0
            ifso [ type = VCC; Offset = (absY-400)/1000 + 5]
            ifnot [ type = GND; Offset = (absY-900)/1000 + 6]
         endcase ]
      default:
         [ CallSwat(" HELP!!!!") ]
      ]
   let grp = absX / 300
   switchon type into
      [
      case GND:
         [ @pPinNo = grp * 11 + Offset + fstGnd
         @picclass = MustFindNamee("GND",typeIcclass)
         resultis @pPinNo + firstGNDPin - 1
         endcase ]
      case VCC:
         [ @pPinNo = grp * 11 + Offset + fstVcc
         @picclass = MustFindNamee("VCC",typeIcclass)
         resultis @pPinNo + firstVCCPin - 1
         endcase ]
      ]
   ]


and Right(absX, absY, fstGnd, fstVcc, pPinNo, picclass) = valof
   [
   let Offset = nil
   let type = nil
   switchon (absX rem 300) into
      [
      case 0: 
         [ // VCC first row
         test ((absY-5900) rem 1000) eq 0
            ifso [ type = VCC; Offset = (absY-5900)/1000 ]
            ifnot [ type = GND; Offset = (absY-6400)/1000 ]
         endcase ]
      case 100:
         [ // GND first row
         test ((absY-5900) rem 1000) eq 0
            ifso [ type = GND; Offset = (absY-5900)/1000 + 6]
            ifnot [ type = VCC; Offset = (absY-6400)/1000 + 6]
         endcase ]
      default:
         [ CallSwat(" HELP!!!!") ]
      ]
   let grp = absX / 300
   switchon type into
      [
      case GND:
         [ @pPinNo = grp * 12 + Offset + fstGnd
         @picclass = MustFindNamee("GND",typeIcclass)
         resultis @pPinNo + firstGNDPin - 1
         endcase ]
      case VCC:
         [ @pPinNo = grp * 12 + Offset + fstVcc
         @picclass = MustFindNamee("VCC",typeIcclass)
         resultis @pPinNo + firstVCCPin - 1
         endcase ]
      ]
   ]


and Center(absX, absY, fstGnd, fstVcc, pPinNo, picclass) = valof
   [
   let Offset = nil
   let type = nil
   switchon (absX rem 200) into
      [
      case 100: 
         [ // VCC first row
         test ((absY-5900) rem 1000) eq 0
            ifso [ type = VCC; Offset = (absY-5900)/1000 + 6 ]
            ifnot [ type = GND; Offset = (absY-6400)/1000 + 6 ]
         endcase ]
      case 0:
         [ // GND first row
         test ((absY-5900) rem 1000) eq 0
            ifso [ type = GND; Offset = (absY-5900)/1000 ]
            ifnot [ type = VCC; Offset = (absY-6400)/1000 ]
         endcase ]
      default:
         [ CallSwat(" HELP!!!!") ]
      ]
   let grp = absX / 200
   switchon type into
      [
      case GND:
         [ @pPinNo = grp * 12 + Offset + fstGnd
         @picclass = MustFindNamee("GND",typeIcclass)
         resultis @pPinNo + firstGNDPin - 1
         endcase ]
      case VCC:
         [ @pPinNo = grp * 12 + Offset + fstVcc
         @picclass = MustFindNamee("VCC",typeIcclass)
         resultis @pPinNo + firstVCCPin - 1
         endcase ]
      ]
   ]

and PinGridPower(absX, absY, fstGnd, fstVcc, pPinNo, picclass) = valof
   [
   let Offset = nil
   let type = nil
   switchon (absX rem 200) into
      [
      case 0: 
         [ // VCC first row
         switchon absY into
            [
            case 7400: [ Offset = 0; type = GND; endcase ]
            case 7700: [ Offset = 0; type = VCC; endcase ]
            case 9400: [ Offset = 1; type = GND; endcase ]
            case 9700: [ Offset = 1; type = VCC; endcase ]
            case 11400: [ Offset = 2; type = GND; endcase ]
            case 11500: [ Offset = 2; type = VCC; endcase ]
            default: resultis 0
            ]
         endcase ]
      case 100:
         [ // GND first row
         switchon absY into
            [
            case 7400: [ Offset = 3; type = VCC; endcase ]
            case 7700: [ Offset = 3; type = GND; endcase ]
            case 9400: [ Offset = 4; type = VCC; endcase ]
            case 9700: [ Offset = 4; type = GND; endcase ]
            case 11400: [ Offset = 5; type = VCC; endcase ]
            case 11500: [ Offset = 5; type = GND; endcase ]
            default: resultis 0
            ]
         endcase ]
      default:
         [ CallSwat(" HELP!!!!") ]
      ]
   let grp = absX / 200
   switchon type into
      [
      case GND:
         [ @pPinNo = grp * 6 + Offset + fstGnd
         @picclass = MustFindNamee("GND",typeIcclass)
         resultis @pPinNo + firstGNDPin - 1
         endcase ]
      case VCC:
         [ @pPinNo = grp * 6 + Offset + fstVcc
         @picclass = MustFindNamee("VCC",typeIcclass)
         resultis @pPinNo + firstVCCPin - 1
         endcase ]
      ]
   ]