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
###############################################################################
# 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.
# =============================================================================
#   @plugin
#       Design Histogram
#   @namespace
#       DesignHistogram
#   @section
#       Create Reports
#   @description
#       Display a "Design Histogram" of the loaded design database.
#   @files
#       cust27/designHistogram.tcl
#   @example
#       demo/spice/aquarius.sp
#   @cmdline
#       -hspice @example[0]
#       -userware @files[0]
#   @tag
#       zdb gui spice
###############################################################################


package require Plotchart


# =============================================================================
# Init - Initialize the plugin.
# =============================================================================
#
proc DesignHistogram:Init {} {
    ##
    # Set the widget title.
    #
    set title "Design Histogram"

    ##
    # Add a menu entry.
    #
    gui menu command {"Userware" "Show Design Histogram"} \
        [list DesignHistogram:Show $title]

    ##
    # Register the Show procedure to be called if the plugin is activated and
    # whenever the database is changed.
    #
    gui database runAndRegisterChangedCallback \
        [list DesignHistogram:Show $title]
}


# =============================================================================
# Finit - Finalize the plugin.
# =============================================================================
#
proc DesignHistogram:Finit {} {
    global DesignHistogram

    ##
    # Undo modifications of the GUI.
    #
    gui menu removeEntry {"Userware" "Show Design Histogram"}

    ##
    # Remove an existing design histogram window.
    #
    if {[gui window exists "Design Histogram"]} {
        gui window removeCustomWidget "Design Histogram"
    }

    ##
    # Remove the callback registration.
    #
    gui database removeChangedCallback DesignHistogram:Show

    ##
    # Remove the DesignHistogram array.
    #
    array unset DesignHistogram
}


# =============================================================================
# Show - Calculate and show the design histogram.
# =============================================================================
#
proc DesignHistogram:Show {title {db {}}} {
    global DesignHistogram

    ##
    # Initialize the 'DesignHistogram' array.
    #
    array unset DesignHistogram "histogram:*"
    set DesignHistogram(histogram:levels) {}

    if {![gui window exists $title]} {
        set w [DesignHistogram:_createWidget $title]
    } else {
        set w [gui window path $title]
    }

    ##
    # If a database is set then set the 'DesignHistogram' array.
    #
    set db [gui database get]
    if {$db != {}} {
        zprogress begin
        zprogress push "Calculate Design Histogram" 1.0
        array set DesignHistogram [$db report histogram]
        zprogress pop
        zprogress end
    }

    ##
    # Display the 'Design Histogram' data as table and a plot.
    #
    DesignHistogram:_fillTable  $w.pane.right.table
    DesignHistogram:_drawCanvas $w.pane.left.image
}


# -----------------------------------------------------------------------------
# _createWidget -
# -----------------------------------------------------------------------------
#
proc DesignHistogram:_createWidget {title} {
    global DesignHistogram

    set w [gui window insertCustomWidget \
            -pluginNamespace "DesignHistogram" $title]

    panedwindow $w.pane -orient horizontal

    ##
    # Left side widget.
    #
    ttk::frame $w.pane.left
    ttk::frame $w.pane.left.toolbar
    canvas $w.pane.left.image  -background white

    set DesignHistogram(config:logyaxis) 1
    pack [ttk::checkbutton $w.pane.left.toolbar.logyaxis \
        -text "Logarithmic Y-Axis" \
        -variable DesignHistogram(config:logyaxis) \
        -command [list DesignHistogram:_drawCanvas $w.pane.left.image]
    ] -side left -padx 10 -pady 2
    pack [ttk::separator $w.pane.left.toolbar.sep -orient vertical] \
        -side left -pady 2 -padx 10 -fill y
    pack [ttk::label $w.pane.left.toolbar.show -text "Show:"] \
        -side left -pady 2 -padx 2

    foreach {
        id              txt
    } {
        cellPlacement   "Cells"
        devicePlacement "Devices"
        netCount        "Nets"
        pinCount        "Pins"
        portCount       "Ports"
    } {
        set DesignHistogram(config:$id) 1
        pack [ttk::checkbutton $w.pane.left.toolbar.$id \
            -text $txt \
            -variable DesignHistogram(config:$id) \
            -command [list DesignHistogram:_drawCanvas $w.pane.left.image]
        ] \
        -side left \
        -padx 10 \
        -pady 2
    }

    grid $w.pane.left.toolbar  -row 0 -column 0 -sticky news
    grid $w.pane.left.image    -row 1 -column 0 -sticky news
    grid rowconfigure    $w.pane.left 1 -weight 1
    grid columnconfigure $w.pane.left 0 -weight 1

    ##
    # Right side widget.
    #
    ttk::frame $w.pane.right
    ttk::scrollbar $w.pane.right.x \
        -orient  horizontal \
        -command [list $w.pane.right.table xview]
    ttk::scrollbar $w.pane.right.y \
        -orient  vertical \
        -command [list $w.pane.right.table yview]
    tablelist::tablelist $w.pane.right.table \
        -stripebackground #f0f0f0 \
        -stretch all \
        -xscrollcommand [list $w.pane.right.x set] \
        -yscrollcommand [list $w.pane.right.y set] \
        -columns { \
            0 "Level"             right \
            0 "Cell Count"        right \
            0 "Cell Placements"   right \
            0 "Device Count"      right \
            0 "Device Placements" right \
            0 "Net Count"         right \
            0 "Pin Count"         right \
            0 "Port Count"        right \
        }

    grid $w.pane.right.table -row 0 -column 0 -sticky news
    grid $w.pane.right.x     -row 1 -column 0 -sticky we
    grid $w.pane.right.y     -row 0 -column 1 -sticky ns
    grid rowconfigure    $w.pane.right 0 -weight 1
    grid columnconfigure $w.pane.right 0 -weight 1

    $w.pane add $w.pane.left -stretch always
    $w.pane add $w.pane.right -stretch always

    grid $w.pane -row 0 -column 0 -sticky news
    grid rowconfigure    $w 0 -weight 1
    grid columnconfigure $w 0 -weight 1

    bind $w.pane.left.image <Configure> [list DesignHistogram:_drawCanvas %W]
    update idletasks
    $w.pane sash place 0 [expr {[winfo width $w] / 2}] 0

    return $w
}


# -----------------------------------------------------------------------------
# _fillTable -
# -----------------------------------------------------------------------------
#
proc DesignHistogram:_fillTable {w} {
    global DesignHistogram

    $w delete 0 end

    foreach level $DesignHistogram(histogram:levels) {
        set levelInfo {}
        if {$level < 0} {
            lappend levelInfo "total"
        } else {
            lappend levelInfo $level
        }
        foreach type {
            cellCount
            cellPlacement
            deviceCount
            devicePlacement
            netCount
            pinCount
            portCount
        } {
            lappend levelInfo [zdb formatvalue thousands \
                                $DesignHistogram(histogram:$level:$type)]
        }
        $w insert end $levelInfo
    }
}


# -----------------------------------------------------------------------------
# _showAnnotation - Showing an annotation on the design histogram.
# -----------------------------------------------------------------------------
#
proc DesignHistogram:_showAnnotation {x y plot c txt} {
    DesignHistogram:_removeAnnotation $c

    set    balloonText "Level [expr {int($x)}]: "
    append balloonText "[zdb formatvalue thousands [expr {int($y)}]] $txt"
    $plot balloon $x $y $balloonText south

    after 2000 [list DesignHistogram:_removeAnnotation $c]
}


# -----------------------------------------------------------------------------
# _removeAnnotation - Erase an annotation on the design histogram.
# -----------------------------------------------------------------------------
#
proc DesignHistogram:_removeAnnotation {c} {
    $c delete BalloonText
    $c delete BalloonFrame
}


# -----------------------------------------------------------------------------
# _drawCanvas -
# -----------------------------------------------------------------------------
#
proc DesignHistogram:_drawCanvas {w} {
    global DesignHistogram

    ##
    # Remember the coordinates of the legend object.
    #
    if {![info exists DesignHistogram(coords:legend)]} {
        set DesignHistogram(coords:legend) {}
    }
    set coords [$w coords legendobj]
    if {($coords == {}) && ($DesignHistogram(coords:legend) != {})} {
        set coords $DesignHistogram(coords:legend)
    }
    set DesignHistogram(coords:legend) $coords

    $w delete all

    if {$DesignHistogram(histogram:levels) == {}} {
        return
    }

    ##
    # Create the plot with its x- and y-axes
    #
    set maxY 0
    foreach {key value} [array get DesignHistogram histogram:*:*] {
        if {[string match "histogram:-1:*" $key]} {
            continue
        }

        set pos [string last ":" $key]
        set type [string range $key [incr pos] end]

        if {[info exists DesignHistogram(config:$type)] &&
            $DesignHistogram(config:$type)} \
        {
            if {$value > $maxY} {
                set maxY $value
            }
        }
    }

    ##
    # Round the maxY value up to the next nice value.
    #
    set len [string length $maxY]
    if {$len > 2} {
        set first [string index $maxY 0]
        incr len -1

        if {$DesignHistogram(config:logyaxis)} {
            set yVal [incr first 3][string repeat 0 $len]
        } else {
            set lower     ${first}[string repeat 0 $len]
            set upper [incr first][string repeat 0 $len]

            set half [expr {($upper - $lower) / 2}]
            if {($upper - $maxY) > $half} {
                set yVal [expr {$lower + $half}]
            } else {
                set yVal $upper
            }
            set step [expr {$yVal / $first}]
            if {$first < 6} {
                set step [expr {$step / 2}]
            }
        }
    } else {
        set yVal $maxY
        if {$len == 1} {
            set step  1
        } else {
            set step 10
        }
    }

    ##
    # Define the X- and Y-Axes.
    #
    set xAxis [list 0 [lindex $DesignHistogram(histogram:levels) end].2 1]
    set yAxis [list 1 $yVal]

    if {$DesignHistogram(config:logyaxis)} {
        set plotType createXLogYPlot
    } else {
        set plotType createXYPlot
        lappend yAxis $step
    }

    set s [::Plotchart::$plotType $w $xAxis $yAxis]

    ##
    # Add axis labels.
    #
    $s xtext "Level"
    $s ytext "Count"

    ##
    # Add legend information.
    #
    $s legendconfig -position top-left -legendtype line

    ##
    # Plot the data.
    #
    foreach level $DesignHistogram(histogram:levels) {
        foreach {
            type            legendText   color
        } {
            cellPlacement   "Cells"      red
            devicePlacement "Devices"    green
            netCount        "Nets"       blue
            pinCount        "Pins"       orange
            portCount       "Ports"      magenta
        } {
            if {!$DesignHistogram(config:$type)} {
                continue
            }

            if {$level < 0} {
                $s dataconfig $type -color $color
                $s legend $type $legendText 20
            } else {
                set value $DesignHistogram(histogram:$level:$type)
                if {$value == 0} {
                    set value 1
                }
            }
            $s plot $type $level $value

            $s bindlast $type <Enter> \
                [list DesignHistogram:_showAnnotation $s %W $legendText]
            $s dataconfig $type -type both -symbol dot
        }
    }

    ##
    # Move the legend to the previous position.
    #
    set coords [$w coords legendobj]
    if {($DesignHistogram(coords:legend) != {}) && ($coords != {})} {
        $w move legendobj \
            [expr {[lindex $DesignHistogram(coords:legend) 0] - \
                   [lindex $coords 0]}] \
            [expr {[lindex $DesignHistogram(coords:legend) 1] - \
                   [lindex $coords 1]}]
    }
}


# =============================================================================
# Call the initialization procedure.
# =============================================================================
#
DesignHistogram:Init