S3 · Lesson 24 · Clock domain crossing
Metastability, synchronizers, Gray codes and handshakes
Most chips have several clocks: one for the processor, one for each interface, one for memory. A signal passing from one clock domain to another arrives at a random moment relative to the receiving clock, and sometimes it changes right inside a flip-flop's setup-and-hold window. The flip-flop can then hang between 0 and 1 for an unpredictable time (metastability, S2.40's latch problem again). It can't be prevented, but it can be made astronomically rare by giving the flip-flop time to settle before anything uses its output. This lesson covers crossings for single bits, for buses and for streams of data. A crossing done wrong can pass every bench test and still fail once a week in the field.
- The mean time between synchronization failures against the settling time allowed, on a log scale: , with τ = 20 ps and = 20 ps.
- Every extra τ of settling time multiplies the MTBF by e: a straight line on this plot. A second synchronizer flip-flop adds a whole clock period of settling.
- The markers are one year and the age of the universe. A design aims far above the first.
Predict
A single flip-flop synchronizer at 2 GHz fails about every two minutes. What does adding a second flip-flop do?
The second flip-flop gives the first one an extra clock period, 500 ps, to resolve before its output is used. MTBF grows as , so 500 ps / 20 ps = 25 time constants multiplies it by ≈ 7 × 10¹⁰: from 2 minutes to about 280,000 years. That exponential is why the two-flop synchronizer is the universal rule for single-bit crossings, and why very fast clocks sometimes need three.
Name it
Metastability and MTBF
A flip-flop whose input changes within its aperture around the edge may go metastable and take an unbounded time to resolve; the probability it's still unresolved after time falls as . With data changing at rate and a clock at :
τ and are properties of the flip-flop, tens of picoseconds in modern processes.
Single bits: the two-flop synchronizer
Pass the asynchronous signal through two flip-flops on the receiving clock and use only the second's output; the first has nearly a full period to settle. Never synchronize the same signal in two places (they can disagree), and never feed a synchronizer through combinational logic from the other domain (glitches become false pulses).
Pulses and handshakes
- A short pulse may fall between receiving clock edges: convert it to a toggle in the source domain, synchronize the level, and detect its edges in the destination.
- A req/ack handshake passes a data word safely: hold the data stable, synchronize a request across, let the receiver capture the data and synchronize an acknowledge back. Safe for any clocks, but each transfer takes several cycles of each clock.
Buses: Gray codes and async FIFOs
Synchronizing a multi-bit binary value fails because its bits can change at slightly different times and be caught half-updated. A Gray code changes one bit per increment, so a sampled value is either the old or the new one. An asynchronous FIFO keeps its write pointer in Gray code, synchronizes it into the read domain (and the read pointer into the write domain), and compares there; the extra synchronization latency only makes full and empty slightly pessimistic. Binary to Gray: .
Worked example
A 2 GHz clock samples data changing at 100 MHz, with τ = 20 ps and = 20 ps. Find the MTBF with one and two flip-flops, allowing 100 ps for clock-to-Q and setup.
Show the solution
- Resolution time, one flip-flop. The 2 GHz period is 500 ps:
- MTBF, one flip-flop.
- Two flip-flops. They give 900 ps: about 280,000 years.
- For a fleet. For a product shipped in millions, with many crossings each, designers still check that the fleet's total failure rate is acceptable.
Answer 121 s with one flip-flop and s (about 280,000 years) with two.
Worked example
What are the 4-bit Gray codes for 5, 6 and 7, and why does that matter for a FIFO pointer?
Show the solution
- Gray code of 5. XOR the number with itself shifted right one place:
- Gray code of 6.
- Gray code of 7. Each step changes one bit: 0111 → 0101 → 0100.
- Why it matters for a FIFO. In binary, 7 → 8 (0111 → 1000) changes all four bits, and a receiver sampling mid-change could see any value from 0 to 15, a wildly wrong fill level. With Gray code it sees 7 or 8.
Answer 0111, 0101 and 0100; each step changes one bit, so a pointer sampled mid-change reads as either the old or the new value.
Worked example
A 2 GHz clock samples data changing at 200 MHz with τ = = 25 ps; each flip-flop stage leaves its period less 120 ps for resolution. How many synchronizer stages are needed?
Show the solution
- One stage. ps: a failure every half second.
- Two stages. They add a full period, ps: about 6 years, still too short for a product.
- Three stages. ps: 3 billion years.
- The rule. Fast clocks need three-flop synchronizers.
Answer three stages, for an MTBF of s (3 billion years).
Check
A synchronizer allows 600 ps of settling, with τ = 15 ps, = 20 ps, a 1500 MHz clock and data at 200 MHz. What's its MTBF?
Write units the way you normally would: 1.5 s and 1500 ms are the same answer.
Check
By what factor does a second synchronizer flip-flop improve the MTBF at 1000 MHz, with τ = 15 ps?
Check
A crossing must reach an MTBF of 1000 years, with τ = 15 ps, = 20 ps, a 2000 MHz clock and data at 100 MHz. How much settling time must the synchronizer allow?
Write units the way you normally would: 1.5 ps and 0.0015 ns are the same answer.
Check
A level from another domain must be caught by a 500 MHz clock whose flip-flops need a 30 ps setup-and-hold window. How long must the level be held to guarantee at least one clean sample?
Write units the way you normally would: 1.5 ps and 0.0015 ns are the same answer.
Check
In a req/ack handshake, the request crosses a 2-flop synchronizer into a 100 MHz domain, which takes one more cycle to capture the data and raise acknowledge. The acknowledge crosses a 3-flop synchronizer back into a 400 MHz domain, which takes one more cycle to see it. Counting whole cycles, how long does one transfer take?
Write units the way you normally would: 1.5 ns, 1500 ps and 0.0015 µs are the same answer.
Check
A 4-bit binary counter counts through a full cycle: increments, including the wrap back to zero. How many bit changes does it make in total?
Check
A synchronizer flip-flop on a 1 GHz clock has τ = = 30 ps and samples data changing at 10 MHz; each stage leaves its period less 100 ps for resolution.
(a)
What is the MTBF with one stage?
Write units the way you normally would: 1.5 s and 1500 ms are the same answer.
(b)
And with two stages?
Check
A synchronizer has resolution time t, time constant u and aperture a, with clock f and data rate d.
Step 1
What is its MTBF?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
How much more resolution time multiplies the MTBF by 10?
Step 3
What is the MTBF with one more stage?
Check
Write stages_for(years, tau, t0, fclk, fdata, overhead): the fewest synchronizer flip-flops whose MTBF reaches the target, where n stages leave to resolve (, all in SI units).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does sending a binary counter's value across a clock domain fail, while a Gray-coded one works?
When a binary counter increments, several bits can change at once (from 0111 to 1000, all four), and the wires carrying them never switch at exactly the same instant. A receiving clock edge that lands during the change can catch some bits old and some new, producing a value that was never on the counter, possibly far from either neighbor. Synchronizers resolve each bit to a clean 0 or 1, but they can't make the bits agree. A Gray code changes exactly one bit per increment, so whatever moment the receiver samples, every bit but one is settled, and the one in transition resolves to either its old or its new value: the received count is always the old count or the new one.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.