2
votes

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 nonblocking assignment, produces a FF.

The always_latch is obviously thought to represent a latch, but then why use a nonblocking assignment?

Wouldn't it be better using an always_comb with blocking assignments?

2

2 Answers

5
votes

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.

3
votes

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.