Pulse generator in Verilog - Time & Space Complexity
We want to understand how the time needed to generate pulses changes as we change the input size.
Specifically, how does the pulse generator's work grow when the input count or width changes?
Analyze the time complexity of the following code snippet.
module pulse_generator(
input wire clk,
input wire rst,
output reg pulse
);
reg [3:0] counter = 0;
always @(posedge clk or posedge rst) begin
if (rst) counter <= 0;
else if (counter == 9) begin
pulse <= 1;
counter <= 0;
end else begin
pulse <= 0;
counter <= counter + 1;
end
end
endmodule
This code creates a pulse every 10 clock cycles by counting from 0 to 9.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The counter increments on every clock cycle.
- How many times: It repeats once per clock cycle continuously.
Each clock cycle triggers one increment operation regardless of the counter size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 increments |
| 100 | 100 increments |
| 1000 | 1000 increments |
Pattern observation: The number of operations grows linearly with the number of clock cycles.
Time Complexity: O(n)
This means the work grows directly in proportion to the number of clock cycles or input size.
[X] Wrong: "The pulse generator does all counting steps at once, so it runs in constant time."
[OK] Correct: The counter updates once per clock cycle, so the total time depends on how many cycles pass, not just one step.
Understanding how hardware counters scale with input size helps you explain timing and performance in digital designs clearly and confidently.
"What if we changed the counter width to count to 1000 instead of 10? How would the time complexity change?"
Practice
Solution
Step 1: Understand the function of a pulse generator
A pulse generator creates short bursts of signals that switch between high and low states.Step 2: Compare options with the definition
Only To create short on/off signals in digital circuits describes creating short on/off signals, which matches the pulse generator's role.Final Answer:
To create short on/off signals in digital circuits -> Option DQuick Check:
Pulse generator = short on/off signals [OK]
- Confusing pulse generator with memory storage
- Thinking it performs calculations
- Mixing it up with analog to digital converters
Solution
Step 1: Recall Verilog register declaration syntax
Registers use 'reg' keyword and bit width is declared as [high:low].Step 2: Check each option
reg [3:0] counter; correctly declares a 4-bit register from bit 3 down to 0. wire [4:0] counter; uses 'wire' which is not for registers. reg counter[3:0]; uses incorrect array syntax. integer counter = 4; declares an integer, not a 4-bit register.Final Answer:
reg [3:0] counter; -> Option CQuick Check:
4-bit reg = reg [3:0] name; [OK]
- Using wire instead of reg for counters
- Incorrect bit range syntax
- Confusing arrays with bit vectors
reg [3:0] count = 0;
reg pulse = 0;
always @(posedge clk or posedge reset) begin
if (reset) begin
count <= 0;
pulse <= 0;
end else if (count == 4) begin
pulse <= 1;
count <= 0;
end else begin
pulse <= 0;
count <= count + 1;
end
endWhat will be the value of
pulse after 5 clock cycles without reset?Solution
Step 1: Trace the counter and pulse values over clock cycles
Count starts at 0. Each clock cycle increments count by 1 until it reaches 4. At count == 4, pulse is set to 1 and count resets to 0.Step 2: Determine pulse value at 5th clock
At 5th clock, count was 4, so pulse becomes 1.Final Answer:
1 -> Option BQuick Check:
Pulse high at count 4 = 1 [OK]
- Assuming pulse stays high after count resets
- Confusing reset behavior
- Ignoring the else conditions
reg [3:0] count;
reg pulse;
always @(posedge clk) begin
if (reset) begin
count = 0;
pulse = 0;
end else if (count == 4) begin
pulse = 1;
count = 0;
end else begin
pulse = 0;
count = count + 1;
end
endSolution
Step 1: Check assignment types in sequential always block
Inside always @(posedge clk), non-blocking assignments (<=) should be used to avoid race conditions.Step 2: Identify the error
The code uses blocking assignments (=) which is incorrect for sequential logic.Final Answer:
Using blocking assignments (=) inside always @(posedge clk) -> Option AQuick Check:
Sequential logic uses <=, not = [OK]
- Using = instead of <= in sequential blocks
- Not including reset in sensitivity list
- Declaring pulse as wire instead of reg
Solution
Step 1: Understand the timing requirements
The pulse should be high for 3 cycles and repeat every 10 cycles, so count cycles from 0 to 9.Step 2: Choose counter and pulse logic
A 4-bit counter can count up to 15, enough for 10 cycles. Setting pulse high when count is less than 3 gives a 3-cycle pulse.Final Answer:
Use a 4-bit counter that resets at 10, set pulse high when count < 3 -> Option AQuick Check:
Pulse high 3 cycles every 10 cycles = Use a 4-bit counter that resets at 10, set pulse high when count < 3 [OK]
- Using wrong counter size
- Setting pulse high at wrong count values
- Confusing reset and pulse conditions
Ready to prove it?
Pass the test and this pattern moves unknown
