Modulo-N counter in Verilog - Time & Space Complexity
We want to understand how the time it takes for a modulo-N counter to run changes as the size of N changes.
Specifically, how does the number of steps grow when the counter counts up to N?
Analyze the time complexity of the following code snippet.
module modulo_n_counter #(
parameter int N = 16
) (
input logic clk,
input logic reset,
output logic [$clog2(N)-1:0] count
);
always_ff @(posedge clk or posedge reset) begin
if (reset) count <= 0;
else if (count == N-1) count <= 0;
else count <= count + 1;
end
endmodule
This code counts from 0 up to N-1, then resets back to 0, repeating continuously.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The counter increments by 1 on each clock cycle.
- How many times: It repeats counting up to N times before resetting.
Explain the growth pattern intuitively.
| Input Size (N) | Approx. Operations (increments) |
|---|---|
| 10 | 10 increments before reset |
| 100 | 100 increments before reset |
| 1000 | 1000 increments before reset |
Pattern observation: The number of increments grows directly with N. If N doubles, the count steps double too.
Time Complexity: O(N)
This means the time to complete one full count grows linearly as N gets bigger.
[X] Wrong: "The counter runs in constant time because it just increments once per clock."
[OK] Correct: While each increment is quick, the total time to count all the way to N depends on N itself, so it grows as N grows.
Understanding how counters scale helps you reason about timing and performance in hardware design, a useful skill in many real-world projects.
"What if we changed the counter to increment by 2 each time instead of 1? How would the time complexity change?"
Practice
What does a modulo-N counter do in Verilog?
Solution
Step 1: Understand the counting range
A modulo-N counter counts numbers starting from 0 up to N-1.Step 2: Recognize the reset behavior
After reaching N-1, it restarts counting from 0 again, repeating the cycle.Final Answer:
Counts from 0 up to N-1 and then restarts at 0 -> Option AQuick Check:
Modulo-N counter = counts 0 to N-1 then resets [OK]
- Thinking it counts down instead of up
- Assuming it stops after one cycle
- Confusing modulo-N with random counting
Which of the following is the correct Verilog syntax to declare a 4-bit register count for a modulo-16 counter?
Solution
Step 1: Determine bit width for modulo-16
Modulo-16 means counting 0 to 15, which fits in 4 bits (2^4=16).Step 2: Choose correct register declaration
Usereg [3:0] count;to declare a 4-bit register named count.Final Answer:
reg [3:0] count; -> Option CQuick Check:
4 bits needed for 16 states [OK]
- Using wire instead of reg for a counter
- Using 5 bits instead of 4 bits
- Using integer which is not fixed bit width
What is the output sequence of count in this Verilog code snippet for a modulo-4 counter after 6 clock cycles (assuming reset is low)?
reg [1:0] count = 0;
always @(posedge clk or posedge reset) begin
if (reset)
count <= 0;
else if (count == 3)
count <= 0;
else
count <= count + 1;
endSolution
Step 1: Trace count values per clock
Starting at 0, count increments each clock until 3, then resets to 0:
Cycle 1: 0
Cycle 2: 1
Cycle 3: 2
Cycle 4: 3
Cycle 5: 0 (reset)
Cycle 6: 1Step 2: Confirm sequence length and values
Sequence after 6 cycles is [0, 1, 2, 3, 0, 1].Final Answer:
[0, 1, 2, 3, 0, 1] -> Option DQuick Check:
Modulo-4 cycles 0 to 3 then repeats [OK]
- Counting beyond 3 (like 4 or 5)
- Starting count at 1 instead of 0
- Forgetting reset condition when count == 3
Identify the error in this Verilog code for a modulo-8 counter:
reg [2:0] count;
always @(posedge clk or posedge reset) begin
if (reset)
count <= 0;
else if (count == 8)
count <= 0;
else
count <= count + 1;
endSolution
Step 1: Check counting range for modulo-8
Modulo-8 counts from 0 to 7, so max count is 7, not 8.Step 2: Identify incorrect condition
The code checks if count == 8, which is invalid since count cannot reach 8 with 3 bits.Final Answer:
The condition should be 'count == 7' instead of 'count == 8' -> Option BQuick Check:
Max count for 3 bits is 7, not 8 [OK]
- Using count == 8 instead of count == 7
- Declaring count as wire instead of reg
- Misunderstanding reset edge sensitivity
You want to design a modulo-10 counter in Verilog that counts from 0 to 9 and then resets to 0. Which code snippet correctly implements this behavior?
reg [3:0] count;
always @(posedge clk or posedge reset) begin
if (reset)
count <= 0;
else if (count == 9)
count <= 0;
else
count <= count + 1;
endSolution
Step 1: Check bit width for modulo-10
4 bits can represent values from 0 to 15, which covers 0 to 9 easily.Step 2: Verify reset condition
The code resets count to 0 when count reaches 9, correctly implementing modulo-10 behavior.Final Answer:
This code is correct because 4 bits can represent 0 to 15, and count resets at 9. -> Option AQuick Check:
4 bits cover 0-9; reset at 9 is correct [OK]
- Thinking 4 bits can't hold 0 to 9
- Resetting count at 10 instead of 9
- Declaring count as wire instead of reg
Ready to prove it?
Pass the test and this pattern moves unknown
