系统Verilog中是否有一个函数可以返回数组的最重要位数

发布于 2025-02-03 04:05:11 字数 89 浏览 5 评论 0原文

IAM试图在数组中获取有效钻头的数量 例如: 如果我有一个包含这些位序列的数组: 0000_0101 阵列的尺寸为8,我只想获得最重要的位数的方法,在这种情况下为3

iam trying to get the number of the effective bits in an array
for example:
if i have an array that contains these sequence of bits:
0000_0101
the dimension of the array is 8 , i just want way to get the number of the most significant bits which are 3 in this case

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朱染 2025-02-10 04:05:11

这是一个如帖子中所述的函数:

module tb ();
  
  reg [7:0] temp;
  reg [7:0] myvector;
  
  function automatic int returns_location(input reg [7:0] vector);
    integer val = 0;
    for(int i =0;i < $size(vector);i++)
        if(vector[i]== 1'b1)
          val = i;
    return (val + 1);
  endfunction
  
  initial
    begin
      // From the post
      temp = 8'b0000_0101;
      $display("---- location of most sig 1 = %0d ----",returns_location(temp));
      #1;
      // Another example, shift 1 left
      temp = 8'b0000_1101;
      $display("---- location of most sig 1 = %0d ----",returns_location(temp));
      #1
      // Another example, shift 2 left
      temp = 8'b0001_1101;
      $display("---- location of most sig 1 = %0d ----",returns_location(temp));
      #1      
      $finish;
    end
    
endmodule    

并产生结果:

# ---- location of most sig 1 = 3 ----
# ---- location of most sig 1 = 4 ----
# ---- location of most sig 1 = 5 ----

Here is a function that behaves as described in the post:

module tb ();
  
  reg [7:0] temp;
  reg [7:0] myvector;
  
  function automatic int returns_location(input reg [7:0] vector);
    integer val = 0;
    for(int i =0;i < $size(vector);i++)
        if(vector[i]== 1'b1)
          val = i;
    return (val + 1);
  endfunction
  
  initial
    begin
      // From the post
      temp = 8'b0000_0101;
      $display("---- location of most sig 1 = %0d ----",returns_location(temp));
      #1;
      // Another example, shift 1 left
      temp = 8'b0000_1101;
      $display("---- location of most sig 1 = %0d ----",returns_location(temp));
      #1
      // Another example, shift 2 left
      temp = 8'b0001_1101;
      $display("---- location of most sig 1 = %0d ----",returns_location(temp));
      #1      
      $finish;
    end
    
endmodule    

And produces the results:

# ---- location of most sig 1 = 3 ----
# ---- location of most sig 1 = 4 ----
# ---- location of most sig 1 = 5 ----
荒芜了季节 2025-02-10 04:05:11

好吧,您正在寻找足以将其索引到8B向量的位。

假设您的变量(REG,逻辑)被命名为ARR []
您可以使用: $ clog2($ bit(arr))

这里$ bits(arr)为8。
然后clog2(8)将是3。这也需要2个宽度的非功率。

编辑:从clogb2()更改为clog2()。

Well, you're looking for number of bits enough to index into an 8b vector.

Assuming your variable (reg, logic) is named arr[]
You could use: $clog2($bits(arr))

Here $bits(arr) would be 8.
Then clog2(8) will be 3. This takes care of non-power of 2 widths also.

Edit: changed from clogb2() to clog2().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文