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
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
###############################################################################
# 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
#       Create HTML/Text Overview
#   @namespace
#       CreateOverview
#   @section
#       Create Reports
#   @description
#       Create a design overview report of the loaded design either in HTML or
#       ASCII text format.
#       This example can easily be extended to create an other output format.
#
#       The HTML pages contain many typical information about the design, such
#       as: module and submodule names with their instantiations, schematics,
#       input/output/inout ports, clocked elements, all with the places of
#       definition in the source files.
#       Furthermore Operator types and Primitive functions are printed with the
#       number of their occurrences.
#
#       Alternately you can create the same information in text files - without
#       connecting links, of course.
#
#       In addition to the schematics in HTML, linked pdf files are generated;
#       they have better zoom view options than the former.
#
#       DIRECTIONS:
#
#       1. Look through the BASIC OPTIONS in this `createOverview.tcl` file.
#          If necessary, edit and save it.
#
#       2. Have this tcl file together with the included file `htmlFormat.tcl`
#          (or `txtFormat.tcl`) stored in one directory.
#
#       3. Open a verilog design in StarVision PRO.
#
#       4. Click Menu-File-LoadUserware and navigate to `createOverview.tcl`
#          from 2.
#
#       5. The createOverview.tcl script will create the depicted html (or txt)
#          files in the specified $outDir. Start browsing with index.html.
#
#   @test
#       ModuleTest
#   @configuration
#   @files
#       createOverview/createOverview.tcl
#       createOverview/htmlFormat.tcl
#       createOverview/txtFormat.tcl
#   @example
#       demo/rtl/aquarius/aquarius.f
#   @cmdline
#       -F @example[0]
#       -userware @files[0]
#       -userwareEval 'exec rm -rf OVERVIEW'
#   @tag
#       zdb gui report
###############################################################################


# =============================================================================
# Init - Initialize the plugin.
# =============================================================================
#
proc CreateOverview:Init {argc argv} {
    global CreateOverview

    set CreateOverview(formatter:html) \
        [file join [file dirname [info script]] htmlFormat.tcl]
    set CreateOverview(formatter:txt) \
        [file join [file dirname [info script]] txtFormat.tcl]

    gui plugin addConfig CreateOverview previewWidth 300 number \
        "Width of preview images in pixels."
    gui plugin addConfig CreateOverview previewHeight 300 number \
        "Height of preview images in pixels."
    gui plugin addConfig CreateOverview lineSize 80 number \
        "Number of characters per line with outputting text format."

    ##
    # In 'plugin mode' just add two main menu entries.
    #
    gui menu command {"Userware" "Create Overview (HTML)"} \
        [list CreateOverview:StartFromMenu html]
    gui menu command {"Userware" "Create Overview (TXT)"} \
        [list CreateOverview:StartFromMenu txt]

    set CreateOverview(designReadyRegistered) 0

    if {![gui plugin check]} {
        set db [gui database get]

        ##
        # Select formatter (html, txt)
        #
        set CreateOverview(fileType) "html"

        ##
        # Directory where all html or txt output files are printed to.
        #
        if {$argc >= 1} {
            set outDir [lindex $argv 0]
        } else {
            set outDir "OVERVIEW"
        }

        ##
        # Check if the option to quit the GUI is specified.
        #
        if {$argc >= 2} {
            set quitGui [lindex $argv 1]
        } else {
            set quitGui false
        }

        # =====================================================================
        # If we have a database, then start CreateOverview now, else register
        # CreateOverview to be executed after the database is available.
        # =====================================================================
        #
        set startCmd [list CreateOverview:Start $outDir $quitGui]
        if {$db == ""} {
            set CreateOverview(designReadyRegistered) 1
            gui database registerDesignReadyCallback $startCmd
        } else {
            {*}$startCmd
        }
    }
}


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

    ##
    # Undo modifications of the GUI.
    #
    gui menu removeEntry {"Userware" "Create Overview (HTML)"}
    gui menu removeEntry {"Userware" "Create Overview (TXT)"}

    ##
    # Remove callbacks.
    #
    if {$CreateOverview(designReadyRegistered)} {
        gui database removeDesignReadyCallback CreateOverview:Start
    }
}


# =============================================================================
# Start - Main function.
# =============================================================================
#
proc CreateOverview:Start {outDir quitGui} {
    global CreateOverview

    ##
    # Return if the database is empty.
    #
    set db [gui database get]
    if {$db == {}} {
        return
    }
    set CreateOverview(db) $db

    ##
    # Validate output directory.
    #
    if {![CreateOverview:_validateOutputDir $outDir]} {
        return
    }
    set CreateOverview(odir) $outDir

    ##
    # Create directories.
    #
    file mkdir $outDir
    file mkdir [file join $outDir "img"]
    file mkdir [file join $outDir "pdf"]
    file mkdir [file join $outDir "src"]

    ##
    # Source the selected formatter.
    #
    if {![info exists CreateOverview(formatter:$CreateOverview(fileType))]} {
        error "Invalid formatter: $CreateOverview(fileType)"
    }
    set     cmd [list source]
    lappend cmd $CreateOverview(formatter:$CreateOverview(fileType))
    uplevel #0 $cmd

    ##
    # Write the CSS file to define the html appearance
    #
    CreateOverview:PrintCssFile "style.css"

    ##
    # Determine the common path from all source files
    #
    set comPathIndex [CreateOverview:_commonPathIndex]
    set CreateOverview(comPathIndex) $comPathIndex

    ##
    # Note: $comPathIndex-1 components of the path are identical
    #       in all sourcefilenames.
    #
    set commonPath ""
    if {$comPathIndex > 0} {
        $db spos foreachfile fname m {
            ##
            # eval creates the distinctive parameters out of [lrange ...]
            #   which are needed for "file join". The list created by
            #   [lrange ...] is NOT joined by "file join" directly.
            #
            set commonPath [eval file join \
                            [lrange [file split $fname] 0 $comPathIndex-1]]
            break
        }
    }

    ##
    # Open index file for writing
    #
    set index [file join $outDir "index.$CreateOverview(fileType)"]
    set outI [open $index "w"]

    ##
    # Print headline with the design title
    #
    set title [gui window getDesignTitle]
    CreateOverview:PrintHeader $outI "Top Description of $title"
    CreateOverview:PrintPageHeadline $outI "Top Description of $title"

    ##
    # Print 2 lists: top modules and all other modules
    # Add a hyperlink to each module name to jump to its details page  .html
    # Add another hyperlink to the place of module definition        .v.html
    #
    array set arrNameLink {}    ;# holds the pair moduleName / moduleFileName
    set topModuleList {}        ;# structured for PrintUnorderedList
    set moduleList {}           ;# structured for PrintUnorderedList
    set indexChNr 1             ;# sequential chapter number in index.html

    ##
    # Clear all red flags (for use in CreateOverview:_genModuleDetail)
    #
    $db foreach cell curCell {
        $db flag $curCell clear red
    }

    ##
    # Disable "Big Module Warning"
    #
    set bigModuleLimit_setting [gui settings get "bigModuleLimit"]
    gui settings set "bigModuleLimit" 0
    gui settings changed

    ##
    # Run through all modules in the design
    #
    set modCnt 0
    set topCnt 0
    array set _processedModules {}
    $db foreach module curMod {
        ##
        # Set up names for the modules and do not process modules with the
        # same name multiple times.
        #
        set moduleName [CreateOverview:_getModuleName $curMod]
        if {[info exists _processedModules($moduleName)]} {
            continue
        }
        set _processedModules($moduleName) 1

        ##
        # Put the current module $curMod into the schematic window.
        #
        set oid [$db oid searchTreeBased $curMod]
        gui schem setCurrentModule -activate $oid
        gui tree  setCurrentModule           $oid

        ##
        # Make a unique filename for the current module from allowed characters
        #
        set moduleFname [CreateOverview:_makeValidUniqueFname \
            $moduleName $CreateOverview(fileType)]

        ##
        # Print all information about the current module
        #
        CreateOverview:_genModuleDetail $curMod $moduleFname

        ##
        # Gather the   top module names+links into the topModuleList
        # Gather all other module names+links into arrNameLink for later sorting
        #
        if [$db isTop $curMod] {
            incr topCnt

            ##
            # For top modules no sorting is necessary
            # Make module and definition hyperlink
            #
            set     v [list $moduleName $moduleFname.$CreateOverview(fileType)]
            lappend v " --- " ""
            set hLink [CreateOverview:_getDefinitionLink $curMod]
            if {$hLink != ""} {
                lappend v "definition" $hLink
            }
            lappend topModuleList $v {}
        } else {
            ##
            # Here we have non-top modules.
            #
            incr modCnt

            ##
            # Collect data for later sorting.
            #
            set v [list $moduleFname.$CreateOverview(fileType) \
                [CreateOverview:_getDefinitionLink $curMod]]
            set arrNameLink($moduleName) $v
        }
    } ;# We have run through all modules

    ##
    # Sort the module names and build the module list for printing
    #
    set moduleNameList [lsort -dictionary [array names arrNameLink]]
    foreach member $moduleNameList {
        set     v [list $member [lindex $arrNameLink($member) 0]]
        lappend v " --- " ""
        set hLink [lindex $arrNameLink($member) 1]
        if {$hLink != ""} {
            lappend v "definition" $hLink
        }
        lappend moduleList $v {}
    }

    ##
    # Print the 2 lists: top module list and all other modules list
    #
    CreateOverview:PrintUnorderedList $outI \
        "$topCnt Top Modules:" indexChNr $topModuleList
    CreateOverview:PrintUnorderedList $outI \
        "$modCnt Modules:"     indexChNr $moduleList

    ##
    # Print a list of all primitive functions
    # (View over the whole design, not module based)
    #
    # Loop over all primitives in the design,
    # write each occurrence of a primitive function into a hash table
    #
    array set arrPrimFunc {}
    $db foreach primitive curItem {
        set primFunc [$db primFuncOf $curItem]
        if {![info exists arrPrimFunc($primFunc)]} {
            ##
            # this primFunc is new - set count to 1
            #
            set arrPrimFunc($primFunc) 1
        } else {
            ##
            # we have gathered this primFunc already - increment the count
            #
            incr arrPrimFunc($primFunc)
        }
    }

    ##
    # Sort the primitives and print them as an unordered list
    #
    set sortedList {}
    foreach member [lsort -dictionary [array names arrPrimFunc]] {
        set     v [list $member ""]
        lappend v " --- " ""
        lappend v $arrPrimFunc($member) ""
        lappend v " occurrences" ""
        lappend sortedList $v {}
    }
    CreateOverview:PrintUnorderedList $outI \
        "[array size arrPrimFunc] Primitives:" indexChNr $sortedList

    ##
    # Print the sum of all Primitive occurrences below the list
    #
    set sumPrimInst 0
    foreach {key value} [array get arrPrimFunc] {
        set sumPrimInst [expr {$sumPrimInst + $value}]
    }
    if {$sumPrimInst > 0} {
        CreateOverview:PrintLine $outI \
            "In total, there are $sumPrimInst occurrences of Primitives\
            in the design."
    }

    ##
    # Print a list of all source files and convert them into html documents
    #
    # listOfSourceFiles stores pairs {$fname $modTime}
    #   $modTime = time of last modification
    #
    set listOfSourceFiles {}
    set noOfFiles   0
    $db spos foreachfile fname modTime {
        incr noOfFiles
        set ncFname [CreateOverview:_getSourceFname $fname]

        ##
        # conversion into html format:
        #
        set targetfname [file join src $ncFname.$CreateOverview(fileType)]
        CreateOverview:PrintSourceFile $fname $targetfname

        set pretty [file join {*}[lrange [file split $fname] $comPathIndex end]]
        lappend listOfSourceFiles [list $pretty $targetfname $modTime]
    }

    ##
    # Sort over the filenames, make the listToPrint and
    # print the list of source files
    #
    set listToPrint {}
    foreach item [lsort -index 0 -dictionary $listOfSourceFiles] {
        set pretty     [lindex $item 0]
        set link       [lindex $item 1]
        set modTime    [lindex $item 2]

        set     v [list $pretty $link]
        lappend v " last modified on " ""
        lappend v [clock format $modTime] ""
        lappend listToPrint $v {}
    }
    CreateOverview:PrintUnorderedList $outI \
        "$noOfFiles source files, located at $commonPath:" \
        indexChNr $listToPrint

    ##
    # Print footer information and close the index file.
    #
    CreateOverview:PrintFooter $outI $indexChNr
    close $outI

    ##
    # Restore bigModuleLimit setting.
    #
    gui settings set "bigModuleLimit" $bigModuleLimit_setting
    gui settings changed

    ##
    # Quit the GUI if requested.
    #
    if {$quitGui} {
        gui quit
    }
}


# =============================================================================
# StartFromMenu -
# =============================================================================
#
proc CreateOverview:StartFromMenu {format} {
    global CreateOverview

    set CreateOverview(fileType) $format

    set odir [gui window fileDialog chooseDir "Select Output Directory" {}]
    if {$odir == {}} {
        return
    }

    CreateOverview:Start $odir false
}


# -----------------------------------------------------------------------------
# _validateOutputDir - return 'true' if $d is a valid output directory.
# -----------------------------------------------------------------------------
#
proc CreateOverview:_validateOutputDir {d} {
    if {[file exists $d]} {
        if {[file isdirectory $d]} {
            set existing [glob -directory $d -nocomplain -- *]
            if {[llength $existing] > 0} {
                set title "CreateOverview - Continue?"
                set msg "The selected output directory '$d' already exists\
                    and contains some files.\n\nIf you continue, existing files\
                    may be overwritten!\n\nDo you want to continue?"
                set res [tk_messageBox     \
                        -title   $title    \
                        -message $msg      \
                        -icon    "warning" \
                        -type    "yesno"   \
                        -default "no"]
                if {$res eq "no"} {
                    return 0
                }
            }
        } else {
            zmessage print ERR \
                "'$d' already exists, but is not a directory => aborting."
            return 0
        }
    }
    return 1
}


# =============================================================================
# GetCreationDate - Returns the creation date of the output.
#                   Format example:  Wed Mar 26 2014 15:40:35 CET
# =============================================================================
#
proc CreateOverview:GetCreationDate {} {
    set now [clock format [clock seconds] -format "%a %b %d %Y %H:%M:%S  %Z"]
    return "Creation Date: $now"
}


# -----------------------------------------------------------------------------
# _makeValidUniqueFname - Returns a valid unique filename from $rawName?--$cnt?
#                         after replacing forbidden characters in the rawName
#                         with "_"
#                         Checks if $newName?--$cnt?.$ext exists already
#                         in $outDir;
#                         If so, $cnt is incremented until $newName--$cnt.$ext
#                         does not exist.
# -----------------------------------------------------------------------------
#
proc CreateOverview:_makeValidUniqueFname {rawName ext} {
    global CreateOverview

    set odir $CreateOverview(odir)

    ##
    # Replace characters which are not allowed in filenames with '_'
    #
    set newName [zos sanitizeFilename $rawName]

    ##
    # Cut to a max length of (255 - (4x --$cnt 5x .html )) = 246
    #
    if {[string length $newName] > 246} {
        set newName [string range $newName 0 245]
    }

    ##
    # Make filename unique
    #
    while {[file exists [file join $odir $newName.$ext]]} {
        set dashPos [string last "--" $newName]
        set cnt [string range $newName $dashPos+2 end]
        if {($dashPos > -1) && [string is integer -strict $cnt]} {
            ##
            # Increment the filecount if fname ends with --nn (nn=integer)
            #
            set newName [string range $newName 0 $dashPos-1]
            set newName ${newName}--[incr cnt]
        } else {
            ##
            # No filecount exists yet - add filecount "--0"
            #
            set newName ${newName}--0
        }
    }
    return $newName
}


# -----------------------------------------------------------------------------
# _genModuleDetail - Prints one html file with detailed information about the
#                    passed module:
#
#                    - image and PDF files with the schematics
#                    - lists of (output input inout) ports and portBuses
#                    - list of all submodules
#                    - lists of clocked elements, operators and primitive
#                      instances
# -----------------------------------------------------------------------------
#
proc CreateOverview:_genModuleDetail {module moduleFname} {
    global CreateOverview

    set db   $CreateOverview(db)
    set odir $CreateOverview(odir)

    ##
    # Set some file related variables
    #
    set moduleName     [CreateOverview:_getModuleName $module]
    set fileName       [file join $odir $moduleFname.$CreateOverview(fileType)]
    set imgFileNamePre [file join $odir img ${moduleFname}_preview.png]
    set imgFileName    [file join $odir img $moduleFname.png]
    set pdfFileName    [file join $odir pdf $moduleFname.pdf]

    ##
    # Sequential number for numbering the module chapters.  This variable is
    # kept here and handed down to various subordinate procs via the upvar
    # command.
    #
    set modChNr 1

    ##
    # Get schematics and pdf files from the GUI
    #
    set nrOfImages  [gui schem pages]
    set width       [gui plugin getConfigValue CreateOverview previewWidth]
    set height      [gui plugin getConfigValue CreateOverview previewHeight]
    gui export photo $imgFileNamePre Schem ${width}x${height} png
    gui export photo $imgFileName    Schem Letter png
    gui export pdf    $pdfFileName   Schem Letter

    ##
    # Open html file for writing the module description
    #
    set outM [open $fileName "w"]
    CreateOverview:PrintHeader $outM "Module $moduleName"
    CreateOverview:PrintPageHeadline $outM "Description of module: $moduleName"

    ##
    # Print schematics as clickable images
    #
    # Create the image list with quadruples of values:
    # fullviewImageFname previewImageFname previewWidth previewHeight
    #
    set imageList {}
    if {$nrOfImages == 1} {
        lappend imageList \
                $moduleFname.png ${moduleFname}_preview.png \
                $width $height
    } else {
        for {set i 1} {$i <= $nrOfImages} {incr i 1} {
            lappend imageList \
                ${moduleFname}_page$i.png ${moduleFname}_preview_page$i.png \
                $width $height
        }
    }

    ##
    # print schematics as clickable images
    #
    CreateOverview:PrintSchematics $outM $imageList

    ##
    # Print link to the schematic as pdf file
    #
    CreateOverview:PrintPdfLink $outM $moduleFname.pdf

    ##
    # Message callback - print a debug message into debug.log
    #
    zmessage print DBG \
        "Module=$module"

    ##
    # Print all parameters (attributes) of the module.
    #
    set collection {}
    $db attr $module foreach curAttr {
        ##
        # Do not regard 3 internal attributes:
        #
        if {[string match "@*"  $curAttr]} {
            continue
        }
        if {[string match "#*"  $curAttr]} {
            continue
        }
        if {[string match "\$*" $curAttr]} {
            continue
        }
        zmessage print DBG \
            "  curAttr=$curAttr"

        if {[string first "RTL_Name=" $curAttr] != 0} {
            ##
            # We have no way to find reliably the default values. Therefore we
            # print only the names of the parameters. Ex: dw=32  All before "="
            #
            set curAttr [string range $curAttr 0 [string first "=" $curAttr]-1]
            lappend collection $curAttr
        }
    }
    set collLength [llength $collection]
    set listToPrint {}
    foreach m [lsort -dictionary $collection] {
        lappend listToPrint [list $m ""] {}
    }
    CreateOverview:PrintUnorderedList $outM \
        "This module has $collLength parameters:" modChNr $listToPrint

    ##
    # Print all instances of this module.
    #
    set nrOfInstances 0
    set paramsPresent 0
    set instancesWithParams 0

    ##
    # For all non-top modules:
    # Make a modList{} with all appearances of $module in the design,
    #   no matter if they appear with parameters or not
    #
    if {![$db isTop $module]} {
        set modList {}
        set thisModuleName [CreateOverview:_getModuleName $module]
        $db foreach module curMod {
            if {[CreateOverview:_getModuleName $curMod] != $thisModuleName} {
                continue
            }
            lappend modList $curMod
        }

        ##
        # Get a hierarchy separator to replace the spaces
        # ($instantiation needs to be one unspaced string)
        #
        set hiersep [$db oper hiersep get]
        set listToPrint {}

        ##
        # Mark all the instances of $module as red
        #
        foreach m $modList {
            $db flag $m set red
        }

        ##
        # Every $module can appear in multiple top modules.
        # Loop through them - used for output plane 0 (=leftmost)
        #
        $db foreach top curTop {
            array set instParams {} ;# key=$instantiation value=paramList
            array set instLink   {} ;# key=$instantiation value=definitionLink

            ##
            # Loop through all "red" instances of $module
            #
            $db flat foreach instOfCell red $curTop curInst {
                ##
                # $instantiation is used for output plane 1:
                #
                set instantiation [$db oid print $curInst \
                                   -hiersep $hiersep -notype -noroot]
                zmessage print DBG \
                    "      \$instantiation - \$curInst=$instantiation \
                                                                    - $curInst"
                zmessage print DBG \
                    "      \$db moduleOf \$curInst=[$db moduleOf \
                                                                    $curInst]"
                set param [$db oid oname [$db moduleOf $curInst]]

                ##
                # Find the parameters
                #
                set parPos [string first "(" $param]
                set paramList {}
                if {$parPos > -1} {
                    set param  [string range $param $parPos+1 end-1]

                    ##
                    # $param = comma separated string like WIDTH=6,R_VAL=3'b111
                    # Make a list from it - used for output plane 2:
                    #
                    set paramList [split $param ","]
                    incr paramsPresent [llength $paramList]
                    incr instancesWithParams
                }

                ##
                # Sort the parameter sequence
                #
                set paramList [lsort -dictionary $paramList]
                set instParams($instantiation) $paramList

                ##
                # Find the link to the source definition of the instantiation
                #
                set instLink($instantiation) \
                    [CreateOverview:_getDefinitionLink $curInst]
            } ;# end of 'Loop through all "red" instances of $module'

            ##
            # Now we have all data from the $curTop module
            # Make listToPrint plane 0: Top module name
            #
            if {[llength [array names instParams]] > 0} {
                lappend listToPrint [list "In Top: [$db oid oname $curTop]" ""]

                ##
                # Make sublist plane 1: instances
                #
                set v {}

                ##
                # lappend the instantiations in sorted order
                #
                foreach m [lsort -dictionary [array names instParams]] {
                    ##
                    # Append the name of the instance + its definition link
                    #
                    lappend v [list $m $instLink($m)]

                    ##
                    # Count this instance
                    #
                    incr nrOfInstances

                    ##
                    # Make sub-sublist plane 2: parameters of this instance
                    #
                    set vv {}
                    foreach mm $instParams($m) {
                        ##
                        # Add parameters one by one (empty sublist is added
                        # in deepest plane).
                        #
                        lappend vv [list $mm ""] {}
                    }
                    lappend v $vv
                }
                lappend listToPrint $v
            }
            array unset instParams
            array unset instLink
        }

        ##
        # Remove the red flag color
        #
        foreach m $modList {
            $db flag $m clear red
        }
    }

    ##
    # Print the list of all instances with all gathered data
    #
    set title "Current module instantiated $nrOfInstances times "
    if {$paramsPresent >= 1} {
        append title "($instancesWithParams times with parameters) as:"
    } else {
        append title "(without parameters) as:"
    }

    ##
    # This completes 'Print all instances of this module'
    #
    CreateOverview:PrintUnorderedList $outM $title modChNr $listToPrint

    ##
    # Print lists of (output input inout unknown) ports and portBuses
    #
    foreach dir {output input inout unknown} {
        ##
        # upvar needs variable name modChNr without $
        #
        set portList [CreateOverview:_getPorts $module $dir]
        CreateOverview:_printPorts $outM $portList $dir modChNr
    }

    ##
    # For print preparation: Build hash tables for these elements of the
    # current module:
    #   - ClockedElements
    #   - Operators
    #   - subModules and their instances
    #   - PrimInstances
    #
    array set clockedList  {} ;# key=clkElemName   value = unused
    array set arrOperFunc  {} ;# key=operatorName  value = number of appearances
    array set instList     {} ;# key=submoduleName value = listOfInstances
    array set primInstList {} ;# key=primitiveName value = number of appearances
    array set arrPrimFunc  {} ;# key=primFuncName  value = number of appearances

    ##
    # Loop over all instances of $module, gather data about all wanted elements
    #
    $db foreach inst $module curInst {
        set iName [$db oid oname $curInst]
        if {[$db flag [$db oid down $curInst] is clock]} {
            ##
            # It is a clocked element
            #
            set clockedList($iName) $curInst
        } elseif {[$db isOperator $curInst]} {
            ##
            # Gather type and quantity of operator function
            #
            set primFunc [$db primFuncOf $curInst]
            if {![info exists arrOperFunc($primFunc)]} {
                ##
                # This primFunc is new - set count to 1
                #
                set arrOperFunc($primFunc) 1
            } else {
                ##
                # We have gathered this primFunc already - increment the count
                #
                incr arrOperFunc($primFunc)
            }
        } elseif {[$db isModule $curInst]} {
            ##
            # It is a module, not a primitive:
            #
            set modName [$db oid cname $curInst]

            ##
            # Add hash table key if modName is still missing:
            #
            if {![info exists instList($modName)]} {set instList($modName) {}}

            ##
            # .. and add the currentInstance of the module to the value list
            #
            lappend instList($modName) $curInst
        } else {
            ##
            # Everything else is regarded as a primInst
            #
            set primInstList($iName) $curInst

            ##
            # Gather data for Primitive List
            #
            set primFunc [$db primFuncOf $curInst]
            if {![info exists arrPrimFunc($primFunc)]} {
                ##
                # This primFunc is new - set count to 1
                #
                set arrPrimFunc($primFunc) 1
            } else {
                ##
                # We have gathered this primFunc already - increment the count
                #
                incr arrPrimFunc($primFunc)
            }
        }
    }

    ##
    # Print the list of all submodules and their instances
    #
    set moduleNameList [array names instList]
    if {[llength $moduleNameList] > 0} {
        set listToPrint {}
        foreach curMod [lsort -dictionary $moduleNameList]  {
            ##
            # Print to plane 0 (=leftmost)
            #
            lappend listToPrint [list $curMod $curMod.$CreateOverview(fileType)\
                                                    " - instantiated as:" ""]
            set subList {}
            foreach inst $instList($curMod) {
                ##
                # Print the list of instance names and the definition links
                # to plane 1.
                # Empty sublist is added in deepest plane
                #
                set link [CreateOverview:_getDefinitionLink $inst]
                lappend subList [list [$db oid oname $inst] $link] {}
            }
            lappend listToPrint $subList
        }

        ##
        # upvar needs variable name modChNr without $
        #
        CreateOverview:PrintUnorderedList $outM \
            "[llength $moduleNameList] Submodules:" modChNr $listToPrint
    }

    ##
    # Print the list of all clocked elements
    #
    CreateOverview:_printItemList $outM clockedList "Clocked Elements:" modChNr

    ##
    # Print the list of operators
    #
    set listToPrint {}
    set sumOperInst 0
    set sortedKeys [lsort -dictionary [array names arrOperFunc]]
    foreach key $sortedKeys {
        set value $arrOperFunc($key)
        lappend listToPrint [list $key "" " --- $value occurrences" ""] {}
        set sumOperInst [expr {$sumOperInst + $value}]
    }
    CreateOverview:PrintUnorderedList $outM \
        "[array size arrOperFunc] Operator types:" modChNr $listToPrint

    ##
    # upvar needs variable name modChNr without $
    #
    if {$sumOperInst > 0} {
        CreateOverview:PrintLine $outM \
            "In total, there are $sumOperInst occurrences of Operators in this\
            module."
    }

    ##
    # Print the list of primitives
    #
    set listToPrint {}
    set sumPrimInst 0
    set sortedKeys [lsort -dictionary [array names arrPrimFunc]]
    foreach key $sortedKeys {
        set value $arrPrimFunc($key)
        lappend listToPrint [list $key "" " --- $value occurrences" ""] {}
        set sumPrimInst [expr {$sumPrimInst + $value}]
    }
    CreateOverview:PrintUnorderedList $outM \
        "[array size arrPrimFunc] Primitive functions:" modChNr $listToPrint

    if {$sumPrimInst > 0} {
        CreateOverview:PrintLine $outM \
            "In total, there are $sumPrimInst occurrences of Primitives in this\
            module."
    }

    ##
    # Print footer information and
    # close the html file for writing the module description
    #
    CreateOverview:PrintFooter $outM $modChNr
    close $outM
}


# -----------------------------------------------------------------------------
# _getDefinitionLink - Returns the complete hyperlink (the last if there are
#                      multiple) to the position in the source file where
#                      {oid} is defined.
#
# Example of $fname: /usr/local/cevision/demo/rtl/wb_sys/aes/verilog/aes.v
# Example of returned value: aes_verilog_aes.v.html#L_70
# -----------------------------------------------------------------------------
#
proc CreateOverview:_getDefinitionLink {oid} {
    global CreateOverview

    set db $CreateOverview(db)

    ##
    # Find the filename and the begin position of $oid
    #
    set fname ""
    set lineNo -1
    $db spos foreach $oid fname begin end {
        ##
        # Find the line number from $fname and $begin:
        #
        set lineNo [$db spos lineno -no $fname $begin]

        ##
        # In the rare case that the definition of an object is split over
        # multiple files, use the first occurrence only.
        #
        break
    }
    if {($lineNo < 0) || ($fname == "")} {
        zmessage print INF \
            "From CreateOverview:_getDefinitionLink: $db spos function\
            yielded no lineNo ($lineNo) or no fname ($fname) for '$oid'."
        return ""
    }

    set base [CreateOverview:_getSourceFname $fname]
    return [file join src $base.$CreateOverview(fileType)\#L_$lineNo]
}


# -----------------------------------------------------------------------------
# _printItemList - Prints a title and the data given in $array as a sorted
#                  unordered list. Each datum is printed with a link to the
#                  place of its definition.
#                  modChNr = sequential chapter numbering on module page;
#                  In function call: use modChNr without $ (upvar command)
# -----------------------------------------------------------------------------
#
proc CreateOverview:_printItemList {out array title ChNr} {
    upvar 1 $array arr $ChNr modChNr

    set sortedList {}
    foreach member [lsort -dictionary [array names arr]] {
        set fname [CreateOverview:_getDefinitionLink $arr($member)]
        lappend sortedList [list $member $fname] {}
    }
    if {[llength $sortedList] > 0} {
        CreateOverview:PrintUnorderedList $out \
            "[llength [array names arr]] $title" modChNr $sortedList
    }
}


# -----------------------------------------------------------------------------
# _getPorts - Returns a list of oids of the objects with specified direction
#             within module
#             plus a link to the place of definition in the source code.
#             List is empty if no such objects exist.
#             Parameter direction = input | output | inout | unknown
# -----------------------------------------------------------------------------
#
proc CreateOverview:_getPorts {module direction} {
    global CreateOverview

    set db $CreateOverview(db)

    ##
    # Hash table: key   = port name
    #             value = {entire_oid_$curPort $link}
    #
    array set ports {}
    $db foreach oPort $direction $module curPort {
        ##
        # Find the place of definition to make $curPort clickable
        #
        set link [CreateOverview:_getDefinitionLink $curPort]
        set name [$db oid oname $curPort]
        set ports($name) [list $curPort $link]
    }
    set resList {}

    ##
    # Now all hash table entries are sorted by the port names.
    # [array names ports] returns a list of all port names, which is sorted and
    # the corresponding oids + links are appended into the result list.
    #
    foreach name [lsort -dictionary [array names ports]]  {
        lappend resList $ports($name)
    }
    return $resList
}


# -----------------------------------------------------------------------------
# _printPorts - From the oids of ports2print the names are extracted and
#               printed; if it is a port bus, the width of the portbus is
#               appended.
#
#               Parameters:
#               out         = print channel
#               ports2print = list of (sorted) oids to be printed + link
#               direction   = input/output/inout, used for the heading
#               modChNr     = sequential chapter numbering on module page;
#                             use modChNr without $ when called (upvar command)
# -----------------------------------------------------------------------------
#
proc CreateOverview:_printPorts {out ports2print direction ChNr} {
    global CreateOverview

    set db $CreateOverview(db)

    upvar 1 $ChNr modChNr
    if {[llength $ports2print] == 0} {
        return
    }

    set listToPrint {}
    foreach member $ports2print {
        set portOid [lindex $member 0]
        set portLink [lindex $member 1]
        set name [$db oid oname $portOid]
        if {[$db oid type $portOid] == "portBus"} {
            append name "([$db widthOf $portOid])"
        }
        lappend listToPrint [list $name $portLink] {}
    }
    CreateOverview:PrintUnorderedList $out "[llength $ports2print] \
                    [string totitle $direction] ports:" modChNr $listToPrint
}


# -----------------------------------------------------------------------------
# _getModuleName - Returns the RTL module name from the given oid.
# -----------------------------------------------------------------------------
#
proc CreateOverview:_getModuleName {oid} {
    global CreateOverview

    set db $CreateOverview(db)

    set name [$db attr $oid getValue "RTL_Name"]
    if {$name == ""} {
        set name [$db oid oname $oid]
    }
    return $name
}


# -----------------------------------------------------------------------------
# _commonPathIndex - Scans through all sourcefilenames, splits the filepaths
#                    into its components.
#                    Returns the 1st position of the component that is not
#                    common to all files, i. e. the $comPathIndex-1 components
#                    of the path are identical in all sourcefilenames.
#                    $comPathIndex=0 means there is no common path component.
# -----------------------------------------------------------------------------
#
proc CreateOverview:_commonPathIndex {} {
    global CreateOverview

    set db $CreateOverview(db)

    set commonPath true
    set comPathIndex -1
    while {$commonPath} {
        set path ""
        incr comPathIndex
        set fileCount 0
        $db spos foreachfile fname m {
            incr fileCount
            set thisPath [lindex [file split $fname] $comPathIndex]
            if {$path eq ""} {
                set path $thisPath
            }
            if {$path ne $thisPath} {
                set commonPath false
                break
            }
        }
        if {$fileCount == 1} {
            break
        }
    }
    return $comPathIndex
}


# -----------------------------------------------------------------------------
# _getSourceFname - Source files may reside in different folders, eventually
#                   with identical filenames.
#                   To have unique html filenames, all folder names of the not
#                   common path are concatenated in front of the filename,
#                   the / characters are mapped to _
# -----------------------------------------------------------------------------
#
proc CreateOverview:_getSourceFname {fname} {
    global CreateOverview

    set comPathIndex $CreateOverview(comPathIndex)

    set notCommon  [lrange [file split $fname] $comPathIndex end]
    set sourceName [join $notCommon "_"]
    set sourceName [string map {. _} $sourceName]
    return $sourceName
}


# =============================================================================
# Call the initialization procedure.
# =============================================================================
#
CreateOverview:Init $argc $argv