Always block sensitivity list in Verilog - Time & Space Complexity
We want to understand how the execution time of an always block changes as the inputs it watches change.
How does the number of operations grow when signals in the sensitivity list change?
Analyze the time complexity of the following Verilog always block.
always @(posedge clk or negedge reset_n) begin
if (!reset_n) begin
count <= 0;
end else begin
count <= count + 1;
end
end
This block increments a counter on each clock rising edge, or resets it when reset is low.
Look for repeated actions triggered by signals.
- Primary operation: The block runs once every time the clock rises or reset falls.
- How many times: It runs once per clock cycle or reset event, not continuously.
The block runs only when signals in the sensitivity list change.
| Input Size (number of signal changes) | Approx. Operations |
|---|---|
| 10 clock cycles | 10 executions |
| 100 clock cycles | 100 executions |
| 1000 clock cycles | 1000 executions |
Pattern observation: The number of executions grows linearly with the number of signal changes.
Time Complexity: O(n)
This means the block runs once for each input event, so the total work grows directly with the number of events.
[X] Wrong: "The always block runs continuously and uses a lot of time even if inputs don't change."
[OK] Correct: The always block only runs when signals in its sensitivity list change, so it does not waste time running when inputs are stable.
Understanding how always blocks trigger helps you write efficient hardware code and explain timing behavior clearly in interviews.
"What if we add more signals to the sensitivity list? How would that affect the time complexity?"
Practice
always block control?Solution
Step 1: Understand the role of sensitivity list
The sensitivity list specifies which signals trigger thealwaysblock to run when they change.Step 2: Differentiate from unrelated concepts
Data types, clock frequency, and synthesis optimization are unrelated to sensitivity lists.Final Answer:
When thealwaysblock executes based on signal changes -> Option CQuick Check:
Sensitivity list controls execution timing [OK]
- Confusing sensitivity list with clock frequency
- Thinking it controls variable types
- Assuming it affects synthesis optimization
always block sensitivity list that triggers on the positive edge of clock clk and any change in reset rst?Solution
Step 1: Recall correct sensitivity list syntax
The correct syntax usesposedge clkandorto separate signals:always @(posedge clk or rst).Step 2: Identify incorrect syntax in other options
A usesandinstead ofor, B uses wrong orderclk posedge, D uses comma which is invalid.Final Answer:
always @(posedge clk or rst) -> Option BQuick Check:
Use 'or' to separate signals in sensitivity list [OK]
- Using 'and' instead of 'or' in sensitivity list
- Placing 'posedge' after signal name
- Separating signals with commas
reg q;
always @(posedge clk or posedge rst) begin
if (rst)
q <= 0;
else
q <= d;
endWhat happens to
q when rst goes high?Solution
Step 1: Analyze sensitivity list triggers
The block triggers on positive edge ofclkorrst. Whenrstgoes high, the block runs immediately.Step 2: Check the if condition inside the block
Ifrstis high,qis assigned 0, resetting it.Final Answer:
qis set to 0 immediately -> Option AQuick Check:
Reset high sets q to 0 [OK]
- Assuming q holds value on reset
- Thinking q updates only on clock edge
- Confusing reset with data input
always @(clk or rst) begin
if (rst)
q <= 0;
else if (posedge clk)
q <= d;
endSolution
Step 1: Check sensitivity list correctness
The sensitivity list usesclkandrstwithout edges, so it triggers on any change.Step 2: Identify improper use of
You cannot useposedge clkinside blockposedge clkas a condition inside the block; it must be in sensitivity list.Final Answer:
Usingposedge clkinside the block instead of sensitivity list -> Option DQuick Check:
Edge triggers belong in sensitivity list [OK]
- Placing edge conditions inside always block
- Ignoring sensitivity list edges
- Confusing blocking and nonblocking assignments
y whenever any input signal a, b, or c changes. Which sensitivity list is best practice to ensure correct behavior?Solution
Step 1: Understand combinational logic sensitivity
For combinational logic, sensitivity list should include all inputs to avoid simulation mismatches.Step 2: Use
@(*)for automatic sensitivity@(*)automatically includes all signals read inside the block, ensuring correctness and easier maintenance.Final Answer:
always @(*) -> Option AQuick Check:
Use @(*) for combinational logic sensitivity [OK]
- Listing signals manually and missing some
- Using edge triggers for combinational logic
- Using 'and' instead of 'or' in sensitivity list
Ready to prove it?
Pass the test and this pattern moves unknown
