问题描述
我刚刚发现 always@(*) 在涉及 function
时不能按预期工作.
I just found that always@(*) does not work as expected when it involve function
.
请参阅下面的简单示例代码.
Please see simple sample code below.
http://www.edaplayground.com/x/J7M
module test;
reg a, b, c;
function reg my_func();
if (b==0) begin
$display("DEBUG 0 @%0t", $time);
return a;
end
else if (b==1) begin
$display("DEBUG 1 @%0t", $time);
return ~a;
end
else begin
$display("DEBUG 2 @%0t", $time);
return 0;
end
endfunction
always @(*) begin
//always_comb begin
c = my_func();
end
initial begin
a = 0; #10;
a = 1; #10;
a=0;
end
endmodule
在模拟中尝试在always@(*)
和always_comb
之间切换.如果我使用 always @(*)
,则不会显示任何内容.但是如果我使用always_comb
,它会显示如下预期的结果:
Try to switch between always @(*)
and always_comb
in simulation. If I use always @(*)
, nothing will display. But if I use always_comb
, it will show expected result as follows:
DEBUG 2 @0
DEBUG 2 @0
DEBUG 2 @10
DEBUG 2 @20
上面的代码只是简单的组合逻辑.为什么 always @(*)
和 always_comb
显示不同的结果?只是模拟问题吗?进一步的实验我注意到它可能与在 always 块中使用 function
有关系.
The above code is just simple combinational logic. Why always @(*)
and always_comb
show different result? Is it just simulation issue? Further experiment I noticed that it may have something to do with usage of function
inside the always block.
推荐答案
always @(*)
块对所有变量的值变化敏感,即由 always 块或我们读取可以说哪些在 always 块内的右侧.
The always @(*)
block is sensitive to change of the values all the variables, that is read by always block or we can say which are at the right side inside the always block.
在您的示例中,在 always 块中没有使用任何变量,因此始终 @(*)
块在这里不起作用.
In your example, there are no any variables used inside always block, so this always @(*)
block will not work here.
根据 SV LRM,
always_comb 对函数内容的变化很敏感,而始终@* 只对 a 的参数变化敏感功能.
即,每当函数内的变量发生变化时, always_comb 都会触发,但在您的情况下, always @(*) 仅在我们在函数中传递参数时才会触发.
i.e., whenever the variables inside a function will change, the always_comb will trigger, but in your case, always @(*) will only trigger if we pass an argument in the function.
所以你可以使用,
always @(*) begin
c = my_func(b);
end
或者你可以使用,
always_comb begin
c = my_func();
end
我已经修改了您的代码.请在下面的链接中找到它.
I have modified your code. Please find it in below link.
http://www.edaplayground.com/x/JiS
这篇关于always_comb 和 always@(*) 之间的行为差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!