An edge detector helps you find when a signal changes from low to high or high to low. This is useful to react exactly at the moment a change happens.
Edge detector (rising, falling, both) in Verilog
module edge_detector(
input wire clk,
input wire signal_in,
output wire rising_edge,
output wire falling_edge,
output wire any_edge
);
reg signal_delayed;
always @(posedge clk) begin
signal_delayed <= signal_in;
end
assign rising_edge = signal_in & ~signal_delayed;
assign falling_edge = ~signal_in & signal_delayed;
assign any_edge = rising_edge | falling_edge;
endmoduleThe input signal is sampled on the clock's rising edge to detect changes.
We use a delayed version of the signal to compare current and previous states.
assign rising_edge = signal_in & ~signal_delayed;
assign falling_edge = ~signal_in & signal_delayed;
assign any_edge = rising_edge | falling_edge;
This testbench creates a clock and changes the input signal to show how the edge detector outputs change.
module test_edge_detector(); reg clk = 0; reg signal_in = 0; wire rising_edge, falling_edge, any_edge; // Instantiate the edge detector edge_detector uut( .clk(clk), .signal_in(signal_in), .rising_edge(rising_edge), .falling_edge(falling_edge), .any_edge(any_edge) ); // Clock generation: 10 time units period always #5 clk = ~clk; initial begin $display("Time clk signal_in rising_edge falling_edge any_edge"); $monitor("%4d %b %b %b %b %b", $time, clk, signal_in, rising_edge, falling_edge, any_edge); // Wait for a few clock cycles #10; signal_in = 1; // rising edge #10; signal_in = 0; // falling edge #10; signal_in = 1; // rising edge #10; signal_in = 1; // no edge #10; signal_in = 0; // falling edge #10; $finish; end endmodule
Make sure the input signal is synchronized with the clock to avoid glitches.
The edge detector only works correctly if the clock is fast enough to catch changes.
Delaying the signal by one clock cycle helps compare current and previous states easily.
Edge detectors find when signals change from low to high or high to low.
They use a delayed signal to compare past and current values.
Outputs can detect rising edges, falling edges, or both.
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
