Synchronizers and Asynchronous Inputs

Bring a signal from outside the clock domain in through two flip-flops, and see what the synchronizer guarantees and what it cannot.

You Will Be Able To

  • Build a two-flop synchronizer and say which output may feed logic.
  • Explain why the second flop sees a settled value.
  • Explain why a bus cannot be synchronized bit by bit.
  • Say what a simulation can and cannot show about metastability.
Lesson
G5.12
Time
25 min
Runs on
Your browser
Check
5 questions

A button input reached three state machines through one flip-flop. Once in a long while the machines disagreed about whether it had been pressed. The flop had gone metastable, and the three machines read its output at different moments.

The First Flop Is x for a Moment; the Second Flop Never IsThe lab's second event, one column per 2 ns from 52 to 78 ns. The model's coin decides the settled value.
012345678910111213clkasync_inmeta (first flop)q (second flop)meta = x
Scroll horizontally ↔
  1. Cycle 1

    async_in falls at 54 ns, 1 ns before the edge at 55: inside the window.

  2. Cycle 2

    The first flop goes x at 56 ns and settles at 57 ns, to the old value 1. The coin chose 1.

  3. Cycle 7

    The next edge, at 65 ns, sees a clean 0: meta falls at 66 ns.

  4. Cycle 12

    q follows at 76 ns, three edges after the change. It was never x.

Measured: the lines the lab prints. MDFF is a bench model of metastability, not a circuit.

Two Flip-Flops in a Row

An asynchronous input changes with no relation to the clock: a button, a sensor, a signal from another clock domain. Sooner or later it changes inside the window of the flop that samples it.

The Two-Flop SynchronizerArrows show data. Both flops use the same clock edge.
  • I/O
  • Storage
  • Datapath
  • Data
The two-flop synchronizerArrows show data. Both flops use the same clock edge. io Asynchronousinput changes at any time storage Flop 1 may go metastable storage Flop 2 samples a settledvalue datapath Logic reads only flop 2 one period to settle
  1. io Asynchronous inputchanges at any time
    • Flop 1
  2. storage Flop 1may go metastable
    • one period to settle → Flop 2
  3. storage Flop 2samples a settled value
    • Logic
  4. datapath Logicreads only flop 2
  1. Caught in the window

    Flop 1 samples the input as it changes and may hang between 0 and 1.

  2. A period to settle

    Nothing reads flop 1 until the next edge. By then it has almost certainly settled.

  3. Clean output

    Flop 2 passes a clean 0 or 1 to the logic.

Read the diagram as text
  • Asynchronous input: changes at any time
  • Flop 1: may go metastable
  • Flop 2: samples a settled value
  • Logic: reads only flop 2
  • Asynchronous input → Flop 1
  • Flop 1 → Flop 2: one period to settle
  • Flop 2 → Logic

The MTBF of G5.11 grows with the time flop 1 gets, so the wire between the flops is kept empty.

Flop 1 gets one clock period, minus the setup time of flop 2, to settle. G5.11 showed that is thousands of years at 500 MHz but only seconds at 1 GHz. Fast designs therefore often add a third flop, which gives a second period.

The first flop may settle to the new value or to the old one. If old, the next edge catches the change. So the output follows the input two or three edges later, and that uncertainty is the price of crossing.

One Bit Only

Two bits that change together each go through their own synchronizer. Each can settle a different way, so for one cycle the output can show a mix that the input never had.

So a synchronizer carries one bit. A multi-bit value crosses as a Gray-coded count, where only one bit changes at a time. Or it crosses with a handshake that holds the data steady. The RTL track covers both in R6.3 and R6.4.

What a Simulator Cannot Show

A normal flip-flop model in a simulator samples exactly at the edge and never goes metastable. A design with no synchronizer at all passes every zero-delay test.

CheckWhat it finds
Zero-delay RTL simulationnothing: flops never go metastable
Bench model such as MDFFlogic that reads an unsettled flop, or a bus that splits
CDC structure checka missing synchronizer, logic between the flops, a bus crossed bit by bit
MTBF calculation (G5.11)whether the synchronizer is long enough for the clock and data rates

A model shows the effect; only the structure and the MTBF make the design safe.

Asked in interviews
  1. Why two flip-flops in a synchronizer?

    The first may go metastable when the input changes near the edge. The second samples it a full period later, by which time it has almost certainly settled. Only the second output feeds logic.

  2. Can you synchronize a bus with a two-flop synchronizer on each bit?

    No. The bits can settle on different edges, so the output can briefly show a value the input never had. Use a Gray code, a handshake or an asynchronous FIFO.

  3. How do you verify a clock-domain crossing?

    Not by plain simulation, which has no metastability. A CDC tool checks the structure, the MTBF is calculated, and benches may add a model that randomizes the synchronizer's delay.

The RTL view of the same synchronizer, with its assertion, is lesson R6.2.

Try It

Both changes of d come 1 ns before an edge, so the first flop goes x each time. The model settles it after 6 ns the first time and 1 ns the second.

The first time it settles to the new value 1, and q follows one edge later, at 16 ns. The second time it settles to the old value, and the change is caught one edge later: q falls at 76 ns.

Change the coin's start value 8'h5a and run again. The settling times and values change; q is still never x.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: t=5 first flop: d changed 1 ns before the edge -> x for 6 ns, then 1 t=12 meta=1 q=x t=16 meta=1 q=1 t=55 first flop: d changed 1 ns before the edge -> x for 1 ns, then 1 t=56 meta=x q=1 t=57 meta=1 q=1 t=66 meta=0 q=1 t=76 meta=0 q=0
Read included file: tb.sv
// tb.sv: an asynchronous input changes twice, each time 1 ns before a rising edge (read-only).
`timescale 1ns/1ns
module tb;
  logic clk = 0, d = 0;
  wire  q;
  logic [7:0] lfsr = 8'h5a;           // the model's coin: a fixed sequence, so every run is the same
  function automatic int settle(); lfsr = {lfsr[6:0], lfsr[7] ^ lfsr[5] ^ lfsr[4] ^ lfsr[3]}; return 1 + lfsr % 7; endfunction
  function automatic bit coin(); lfsr = {lfsr[6:0], lfsr[7] ^ lfsr[5] ^ lfsr[4] ^ lfsr[3]}; return lfsr[0]; endfunction
  sync_2ff dut (.clk(clk), .async_in(d), .q(q));
  always #5 clk = ~clk;                 // rising edges at 5, 15, 25, ...
  always @(dut.meta or q) if ($time > 3) $display("t=%0d  meta=%b q=%b", $time, dut.meta, q);
  initial begin
    #4  d = 1;                          // 1 ns before the edge at 5
    #50 d = 0;                          // t=54: 1 ns before the edge at 55
    #50 $finish;
  end
endmodule

Check Yourself

ConceptWhich output of a two-flop synchronizer may feed the rest of the logic?
ConceptAn asynchronous input changes just before an edge. After how many rising edges does a two-flop synchronizer show the new value?
ConceptA design passes all its zero-delay simulations. Does that show its asynchronous inputs are safe?
Read the waveformIn this trace, which signal must never reach the logic of the clock domain?
Inside a synchronizer
012345clkasync_inmetaq
Scroll horizontally ↔

Half a clock period per column.

Predict the outputThe lab bench with only one MDFF, its output read 1 ns after the first edge. Predict the exact line.
`timescale 1ns/1ns
// MDFF: a flip-flop with a bench model of metastability (a model, not a circuit). Clock-to-Q 1 ns. If d changed less
// than 2 ns before the rising edge, q goes x and settles 1 to 7 ns later to 0 or 1, as the bench's coin decides.
module MDFF (output reg q, input wire d, input wire clk);
  time t_d = -100;
  always @(d) t_d = $time;
  always @(posedge clk)
    if (d !== 1'b0 && d !== 1'b1) q <= #1 1'bx;
    else if ($time - t_d < 2) begin
      int r; bit pick;
      r = tb.settle(); pick = tb.coin();
      q <= #1 1'bx;
      q <= #(1 + r) (pick ? d : ~d);
    end
    else q <= #1 d;
endmodule

module tb;
  logic clk = 0, d = 0;
  wire  q;
  logic [7:0] lfsr = 8'h5a;           // the model's coin: a fixed sequence, so every run is the same
  function automatic int settle(); lfsr = {lfsr[6:0], lfsr[7] ^ lfsr[5] ^ lfsr[4] ^ lfsr[3]}; return 1 + lfsr % 7; endfunction
  function automatic bit coin(); lfsr = {lfsr[6:0], lfsr[7] ^ lfsr[5] ^ lfsr[4] ^ lfsr[3]}; return lfsr[0]; endfunction
  MDFF ff1 (q, d, clk);
  always #5 clk = ~clk;
  initial begin
    #4 d = 1;
    #2 $display("q=%b at %0d ns", q, $time);
    $finish;
  end
endmodule

Common Mistakes

Wrong Use the first flop's output anywhere in the logic.

It is the flop that may be metastable. In the lab it was x for 6 ns after an edge.

Fix Only the second flop's output leaves the synchronizer.

Wrong Put logic between the two flops.

Every gate there takes time from the period the first flop has to settle, and so from the MTBF exponent.

Fix Wire the first flop straight to the second.

Wrong Synchronize each bit of a bus with its own two flops.

Bits that change together can settle on different edges. In the predict item 01 became 10 by way of 11.

Fix Synchronize one bit, a Gray-coded count, or a handshake (RTL lesson R6.3).

Wrong Trust a zero-delay simulation of a clock crossing.

A plain flip-flop model never goes metastable, so a missing synchronizer passes every test.

Fix Check crossings by structure (CDC tools) and by MTBF, and use bench models such as MDFF only to show the effect.

Remember

  • A two-flop synchronizer gives the first flop a whole period to settle; only the second flop's output is used.
  • Keep the path between the flops empty: logic there eats the settling time.
  • Each synchronizer carries one bit; a bus crossed bit by bit can show values it never had.
  • Zero-delay simulation cannot show metastability: use structure checks, MTBF and bench models.

Practice

Report a problem with this page