---
title: Clock domain crossing
heading: Metastability, synchronizers, Gray codes and handshakes
eyebrow: S3 · Lesson 24 · Clock domain crossing
xp: 50
---

::: hook
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.
:::

::: play
- The mean time between synchronization failures against the settling time allowed, on a log scale: $\text{MTBF} = \frac{e^{t_r/\tau}}{T_0f_\text{clk}f_\text{data}}$, with τ = 20 ps and $T_0$ = 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.
:::
:::

```sim plot
view: graph
x: t = 0..2000 ps
y: M = 1e-6..1e40 log
slider: fc = 100..3000 = 2000 MHz log
slider: fd = 1..500 = 100 MHz log
plot: exp(t/20)/(20e-12*fc*1e6*fd*1e6) | samples: 300 | label: MTBF, seconds
marker: y = 3.156e7 | label: one year
marker: y = 4.35e17 | label: age of the universe
readout: one flip-flop (one period less 100 ps), s = exp((1e6/fc - 100)/20)/(20e-12*fc*1e6*fd*1e6) | format: sci
readout: two flip-flops (one more period), s = exp((2e6/fc - 100)/20)/(20e-12*fc*1e6*fd*1e6) | format: sci
```

::: predict
## A single flip-flop synchronizer at 2 GHz fails about every two minutes. What does adding a second flip-flop do?

- [ ] Halves the failure rate
- [ ] Makes it fail every four minutes
- [x] Makes failures about 70 billion times rarer: once in hundreds of thousands of years
- [ ] Eliminates failures completely

::: reveal
The second flip-flop gives the first one an extra clock period, 500 ps, to resolve before its output is used. MTBF grows as $e^{t/\tau}$, so 500 ps / 20 ps = 25 time constants multiplies it by $e^{25}$ ≈ 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.
:::
:::

::: nameit
## Metastability and MTBF

A flip-flop whose input changes within its aperture $T_0$ around the edge may go metastable and take an unbounded time to resolve; the probability it's still unresolved after time $t_r$ falls as $e^{-t_r/\tau}$. With data changing at rate $f_\text{data}$ and a clock at $f_\text{clk}$:

$$\text{MTBF} = \frac{e^{t_r/\tau}}{T_0f_\text{clk}f_\text{data}}$$

τ and $T_0$ 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: $g = b \oplus (b \gg 1)$.
:::

::: example
## A 2 GHz clock samples data changing at 100 MHz, with τ = 20 ps and $T_0$ = 20 ps. Find the MTBF with one and two flip-flops, allowing 100 ps for clock-to-Q and setup.

::: solution
1. **Resolution time, one flip-flop.** The 2 GHz period is 500 ps:
   $$\begin{aligned} t_r &= 500 - 100 \\ &= 400\ \text{ps} \end{aligned}$$
2. **MTBF, one flip-flop.**
   $$\begin{aligned} \text{MTBF} &= \frac{e^{20}}{20 \times 10^{-12} \times 2 \times 10^9 \times 10^8} \\ &= \frac{4.85 \times 10^8}{4 \times 10^6} \\ &= 121\ \text{s} \end{aligned}$$
3. **Two flip-flops.** They give 900 ps:
   $$\begin{aligned} \text{MTBF} &= \frac{e^{45}}{4 \times 10^6} \\ &= 8.7 \times 10^{12}\ \text{s} \end{aligned}$$
   about 280,000 years.
4. **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 $8.7 \times 10^{12}$ s (about 280,000 years) with two.
:::
:::

::: example
## What are the 4-bit Gray codes for 5, 6 and 7, and why does that matter for a FIFO pointer?

::: solution
1. **Gray code of 5.** XOR the number with itself shifted right one place:
   $$0101 \oplus 0010 = 0111$$
2. **Gray code of 6.**
   $$0110 \oplus 0011 = 0101$$
3. **Gray code of 7.**
   $$0111 \oplus 0011 = 0100$$
   Each step changes one bit: 0111 → 0101 → 0100.
4. **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.
:::
:::

::: example
## A 2 GHz clock samples data changing at 200 MHz with τ = $T_0$ = 25 ps; each flip-flop stage leaves its period less 120 ps for resolution. How many synchronizer stages are needed?

::: solution
1. **One stage.** $t_r = 380$ ps:
   $$\begin{aligned} \text{MTBF} &= \frac{e^{380/25}}{25\text{ ps} \times 2\text{ GHz} \times 200\text{ MHz}} \\ &= 0.4\ \text{s} \end{aligned}$$
   a failure every half second.
2. **Two stages.** They add a full period, $t_r = 880$ ps:
   $$\text{MTBF} = 1.9 \times 10^8\ \text{s}$$
   about 6 years, still too short for a product.
3. **Three stages.** $t_r = 1380$ ps:
   $$\text{MTBF} = 9.4 \times 10^{16}\ \text{s}$$
   3 billion years.
4. **The rule.** Fast clocks need three-flop synchronizers.

**Answer:** three stages, for an MTBF of $9.4 \times 10^{16}$ s (3 billion years).
:::
:::

::: check
given: t = 300 | 600 | 1000; u = 15 | 30; f = 500 | 1500; d = 50 | 200
answer: exp(t/u)/(20e-12*f*1e6*d*1e6)
unit: s
tolerance: 1%

## A synchronizer allows {t} ps of settling, with τ = {u} ps, $T_0$ = 20 ps, a {f} MHz clock and data at {d} MHz. What's its MTBF?

::: right
Right. $\frac{e^{t_r/\tau}}{T_0f_\text{clk}f_\text{data}}$ = {answer} s.
:::

::: misses
- [any] Not yet. Use the MTBF formula with consistent units.
:::
:::

::: check
given: f = 500 | 1000 | 2000; u = 15 | 25
answer: exp(1e6/f/u)
tolerance: 1%

## By what factor does a second synchronizer flip-flop improve the MTBF at {f} MHz, with τ = {u} ps?

::: right
Right. $e^{T_\text{clk}/\tau}$ = {answer}.
:::

::: misses
- [2] The extra settling time enters the exponent.
- [any] Not yet. One more period of settling.
:::
:::

::: check
given: Y = 10 | 1000 | 100000; u = 15 | 25; f = 500 | 2000; d = 10 | 100
answer: u*ln(Y*3.156e7*20e-12*f*1e6*d*1e6)
unit: ps
tolerance: 0.5%

## A crossing must reach an MTBF of {Y} years, with τ = {u} ps, $T_0$ = 20 ps, a {f} MHz clock and data at {d} MHz. How much settling time must the synchronizer allow?

::: right
Right. $t_r = \tau\ln(\text{MTBF} \cdot T_0f_\text{clk}f_\text{data})$ = {answer} ps.
:::

::: misses
- [u*log(Y*3.156e7*20e-12*f*1e6*d*1e6)] The MTBF grows as $e^{t_r/\tau}$: use the natural logarithm.
- [u*ln(Y*20e-12*f*1e6*d*1e6)] Convert years to seconds first.
- [any] Not yet. Solve the MTBF formula for $t_r$.
:::
:::

::: check
given: f = 250 | 500 | 800; w = 30 | 60
answer: 1e6/f + w
unit: ps
tolerance: 0.5%

## A level from another domain must be caught by a {f} MHz clock whose flip-flops need a {w} ps setup-and-hold window. How long must the level be held to guarantee at least one clean sample?

::: right
Right. One full period, so an edge is certain to fall inside, plus the window around that edge: {answer} ps.
:::

::: misses
- [1e6/f] An edge is certain to land in one period, but its setup-and-hold window must fit inside too.
- [any] Not yet. Any interval one period long contains a clock edge.
:::
:::

::: check
given: s = 2 | 3; f = 100 | 250; d = 2 | 3; g = 50 | 400
answer: (s + 1)*1000/f + (d + 1)*1000/g
unit: ns
tolerance: 0.5%

## In a req/ack handshake, the request crosses a {s}-flop synchronizer into a {f} MHz domain, which takes one more cycle to capture the data and raise acknowledge. The acknowledge crosses a {d}-flop synchronizer back into a {g} MHz domain, which takes one more cycle to see it. Counting whole cycles, how long does one transfer take?

::: right
Right. $(s + 1)T_\text{dest} + (d + 1)T_\text{src}$ = {answer} ns, which caps the handshake at one word per round trip.
:::

::: misses
- [s*1000/f + d*1000/g] Add the extra cycle on each side.
- [any] Not yet. Count each side's cycles at its own period.
:::
:::

::: check
given: N = 3 | 4 | 6 | 8
answer: 2^(N + 1) - 2
tolerance: 0

## An {N}-bit binary counter counts through a full cycle: $2^N$ increments, including the wrap back to zero. How many bit changes does it make in total?

::: right
Right. Bit k toggles every $2^k$ increments, so the total is $2^N + 2^{N-1} + \dots + 2$ = {answer}. A Gray counter makes only $2^N$, one per increment, which also saves switching power.
:::

::: misses
- [2^N] That's a Gray counter's total. In binary, the low bit alone toggles on every increment.
- [N*2^N] Not every bit changes on every increment.
- [any] Not yet. Count how often each bit toggles.
:::
:::

::: check
kind: parts
given: f = 1 | 2; d = 10 | 100; u = 20 | 30

## A synchronizer flip-flop on a {f} GHz clock has τ = $T_0$ = {u} ps and samples data changing at {d} MHz; each stage leaves its period less 100 ps for resolution.

::: part
answer: exp((1000/f - 100)/u)/(u*1e-12*f*1e9*d*1e6)
unit: s
tolerance: 0.5%

### What is the MTBF with one stage?

::: misses
- [any] $e^{t_r/\tau}/(T_0f_\text{clk}f_\text{data})$.
:::
:::

::: part
answer: exp((2000/f - 100)/u)/(u*1e-12*f*1e9*d*1e6)/3.156e7
unit: years
tolerance: 0.5%

### And with two stages?

::: misses
- [any] The second stage adds a full period of resolution time.
:::
:::
:::

::: check
kind: derivation
vars: t = 1e-10..3e-9, u = 1e-11..5e-11, a = 1e-11..5e-11, f = 1e8..3e9, d = 1e6..5e8

## A synchronizer has resolution time t, time constant u and aperture a, with clock f and data rate d.

::: step
answer: exp(t/u)/(a*f*d)

### What is its MTBF?
:::

::: step
answer: u*ln(10)

### How much more resolution time multiplies the MTBF by 10?
:::

::: step
answer: exp((t + 1/f)/u)/(a*f*d)

### What is the MTBF with one more stage?
:::

::: right
Right. MTBF grows exponentially with resolution time, which is why one more stage usually solves the problem.
:::
:::

::: check
kind: python

## Write `stages_for(years, tau, t0, fclk, fdata, overhead)`: the fewest synchronizer flip-flops whose MTBF reaches the target, where n stages leave $nT - t_\text{overhead}$ to resolve ($T = 1/f_\text{clk}$, all in SI units).

```python starter
def stages_for(years, tau, t0, fclk, fdata, overhead):
    return 2
```

```python solution
import math

def stages_for(years, tau, t0, fclk, fdata, overhead):
    T = 1 / fclk
    n = 1
    while True:
        tr = n * T - overhead
        if math.exp(tr / tau) / (t0 * fclk * fdata) >= years * 3.156e7:
            return n
        n += 1
```

```python tests
assert stages_for(100, 25e-12, 25e-12, 2e9, 2e8, 120e-12) == 3, "The worked example: 2 GHz needs three stages for a product's lifetime."
assert stages_for(1, 25e-12, 25e-12, 2e9, 2e8, 120e-12) == 2, "Two stages give six years."
assert stages_for(1000, 20e-12, 20e-12, 5e8, 1e8, 100e-12) == 1, "At 500 MHz one stage has nearly 2 ns to settle."
```

::: right
Right. Each stage adds a full period of resolution time, multiplying the MTBF by $e^{T/\tau}$.
:::
:::

::: explain
## Why does sending a binary counter's value across a clock domain fail, while a Gray-coded one works?

::: model
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

::: check
given: t = 400 | 800; u = 20 | 30; f = 800 | 1200; d = 80 | 150
answer: exp(t/u)/(20e-12*f*1e6*d*1e6)
unit: s
tolerance: 1%

## MTBF: {t} ps settling, τ {u} ps, {f} MHz clock, {d} MHz data?

::: misses
- [any] $e^{t/\tau}/T_0f_cf_d$.
:::
:::

::: check
given: f = 700 | 1600; u = 18 | 28
answer: exp(1e6/f/u)
tolerance: 1%

## Improvement from one more flop at {f} MHz, τ = {u} ps?

::: misses
- [any] $e^{T/\tau}$.
:::
:::

::: check
given: s = 2 | 4; f = 125 | 300; d = 2 | 3; g = 80 | 200
answer: (s + 1)*1000/f + (d + 1)*1000/g
unit: ns
tolerance: 0.5%

## Handshake transfer time: request through {s} flops at {f} MHz plus a cycle, acknowledge through {d} flops at {g} MHz plus a cycle?

::: misses
- [any] $(s + 1)T_\text{dest} + (d + 1)T_\text{src}$.
:::
:::
:::
