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
###############################################################################
# Copyright (c) 2016-2025 by Altair Engineering, Inc.
# All rights reserved.
#
# Altair Engineering, Inc. makes this software available as part of the Vision
# tool platform.  As long as you are a licensee of the Vision tool platform
# you may make copies of the software and modify it to be used within the
# Vision tool platform, but you must include all of this notice on any copy.
# Redistribution without written permission to any third party, with or
# without modification, is not permitted.
# Altair Engineering, Inc. does not warrant that this software is error free
# or fit for any purpose.  Altair Engineering, Inc. disclaims any liability for
# all claims, expenses, losses, damages and costs any user may incur as a
# result of using, copying or modifying the software.
# =============================================================================
#   @userware
#       Example for C Customdb
#   @section
#       C Level Examples
#   @description
#       C implementation of the customdb Userware.
#       This example script demonstrates how to use the customdb extension
#       compiled as a shared library in the *Vision GUI as well as in the
#       starsh.
#   @files
#       wdb/customdb.c
#       wdb/customdb.tcl
#   @tag
#       wdb
###############################################################################


##
# Load the shared library customdb.
# This will provide the new Tcl command customdb
#
load [file join . customdb[info sharedlibextension]] Customdb


##
# Get the name of the running executable.
#
set exeName [file tail [info nameofexecutable]]


# -----------------------------------------------------------------------------
# expect - for expected value
# -----------------------------------------------------------------------------
#
proc expect {no value expected} {
    if {![string equal $value $expected]} {
        error "$no: Expected '$expected' but got '$value'"
    }
}


# -----------------------------------------------------------------------------
# check - customdb values
# -----------------------------------------------------------------------------
#
proc check {wdb} {
    ##
    # Only for testing purposes we read some scopes/variables.
    #
    set readerOptions {-dummySwitch -dummyValue 2}
    set rdr [$wdb reader $readerOptions]

    expect "options" [$rdr options] $readerOptions

    ##
    # get header information
    #
    set version   [$rdr version]
    set date      [$rdr date]
    set timescale [$rdr timescale]
    expect "version"   $version   "Customdb Version"
    expect "date"      $date      "Customdb Date"
    expect "timescale" $timescale "1ns"

    ##
    # get first scope
    #
    set downScpId [$rdr scopedown]
    set type [$rdr scopetype $downScpId]
    set name [$rdr scopename $downScpId]
    expect "DownId"    $downScpId "Scp0x0"
    expect "Scopetype" $type      "module"
    expect "Scopename" $name      "dummyscope1"

    ##
    # get all top scopes
    #
    set scpLst {}
    $rdr foreach scope scpId {
        set type [$rdr scopetype $scpId]
        set name [$rdr scopename $scpId]
        lappend scpLst [list $scpId $type $name]
    }
    expect "Scopes" $scpLst [list    \
        {Scp0x0 module dummyscope1}  \
        {Scp0x1 module dummyscope2}  \
    ]

    ##
    # search for a scope(s)
    #
    set scpId [$rdr scopefind "dummyscope1"]
    set name  [$rdr scopename $scpId]
    expect "ScopeFind" $name "dummyscope1"
    set scpId1 [$rdr scopefind $scpId "dummyscope1a"]
    set name  [$rdr scopename $scpId1]
    expect "ScopeFind" $name "dummyscope1a"


    ##
    # get all subscopes of a scope (here "dummyscope1")
    #
    set scpLst {}
    $rdr foreach scope $scpId subId {
        set name [$rdr scopename     $subId]
        set path [$rdr scopenamepath $subId]
        lappend scpLst [list $subId $name $path]
    }
    expect "SubScopes" $scpLst [list    \
        {Scp0x2 dummyscope1a {dummyscope1 dummyscope1a}}  \
        {Scp0x3 dummyscope1b {dummyscope1 dummyscope1b}}  \
    ]


    ##
    # get all scopes (recursive)
    #
    proc getScopes {rdr scpId} {
        set scpLst {}
        $rdr foreach scope $scpId subId {
            set name [$rdr scopename $subId]
            lappend scpLst $subId $name
            set subLst [getScopes $rdr $subId]
            lappend scpLst {*}$subLst
        }
        return $scpLst
    }
    set scpLst [getScopes $rdr {}]
    expect "Scopes" $scpLst [list    \
        Scp0x0 dummyscope1  \
        Scp0x2 dummyscope1a \
        Scp0x3 dummyscope1b \
        Scp0x1 dummyscope2  \
        Scp0x4 dummyscope2a \
        Scp0x5 dummyscope2b \
    ]

    ##
    # get all variables
    #
    set varLst {}
    $rdr foreach variable varId {
        set scpId   [$rdr varscope $varId]
        set type    [$rdr vartype  $varId]
        set name    [$rdr varname  $varId]
        set width   [$rdr varwidth $varId]
        set sigNo   [$rdr signo    $varId]
        set valType [$rdr valtype  $varId]
        lappend varLst [list $scpId $varId $type $name $width $sigNo $valType]
    }
    expect "Variables" $varLst [list    \
        {Scp0x0 Var0x0 wire dummyvar1 1 0 scalar} \
        {Scp0x0 Var0x1 wire dummyvar2 1 1 scalar} \
        {Scp0x1 Var0x2 wire dummyvar3 1 1 scalar} \
        {Scp0x1 Var0x3 wire dummyvar4 4 2 vector} \
        {Scp0x2 Var0x4 wire dummyvar5 1 1 scalar} \
        {Scp0x3 Var0x5 wire dummyvar6 1 1 scalar} \
        {Scp0x4 Var0x6 wire dummyvar7 1 1 scalar} \
        {Scp0x5 Var0x7 wire dummyvar8 1 1 scalar} \
    ]

    ##
    # get all variables per scope
    #
    set varLst {}
    foreach {scpId scpName} $scpLst {
        $rdr foreach variable $scpId varId {
            set type  [$rdr vartype  $varId]
            set name  [$rdr varname  $varId]
            set width [$rdr varwidth $varId]
            lappend varLst [list $scpId $varId $type $name $width]
        }
    }
    expect "Variables Per Scope" $varLst [list    \
        {Scp0x0 Var0x0 wire dummyvar1 1} \
        {Scp0x0 Var0x1 wire dummyvar2 1} \
        {Scp0x2 Var0x4 wire dummyvar5 1} \
        {Scp0x3 Var0x5 wire dummyvar6 1} \
        {Scp0x1 Var0x2 wire dummyvar3 1} \
        {Scp0x1 Var0x3 wire dummyvar4 4} \
        {Scp0x4 Var0x6 wire dummyvar7 1} \
        {Scp0x5 Var0x7 wire dummyvar8 1} \
    ]

    ##
    # get one vector to test member functions
    #
    set vectorVarId {}
    $rdr foreach variable varId {
        set width [$rdr varwidth $varId]
        if {$width > 1} {
            set vectorVarId $varId
            break
        }
    }

    ##
    # test some member functions
    #
    set memLst {}
    $rdr foreach member $vectorVarId memId {
        set name [$rdr varname $memId]
        set idx  [$rdr varmem  $memId]
        set mem  [$rdr varbit  $memId]
        lappend memLst [list $name $idx $mem]
    }
    expect "Members" $memLst [list    \
        {{dummyvar4[3]} 3 3} \
        {{dummyvar4[2]} 2 2} \
        {{dummyvar4[1]} 1 1} \
        {{dummyvar4[0]} 0 0} \
    ]

    ##
    # test hide and dir
    #
    set varLst {}
    $rdr foreach variable varId {
        set hide  [$rdr varhide  $varId]
        set dir   [$rdr vardir   $varId]
        expect "hide" $hide 0
        expect "dir"  $dir  none

        $rdr varhideset $varId 1
        $rdr vardirset  $varId in

        set hide  [$rdr varhide  $varId]
        set dir   [$rdr vardir   $varId]
        expect "hide" $hide 1
        expect "dir"  $dir  in

        $rdr varhideset $varId 0
        $rdr vardirset  $varId none
    }

    ##
    # test time
    #
    set first [$rdr firsttime]
    set last  [$rdr lasttime]
    expect "firsttime" $first 0
    expect "lasttime"  $last  1000

    ##
    # test next time changes
    #
    set res {}
    $rdr foreach variable varId {
        set name [$rdr varname $varId]
        set changes {}
        set t [$rdr firsttime]
        while {$t != {}} {
            set val [$rdr value $t $varId]
            lappend changes [list $t $val]
            set t [$rdr nexttime $t [list $varId]]
        }
        lappend res [list $name $changes]
    }

    expect "NextTime/Value" $res [list \
        [list dummyvar1       \
            [list {0 x}       \
                  {100 1}     \
                  {200 0}     \
                  {300 1}     \
                  {400 z}     \
                  {500 1}     \
                  {600 0}     \
                  {700 1}     \
                  {800 0}     \
            ]                 \
        ]                     \
        [list dummyvar2       \
            [list {0 0}       \
                  {50 1}      \
                  {250 0}     \
                  {350 1}     \
            ]                 \
        ]                     \
        [list dummyvar3       \
            [list {0 0}       \
                  {50 1}      \
                  {250 0}     \
                  {350 1}     \
            ]                 \
        ]                     \
        [list dummyvar4       \
            [list {0 0000}    \
                  {300 0101}  \
                  {600 1010}  \
                  {900 1111}  \
            ]                 \
        ]                     \
        [list dummyvar5       \
            [list {0 0}       \
                  {50 1}      \
                  {250 0}     \
                  {350 1}     \
            ]                 \
        ]                     \
        [list dummyvar6       \
            [list {0 0}       \
                  {50 1}      \
                  {250 0}     \
                  {350 1}     \
            ]                 \
        ]                     \
        [list dummyvar7       \
            [list {0 0}       \
                  {50 1}      \
                  {250 0}     \
                  {350 1}     \
            ]                 \
        ]                     \
        [list dummyvar8       \
            [list {0 0}       \
                  {50 1}      \
                  {250 0}     \
                  {350 1}     \
            ]                 \
        ]                     \
    ]

    ##
    # test prev time changes
    #
    set res {}
    $rdr foreach variable varId {
        set name [$rdr varname $varId]
        set changes {}
        set t [$rdr lasttime]
        while {true} {
            set t [$rdr prevtime $t [list $varId]]
            if {$t == {}} {
                break
            }
            set val [$rdr value $t $varId]
            lappend changes [list $t $val]
        }
        lappend res [list $name $changes]
    }

    expect "PrevTime/Value" $res [list \
        [list dummyvar1       \
            [list {800 0}     \
                  {700 1}     \
                  {600 0}     \
                  {500 1}     \
                  {400 z}     \
                  {300 1}     \
                  {200 0}     \
                  {100 1}     \
                    {0 x}     \
            ]                 \
        ]                     \
        [list dummyvar2       \
            [list {350 1}     \
                  {250 0}     \
                  {50 1}      \
                  {0 0}       \
            ]                 \
        ]                     \
        [list dummyvar3       \
            [list {350 1}     \
                  {250 0}     \
                  {50 1}      \
                  {0 0}       \
            ]                 \
        ]                     \
        [list dummyvar4       \
            [list {900 1111}  \
                  {600 1010}  \
                  {300 0101}  \
                  {0 0000}    \
            ]                 \
        ]                     \
        [list dummyvar5       \
            [list {350 1}     \
                  {250 0}     \
                  {50 1}      \
                  {0 0}       \
            ]                 \
        ]                     \
        [list dummyvar6       \
            [list {350 1}     \
                  {250 0}     \
                  {50 1}      \
                  {0 0}       \
            ]                 \
        ]                     \
        [list dummyvar7       \
            [list {350 1}     \
                  {250 0}     \
                  {50 1}      \
                  {0 0}       \
            ]                 \
        ]                     \
        [list dummyvar8       \
            [list {350 1}     \
                  {250 0}     \
                  {50 1}      \
                  {0 0}       \
            ]                 \
        ]                     \
    ]

    ##
    # test next value
    #
    set refList {}
    $rdr foreach variable varId {
        set name [$rdr varname $varId]
        switch -- $name {
            "dummyvar1" {
                lappend refList $varId "z" 400
            }
            "dummyvar2" {
                lappend refList $varId "1" 50
            }
            "dummyvar3" {
                lappend refList $varId "1" 50
            }
            "dummyvar4" {
                lappend refList $varId "1010" 600
            }
            "dummyvar5" {
                lappend refList $varId "1" 50
            }
            "dummyvar6" {
                lappend refList $varId "1" 50
            }
            "dummyvar7" {
                lappend refList $varId "1" 50
            }
            "dummyvar8" {
                lappend refList $varId "1" 50
            }
        }
    }
    set start [$rdr firsttime]
    foreach {varId value ref} $refList {
        set t [$rdr nextvalue $start [list $varId] $value]
        if {$t != $ref} {
            error "nextvalue $start $value returned '$t' instead of '$ref'"
        }
    }

    ##
    # test prev value.
    #
    set refList {}
    $rdr foreach variable varId {
        set name [$rdr varname $varId]
        switch -- $name {
            "dummyvar1" {
                lappend refList $varId "z" 400
            }
            "dummyvar2" {
                lappend refList $varId "0" 250
            }
            "dummyvar3" {
                lappend refList $varId "0" 250
            }
            "dummyvar4" {
                lappend refList $varId "1010" 600
            }
            "dummyvar5" {
                lappend refList $varId "0" 250
            }
            "dummyvar6" {
                lappend refList $varId "0" 250
            }
            "dummyvar7" {
                lappend refList $varId "0" 250
            }
            "dummyvar8" {
                lappend refList $varId "0" 250
            }
        }
    }
    set start [$rdr lasttime]
    foreach {varId value ref} $refList {
        set t [$rdr prevvalue $start [list $varId] $value]
        if {$t != $ref} {
            error "prevvalue $start $value returned '$t' instead of '$ref'"
        }
    }

    ##
    # test valarray
    #
    set start [$rdr firsttime]
    set end   [$rdr lasttime]
    set varIstList {}
    $rdr foreach variable varId {
        lappend varIstList $varId
    }
    set pixelCnt 80
    set res [$rdr valarray $start $end $varIstList $pixelCnt]
    set cnt 0
    foreach line $res {
        set len [string length $line]
        if {$len != $pixelCnt} {
            error "valarray has wrong length in line $cnt ($len != $pixelCnt)"
        }
        incr cnt
    }
    if {$cnt != [llength $varIstList]} {
        error "valarray returned wrong number of lines"
    }

    ##
    # free reader
    #
    $rdr free
}


# =============================================================================
# If this script runs in starsh then the following code will be used.
# =============================================================================
#
if {[string equal -length 6 $exeName "starsh"]} {
    ##
    # Check for wrong arguments.
    #
    if {($argc < 1) || ($argc > 3)} {
        error "Wrong args.\nUsage: $exeName $argv0 <fname> ?-debug?"
    }

    ##
    # Get arguments.
    #
    set fname [lindex $argv 0]
    set debug false
    if {[lindex $argv 1] == "-debug"} {
        set debug true
    }

    ##
    # Install message handler for debug messages
    #
    if {$debug} {
        proc msgCB {time type message file line tclScript} {
            switch -- $type {
                "x" {}
                default {
                    puts "[zmessage expandType $type] $message"
                }
            }
        }
        zmessage init
        zmessage setCallback msgCB
        zmessage enableDebug "wdb/calls"
    }

    ##
    # Call the customdb to open database.
    #
    set wdb [wdb customdb $fname [list "-debug" "-endTime" "1000"]]

    ##
    # Only for testing purposes we read some scopes/variables.
    #
    check $wdb

    ##
    # Close the database.
    #
    $wdb close


# =============================================================================
# If this script runs in a *Vision GUI then the following code will be used.
# =============================================================================
#
} else {
    ##
    # Set custom database options and open the database.
    #
    gui wave customDatabaseOptions [list "-debug" "-endTime" "1000"]
    set wdb [gui wave open -databaseFormat "Custom" "filename1"]

    ##
    # Only for testing purposes we read some scopes/variables.
    #
    check $wdb

    ##
    # exit gui if running in non-interactive mode
    #
    if {"-interactive" ni $argv} {
        gui quit
    }
}