Bird
Raised Fist0
Verilogprogramming~20 mins

When to use non-blocking (sequential) in Verilog - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Non-blocking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of non-blocking assignments in sequential logic

What is the output of the following Verilog code snippet after one clock cycle?

reg [3:0] a, b;
always @(posedge clk) begin
  a <= b;
  b <= a + 1;
end
Verilog
reg [3:0] a = 0, b = 0;
always @(posedge clk) begin
  a <= b;
  b <= a + 1;
end
// After one positive clock edge, what are the values of a and b?
Aa = 1, b = 1
Ba = 0, b = 1
Ca = 0, b = 0
Da = 1, b = 0
Attempts:
2 left
💡 Hint

Remember that non-blocking assignments update all variables simultaneously after the always block finishes.

🧠 Conceptual
intermediate
1:30remaining
Why use non-blocking assignments in sequential always blocks?

Which reason best explains why non-blocking assignments (<=) are preferred in sequential always blocks triggered by clock edges?

AThey automatically infer combinational logic without needing sensitivity lists.
BThey execute assignments immediately, making simulation faster.
CThey are simpler to write and require less code than blocking assignments.
DThey allow all right-hand side expressions to be evaluated before any left-hand side variables are updated, preventing race conditions.
Attempts:
2 left
💡 Hint

Think about how multiple assignments inside a clocked block should behave simultaneously.

🔧 Debug
advanced
2:00remaining
Identify the problem with blocking assignments in sequential logic

Consider this Verilog code inside a clocked always block:

always @(posedge clk) begin
  a = b;
  b = a + 1;
end

What problem will this cause?

AIt causes a race condition because 'a' is updated immediately, affecting the next assignment to 'b'.
BIt will synthesize correctly with no issues.
CIt causes a syntax error because blocking assignments are not allowed in always blocks.
DIt will cause 'b' to never update.
Attempts:
2 left
💡 Hint

Think about how blocking assignments execute in order inside an always block.

📝 Syntax
advanced
1:30remaining
Which code correctly uses non-blocking assignments in sequential logic?

Which of the following always blocks correctly uses non-blocking assignments for sequential logic?

A
always @(posedge clk) begin
  x &lt;= y;
  y &lt;= x + 1;
end
B
always @(posedge clk) begin
  x = y;
  y = x + 1;
end
C
always @(*) begin
  x &lt;= y;
  y &lt;= x + 1;
end
D
always @(*) begin
  x = y;
  y = x + 1;
end
Attempts:
2 left
💡 Hint

Non-blocking assignments are used in clocked always blocks, not combinational ones.

🚀 Application
expert
2:30remaining
Predict the final values after multiple clock cycles with non-blocking assignments

Given the following Verilog code, what are the values of a and b after 3 positive clock edges?

reg [3:0] a = 0, b = 0;
always @(posedge clk) begin
  a <= b;
  b <= a + 1;
end
AAfter 3 cycles: a = 3, b = 4
BAfter 3 cycles: a = 3, b = 3
CAfter 3 cycles: a = 2, b = 3
DAfter 3 cycles: a = 1, b = 2
Attempts:
2 left
💡 Hint

Trace the values step-by-step, remembering non-blocking assignments update simultaneously after each clock.

Practice

(1/5)
1. When should you use non-blocking assignments (<=) in Verilog?
easy
A. To declare constants in your design
B. To model sequential logic that updates on clock edges
C. To model combinational logic with immediate updates
D. To assign values outside of always blocks

Solution

  1. 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.
  2. Step 2: Differentiate from other assignment types

    Blocking assignments (=) are used for combinational logic with immediate updates, not sequential logic.
  3. Final Answer:

    To model sequential logic that updates on clock edges -> Option B
  4. Quick Check:

    Non-blocking = sequential logic [OK]
Hint: Use non-blocking for clocked (sequential) logic only [OK]
Common Mistakes:
  • Using non-blocking for combinational logic
  • Confusing blocking and non-blocking assignments
  • Assigning outside always blocks
2. Which of the following is the correct syntax for a non-blocking assignment inside an always block triggered on a clock edge?
easy
A. always @(posedge clk) begin a <= b; end
B. always @(posedge clk) begin a = b; end
C. always @(clk) begin a <= b; end
D. always @(posedge clk) a = b;

Solution

  1. Step 1: Identify the correct event control

    The always block should trigger on the positive edge of the clock: posedge clk.
  2. Step 2: Use non-blocking assignment inside the block

    Non-blocking assignment uses <= to update signals sequentially.
  3. Final Answer:

    always @(posedge clk) begin a <= b; end -> Option A
  4. Quick Check:

    Non-blocking inside posedge always block = correct syntax [OK]
Hint: Use '<= ' inside 'always @(posedge clk)' for sequential logic [OK]
Common Mistakes:
  • Using blocking '=' instead of '<=' in sequential blocks
  • Triggering always block on wrong event
  • Missing begin-end for multiple statements
3. What is the output of this Verilog code snippet after one positive clock edge?
reg a, b;
always @(posedge clk) begin
  a <= b;
  b <= 1'b1;
end

Assuming initial values a=0 and b=0.
medium
A. a=0, b=1
B. a=1, b=1
C. a=0, b=0
D. a=1, b=0

Solution

  1. Step 1: Understand non-blocking update behavior

    Non-blocking assignments update all variables simultaneously after the always block finishes.
  2. 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.
  3. Final Answer:

    a=0, b=1 -> Option A
  4. Quick Check:

    Non-blocking updates use old values simultaneously [OK]
Hint: Non-blocking uses old values simultaneously on clock edge [OK]
Common Mistakes:
  • Thinking 'a' updates immediately with new 'b'
  • Confusing blocking and non-blocking timing
  • Ignoring initial values
4. Identify the error in this Verilog code snippet:
always @(posedge clk) begin
  a = b;
  b = 1'b1;
end

where a and b are registers updated on clock edge.
medium
A. Missing sensitivity list for combinational logic
B. Using non-blocking assignments outside always block
C. Incorrect data types for registers
D. Using blocking assignments instead of non-blocking in sequential logic

Solution

  1. Step 1: Check assignment type in clocked always block

    Sequential logic triggered on clock edges should use non-blocking assignments (<=), not blocking (=).
  2. Step 2: Understand consequences of blocking in sequential logic

    Blocking assignments cause race conditions and incorrect simulation of flip-flops.
  3. Final Answer:

    Using blocking assignments instead of non-blocking in sequential logic -> Option D
  4. Quick Check:

    Sequential logic needs non-blocking assignments [OK]
Hint: Use '<=' in clocked always blocks to avoid race conditions [OK]
Common Mistakes:
  • Using '=' in clocked always blocks
  • Ignoring race condition risks
  • Confusing sensitivity lists
5. You want to design a 2-bit register that updates both bits simultaneously on the rising edge of 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
hard
A. Assigns bit1 to both bits, losing original bit0
B. Uses blocking assignments causing race conditions
C. Swaps bits correctly using non-blocking assignments
D. No change in bits, blocking assignments used

Solution

  1. 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 swap bit0 and bit1 simultaneously, avoiding race conditions.
  2. 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 overwrites bit0 and bit1 incorrectly.
    No change in bits, blocking assignments used does not change bits and uses blocking assignments.
  3. Final Answer:

    Swaps bits correctly using non-blocking assignments -> Option C
  4. Quick Check:

    Non-blocking swaps bits simultaneously [OK]
Hint: Use non-blocking to swap registers simultaneously on clock edge [OK]
Common Mistakes:
  • Using blocking assignments in sequential logic
  • Overwriting values before reading them
  • Not updating both bits simultaneously
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard