Module 5: Behavioral Verilog for Synthesis · 5.1 Procedural Blocks: always and initial

always and initial

Verilog source files divide into two categories: synthesizable design files and verification testbenches. Design files describe physical silicon hardware. Testbenches generate stimulus vectors and check design responses. The distinction between always and initial procedural blocks separates these two domains.

Fundamental Operational Rules

  • always Blocks: Describe continuous hardware circuits. An always block runs continuously during simulation, executing again each time its execution completes. Synthesizable always blocks map to physical logic gates or sequential registers.
  • initial Blocks: Execute exactly once at simulation time zero. An initial block has no physical hardware meaning. ASIC logic synthesis tools ignore initial blocks during elaboration.

Three Standard Synthesizable Forms of always

// 1. Pure combinational logic: infers combinational gates
always @(*) begin
    y = a & b;
end

// 2. Synchronous sequential logic: infers edge-triggered flip-flops
always @(posedge clk) begin
    q <= d;
end

// 3. Sequential logic with asynchronous reset: infers flip-flops with clear pins
always @(posedge clk or negedge rst_n) begin
    if (!rst_n)
        q <= 1'b0;
    else
        q <= d;
end

The combinational block executes whenever any input signal changes. The synchronous block executes only on the rising clock edge. The asynchronous block executes on rising clock edges or active-low reset transitions. All three forms describe hardware operating continuously under electrical power.

Role of the initial Block in Testbenches

initial begin
    $dumpfile("tb.vcd");
    $dumpvars(0, tb_name);

    clk = 1'b0;
    rst = 1'b1;
    d   = 1'b0;

    #20 rst = 1'b0;
    #10 d   = 1'b1;
    #50 $finish;
end

The initial block provides the operational core of a simulation testbench: initialize input pins, wait across specified time delays, assert control signals, and terminate simulation. Physical silicon does not support temporal delay statements (#20). ASIC design flows reject initial statements during static linting.

Design Module and Testbench Side by Side

The D flip-flop module and its testbench demonstrate this structural separation:

// dff.v: Synthesizable design module
`timescale 1ns / 1ps
`default_nettype none

module dff (
    input  wire clk,
    input  wire rst,
    input  wire d,
    output reg  q
);
    always @(posedge clk) begin
        if (rst)
            q <= 1'b0;
        else
            q <= d;
    end

endmodule
// tb_dff.v: Verification testbench
`timescale 1ns / 1ps
`default_nettype none

module tb_dff;

    reg  clk, rst, d;
    wire q;

    dff uut (
        .clk(clk),
        .rst(rst),
        .d  (d),
        .q  (q)
    );

    // Free-running simulation clock generator (10 ns period)
    always #5 clk = ~clk;

    initial begin
        $dumpfile("tb.vcd");
        $dumpvars(0, tb_dff);

        clk = 1'b0;
        rst = 1'b1;
        d   = 1'b0;

        @(posedge clk); 
        @(posedge clk);
        rst = 1'b0;

        @(posedge clk); d = 1'b1;
        @(posedge clk); d = 1'b0;
        @(posedge clk); d = 1'b1;

        #50 $finish;
    end

endmodule

The clock generator uses an always block without a sensitivity list, toggling every 5 ns. This construct is valid in simulation testbenches but non-synthesizable in hardware design files.

The Register Initialization Mismatch

New designers often use initial blocks to initialize register values:

// Incorrect: initial block used in synthesizable RTL
module counter (
    input  wire       clk,
    output reg  [3:0] count
);
    initial begin
        count = 4'd0;    // Ignored by ASIC logic synthesis
    end

    always @(posedge clk) begin
        count <= count + 1'b1;
    end

endmodule

In simulation, count starts at 0 and increments correctly. In physical ASIC silicon, the synthesis tool ignores the initial block. The physical flip-flops power up in an unpredictable random state, creating a simulation-synthesis mismatch.

[!NOTE] FPGA architectures represent a technology-specific exception. FPGA configuration bitstreams can initialize flip-flops and lookup table RAMs at device power-up. However, relying on initial blocks impairs code portability across ASIC standard cell libraries.

Synthesizable Hardware Reset Implementation

To guarantee predictable startup states, implement an explicit hardware reset path:

`timescale 1ns / 1ps
`default_nettype none

module counter (
    input  wire       clk,
    input  wire       rst,        // Synchronous active-high reset
    output reg  [3:0] count
);
    always @(posedge clk) begin
        if (rst)
            count <= 4'd0;
        else
            count <= count + 1'b1;
    end

endmodule

The testbench asserts rst during initialization. The synthesis tool infers a multiplexer in front of each flip-flop input. The reset operates identically in simulation and physical silicon.

The primary synthesizable application of initial blocks is loading memory array contents in FPGAs using $readmemh.

Practice Exercise

Identify two defects in this loadable register module and rewrite it to adhere to synthesizable RTL standards:

`timescale 1ns / 1ps
`default_nettype none

module loadable_reg (
    input  wire       clk,
    input  wire       load,
    input  wire [3:0] data_in,
    output reg  [3:0] data_out
);
    initial begin
        data_out = 4'hF;
    end

    always @(posedge clk) begin
        if (load) begin
            data_out = data_in;
        end
    end

endmodule
Solution

Defects in original code:

  1. The initial block sets a power-on state ignored by ASIC synthesis tools. Replace with an explicit reset input.
  2. The clocked block uses blocking assignments (=). Clocked sequential registers must use non-blocking assignments (<=).

Corrected implementation:

`timescale 1ns / 1ps
`default_nettype none

module loadable_reg (
    input  wire       clk,
    input  wire       rst,
    input  wire       load,
    input  wire [3:0] data_in,
    output reg  [3:0] data_out
);
    always @(posedge clk) begin
        if (rst)
            data_out <= 4'hF;
        else if (load)
            data_out <= data_in;
    end

endmodule

The next article examines blocking procedural assignments (=), explaining their execution semantics in combinational logic and why they cause race conditions in sequential blocks.

← Everything Runs at Once Blocking Assignments →