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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
###############################################################################
# Copyright (c) 2014-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
#       Debug SPEF
#   @namespace
#       DebugSpef
#   @section
#       Analyze the Loaded Database
#   @description
#       Internal SPEF debugging.
#   @license
#       permit spice
#       available gv-spf
#   @files
#       parasitic/debugSPEF.tcl
#   @example
#       demo/spef/uart_layout.spef
#   @cmdline
#       -readSPEF @example[0]
#       -userware @files[0]
#   @tag
#       spice parasitic spef spf
###############################################################################


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

    ##
    # Create menu entries to start the code.
    #
    set DebugSpef(enabled) 0
    gui menu checkbutton {"Parasitic" "Debug SPEF"}              \
                           [list DebugSpef:_toggle "Debug SPEF"] \
                           DebugSpef(enabled)

    ##
    # Placeholder for the filter entry field.
    #
    set DebugSpef(filterPlaceholder) "Filter list of displayed D_NETs"

    ##
    # Initialize DebugSpef variables.
    #
    set DebugSpef(crossprobeTo)   "Cone"
    set DebugSpef(mapNames)       1
    set DebugSpef(firstLineD_NET) 0
    set DebugSpef(displayedD_NET) ""
    set DebugSpef(markedItem)     {}
    set DebugSpef(widgetIsCreated) false
}


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

    ##
    # Undo modifications of the GUI.
    #
    gui menu removeEntry {"Parasitic" "Debug SPEF"}

    ##
    # Destroy widget and trigger callback to remove database callbacks.
    #
    if {$DebugSpef(enabled)} {
        gui window removeCustomWidget "Debug SPEF"
    }
}


# =============================================================================
# Start - Create the Debug SPEF widget and add it as a custom widget
#         to the GUI.
# =============================================================================
#
proc DebugSpef:Start {f} {
    global DebugSpef

    ##
    # Create and add a paned window.
    #
    set pane [ttk::panedwindow $f.pane -orient horizontal]

    ##
    # Create a treeview to store the list of matched D_NETs.
    #
    set left [ttk::frame $pane.left -width 200]
    set dnet [ttk::treeview $left.dnet -selectmode browse -columns {netcap}]
    $dnet heading #0 -text "D_NET [gui settings get "texticon:triangleUp"]"  \
                     -command [list DebugSpef:_reorderD_NETs $dnet "name"]
    $dnet heading  0 -text "NetCap" \
                     -command [list DebugSpef:_reorderD_NETs $dnet "cap"]
    $dnet column   0 -anchor e -width 80 -stretch 0
    gui window autoscroll $left $dnet -direction y

    ##
    # Create a text view to display the selected D_NET.
    #
    set right [ttk::frame $pane.right]
    ttk::frame $right.f
    set spef  [text $right.f.spef]
    gui window autoscroll $right.f $spef
    DebugSpef:_configureText $spef

    grid $right.f -row 0 -column 0 -sticky news
    grid rowconfigure    $right 0 -weight 1
    grid columnconfigure $right 0 -weight 1

    ##
    # Add bindings.
    #
    bind $dnet <<TreeviewSelect>> [list DebugSpef:_selectNet %W $spef]
    bind $dnet <<TreeviewOpen>>   [list DebugSpef:_fillTree  %W]

    ##
    # Add the created widgets to the panedwindow.
    #
    $pane add $left
    $pane add $right

    ##
    # Create a toolbar for the Debug SPEF widget.
    #
    set tools [ttk::frame $f.tools -height 30]

    ttk::label $tools.activateLabel -text "Crossprobe to:"
    pack $tools.activateLabel -side left -pady 3 -padx 2
    ttk::combobox $tools.activateTo \
        -state readonly \
        -values {"Schem" "Cone" "Source"} \
        -textvariable DebugSpef(crossprobeTo)
    pack $tools.activateTo -side left -pady 3 -padx 2

    ttk::separator $tools.sep1 -orient vertical
    pack $tools.sep1 -side left -fill y -padx 3

    ttk::checkbutton $tools.mapNames \
        -text "Map Names" \
        -variable DebugSpef(mapNames) \
        -command [list DebugSpef:_toggleNameMap $dnet $spef]
    pack $tools.mapNames -side left -pady 3

    ttk::separator $tools.sep2 -orient vertical
    pack $tools.sep2 -side left -fill y -padx 3

    ##
    # Create en entry to search for a D_NET (name or number).
    #
    set find [ttk::entry $f.find \
                -validate key \
                -validatecommand [list DebugSpef:_filter %W $dnet %P]]
    bind $find <Button>   {focus %W}
    bind $find <FocusIn>  {DebugSpef:_enterFind %W}
    bind $find <FocusOut> {DebugSpef:_leaveFind %W}

    ##
    # Layout the widgets.
    #
    grid $find  -row 0 -column 0 -sticky we
    grid $tools -row 1 -column 0 -sticky nwe
    grid $pane  -row 2 -column 0 -sticky news
    grid rowconfigure    $f 2 -weight 1
    grid columnconfigure $f 0 -weight 1

    ##
    # Register a callback function to initialize the DebugSpef widget.
    #
    gui database runAndRegisterChangedCallback \
        [list DebugSpef:_initialize $dnet $spef]

    ##
    # Show the default value for the filter entry.
    #
    $find insert 0 $DebugSpef(filterPlaceholder)
}


# -----------------------------------------------------------------------------
# _toggle - Toggle the visibility of the DebugSpef widget.
# -----------------------------------------------------------------------------
#
proc DebugSpef:_toggle {name} {
    global DebugSpef

    if {$DebugSpef(enabled)} {
        set DebugSpef(widgetIsCreated) true
        set widgetPath [gui window insertCustomWidget $name \
            -pluginNamespace "DebugSpef" \
            -destroyCallback DebugSpef:_onDestroy]
        DebugSpef:Start  $widgetPath
    } elseif {$DebugSpef(widgetIsCreated)} {
        gui window removeCustomWidget $name
        set DebugSpef(widgetIsCreated) false
    }
}


# -----------------------------------------------------------------------------
# _onDestroy - Remove callbacks if window is destroyed.
# -----------------------------------------------------------------------------
#
proc DebugSpef:_onDestroy {} {
    set DebugSpef(enabled) false

    gui database removeChangedCallback "DebugSpef:_initialize"
}


# -----------------------------------------------------------------------------
# _initialize -
# -----------------------------------------------------------------------------
#
proc DebugSpef:_initialize {dnet spef db} {
    global DebugSpef

    ##
    # Return if the Debug SPEF widgets do not exist.
    #
    if {(![winfo exists $dnet]) || (![winfo exists $spef])} {
        return
    }

    ##
    # Initially fill the list of D_NETs.
    #
    DebugSpef:_initializeListOfD_NETs $dnet $db

    ##
    # Clear the text widget.
    #
    $spef configure -state normal
    $spef delete 1.0 end
    $spef configure -state disabled

    ##
    # Initialize the name map table.
    #
    if {$db == {}} {
        set DebugSpef(nameMap) {}
    } else {
        set DebugSpef(nameMap) [$db attr -db getValue "@NAMEMAP"]

        foreach {key value} $DebugSpef(nameMap) {
            set DebugSpef(NAME_MAP:$key) $value
        }
    }
}


# -----------------------------------------------------------------------------
# _filter -
# -----------------------------------------------------------------------------
#
proc DebugSpef:_filter {w dnet pattern} {
    global DebugSpef

    ##
    # Get the database.
    #
    set db [gui database get]

    ##
    # The default foreground color is black.
    # But the help text is displayed in grey.
    #
    set fgColor black
    if {$pattern == $DebugSpef(filterPlaceholder)} {
        set fgColor grey
        set pattern ""
    }

    set matchList {}
    if {$db != {}} {
        ##
        # If the pattern starts with "*" then lookup the parasitic net name
        # in the name map table.
        #
        if {[string index $pattern 0] == "*"} {
            set pattern [string range $pattern 1 end]
            if {[info exists DebugSpef(NAME_MAP:$pattern)]} {
                set pattern $DebugSpef(NAME_MAP:$pattern)
            }
        }

        ##
        # Create a list with all cells matching the given pattern.
        #
        $db foreach parasitic parasitic {
            if {![$dnet exists $parasitic]} {
                continue
            }
            set txt [$db oid oname $parasitic]
            if {$pattern == $txt} {
                lappend matchList $parasitic
                continue
            }
            if {[string match "*${pattern}*" $txt]} {
                lappend matchList $parasitic
            }
        }
    }

    ##
    # Detach all D_NET entries.
    #
    $dnet detach [$dnet children {}]

    ##
    # Re-insert all D_NET items that match the entered pattern.
    #
    foreach key [lsort -dictionary $matchList] {
        $dnet move $key {} end
    }

    ##
    # Set the foreground color of the filter entry widget to red if
    # no cell matches the entered pattern.
    #
    if {($fgColor != "grey") && ([llength $matchList] == 0)} {
        set fgColor red
    }

    ##
    # Configure the foreground color of the filter entry to the color set above.
    #
    $w configure -foreground $fgColor

    ##
    # Always return true (this procedure is called by the validation command).
    #
    return true
}


# -----------------------------------------------------------------------------
# _enterFind - The focus enters the D_NET find field.
# -----------------------------------------------------------------------------
#
proc DebugSpef:_enterFind {w} {
    global DebugSpef

    ##
    # If the default text is displayed then delete this text.
    #
    if {[$w get] == $DebugSpef(filterPlaceholder)} {
        $w delete 0 end
    }
}


# -----------------------------------------------------------------------------
# _leaveFind - The focus leaves the D_NET find field.
# -----------------------------------------------------------------------------
#
proc DebugSpef:_leaveFind {w} {
    global DebugSpef

    ##
    # If no pattern is displayed then enter the default text.
    #
    if {[string trim [$w get]] == ""} {
        $w delete 0 end
        $w insert 0 $DebugSpef(filterPlaceholder)
    }
}


# -----------------------------------------------------------------------------
# _configureText - Configure the text widget.
# -----------------------------------------------------------------------------
#
proc DebugSpef:_configureText {w} {
    set fix [font actual [list [gui settings get "source:fontFamily"]]]
    set fontSize    [gui settings get "source:fontSize"]
    set font_fixed  [eval font create $fix -size $fontSize]
    set font_bold   [eval font create $fix -size $fontSize -weight bold]
    set font_italic [eval font create $fix -size $fontSize -slant  italic]

    $w configure -state disabled
    $w configure -font $font_fixed
    $w configure -background [gui settings get "color:srcBackground"]
    $w configure -wrap none
    $w configure -undo off

    $w tag configure keyword \
        -foreground [gui settings get "color:srcForeground"] \
        -font       $font_bold
    $w tag configure linemark -background yellow
}


# -----------------------------------------------------------------------------
# _reorderD_NETs -
# -----------------------------------------------------------------------------
#
proc DebugSpef:_reorderD_NETs {w what} {
    set parasiticList {}
    foreach item [$w children {}] {
        lappend parasiticList [$w item $item -values]
    }

    set nameHead [lindex [$w heading #0 -text] 0]
    set capHead  [lindex [$w heading  0 -text] 0]

    set up   [gui settings get "texticon:triangleUp"]
    set down [gui settings get "texticon:triangleDown"]

    if {$what == "name"} {
        set index 1
        set style "dictionary"

        if {[string match "*$up" [$w heading #0 -text]]} {
            set order  "decreasing"
            set suffix $down
        } else {
            set order  "increasing"
            set suffix $up
        }
        append nameHead " $suffix"
    } else {
        set index 0
        set style "real"

        if {[string match "*$up" [$w heading 0 -text]]} {
            set order  "decreasing"
            set suffix $down
        } else {
            set order  "increasing"
            set suffix $up
        }
        append capHead " $suffix"
    }

    foreach val [DebugSpef:_sortD_NETs $parasiticList $order $index $style] {
        set item [lindex $val 2]
        $w move $item {} end
    }

    $w heading #0 -text $nameHead
    $w heading  0 -text $capHead
}


# -----------------------------------------------------------------------------
# _sortD_NETs -
# -----------------------------------------------------------------------------
#
proc DebugSpef:_sortD_NETs {
    parasiticList
    {order increasing}
    {index 1}
    {style dictionary}
} {
    return [lsort -index $index -$order -$style $parasiticList]
}


# -----------------------------------------------------------------------------
# _initializeListOfD_NETs - Find all loaded D_NETs and fill the left side list.
# -----------------------------------------------------------------------------
#
proc DebugSpef:_initializeListOfD_NETs {w db} {
    ##
    # Remove all existing entries.
    #
    $w delete [$w children {}]

    ##
    # Return if the database is empty.
    #
    if {$db == {}} {
        return
    }

    ##
    # Identify all loaded parasitic modules.
    #
    set parasiticList {}
    $db foreach parasitic parasitic {
        if {[$db flag $parasitic is parasiticTop]} {
            continue
        }
        set parasiticName [$db oid oname $parasitic]
        set netCap        [$db attr $parasitic getValue "netCap"]
        if {$netCap == ""} {
            set netCap -1
        }
        lappend parasiticList [list $netCap $parasiticName $parasitic]
    }

    ##
    # Sort and add all identified parasitic modules.
    #
    foreach parasiticItem [DebugSpef:_sortD_NETs $parasiticList] {
        set name      [lindex $parasiticItem 1]
        set parasitic [lindex $parasiticItem 2]

        ##
        # Show the parasitic name and net cap value in the displayed table.
        #
        $w insert {} end -id $parasitic -text $name -values $parasiticItem

        ##
        # Add children to show the "CONN", "GROUND_CAP", "CAP" and "RES"
        # sections.
        #
        foreach child {"CONN" "GROUND_CAP" "CAP" "RES" "COUPLING_TO"} {
            $w insert $parasitic end \
                -id   $child:$parasitic \
                -text $child \
                -tags "NO_SELECTION"

            ##
            # Add a dummy entry below each item to show the tree open icon.
            #
            $w insert $child:$parasitic end -text ""
        }
    }
}


# -----------------------------------------------------------------------------
# _loadD_NET - Display the selected D_NET in the text widget.
# -----------------------------------------------------------------------------
#
proc DebugSpef:_loadD_NET {t db parasitic} {
    global DebugSpef

    set DebugSpef(firstLineD_NET) 0
    set DebugSpef(fnameOfD_NET)   ""

    set d_net ""
    set begin 0
    set end   0

    $db spos foreach $parasitic fname begin end {
        ##
        # Read the D_NET from the SPEF file.
        #
        set in [open $fname r]
        seek $in $begin
        set d_net [read $in [expr {$end - $begin}]]
        close $in

        ##
        # Store the Spef filename and the start line of the D_NET.
        #
        set DebugSpef(fnameOfD_NET) $fname
        set DebugSpef(firstLineD_NET) [$db spos lineno -no $fname $begin]

        break
    }

    ##
    # Display the D_NET.
    #
    $t configure -state normal
    $t delete 1.0 end
    eval $t insert end [DebugSpef:_mapNames $db $d_net $begin]
    $t configure -state disabled
}


# -----------------------------------------------------------------------------
# _mapNames -
# -----------------------------------------------------------------------------
#
proc DebugSpef:_mapNames {db d_net begin} {
    global DebugSpef

    set delimiter [string index [$db attr -db getValue "@DELIMITER"] 0]

    if {$DebugSpef(mapNames)} {
        array set _nameMapTable {}
        foreach {key value} $DebugSpef(nameMap) {
            set prefix "*"
            foreach suffix [list { } $delimiter] {
                set _nameMapTable(${prefix}${key}${suffix}) ${value}${suffix}
            }
        }
    }

    set mapped_d_net {}
    foreach line [split $d_net \n] {
        set index 0
        switch -glob -- $line {
            "\*I *" -
            "\*P *" {
                set index 2
            }
            "\*D_NET *" {
                set index 6
            }
            "\*END"  -
            "\*CONN" -
            "\*CAP"  -
            "\*RES"  {
                lappend mapped_d_net $line keyword "\n" {}
                continue
            }
            default {
            }
        }

        if {$index} {
            lappend mapped_d_net [string range $line 0 $index] keyword
            set line [string range $line [incr index] end]
        }

        if {$DebugSpef(mapNames)} {
            set map {}
            set pattern "\\*\[0-9\]+$delimiter|\\*\[0-9\]+ "
            foreach res [regexp -inline -all -- $pattern $line] {
                if {![info exists _nameMapTable($res)]} {
                    continue
                }
                lappend map $res $_nameMapTable($res)
            }
            set line [string map $map $line]
        }
        lappend mapped_d_net $line {}
        lappend mapped_d_net "\n" {}
    }

    return $mapped_d_net
}


# -----------------------------------------------------------------------------
# _fillTree - Callback to interactively fill the "CONN", "GROUND_CAP", "CAP"
#             and "RES" nodes.
# -----------------------------------------------------------------------------
#
proc DebugSpef:_fillTree {w} {
    ##
    # Get the database and return if the database is empty.
    #
    set db [gui database get]
    if {$db == {}} {
        return
    }

    set item [$w focus]

    set parent [$w parent $item]

    if {$parent == {}} {
        return
    }

    set colon [string first ":" $item]
    set parasitic [string range $item $colon+1 end]
    $w delete [$w children $item]
    switch -glob -- $item {
        "CONN:*" {
            set portList {}
            $db foreach port $parasitic port {
                if {[$db oid oname $port] != [$db oid oname $parasitic]} {
                    lappend portList $port
                }
            }
            foreach port [lsort -dictionary $portList] {
                $w insert $item end -id $port -text [$db oid oname $port]
            }
        }
        "GROUND_CAP:*" {
            $db foreach inst $parasitic inst {
                if {[$db primFuncOf $inst] == "CAP"} {
                    $db foreach pin $inst pin {
                        if {[$db isConnected $pin]} {
                            set net [$db connectedNet $pin]
                            if {[$db isPgNet $net]} {
                                $w insert $item end \
                                    -id   $inst \
                                    -text [$db oid oname $inst]
                                break
                            }
                        }
                    }
                }
            }
        }
        "CAP:*" {
            $db foreach inst $parasitic inst {
                if {[$db primFuncOf $inst] == "CAP"} {
                    set groundCap false
                    $db foreach pin $inst pin {
                        if {[$db isConnected $pin]} {
                            set net [$db connectedNet $pin]
                            if {[$db isPgNet $net]} {
                                set groundCap true
                                break
                            }
                        }
                    }
                    if {!$groundCap} {
                        $w insert $item end \
                            -id   $inst \
                            -text [$db oid oname $inst]
                    }
                }
            }
        }
        "COUPLING_TO:*" {
            set delimiter [string index [$db attr -db getValue "@DELIMITER"] 0]
            set couplingTo {}
            set dnetName [$db oid oname $parasitic]
            $db foreach inst $parasitic inst {
                $db foreach pin $inst pin {
                    if {![$db isConnected $pin]} {
                        continue
                    }
                    set net [$db connectedNet $pin]
                    if {[$db isPgNet $net]} {
                        continue
                    }

                    set cnetName [$db oid oname $net]
                    if {![string match "${dnetName}${delimiter}*" $cnetName]} {
                        set pos [string first $delimiter $cnetName]
                        if {$pos < 0} {
                            if {$dnetName == $cnetName} {
                                continue
                            }
                            lappend couplingTo $cnetName
                        } else {
                            incr pos -1
                            lappend couplingTo [string range $cnetName 0 $pos]
                        }
                        break
                    }
                }
            }
            foreach couplingNet [lsort -unique -dictionary $couplingTo] {
                $w insert $item end \
                    -text $couplingNet \
                    -tags "NO_SELECTION"
            }
        }
        "RES:*" {
            $db foreach inst $parasitic inst {
                if {[$db primFuncOf $inst] == "RES"} {
                    $w insert $item end -id $inst -text [$db oid oname $inst]
                }
            }
        }
        default {
        }
    }
}


# -----------------------------------------------------------------------------
# _selectNet -
# -----------------------------------------------------------------------------
#
proc DebugSpef:_selectNet {w spef} {
    ##
    # Get the database and return if the database is empty.
    #
    set db [gui database get]
    if {$db == {}} {
        return
    }

    ##
    # Get the currently selected item and return if no item is selected.
    #
    set item [$w focus]
    if {$item == {}} {
        return
    }

    ##
    # Return if the item has the "NO_SELECTION" tag.
    #
    if {[lsearch -exact [$w item $item -tags] "NO_SELECTION"] >= 0} {
        return
    }

    ##
    # Set the corresponding parasitic module of the selected item.
    #
    set parasitic $item
    set parent [$w parent $item]
    while {$parent != {}} {
        set parasitic $parent
        set parent [$w parent $parent]
    }

    ##
    # Call "fillparasitic" to load the RC network into the database.
    #
    zspef -into $db -fillParasitic [$db oid oname $parasitic]

    ##
    #
    #
    DebugSpef:_updateView $w $spef $parasitic $item
}


# -----------------------------------------------------------------------------
# _toggleNameMap -
# -----------------------------------------------------------------------------
#
proc DebugSpef:_toggleNameMap {w t} {
    global DebugSpef

    set parasitic $DebugSpef(displayedD_NET)
    set DebugSpef(displayedD_NET) ""

    DebugSpef:_updateView $w $t $parasitic $DebugSpef(markedItem)
}


# -----------------------------------------------------------------------------
# _updateView -
# -----------------------------------------------------------------------------
#
proc DebugSpef:_updateView {w t parasitic item} {
    ##
    # Get the database and return if the database is empty.
    #
    set db [gui database get]
    if {$db == {}} {
        return
    }

    DebugSpef:_show $w $t $db $parasitic
    DebugSpef:_mark $w $t $db $parasitic $item
    DebugSpef:_goto $w $t $db $parasitic $item
}


# -----------------------------------------------------------------------------
# _show -
# -----------------------------------------------------------------------------
#
proc DebugSpef:_show {w t db parasitic} {
    global DebugSpef

    if {$DebugSpef(displayedD_NET) == $parasitic} {
        return
    }
    set DebugSpef(displayedD_NET) $parasitic

    gui cone clear

    ##
    # Load the D_NET into the text widget.
    #
    DebugSpef:_loadD_NET $t $db $parasitic

    if {[catch {set signal [$db oid convertFromParasitic $parasitic]} msg]} {
        zmessage print ERR $msg
        return
    }

    parasitic $db createTop "DEBUG_SPEF_TOP_$parasitic" [list $signal]

    ##
    # Load the design signal for this D_NET to the Cone window.
    #
    gui cone load [list $signal]
}


# -----------------------------------------------------------------------------
# _mark -
# -----------------------------------------------------------------------------
#
proc DebugSpef:_mark {w t db parasitic item} {
    global DebugSpef

    set DebugSpef(markedItem) $item

    ##
    # Mark the line for the selected object.
    #
    $t tag remove linemark 1.0 end
    if {$item == {}} {
        set markLine 1
    } else {
        set markLine 0

        $db spos foreach $item -idx fname beg end {
            set lineno [$db spos lineno -no -idx $fname $beg]
            set markLine [expr {1 + ($lineno - $DebugSpef(firstLineD_NET))}]
            break
        }
    }

    $t see $markLine.0
    $t tag add linemark $markLine.0 $markLine.end
}


# -----------------------------------------------------------------------------
# _goto -
# -----------------------------------------------------------------------------
#
proc DebugSpef:_goto {w t db parasitic item} {
    global DebugSpef

    gui window show $DebugSpef(crossprobeTo)

    if {[catch {set signal [$db oid convertFromParasitic $parasitic]}]} {
        return
    }

    if {($item == {}) || ($parasitic == $item)} {
        gui goto [list $signal]
        gui cone zoom fullfit

        ##
        # Display the original D_NET in the Source window.
        #
        gui source gotoLine $DebugSpef(fnameOfD_NET) $DebugSpef(firstLineD_NET)
    } else {
        if {[$db oid type $item] != "port"} {
            return
        }

        set port $item
        set divider   [string index [$db attr -db getValue "@DIVIDER"] 0]
        set pInstPath [concat [$db oid path  $signal] \
                        [list [$db oid oname $signal]]]
        set pInstName [join $pInstPath $divider]
        set pTopMod   [$db search parasitic "DEBUG_SPEF_TOP_$parasitic"]
        set pInst     [$db search inst $pTopMod $pInstName]

        set pin [$db search pin $pInst [$db oid oname $port]]

        if {[$db isConnected $pin]} {
            set connectedPinList {}
            set connectedNet [$db connectedNet $pin]
            $db foreach pin $connectedNet connectedPin {
                if {[$db oid type $connectedPin] eq "pin"} {
                    set inst [$db oid convertTo inst $connectedPin]
                    if {[$db oid isparasiticmodinst $inst]} {
                        continue
                    }
                }
                if {[catch {
                    set designPin [$db oid convertFromParasitic $connectedPin]
                    lappend connectedPinList $designPin
                }]} {
                    continue
                }
            }
            gui goto  $connectedPinList
            gui zoomTo -window Cone $connectedPinList
            gui cone  zoom -1
            gui zoomTo -window Cone $connectedPinList
            gui schem zoom -1
        }
    }
}


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