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

Everything Runs at Once

Consider the behavior of this dual-block Verilog module:

`timescale 1ns / 1ps
`default_nettype none

module mystery (
    input  wire a,
    input  wire b,
    output reg  x,
    output reg  y
);
    always @(*) begin
        x = a & y;
    end

    always @(*) begin
        y = b | x;
    end

endmodule

Software engineers often expect the first block to execute sequentially before the second block. That assumption is incorrect. Verilog describes physical hardware where all statements evaluate concurrently.

Sequential Software Versus Concurrent Hardware

Software languages execute instructions sequentially using an instruction pointer. Line $N$ completes execution before line $N+1$ begins.

Verilog does not use an instruction pointer. Everything in a Verilog module operates concurrently unless explicitly constrained. The example module describes two interconnected logic gates: an AND gate and an OR gate. The output of each gate drives an input of the opposite gate. This structure forms a closed feedback loop in physical silicon.

The Three Concurrent Hardware Constructs

Verilog systems contain three concurrent construct types:

  1. Procedural always Blocks: Execute whenever a signal in their sensitivity list transitions. They synthesize into combinational gates or clocked flip-flops.
  2. Continuous assign Statements: Re-evaluate whenever right-hand operands change. They model direct logic networks driving net conductors.
  3. Module Instantiations: Represent independent hardware sub-circuits with active I/O terminals.

In a design with ten always blocks, five assign statements, and three module instances, all eighteen elements operate simultaneously (Figure 1). They respond continuously to arriving signal transitions.

Three concurrent constructs
Figure 1: Concurrency of procedural blocks, continuous assignments, and module instances

The Event-Driven Simulation Engine

Digital simulators evaluate temporal changes using discrete event queues:

  1. Advance simulation time to the next scheduled event timestamp.
  2. Identify all processes whose sensitivity conditions match the updated signals.
  3. Execute active processes in an arbitrary order not defined by the language standard.
  4. Schedule newly evaluated signal updates as events within the current time step.
  5. Repeat evaluation passes until signals reach a stable state, then advance simulation time.

The IEEE standard does not specify process execution order. In physical silicon, independent logic gates switch simultaneously. If two concurrent blocks write to the same register or read uncommitted blocking updates, simulation results depend on compiler execution order. This condition is a simulation race defect.

Analysis of the Feedback Circuit

Trace the example module:

  • Set initial values: a = 1, b = 0, x = 0, y = 0.
  • Transition input b from 0 to 1.
  • The second block evaluates: y = 1 | 0 = 1.
  • The update on y triggers the first block: x = 1 & 1 = 1.
  • The update on x triggers the second block: y = 1 | 1 = 1. The loop stabilizes with x = 1 and y = 1.
  • Transition input b back to 0: y = 0 | 1 = 1. Output y remains 1.

The circuit forms a cross-coupled transparent latch. Combinational feedback creates memory latches or unstable ring oscillators.

[!IMPORTANT] To prevent unintended feedback latches, never create circular signal dependencies between combinational blocks. Separate combinational logic from sequential storage registers.

Sequential Race Conditions

Race conditions also occur in sequential RTL when using incorrect assignment operators. Consider this two-stage shift register:

// Incorrect: Blocking assignments create race conditions
always @(posedge clk) begin
    q1 = d;
end

always @(posedge clk) begin
    q2 = q1;
end

Both procedural blocks trigger on the identical clock edge. If the simulator executes the first block first, q1 updates to d immediately. The second block then reads the new value of q1, copying d directly into q2 within a single clock cycle. The shift register fails and acts as a single wire.

If the simulator executes the second block first, q2 reads the old value of q1. Both stages advance correctly.

The result depends entirely on simulator tool ordering. Non-blocking assignments (<=) resolve this defect by separating value evaluation from register commitment.

Practice Exercise

Classify each numbered block as synthesizable or simulation-only, and state whether it is combinational, sequential, or verification code:

`timescale 1ns / 1ps
`default_nettype none

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

    // Block 2
    always @(*) begin
        parity = q ^ d;
    end

    // Block 3
    initial begin
        $display("Simulation initialized");
    end

endmodule
Solution
  • Block 1: Synthesizable sequential hardware. Infers an edge-triggered D flip-flop with a synchronous active-high reset.
  • Block 2: Synthesizable combinational hardware. Infers a 2-input XOR gate.
  • Block 3: Simulation-only verification construct. Executes once at time zero to display console text; ignored by logic synthesis.

The next article examines procedural initial blocks and explains why hardware reset logic requires explicit control signals rather than simulation initializers.

always and initial →