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
###############################################################################
# Copyright (c) 2021-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
#       Merge Tab.
###############################################################################


# =============================================================================
# CreateMergeTab -
# =============================================================================
#
proc ::ManipulateDesign::CreateMergeTab {
    w
} {
    variable _MergeTab
    variable _MergeTabRestrictToCone

    set _MergeTab [ttk::frame $w]
    set _MergeTabRestrictToCone 1

    _mergeTabResetSelection

    ttk::frame $w.top
    ttk::checkbutton $w.top.restrict_to_cone \
        -text "Restrict to Cone" \
        -variable ::ManipulateDesign::_MergeTabRestrictToCone \
        -command "::ManipulateDesign::_mergeTabUpdate $w"
    ttk::button $w.top.update \
        -text "Update" \
        -command "::ManipulateDesign::_mergeTabUpdate $w"
    ttk::button $w.top.merge_all \
        -text "Merge All" \
        -command "::ManipulateDesign::_mergeTabMerge $w 1"
    ttk::button $w.top.merge \
        -text "Merge Selected Group(s)" \
        -command "::ManipulateDesign::_mergeTabMerge $w 0"
    ttk::label $w.top.selection \
        -textvariable ::ManipulateDesign::_MergeTabSelectionLabel
    grid $w.top.restrict_to_cone -column 0 -row 0 -stick news -padx 4 -pady 4
    grid $w.top.update           -column 1 -row 0 -stick news -padx 4 -pady 4
    grid $w.top.merge_all        -column 2 -row 0 -stick news -padx 4 -pady 4
    grid $w.top.merge            -column 3 -row 0 -stick news -padx 4 -pady 4
    grid $w.top.selection        -column 4 -row 0 -stick news -padx 4 -pady 4
    grid rowconfigure    $w.top 0 -weight 1
    grid columnconfigure $w.top 4 -weight 1

    set middle [ttk::frame $w.middle]
    ttk::treeview $middle.tree \
        -selectmode extended \
        -yscrollcommand "$middle.y set"
    $middle.tree heading #0 \
        -text "Parallel/Serial Candidates" \
        -anchor w
    bind $w.middle.tree <Double-1> \
        "::ManipulateDesign::_mergeTabDouble $w %x %y; break"
    bind $w.middle.tree <<TreeviewSelect>> \
        "::ManipulateDesign::_mergeTabSelect $w"
    ttk::scrollbar $middle.y \
        -orient vertical \
        -command "$middle.tree yview"
    grid $middle.tree -column 0 -row 0 -stick news -padx {4 0} -pady 4
    grid $middle.y -column 1 -row 0 -stick news -padx {0 4} -pady 4
    grid rowconfigure $middle 0 -weight 1
    grid columnconfigure $middle 0 -weight 1

    ttk::frame $w.bottom

    grid $w.top -row 0 -column 0 -sticky news
    grid $w.middle -row 1 -column 0 -sticky news
    grid $w.bottom -row 2 -column 0 -sticky news
    grid rowconfigure $w 1 -weight 1
    grid columnconfigure $w 0 -weight 1
    return $w
}


# =============================================================================
# MergeTabReset -
# =============================================================================
#
proc ::ManipulateDesign::MergeTabReset {} {
    variable _MergeTab

    if {[winfo exists $_MergeTab]} {
        _mergeTabResetSelection
        $_MergeTab.middle.tree delete [$_MergeTab.middle.tree children {}]
    }
}


# -----------------------------------------------------------------------------
# _mergeTabUpdate -
# -----------------------------------------------------------------------------
#
proc ::ManipulateDesign::_mergeTabUpdate {
    w
} {
    variable _MergeTabRestrictToCone
    variable _ReplaceMap

    MergeTabReset

    set db [gui database get]
    if {$db == {}} {
        return
    }

    if {[dict size $_ReplaceMap] == 0} {
        return
    }

    ##
    # Collect cells whose instances are visible in at least one cone window.
    #
    set in_cone [dict create]
    if {$_MergeTabRestrictToCone} {
        ::WindowManager::Foreach Cone cone {
            foreach oid [gui cone contents -window $cone -type "inst"] {
                set parent [$db parentModule $oid]
                set parent_name [$db oid oname $parent]
                set inst_name [$db oid oname $oid]
                dict set in_cone $parent_name $inst_name 1
            }
        }
    }

    set index 1
    set hiersep [$db hiersep]
    dict for {orig_cell_name cell_name} $_ReplaceMap {
        foreach group [$db oper reportParallelInst \
                        [list cell:$cell_name {} NOOP NOOP]] {
            set parent_oid [$db parentModule [lindex $group 0]]

            ##
            # Skip group if the parent module only contains group instances.
            # => already merged...
            #
            set children 0
            $db foreach inst $parent_oid oid {
                incr children
            }
            if {$children == [llength $group]} {
                continue
            }

            ##
            # Check that at least one instance is visible in a Cone window.
            #
            if {$_MergeTabRestrictToCone} {
                set parent_name [$db oid oname $parent_oid]
                if {![dict exists $in_cone $parent_name]} {
                    continue
                }
                set found 0
                foreach oid $group {
                    set inst_name [$db oid oname $oid]
                    if {[dict exists $in_cone $parent_name $inst_name]} {
                        set found 1
                        break
                    }
                }
                if {!$found} {
                    continue
                }
            }

            set parent_path \
                [$db oid print $parent_oid -notype -noroot -hiersep $hiersep]
            set parent_text \
                "#$index, [llength $group]x $orig_cell_name in $parent_path"
            set parent [$w.middle.tree insert {} end \
                -text $parent_text \
                -values [list $parent_oid $group] \
            ]
            foreach inst_oid $group {
                $w.middle.tree insert $parent end \
                    -text [$db oid oname $inst_oid] \
                    -values [list $inst_oid]
            }
            incr index
        }
    }

    foreach group [_findSerialResistors] {
        set parent_oid [$db parentModule [lindex $group 0]]

        ##
        # Skip group if the parent module only contains group instances.
        # => already merged...
        #
        set children 0
        $db foreach inst $parent_oid oid {
            incr children
        }
        if {$children == [llength $group]} {
            continue
        }

        ##
        # Check that at least one instance is visible in a Cone window.
        #
        if {$_MergeTabRestrictToCone} {
            set parent_name [$db oid oname $parent_oid]
            if {![dict exists $in_cone $parent_name]} {
                continue
            }
            set found 0
            foreach oid $group {
                set inst_name [$db oid oname $oid]
                if {[dict exists $in_cone $parent_name $inst_name]} {
                    set found 1
                    break
                }
            }
            if {!$found} {
                continue
            }
        }

        set parent_path \
            [$db oid print $parent_oid -notype -noroot -hiersep $hiersep]
        set parent_text \
            "#$index, [llength $group]x $orig_cell_name in $parent_path"
        set parent [$w.middle.tree insert {} end \
            -text $parent_text \
            -values [list $parent_oid $group] \
        ]
        foreach inst_oid $group {
            $w.middle.tree insert $parent end \
                -text [$db oid oname $inst_oid] \
                -values [list $inst_oid]
        }
        incr index
    }
}


# -----------------------------------------------------------------------------
# _findSerialResistors -
# -----------------------------------------------------------------------------
#
proc ::ManipulateDesign::_findSerialResistors {} {
    set db [gui database get]
    if {$db == {}} {
        return {}
    }

    set results {}

    $db foreach module parent {
        set leaders [dict create]
        set groups [dict create]
        set candidates {}
        $db foreach inst $parent inst {
            set inst_mod [$db oid down $inst]
            if {([$db attr $inst_mod getValue ORIGINAL_FUNC] eq "RES") ||
                ([$db primFuncOf $inst_mod] eq "RES")
            } {
                set name [$db oid oname $inst]
                dict set leaders $name $name
                dict set groups $name [list $name]
            }
        }

        $db foreach net $parent net {
            if {([$db count pin $net] != 2) ||
                ([$db count pin $net -noport] != 2)
            } {
                continue
            }

            set names {}
            set bulk -1
            $db foreach pin $net pin {
                if {[$db oid type $pin] ne "pin"} {
                    set names {}
                    break
                }
                set inst [$db oid convertTo inst $pin]
                set name [$db oid oname $inst]
                if {![dict exists $leaders $name]} {
                    set names {}
                    break
                }
                if {$bulk == -1} {
                    set bulk [_resistorIsBulkPin $db $pin]
                }

                ##
                # Skip bulk pin connections at this point.
                #
                if {$bulk == 1} {
                    set names {}
                    break
                }

                lappend names $name
                if {[llength $names] > 2} {
                    set names {}
                    break
                }
            }
            if {[llength $names] != 2} {
                continue
            }

            lassign $names n0 n1
            set new_leader [dict get $leaders $n0]
            set new_group  [dict get $groups $new_leader]
            set old_leader [dict get $leaders $n1]

            foreach member [dict get $groups $old_leader] {
                dict set leaders $member $new_leader
                lappend new_group $member
            }
            dict unset groups $old_leader
            dict set groups $new_leader $new_group
        }

        dict for {leader group} $groups {
            if {[llength $group] <= 1} {
                continue
            }

            set end_pins {}
            foreach name $group {
                set inst [$db search inst $parent $name]
                set index 0
                $db foreach pin $inst pin {
                    ##
                    # Skip bulk pins.
                    #
                    if {$index >= 2} {
                        break
                    }
                    incr index

                    if {![$db isConnected $pin]} {
                        lappend end_pins $pin
                        continue
                    }

                    set net [$db connectedNet $pin]
                    if {([$db count pin $net] != 2) ||
                        ([$db count pin $net -noport] != 2)
                    } {
                        lappend end_pins $pin
                        continue
                    }

                    set other_pin {}
                    $db foreach pin $net net_pin {
                        if {$net_pin != $pin} {
                            set other_pin $net_pin
                            break
                        }
                    }

                    set other_inst [$db oid convertTo inst $other_pin]
                    set other_name [$db oid oname $other_inst]
                    if {(![dict exists $leaders $other_name]) ||
                        ([dict get $leaders $other_name] ne $leader)
                    } {
                        lappend end_pins $pin
                        continue
                    }

                    if {[_resistorIsBulkPin $db $other_pin]} {
                        lappend end_pins $pin
                        continue
                    }
                }
            }

            if {[llength $end_pins] != 2} {
                error "end_pins: $end_pins"
            }

            ##
            # Iterate over $instances in serial connectivity order, start with
            # $pin; group by equal bulk nets; add groups to results.
            #
            set group {}
            set group_bulks {}
            lassign $end_pins pin end_pin

            set pin [_resistorOppositePin $db $pin]
            while {$pin != $end_pin} {
                set inst [$db oid convertTo inst $pin]
                set bulks [_resistorBulkNets $db $inst]
                if {$group == {}} {
                    set group [list $inst]
                    set group_bulks $bulks
                } else {
                    if {$bulks == $group_bulks} {
                        lappend group $inst
                    } else {
                        if {[llength $group] > 1} {
                            lappend results $group
                        }
                        set group [list $inst]
                        set group_bulks $bulks
                    }
                }

                set net [$db connectedNet $pin]
                $db foreach pin $net net_pin {
                    if {$net_pin != $pin} {
                        set pin $net_pin
                        break
                    }
                }
                set pin [_resistorOppositePin $db $pin]
            }

            if {[llength $group] > 1} {
                lappend results $group
            }
        }
    }

    return $results
}


# -----------------------------------------------------------------------------
# _resistorIsBulkPin -
# -----------------------------------------------------------------------------
#
proc ::ManipulateDesign::_resistorIsBulkPin {
    db
    pin
} {
    set inst [$db oid convertTo inst $pin]
    set index 0
    $db foreach pin $inst oid {
        if {$oid == $pin} {
            if {[expr {$index >= 2}]} {
                return 1
            }
            return 0
        }
        incr index
    }
    return 0
}


# -----------------------------------------------------------------------------
# _resistorBulkNets -
# -----------------------------------------------------------------------------
#
proc ::ManipulateDesign::_resistorBulkNets {
    db
    inst
} {
    set bulk_nets {}
    set index 0
    $db foreach pin $inst pin {
        if {$index >= 2} {
            if {[$db isConnected $pin]} {
                lappend bulk_nets [$db connectedNet $pin]
            } else {
                lappend bulk_nets {}
            }
        }
        incr index
    }
    return $bulk_nets
}


# -----------------------------------------------------------------------------
# _resistorOppositePin -
# -----------------------------------------------------------------------------
#
proc ::ManipulateDesign::_resistorOppositePin {
    db
    pin
} {
    set inst [$db oid convertTo inst $pin]
    $db foreach pin $inst oid {
        if {$oid != $pin} {
            return $oid
        }
    }
    return {}
}


# -----------------------------------------------------------------------------
# _mergeTabDouble - Double click handler.
# -----------------------------------------------------------------------------
#
proc ::ManipulateDesign::_mergeTabDouble {
    w
    x
    y
} {
    set item [$w.middle.tree identify item $x $y]
    if {$item == {}} {
        return
    }

    set value [$w.middle.tree item $item -value]
    switch -exact -- [llength $value] {
        1 {
            gui zoomTo $value -class Cone
            gui goto $value -class Cone
        }
        2 {
            gui zoomTo [lindex $value 1] -class Cone
            gui goto [lindex $value 1] -class Cone
        }
        default {}
    }
}


# -----------------------------------------------------------------------------
# _mergeTabMerge -
# -----------------------------------------------------------------------------
#
proc ::ManipulateDesign::_mergeTabMerge {
    w
    all
} {
    variable _MergeTabSelectionGroups
    variable _MergeTabSelectionNodes

    if {$all} {
        set _MergeTabSelectionGroups {}
        set _MergeTabSelectionNodes [$w.middle.tree children {}]
        foreach node $_MergeTabSelectionNodes {
            lappend _MergeTabSelectionGroups \
                [lindex [$w.middle.tree item $node -value] 1]
        }
    }

    if {[llength $_MergeTabSelectionGroups] == 0} {
        return
    }

    if {[::ManipulateDesign::MergeInstanceLists {*}$_MergeTabSelectionGroups]} {
        $w.middle.tree delete $_MergeTabSelectionNodes
        _mergeTabResetSelection
    }
}


# -----------------------------------------------------------------------------
# _mergeTabSelect -
# -----------------------------------------------------------------------------
#
proc ::ManipulateDesign::_mergeTabSelect {
    w
} {
    variable _MergeTabSelectionGroups
    variable _MergeTabSelectionNodes
    variable _MergeTabSelectionLabel

    set _MergeTabSelectionGroups {}
    set _MergeTabSelectionNodes {}
    foreach s [$w.middle.tree selection] {
        if {$s == {}} {
            continue
        }

        set value [$w.middle.tree item $s -value]
        if {[llength $value] == 1} {
            continue
        }

        lappend _MergeTabSelectionNodes $s
        lappend _MergeTabSelectionGroups \
            [lindex [$w.middle.tree item $s -value] 1]
    }

    switch -- [llength $_MergeTabSelectionNodes] {
        0 {
            set _MergeTabSelectionLabel "No Group Selected"
        }
        1 {
            set _MergeTabSelectionLabel \
                [$w.middle.tree item [lindex $_MergeTabSelectionNodes 0] -text]
        }
        default {
            set _MergeTabSelectionLabel \
                "[llength $_MergeTabSelectionNodes] Selected Groups"
        }
    }
}


# -----------------------------------------------------------------------------
# _mergeTabResetSelection -
# -----------------------------------------------------------------------------
#
proc ::ManipulateDesign::_mergeTabResetSelection {} {
    variable _MergeTabSelectionGroups
    variable _MergeTabSelectionNodes
    variable _MergeTabSelectionLabel

    set _MergeTabSelectionGroups {}
    set _MergeTabSelectionNodes {}
    set _MergeTabSelectionLabel "No Groups Selected"
}