1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
###############################################################################
# Copyright (c) 2011-2024 by Altair Engineering, Inc.
# All rights reserved.
#
# Altair Engineering, Inc. makes this software available as part of the Vision
# tool platform.  As long as you are a licensee of the Vision tool platform
# you may make copies of the software and modify it to be used within the
# Vision tool platform, but you must include all of this notice on any copy.
# Redistribution without written permission to any third party, with or
# without modification, is not permitted.
# Altair Engineering, Inc. does not warrant that this software is error free
# or fit for any purpose.  Altair Engineering, Inc. disclaims any liability for
# all claims, expenses, losses, damages and costs any user may incur as a
# result of using, copying or modifying the software.
# =============================================================================
#   @userware
#       Find Paths to Clocked Cells
#   @section
#       Create Reports
#   @description
#       Read an input file that contains cell names and pin names. Then a path
#       search is performed and the name of the first reachable clocked cell
#       is dumped into an output file.
#   @files
#       cust11/flopsAtIP.tcl
#   @example
#       demo/spice/gl85.sp
#   @tag
#       zdb netlist verilog
###############################################################################


# =============================================================================
# Define a list of patterns to identify clocked cells.
# =============================================================================
#
set FindFlopAtIP(clockedCellPatterns) {
    DFF*
    df*
    sync*
}


##
# Specify the name of a re-convergence error report file.
#
set FindFlopAtIP(errorRep) "reconvergence.errors.txt"


# =============================================================================
# Start - This is the main procedure of this Userware.
# =============================================================================
#
proc FindFlopAtIP:Start {db} {
    global FindFlopAtIP

    ##
    # Return if the database is empty.
    #
    if {$db == {}} {
        return
    }

    ##
    # Check if the pin list file exists.
    #
    if {![file exists $FindFlopAtIP(PinList)]} {
        error "Pin list file '$FindFlopAtIP(PinList)' does not exist."
    }

    ##
    # Enable the progress bar.
    #
    zprogress begin

    ##
    # Pre-process the database and flag clocked cells and leaf cells.
    #
    zprogress push "Flag clocked cells" 0.05
    FindFlopAtIP:_flagClockedCells $db $FindFlopAtIP(clockedCellPatterns)
    if {[zprogress pop]} {
        zprogress end
        return
    }
    zprogress push "Flag leaf cells" 0.1
    FindFlopAtIP:_flagLeafCells    $db
    if {[zprogress pop]} {
        zprogress end
        return
    }

    ##
    # Open the pin list file and the inst list report file.
    #
    set in  [open $FindFlopAtIP(PinList)  "r"]
    set out [open $FindFlopAtIP(InstList) "w"]
    set err [open $FindFlopAtIP(errorRep) "w"]

    ##
    # Read each line of the input file.
    #
    set count 0
    while {![eof $in]} {
        gets $in
        incr count
    }
    seek $in 0
    set i 0
    zprogress push "Find paths" 0.9
    while {![eof $in]} {
        ##
        # Read the next line of the input file.
        #
        set bytes [gets $in line]
        if {$bytes < 0} {
            break
        }

        ##
        # Update the progress bar.
        #
        zprogress push "" [expr {[incr i] / double($count)}]

        ##
        # Skip empty lines.
        #
        set line [string trim $line]
        if {($line == "") || [string match "#*" $line]} {
            continue
        }

        ##
        # Two entries per line expected
        #
        if {[llength $line] != 2} {
            zmessage print ERR "Two entries per line expected."
            continue
        }

        ##
        # Get the cell and pin name.
        #
        set cellName [lindex $line 0]
        set pinName  [lindex $line 1]

        ##
        # Try to find the IP module.
        #
        set module [$db search module $cellName]
        if {[$db oid isnull $module]} {
            zmessage print ERR "Cannot find module '$cellName'."
            continue
        }

        ##
        # Find the port of the IP module.
        #
        set port [$db search port $module $pinName]
        if {[$db oid isnull $port]} {
            zmessage print ERR \
                "Cannot find pin '$pinName' at module '$cellName'."
            continue
        }

        ##
        # Flag all ports of the module as target except the start port.
        #
        $db foreach port $module ioPort {$db flag $ioPort set target}
        $db flag $port clear target

        ##
        # Set the path extraction direction depending on the pin direction.
        #
        switch -- [$db directionOf $port] {
            "input"  {set dir "-out"}
            "output" -
            "inout"  {set dir "-in"}
            default  {
                set msg "Unknown direction: port '$cellName $port' ignored."
                zmessage print ERR $msg
                continue
            }
        }

        ##
        # Find paths to the first clocked cell or to I/O ports of this module.
        # The 'catch' is needed to avoid a Tcl error if the interrupt button
        # on the progress bar is pressed.
        #
        catch {
            set res [$db cone -paths $dir \
                     -targetFlaggedCell clock \
                     -targetFlaggedPort target \
                     $port]
        }

        if {[zprogress pop]} {
            break
        }

        ##
        # Only dump the name of the start cell and start pin to the
        # report file if the result is not empty.
        #
        if {[llength $res] > 0} {
            puts $out "$cellName $pinName"
        }

        ##
        # Process the result and fill a hash table with the target instances.
        #
        array unset _targets
        array set   _targets {}
        foreach path $res {
            ##
            # Remove the depth of the path from the result.
            #
            set path [lrange $path 1 end]

            ##
            # Get the target of this path.
            #
            set target [lindex $path end]

            ##
            # If this target is hit the first time then initialize the
            # hash table that count the number of occurrences.
            #
            if {![info exists _targets($target)]} {
                set _targets($target) 0
            }

            ##
            # Increase the number of occurrences.
            #
            incr _targets($target)
        }

        ##
        # If at least one pin target is reached more than one time
        # then create an entry in the error report file.
        #
        foreach target [array names _targets] {
            ##
            # Skip port targets.
            #
            if {[$db oid type $target] == "port"} {
                continue
            }

            ##
            # If the pin target count is greater than one then print the
            # cell name and start pin name to the error report.
            #
            if {$_targets($target) > 1} {
                puts $err "$cellName $pinName"
                break
            }
        }

        ##
        # Process all targets in the _target hash table and write either
        # the instance name of the flop or the port name to the report file.
        #
        set hiersep [$db oper hiersep get]
        foreach target [array names _targets] {
            ##
            # If the target is a pin then convert it to an instance.
            #
            if {[$db oid type $target] == "pin"} {
                set inst [$db oid convertTo inst $target]
                set path [$db oid path  $inst]
                set name [$db oid oname $inst]
                set instName [join [concat $path $name] $hiersep]
                set pinName  [$db oid pname $target]
                set cellName [$db oid cname $inst]
                puts $out "    IP_Inst: $instName/$pinName $cellName"

                ##
                # If this target is more than one time in the result list
                # then this is already a case of re-convergence.
                # Need to dump it to the error report.
                #
                if {$_targets($target) > 1} {
                    puts $err "    IP_Inst: $instName"
                }
            }

            ##
            # If the target is a port then dump it to the report file.
            #
            if {[$db oid type $target] == "port"} {
                set portName [$db oid oname $target]
                puts $out "    IP_Port: $portName"
            }
        }
    }

    ##
    # Close the input and the output files.
    #
    close $in
    close $out
    close $err

    ##
    # End the progress bar.
    #
    zprogress pop
    zprogress end
}


# -----------------------------------------------------------------------------
# _flagClockedCells - Flag all cells that match the given pattern as clocked.
# -----------------------------------------------------------------------------
#
proc FindFlopAtIP:_flagClockedCells {db cellListPattern} {
    ##
    # Count the number of cells (for the progress bar).
    #
    set count 0
    $db foreach cell cell {incr count}

    ##
    # Loop over all loaded cells.
    #
    set i 0
    $db foreach cell cell {
        ##
        # Update the progress bar.
        #
        if {[zprogress update "" [incr i] $count]} {
            break
        }

        ##
        # Loop over all clocked cell patterns.
        #
        foreach cellPattern $cellListPattern {
            ##
            # If a pattern matches the cell name then set the 'clock' flag
            # and break the pattern loop.
            #
            if {[string match $cellPattern [$db oid oname $cell]]} {
                $db flag $cell set clock
                break
            }
        }
    }
}


# -----------------------------------------------------------------------------
# _flagLeafCells - Flag all leaf cells.
# -----------------------------------------------------------------------------
#
proc FindFlopAtIP:_flagLeafCells {db} {
    set count 0
    $db foreach module module {incr count}

    ##
    # Loop over all modules and flag modules that contain only devices
    # as library cells.
    #
    set i 0
    $db foreach module module {
        ##
        # Update the progress bar.
        #
        if {[zprogress update "" [incr i] $count]} {
            break
        }

        set isLeaf true
        $db foreach inst $module inst {
            if {![$db tdevice $inst]} {
                set isLeaf false
                zprogress update "" 99 100
                break
            }
        }
        if {$isLeaf} {$db flag $module set libcell}
    }
    if {[zprogress isinterrupted]} {
        return
    }

    ##
    # From now on the database treats all library cells as primitives.
    #
    $db setPrimitive -libcell
}


##
# Get the name of the input file from the command line.
#
if {($argc != 2) || ([lindex $argv 0] == {}) || ([lindex $argv 1] == {})} {
    error "Usage '-userware3 flopsAtIP.tcl pinList.txt instListReport.txt'."
}

set FindFlopAtIP(PinList)  [lindex $argv 0]
set FindFlopAtIP(InstList) [lindex $argv 1]


##
# Use gui database runOrRegisterChangedCallback to immediately run
# FindFlopAtIP:Start if we have a database, or otherwise register the proc to
# be executed after the database is available.
#
gui database runOrRegisterChangedCallback FindFlopAtIP:Start