Skip to content
systemcoding.dev
Hardware

Use SystemVerilog always_ff, always_comb and logic Types

Replace Verilog reg, wire and always @* with SystemVerilog logic, always_ff and always_comb, so the tools check your intent and catch latches early.

7 min read
Summarize in AI
Flat illustration of logic gates wired into a small circuit, with the KiCad, SparkFun and Haskell logos.

Verilog-2005 lets you write correct hardware, but it does not let you say what you meant. An always @(posedge clk) block could be a flip-flop or a mistake, and an always @* block could be combinational logic or an accidental latch. SystemVerilog, defined by IEEE 1800-2017, adds three small features that fix this: the logic type, always_ff and always_comb. This post shows each one, rewrites a Verilog design with them, and explains the checks they give you. The examples use Icarus Verilog 12.0 with -g2012, and Yosys 0.40 with read_verilog -sv, both of which accept the subset shown.

logic replaces reg and wire for most signals

In Verilog, a signal is a wire if a continuous assignment or a port drives it, and a reg if a procedural block assigns it. The wrong choice is a compile error, and the right choice tells the reader nothing about whether storage exists. SystemVerilog introduces logic, a four-state type that can be driven by either a continuous assignment or one procedural block, but not both. You declare it once and stop thinking about the keyword.

logic_demo.svsystemverilog
module logic_demo (
    input  logic       clk,
    input  logic [3:0] a,
    input  logic [3:0] b,
    output logic [3:0] sum,
    output logic [3:0] sum_q
);
    assign sum = a + b;

    always_ff @(posedge clk)
        sum_q <= sum;
endmodule

Both sum and sum_q are logic, and the driver, not the type, decides what they are. sum follows an adder. sum_q is a register. One rule keeps this safe: a logic variable may have only one driver kind. If you assign it in two different always blocks, or in an assign and a block, the tool reports an error. That catches the multiple-driver bugs that Verilog's reg allowed.

Use wire for nets that genuinely have several drivers, such as a tri-state bus. For nearly everything else, logic is correct.

always_ff states that this block is a register

always_ff requires an edge-based sensitivity list, so a block that reads always_ff @(posedge clk) is a flip-flop by declaration. The tool can then verify the intent. If the block has no edge, or if it assigns a variable that another process also assigns, the compiler flags it. By the same convention, the assignments inside use <=.

counter_ff.svsystemverilog
module counter_ff #(
    parameter int WIDTH = 8
) (
    input  logic             clk,
    input  logic             rst,
    output logic [WIDTH-1:0] count
);
    always_ff @(posedge clk) begin
        if (rst)
            count <= '0;
        else
            count <= count + 1'b1;
    end
endmodule

Two more SystemVerilog features appear here. parameter int WIDTH = 8 gives the parameter a type. The literal '0 is an unsized fill: it sets every bit of the target to zero, whatever its width, which removes the replication syntax {WIDTH{1'b0}} from the Verilog version. There are also '1 for all ones, and 'x and 'z.

always_comb states that this block is pure logic

always_comb replaces always @*. It differs in three ways that matter.

  1. Its sensitivity list is inferred from everything the block reads, including signals read inside called functions, which @* can miss.

  2. It runs once at time zero so that outputs are consistent from the start of simulation.

  3. It requires that the block be combinational. If a tool sees a path that leaves an output unassigned, which would infer a latch, it can warn or error.

decode.svsystemverilog
module decode (
    input  logic [1:0] sel,
    output logic [3:0] onehot
);
    always_comb begin
        onehot = 4'b0000;
        case (sel)
            2'd0: onehot = 4'b0001;
            2'd1: onehot = 4'b0010;
            2'd2: onehot = 4'b0100;
            2'd3: onehot = 4'b1000;
        endcase
    end
endmodule

The default assignment at the top still matters, even when the case covers every value, because it makes the intent obvious and protects the code when someone later edits it. In always_comb blocks, use blocking = assignments, as the combinational rule from the earlier post on assignments says.

Enumerations for states

SystemVerilog adds enum, which gives state names a type and lets the tool check them. A typed state variable also shows its names in a waveform viewer.

fsm_enum.svsystemverilog
module fsm_enum (
    input  logic clk,
    input  logic rst,
    input  logic start,
    output logic busy
);
    typedef enum logic [1:0] {IDLE, RUN, DONE} state_t;
    state_t state, next;

    always_ff @(posedge clk) begin
        if (rst) state <= IDLE;
        else     state <= next;
    end

    always_comb begin
        next = state;
        unique case (state)
            IDLE: if (start) next = RUN;
            RUN:             next = DONE;
            DONE:            next = IDLE;
        endcase
    end

    assign busy = (state == RUN);
endmodule

typedef enum logic [1:0] {...} state_t declares a two-bit type with three named values. Declaring state_t state, next; gives both variables that type. The unique case modifier tells the tool that exactly one branch matches every time, and a simulator can warn if none does. With a three-value enum in a two-bit field there is one unused encoding, so for a design that must recover from upsets, add a default branch as well.

A complete before and after

Here is the pedestrian counter from the guide, rewritten. First, the Verilog-2005 style:

timer_v.vverilog
module timer_v (
    input  wire       clk,
    input  wire       rst,
    input  wire       enable,
    output reg  [3:0] count,
    output wire       done
);
    always @(posedge clk) begin
        if (rst)
            count <= 4'd0;
        else if (enable)
            count <= count + 4'd1;
    end

    assign done = (count == 4'd9);
endmodule

Then SystemVerilog:

timer_sv.svsystemverilog
module timer_sv (
    input  logic       clk,
    input  logic       rst,
    input  logic       enable,
    output logic [3:0] count,
    output logic       done
);
    always_ff @(posedge clk) begin
        if (rst)
            count <= '0;
        else if (enable)
            count <= count + 1'b1;
    end

    always_comb done = (count == 4'd9);
endmodule

The logic is identical. What changed is that the register is declared as a register and the combinational output as combinational, and the type of every signal is logic. A testbench is the same in both cases.

Building with the open tools

Icarus Verilog needs the generation flag to accept SystemVerilog:

Shell
iverilog -g2012 -o timer_sv.vvp timer_sv.sv timer_tb.sv
vvp timer_sv.vvp

Yosys needs -sv:

Shell
yosys -p "read_verilog -sv timer_sv.sv; synth_ice40 -top timer_sv; stat"

Neither tool supports all of SystemVerilog. Classes, randomisation and full assertion support are for verification tools. The synthesisable subset in this post is accepted. If the tool does not recognise a construct, it stops with a syntax error at that line, which is a prompt to check the tool's documented support, not to change the design.

A quick testbench in SystemVerilog uses the same ideas:

timer_tb.svsystemverilog
`timescale 1ns/1ps

module timer_tb;
    logic       clk = 1'b0;
    logic       rst = 1'b1;
    logic       enable = 1'b0;
    logic [3:0] count;
    logic       done;

    timer_sv dut (.*);

    always #5 clk = ~clk;

    initial begin
        #12 rst = 1'b0;
        enable = 1'b1;
        repeat (9) @(posedge clk);
        #1 $display("count=%0d done=%b", count, done);
        $finish;
    end
endmodule

The .* connects every port of dut to the signal in scope with the same name, a SystemVerilog shortcut that removes long port lists. After reset releases at 12 ns the edges at 15, 25, 35, 45, 55, 65, 75, 85 and 95 ns each increment, so nine edges give a count of 9 and done is high.

Text
count=9 done=1

What not to do

  • **Do not use always_ff for a latch or always_comb with an edge.** The tools reject both, which is the point.

  • **Do not declare logic and drive it from two blocks.** Use one driver, or use a wire and a tri-state structure.

  • Do not rely on vendor-only SystemVerilog features in files meant for the open toolchain, because support differs between tools.

  • **Do not skip the reset on always_ff state.** The keyword proves a flip-flop exists, not that it starts in a known state.

Migrating existing code

Moving a Verilog codebase to this style is mostly mechanical, and it can be done file by file because the two styles coexist. Rename the file to .sv, change wire and reg declarations to logic, turn each always @(posedge clk) into always_ff @(posedge clk), and each always @* into always_comb. Then build, and read the new diagnostics. Each one is a bug the old style hid: a signal driven from two places, a combinational block with an unassigned path, or a clocked block that also contains a blocking assignment to a variable read elsewhere. Fix those before moving on, because they are the real payoff of the migration. Keep tri-state buses as wire, since logic has one driver by design.

A final point on portability. If a file must be read by an older Verilog-only tool, keep it in plain Verilog and put the SystemVerilog in files that need it. Most modern tools infer the language from the extension, .v for Verilog and .sv for SystemVerilog, and some switch on a flag instead, so check what your build script passes.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.