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
###############################################################################
# Copyright (c) 2017-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
#       FSM
#   @namespace
#       FSM
#   @section
#       Read Side Files and Annotate Data
#   @description
#       Extract FSM information copied from the parse tree to the netlist
#       database and display FSMs using a graphical and a table view.
#   @files
#       fsm.tcl
#   @example
#       demo/rtl/wb_sys/conmax_top.f
#   @cmdline
#       -extractfsms 1
#       -pedantic off
#       -F @example[0]
#       -userware @files[0]
#   @tag
#       verilog rtl
###############################################################################


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

    set FSM(name)   "FSM"
    set FSM(widget) ""

    ##
    # Add menu entries.
    #
    gui menu command [list $FSM(name) "Show Tab"] \
        [list FSM:_createWidget]
    gui menu customizeEntry [list $FSM(name) "Show Tab"] \
        [list FSM:_customizeMenu "show"]

    gui menu command [list $FSM(name) "Hide Tab"] \
        [list FSM:_closeWidget]
    gui menu customizeEntry [list $FSM(name) "Hide Tab"] \
        [list FSM:_customizeMenu "hide"]

    FSM:_createWidget
}


# =============================================================================
# Finit - Finalize the plugin.
#         This procedure is automatically called when deactivating the plugin.
# =============================================================================
#
proc FSM:Finit {} {
    global FSM

    FSM:_closeWidget
    gui menu removeEntry [list $FSM(name)]
}


# -----------------------------------------------------------------------------
# _wheelScroll - Wheel scrolling handler.
# -----------------------------------------------------------------------------
#
proc FSM:_wheelScroll {w d} {
    $w yview scroll [expr {-int($d / 120.0)}] units
}


# -----------------------------------------------------------------------------
# _scroll - Setup widget $w with scroll bars $x and $y.
# -----------------------------------------------------------------------------
#
proc FSM:_scroll {w x y} {
    if {$x != {}} {
        scrollbar $x -orient horizontal -command [list $w xview]
        $w configure -xscrollcommand [list $x set]
    }
    if {$y != {}} {
        scrollbar $y -orient vertical -command [list $w yview]
        $w configure -yscrollcommand [list $y set]
        bind $w <MouseWheel> "FSM:_wheelScroll $w  %D;  break"
        bind $w <Button-4>   "FSM:_wheelScroll $w  120; break"
        bind $w <Button-5>   "FSM:_wheelScroll $w -120; break"
    }
}


# -----------------------------------------------------------------------------
# _createWidget - Create the custom FSM widget.
# -----------------------------------------------------------------------------
#
proc FSM:_createWidget {} {
    global FSM

    if {$FSM(widget) ne ""} {
        return
    }

    set w [gui window insertCustomWidget \
        -pluginNamespace "FSM" \
        -destroyCallback FSM:_onWidgetClosed \
        $FSM(name)]
    set FSM(widget) $w

    set FSM(normal_color)    "#d1e3ff"
    set FSM(highlight_color) "#feaacd"

    if {[info commands "dotnew"] eq "dotnew"} {
        set algorithms {dot circo fdp simple-circle}
    } else {
        set algorithms {simple-circle}
    }
    set FSM(algorithm) [lindex $algorithms 0]

    panedwindow $w.panes -orient horizontal

    set left [ttk::frame $w.panes.left]
    button $left.load -text "Reload FSM Data" -command {
        FSM:_loadDataFromDB
    }
    tablelist::tablelist $left.table \
        -columns {0 "Module" 0 "State Variable(s)"} \
        -stripebackground #f0f0f0 \
        -activestyle none \
        -exportselection 0
    set FSM(list) $left.table
    FSM:_scroll $left.table $left.x $left.y
    bind $left.table <<TablelistSelect>> {FSM:_redraw}
    grid $left.load -row 0 -column 0 -columnspan 2 -sticky news
    grid $left.table -row 1 -column 0 -sticky news
    grid $left.x -row 2 -column 0 -sticky ew
    grid $left.y -row 1 -column 1 -sticky ns
    grid columnconfigure $left 0 -weight 1
    grid rowconfigure    $left 1 -weight 1

    set right [ttk::notebook $w.panes.right]

    ttk::frame $right.canvas
    set FSM(canvas) [canvas $right.canvas.c -bg white]
    bind $right.canvas.c <Double-1> [list FSM:_canvasDouble %x %y]
    FSM:_scroll $right.canvas.c $right.canvas.x $right.canvas.y
    grid $right.canvas.c -row 0 -column 0 -sticky news
    grid $right.canvas.x -row 1 -column 0 -sticky ew
    grid $right.canvas.y -row 0 -column 1 -sticky ns

    ttk::frame $right.canvas.layout
    ttk::label $right.canvas.layout.label -text "Layout:"
    ttk::combobox $right.canvas.layout.algorithm \
        -values $algorithms \
        -textvariable FSM(algorithm) \
        -state readonly
    bind $right.canvas.layout.algorithm <<ComboboxSelected>> {
        FSM:_redraw
    }
    grid $right.canvas.layout.label -row 0 -column 0 -sticky news -padx {0 4}
    grid $right.canvas.layout.algorithm -row 0 -column 1 -sticky news
    grid columnconfigure $right.canvas.layout 1 -weight 1
    grid rowconfigure    $right.canvas.layout 0 -weight 1

    grid $right.canvas.layout -row 2 -column 0 -columnspan 2 -sticky news
    grid columnconfigure $right.canvas 0 -weight 1
    grid rowconfigure    $right.canvas 0 -weight 1

    $right add $right.canvas -text "Graph"

    ttk::frame $right.table
    tablelist::tablelist $right.table.t \
        -columns {0 "State" 0 "Next State" 0 "Condition"} \
        -stripebackground #f0f0f0 \
        -activestyle none \
        -exportselection 0
    set FSM(table) $right.table.t
    FSM:_scroll $right.table.t $right.table.x $right.table.y
    grid $right.table.t -row 0 -column 0 -sticky news
    grid $right.table.x -row 1 -column 0 -sticky ew
    grid $right.table.y -row 0 -column 1 -sticky ns
    grid columnconfigure $right.table 0 -weight 1
    grid rowconfigure    $right.table 0 -weight 1
    $right add $right.table -text "Table"

    $w.panes add $left -minsize 300
    $w.panes add $right
    pack $w.panes -expand 1 -fill both

    gui database runAndRegisterChangedCallback FSM:_loadDataFromDB
}


# -----------------------------------------------------------------------------
# _closeWidget - Close the custom FSM widget.
# -----------------------------------------------------------------------------
#
proc FSM:_closeWidget {} {
    global FSM

    if {$FSM(widget) ne ""} {
        gui window removeCustomWidget $FSM(name)
    }
}


# -----------------------------------------------------------------------------
# _onWidgetClosed -
# -----------------------------------------------------------------------------
#
proc FSM:_onWidgetClosed {} {
    global FSM

    if {$FSM(widget) eq ""} {
        return
    }

    set FSM(widget) ""
    gui database removeChangedCallback FSM:_loadDataFromDB
}


# -----------------------------------------------------------------------------
# _customizeMenu -
# -----------------------------------------------------------------------------
#
proc FSM:_customizeMenu {mode menu} {
    global FSM

    switch -exact -- $mode {
        "show" {
            set state [expr {($FSM(widget) eq "") ? "normal" : "disabled"}]
        }
        "hide" {
            set state [expr {($FSM(widget) eq "") ? "disabled" : "normal"}]
        }
        default {
            error "Bad mode: $mode"
        }
    }

    $menu entryconfigure end -state $state
}


# -----------------------------------------------------------------------------
# _findModule - Find module by name.
# -----------------------------------------------------------------------------
#
proc FSM:_findModule {name} {
    set db [gui database get]

    set oid [$db search module $name]
    if {![$db oid isnull $oid]} {
        return $oid
    }

    $db foreach module oid {
        set rtl_name [$db attr $oid getValue "RTL_Name"]
        if {[string equal $name $rtl_name]} {
            return $oid
        }
    }

    return {}
}


# -----------------------------------------------------------------------------
# _findDFFs - Scan $mod for DFF instances corresponding to state variables.
# -----------------------------------------------------------------------------
#
proc FSM:_findDFFs {mod variables} {
    set db [gui database get]

    set dffs {}
    $db foreach inst $mod inst {
        set func [$db primFuncOf $inst]
        if {$func != "DFF"} {
            continue
        }
        set name [$db oid oname $inst]
        foreach variable $variables {
            if {[string match "$variable.*" $name]} {
                lappend dffs $inst
                break
            }
        }
    }

    return $dffs
}


# -----------------------------------------------------------------------------
# _drawArrow - Draw an arrow.
# -----------------------------------------------------------------------------
#
proc FSM:_drawArrow {canvas r x0 y0 x1 y1} {
    set dx  [expr {$x1 - $x0}]
    set dy  [expr {$y1 - $y0}]
    set len [expr {sqrt(($dx * $dx) + ($dy * $dy))}]
    set dx  [expr {$dx / $len}]
    set dy  [expr {$dy / $len}]
    $canvas create line \
        [expr {$x0 + ($r * $dx)}] \
        [expr {$y0 + ($r * $dy)}] \
        [expr {$x1 - ($r * $dx)}] \
        [expr {$y1 - ($r * $dy)}] \
        -fill blue -arrow last
}


# -----------------------------------------------------------------------------
# _drawSelfLoop - Draw a self loop.
# -----------------------------------------------------------------------------
#
proc FSM:_drawSelfLoop {canvas r x y a} {
    set b [expr {3.1415927 / 4}]

    $canvas create line \
        [expr {$x - $r * cos($a - $b)}] \
        [expr {$y + $r * sin($a - $b)}] \
        [expr {$x - 1.5 * $r * cos($a - $b)}] \
        [expr {$y + 1.5 * $r * sin($a - $b)}] \
        [expr {$x - 2 * $r * cos($a)}] \
        [expr {$y + 2 * $r * sin($a)}] \
        [expr {$x - 1.5 * $r * cos($a + $b)}] \
        [expr {$y + 1.5 * $r * sin($a + $b)}] \
        [expr {$x - $r * cos($a + $b)}] \
        [expr {$y + $r * sin($a + $b)}] \
        -arrow last -smooth bezier -fill blue
}


# -----------------------------------------------------------------------------
# _sortNodes - Compute "good" node order. Adapted from
#
# https://pdfs.semanticscholar.org/7b4d/eb64e40f3ba920805143d0c228f4f80c350c.pdf
# -----------------------------------------------------------------------------
#
proc FSM:_sortNodes {nodes transitions} {
    if {($nodes == {}) || ($transitions == {})} {
        return $nodes
    }

    set sorted {}
    array set t  {}
    array set tt {}
    foreach data $transitions {
        set from [lindex $data 0]
        set to   [lindex $data 1]
        if {$from != $to} {
            set t($from,$to) 1
            set t($to,$from) 1
        }
    }

    ##
    # start with the maximum connected node
    #
    set best_n {}
    set best_c 0
    foreach n $nodes {
        set c [llength [array names t $n,*]]
        if {($best_n == {}) || ($c > $best_c)} {
            set best_n $n
            set best_c $c
        }
    }
    lappend sorted $best_n
    set i     [lsearch $nodes $best_n]
    set nodes [lreplace $nodes $i $i]
    foreach x [array names t $best_n,*] {
        set tt($x) 1
    }
    foreach x [array names t *,$best_n] {
        set tt($x) 1
    }
    array unset t $best_n,*
    array unset t *,$best_n

    ##
    # Append nodes with fewest connections to non-appended nodes;
    # prefer nodes with max connections to already appended nodes on ties.
    #
    while {$nodes != {}} {
        set best_n  {}
        set best_c  0
        set best_cc 0
        foreach n $nodes {
            set c  [llength [array names t  $n,*]]
            set cc [llength [array names tt $n,*]]
            if {($best_n == {}) || \
                ($c < $best_c) || \
                (($c == $best_c) && ($cc > $best_cc))} {
                set best_n  $n
                set best_c  $c
                set best_cc $cc
            }
        }
        lappend sorted $best_n
        set i [lsearch $nodes $best_n]
        set nodes [lreplace $nodes $i $i]
        foreach x [array names t $best_n,*] {
            set tt($x) 1
        }
        foreach x [array names t *,$best_n] {
            set tt($x) 1
        }
        array unset t $best_n,*
        array unset t *,$best_n
    }
    return $sorted
}


# -----------------------------------------------------------------------------
# _redrawSimpleCircle - Redraw the FSM using a simple circular layout avoiding
#                       Graphviz.
# -----------------------------------------------------------------------------
#
proc FSM:_redrawSimpleCircle {} {
    global Global FSM

    set db [gui database get]

    set list   $FSM(list)
    set canvas $FSM(canvas)
    set table  $FSM(table)

    set FSM(oids) {}

    $canvas delete "all"
    $table  delete 0 end

    set row  [$list curselection]
    if {$row == {}} {
        return
    }
    set key [$list get $row]

    set module [lindex $key 0]
    set db_mod [FSM:_findModule $module ]
    if {$db_mod != {}} {
        set variables [split [lindex $key 1] /]
        set FSM(oids) [FSM:_findDFFs $db_mod $variables]
        gui goto $FSM(oids)
    }

    set key [join $key :]

    set font $Global(font:tiny)
    set max_width 0
    foreach state $FSM(fsm:$key:states) {
        set w [font measure $font $state]
        if {$w > $max_width} {
            set max_width $w
        }
    }
    incr max_width 16
    if {$max_width < 80} {
        set max_width 80
    }

    array set resets {}
    if {[info exists FSM(fsm:$key:resets)]} {
        foreach data $FSM(fsm:$key:resets) {
            set state [lindex $data 0]
            set resets($state) $state
        }
    }
    set nodes [FSM:_sortNodes $FSM(fsm:$key:states) $FSM(fsm:$key:transitions)]
    array set indices {}
    if {[llength $nodes] > 1} {
        set pi 3.1415927
        set n [llength $nodes]
        set r [expr {$max_width * 0.5}]
        set R [expr {(2.0 * $n * $r) / $pi}]
        set phi [expr {(2.0 * $pi) / $n}]
        set i 0
        foreach node $nodes {
            set indices($node) $i
            set x [expr {-$R * cos($phi * $i)}]
            set y [expr {$R * sin($phi * $i)}]
            if {[info exists resets($node)]} {
                $canvas create oval \
                    [expr {($x - $r) + 5}] \
                    [expr {($y - $r) + 5}] \
                    [expr {($x + $r) - 5}] \
                    [expr {($y + $r) - 5}] \
                    -tags [list 1node$node] \
                    -fill $FSM(normal_color)
                $canvas create oval \
                    [expr {$x - $r}] \
                    [expr {$y - $r}] \
                    [expr {$x + $r}] \
                    [expr {$y + $r}] \
                    -tags [list 1node$node]
            } else {
                $canvas create oval \
                    [expr {$x - $r}] \
                    [expr {$y - $r}] \
                    [expr {$x + $r}] \
                    [expr {$y + $r}] \
                    -tags [list 1node$node] \
                    -fill $FSM(normal_color)
            }
            $canvas create text $x $y \
                -anchor center \
                -text $node \
                -font $font \
                -tags [list 0node$node]
            incr i
        }
        foreach data $FSM(fsm:$key:transitions) {
            set from [lindex $data 0]
            set to   [lindex $data 1]
            set i $indices($from)
            set j $indices($to)
            set a0 [expr {$phi * $i}]
            set x0 [expr {-$R * cos($a0)}]
            set y0 [expr {$R * sin($a0)}]
            if {$i != $j} {
                set a1 [expr {$phi * $j}]
                set x1 [expr {-$R * cos($a1)}]
                set y1 [expr {$R * sin($a1)}]
                FSM:_drawArrow $canvas $r $x0 $y0 $x1 $y1
            } else {
                FSM:_drawSelfLoop $canvas $r $x0 $y0 $a0
            }
        }
    }

    if {[info exists FSM(fsm:$key:resets)]} {
        foreach data $FSM(fsm:$key:resets) {
            set to        [lindex $data 0]
            set condition [lindex $data 1]
            $table insert end [list __RESET__ $to $condition]
        }
    }
    foreach data $FSM(fsm:$key:transitions) {
        set from      [lindex $data 0]
        set to        [lindex $data 1]
        set condition [lindex $data 2]
        $table insert end [list $from $to $condition]
    }

    $canvas configure -scrollregion [$canvas bbox all]
}


# -----------------------------------------------------------------------------
# _redrawGraphviz - Redraw the FSM using Graphviz algorithms.
# -----------------------------------------------------------------------------
#
proc FSM:_redrawGraphviz {} {
    global FSM

    set db [gui database get]

    set list   $FSM(list)
    set canvas $FSM(canvas)
    set table  $FSM(table)

    set FSM(oids) {}

    $canvas delete "all"
    $table  delete 0 end

    set row  [$list curselection]
    if {$row == {}} {
        return
    }
    set key [$list get $row]

    set module [lindex $key 0]
    set db_mod [FSM:_findModule $module ]
    if {$db_mod != {}} {
        set variables [split [lindex $key 1] /]
        set FSM(oids) [FSM:_findDFFs $db_mod $variables]
        gui goto $FSM(oids)
    }

    set key [join $key :]

    set g [dotnew digraph \
        rankdir LR \
        overlap 0 \
    ]

    ##
    # Determine nice fixed node size. It's magic!
    #
    set max_len 8
    foreach state $FSM(fsm:$key:states) {
        set len [string length $state]
        if {$len > $max_len} {
            set max_len $len
        }
    }
    set width [expr {$max_len / 14.0}]

    $g setnodeattribute \
        shape circle \
        style filled \
        fillcolor $FSM(normal_color) \
        color black \
        fixedsize 1 \
        width $width \
        fontsize 8 \
        fontname sans
    $g setedgeattribute \
        color blue

    if {[info exists FSM(fsm:$key:resets)]} {
        foreach data $FSM(fsm:$key:resets) {
            set to        [lindex $data 0]
            set condition [lindex $data 1]
            set to_node   [$g addnode $to shape doublecircle]
            $table insert end [list __RESET__ $to $condition]
        }
    }
    foreach data $FSM(fsm:$key:transitions) {
        set from      [lindex $data 0]
        set to        [lindex $data 1]
        set condition [lindex $data 2]
        set from_node [$g addnode $from]
        set to_node   [$g addnode $to]
        $from_node addedge $to_node
        $table insert end [list $from $to $condition]
    }

    $g layout $FSM(algorithm)
    set commands [$g render $canvas]
    eval $commands
    $canvas configure -scrollregion [$canvas bbox all]
}


# -----------------------------------------------------------------------------
# _redraw - Redraw the FSM using the selected algorithm.
# -----------------------------------------------------------------------------
#
proc FSM:_redraw {} {
    global FSM

    if {$FSM(algorithm) == "simple-circle"} {
        FSM:_redrawSimpleCircle
        return
    } else {
        FSM:_redrawGraphviz
    }
}


# -----------------------------------------------------------------------------
# _findStateOID - Determine the OID corresponding to a FSM state.
# -----------------------------------------------------------------------------
#
proc FSM:_findStateOID {label} {
    global FSM

    set db [gui database get]
    foreach inst $FSM(oids) {
        set name [lindex [split [$db oid oname $inst] .] 1]
        if {[string equal $label $name]} {
            return $inst
        }
        if {[string is integer $name] && [string is integer $label]} {
            set binary [format "%0[string length $name]llb" $label]
            if {[string equal $name $binary]} {
                return $inst
            }
        }
    }
    return {}
}


# -----------------------------------------------------------------------------
# _canvasDouble - Double click handler for the graph canvas.
# -----------------------------------------------------------------------------
#
proc FSM:_canvasDouble {x y} {
    global FSM

    set tags   {}
    set labels {}
    set oids   {}
    set canvas $FSM(canvas)
    set x [$canvas canvasx $x]
    set y [$canvas canvasy $y]
    foreach item [$canvas find overlapping $x $y $x $y] {
        foreach tag [$canvas gettags $item] {
            if {[string first "node" $tag] == 1} {
                set item [string range $tag 1 end]
                lappend tags 1$item
                set label [$canvas itemcget 0$item -text]
                set oid [FSM:_findStateOID $label]
                lappend labels $label
                if {$oid != {}} {
                    lappend oids $oid
                }
            }
        }
    }

    set normal_color    $FSM(normal_color)
    set highlight_color $FSM(highlight_color)
    set tags [lsort -unique $tags]
    foreach item [$canvas find all] {
        ##
        # item is currently not colored -> nothing to do
        #
        set current [$canvas itemcget $item -fill]
        if {($current != $normal_color) && ($current != $highlight_color)} {
            continue
        }

        ##
        # determine item's target color (highlight or normal)
        #
        set target  $normal_color
        foreach tag [$canvas gettags $item] {
            if {[lsearch -exact $tags $tag] >= 0} {
                set target $highlight_color
                break
            }
        }

        ##
        # apply target color
        #
        if {$target != $current} {
            $canvas itemconfigure $item -fill $target
        }
    }

    if {$oids != {}} {
        gui goto $oids
    } elseif {$labels != {}} {
        zmessage print WAR \
            "Cannot find any object for [join [lsort -unique $labels] ,]"
    }
}


# -----------------------------------------------------------------------------
# _loadDataFromDB - Import FSM data from the DB root attributes.
# -----------------------------------------------------------------------------
#
proc FSM:_loadDataFromDB {{db {}}} {
    global FSM

    array unset FSM oids
    array unset FSM fsm:*

    set list   $FSM(list)
    set canvas $FSM(canvas)
    set table  $FSM(table)
    $list   delete 0 end
    $canvas delete "all"
    $table  delete 0 end

    if {$db == {}} {
        set db [gui database get]
    }
    if {$db == {}} {
        zmessage print WAR "There's no database. Please load an RTL design."
        return
    }
    set data [$db attr -db getValue fsm]
    if {$data == {}} {
        zmessage print ERR "There's no FSM data in the current database."
        return
    }
    set data [split $data \n]

    set module ""
    set variables   {}
    set states      {}
    set resets      {}
    set transitions {}

    foreach line $data {
        set line [string trim $line]
        switch -glob -- $line {
            module:* {
                set module [lindex [split $line :] 1]
            }
            "end module" {
                set module {}
            }
            fsm {
                set variables   {}
                set states      {}
                set resets      {}
                set transitions {}
            }
            "end fsm" {
                set id [join $variables /]
                set FSM(fsm:$module:$id:variables)   $variables
                set FSM(fsm:$module:$id:states)      $states
                set FSM(fsm:$module:$id:resets)      $resets
                set FSM(fsm:$module:$id:transitions) $transitions
            }
            variable:* {
                lappend variables [lindex [split $line :] 1]
            }
            state:* {
                lappend states [lindex [split $line :] 1]
            }
            reset:* {
                lappend resets [lrange [split $line :] 1 end]
            }
            transition:* {
                lappend transitions [lrange [split $line :] 1 end]
            }
            "" {}
            default {
                error "BAD LINE: $line"
            }
        }
    }

    foreach key [lsort [array names FSM fsm:*:states]] {
        set key [split $key :]
        set module    [lindex $key 1]
        set variables [lindex $key 2]
        $list insert end [list $module $variables]
    }
}


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