Edge detector

Compare a synchronous input with its previous sample to turn a low-to-high transition into a one-cycle event. Trace the first sample after reset and examine a short pulse crossing clock domains that the receiver can miss entirely.

An input changes from low to high and stays there. The receiver needs one event. Save the previous sample and compare it with the next one to produce a one-cycle pulse.

The previous sample is low and the current sample is high

sample_i is a one-bit input for this clock domain. prev_q remembers the previous sample. A rising edge uses old prev_q to compute rise and stores this sample for the next comparison. Two consecutive highs bring rise back to zero.

After reset prev_q is zero. E1 reads sample_i high and updates rise to one.
The comparison uses the pre-update prev_q.

Sample and compare on the same edge

Both nonblocking assignments share the rising edge. Reset clears the history to zero, so this policy counts a first high sample after reset.

module edge_detector (
    input  logic       clk,
    input  logic       rst_n,
    input  logic       sample_i,
    output logic       rise
);
    logic prev_q;

    always_ff @(posedge clk) begin
        if (!rst_n) begin
            prev_q <= 1'b0;
            rise <= 1'b0;
        end else begin
            rise <= sample_i && !prev_q;
            prev_q <= sample_i;
        end
    end
endmodule

A held high and a short pulse between samples

sample_i is already high during reset. E1 produces a pulse after release. E2 samples another high without producing a new event. After lowering the input, the test applies a short high pulse from 37 to 41 ns.

TimeEventsample_irise
5 nsE0 reset10
15 nsE1 first high sample11
25 nsE2 stays high10
35 nsE3 samples low00
37 to 41 nsHigh pulse between edges1→00
45 nsE4 only samples low00
55 nsE5 samples high again11
65 nsE6 stays high10
E1 and E5 each produce a rise pulse. The short 37 to 41 ns pulse lies between E3 and E4 and leaves rise unchanged.
E3 and E4 both sample low. The brief high between them goes unobserved.

A pulse from another clock domain can likewise fall between all destination samples. This trace demonstrates the sampling geometry in RTL. A crossing also has to account for metastability and possible event loss. When each event must arrive, a protocol can hold the request until the destination acknowledges it.

Replay the lab

Extract the executable lab on Linux or WSL and run bash run.sh. Icarus Verilog 12.0 checked 9 timed observations. The archive contains the RTL, testbench, log, VCD and generic Yosys structure. The lab checks both sampled transitions and a pulse strictly between clock edges. Continue with F08 on sampling and updates, F09 on synchronous work events, F14 on clock-domain crossings.

Interactive lab

Sample reset, then release it with sample_i still high. Apply two edges and compare rise.

Stored history prev_q
Rising-event pulse rise
Show the last eight observations

    Teaching film

    Read the transcript

    Run the RTL yourself

    Extract the lab and follow the commands in its README. Compare the simulated outputs and waveform with your predictions from the interactive lab.

    Download Edge detector RTL (.zip)

    Check your understanding

    1. E1 is the first sample after reset and sample_i is high. What happens to rise?
    2. E2 samples a high value and the previous sample was also high. What is the new rise?
    3. A high pulse from 37 to 41 ns is entirely between the 35 and 45 ns edges. What does E4 see?
    4. Both assignments run on the same rising edge. Which prev_q does the rise expression use?
    5. If every event must cross clock domains, what does this experiment tell you?