【问题标题】:How to properly cast arrays in SystemVerilog?如何在 SystemVerilog 中正确转换数组?
【发布时间】:2014-04-24 17:16:15
【问题描述】:

SystemVerilog 中用于数组和结构的比特流转换似乎不是很安全。

例如,以下铸造问题只会在运行时(可能是模拟的几个小时)被发现:

bit [31:0] bit_queue[$];
logic [31:0] logic_array[5];
for (int i = 0; i < 10; i++) begin
  bit_queue[i] = $urandom;
end

if (catch_issue) begin
  typedef logic [31:0] logic_array_t [5];
  logic_array = logic_array_t'(bit_queue); // <-- ISSUE
end

是否有适当的“安全”程序来进行比特流转换?哪里可以在编译时发现任何问题或在运行时安全处理?还是在这种情况下语言被破坏了?

EDA Playground 上的示例代码:http://www.edaplayground.com/x/2tp

【问题讨论】:

    标签: arrays casting system-verilog bitstream


    【解决方案1】:

    类似于 $cast,由用户在运行时检查兼容性

    if ( $bits(bit_queue) == $bits(logic_array) )
      logic_array = logic_array_t'(bit_queue);
    else
      $error("sizes do not match");
    

    对于涉及动态大小变量的转换,如果没有可能的数组大小会产生有效的赋值,则可能会出现编译时错误,但如果存在可能的大小,则在转换发生之前无法执行该检查,因为大小可以改变直到演员发生的时间。

    【讨论】:

      【解决方案2】:

      因为您在 RHS 上使用队列(暗示可变大小),我认为编译器不能静态检查这一点(因为它不评估 for 循环)。请注意,如果将 typedef 更改为固定大小的解压缩数组 > 5 个条目,则会出现编译时错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-31
        • 2019-01-19
        • 2021-10-30
        • 1970-01-01
        • 2022-11-01
        • 1970-01-01
        相关资源
        最近更新 更多