Understanding Blocking vs Non-blocking Assignment in Verilog
📖 Scenario: You are designing a simple digital circuit in Verilog that updates registers based on input signals. Understanding how blocking and non-blocking assignments work will help you predict the circuit's behavior correctly.
🎯 Goal: Build a Verilog module that demonstrates the difference between blocking (=) and non-blocking (<=) assignments by updating registers a, b (blocking), c, d (non-blocking) and observing their values after a clock edge.
📋 What You'll Learn
Create registers a, b, c, d initialized to 0
Create a clock signal clk
Use blocking assignment to update a and b in one always block
Use non-blocking assignment to update c and d in another always block
Display the values of a, b, c, d after updates to observe the difference
💡 Why This Matters
🌍 Real World
Understanding blocking and non-blocking assignments is essential for designing correct synchronous digital circuits and avoiding bugs in hardware description languages like Verilog.
💼 Career
Hardware engineers and FPGA developers must master these concepts to write reliable and predictable RTL code for digital systems.
Progress0 / 4 steps
1
Create registers and clock signal
Create a Verilog module called blocking_nonblocking_demo. Inside it, declare registers a, b, c, d initialized to 0, and a register clk initialized to 0.
Verilog
Hint
Use reg to declare registers and assign initial values with = 0;.
2
Create a clock toggle
Add an always block that toggles the clk signal every 5 time units using #5 clk = ~clk;.
Verilog
Hint
Use an always block without sensitivity list to create a clock by toggling clk every 5 time units.
3
Add blocking assignment always block
Add an always @(posedge clk) block that updates a and b using blocking assignments: first a = b + 1; then b = a + 1;.
Verilog
Hint
Use blocking assignments with = inside the always @(posedge clk) block.
4
Add non-blocking assignment always block and display
Add another always @(posedge clk) block that updates c and d using non-blocking assignments: first c <= d + 1; then d <= c + 1;. Also, add an initial block that runs for 30 time units and uses $display to print a, b, c, d every 5 time units.
Verilog
Hint
Use non-blocking assignments with <= in a separate always @(posedge clk) block to update c and d. Use initial block with $display and repeat to print values of all registers every 5 time units.
Practice
(1/5)
1. Which of the following statements correctly describes the difference between blocking and non-blocking assignments in Verilog?
easy
A. Blocking assignments are used only in testbenches, non-blocking only in synthesis.
B. Blocking assignments use '<=' and update variables after all right sides are evaluated, while non-blocking assignments use '=' and update variables immediately.
C. Blocking assignments use '=' and update variables immediately, while non-blocking assignments use '<=' and update variables after all right sides are evaluated.
D. Both blocking and non-blocking assignments use '=' but differ in execution speed.
Solution
Step 1: Understand blocking assignment behavior
Blocking assignments use '=' and execute statements in order, updating variables immediately.
Non-blocking assignments use '<=' and update variables only after all right-hand sides are evaluated, allowing parallel updates.
Final Answer:
Blocking assignments use '=' and update variables immediately, while non-blocking assignments use '<=' and update variables after all right sides are evaluated. -> Option C
Hint: Use non-blocking for all regs in sequential blocks [OK]
Common Mistakes:
Thinking blocking is allowed in sequential always blocks
Ignoring race conditions caused by mixed assignments
Assuming wires can be assigned in always blocks
5. You want to model a flip-flop with asynchronous reset in Verilog. Which assignment style correctly models the sequential behavior inside the always block?
always @(posedge clk or posedge reset) begin
if (reset)
q = 0;
else
q <= d;
end
What is the problem with this code?
hard
A. Reset should use non-blocking '<=' and data should use blocking '=' assignments.
B. The sensitivity list is incorrect; it should not include reset.
C. Both assignments should be blocking '=' for correct flip-flop behavior.
D. Mixing blocking '=' for reset and non-blocking '<=' for data causes simulation mismatches.
Solution
Step 1: Understand assignment consistency in sequential blocks
Sequential logic should use non-blocking assignments consistently to avoid simulation and synthesis mismatches.
Step 2: Identify mixed assignment problem
Using blocking '=' for reset and non-blocking '<=' for data causes timing mismatches and incorrect behavior.
Final Answer:
Mixing blocking '=' for reset and non-blocking '<=' for data causes simulation mismatches. -> Option D
Quick Check:
Use non-blocking consistently in sequential logic [OK]
Hint: Use non-blocking for all sequential assignments including reset [OK]
Common Mistakes:
Mixing blocking and non-blocking in same always block
Ignoring asynchronous reset in sensitivity list
Assuming blocking '=' is fine for reset
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.