System Verilog always_latch vs. always_ff

大兔子大兔子 提交于 2019-12-25 06:05:06

问题


Just started learning System Verilog. I am confused about the usage of statements always_ff and always_latch. The former would be used as:

always_ff @ (posedge clk)
begin
    a <= b;
end

while the latter:

always_latch
begin
    a <= b;
end

The first is activated just by the positive edge of the clock and coupled with non-blocking assignment produces a FF.

The always_latch is obviously thought to represent a latch, but then why use a non-blocking assignment? Wouldn't be better using a always_comb with blocking assignments?


回答1:


By using always_latch or always_ff a designers intent to infer a latch or a sequential logic respectively, but if the logic is not correct software tools can warn the designer that the intended hardware logic is not inferred properly.

eg:

always_ff @ (posedge clk or negedge rst) 
begin
  if (!rst)
    a <= '0;
end

For the above code the designer intended to get only a sequential logic and not a latch but a latch would be generated in actual (Any static tool will generate a warning message as "Latch will be inferred for the logic")

Similarly for the below code the designers intent is to infer a hardware latch so tool will(understand your logic better) and won't report it.

    always_latch
    begin
      if (rst)
        a <= b;
    end

Latch is a sequential logic which works on levels of clocks instead of clock edges.

In general best practice is to use Non-blocking assignments for sequential logic and blocking assignments for combinatorial logic which is explained in detail under Section 5.0 Verilog coding guidelines of Nonblocking Assignments in Verilog Synthesis, Coding Styles That Kill!

Guideline #2: When modeling latches, use nonblocking assignments.




回答2:


From IEEE Std 1800-2012, section "9.2.2.3 Latched logic always_latch procedure":

The always_latch construct is identical to the always_comb construct except that software tools should perform additional checks and warn if the behavior in an always_latch construct does not represent latched logic, whereas in an always_comb construct, tools should check and warn if the behavior does not represent combinational logic.

The code example in the Std shows the always_latch using a non-blocking assignment.



来源:https://stackoverflow.com/questions/31165545/system-verilog-always-latch-vs-always-ff

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!