Skip to main content

SystemVerilog always_ff vs always_comb vs always_latch – Safe RTL Coding Explained

· loading · loading · ·
Hardware Design SystemVerilog Always_ff Always_comb RTL Design Sequential Logic Latches
Hardware Design
Axolot Logic
Author
Axolot Logic
Digital Design Engineer
Table of Contents
SystemVerilog Design Series - This article is part of a series.
Part 8: This Article

🔁 always_ff vs always @(posedge clk)
#

✅ Why always_ff is Superior
#

SystemVerilog’s always_ff is a strictly enforced sequential logic block that eliminates common pitfalls of Verilog’s always @(posedge clk):

  • Compiler-enforced rules:
    • Only non-blocking assignments (<=) allowed.
    • Prohibits mixing combinational/sequential logic.
    • Accepts only variables (no wire assignments).
  • Guaranteed synthesis-simulation alignment.
always_ff @(posedge clk or posedge rst) begin  
  if (rst)  
    q <= 0;  
  else  
    q <= d;  
end  

⚠️ Critical: Using blocking assignments (=) in always_ff causes a compiler error.


⚙️ always_comb vs always @(*)
#

✅ Key Advantages of always_comb
#

Featurealways @(*) (Verilog)always_comb (SystemVerilog)
SensitivityIncomplete (heuristic-based)Perfect (all signals tracked)
AssignmentsAllows both = and <=Enforces blocking (=)
Latch DetectionTool-dependentWarns on incomplete branches
Function Tracking❌ Misses signals inside functions✅ Tracks all function-internal signals
Time-0 EvaluationNo✅ Executes once at simulation start
always_comb begin  
  y = my_func(sel);  // Auto-tracks ALL inputs (even in `my_func`).  
end  

🚫 Restriction: No delays (e.g., #5) allowed in always_comb.


🧠 Hidden Pitfall: Function Sensitivity in always @(*)
#

❌ Buggy Verilog Example
#

function logic [3:0] my_func(input logic flag);  
  if (flag) return a + b;  // `a` and `b` NOT in sensitivity list!  
  else      return a - b;  
endfunction  

always @(*) begin  
  y = my_func(sel);  // ❌ Fails if `a`/`b` change (no re-evaluation).  
end  

✅ Fixed with always_comb
#

always_comb begin  
  y = my_func(sel);  // ✅ Auto-detects `a`, `b`, and `sel`.  
end  

🕳️ always_latch – Intentional Latch Design
#

🎯 When to Use
#

Only for explicit level-sensitive latches:

always_latch begin  
  if (en) q = d;  // Latches value when `en` is low.  
end  

🔍 Tool Warnings:

  • Triggers if edge-sensitive logic (e.g., posedge) is used.
  • Infers latches on incomplete branches (no else).

📚 Quick Reference Table
#

Block TypePurposeAssignmentsKey Rules
always_ffSequential (flip-flops)<=No =, no wire, no mixed logic.
always_combCombinational=No latches, no delays, time-0 execution.
always_latchIntentional latches=Requires if-without-else for inference.
always @(*)Legacy combinational=/<=Avoid (misses function signals).
always @(posedge clk)Legacy sequential<=/=Prefer always_ff for new designs.

✅ Best Practices
#

  1. Always prefer always_ff/always_comb over legacy Verilog.
  2. Never mix = and <= in the same block.
  3. Default assignments: Initialize outputs in always_comb to avoid latches.
  4. Functions/Tasks: Use always_comb to auto-track all dependencies.

🔥 SystemVerilog’s always_* blocks are industry standards for unambiguous, synthesis-safe RTL design.


SystemVerilog Design Series - This article is part of a series.
Part 8: This Article

Related

SystemVerilog Modules – Structure, Instantiation, and RTL Best Practices
· loading · loading
Hardware Design SystemVerilog Module RTL Design Always_ff Parameter Instantiation
Hardware Design
SystemVerilog Tasks and Functions
· loading · loading
Hardware Design Verification SystemVerilog Tasks Functions RTL Design Testbench Reusability
Hardware Design Verification
SystemVerilog Blocking vs Non-Blocking Assignments Explained
· loading · loading
Hardware Design SystemVerilog Blocking Non-Blocking RTL Design Simulation Always Block
Hardware Design
SystemVerilog Clocking Block – Timing Control for Testbenches
· loading · loading
Verification Hardware Design SystemVerilog Clocking Block RTL Design Testbench UVM Timing
Verification Hardware Design
SystemVerilog Data Types
· loading · loading
Hardware Design Verification SystemVerilog Verilog RTL Design Data Types Synthesis Simulation
Hardware Design Verification
SystemVerilog Enum Data Type
· loading · loading
Hardware Design Verification SystemVerilog Enum FSM RTL Design Testbench Debugging
Hardware Design Verification