always_ff, always_comb and always_latch
Classic Verilog uses the generic always keyword for sequential, combinational, and latch circuits.
Generic blocks do not indicate design intent to EDA tools.
SystemVerilog introduces three specialized procedural blocks: always_ff, always_comb, and always_latch.
These blocks enforce explicit architectural rules at compile time.
Sequential Logic: always_ff
Use always_ff exclusively to model edge-triggered sequential registers:
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n) q <= 1'b0;
else q <= d;
end
The compiler verifies that the sensitivity list contains edge specifiers like posedge or negedge.
The tool flags illegal blocking assignments (=) inside always_ff blocks.
It also verifies that no other procedural block writes to the assigned variable.
Combinational Logic: always_comb
Use always_comb to model pure combinational logic:
always_comb begin
if (sel)
y = b;
else
y = a;
end
The always_comb construct provides two major advantages over always @(*):
First, the compiler infers complete sensitivity lists automatically, including function calls.
Second, always_comb executes automatically at simulation time zero to initialize outputs.
In contrast, always @(*) waits for an initial input transition, which can cause simulation startup bugs.
Transparent Latches: always_latch
Use always_latch to model intentional level-sensitive latches:
always_latch begin
if (latch_enable)
q_latched <= data_in;
// Deliberate level-sensitive retention without else clause
end
Section 7.6 described operand isolation using intentional latches.
The always_latch keyword explicitly informs compilers that data retention is intentional.
This declaration suppresses unwanted latch warnings in synthesis tools while maintaining strict checking elsewhere.
Compile-Time Error Detection
In classic Verilog, incomplete branching in combinational blocks infers unintended hardware latches.
Engineers typically discover these errors late in the design cycle from synthesis logs.
When using always_comb, linters and simulators detect incomplete branch assignments immediately during compilation.
These three constructs do not alter synthesized hardware structures.
An always_ff block infers the same physical flip-flop as a standard always @(posedge clk) block.
However, specialized blocks declare explicit design intent.
Compilers use this intent to validate your code against design rules before synthesis.
Practice Exercise
Identify the coding error in the combinational block below.
Explain why always_comb detects this defect more reliably than always @(*).
always_comb begin
if (mode)
result <= a + b; // Error: non-blocking assignment in combinational logic
else
result = a - b;
end
Solution
The if branch uses a non-blocking assignment (<=), while the else branch uses a blocking assignment (=).
Combinational blocks must use blocking assignments exclusively.
Compilers enforce this rule strictly inside always_comb and flag the non-blocking assignment as an error.
In contrast, classic always @(*) blocks permit non-blocking assignments without warnings.
That permits race conditions and event scheduling mismatches during simulation.
SystemVerilog specialized blocks declare explicit hardware intent.
The compiler uses always_ff, always_comb, and always_latch to detect syntax and architectural errors at compile time.
The next article introduces typedef, struct, union, and enum constructs for structured data modeling.