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.
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.
| Time | Event | sample_i | rise |
|---|---|---|---|
| 5 ns | E0 reset | 1 | 0 |
| 15 ns | E1 first high sample | 1 | 1 |
| 25 ns | E2 stays high | 1 | 0 |
| 35 ns | E3 samples low | 0 | 0 |
| 37 to 41 ns | High pulse between edges | 1→0 | 0 |
| 45 ns | E4 only samples low | 0 | 0 |
| 55 ns | E5 samples high again | 1 | 1 |
| 65 ns | E6 stays high | 1 | 0 |
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
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)