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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
###############################################################################
# Copyright (c) 2020-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.
# =============================================================================
#   @plugin
#       Read ERC Analyzer Report
#   @namespace
#       AnalyzerReport
#   @section
#       Read Side Files and Annotate Data
#   @description
#       Parse a AnalyzerReport report file and display all paths in a custom
#       widget.
#   @files
#       cust31/analyzerReport.tcl
#   @tag
#       spice gui
###############################################################################


# =============================================================================
# Init - Initialize the plugin.
# =============================================================================
#
proc AnalyzerReport:Init {argc argv} {
    ##
    # Add menu entries.
    #
    gui menu command {"Analyzer Report" "Load Report"} \
        [list AnalyzerReport:_loadReport]
    gui menu command {"Analyzer Report" "Close Tab"} \
        [list AnalyzerReport:_closeWidget]
    gui menu customizeEntry {"Analyzer Report" "Close Tab"} \
        [list AnalyzerReport:_customizeCloseMenu]
    gui popup customize AnalyzerReport:_customizePopup

    ##
    # Initialize global values.
    #
    AnalyzerReport:_initialize

    ##
    # Load a report given on the command line.
    #
    if {$argc > 0} {
        AnalyzerReport:_loadReport [lindex $argv 0]
    }
}


# =============================================================================
# Finit - Finalize the plugin.
# =============================================================================
#
proc AnalyzerReport:Finit {} {
    ##
    # Undo modifications of the GUI.
    #
    gui menu removeEntry {"Analyzer Report"}
    gui popup removeCustomize "AnalyzerReport:_customizePopup"

    ##
    # Close tab.
    #
    AnalyzerReport:_closeWidget
}


# -----------------------------------------------------------------------------
# _loadReport -
# -----------------------------------------------------------------------------
#
proc AnalyzerReport:_loadReport {{fname ""}} {
    if {$fname eq ""} {
        set fname [gui window fileDialog openFile \
            "Open an ERC Analyzer Report File"    \
            {{"Analyzer Report" {"*.rpt"}}}]
    }
    if {$fname eq ""} {
        return
    }

    ##
    # Close an existing widget, parse the report and create the report widget.
    #
    AnalyzerReport:_closeWidget
    AnalyzerReport:_parseReport $fname
    AnalyzerReport:_selectColumns
    AnalyzerReport:_createWidget
    AnalyzerReport:_fillTable
}


# -----------------------------------------------------------------------------
# _selectColumns -
# -----------------------------------------------------------------------------
#
proc AnalyzerReport:_selectColumns {} {
    global AnalyzerReport

    set typeCount      [llength $AnalyzerReport(colType)]
    set nameCount      [llength $AnalyzerReport(columns)]
    if {$typeCount != $nameCount} {
        zmessage print WAR \
            "ColumnTypes != ColumnNames ($typeCount != $nameCount)."
    }
    set newTypes      {}
    set newColumns    {}
    set newViolations {}
    set keep          {}
    for {set i 0} {$i < $typeCount} {incr i} {
        set type [lindex $AnalyzerReport(colType) $i]
        set name [lindex $AnalyzerReport(columns) $i]
        switch -exact -- $name {
            "Category" -
            "IDhash"   {
                continue
            }
            default {
                lappend newTypes      $type
                lappend newColumns    $name
                lappend keep          $i
            }
        }
    }
    foreach violation $AnalyzerReport(violations) {
        set newViolation {}
        foreach i $keep {
            lappend newViolation [lindex $violation $i]
        }
        lappend newViolations $newViolation
    }

    set AnalyzerReport(colType)    $newTypes
    set AnalyzerReport(columns)    $newColumns
    set AnalyzerReport(violations) $newViolations
}


# -----------------------------------------------------------------------------
# _createWidget -
# -----------------------------------------------------------------------------
#
proc AnalyzerReport:_createWidget {} {
    global AnalyzerReport

    if {$AnalyzerReport(title) eq ""} {
        set AnalyzerReport(title) "Analyzer Report"
    }

    set w [gui window insertCustomWidget $AnalyzerReport(title) \
                -pluginNamespace "AnalyzerReport"]
    set AnalyzerReport(widget) $w

    ttk::frame $w.toolbar
    ttk::button $w.toolbar.clearHiLi \
        -text     "Clear Highlight"  \
        -command  [list AnalyzerReport:_clearHighlight {} true]
    ttk::checkbutton $w.toolbar.appendHiLi \
        -text     "Append Highlight" \
        -variable AnalyzerReport(appendHiLi)
    ttk::separator $w.toolbar.sep1 -orient vertical
    ttk::checkbutton $w.toolbar.autoUnfoldInst \
        -text     "Auto Unfold Inst" \
        -variable AnalyzerReport(autoUnfoldInst)
    ttk::checkbutton $w.toolbar.autoUnfoldNet \
        -text     "Auto Unfold Net" \
        -variable AnalyzerReport(autoUnfoldNet)
    grid $w.toolbar.clearHiLi      -row 0 -column 0 -sticky w  -padx 5 -pady 5
    grid $w.toolbar.appendHiLi     -row 0 -column 1 -sticky w  -padx 5 -pady 5
    grid $w.toolbar.sep1           -row 0 -column 2 -sticky ns -padx 5 -pady 2
    grid $w.toolbar.autoUnfoldInst -row 0 -column 3 -sticky w  -padx 5 -pady 5
    grid $w.toolbar.autoUnfoldNet  -row 0 -column 4 -sticky w  -padx 5 -pady 5

    ttk::frame $w.f
    ttk::scrollbar $w.f.x -orient  horizontal -command [list $w.f.list xview]
    ttk::scrollbar $w.f.y -orient  vertical   -command [list $w.f.list yview]
    ttk::treeview $w.f.list \
        -columns        [lrange $AnalyzerReport(columns) 0 end-1] \
        -xscrollcommand [list $w.f.x set] \
        -yscrollcommand [list $w.f.y set]
    $w.f.list tag configure "MONOSPACE_FONT" -font TkFixedFont

    grid $w.f.list -row 0 -column 0 -sticky news
    grid $w.f.x    -row 1 -column 0 -sticky we
    grid $w.f.y    -row 0 -column 1 -sticky ns
    grid rowconfigure    $w.f 0 -weight 1
    grid columnconfigure $w.f 0 -weight 1
    bind $w.f.list <Button-1> [list AnalyzerReport:_clickTable $w %x %y]

    grid $w.toolbar -row 0 -column 0 -sticky we
    grid $w.f       -row 1 -column 0 -sticky news
    grid rowconfigure    $w 1 -weight 1
    grid columnconfigure $w 0 -weight 1
}


# -----------------------------------------------------------------------------
# _fillTable -
# -----------------------------------------------------------------------------
#
proc AnalyzerReport:_fillTable {} {
    global AnalyzerReport

    set w $AnalyzerReport(widget)

    set maxCol [llength $AnalyzerReport(columns)]
    incr maxCol -1
    for {set col 0} {$col < $maxCol} {incr col} {
        $w.f.list heading #$col \
            -text [lindex $AnalyzerReport(columns) $col] \
            -command [list AnalyzerReport:_reorderTable $w $col]
        $w.f.list column #$col -width 150 -stretch 0
    }
    $w.f.list column #0 -width 100
    $w.f.list column #4 -stretch 1

    AnalyzerReport:_reorderTable $w ""
}


# -----------------------------------------------------------------------------
# _customizeCloseMenu - Disable the 'Close Tab' menu entry if the tab is
#                       already closed.
# -----------------------------------------------------------------------------
#
proc AnalyzerReport:_customizeCloseMenu {menuId} {
    global AnalyzerReport

    set state "disabled"
    if {[info exists AnalyzerReport(widget)]} {
        set state "normal"
    }
    $menuId entryconfigure end -state $state
}


# -----------------------------------------------------------------------------
# _closeWidget - Destroy the Analyzer Report tab.
# -----------------------------------------------------------------------------
#
proc AnalyzerReport:_closeWidget {} {
    global AnalyzerReport

    if {[info exists AnalyzerReport(widget)]} {
        gui window removeCustomWidget $AnalyzerReport(title)
        destroy $AnalyzerReport(widget)
        array unset AnalyzerReport widget
    }
    AnalyzerReport:_initialize
}


# -----------------------------------------------------------------------------
# _initialize - Initialize the array keys.
# -----------------------------------------------------------------------------
#
proc AnalyzerReport:_initialize {} {
    global AnalyzerReport

    set AnalyzerReport(title)          ""
    set AnalyzerReport(topCell)        ""
    set AnalyzerReport(columns)        {}
    set AnalyzerReport(violations)     {}
    set AnalyzerReport(appendHiLi)      1
    set AnalyzerReport(autoUnfoldInst)  1
    set AnalyzerReport(autoUnfoldNet)   0
}


# -----------------------------------------------------------------------------
# _commentValue -
# -----------------------------------------------------------------------------
#
proc AnalyzerReport:_commentValue {line} {
    set equal [string first "=" $line]
    incr equal
    return [string range $line $equal end]
}


# -----------------------------------------------------------------------------
# _parseReport -
# -----------------------------------------------------------------------------
#
proc AnalyzerReport:_parseReport {fname} {
    global AnalyzerReport

    set AnalyzerReport(violations) {}

    set in [open $fname rb]
    while {![eof $in]} {
        set line [string trim [gets $in]]
        switch -glob -- $line {
            "# ReportTitle=*" {
                set AnalyzerReport(title) [AnalyzerReport:_commentValue $line]
            }
            "# TopCell=*" {
                set AnalyzerReport(topCell) [AnalyzerReport:_commentValue $line]
            }
            "# ColumnTypes=*" {
                set columns [AnalyzerReport:_commentValue $line]
                set AnalyzerReport(colType) [lrange [split $columns "\t"] 1 end]
            }
            "# ColumnNames=*" {
                set columns [AnalyzerReport:_commentValue $line]
                set AnalyzerReport(columns) [lrange [split $columns "\t"] 1 end]
            }
            "# *" -
            "*<INFO>*Summary*" -
            "" {
                continue
            }
            default {
                set fields [split $line "\t"]
                set count  [llength $fields]
                set colCnt [llength $AnalyzerReport(columns)]
                if {$count != $colCnt} {
                    set    msg "Unexpected number of fields "
                    append msg "($count != $colCnt ColumnNames)"
                    zmessage print WAR $msg
                }
                lappend AnalyzerReport(violations) $fields
            }
        }
    }
    close $in
}


# -----------------------------------------------------------------------------
# _findNet -
# -----------------------------------------------------------------------------
#
proc AnalyzerReport:_findNet {db cellOid obj} {
    set netName [lindex [split $obj "-"] end]
    set len [string length $netName]
    incr len
    set obj [string range $obj 0 end-$len]

    foreach item [split $obj "/"] {
        if {$item == {}} {
            continue
        }
        set item [string map {~ /} $item]
        set instOid [$db search inst $cellOid $item]
        if {[$db oid isnull $instOid]} {
            set cellOid [$db oid create null]
            break
        }
        set cellOid [$db down $instOid]
    }
    set netOid [$db oid create null]
    if {![$db oid isnull $cellOid]} {
        set netOid [$db search net $cellOid $netName]
        if {[$db oid isnull $netOid]} {
            return $netOid
        }
    }
    return $netOid
}


# -----------------------------------------------------------------------------
# _findInst -
# -----------------------------------------------------------------------------
#
proc AnalyzerReport:_findInst {db cellOid obj} {
    set instOid [$db oid create null]
    foreach item [split $obj "/"] {
        if {$item == {}} {
            continue
        }
        set item [string map {~ /} $item]
        set instOid [$db search inst $cellOid $item]
        if {[$db oid isnull $instOid]} {
            break
        }
        set cellOid [$db down $instOid]
    }
    return $instOid
}


# -----------------------------------------------------------------------------
# _reorderTable -
# -----------------------------------------------------------------------------
#
proc AnalyzerReport:_reorderTable {w col} {
    global AnalyzerReport

    set up   [gui settings get "texticon:triangleUp"]
    set down [gui settings get "texticon:triangleDown"]

    if {$col ne ""} {
        set heading [$w.f.list heading #$col -text]
        if {[string match "*$down" $heading]} {
            set order "decreasing"
            set icon $up
        } else {
            set order "increasing"
            set icon $down
        }

        set cid -1
        foreach colTxt [lrange $AnalyzerReport(columns) 0 end-1] {
            $w.f.list heading #[incr cid] -text $colTxt
        }
        set title [lindex $AnalyzerReport(columns) $col]
        $w.f.list heading #$col -text "$title $icon"
        set violations [lsort -index $col -$order $AnalyzerReport(violations)]
    } else {
        set violations $AnalyzerReport(violations)
    }

    $w.f.list delete [$w.f.list children {}]

    foreach violation $violations {
        set id [$w.f.list insert {} end \
            -text   [lindex $violation 0] \
            -values [lrange $violation 1 end-1] \
        ]
        set description [lindex $violation end]
        if {[string match "`*" $description]} {
            foreach sub [split $description "`"] {
                if {[string trim $sub] eq ""} {
                    continue
                }
                set inst {}
                set net  {}
                switch -glob -- $sub {
                    "*/*-* ->*Net*" -
                    "/*-* (*)" {
                        set net [lindex $sub 0]
                    }
                    "/* -> *" -
                    "/* : *" {
                        set inst [lindex $sub 0]
                    }
                    "/*-*" {
                        set net $sub
                    }
                    "/*" {
                        set inst $sub
                    }
                    default {}
                }
                $w.f.list insert $id end \
                    -text   "" \
                    -tags   [list MONOSPACE_FONT] \
                    -values [list {} $net $inst $sub]
            }
        }
    }
}


# -----------------------------------------------------------------------------
# _clickTable -
# -----------------------------------------------------------------------------
#
proc AnalyzerReport:_clickTable {w x y} {
    global AnalyzerReport

    set db [gui database get]
    if {$db == {}} {
        return
    }
    set item   [$w.f.list identify item $x $y]
    set values [$w.f.list item $item -values]
    set top    [$db get_top_design]

    gui window show Schem

    switch -exact -- [$w.f.list identify region $x $y] {
        "tree" -
        "cell" {
        }
        default {
            return
        }
    }

    set col  [string trimleft [$w.f.list identify column $x $y] "#"]
    set type [lindex $AnalyzerReport(colType) $col]
    set gotoList      {}
    set highlightList {}
    switch -exact -- $type {
        "instance" {
            set pos  [lsearch -exact $AnalyzerReport(colType) "instance"]
            set inst [lindex $values [expr {$pos - 1}]]
            AnalyzerReport:_showInst $db $top $inst gotoList highlightList
        }
        "net" {
            set pos  [lsearch -exact $AnalyzerReport(colType) "net"]
            set net  [lindex $values [expr {$pos - 1}]]
            AnalyzerReport:_showNet $db $top $net gotoList highlightList
        }
        "cell" {
            set pos  [lsearch -exact $AnalyzerReport(colType) "cell"]
            set cell [lindex $values [expr {$pos - 1}]]
            AnalyzerReport:_showBlock $db $top $cell gotoList highlightList
            gui schem setCurrentModule [lindex $highlightList end]
            gui tree  setCurrentModule [lindex $highlightList end]
        }
        default {
        }
    }

    if {$AnalyzerReport(autoUnfoldInst) || $AnalyzerReport(autoUnfoldNet)} {
        set currentModule [gui schem getCurrentModule]
        set instList {}
        foreach oid [concat $gotoList $highlightList] {
            set type [$db oid type $oid]
            if {($type eq "inst") && (!$AnalyzerReport(autoUnfoldInst))} {
                continue
            }
            if {($type eq "net") && (!$AnalyzerReport(autoUnfoldNet))} {
                continue
            }
            set instName [lindex [$db oid path $oid] 0]
            set inst [$db search inst $currentModule $instName]
            if {![$db oid isnull $inst]} {
                lappend instList $inst
            }
        }
        gui schem unfold $instList
    }

    gui schem zoom 1
    gui goto $gotoList

    if {!$AnalyzerReport(appendHiLi)} {
        AnalyzerReport:_clearHighlight $db false
    }
    foreach oid $highlightList {
        $db flathilight $oid set 0
    }
    gui highlight changed
}


# -----------------------------------------------------------------------------
# _clearHighlight -
# -----------------------------------------------------------------------------
#
proc AnalyzerReport:_clearHighlight {db callUpdate} {
    if {$db == {}} {
        set db [gui database get]
        if {$db == {}} {
            return
        }
    }

    $db flathilight * deleteAll

    if {$callUpdate} {
        gui highlight changed
    }
}


# -----------------------------------------------------------------------------
# _showNet -
# -----------------------------------------------------------------------------
#
proc AnalyzerReport:_showNet {db top net gotoListName highlightListName} {
    upvar 1 $gotoListName      gotoList
    upvar 1 $highlightListName highlightList

    set netOid [AnalyzerReport:_findNet $db $top $net]
    if {[$db oid isnull $netOid]} {
        return
    }

    lappend gotoList $netOid

    set signal [$db oid convertTo signal $netOid]
    $db flat foreach net $signal net {
        lappend highlightList $net
    }
}


# -----------------------------------------------------------------------------
# _showInst -
# -----------------------------------------------------------------------------
#
proc AnalyzerReport:_showInst {db top inst gotoListName highlightListName} {
    upvar 1 $gotoListName      gotoList
    upvar 1 $highlightListName highlightList

    set instOid [AnalyzerReport:_findInst $db $top $inst]
    if {[$db oid isnull $instOid]} {
        return
    }

    lappend gotoList      $instOid
    lappend highlightList $instOid
}


# -----------------------------------------------------------------------------
# _showBlock -
# -----------------------------------------------------------------------------
#
proc AnalyzerReport:_showBlock {db top block gotoListName highlightListName} {
    upvar 1 $gotoListName      gotoList
    upvar 1 $highlightListName highlightList

    set cellOid [$db search cell $block]

    if {[$db oid isnull $cellOid]} {
        return
    }

    set cellOid [$db oid searchTreeBased $cellOid]
    lappend highlightList $cellOid
}


# -----------------------------------------------------------------------------
# _unfoldInstOfCell -
# -----------------------------------------------------------------------------
#
proc AnalyzerReport:_unfoldInstOfCell {db module name} {
    set instList {}
    $db foreach inst $module inst {
        if {$name eq [$db oid cname $inst]} {
            lappend instList $inst
        }
    }
    gui schem unfold $instList
}


# -----------------------------------------------------------------------------
# _customizePopup -
# -----------------------------------------------------------------------------
#
proc AnalyzerReport:_customizePopup {menu oidList} {
    set db [gui database get]

    set instList {}
    array set _cnameTable {}
    foreach oid $oidList {
        if {([$db oid type $oid] ne "inst") || (![$db isModule $oid])} {
            continue
        }
        set cellName [$db oid cname $oid]
        if {![info exists _cnameTable($cellName)]} {
            set _cnameTable($cellName) [$db parentModule $oid]
        }
        lappend instList $oid
    }

    $menu add separator
    set subMenu [menu $menu.customFold]
    $menu add cascade -label "Custom Folding" -menu $subMenu

    if {[llength $instList] == 0} {
        set state disabled
    } else {
        set state normal
    }
    $subMenu add command \
        -label   "Unfold Selected Instances" \
        -state   $state \
        -command [list gui schem unfold $instList]

    foreach {name parent} [array get _cnameTable] {
        $subMenu add command \
            -label   "Unfold all Instances of Cell: '$name'" \
            -command [list AnalyzerReport:_unfoldInstOfCell $db $parent $name]
    }

    $subMenu add separator
    $subMenu add command \
        -label   "Fold Selected Instances" \
        -state   $state \
        -command [list gui schem fold $instList]

    $subMenu add command \
        -label   "Fold All Instances" \
        -command [list AnalyzerReport:_foldAll]
}


# -----------------------------------------------------------------------------
# _foldAll -
# -----------------------------------------------------------------------------
#
proc AnalyzerReport:_foldAll {} {
    set db [gui database get]
    set instList {}
    foreach oid [gui schem contents -type "inst"] {
        if {[$db isModule $oid]} {
            lappend instList $oid
        }
    }
    gui schem fold $instList
}


# =============================================================================
# Call the initialization procedure.
# =============================================================================
#
AnalyzerReport:Init $argc $argv