📖 Scenario: You are designing a simple digital circuit that counts the number of clock cycles. You want to update the count correctly on each clock edge without errors caused by simultaneous updates.
🎯 Goal: Build a Verilog module that uses non-blocking assignments to update a counter sequentially on the clock's rising edge.
📋 What You'll Learn
Create a 4-bit register called count initialized to 0
Create a clock input called clk
Use a non-blocking assignment inside an always @(posedge clk) block to increment count
Print the new value of count after each clock cycle
💡 Why This Matters
🌍 Real World
Counters are used in many digital circuits like timers, frequency dividers, and state machines. Using non-blocking assignments ensures the counter updates correctly on clock edges.
💼 Career
Understanding when to use non-blocking assignments is essential for hardware design engineers working with synchronous digital circuits and FPGA or ASIC development.
Progress0 / 4 steps
1
DATA SETUP: Create a 4-bit register called count initialized to 0
Write a Verilog line to declare a 4-bit register named count and initialize it to 0.
Verilog
Hint
Use reg [3:0] count = 0; to declare a 4-bit register initialized to zero.
2
CONFIGURATION: Declare an input clock signal called clk
Add a line to declare an input signal named clk to represent the clock.
Verilog
Hint
Use input clk; to declare the clock input.
3
CORE LOGIC: Use a non-blocking assignment inside an always block triggered on the rising edge of clk to increment count
Write an always @(posedge clk) block that increments count by 1 using a non-blocking assignment <=.
Verilog
Hint
Use always @(posedge clk) and inside it write count <= count + 1; to update count sequentially.
4
OUTPUT: Display the value of count after each clock cycle
Add a line to print the new value of count inside the always block after the assignment using $display.
Verilog
Hint
Use $display("Count value: %d", count + 1); to print the new count value.
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
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 B
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
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 A
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
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.
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.
Final Answer:
Swaps bits correctly using non-blocking assignments -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.