Latest
VLSI · 4 min read

Clock Domain Crossing: Why the Two-Flop Synchronizer Works, and When It Doesn't

Metastability, MTBF, and the right way to pass single bits, multi-bit buses and data streams between asynchronous clock domains.

Modern chips rarely run on a single clock. A processor core, a UART, a sensor interface and a memory controller may all have their own. Every signal that travels from one clock domain to another is a clock domain crossing (CDC), and handling CDC badly produces the worst kind of bug: one that appears rarely, randomly, and only on silicon.

The problem: metastability

A flip-flop needs its input stable for the setup time before the clock edge and the hold time after it. When the input comes from an unrelated clock, nothing guarantees this. Sooner or later the data changes inside that window.

When that happens, the flop can become metastable: its internal latch balances near the midpoint between 0 and 1, and its output takes an unpredictable time to settle. The settling is exponential: the probability that the flop is still unresolved after time \(t\) falls as \(e^{-t/\tau}\), where \(\tau\) is a technology constant (on the order of tens of picoseconds in modern processes).

The two-flop synchronizer

The standard fix for a single-bit control signal is to pass it through two flip-flops clocked by the destination clock:

module sync_2ff (
  input  wire clk_dst,
  input  wire rst_n,
  input  wire d_async,   // from another clock domain
  output wire q_sync
);
  (* ASYNC_REG = "TRUE" *) reg s1, s2;   // keep the flops together (Xilinx attribute)

  always @(posedge clk_dst or negedge rst_n) begin
    if (!rst_n) begin
      s1 <= 1'b0;
      s2 <= 1'b0;
    end else begin
      s1 <= d_async;   // may go metastable
      s2 <= s1;        // almost certainly resolved by now
    end
  end

  assign q_sync = s2;
endmodule

The first flop may go metastable. It then has almost a full destination clock period to resolve before the second flop samples it. The synchronizer does not prevent metastability; it gives it time to resolve.

How much safer is it? MTBF

The mean time between synchronizer failures is commonly modelled as

\[\text{MTBF} = \frac{e^{\,t_r/\tau}}{T_W \, f_{clk} \, f_{data}}\]

where \(t_r\) is the resolution time available, \(T_W\) is the flop’s metastability window, \(f_{clk}\) is the destination clock frequency and \(f_{data}\) is the rate at which the input toggles.

Take illustrative values \(\tau = T_W = 20\) ps, \(f_{clk} = 500\) MHz and \(f_{data} = 50\) MHz:

Design Resolution time \(t_r\) MTBF
One flop, logic right after it 0.2 ns about 0.04 s
Two-flop synchronizer 1.8 ns about \(\mathbf{2 \times 10^{33}}\) s

The age of the universe is roughly \(4 \times 10^{17}\) s. Because time appears in the exponent, a single extra clock period of resolution time changes “fails many times a second” into “never fails in practice”. For very fast clocks, designers add a third flop.

Rule 1: never synchronize a multi-bit bus bit by bit

If you pass a 4-bit counter through four separate two-flop synchronizers, each bit resolves independently. During a transition like 0111 → 1000, the destination can capture any mix of old and new bits, such as 1111 or 0000, values that never existed.

Fixes:

  • Gray code. Only one bit changes per increment, so the receiver sees either the old or the new value. This is how asynchronous FIFO pointers cross domains.
  • Handshake. Hold the bus stable, synchronize a single req bit, have the receiver capture the bus, and send back an ack. It’s slower, but it works for any data.
  • Asynchronous FIFO. For streams of data, write in one domain and read in the other. Gray-coded pointers are synchronized across to generate the full and empty flags.

Rule 2: no combinational logic before the synchronizer

The signal entering the first flop must come directly from a flop in the source domain. Combinational logic can glitch, and the destination clock may sample a glitch that was never meant to be seen.

Rule 3: watch out for pulses

A one-cycle pulse from a fast domain may be shorter than one period of a slow destination clock and can be missed entirely. Convert it into a level (toggle a flop on each pulse), synchronize the level, and detect its edges on the destination side.

Verifying CDC

Ordinary RTL simulation does not model metastability, so CDC bugs pass functional tests. Dedicated CDC analysis tools (for example Synopsys SpyGlass CDC or Siemens Questa CDC) structurally check every crossing for missing synchronizers, reconvergence and multi-bit issues. On any multi-clock design, run one before tape-out.

Key takeaways

  • Metastability cannot be avoided, only made extremely unlikely to cause failures.
  • Use a two-flop (or three-flop) synchronizer for single control bits.
  • Use Gray code, a handshake or an async FIFO for multi-bit data.
  • Drive synchronizers from flops, never from logic, and stretch short pulses.

#cdc#metastability#verilog#synchronizer

LearnSilicon

Clear, practical articles on VLSI, chip design and electronics, from first principles. About LearnSilicon →