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

The Scheduler

Understanding the internal simulation event scheduler explains why specific RTL coding styles prevent hardware race conditions. The IEEE Verilog standard specifies a stratified event queue. The simulator processes distinct event regions in a fixed sequential order within each time step.

The Stratified Event Queue

Three primary event regions govern digital RTL simulation:

Event Queue Region Operations Processed in Region
Active Region Evaluates blocking assignments (=), continuous assignments (assign), right-hand operands of non-blocking assignments (<=), and $display statements.
Non-Blocking Assign (NBA) Region Commits left-hand target updates for all non-blocking assignments evaluated during the Active region.
Postponed Region Executes read-only monitoring tasks such as $strobe and $monitor after all signal transitions in the current time step settle.

The simulator processes the Active region first, followed by the NBA region, and concludes with the Postponed region. If NBA updates trigger sensitive combinational blocks, the simulator returns to the Active region within the identical time step. This iterative zero-time pass is a delta cycle. When all queues empty, simulation time advances.

Sequential Evaluation Across Two Flip-Flops

`timescale 1ns / 1ps
`default_nettype none

module two_ff_chain (
    input  wire clk,
    input  wire d,
    output wire q2
);
    reg q1, q2_reg;

    always @(posedge clk) begin
        q1 <= d;
    end

    always @(posedge clk) begin
        q2_reg <= q1;
    end

    assign q2 = q2_reg;

endmodule

Consider initial values d = 1, q1 = 0, and q2_reg = 0:

  1. Active Region: The simulator executes both procedural blocks in arbitrary order:
    • If block 1 executes first: evaluates d (1) and queues update $q_1 \leftarrow 1$. Block 2 evaluates q1 (0) and queues update $q_{2_reg} \leftarrow 0$.
    • If block 2 executes first: evaluates q1 (0) and queues update $q_{2_reg} \leftarrow 0$. Block 1 evaluates d (1) and queues update $q_1 \leftarrow 1$.
  2. NBA Region: The simulator applies both queued updates simultaneously: $q_1 = 1$ and $q_{2_reg} = 0$.

Data shifts forward exactly one register per clock edge, independent of block execution order.

The Same Circuit with Blocking Race Defects

// Incorrect: Blocking assignments introduce simulator-dependent execution order
always @(posedge clk) begin
    q1 = d;
end

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

If the simulator executes the first block first, q1 updates to 1 immediately in the Active region. The second block then reads the new value q1 = 1, updating q2_reg = 1 during the same clock edge.

If the simulator executes the second block first, q2_reg reads the old value 0. The design outcome depends on tool implementation rather than hardware architecture.

Delta Cycles in Combinational Networks

A delta cycle represents an evaluation pass within the Active region that consumes zero simulation time. Combinational logic regularly triggers delta cycles:

  • A continuous assignment updates an intermediate net.
  • Downstream continuous assignments sensitive to that net evaluate in a subsequent delta pass.
  • Evaluation repeats until all combinational nets stabilize.

Unstable feedback loops cause infinite delta cycles:

// Combinational oscillation creates infinite delta loops
assign b = ~a;
assign a = ~b;

An odd number of inverters forms a ring oscillator. The simulator executes infinite delta cycles at the current timestamp until it triggers a runtime iteration limit error.

Why $display Prints Stale Register Values

always @(posedge clk) begin
    q <= d;
    $display("Active region display: q=%b d=%b", q, d);
end

The system task $display executes in the Active region. It runs when the non-blocking assignment evaluates, before the NBA region updates register q. Consequently, $display prints the old pre-edge value of q.

To inspect the updated register value, use $strobe:

always @(posedge clk) begin
    q <= d;
    $strobe("Postponed region strobe: q=%b after update", q);
end

The system task $strobe executes in the Postponed region after all NBA updates commit (Figure 1).

The scheduler loop
Figure 1: Stratified event execution cycle: Active region, NBA update region, and Postponed region

Summary of Assignment Rules

  • Use <= in Clocked Blocks: Non-blocking updates defer register commitment to the NBA region, allowing all concurrent blocks to read stable pre-edge values during the Active region.
  • Use = in Combinational Blocks: Blocking assignments evaluate sequentially within the Active region, allowing intermediate calculation steps to resolve before driving outputs.
  • Do Not Mix Operators: Mixing = and <= distributes updates across multiple regions in an undefined order, causing simulation-synthesis mismatches.

Practice Exercise

`timescale 1ns / 1ps
`default_nettype none

module trace_me (
    input  wire       clk,
    input  wire [3:0] d,
    output reg  [3:0] q1,
    output reg  [3:0] q2
);
    always @(posedge clk) begin
        q1 <= d;
    end

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

endmodule

Initial states: q1 = 4'h0, q2 = 4'h0. Input d transitions to 4'hB before clock edge 1. Trace the Active and NBA events across clock edges 1 and 2:

Solution
  • Clock Edge 1:
    • Active Region: Block 1 samples d = 4'hB and schedules $q_1 \leftarrow 4’hB$. Block 2 samples q1 = 4'h0 and schedules $q_2 \leftarrow 4’h0$.
    • NBA Region: Updates commit simultaneously. Final states: q1 = 4'hB, q2 = 4'h0.
  • Clock Edge 2:
    • Active Region: Block 1 samples d = 4'hB and schedules $q_1 \leftarrow 4’hB$. Block 2 samples q1 = 4'hB and schedules $q_2 \leftarrow 4’hB$.
    • NBA Region: Updates commit simultaneously. Final states: q1 = 4'hB, q2 = 4'hB.

This completes Section 5.1. Section 5.2 examines procedural sensitivity lists, detailing edge-triggered versus level-sensitive triggers and latch avoidance techniques.

← Non-Blocking Assignments The Sensitivity List →