Blocking and non-blocking assignments control how values update in Verilog code. They help decide if statements run one after another or all at once.
Blocking vs non-blocking assignment in Verilog
blocking assignment: variable = expression; non-blocking assignment: variable <= expression;
Blocking assignments use the '=' sign and execute statements in order.
Non-blocking assignments use the '<=' sign and update variables after all right-hand sides are evaluated.
a = b + c; // blocking assignment x <= y + z; // non-blocking assignment
always @(*) begin
sum = a + b; // blocking
carry = (a & b);
endalways @(posedge clk) begin q <= d; // non-blocking end
This program shows how blocking assignments update variables immediately and in order, while non-blocking assignments update all variables together after the block finishes.
module test;
reg a, b, c;
reg x, y, z;
initial begin
a = 1; b = 0; c = 1;
x = 0; y = 1; z = 0;
// Blocking assignments
a = b;
b = c;
c = a;
$display("After blocking: a=%b b=%b c=%b", a, b, c);
// Reset values
a = 1; b = 0; c = 1;
// Non-blocking assignments
a <= b;
b <= c;
c <= a;
#1; // wait for non-blocking updates
$display("After non-blocking: a=%b b=%b c=%b", a, b, c);
end
endmoduleBlocking assignments run one after another, so the next line sees the updated value.
Non-blocking assignments schedule updates to happen later, so all right sides use old values.
Use blocking for combinational logic and non-blocking for sequential logic to avoid bugs.
Blocking assignments use '=' and run statements in order, updating variables immediately.
Non-blocking assignments use '<=' and update variables after evaluating all right sides.
Choose blocking for combinational logic and non-blocking for sequential logic to model hardware correctly.
Practice
Solution
Step 1: Understand blocking assignment behavior
Blocking assignments use '=' and execute statements in order, updating variables immediately.Step 2: Understand non-blocking assignment behavior
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 CQuick Check:
Blocking = immediate, Non-blocking = delayed update [OK]
- Confusing '=' and '<=' usage
- Thinking both update variables immediately
- Assuming non-blocking is only for testbenches
Solution
Step 1: Identify non-blocking assignment operator
Non-blocking assignments use the '<=' operator in Verilog.Step 2: Check syntax correctness
reg_var <= value; uses '<=' correctly, while others use invalid or different operators.Final Answer:
reg_var <= value; -> Option BQuick Check:
Non-blocking uses '<=' [OK]
- Using '=' instead of '<=' for non-blocking
- Confusing ':=' or '==' as assignment
- Syntax errors with missing semicolons
reg a, b, c; always @(posedge clk) begin a = b; b <= c; end
What will be the value of 'a' and 'b' after the clock edge if initially b=0 and c=1?
Solution
Step 1: Evaluate blocking assignment 'a = b;'
Blocking assignment updates 'a' immediately with current 'b' value, which is 0.Step 2: Evaluate non-blocking assignment 'b <= c;'
Non-blocking assignment schedules 'b' to update after the block with 'c' value, which is 1.Final Answer:
a = 0, b = 1 -> Option AQuick Check:
Blocking immediate, non-blocking delayed update [OK]
- Assuming both update simultaneously
- Confusing initial values of variables
- Ignoring order of assignments
always @(posedge clk) begin reg1 <= reg2; reg2 = reg1; end
Solution
Step 1: Analyze assignment types in sequential block
In sequential always blocks, non-blocking assignments should be used for all registers to avoid race conditions.Step 2: Identify blocking assignment misuse
Using blocking assignment '=' for reg2 causes immediate update, which can cause incorrect behavior in sequential logic.Final Answer:
Using blocking assignment for reg2 causes incorrect sequential behavior. -> Option AQuick Check:
Sequential logic needs non-blocking assignments [OK]
- Thinking blocking is allowed in sequential always blocks
- Ignoring race conditions caused by mixed assignments
- Assuming wires can be assigned in always blocks
always @(posedge clk or posedge reset) begin
if (reset)
q = 0;
else
q <= d;
endWhat is the problem with this code?
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 DQuick Check:
Use non-blocking consistently in sequential logic [OK]
- Mixing blocking and non-blocking in same always block
- Ignoring asynchronous reset in sensitivity list
- Assuming blocking '=' is fine for reset
Ready to prove it?
Pass the test and this pattern moves unknown
