Skip to content
systemcoding.dev
Hardware

Detect a rising edge with a registered input in Verilog

Delay a signal by one clock in a flip-flop and AND it with the inverted copy to get a one-cycle rising-edge pulse in Verilog-2005.

1 min read
Summarize in AI

To act once when a button or strobe goes high, keep the previous value in a flip-flop and compare it with the current one. The pulse is high for exactly one clock, however long the input stays high.

edge_detect.vverilog
module edge_detect (
    input  wire clk,
    input  wire sig,
    output wire rise
);
    reg prev = 1'b0;

    always @(posedge clk) prev <= sig;

    assign rise = sig & ~prev;
endmodule

Here is how the signals line up over six clock cycles:

Text
cycle : 0 1 2 3 4 5
sig   : 0 0 1 1 1 0
prev  : 0 0 0 1 1 1
rise  : 0 0 1 0 0 0

prev is sig delayed by one clock, so in cycle 2 the input is already high while the register still holds 0. That is the only cycle where both conditions hold. Swap the terms (prev & ~sig) and you get the falling edge, and OR the two for any change.

The input must already be synchronous to clk. A raw pin or a signal from another clock domain needs two flip-flops in front of it first, otherwise prev can go metastable and rise can glitch or fire twice. Note that rise is combinational, so register it again if it drives a long path.

Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.