PWM generator in VHDL - Time & Space Complexity
We want to understand how the time needed to run a PWM generator changes as the input size changes.
Specifically, we ask: how does the number of operations grow when the PWM period or resolution changes?
Analyze the time complexity of the following code snippet.
process(clk)
variable counter : integer range 0 to PERIOD := 0;
begin
if rising_edge(clk) then
if counter < PERIOD then
counter := counter + 1;
else
counter := 0;
end if;
pwm_out <= '1' when counter < duty_cycle else '0';
end if;
end process;
This code creates a PWM signal by counting clock cycles up to a set period and comparing the count to the duty cycle.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The counter increments once every clock cycle until it reaches the period.
- How many times: It repeats exactly once per clock cycle, resetting after reaching the period value.
As the period value increases, the counter counts more steps before resetting, but each clock cycle still does only one increment operation.
| Input Size (Period) | Approx. Operations per Cycle |
|---|---|
| 10 | 1 increment + 1 compare |
| 100 | 1 increment + 1 compare |
| 1000 | 1 increment + 1 compare |
Pattern observation: The number of operations per clock cycle stays the same regardless of period size.
Time Complexity: O(1)
This means the time to process each clock cycle does not grow with the period size; it stays constant.
[X] Wrong: "The time to generate PWM grows as the period gets bigger because the counter counts more."
[OK] Correct: The counter increments once per clock cycle, so each cycle's work is the same no matter the period length.
Understanding how hardware counters work and their time behavior helps you explain efficient designs clearly and confidently.
"What if we changed the PWM generator to use a nested loop to count multiple periods before resetting? How would the time complexity change?"
Practice
Solution
Step 1: Understand PWM function
PWM (Pulse Width Modulation) generates a signal that switches on and off rapidly.Step 2: Identify purpose in power control
This switching controls the power delivered to devices by changing the duty cycle.Final Answer:
To create a signal that switches on and off quickly to control power -> Option AQuick Check:
PWM controls power by switching signal [OK]
- Confusing PWM with memory storage
- Thinking PWM does arithmetic
- Assuming PWM generates random signals
Solution
Step 1: Identify correct VHDL assignment syntax
In VHDL, signal assignment uses '<=' and conditional assignment uses 'when ... else'.Step 2: Match correct comparison and syntax
pwm_out <= '1' when counter < duty_cycle else '0'; uses '<=' and 'when ... else' with correct comparison 'counter < duty_cycle'.Final Answer:
pwm_out <= '1' when counter < duty_cycle else '0'; -> Option CQuick Check:
VHDL signal conditional assignment = pwm_out <= '1' when counter < duty_cycle else '0'; [OK]
- Using ':=' instead of '<=' for signals
- Using 'if' instead of 'when' in signal assignment
- Using '=' for assignment instead of comparison
process(clk)
begin
if rising_edge(clk) then
if counter < duty_cycle then
pwm_out <= '1';
else
pwm_out <= '0';
end if;
if counter = max_count then
counter <= 0;
else
counter <= counter + 1;
end if;
end if;
end process;What is the value of
pwm_out when counter is 3 and duty_cycle is 5?Solution
Step 1: Compare counter and duty_cycle values
Counter is 3, duty_cycle is 5, so 3 < 5 is true.Step 2: Determine pwm_out value based on condition
Since counter < duty_cycle, pwm_out is assigned '1'.Final Answer:
'1' -> Option BQuick Check:
3 < 5 means pwm_out = '1' [OK]
- Confusing '<' with '>' in condition
- Assuming pwm_out is '0' when counter < duty_cycle
- Ignoring the process clock edge condition
process(clk)
begin
if clk'event and clk = '1' then
if counter <= duty_cycle then
pwm_out <= '1';
else
pwm_out <= '0';
end if;
if counter = max_count then
counter <= 0;
else
counter <= counter + 1;
end if;
end if;
end process;Solution
Step 1: Check clock edge detection method
Using 'clk'event and clk = '1' is legal but less safe than rising_edge(clk).Step 2: Identify best practice for clock edge
rising_edge(clk) is preferred for clarity and synthesis compatibility.Final Answer:
Using 'clk'event and clk = '1' instead of rising_edge(clk) -> Option AQuick Check:
Use rising_edge(clk) for clock detection [OK]
- Using 'clk'event and clk = '1' which is outdated
- Confusing '<=' with '<' in duty cycle comparison
- Ignoring reset logic necessity
Solution
Step 1: Understand dynamic frequency and duty cycle needs
Changing frequency means changing the PWM period, so a period counter is needed.Step 2: Use counters for period and duty cycle comparison
One counter tracks the period length, another compares to duty cycle to set output.Step 3: Allow dynamic updates
Both counters and duty cycle values must be variable to change PWM parameters on the fly.Final Answer:
Use two counters: one for period (frequency) and one for duty cycle comparison, both updated dynamically -> Option DQuick Check:
Two counters enable dynamic frequency and duty cycle [OK]
- Fixing duty cycle or frequency at compile time
- Using asynchronous signals causing glitches
- Trying to change frequency without a period counter
Ready to prove it?
Pass the test and this pattern moves unknown
