###############################################################################
# CADENCE COPYRIGHT NOTICE                                                    #
# September 2007 Cadence Design Systems, Inc. All rights reserved.            #
#                                                                             #
# This script is AEWare, provided as an example of how to perform specialized #
# tasks within SoC Encounter.  It is not supported via the Cadence Hotline    #
# nor the CCR system.                                                         #
#                                                                             #
# This work may not be copied, re-published, uploaded, or distributed in any  #
# way, in any medium, whether in whole or in part, without prior written      #
# permission from Cadence.                                                    #
#                                                                             #
# This work is Cadence intellectual property and may under no circumstances   #
# be given to third parties, neither in original nor in modified versions,    #
# without explicit written permission from Cadence                            #
#                                                                             #
# The information contained herein is the proprietary and confidential        #
# information of Cadence or its licensors, and is supplied subject to, and    #
# may be used only by Cadence's customers in accordance with, a previously    #
# executed license and maintenance agreement between Cadence and its          #
# customer.                                                                   #
###############################################################################

#Usage: userAttachIoDiodes <diodeCellName>
#Editor's Notes:
# -Intended to be used in a block-level design to connect and
#  place a user-specified diode cell next to each port
# -Usually used to make hierarchical antenna modeling and fixing
#  simpler since input ports are known to have a diode cell very
#  near the input port

proc userAttachIoDiodes {diodeCellName} {
  Puts "Attaching diode cells to each block-level inputs port..."
  set count 0
  set diodeCell [dbGetCellByName $diodeCellName]
  if {$diodeCell == "0x0"} {
    Puts "Couldn't find a diode cell in the library called \"$diodeCellName\""
    Puts "Quitting..."
    return
  }
  dbForEachCellFTerm [dbGetCellByName $diodeCellName] fterm {
    if {[dbIsFTermInput $fterm]} {
      set diodeCellFTermName [dbFTermName $fterm]
    }
  }
  dbForEachCellFTerm [dbHeadTopCell] fterm {
#     if {[dbIsFTermInput $fterm]} {
      set net [dbFTermNet $fterm]
      if {$net > 0} {
        set netName [dbNetName $net]
#         set ftermName [dbFTermName $fterm]
#         set instName "${ftermName}_user_added_diode"
        set instName "io_diode_$count"
        addInst -cell $diodeCellName -inst $instName
        set ftermLoc [dbFTermLoc $fterm]
        set x [dbDBUToMicrons [dbLocX $ftermLoc]]
        set y [dbDBUToMicrons [dbLocY $ftermLoc]]
        placeInstance $instName $x $y -placed
        attachTerm $instName $diodeCellFTermName $netName
        incr count
      }
#     }
  }
  ecoPlace
#   setBlockPlacementStatus -name *user_added_diode -status preplaced
  Puts "Done.  Added $count diode cell(s)."
}
