Question

How can I retrieve a Modelsim signal value in this form x y into tcl so I can process x and y individually?

Currently I have this line in tcl to trace a signal value

when {/currentstate/comp_occupy} {set comp [exa {/currentstate/comp_occupy}]}

This signal is a 2D array in Modelsim which is shown like x y in the widget.

This snippet should trace that variable

trace variable comp w grid_monitor

proc grid_monitor {name arrayindex op} {
    global comp flag_ttt cells
    if {$flag_ttt == 1} {
        puts $comp  
        puts [llength $comp]
        }

}

What I get out of this proc is like this {x y} but I have no idea how I can separate x and y. First I thought that's a list but llength returns 1!

Any idea how I can go about doing this? Or rather, how can I turn it into a proper list?

Thanks

Was it helpful?

Solution

Since we established that the braces were literal braces, you can trim them out. Once done, you can then split to get a list:

proc grid_monitor {name arrayindex op} {
    global comp flag_ttt cells
    if {$flag_ttt == 1} {
        set new_comp [split [string trim $comp "{}"]]
        puts $new_comp  
        puts [llength $new_comp]
    }
}

string trim will trim from $comp the characters contained within the quotes, that is { and }. split will then split the string on space to give a list.

And if you want to assign x and y to the above, you can use lindex or lassign (if you have Tcl8.5 or later):

proc grid_monitor {name arrayindex op} {
    global comp flag_ttt cells
    if {$flag_ttt == 1} {
        set new_comp [split [string trim $comp "{}"]]
        puts $new_comp  
        puts [llength $new_comp]
        set x [lindex $new_comp 0]
        set y [lindex $new_comp 1]
        puts "x is $x and y is $y"
    }
}

Or...

set new_comp [split [string trim $comp "{}"]]
puts $new_comp  
puts [llength $new_comp]
lassign $new_comp x y
puts "x is $x and y is $y"

OTHER TIPS

In Tcl 8.5 the syntax to convert a string containing a valid list is to use the new expand operator:

set comp {*}$comp

It isn't clear if current versions of Modelsim have upgraded beyond 8.4 in which you need to do the same with eval:

eval set comp $comp

This uses the interpreter to do what it does best and avoids manually massaging the string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top