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 | ###############################################################################
# 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 instances.
###############################################################################
# =============================================================================
# MergeInstances -
# =============================================================================
#
proc ::ManipulateDesign::MergeInstances {
new_instance_name
oids
} {
set db [gui database get]
if {$db == {}} {
return 0
}
if {[$db attr -db getValue ManipulateDesign:Annotated] == {}} {
zmessage print ERR "Paths have to be annotated first!"
return 0
}
set common_parent {}
set instances {}
foreach oid $oids {
if {![catch {$db oid create $oid} result]} {
set oid $result
} else {
zmessage print WAR "Merge: Ignoring non-existing OID: $oid"
continue
}
if {[$db oid type $oid] ne "inst"} {
zmessage print WAR "Merge: Ignoring non-instance $oid"
continue
}
if {[$db tdevice $oid]} {
zmessage print ERR "Merge: Cannot merge device instance: $oid"
return 0
}
set parent [$db parentModule $oid]
if {$common_parent == {}} {
set common_parent $parent
} elseif {$parent != $common_parent} {
zmessage print ERR \
"Merge: cannot merge instances from multiple parents"
return 0
}
lappend instances $oid
}
if {[llength $instances] <= 1} {
return 0
}
zprogress begin
set cones {}
::WindowManager::Foreach Cone w {
set nlv [gui cone nlv -window $w]
foreach instance $instances {
zprogress update "CHECK CONE" 0 1
if {[gui nlview isDisplayed $nlv $instance]} {
lappend cones $w
break
}
}
}
set first [lindex $instances 0]
if {$new_instance_name eq ""} {
set new_instance_name [$db oid oname $first]
}
set addhier 0
if {[_parallel $db $instances]} {
zprogress update "MERGE PARALLEL" 0 1
RecordMerge $new_instance_name $instances
set newInstance [_merge $db $instances $new_instance_name]
} elseif {[_serialResistors $db $instances]} {
zprogress update "MERGE SERIAL" 0 1
RecordMerge $new_instance_name $instances
set newInstance [_mergeSerial $db $instances $new_instance_name]
} else {
zprogress update "MERGE" 0 1
set new_instance_name [$db uniqueName \
[$db oid oname $common_parent] inst $new_instance_name]
RecordMerge $new_instance_name $instances
set newInstance [$db oper addhier \
$new_instance_name $new_instance_name $instances -pwrgndtoo]
set addhier 1
}
set newModule [$db oid down $newInstance]
$db attr $newModule set @nlv=%mergemarker
$db hilight $newInstance set 2 -permanent
$db attr $newInstance set mergemarker=MERGED([llength $instances])
gui database modified
foreach w $cones {
zprogress update "UPDATE CONE" 0 1
set add [list $newInstance]
if {$addhier} {
$db foreach pin $newInstance oid {
lappend add $oid
}
}
gui cone append -window $w -highlight $add
gui goto -window $w [list $newInstance]
}
zprogress end
ClearOriginalPathCache
return 1
}
# =============================================================================
# MergeInstanceLists - Merge multiple lists of OIDs in one go, e.g.
# ::ManipulateDesign::MergeInstanceLists list1 list2 ...
# =============================================================================
#
proc ::ManipulateDesign::MergeInstanceLists {
args
} {
set db [gui database get]
if {$db == {}} {
return 0
}
if {[$db attr -db getValue ManipulateDesign:Annotated] == {}} {
zmessage print ERR "Paths have to be annotated first!"
return 0
}
zprogress begin
set lists {}
foreach oids $args {
set common_parent {}
set instances {}
foreach oid $oids {
if {![catch {$db oid create $oid} result]} {
set oid $result
} else {
zmessage print WAR "Merge: Ignoring non-existing OID: $oid"
continue
}
if {[$db oid type $oid] ne "inst"} {
zmessage print WAR "Merge: Ignoring non-instance $oid"
continue
}
if {[$db tdevice $oid]} {
zmessage print ERR "Merge: Cannot merge device instance: $oid"
zprogress end
return 0
}
set parent [$db parentModule $oid]
if {$common_parent == {}} {
set common_parent $parent
} elseif {$parent != $common_parent} {
zmessage print ERR \
"Merge: cannot merge instances from multiple parents"
zprogress end
return 0
}
lappend instances $oid
}
if {[llength $instances] == 0} {
continue
}
set cones {}
::WindowManager::Foreach Cone w {
set nlv [gui cone nlv -window $w]
foreach instance $instances {
zprogress update "CHECK CONE CONE" 0 1
if {[gui nlview isDisplayed $nlv $instance]} {
lappend cones $w
break
}
}
}
lappend lists $instances $cones
}
RecordMergeLists {*}$args
set cone_add [dict create]
foreach {instances cones} $lists {
set first [lindex $instances 0]
set new_instance_name [$db oid oname $first]
set addhier 0
if {[_parallel $db $instances]} {
zprogress update "MERGE PARALLEL" 0 1
set newInstance [_merge $db $instances $new_instance_name]
} elseif {[_serialResistors $db $instances]} {
zprogress update "MERGE SERIAL" 0 1
set newInstance \
[_mergeSerialResistors $db $instances $new_instance_name]
} else {
zprogress update "MERGE" 0 1
set new_instance_name [$db uniqueName \
[$db oid oname $common_parent] inst $new_instance_name]
set newInstance [$db oper addhier \
$new_instance_name $new_instance_name $instances -pwrgndtoo]
set addhier 1
}
set newModule [$db oid down $newInstance]
$db attr $newModule set @nlv=%mergemarker
$db hilight $newInstance set 2 -permanent
$db attr $newInstance set mergemarker=MERGED([llength $instances])
foreach w $cones {
set add [list $newInstance]
if {$addhier} {
$db foreach pin $newInstance oid {
lappend add $oid
}
}
dict lappend cone_add $w {*}$add
}
}
gui database modified
dict for {w oids} $cone_add {
zprogress update "UPDATE CONE" 0 1
gui cone append -window $w -highlight $oids
}
zprogress end
ClearOriginalPathCache
return 1
}
# -----------------------------------------------------------------------------
# _parallel -
# -----------------------------------------------------------------------------
#
proc ::ManipulateDesign::_parallel {
db
instances
} {
if {[llength $instances] <= 1} {
return 0
}
set first [lindex $instances 0]
set cell [$db oid down $first]
set nets [dict create]
$db foreach pin $first pin {
set name [$db oid pname $pin]
if {[$db isConnected $pin]} {
dict set nets $name [$db connectedNet $pin]
} else {
dict set nets $name ""
}
}
foreach inst $instances {
if {[$db oid oname [$db oid down $inst]] ne [$db oid oname $cell]} {
return 0
}
$db foreach pin $inst pin {
set name [$db oid pname $pin]
if {[$db isConnected $pin]} {
set net [$db connectedNet $pin]
} else {
set net ""
}
if {$net != [dict get $nets $name]} {
return 0
}
}
}
return 1
}
# -----------------------------------------------------------------------------
# _merge -
# -----------------------------------------------------------------------------
#
proc ::ManipulateDesign::_merge {
db
instances
new_inst_name
} {
set first [lindex $instances 0]
set cell [$db down $first]
set parent [$db parentModule $first]
set func [$db attr $cell getValue ORIGINAL_FUNC]
if {$func eq ""} {
set func [$db primFuncOf $cell]
}
if {$func eq "UNKNOWN"} {
set func [$db oid oname $cell]
}
set new_cell_name [$db uniqueName "" cell merged-[llength $instances]-$func]
set symbol [gui nlview getSymbol $first]
if {$symbol ne ""} {
$db load module $new_cell_name -attr @symbol=$symbol
} else {
$db load module $new_cell_name
}
set new_cell [$db search module $new_cell_name]
foreach inst $instances {
$db flag $inst set zombie
set iname [$db oid oname $inst]
$db load inst $iname [$db oid oname $cell]
set new_inst [$db search inst $new_cell $iname]
$db orient $new_inst [$db orient $inst]
$db attr $inst foreach attr {
$db attr $new_inst add $attr
}
}
$db foreach port $cell port {
set pname [$db oid oname $port]
$db load port $pname [$db directionOf $port]
$db load net $pname -port $pname
foreach inst $instances {
set iname [$db oid oname $inst]
$db load net $pname -pin $iname $pname
}
}
$db reloadModule [$db oid oname $parent]
$db load inst $new_inst_name $new_cell_name
set new_inst {}
$db foreach inst $parent oid {
if {[$db oid oname [$db oid down $oid]] eq $new_cell_name} {
set new_inst $oid
break
}
}
$db orient $new_inst [$db orient $first]
$db foreach pin $first pin {
if {[$db isConnected $pin]} {
set pname [$db oid pname $pin]
set net [$db connectedNet $pin]
$db load net [$db oid oname $net] -pin $new_inst_name $pname
}
}
$db deleteZombies
return $new_inst
}
# -----------------------------------------------------------------------------
# _serialResistors -
# -----------------------------------------------------------------------------
#
proc ::ManipulateDesign::_serialResistors {
db
instances
} {
if {[llength $instances] <= 1} {
return 0
}
##
# Check that all instances are resistors.
#
set instSet [dict create]
foreach inst $instances {
set inst_mod [$db oid down $inst]
if {([$db attr $inst_mod getValue ORIGINAL_FUNC] ne "RES") &&
([$db primFuncOf $inst_mod] ne "RES")
} {
return 0
}
dict set instSet $inst 1
}
##
# Check for common bulks.
#
set common_bulks "NOT_INITIALIZED"
foreach inst $instances {
set bulks [_resistorBulkNets $db $inst]
if {$common_bulks == "NOT_INITIALIZED"} {
set common_bulks $bulks
} elseif {$bulks != $common_bulks} {
return 0
}
}
##
# Collect interconnecting nets (there must be exactly n-1),
# count end pins of the instance group (there must be exactly 2).
#
set endPins 0
set netSet [dict create]
foreach inst $instances {
set index 0
$db foreach pin $inst inst_pin {
##
# Skip bulks.
#
if {$index >= 2} {
break
}
incr index
if {![$db isConnected $inst_pin]} {
incr endPins
continue
}
set net [$db connectedNet $inst_pin]
if {![_isInterconnecting2 $db $instSet $net]} {
incr endPins
continue
}
dict set netSet $net 1
}
}
if {($endPins != 2) ||
(([dict size $netSet] + 1) != [llength $instances])
} {
return 0
}
return 1
}
# -----------------------------------------------------------------------------
# _isInterconnecting2 -
# -----------------------------------------------------------------------------
#
proc ::ManipulateDesign::_isInterconnecting2 {
db
instSet
net
} {
set count 0
$db foreach pin $net net_pin {
if {[$db oid type $net_pin] ne "pin"} {
return 0
}
if {![dict exists $instSet [$db oid convertTo inst $net_pin]]} {
return 0
}
incr count
}
return [expr {$count == 2}]
}
# -----------------------------------------------------------------------------
# _mergeSerialResistors -
# -----------------------------------------------------------------------------
#
proc ::ManipulateDesign::_mergeSerialResistors {
db
instances
new_inst_name
} {
set first [lindex $instances 0]
set cell [$db down $first]
set parent [$db parentModule $first]
set func [$db attr $cell getValue ORIGINAL_FUNC]
if {$func eq ""} {
set func [$db primFuncOf $cell]
}
if {$func eq "UNKNOWN"} {
set func [$db oid oname $cell]
}
##
# Create a new (hierarchical) resistor cell.
#
set new_cell_name [$db uniqueName "" cell merged-[llength $instances]-$func]
set symbol [gui nlview getSymbol $first]
if {$symbol ne ""} {
$db load module $new_cell_name -attr @symbol=$symbol
} else {
$db load module $new_cell_name
}
set new_cell [$db search module $new_cell_name]
set ports {}
$db foreach port $cell cell_port {
set port_name [$db oid oname $cell_port]
$db load port $port_name [$db directionOf $cell_port]
lappend ports [$db search port $new_cell $port_name]
}
##
# Create child instances.
#
set instMap [dict create]
foreach inst $instances {
$db flag $inst set zombie
set iname [$db oid oname $inst]
$db reloadModule $new_cell_name
$db load inst $iname [$db oid oname $cell]
set new_inst [$db search inst $new_cell $iname]
$db orient $new_inst [$db orient $inst]
$db attr $inst foreach attr {
$db attr $new_inst add $attr
}
dict set instMap $inst $new_inst
}
##
# Interconnect...
#
set next_port 0
$db reloadModule $new_cell_name
set external_pin_net {}
foreach inst $instances {
set index -1
$db foreach pin $inst inst_pin {
incr index
##
# Bulk pin.
#
if {$index >= 2} {
if {[$db isConnected $inst_pin]} {
set net [$db connectedNet $inst_pin]
set net_name [$db oid oname $net]
set port [lindex $ports $index]
if {[$db isConnected $port]} {
$db load net $net_name \
-pin [$db oid oname $inst] [$db oid pname $inst_pin]
} else {
$db load net $net_name \
-port [$db oid oname $port] \
-pin [$db oid oname $inst] [$db oid pname $inst_pin]
lappend external_pin_net [$db oid oname $port] $net_name
}
}
continue
}
##
# Dangling resistor pin
# => connect to a hierarchical port
#
if {![$db isConnected $inst_pin]} {
if {$next_port < 2} {
set port [lindex $ports $next_port]
incr next_port
$db load net "end-net-[$db oid oname $port]" \
-port [$db oid oname $port] \
-pin [$db oid oname $inst] [$db oid pname $inst_pin]
}
continue
}
set net [$db connectedNet $inst_pin]
set net_name [$db oid oname $net]
##
# "End" resistor pin
# => connect to a hierarchical port
# + connect hierarchical pin to original net
#
if {![_isInterconnecting2 $db $instMap $net]} {
if {$next_port < 2} {
set port [lindex $ports $next_port]
incr next_port
$db load net "end-net-[$db oid oname $port]" \
-port [$db oid oname $port] \
-pin [$db oid oname $inst] [$db oid pname $inst_pin]
lappend external_pin_net [$db oid oname $port] $net_name
}
continue
}
##
# Check if we already created this net.
#
if {![$db oid isnull [$db search net $new_cell $net_name]]} {
continue
}
$db flag $net set zombie
$db load net $net_name
$db foreach pin $net net_pin {
set net_pin_inst [$db oid convertTo inst $net_pin]
$db load net [$db oid oname $net] \
-pin [$db oid oname $net_pin_inst] [$db oid pname $net_pin]
}
}
}
##
# Create a new (hierarchical) instance.
#
$db reloadModule [$db oid oname $parent]
$db load inst $new_inst_name $new_cell_name
set new_cell_inst [$db search inst $parent $new_inst_name]
$db orient $new_cell_inst [$db orient $first]
$db deleteZombies
##
# Create external connections
#
$db reloadModule [$db oid oname $parent]
foreach {pin_name net_name} $external_pin_net {
$db load net $net_name -pin $new_inst_name $pin_name
}
return $new_cell_inst
}
|