【问题标题】:Retrieving modelsim signals into tcl将 modelsim 信号检索到 tcl
【发布时间】:2014-04-11 09:38:55
【问题描述】:

如何以x y 的形式将 Modelsim 信号值检索到 tcl 中,以便单独处理 x 和 y?

目前我在 tcl 中有这条线来跟踪信号值

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

此信号是 Modelsim 中的二维数组,在小部件中显示为 x y

这个 sn-p 应该跟踪那个变量

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]
        }

}

我从这个过程中得到的是这样的{x y},但我不知道如何将 x 和 y 分开。首先我认为这是一个列表,但 llength 返回 1!

知道我该怎么做吗?或者更确切地说,我怎样才能把它变成一个合适的列表?

谢谢

【问题讨论】:

  • 你的进程中puts $comp 的输出是什么?
  • 例如这个{1 3}。它看起来像一个列表,但似乎都是一个元素。
  • 好吧,如果你在列表中有它,比如[list {1 3}] 并使用puts [lindex [list {1 3}] 0],你会得到{1 3}:这是1 个元素。你需要拆分它。试试puts [llength [split $comp]]
  • 好的,这就证明$comp 不是一个列表,而是一个字符串。我认为最好先使用puts [string trim $comp "{}"],然后使用puts [llength [string trim $comp "{}"]]string trim 将删除字符串左右两边的字符 {}
  • 好吧,结合我们所讨论的内容,您可以将这一行放在puts $comp 的上方以生成一个列表:set comp [split [string trim $comp "{}"]]。如果你想得到 x = 3 和 y = 1,你可以使用lindexlassign(如果你有 Tcl 8.5 或更高版本)。

标签: arrays list tcl modelsim


【解决方案1】:

由于我们确定大括号是字面大括号,您可以将它们修剪掉。完成后,您可以拆分以获取列表:

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 将从$comp 中删除引号中包含的字符,即{}。然后split 会在空间上拆分字符串以给出一个列表。

如果你想将xy分配给上面,你可以使用lindexlassign(如果你有Tcl8.5或更高版本):

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"
    }
}

或者……

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"

【讨论】:

    【解决方案2】:

    在 Tcl 8.5 中,转换包含有效列表的字符串的语法是使用新的扩展运算符:

    set comp {*}$comp
    

    目前尚不清楚 Modelsim 的当前版本是否已升级到 8.4 以上,您需要对 eval 执行相同操作:

    eval set comp $comp
    

    这使用解释器来做它最擅长的事情,并避免手动按摩字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-28
      • 1970-01-01
      相关资源
      最近更新 更多