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
###############################################################################
# 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
#       Histogram of W/L
#   @namespace
#       Histogram
#   @section
#       Analyze the Loaded Database
#   @description
#       Create a histogram of all W/L values in the database.
#   @files
#       cust36/histogram.tcl
#   @example
#       demo/spice/gl85.sp
#   @cmdline
#       -hspice @example[0]
#       -userware @files[0]
#   @tag
#       zdb spice
###############################################################################


package require Plotchart


# =============================================================================
# Init - Initialize the plugin.
# =============================================================================
#
proc Histogram:Init {} {
    global Histogram

    ##
    # Set the widget title and path.
    #
    set Histogram(title) "Histogram"
    set Histogram(widget) ""

    ##
    # Add a menu entry.
    #
    gui menu command {"Userware" "Show Histogram"} [list Histogram:Show]
    gui menu command {"Userware" "Save Histogram"} [list Histogram:Save]

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


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

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

    ##
    # Remove an existing design histogram window.
    #
    if {[gui window exists $Histogram(title)]} {
        gui window removeCustomWidget $Histogram(title)
    }
    set Histogram(widget) ""

    ##
    # Remove the callback registration.
    #
    gui database removeChangedCallback "Histogram:DatabaseChanged"

    ##
    # Remove the Histogram array.
    #
    array unset Histogram
}


# =============================================================================
# DatabaseChanged - Callback for the database changed event.
# =============================================================================
#
proc Histogram:DatabaseChanged {db} {
    global Histogram

    set Histogram(db) $db

    set Histogram(initialized) false

    ##
    # Re-calculate the histogram data.
    #
    Histogram:_calculate false

    ##
    # Update an already displayed histogram widget.
    #
    if {[Histogram:_isVisible]} {
        Histogram:Show
    }
}


# =============================================================================
# Save - Save the histogram.
# =============================================================================
#
proc Histogram:Save {{fname ""}} {
    global Histogram

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

    ##
    # Browse for an output file.
    #
    if {$fname eq ""} {
        set fname [gui window fileDialog saveFile "Save Histogram" \
                        {{"Text File" {.txt}}}]
        if {$fname eq ""} {
            return
        }
    }

    ##
    # Force the calculation of the histogram data.
    #
    Histogram:_calculate true

    ##
    # Dump the result to the output file.
    #
    set out [open $fname wb]
    array set _keyTable {W {} L {}}
    foreach {key value} [array get Histogram "histogram:*"] {
        set splitKey [split $key ":"]
        set type     [lindex $splitKey 1]
        set x        [lindex $splitKey 2]
        lappend _keyTable($type) [list $x $value]
    }

    foreach name [array names _keyTable] {
        puts $out "# Distribution of the $name value:"
        foreach key [lsort -index 1 -integer -decreasing $_keyTable($name)] {
            puts $out [format "%12.3g %d" [lindex $key 0] [lindex $key 1]]
        }
    }
    close $out
}


# =============================================================================
# Show - Calculate and show the histogram.
# =============================================================================
#
proc Histogram:Show {} {
    global Histogram

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

    ##
    # Create the histogram widget.
    #
    if {$Histogram(widget) eq ""} {
        Histogram:_createWidget
        update idletasks
    }

    ##
    # Calculate the histogram data.
    #
    Histogram:_calculate false

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


# -----------------------------------------------------------------------------
# _isVisible -
# -----------------------------------------------------------------------------
#
proc Histogram:_isVisible {} {
    global Histogram

    set w $Histogram(widget)

    if {($w eq "") || (![winfo exists $w]) || (![winfo ismapped $w])} {
        return false
    }

    return true
}


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

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

    panedwindow $w.pane -orient horizontal

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

    grid $w.pane.left.image -row 0 -column 0 -sticky news
    grid rowconfigure    $w.pane.left 0 -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 "W"      right \
            0 "Count"  right \
            0 "L"      right \
            0 "Count"  right \
        } \
        -labelcommand tablelist::sortByColumn

    $w.pane.right.table columnconfigure 0 -sortmode real
    $w.pane.right.table columnconfigure 1 -sortmode integer
    $w.pane.right.table columnconfigure 1 -formatcommand Histogram:_formatCell
    $w.pane.right.table columnconfigure 3 -formatcommand Histogram:_formatCell

    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 Histogram:_drawCanvas %W]
    update idletasks
    $w.pane sash place 0 [expr {[winfo width $w] / 2}] 0
}


# -----------------------------------------------------------------------------
# _formatCell -
# -----------------------------------------------------------------------------
#
proc Histogram:_formatCell {value} {
    if {$value eq ""} {
        return $value
    }
    return [zdb formatvalue thousands $value]
}


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

    $w delete 0 end

    set wInfoTable {}
    foreach {key count} [array get Histogram "histogram:W:*"] {
        set splitKey [split $key ":"]
        set value    [lindex $splitKey 2]
        lappend wInfoTable $value $count
    }

    set lInfoTable {}
    foreach {key count} [array get Histogram "histogram:L:*"] {
        set splitKey [split $key ":"]
        set value    [lindex $splitKey 2]
        lappend lInfoTable $value $count
    }
    foreach {wValue wCount} $wInfoTable {lValue lCount} $lInfoTable {
        $w insert end [list $wValue $wCount $lValue $lCount]
    }
}


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

    if {!($Histogram(initialized) && [Histogram:_isVisible])} {
        return
    }

    $w delete all

    ##
    # Create the plot with its x- and y-axes
    #
    set maxY 0

    ##
    # Round the maxY value up to the next nice value.
    #
    set yVal 0
    array set _valTable {}
    foreach {key value} [array get Histogram "histogram:*"] {
        set splitKey [split $key ":"]
        set type [lindex $splitKey 1]
        set x    [lindex $splitKey 2]
        if {$value > $yVal} {
            set yVal $value
        }
        set _valTable($x) 1
    }

    set lst [lsort -real [array names _valTable]]

    set xMin [lindex $lst 0]
    set xMax [lindex $lst end]

    set l [string length $yVal]
    if {$l > 2} {
        incr l -2
    }
    set yVal [expr {int(ceil($yVal/pow(10, $l)) * pow(10, $l))}]
    set step [expr {$yVal / 5}]

    ##
    # Define the X- and Y-Axes.
    #
    set xStep [expr {($xMax - $xMin) / 5.0}]
    set xAxis [list $xMin $xMax $xStep]
    set yAxis [list 1 $yVal $step]

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

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

    $s dataconfig W -style spike -fillcolour green -width 8 -colour green
    $s dataconfig L -style spike -fillcolour red   -width 8 -colour red

    foreach {key value} [array get Histogram "histogram:*"] {
        set splitKey [split $key ":"]
        set type     [lindex $splitKey 1]
        set x        [lindex $splitKey 2]
        $s plot $type $x $value
    }
}


# -----------------------------------------------------------------------------
# _calculate - Calculate the W/L distribution.
# -----------------------------------------------------------------------------
#
proc Histogram:_calculate {force} {
    global Histogram

    ##
    # Do not recalculate the histogram data if force is not true and
    # the 'Histogram' array is already initialized, no database is available or
    # the widget is not visible.
    #
    if {(!$force) &&
        ($Histogram(initialized) || ($Histogram(db) == {}) ||
        (![Histogram:_isVisible]))} \
    {
        return
    }

    set db $Histogram(db)

    ##
    # Initialize the 'Histogram' array.
    #
    set Histogram(initialized) true
    array unset Histogram "histogram:*"

    zprogress begin
    zprogress push "Calculate Histogram" 1.0

    ##
    # Loop over all database cells.
    #
    zprogress push "" 0.5
    $db foreach cell cell {
        ##
        # First clear the used flag.
        #
        $db flag $cell clear blue

        ##
        # Get the function of the cell.
        #
        set func [$db primFuncOf $cell]

        ##
        # Flag all mos functions
        #
        switch $func {
            "NMOS" -
            "PMOS" -
            "NMOSM" -
            "PMOSM" {
                $db flag $cell set blue
            }
            default {
            }
        }
    }
    zprogress pop

    ##
    # Traverse the design hierarchy starting from all top modules to find
    # flagged MOS cells.
    #
    zprogress push "" 1.0
    $db foreach top top {
        $db flat foreach instOfCell blue $top inst {
            set w [$db flatattr $inst getMergedValue "w"]
            set l [$db flatattr $inst getMergedValue "l"]
            if {$w == {} || $l == {}} {
                continue
            }
            set w [zdb formatvalue fromspice $w]
            set l [zdb formatvalue fromspice $l]

            set w [format "%g" $w]
            set l [format "%g" $l]

            if {![info exists Histogram(histogram:W:$w)]} {
                set Histogram(histogram:W:$w) 0
            }
            incr Histogram(histogram:W:$w)

            if {![info exists Histogram(histogram:L:$l)]} {
                set Histogram(histogram:L:$l) 0
            }
            incr Histogram(histogram:L:$l)
        }
    }
    zprogress pop
    zprogress end
}


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