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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830 | ###############################################################################
# Copyright (c) 2011-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.
# =============================================================================
# @userware
# Find Reconvergent Logic
# @section
# Analyze the Loaded Database
# @description
# Read a side file that contains pairs of FF pins. The task of this
# userware is to find paths with reconvergent logic between these pins.
# The first column in the file is used as the start pin the second column
# is used as the target pin.
# @files
# cust11/hireconvlogic.tcl
# @example
# demo/spice/gl85.sp
# @tag
# zdb gui
###############################################################################
# =============================================================================
# Static configuration
# =============================================================================
#
set HiReConvLogic(reportFile) "report.txt"
set HiReConvLogic(pinDelim) "/"
# =============================================================================
# Start - This is the main procedure of the HiReConvLogic Userware.
# =============================================================================
#
proc HiReConvLogic:Start {db} {
global HiReConvLogic
##
# Check if the FF pair list file exists.
#
if {![file exists $HiReConvLogic(FFPairList)]} {
error "FF pair list file $HiReConvLogic(FFPairList) does not exist."
}
##
# Check if the async cell list file exists.
#
if {![file exists $HiReConvLogic(asyncCells)]} {
error "Async cell list file $HiReConvLogic(asyncCells) does not exist."
}
##
# Return if the database is empty.
#
if {$db == {}} {
return
}
##
# Remove all top level cells that have the libcell flag set.
#
set haveZombies False
$db foreach top top {
if {[$db flag $top is libcell]} {
$db flag $top set zombie
set haveZombies true
}
}
if {$haveZombies} {
$db deleteZombies
}
##
# Toggle the primitive view of the database.
#
HiReConvLogic:_togglePrimLevel $db true
gui database modified
##
# Remember the current database.
#
set HiReConvLogic(db) $db
##
# Create the result widget.
#
set table [HiReConvLogic:_create "Path Vision"]
##
# Show the 'Cone' tab.
#
gui window show Cone
##
# Get the top module of the loaded design. If is more than one top then
# show a warning message with the used top.
#
set topCnt 0
$db foreach top top {incr topCnt}
set top {}
$db foreach top top break
if {$topCnt > 1} {
gui console print "Found multiple TOPs (using [$db oid oname $top])" w
}
##
# Get the hierarchy separator used in the loaded design.
#
set hiersep [$db oper hiersep get]
##
# Open the FF pair list file.
#
foreach row [$table children {}] {
$table delete $row
}
HiReConvLogic:_readInputFile $db $table $top $hiersep \
$HiReConvLogic(pinDelim) \
$HiReConvLogic(FFPairList) \
$HiReConvLogic(asyncCells)
}
# -----------------------------------------------------------------------------
# _togglePrimLevel - This procedure toggles the primitive level of the
# database. If $doit is true then library cells and
# all cells with a symbol are treated as primitives.
# -----------------------------------------------------------------------------
#
proc HiReConvLogic:_togglePrimLevel {db doit} {
set args -clear
gui settings set "primLevelOper" $doit
gui settings set "primLevelLibCell" $doit
gui settings set "primLevelSymbol" $doit
gui settings changed
if {[gui settings get "primLevelOper"]} {
lappend args -operator
}
if {[gui settings get "primLevelLibCell"]} {
lappend args -libcell
}
if {[gui settings get "primLevelSymbol"]} {
lappend args -symbol
}
eval $db setPrimitive $args
}
# -----------------------------------------------------------------------------
# _readInputFile - Read the given input file that contains start and target
# pin names. Create valid OIDs of the start and target pins,
# store these OIDs in a global array.
# -----------------------------------------------------------------------------
#
proc HiReConvLogic:_readInputFile {db table top hiersep delim pairF asyncF} {
global HiReConvLogic
##
# Open the FF pair list file.
#
set in [open $pairF "r"]
set count 0
set fsize [file size $pairF]
set fpos 0
##
# Create a report file if requested.
#
set out ""
if {$HiReConvLogic(reportFile) != ""} {
set out [open $HiReConvLogic(reportFile) "w"]
}
##
# Clear the private array.
#
array unset HiReConvLogic *:start
array unset HiReConvLogic *:target
zprogress begin
zprogress push "read input file" 0.02
while {![eof $in]} {
##
# Read the next line of the input file.
#
set bytes [gets $in line]
if {$bytes < 0} {
break
}
incr pos $bytes
##
# Update the progress bar every 100 lines.
#
if {($count % 100) == 0} {
if {[zprogress update "" $pos $fsize]} {
break
}
}
##
# Skip empty lines.
#
set line [string trim $line]
if {$line == ""} {
continue
}
##
# Try to create a valid OID for the start and target names.
#
set line0 [lindex $line 0]
set line1 [lindex $line 1]
set start [HiReConvLogic:_createOID $db $top $hiersep $delim $line0]
set target [HiReConvLogic:_createOID $db $top $hiersep $delim $line1]
##
# Skip this line if either the start or target OID could not be created.
#
if {($start == {}) || ($target == {})} {
continue
}
##
# Store the start and the target OIDs.
#
set HiReConvLogic($count:start) $start
set HiReConvLogic($count:target) $target
##
# Add an entry to the result list.
#
set txt "Path [expr {$count + 1}]: $line0 --> $line1"
set HiReConvLogic($count:ID) [$table insert {} end -text $txt]
##
# Increase the number of lines.
#
incr count
}
close $in
zprogress pop
##
# Open the async cell list file.
#
set in [open $asyncF "r"]
##
# Flag all ports of all clocked cells as red (needed by the
# -excludeFlaggedPort option).
#
$db foreach cell cell {
if {[$db flag $cell is clock]} {
$db foreach port $cell port {$db flag $port set red}
}
}
##
# Lop over all lines in the async cell list file and set the exclude flags
# for the path search.
#
while {![eof $in]} {
##
# Read the next line of the input file.
#
set bytes [gets $in line]
if {$bytes < 0} {
break
}
##
# Skip empty or comment lines.
#
set line [string trim $line]
if {($line == "") || [string match "#*" $line]} {
continue
}
##
# The first column contains the cell name, the second column contains
# the input pin name and the third column contains the output pin name.
#
set cellName [lindex $line 0]
set inPinName [lindex $line 1]
set outPinName [lindex $line 2]
##
# Search the cell in the loaded database.
#
set cell [$db search cell $cellName]
##
# Skip all cells that could not be found.
#
if {[$db oid isnull $cell]} {
continue
}
##
# Remove the red flag at all ports specified in the async cell file.
#
$db foreach port $cell port {
set portName [$db oid oname $port]
if {$portName == $inPinName || $portName == $outPinName} {
$db flag $port clear red
}
}
}
close $in
##
# If the report file should be created then calculate all paths now,
# add them to the value field of the result list and dump it to the
# report file.
#
set stop [zprogress isinterrupted]
for {set i 0} {(!$stop) && ($out != "") && ($i < $count)} {incr i} {
zprogress push "Path [expr {$i + 1}]:" [expr {$i / double($count)}]
set res [HiReConvLogic:_calculatePath $i]
HiReConvLogic:_addResult $table $HiReConvLogic($i:ID) $res
puts $out [$table item $HiReConvLogic($i:ID) -text]
HiReConvLogic:_createReport $res $out
if {[zprogress pop]} {
break
}
}
zprogress end
##
# Close the report file.
#
if {$out != ""} {
close $out
}
}
# -----------------------------------------------------------------------------
# _createOID - Try to create a valid OID for the given name. Return an empty
# OID if no valid OID could be created.
# -----------------------------------------------------------------------------
#
proc HiReConvLogic:_createOID {db top hiersep delim name} {
set oid {}
if {[catch {
set oid [$db oid createFromString pin $top $name $hiersep -delim $delim]
} msg]} {
zmessage print ERR "Cannot create an OID for $name:\n$msg"
}
return $oid
}
# -----------------------------------------------------------------------------
# _create - Create a table widget as a custom widget in the main GUI.
# -----------------------------------------------------------------------------
#
proc HiReConvLogic:_create {name} {
##
# Return if the custom widget already exists.
#
if {[gui window exists $name]} {
return
}
##
# Create a custom widget.
#
set w [gui window insertCustomWidget $name]
##
# Use a Tree (as a table) to display the paths.
#
ttk::treeview $w.table -yscrollcommand [list $w.scrollbar set]
$w.table heading #0 -text "Path" -anchor w
ttk::scrollbar $w.scrollbar -orient vertical -command [list $w.table yview]
grid $w.table -row 1 -column 0 -sticky news
grid $w.scrollbar -row 1 -column 1 -sticky ns
grid columnconfigure $w 0 -weight 1
grid rowconfigure $w 1 -weight 1
##
# Configure two tags for coloring the results in red, green or gray.
#
$w.table tag configure red -foreground red
$w.table tag configure green -foreground #009900
$w.table tag configure grey -foreground #999999
##
# Bind a double click to show one path.
#
bind $w.table <Double-1> {HiReConvLogic:_double %W %x %y; break}
##
# Return the name of the table widget.
#
return $w.table
}
# -----------------------------------------------------------------------------
# _calculatePath - Calculate one path for the start and end point for the
# given index.
# -----------------------------------------------------------------------------
#
proc HiReConvLogic:_calculatePath {index} {
global HiReConvLogic
set startPin $HiReConvLogic($index:start)
set targetPin $HiReConvLogic($index:target)
set val [$HiReConvLogic(db) cone -paths -out -excludeFlaggedPort red \
-targetObj $targetPin $startPin]
return $val
}
# -----------------------------------------------------------------------------
# _hasReconvergentPaths - Return true if the given result has a re-convergent
# path. Otherwise return false.
# -----------------------------------------------------------------------------
#
proc HiReConvLogic:_hasReconvergentPaths {res} {
if {[llength $res] == 1} {
return false
}
return true
}
# -----------------------------------------------------------------------------
# _addResult - Add the given paths to the result table.
# -----------------------------------------------------------------------------
#
proc HiReConvLogic:_addResult {w id res} {
global HiReConvLogic
set db $HiReConvLogic(db)
##
# If this result has a re-convergent path then add all paths to the
# result table.
#
set color green
if {[HiReConvLogic:_hasReconvergentPaths $res]} {
set color red
##
# Add each path as a child of $id.
#
foreach path $res {
$w insert $id end -text $path
}
}
##
# If the result list is empty show the entry in gray color.
#
if {[llength $res] == 0} {
set color grey
}
##
# Add the result to the values field of the parent item.
#
$w item $id -values $res
##
# Apply the color tag.
#
$w item $id -tag $color
}
# -----------------------------------------------------------------------------
# _hilightPath - Highlight the given path with the given color.
# -----------------------------------------------------------------------------
#
proc HiReConvLogic:_hilightPath {col path} {
global HiReConvLogic
set db $HiReConvLogic(db)
##
# Loop over each path element and create net segments connecting the pins.
#
set oidList {}
set pair {}
foreach oid $path {
##
# Convert this OID only to an instance if it's a pin.
#
if {[$db oid type $oid] == "pin"} {
$db flathilight [$db oid convertTo inst $oid] set $col
}
$db flathilight $oid set $col
lappend pair $oid
if {[llength $pair] == 2} {
set netseg [$db oid createNetSeg [lindex $pair 0] [lindex $pair 1]]
$db flathilight $netseg set $col
set pair {}
}
}
gui highlight changed
}
# -----------------------------------------------------------------------------
# _loadToCone - Load the given path to the Cone window.
# -----------------------------------------------------------------------------
#
proc HiReConvLogic:_loadToCone {res} {
global HiReConvLogic
set db $HiReConvLogic(db)
##
# Clear the old contents of the Cone window.
#
gui cone clear
##
# calculate the oidList from the given result.
#
set oidList {}
foreach path $res {
foreach oid [lrange $path 1 end] {
lappend oidList $oid
}
}
##
# Highlight the complete result (all paths).
#
HiReConvLogic:_hilightPath 1 $oidList
##
# Count the frequency of pins in the result.
#
set path $res
array set _pathMap {}
array set _oidMap {}
set count [llength $path]
set maxLen 0
for {set n 0} {$n < $count} {incr n} {
set _pathMap($n) [lrange [lindex $path $n] 1 end]
set len [llength $_pathMap($n)]
if {$len > $maxLen} {
set maxLen $len
}
foreach oid $_pathMap($n) {
if {![info exists _oidMap($oid)]} {
set _oidMap($oid) 0
}
incr _oidMap($oid)
}
}
##
# Calculate the start point of the re-convergence.
#
set stop 0
set startOfReconvergence {}
for {set i 0} {(!$stop) && ($i < $maxLen)} {incr i} {
set start [lindex $_pathMap(0) $i]
for {set n 0} {$n < $count} {incr n} {
if {$start != [lindex $_pathMap($n) $i]} {
set oid [lindex $_pathMap($n) [expr {$i -1 }]]
set startOfReconvergence $oid
set stop 1
break
}
}
}
##
# Calculate the end points of the re-convergence.
#
set stop 0
for {set i 0} {(!$stop) && ($i < $maxLen)} {incr i} {
set end [lindex $_pathMap(0) end-$i]
for {set n 0} {$n < $count} {incr n} {
if {$end != [lindex $_pathMap($n) end-$i]} {
set oid [lindex $_pathMap($n) end-[expr {$i -1 }]]
if {[$HiReConvLogic(db) oid type $oid] != "pin"} {
continue
}
set inst [$HiReConvLogic(db) oid convertTo inst $oid]
set endPointList {}
$HiReConvLogic(db) foreach oPin input $inst iPin {
if {![info exists _oidMap($iPin)]} {
continue
}
lappend endPointList $iPin
$db flag $iPin set target
}
set stop 1
break
}
}
}
if {$startOfReconvergence == {}} {
return
}
set reconvergenceList \
[$db cone -paths -out -targetFlaggedPin target $startOfReconvergence]
foreach path $reconvergenceList {
HiReConvLogic:_hilightPath 0 [lrange $path 1 end]
}
foreach endPoint $endPointList {
$db flag $endPoint clear target
}
##
# Make sure that the Cone window is visible and zoom to fullfit.
#
gui window show Cone
gui cone load $oidList
gui cone zoom fullfit
}
# -----------------------------------------------------------------------------
# _double - Binding for a double click in the table.
# -----------------------------------------------------------------------------
#
proc HiReConvLogic:_double {w x y} {
global HiReConvLogic
##
# Identify the item under the mouse and return if it is not a table entry.
#
set identification [$w identify $x $y]
if {$identification == "nothing"} {
return
}
if {[string match "heading #*" $identification]} {
return
}
if {[string match "separator #*" $identification]} {
return
}
##
# Get the selected item and return if nothing is selected.
#
set item [$w selection]
if {$item == {}} {
return
}
##
# Delete all existing highlights.
#
set db $HiReConvLogic(db)
$db foreach top top {$db flathilight $top deleteAll}
##
# If the clicked entry is not a root entry then set item to the parent
# and remember the child.
#
set child {}
if {[$w parent $item] != {}} {
set child $item
set item [$w parent $item]
}
##
# Get the value (oidList) of the selected path. If this path has not been
# calculated before then calculate the path and store it.
#
set val [$w item $item -values]
if {$val == {}} {
zprogress begin
set val [HiReConvLogic:_calculatePath [$w index $item]]
HiReConvLogic:_addResult $w $item $val
zprogress end
}
##
# Load this result to the Cone window.
#
HiReConvLogic:_loadToCone $val
##
# Highlight the selected path.
#
if {$child != {}} {
set path [$w item $child -text]
HiReConvLogic:_hilightPath 5 [lrange $path 1 end]
}
}
# -----------------------------------------------------------------------------
# _createReport - Dump the start and end point of the re-convergence to the
# report file.
# -----------------------------------------------------------------------------
#
proc HiReConvLogic:_createReport {path out} {
global HiReConvLogic
##
# Only add re-convergent paths to the report file.
#
if {![HiReConvLogic:_hasReconvergentPaths $path]} {
return
}
##
# Count the frequency of pins in the result.
#
array set _pathMap {}
array set _oidMap {}
set count [llength $path]
set maxLen 0
for {set n 0} {$n < $count} {incr n} {
set _pathMap($n) [lrange [lindex $path $n] 1 end]
set len [llength $_pathMap($n)]
if {$len > $maxLen} {
set maxLen $len
}
foreach oid $_pathMap($n) {
if {![info exists _oidMap($oid)]} {
set _oidMap($oid) 0
}
incr _oidMap($oid)
}
}
set delim $HiReConvLogic(pinDelim)
##
# Calculate the start point of the re-convergence.
#
set stop 0
for {set i 0} {(!$stop) && ($i < $maxLen)} {incr i} {
set start [lindex $_pathMap(0) $i]
for {set n 0} {$n < $count} {incr n} {
if {$start != [lindex $_pathMap($n) $i]} {
set oid [lindex $_pathMap($n) [expr {$i -1 }]]
set iname [$HiReConvLogic(db) oid oname $oid]
set pname [$HiReConvLogic(db) oid pname $oid]
set txt "Start-point of Reconvergence: ${iname}${delim}${pname}"
puts $out $txt
set stop 1
break
}
}
}
##
# Calculate the end points of the re-convergence.
#
set stop 0
for {set i 0} {(!$stop) && ($i < $maxLen)} {incr i} {
set end [lindex $_pathMap(0) end-$i]
for {set n 0} {$n < $count} {incr n} {
if {$end != [lindex $_pathMap($n) end-$i]} {
set oid [lindex $_pathMap($n) end-[expr {$i -1 }]]
if {[$HiReConvLogic(db) oid type $oid] != "pin"} {
continue
}
set inst [$HiReConvLogic(db) oid convertTo inst $oid]
set txt "End-points of Reconvergence:"
set endPointList {}
$HiReConvLogic(db) foreach oPin input $inst iPin {
if {![info exists _oidMap($iPin)]} {
continue
}
lappend endPointList $iPin
}
set e 0
set l [llength $endPointList]
foreach endPoint $endPointList {
set iname [$HiReConvLogic(db) oid oname $endPoint]
set pname [$HiReConvLogic(db) oid pname $endPoint]
append txt " ${iname}${delim}${pname}"
if {[incr e] < $l} {
append txt " and"
}
}
puts $out $txt
set stop 1
break
}
}
}
}
##
# Get the name of the input file from the command line.
#
if {($argc != 2) || ([lindex $argv 0] == {}) || ([lindex $argv 1] == {})} {
set pp "pinPairList.txt"
set ac "asynCellList.txt"
error "Usage '-userware3 hireconvlogic.tcl $pp $ac'."
}
set HiReConvLogic(FFPairList) [lindex $argv 0]
set HiReConvLogic(asyncCells) [lindex $argv 1]
##
# Use gui database runOrRegisterChangedCallback to immediately run
# HiReConvLogic:Start if we have a database, or otherwise register the proc to
# be executed after the database is available.
#
gui database runOrRegisterChangedCallback HiReConvLogic:Start
|