Blocking vs. Non-blocking Assignments in SystemVerilog
Blocking Assignment (=
)
- Immediate Execution: In blocking assignments, the right-hand side of the assignment is evaluated immediately, and the result is assigned to the left-hand side variable.
- Sequential Execution: Assignments are executed sequentially in the order they appear in the code.
- Example:
a = b; <span class="hljs-comment">// Value of 'b' is assigned to 'a' immediately</span>
c = a + <span class="hljs-number">1</span>; <span class="hljs-comment">// Value of 'a' (after the previous assignment) is used to calculate 'c'</span>
Non-blocking Assignment (<=
)
- Scheduled Execution: Non-blocking assignments are scheduled to occur at the end of the current time step. The actual assignment happens after all non-blocking assignments within the same time step are evaluated.
- Concurrent Execution: Multiple non-blocking assignments within the same
always
block are scheduled concurrently. - Example:
a <= b; <span class="hljs-comment">// Schedule the assignment of 'b' to 'a' for the end of the time step</span>
c <= a + <span class="hljs-number">1</span>; <span class="hljs-comment">// Schedule the assignment of 'a+1' to 'c' for the end of the time step</span>
Key Differences and Implications:
Feature | Blocking Assignment (= ) |
Non-blocking Assignment (<= ) |
---|---|---|
Execution | Immediate | Scheduled |
Order | Sequential | Concurrent |
Timing | Can introduce unintended delays | Essential for modeling sequential logic |
Use Cases | Combinational logic, procedural blocks with sequential execution | Sequential logic, register updates, modeling clocked circuits |
Illustrative Example:
<span class="hljs-keyword">always</span> @ (<span class="hljs-keyword">posedge</span> clk) <span class="hljs-keyword">begin</span>
a = b; <span class="hljs-comment">// Blocking assignment</span>
c <= a + <span class="hljs-number">1</span>; <span class="hljs-comment">// Non-blocking assignment</span>
<span class="hljs-keyword">end</span>
In this example:
- At the positive edge of
clk
,b
is assigned toa
immediately. - The value of
a
(after the blocking assignment) is used to calculatec
. - The assignment of
a
and the calculation ofc
are scheduled to happen at the end of the current time step.
Important Notes:
- Non-blocking assignments are crucial for modeling sequential logic and updating registers at the rising or falling edge of the clock.
- Blocking assignments are typically used within combinational logic or for immediate assignments within a procedural block.
- Using blocking assignments within an
always
block with a sensitivity list that includes the signals being assigned can lead to unexpected behavior.