Up counter design in Verilog - Time & Space Complexity
We want to understand how the time it takes for an up counter to run grows as the count value increases.
How does the number of steps change when the counter counts higher?
Analyze the time complexity of the following code snippet.
module up_counter(
input wire clk,
input wire reset,
output reg [3:0] count
);
always @(posedge clk or posedge reset) begin
if (reset)
count <= 0;
else
count <= count + 1;
end
endmodule
This code defines a 4-bit up counter that increases its value by 1 on each clock pulse, resetting to zero when reset is high.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Incrementing the count value on each clock cycle.
- How many times: Once per clock pulse, repeating continuously.
Each clock pulse causes one increment operation, so the number of increments grows directly with the count value.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 increments |
| 100 | 100 increments |
| 1000 | 1000 increments |
Pattern observation: The number of operations grows in a straight line as the count increases.
Time Complexity: O(n)
This means the time to reach a certain count grows directly with the size of that count.
[X] Wrong: "The counter increments instantly for any count value, so time does not grow with count."
[OK] Correct: Each increment happens on a clock pulse, so counting higher means more pulses and more time.
Understanding how counters work and how their operation time grows helps you reason about hardware timing and performance in real designs.
"What if we changed the counter to increment by 2 each clock cycle? How would the time complexity change?"
Practice
Solution
Step 1: Understand the function of an up counter
An up counter increments its stored value by 1 every time the clock signal pulses.Step 2: Compare options with the definition
Only It increases its value by 1 on each clock pulse. describes increasing the value by 1 on each clock pulse, which matches the up counter behavior.Final Answer:
It increases its value by 1 on each clock pulse. -> Option DQuick Check:
Up counter = increments by 1 [OK]
- Confusing up counter with down counter
- Thinking reset happens every clock pulse
- Assuming counter holds value without change
Solution
Step 1: Identify correct clock edge and reset behavior
Up counters usually update on the positive edge of the clock and reset synchronously to zero.Step 2: Check assignment operators and counting direction
Non-blocking assignment '<=' is preferred in sequential logic. Counting up means adding 1, and reset sets count to 0.Final Answer:
always @(posedge clk) if (reset) count <= 0; else count <= count + 1; -> Option BQuick Check:
posedge + non-blocking + reset to 0 + count up = A [OK]
- Using negedge instead of posedge
- Using blocking '=' in sequential logic
- Resetting count to 1 instead of 0
- Counting down instead of up
module counter(input clk, input reset, output reg [2:0] count);
always @(posedge clk) begin
if (reset) count <= 0;
else count <= count + 1;
end
endmoduleSolution
Step 1: Understand initial value and counting
The counter starts at 0 after reset. On first clock pulse, count increments to 1.Step 2: List values after each clock pulse
After 1st pulse: 1, 2nd: 2, 3rd: 3, 4th: 4, 5th: 5.Step 3: Consider 3-bit counter wrap-around
Since it's a 3-bit counter, it counts from 0 to 7 and then wraps around. After 5 pulses starting from 0, the sequence is [1, 2, 3, 4, 5]. However, the question asks for the output sequence starting from 0 after 5 pulses, which includes the initial 0 and the next 4 increments: [0, 1, 2, 3, 4].Final Answer:
[0, 1, 2, 3, 4] -> Option AQuick Check:
Count starts at 0, increments each pulse = [0..4] [OK]
- Including initial 0 before first pulse
- Counting 6 values instead of 5
- Confusing count start value
module counter(input clk, input reset, output reg [3:0] count);
always @(posedge clk) begin
if (reset == 1) count = 0;
else count = count + 1;
end
endmoduleSolution
Step 1: Check assignment type in sequential always block
Inside always @(posedge clk), non-blocking assignments '<=' should be used to avoid race conditions.Step 2: Verify other parts of the code
Reset condition and reg declaration are correct. Clock edge is correctly posedge.Final Answer:
Using blocking assignments '=' instead of non-blocking '<=' inside always block. -> Option CQuick Check:
Sequential logic uses '<=' not '=' [OK]
- Using '=' in always @(posedge clk)
- Misunderstanding reset condition
- Thinking reg is wrong for output
- Changing clock edge unnecessarily
module counter(input clk, input reset, output reg [3:0] count);
always @(posedge clk) begin
if (reset) count <= 0;
else count <= count + 1;
end
endmoduleSolution
Step 1: Understand how to control counting with enable
Counting should happen only when enable is high, so add a condition inside the always block after reset.Step 2: Implement enable check correctly
Use 'if (enable) count <= count + 1;' with non-blocking assignment inside the always block.Final Answer:
Add 'if (enable) count <= count + 1;' inside the always block after reset check. -> Option AQuick Check:
Enable controls counting inside always block [OK]
- Using blocking assignment outside always block
- Changing count direction instead of gating
- Adding enable to sensitivity list (not needed)
Ready to prove it?
Pass the test and this pattern moves unknown
