在系统verilog中有一个函数可以返回数组的最高位的个数



我试图获得数组中有效位的数量例如:如果我有一个包含这些位序列的数组:0000年_0101数组的维数是8,我只是想求出最高位的位数在这里是3

下面是一个函数,其行为与文章中描述的一致:

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

好吧,你正在寻找足够的位数来索引一个8b向量。

假设变量(reg, logic)命名为arr[]您可以使用:$clog2($bits(arr))

这里$bits(arr)等于8。那么clog2(8)就等于3。这也考虑到了宽度的非2次方

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

最新更新