Verilog 中的 FSM 状态变化
我看到以下用于在 Verilog 模块中进行状态更改:
state <= 2'b10;
state <= #1 IDLE;
为什么使用 <=而不仅仅是=?使用#1 的目的是什么?有什么区别吗?
以下是 FSM 的一些 Verilog 代码,显示了正在使用的第一个代码。如果换成第二个,效果会一样吗?
module fsm( clk, rst, inp, outp);
input clk, rst, inp;
output outp;
reg [1:0] state;
reg outp;
always @( posedge clk, posedge rst )
begin
if( rst )
state <= 2'b00;
else
begin
case( state )
2'b00:
begin
if( inp ) state <= 2'b01;
else state <= 2'b10;
end
2'b01:
begin
if( inp ) state <= 2'b11;
else state <= 2'b10;
end
2'b10:
begin
if( inp ) state <= 2'b01;
else state <= 2'b11;
end
2'b11:
begin
if( inp ) state <= 2'b01;
else state <= 2'b10;
end
endcase
end
end
I have seen the following used to make state changes in Verilog modules:
state <= 2'b10;
state <= #1 IDLE;
Why is <= used and not just =? What is the purpose of using #1? Does it make a difference?
Here is some Verilog code for a FSM that shows the first one being used. Would it work the same if it was replaced with the second?
module fsm( clk, rst, inp, outp);
input clk, rst, inp;
output outp;
reg [1:0] state;
reg outp;
always @( posedge clk, posedge rst )
begin
if( rst )
state <= 2'b00;
else
begin
case( state )
2'b00:
begin
if( inp ) state <= 2'b01;
else state <= 2'b10;
end
2'b01:
begin
if( inp ) state <= 2'b11;
else state <= 2'b10;
end
2'b10:
begin
if( inp ) state <= 2'b01;
else state <= 2'b11;
end
2'b11:
begin
if( inp ) state <= 2'b01;
else state <= 2'b10;
end
endcase
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在像您这样的顺序逻辑
always
块中,最好使用非阻塞赋值 (<=
) 而不是阻塞赋值 (=
)。模拟将更好地代表实际产生的逻辑。在纯 RTL Verilog 代码中,如果您对所有顺序逻辑使用非阻塞分配,则没有理由使用
#1
延迟。我还看到其他人使用像这样的
#
延迟。有时这是由于在同一仿真中混合了 RTL 和门网表。其他时候这样做是为了弥补糟糕的建模。如果可以的话,您应该避免在 RTL 代码中使用延迟。参见:
Verilog 综合、编码中的非阻塞赋值致命的样式!
另外,最好使用
参数
来命名每个状态。如果状态被命名为IDLE
而不是2'b10
则更有意义。In a sequential logic
always
block like yours, it is better to use non-blocking assignments (<=
) instead of blocking assignments (=
). Simulations will be better representative of the actual resulting logic.In pure RTL Verilog code, if you are using non-blocking assignments for all sequential logic, there should be no reason to use
#1
delays.I have also seen others use
#
delays like this. Sometimes it is due to mixing RTL and gate netlists in the same simulation. Other times it is done to compensate for poor modeling. You should avoid using delays in RTL code if you can.See also:
Nonblocking Assignments in Verilog Synthesis, Coding Styles That Kill!
Also, it is better to use a
parameter
to name each of your states. It is much more meaningful if a state is namedIDLE
instead of2'b10
.Verilog 本质上是非确定性的。这意味着一般来说,模拟有多种可能的结果,这些结果都是不同的,但根据标准是有效的。
为了避免时钟always 块的非确定性行为,您必须对其他块中使用的变量(非局部变量)使用非阻塞赋值。有关更详细的分析,请参阅我关于该主题的博客文章:
http://www.sigasi .com/content/verilogs-major-flaw
综合工具通常接受时钟always块中非局部变量的阻塞赋值,但这实际上是一个错误,因为它们无法保证综合逻辑的行为就像模型一样(因为这样的模型是不确定的)。
与您从许多其他人那里听到的内容(例如 Cliff Cummings 的热门论文)相反,不需要只对本地变量使用非阻塞赋值。对于局部变量来说,阻塞赋值是完全可以接受的。这是相关的,因为它允许使用纯“变量”语义(如在编程语言中)进行内部建模。
在纯 RTL 风格的建模中,#1 延迟只是开销。
Verilog is inherently non-deterministic. This means that in general, there are multiple possible outcomes of a simulation, which are all different, but valid according to the standard.
To avoid non-deterministic behavior from clocked always blocks, you have to use non-blocking assignments for those variables that are used in other blocks (non-local variables). For a more detailed analysis, see my blog post on the subject:
http://www.sigasi.com/content/verilogs-major-flaw
Synthesis tools typically accept blocking assignments for non-local variables in clocked always blocks, but this is really an error as there is no way they can guarantee that the synthesized logic will behave like the model (as such a model is non-deterministic).
In contrast to what you will hear from many others, such a Cliff Cummings' popular papers, there is no need to only use non-blocking assignments for local variables. Blocking assignments are perfectly acceptable for local variables. This is relevant, because it permits to use pure "variable" semantics (as in programming languages) for internal modeling.
In pure RTL style modeling, #1 delays are just overhead.