๐ฐ Assignments โ Blocking vs Nonblocking
This notebook explains the difference between blocking (=) and nonblocking (<=) assignments in Verilog, with examples, use cases, and best practices for modeling combinational and sequential logic.
๐ Overview
| Type | Syntax | Execution Style | Use Case |
|---|---|---|---|
| Blocking | = | Step-by-step (sequential) | Combinational logic, temporary variables |
| Nonblocking | <= | Parallel (scheduled) | Sequential logic (flip-flops, registers) |
Why “Blocking” and “Nonblocking”?
These terms come from software concurrency, especially in languages like C or Java, where:
- A blocking call halts execution until it completes.
- A nonblocking call allows other operations to proceed in parallel.
Verilog borrowed this terminology to describe how assignments behave inside procedural blocks (always, initial), especially when modeling sequential logic.
Why “Blocking” and “Nonblocking”?
These terms come from software concurrency, especially in languages like C or Java, where:
- A blocking call halts execution until it completes.
- A nonblocking call allows other operations to proceed in parallel.
Verilog borrowed this terminology to describe how assignments behave inside procedural blocks (always, initial), especially when modeling sequential logic.
๐ Blocking Assignment (=)
- Executes immediately in the order written
- Order of assignments matter because of this
- Later statements see updated values.
- Can cause race conditions if used in sequential logic.
๐งช Example
always @(posedge clk) begin
a = b;
b = a; // b gets the new value of a (which is b) โ not intended!
end๐ซ Pitfall
- This creates unintended behavior because
ais updated beforeb.
๐ Nonblocking Assignment (<=)
- Schedules updates โ all assignments happen in parallel at the end of the time step.
- Order of assignments does not matter because of this
- Later statements see old values.
- Ideal for modeling flip-flops and registers.
๐งช Example
always @(posedge clk) begin
a <= b;
b <= a; // b gets the old value of a โ safe and predictable
endโ Benefit
- Ensures consistent behavior across clock cycles.
๐งฉ When to Use Each
| Context | Use = (Blocking)? | Use <= (Nonblocking)? |
|---|---|---|
| Combinational logic | โ Yes | โ No |
| Sequential logic | โ No | โ Yes |
| Temporary variables | โ Yes | โ No |
| Flip-flop modeling | โ No | โ Yes |
๐ง Best Practices
- Use
<=for registers and state machines insidealways @(posedge clk)blocks. - Use
=for combinational logic insidealways @(*)blocks. - Never mix
=and<=in the samealwaysblock โ it leads to subtle bugs.
Last updated on