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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
###############################################################################
# Copyright (c) 2014-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.
# =============================================================================
#   @script
#       Read slack values and store them at instances and instances.
#       Set Highlight colors for given slack ranges.
#       Instances are searched with partial flat instance/net names.
#       files
#           slack.tcl
#           slack.v
#           slack.txt
###############################################################################


# -----------------------------------------------------------------------------
# _init - Init global data. Slack ranges and their highlight colors.
# -----------------------------------------------------------------------------
#
proc Slack:_init {db} {
    global Slack

    ##
    # Get top module.
    #
    set top [$db get_top_design]

    ##
    # Set global hierSep and top module.
    #
    set Slack(hierSep) "/"
    set Slack(db)      $db
    set Slack(top)     $top

    ##
    # Set limits, colors and mappings.
    #
    set Slack(limits) {}
    set index 0
    foreach {
        limit   hicol comment
    } {
        -0.020   1    "green"
        -0.030   6    "lightgreen"
        -0.040  10    "yellow"
        -0.050   5    "orange"
        -99999   0    "red"
    } {
        lappend Slack(limits)       $limit ;# ordered list of limits
        set     Slack(hicol:$index) $hicol ;# mapping from rangeIndex to color
        set     Slack(index:$hicol) $index ;# mapping from color to rangeIndex
        incr index
    }
}


# -----------------------------------------------------------------------------
# _getIndex - Search rangeIndex in order list of limits.
#             Slack values are negative.
#             More negative values get higher slackIndex values.
#             return slackIndex
# -----------------------------------------------------------------------------
#
proc Slack:_getIndex {slack} {
    global Slack

    set index 0
    foreach limit $Slack(limits) {
        if {$slack > $limit} {
            return $index
        }
        incr index
    }
    incr index -1
    return $index
}


# -----------------------------------------------------------------------------
# _getHiColor - return color for given slackIndex
# -----------------------------------------------------------------------------
#
proc Slack:_getHiColor {slackIndex} {
    global Slack

    return $Slack(hicol:$slackIndex)
}


# -----------------------------------------------------------------------------
# _getIndexFromHiColor - return slackIndex for a given hiColor
# -----------------------------------------------------------------------------
#
proc Slack:_getIndexFromHiColor {hiColor} {
    global Slack

    return $Slack(index:$hiColor)
}


# -----------------------------------------------------------------------------
# _find - Search instance (what="inst") in module with split pathLst
#         starting at startIdx or net (what="net").
# -----------------------------------------------------------------------------
#
proc Slack:_find {mod pathLst startIdx what} {
    global Slack

    set len [llength $pathLst]

    set inst   {}
    set endIdx $startIdx

    while {$endIdx < $len} {
        set instName [join [lrange $pathLst $startIdx $endIdx] $Slack(hierSep)]
        set inst [$Slack(db) search inst $mod $instName]

        ##
        # Increment endIdx for next loop.
        #
        incr endIdx

        ##
        # If this instName can not be found,
        # look for net name, if nothing found try a longer path.
        #
        if {[$Slack(db) oid isnull $inst]} {
            if {$what == "net"} {
                set netName [join [lrange $pathLst $endIdx-1 end] \
                                                                $Slack(hierSep)]
                set net [$Slack(db) search net $mod $netName]
                if {![$Slack(db) oid isnull $net]} {
                    return $net
                }
            }

            set inst {}
            continue
        }

        ##
        # If we are not at the end of path,
        # prepare for next loop.
        #
        if {$endIdx != $len} {
            if {[$Slack(db) isModule $inst]} {
                ##
                # If found instance is a module instance,
                # restart search with remaining path in referenced module.
                #
                set mod [$Slack(db) moduleOf $inst]
                set startIdx $endIdx
                set inst {}
            } else {
                ##
                # If found instance is a primitive instance,
                # but we are not at the end of the path,
                # path can not be resolved.
                #
                return {}
            }
        }
    }

    if {$what == "net"} {
        return ""
    }
    return $inst
}


# -----------------------------------------------------------------------------
# _findInst - Find inst by instname which may contain partial flat inst parts.
# -----------------------------------------------------------------------------
#
proc Slack:_findInst {instName} {
    global Slack

    ##
    # Use hierSep for Splitting instName.
    #
    set pathLst [split $instName $Slack(hierSep)]

    ##
    # Search complete instance starting at top
    #
    set inst [Slack:_find $Slack(top) $pathLst 0 "inst"]

    ##
    # Return result
    #
    return $inst
}


# -----------------------------------------------------------------------------
# _findNet - Find inst by instname which may contain partial flat inst parts.
# -----------------------------------------------------------------------------
#
proc Slack:_findNet {netName} {
    global Slack

    ##
    # Use hierSep for Splitting netName.
    #
    set pathLst [split $netName $Slack(hierSep)]

    ##
    # Search complete instance/net starting at top
    #
    set net [Slack:_find $Slack(top) $pathLst 0 "net"]

    ##
    # Return result
    #
    return $net
}


# -----------------------------------------------------------------------------
# _add - Add slack to design
# -----------------------------------------------------------------------------
#
proc Slack:_add {obj slack} {
    global Slack

    ##
    # Categorize slack value.
    #
    set slackIndex [Slack:_getIndex $slack]

    ##
    # Add slack attribute.
    #
    $Slack(db) flatattr $obj set "slack=$slack"

    ##
    # Add highlight for found slack range.
    #
    set hicol [Slack:_getHiColor $slackIndex]
    $Slack(db) flathilight $obj set $hicol

    ##
    # Propagate the color up the hierarchy.
    #
    set top  [$Slack(db) oid root $obj]
    set path [$Slack(db) oid path $obj] ;# up instance path
    while {$path != {}} {
        ##
        # Create new instance oid.
        #
        set inst [$Slack(db) oid create [concat inst $top $path]]

        ##
        # Check whether there is already an highlight in this path.
        #
        set col  [$Slack(db) flathilight $inst get]
        if {$col != {}} {
            set index [Slack:_getIndexFromHiColor $col]

            ##
            # If other slack index is already higher, stop propagation.
            #
            if {$slackIndex <= $index} {
                break
            }
        }

        ##
        # Set highlight color.
        #
        $Slack(db) flathilight $inst set $hicol

        ##
        # Go up the hierarchy.
        #
        set path [$Slack(db) oid path $inst]
    }
}


# -----------------------------------------------------------------------------
# _parse - Parse the Slack file.
# -----------------------------------------------------------------------------
#
proc Slack:_parse {fname} {
    ##
    # open the file for reading.
    #
    set in [open $fname r]

    ##
    # Read the file line by line.
    #
    set header true
    set lineNo 0
    while {![eof $in]} {
        incr lineNo
        gets $in line

        ##
        # Look for header separator.
        #
        if {[string match "-----------*" $line]} {
            set header false
            continue
        }

        ##
        # Skip header info.
        #
        if {$header} {
            continue
        }

        ##
        # Split line (multiple blank, tab etc. are ok)
        #
        set lst [regexp -all -inline {\S+} $line]

        ##
        # Ignore lines which do not contain 2 fields.
        #
        set len [llength $lst]
        if {$len != 2} {
            if {$len != 0} {
                gui console print "ignored line" w $fname $lineNo
            }
            continue
        }

        ##
        # Get values.
        #
        set objName [lindex $lst 0]
        set slack   [lindex $lst 1]

        ##
        # Search instance.
        #
        set inst [Slack:_findInst $objName]
        if {$inst != {}} {
            ##
            # Process inst line.
            #
            Slack:_add $inst $slack
        } else {
            set net [Slack:_findNet $objName]
            if {$net == {}} {
                gui console print "$objName not found" w $fname $lineNo
                continue
            }

            ##
            # Process net line.
            #
            Slack:_add $net $slack
        }
    }

    ##
    # Close the input file.
    #
    close $in
}


# -----------------------------------------------------------------------------
# _read - Read the Slack file.
# -----------------------------------------------------------------------------
#
proc Slack:_read {fname} {
    global Slack

    ##
    # Get Database
    #
    set db [gui database get]
    if {$db == {}} {
        gui console print "empty database" w
        return
    }

    ##
    # Init slack ranges
    #
    Slack:_init $db
    if {$Slack(top) == {}} {
        gui console print "no top module found" w
        return
    }

    ##
    # Clear flat data.
    #
    $Slack(db) flatattr    $Slack(top) deleteAll "slack"
    $Slack(db) flathilight $Slack(top) deleteAll

    ##
    # Parse the file.
    #
    Slack:_parse $fname

    ##
    # Update GUI.
    #
    gui attribute changed
    gui highlight changed
}


# -----------------------------------------------------------------------------
# _openFile - Show a browse file dialog to open a slack file.
# -----------------------------------------------------------------------------
#
proc Slack:_openFile {} {
    set fname [gui window fileDialog openFile "Load Slack" \
                                      {{"Slack Files" {".txt"}}}]
    if {$fname == "" } {
        return
    }
    Slack:_read $fname
}


# =============================================================================
# Add a new entry to the main menu to open a slack file.
# =============================================================================
#
gui menu command {"Userware" "Open Slack File"} {Slack:_openFile}