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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
###############################################################################
# Copyright (c) 2017-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
#       This Userware example load paths from a report file and provide the
#       functionality to display the paths in the Cone window as well as
#       annotation of the timing values at the pins.
###############################################################################


##
# Get a pointer to the database.
#
set db [gui database get]


# =============================================================================
# Init - Initialize global variables.
# =============================================================================
#
proc PathReport:Init {{db {}} {argc {}} {argv {}}} {
    global PathReport

    ##
    # If no database is given then get the current database.
    #
    if {$db == {}} {
        set db [gui database get]
    }

    ##
    # Initialize the variable storing the database.
    #
    set PathReport(db) $db

    ##
    # Return if no database is set.
    #
    if {$db == {}} {
        return
    }

    ##
    # Initialize the design top module.
    #
    set PathReport(top) [$db get_top_design]

    ##
    # If an additional argument is present then it is a file name of a
    # report file to be loaded.
    #
    if {$argc == 1} {
        PathReport:LoadFile [lindex $argv 0]
    }
}


# -----------------------------------------------------------------------------
# _parseFile - Parse the report file.
# -----------------------------------------------------------------------------
#
proc PathReport:_parseFile {fname} {
    ##
    # Open the report file for reading.
    #
    set in [open $fname "r"]

    ##
    # Read each line of the input file.
    #
    while {![eof $in]} {
        set line [PathReport:_getLine $in]

        ##
        # Match the start of the path summary section.
        #
        if {[string match "-- Path Summary --*" $line]} {
            PathReport:_extractSummary $in
        }

        ##
        # Match the start of the actual details for each path.
        #
        if {[string match {Path:[0-9]*} $line]} {
            PathReport:_extractPath $in $line
        }
    }

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


# -----------------------------------------------------------------------------
# _getLine - Get the next line from the given input stream.
# -----------------------------------------------------------------------------
#
proc PathReport:_getLine {in} {
    set line [gets $in]
    set line [string trim $line]
    set line [string map {"\\" ""} $line]

    return $line
}


# -----------------------------------------------------------------------------
# _extractSummary - Add path summary to the result table.
# -----------------------------------------------------------------------------
#
proc PathReport:_extractSummary {in} {
    global PathReport

    while {![eof $in]} {
        set line [PathReport:_getLine $in]

        ##
        # Add the path summary to the report table.
        #
        if {[string match {Path:[0-9]*} $line]} {
            set id [lindex $line 0]
            $PathReport(table) insert {} end -id $id -text $line
        }

        ##
        # Match the end of the path summary section.
        #
        if {[string match ">report_paths*" $line]} {
            break
        }
    }
}


# -----------------------------------------------------------------------------
# _extractPath - Extract a path from the report and add it to the result table.
# -----------------------------------------------------------------------------
#
proc PathReport:_extractPath {in line} {
    global PathReport

    set db    $PathReport(db)
    set id    [lindex $line 0]
    set delay [lindex $line 1]

    set headerRead 0
    array set detailKeys {}

    while {![eof $in]} {
        ##
        # Get the next line of the input file.
        #
        set line [PathReport:_getLine $in]

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

        ##
        # Match the headline of a detailed path.
        #
        if {[string match "Time*" $line]} {
            ##
            # Store the indices of each detailed information.
            #
            set detailKeys(time)   [lsearch -exact $line "Time"]
            set detailKeys(edge)   [lsearch -exact $line "Edge"]
            set detailKeys(net)    [lsearch -exact $line "Net"]
            set detailKeys(inst)   [lsearch -exact $line "Inst(Cell)"]
            set detailKeys(inPin)  [lsearch -exact $line "InPin"]
            set detailKeys(outPin) [lsearch -exact $line "OutPin"]
            set detailKeys(delay)  [lsearch -exact $line "Delay(ns)"]
            set detailKeys(slope)  [lsearch -exact $line "Slope(ns)"]

            ##
            # Set a variable to indicate that the header was read.
            #
            set headerRead 1
            continue
        }

        ##
        # Match the end of a detailed path entry.
        #
        if {[string match "Total path delay*" $line]} {
            break
        }

        ##
        # Skip the following code if the header was not read yet.
        #
        if {!$headerRead} {
            continue
        }

        ##
        # Extract the instance OID from the report line.
        #
        set iIdx $detailKeys(inst)
        set inst [PathReport:_getOid $db $line $iIdx $detailKeys(inst) {}]

        ##
        # Skip the following code if the instance OID could not be created.
        #
        if {$inst == {}} {
            continue
        }

        ##
        # Get the input pin (inPin) OID.
        #
        set pin [PathReport:_getOid $db $line $iIdx $detailKeys(inPin) $inst]

        ##
        # If the input pin OID was created then add it to the result table.
        #
        if {$pin != {}} {
            set pinLabel [$db oid print $pin]
            set t [$PathReport(table) insert $id end -text $pinLabel]
            $PathReport(table) item $t -values [list $pin]
        }

        ##
        # Get the input pin (outPin) OID.
        #
        set pin [PathReport:_getOid $db $line $iIdx $detailKeys(outPin) $inst]

        ##
        # If the output pin OID was created then add it to the result table.
        # Also store timing information at the output pin.
        #
        if {$pin != {}} {
            set pinLabel [$db oid print $pin]
            set t [$PathReport(table) insert $id end -text $pinLabel]
            set timeVal  [PathReport:getString $line $iIdx $detailKeys(time)]
            set edgeVal  [PathReport:getString $line $iIdx $detailKeys(edge)]
            set delayVal [PathReport:getString $line $iIdx $detailKeys(delay)]
            set slopeVal [PathReport:getString $line $iIdx $detailKeys(slope)]
            set values   [list $pin $timeVal $edgeVal $delayVal $slopeVal]
            $PathReport(table) item $t -values $values
        }
    }
}


# -----------------------------------------------------------------------------
# getString - Extract a string from a report line.
# -----------------------------------------------------------------------------
#
proc PathReport:getString {line instIdx index} {
    ##
    # Return if an invalid index is given.
    #
    if {$index < 0} {
        return {}
    }

    ##
    # If the given index is behind the instance header then increment it.
    #
    if {$index > $instIdx} {
        incr index
    }

    ##
    # Return the requested item.
    #
    return [lindex $line $index]
}


# -----------------------------------------------------------------------------
# _getOid - Extract an OID from a report line.
# -----------------------------------------------------------------------------
#
proc PathReport:_getOid {db line instIdx index inst} {
    global PathReport

    ##
    # Get the item from the report line.
    #
    set string [PathReport:getString $line $instIdx $index]

    ##
    # Return if there is not item at the given index.
    #
    if {$string == {}} {
        return {}
    }

    ##
    # Remove the name of the top module from the string.
    #
    set first [string first "/" $string]
    if {$first > 0} {
        set string [string range $string $first+1 end]
    }

    ##
    # If no instance OID is given then it should be created.
    # Otherwise a pin object will be created.
    #
    if {$inst == {}} {
        set type "inst"
    } else {
        set type "pin"

        ##
        # Remove the pin type from the pin name.
        #
        set last [string last "(" $string]
        if {$last > 0} {
            set string [string range $string 0 $last-1]
        }

        ##
        # Prepend the instance name to the pin name.
        #
        set string "[$db oid oname $inst]/$string"
    }

    ##
    # Try to create an OID.
    #
    set oid {}
    set top $PathReport(top)
    if {[catch {set oid [$db oid createFromString $type $top $string "/"]} e]} {
        zmessage print ERR "Cannot create OID from $string: $e"
    }
    return $oid
}


# -----------------------------------------------------------------------------
# _create - build the GUI to visualize paths.
# -----------------------------------------------------------------------------
#
proc PathReport:_create {w} {
    global PathReport

    ##
    # Use a treeview to show the paths.
    #
    set PathReport(table) [ttk::treeview $w.table \
                                -selectmode browse \
                                -show tree\
                                -yscrollcommand [list $w.scrollbar set]]
    ttk::scrollbar $w.scrollbar -orient vertical -command [list $w.table yview]

    ##
    # Bind a double click to show the result in the Cone window.
    #
    bind $w.table <Double-1> {
        PathReport:_double %W %x %y
        break
    }

    ##
    # Layout the components.
    #
    grid $w.table     -row 0 -column 0 -sticky news
    grid $w.scrollbar -row 0 -column 1 -sticky ns
    grid columnconfigure $w 0 -weight 1
    grid rowconfigure    $w 0 -weight 1
}


# -----------------------------------------------------------------------------
# _double - Display the path in the Cone window.
# -----------------------------------------------------------------------------
#
proc PathReport:_double {w x y} {
    global PathReport

    ##
    # Identify the item under the mouse and return if there is nothing.
    #
    set identification [$w identify $x $y]
    if {$identification == "nothing"} {
        return
    }

    ##
    # Get the item under the mouse.
    #
    set item [$w identify item $x $y]

    ##
    # Get the associated database.
    #
    set db $PathReport(db)

    ##
    # If the clicked item has no parent then it is a root node and represents
    # the entire path else it is a child and represents a pin entry.
    #
    if {[$w parent $item] == {}} {
        ##
        # Clear any existing attributes.
        #
        $db flatattr * deleteAll edge time delay slope

        ##
        # Set display attributes for the timing values.
        #
        set show "Delay=%delay\nTime=%time\nSlope=%slope"
        $db attr -db set @nlv:pin=$show
        $db attr -db set @nlv:pinBus=$show
        $db attr -db set @nlv:port=$show
        $db attr -db set @nlv:portBus=$show
        $db attr -db set @nlv:marks=%edge

        ##
        # Get all OIDs of this path and add the timing information at each pin.
        #
        set oidList {}
        foreach child [$w children $item] {
            set values [$w item $child -values]
            set oid    [lindex $values 0]
            set time   [lindex $values 1]
            set edge   [lindex $values 2]
            set delay  [lindex $values 3]
            set slope  [lindex $values 4]

            if {$edge == "f"} {
                $db flatattr $oid set "edge=ef,+#ff0000"
            }
            if {$edge == "r"} {
                $db flatattr $oid set "edge=er,+#00ff00"
            }

            $db flatattr $oid set "time=$time"
            $db flatattr $oid set "delay=$delay"
            $db flatattr $oid set "slope=$slope"

            lappend oidList $oid
        }

        ##
        # Extend the start and end point of the path by one connected object.
        #
        set first [lindex $oidList 0]
        set last  [lindex $oidList end]
        foreach pin [list $first $last] {
            set oid [PathReport:_extendPath $db $pin]
            if {$oid != {}} {
                lappend oidList $oid
            }
        }

        ##
        # Load the result into the Cone window.
        #
        gui cone load $oidList
        gui cone zoom fullfit
        gui window show Cone
        gui attribute changed
    } else {
        set values  [$w item $item -values]
        set oidList [list [lindex $values 0]]
        gui goto $oidList
    }
}


# -----------------------------------------------------------------------------
# _extendPath - From the given pin oid get one further connected object.
# -----------------------------------------------------------------------------
#
proc PathReport:_extendPath {db oid} {
    if {[$db oid type $oid] != "pin"} {
        return {}
    }
    if {![$db isConnected $oid]} {
        return {}
    }
    set net [$db connectedNet $oid]
    set res {}
    $db foreach pin $net pin {
        if {[$db oid isequal $pin $oid]} {
            continue
        }
        set res $pin
        break
    }
    return $res
}


# =============================================================================
# LoadFile - Show a browse file dialog to open a path report file.
# =============================================================================
#
proc PathReport:LoadFile {{fname ""}} {
    ##
    # If no filename was given then show a file open dialog.
    #
    if {$fname == ""} {
        set fTypes {{"Report" .txt} {"Report" .rpt}}
        set fname [gui window fileDialog openFile "Open Report File" $fTypes]
    }

    ##
    # Return if no filename was selected.
    #
    if {$fname == ""} {
        return
    }

    ##
    # Set the name of the custom widget.
    #
    set name "Path Report"

    ##
    # Destroy an existing custom widgets.
    #
    if {[gui window exists $name]} {
        gui window removeCustomWidget $name
    }

    ##
    # Create the PathReport widget.
    #
    set PathReport(db) {}
    PathReport:_create [gui window insertCustomWidget $name]

    ##
    # Parse the report file.
    #
    PathReport:_parseFile $fname
}


# -----------------------------------------------------------------------------
# Create the menu entry and a database changed hook.
# -----------------------------------------------------------------------------
#
gui menu command {"Userware" "Load Path Report"} {PathReport:LoadFile}
gui database registerChangedCallback "PathReport:Init"
gui settings set "cone:autohide" 0
gui settings changed
PathReport:Init {} $argc $argv