When to use non-blocking (sequential) in Verilog - Time & Space Complexity
We want to understand how the time it takes to run sequential code with non-blocking assignments grows as the input size changes.
How does the number of steps change when we use non-blocking assignments in sequential logic?
Analyze the time complexity of the following Verilog sequential block using non-blocking assignments.
always @(posedge clk) begin
for (integer i = 0; i < N; i++) begin
reg_array[i] <= input_array[i];
end
end
This code copies values from one array to another on each clock cycle using non-blocking assignments.
Look for loops or repeated actions inside the sequential block.
- Primary operation: The for-loop that copies each element from input_array to reg_array.
- How many times: The loop runs N times every clock cycle.
Each clock cycle, the number of copy operations grows with N.
| Input Size (N) | Approx. Operations per cycle |
|---|---|
| 10 | 10 copies |
| 100 | 100 copies |
| 1000 | 1000 copies |
Pattern observation: The work grows directly with the size of the array. Double the array size, double the operations.
Time Complexity: O(N)
This means the time to complete the copying grows linearly with the number of elements.
[X] Wrong: "Non-blocking assignments run all at once instantly, so time does not grow with N."
[OK] Correct: Even though non-blocking assignments update registers simultaneously at the clock edge, the hardware still needs to handle each element, so the total work grows with the number of elements.
Understanding how sequential blocks with non-blocking assignments scale helps you design efficient hardware and explain your design choices clearly.
What if we replaced the for-loop with parallel assignments for each element? How would the time complexity change?
Practice
<=) in Verilog?Solution
Step 1: Understand the purpose of non-blocking assignments
Non-blocking assignments (<=) are used to model sequential logic, which updates values on clock edges, like flip-flops.Step 2: Differentiate from other assignment types
Blocking assignments (=) are used for combinational logic with immediate updates, not sequential logic.Final Answer:
To model sequential logic that updates on clock edges -> Option BQuick Check:
Non-blocking = sequential logic [OK]
- Using non-blocking for combinational logic
- Confusing blocking and non-blocking assignments
- Assigning outside always blocks
Solution
Step 1: Identify the correct event control
The always block should trigger on the positive edge of the clock:posedge clk.Step 2: Use non-blocking assignment inside the block
Non-blocking assignment uses<=to update signals sequentially.Final Answer:
always @(posedge clk) begin a <= b; end -> Option AQuick Check:
Non-blocking inside posedge always block = correct syntax [OK]
- Using blocking '=' instead of '<=' in sequential blocks
- Triggering always block on wrong event
- Missing begin-end for multiple statements
reg a, b; always @(posedge clk) begin a <= b; b <= 1'b1; end
Assuming initial values
a=0 and b=0.Solution
Step 1: Understand non-blocking update behavior
Non-blocking assignments update all variables simultaneously after the always block finishes.Step 2: Trace values before and after clock edge
Before clock edge: a=0, b=0.
At clock edge: a gets old b (0), b gets 1.
After update: a=0, b=1.Final Answer:
a=0, b=1 -> Option AQuick Check:
Non-blocking updates use old values simultaneously [OK]
- Thinking 'a' updates immediately with new 'b'
- Confusing blocking and non-blocking timing
- Ignoring initial values
always @(posedge clk) begin a = b; b = 1'b1; end
where
a and b are registers updated on clock edge.Solution
Step 1: Check assignment type in clocked always block
Sequential logic triggered on clock edges should use non-blocking assignments (<=), not blocking (=).Step 2: Understand consequences of blocking in sequential logic
Blocking assignments cause race conditions and incorrect simulation of flip-flops.Final Answer:
Using blocking assignments instead of non-blocking in sequential logic -> Option DQuick Check:
Sequential logic needs non-blocking assignments [OK]
- Using '=' in clocked always blocks
- Ignoring race condition risks
- Confusing sensitivity lists
clk. Which code snippet correctly uses non-blocking assignments to avoid race conditions?A) always @(posedge clk) begin bit0 <= bit1; bit1 <= bit0; end B) always @(posedge clk) begin bit0 = bit1; bit1 = bit0; end C) always @(posedge clk) begin bit0 <= bit1; bit1 <= bit1; end D) always @(posedge clk) begin bit0 = bit0; bit1 = bit1; end
Solution
Step 1: Analyze non-blocking assignments in Swaps bits correctly using non-blocking assignments
Swaps bits correctly using non-blocking assignments uses non-blocking assignments to swapbit0andbit1simultaneously, avoiding race conditions.Step 2: Compare with other options
Uses blocking assignments causing race conditions uses blocking assignments causing race conditions.
Assigns bit1 to both bits, losing original bit0 overwritesbit0andbit1incorrectly.
No change in bits, blocking assignments used does not change bits and uses blocking assignments.Final Answer:
Swaps bits correctly using non-blocking assignments -> Option CQuick Check:
Non-blocking swaps bits simultaneously [OK]
- Using blocking assignments in sequential logic
- Overwriting values before reading them
- Not updating both bits simultaneously
Ready to prove it?
Pass the test and this pattern moves unknown
