Choose the right procedural block and avoid multiple drivers, unintended latches, and sensitivity mistakes.
The procedural block states the intended storage: always_comb for combinational logic, for flip-flops, and for an intentional latch. These forms enable additional tool checks.
always_ff
always_latch
This chapter is for readers who can already write a module and want to describe combinational and clocked circuits in SystemVerilog. Review modules and ports and types, widths and signed values first. You should also know the difference between a gate and a register.
Cover every combinational path
always_comb runs once at simulation startup, then responds to read signals, including signals read within called functions:
The default assignment to o_y covers unselected cases, including an unknown operation code. Missing assignments can request value retention and infer a latch.
For i_a=5 and i_b=3, check these four results in simulation:
i_op
Operation
Expected o_y
00
Add
8
01
Subtract
2
10
Bitwise AND
1
11
Bitwise OR
7
Also try 15 + 1: the four-bit output wraps to 0. Change the inputs without toggling a clock; this ALU must respond combinationally. These checks cover behavior, while the synthesis report lets you check that no latch was inferred.
Use = for ordered intermediate calculations. A variable written in always_comb must not be written by another process, and timing waits are prohibited. Do not rely on the block retriggering because it changes a variable it also reads; order dependencies explicitly.
Store values at an edge
always_ff has one event control, which can combine clock and asynchronous reset:
always_ff @(posedge i_clk or negedge i_rst_n) begin if (!i_rst_n) o_q <= '0; else if (i_enable) o_q <= i_d;end
<= evaluates the new data and schedules the update after the edge. With i_enable low, the missing else means the flip-flop holds its value. Another process must not drive the same output.
This reset asserts on a falling edge of i_rst_n. Its release must meet the clock domain's requirements.
Use a latch deliberately
A latch follows its input while the gate is active, then stores the value:
always_latch begin if (i_gate) o_q <= i_d;end
always_latch states that intent. Latches need appropriate timing analysis and are rarely needed in FPGA designs. If combinational code infers one, first look for a missing assignment. Changing the keyword does not fix the circuit.
Practice combinational and registered behavior
The two-digit BCD incrementer in Verilog is a combinational exercise: compare ordinary increments, 99 wrapping to 00, and invalid BCD inputs against its specification. It uses Verilog syntax; the complete assignments needed to avoid accidental storage apply in both languages.
For edge-triggered storage, write a SystemVerilog testbench for the 4x8 register bank. Check writes at a rising edge, value retention with write-enable low, combinational reads and reset. Allow scheduled updates to settle before comparing outputs. The exercise validates the testbench against the correct DUT and hidden faulty variants; a passing run must detect those faults as well as accept the correct circuit.