Why signals that cross between clocks fail, and the standard fixes: two-flop synchronizers, pulse synchronizers, handshakes and asynchronous FIFOs, with Verilog for each.
By BitFlow · · 8 min read
The problem
A clock domain is all the flip-flops driven by one clock. A clock domain crossing (CDC) is any signal launched by a flip-flop in one domain and captured by a flip-flop in another. When the two clocks are unrelated, the data can change at any moment relative to the capturing edge, so sooner or later it lands inside the setup-hold window.
The capturing flip-flop may then go metastable: its output sits between 0 and 1 for an unpredictable time. Static timing analysis can't help, because there is no fixed relationship between the clocks to analyse. CDC has to be handled in the design.
Single bits: the two-flop synchronizer
For one level signal (a flag that stays high for many cycles), pass it through two flip-flops in the destination domain. The first may go metastable; the second samples it a full clock period later, by which time it has almost certainly settled.
module sync2 (input clk_dst, input d_async, output q);
(* ASYNC_REG = "TRUE" *) reg s1, s2;
always @(posedge clk_dst) begin
s1 <= d_async; // may go metastable
s2 <= s1; // safe to use
end
assign q = s2;
endmodule
Watch outThe source signal must come straight from a flip-flop. Combinational logic before the crossing can glitch, and the synchronizer may catch the glitch.
Pulses: toggle and synchronize
A one-cycle pulse from a fast clock can fall entirely between two edges of a slow clock and be missed. Convert it into a level change first: toggle a flip-flop on every pulse, synchronize the toggle, then detect its edge on the other side.
// source domain
reg tog;
always @(posedge clk_a) if (pulse_a) tog <= ~tog;
// destination domain
reg [2:0] s;
always @(posedge clk_b) s <= {s[1:0], tog};
assign pulse_b = s[2] ^ s[1]; // one pulse per toggle
Pulses must be spaced at least a few destination-clock cycles apart, or two toggles merge into none.
Multi-bit values
Never put each bit of a bus through its own synchronizer. The bits settle at slightly different times, so the destination can see a value that was never sent (from 0111 to 1000, it might read 1111). Use one of these instead:
Method
How it works
Use when
Handshake (req/ack)
Hold the data steady, synchronize a single request bit, send back an acknowledge
Occasional transfers, throughput doesn't matter
Gray code
Only one bit changes per step, so a half-captured value is either the old or the new one
Counters and FIFO pointers
Asynchronous FIFO
Dual-port memory with Gray-coded read and write pointers synchronized across
Streams of data between domains
Mux recirculation
Synchronize an enable; the data bus is sampled only when the enable says it's stable
Wide buses with a valid signal
Checking CDC
Mark every crossing in the timing constraints (set_clock_groups -asynchronous or set_false_path) so STA doesn't report false failures, and so you know where they all are.
Use a structural CDC checker (Questa CDC, SpyGlass, or Verilator lint plus review) to find crossings with no synchronizer.
Clock domain crossing is when a signal generated by a flip-flop on one clock is captured by a flip-flop on a different, unrelated clock. Without a synchronizer the capturing flip-flop can go metastable.
Why are two flip-flops used in a synchronizer?+
The first flip-flop may go metastable when its input changes near the clock edge. The second samples it one full clock period later, giving it time to settle, which makes the probability of failure extremely small.
Can a two-flop synchronizer be used for a bus?+
No. Bits synchronized separately can settle on different cycles, so the receiver may see a value that was never sent. Use a handshake, Gray code or an asynchronous FIFO for multi-bit data.
How do you send a pulse to a slower clock domain?+
Turn the pulse into a toggle in the source domain, synchronize the toggle with two flip-flops in the destination domain, and generate a pulse when the synchronized value changes.