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.
- Cycle 1
async_infalls at 54 ns, 1 ns before the edge at 55: inside the window. - Cycle 2
The first flop goes x at 56 ns and settles at 57 ns, to the old value 1. The coin chose 1.
- Cycle 7
The next edge, at 65 ns, sees a clean 0:
metafalls at 66 ns. - Cycle 12
qfollows 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.
- I/O
- Storage
- Datapath
- Data
- Caught in the window
Flop 1 samples the input as it changes and may hang between 0 and 1.
- A period to settle
Nothing reads flop 1 until the next edge. By then it has almost certainly settled.
- 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.
| Check | What it finds |
|---|---|
| Zero-delay RTL simulation | nothing: flops never go metastable |
| Bench model such as MDFF | logic that reads an unsettled flop, or a bus that splits |
| CDC structure check | a 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
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.
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.
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.
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=0Read 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
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.