Edge detector (rising, falling, both) in Verilog - Time & Space Complexity
We want to understand how the time needed to detect signal edges changes as the input size grows.
Specifically, how does the work scale when checking for rising, falling, or both edges in a signal?
Analyze the time complexity of the following Verilog edge detector code.
module edge_detector(
input wire clk,
input wire signal_in,
output reg rising_edge,
output reg falling_edge
);
reg signal_d;
always @(posedge clk) begin
signal_d <= signal_in;
rising_edge <= signal_in & ~signal_d;
falling_edge <= ~signal_in & signal_d;
end
endmodule
This code detects rising and falling edges of a signal synchronized to a clock.
Look for repeated checks or loops in the code.
- Primary operation: The always block runs once every clock cycle.
- How many times: It executes once per clock tick, regardless of input size.
The code checks the signal edges every clock cycle, doing a fixed number of operations each time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of operations grows linearly with the number of clock cycles, but each cycle does a constant amount of work.
Time Complexity: O(1)
This means the work done each clock cycle is constant and does not increase with input size.
[X] Wrong: "The edge detector takes longer as the signal changes more often."
[OK] Correct: The detector checks edges every clock cycle with fixed steps, so frequency of changes does not affect time per cycle.
Understanding fixed-time operations in hardware logic helps you explain how circuits handle signals efficiently and predictably.
What if we added a loop inside the always block to check multiple signals? How would the time complexity change?
Practice
always @(posedge clk) is often used in such detectors.Solution
Step 1: Understand rising edge meaning
A rising edge means the signal changes from low (0) to high (1).Step 2: Relate to Verilog edge detection
Theposedgekeyword triggers on this rising edge.Final Answer:
A change of signal from 0 to 1 -> Option CQuick Check:
Rising edge = 0 to 1 transition [OK]
- Confusing rising edge with falling edge
- Thinking it detects no change
- Mixing up posedge and negedge keywords
sig?Solution
Step 1: Identify falling edge keyword
Falling edge detection usesnegedgein Verilog.Step 2: Match code to falling edge
always @(negedge sig) begin // detect falling edge end usesalways @(negedge sig), which triggers on signal going from 1 to 0.Final Answer:
always @(negedge sig) begin // detect falling edge end -> Option AQuick Check:
Falling edge = negedge keyword [OK]
- Using posedge for falling edge detection
- Using level-sensitive sensitivity list
- Confusing signal and clock edges
sig:reg sig_d; always @(posedge clk) begin sig_d <= sig; end wire edge_detect = sig ^ sig_d;
What is the value of
edge_detect when sig changes from 0 to 1?Solution
Step 1: Understand delayed signal
sig_dsig_dholds previous clock cycle'ssigvalue.Step 2: Calculate XOR for edge detection
Whensigchanges from 0 to 1, XOR with previous 0 gives 1, indicating an edge.Final Answer:
1 -> Option AQuick Check:
sig ^ sig_d = 1 on edge [OK]
- Forgetting to delay signal before XOR
- Assuming edge_detect stays 0 on change
- Confusing XOR with AND or OR
sig:reg sig_d; always @(posedge clk) begin sig_d <= sig; end wire rising_edge = sig & ~sig_d;
Solution
Step 1: Analyze rising edge logic
Rising edge is when signal changes from 0 to 1, so previous was 0 and current is 1.Step 2: Check given logic expression
The expressionsig & ~sig_dcorrectly detects rising edge by current high and previous low.Step 3: Re-examine for errors
Actually, the logic is correct; the error is elsewhere.Step 4: Identify actual error
Missing initial value forsig_dcan cause unknown behavior at start.Final Answer:
Missing initial value for sig_d -> Option DQuick Check:
Initialize delayed signal to avoid unknowns [OK]
- Assuming logic expression is wrong
- Ignoring initial values for registers
- Confusing blocking and non-blocking assignments
sig changes either from 0 to 1 or 1 to 0. Which code snippet correctly implements this?Solution
Step 1: Understand requirement for both edges
The pulse should be high for one clock cycle on any signal change (rising or falling).Step 2: Analyze each option
reg sig_d; always @(posedge clk) sig_d <= sig; wire edge_pulse = sig ^ sig_d; uses XOR between current and delayed signal, which is 1 only on change, producing a 1-cycle pulse.Step 3: Confirm other options
reg sig_d; always @(posedge clk) sig_d <= sig; wire edge_pulse = sig & ~sig_d; detects only rising edge; C is level detection; D triggers on posedge of sig, not clock, and lacks pulse width control.Final Answer:
reg sig_d; always @(posedge clk) sig_d <= sig; wire edge_pulse = sig ^ sig_d; -> Option BQuick Check:
XOR current and delayed signal for both edges pulse [OK]
- Using only rising edge logic for both edges
- Triggering on signal edge instead of clock
- Not producing single clock cycle pulse
Ready to prove it?
Pass the test and this pattern moves unknown
