---
title: Crossing clock domains
heading: Synchronizers, handshakes and asynchronous FIFOs
eyebrow: C7 · Lesson 41 · Crossing clock domains
xp: 40
---

::: hook
Most chips run several clocks: a fast one for the core, slower ones for peripherals, another recovered from an incoming data stream. A signal passing from one clock's domain to another arrives at a moment the receiving flip-flops didn't plan for, and sometimes it violates their setup time. Lesson 27 showed that the flip-flop may then hang between 0 and 1. This lesson is about living with that: how many flip-flops make a synchronizer safe, why a multi-bit value can't be synchronized bit by bit, and how handshakes and FIFOs move data across safely.

::: play
- The curve is a synchronizer's mean time between failures, as a power of ten in seconds, against the time it gives a metastable flip-flop to resolve.
- Each extra flip-flop adds a whole clock period of resolution time. Watch how far one period moves the curve.
- Slower flip-flops (larger τ) need much more time for the same safety.
:::
:::

```sim plot
view: graph
x: t = 0..3 ns
y: m = -10..40
slider: tau = 10..100 = 30 ps log
slider: fc = 100..2000 = 1000 MHz log
slider: fd = 1..500 = 100 MHz log
plot: t*1e-9/(tau*1e-12)/ln(10) - log(50e-12*fc*1e6*fd*1e6) | label: log10 of MTBF in seconds | samples: 200
readout: one clock period, ns = 1000/fc | digits: 3
readout: log10 MTBF with one period of resolution = (1000/fc - 0.05)*1e-9/(tau*1e-12)/ln(10) - log(50e-12*fc*1e6*fd*1e6) | digits: 3
```

::: predict
## An 8-bit counter in one clock domain is read in another by passing each bit through its own two-flop synchronizer. The counter steps from 0111 1111 to 1000 0000 just as the receiver samples. What can the receiver see?

- [ ] Only 127 or 128
- [ ] 127, 128 or a value one away
- [x] Almost anything: each bit is synchronized independently, so some may show the old value and some the new
- [ ] Nothing: synchronizers prevent all errors

::: reveal
Almost anything: all eight bits change, and each synchronizer independently catches its bit before or after the change. The receiver might see 0000 0000, 1111 1111 or any mix. Synchronizers make each bit a clean 0 or 1, but they can't make several bits agree. If the counter counted in Gray code, only one bit would change per step, and the receiver would see either the old value or the new one.
:::
:::

::: nameit
## Synchronizers

A flip-flop sampling an asynchronous input becomes metastable now and then; given time $t_r$ to resolve, the mean time between failures is

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

A **two-flop synchronizer** gives the first flip-flop a whole clock period, less setup, to resolve before the second samples it. Each extra stage adds another period, multiplying the MTBF by $e^{T/\tau}$, typically by a factor of $10^{13}$ or more.

## Multi-bit values

Bits synchronized separately can disagree. Three safe ways across:

- **Gray-coded counters:** one bit changes per step, so a sample is either the old or the new value (lesson 3).
- **Handshakes:** the sender holds the data steady and raises a request; the request (one bit) is synchronized; the receiver reads the stable data and returns an acknowledge, synchronized back. A four-phase handshake (request up, acknowledge up, request down, acknowledge down) passes four synchronizations per word, about $4T_\text{dst} + 4T_\text{src}$.
- **Asynchronous FIFOs:** a dual-port memory written in one domain and read in the other, with Gray-coded read and write pointers synchronized across to compute full and empty.

## Sizing a FIFO

If the writer delivers bursts of B words at $f_w$ while the reader drains at a slower $f_r$, the FIFO must absorb the difference:

$$\text{depth} \ge B\left(1 - \frac{f_r}{f_w}\right),$$

plus a few words for the pointers' synchronizer delay.

## Pulses

A one-clock pulse from a fast domain can fall between a slow domain's edges and be missed. Convert it to a level change (a toggle), synchronize the level, and detect its edges on the other side.
:::

::: example
## A 1 GHz design samples a 100 MHz asynchronous signal with flip-flops having τ = 30 ps, $T_0$ = 50 ps and 50 ps of setup. Compare one flop whose output feeds 0.7 ns of logic, two flops, and three.

::: solution
1. **One flop.** It leaves only $1 - 0.05 - 0.7$ = 0.25 ns to resolve:
   $$\begin{aligned} \text{MTBF} &= \frac{e^{0.25/0.03}}{50\ \text{ps} \times 10^9 \times 10^8} \\ &= 0.8\ \text{ms} \end{aligned}$$
   It fails about a thousand times a second.
2. **Two flops.** The first gets a clean 0.95 ns:
   $$\begin{aligned} \text{MTBF} &= \frac{e^{0.95/0.03}}{5 \times 10^6} \\ &= 1.1 \times 10^7\ \text{s} \end{aligned}$$
   or 131 days.
3. **Three flops.** They give 1.95 ns: about $10^{14}$ years.
4. **Compare.** The second stage turns a broken design into a rare failure; the third makes failure practically impossible.

**Answer:** about 0.8 ms with one flop, 131 days with two, and about $10^{14}$ years with three.
:::
:::

::: example
## A four-phase handshake passes words from a 100 MHz domain to a 50 MHz domain, with two-flop synchronizers both ways. About how many words per second can it move?

::: solution
1. **Count the synchronizations.** Each word needs four: request rising and falling into the 100 MHz side, and acknowledge rising and falling back into the 50 MHz side.
2. **Time each one.** Each takes two clocks: 10 ns each on the 100 MHz side and 20 ns each on the 50 MHz side.
3. **Add them up.**
   $$4 \times 10 + 4 \times 20 = 120\ \text{ns}$$
   per word, roughly 8.3 million words a second.
4. **Compare.** Safe and simple, but slow; bulk data uses a FIFO instead.

**Answer:** roughly 8.3 million words a second.
:::
:::

::: example
## A writer at 100 MHz sends bursts of 120 words; a reader drains at 80 MHz. How deep must the FIFO be?

::: solution
1. **Words drained during the burst.** During the 1.2 µs burst the reader removes
   $$1.2\ \mu\text{s} \times 80\ \text{MHz} = 96$$
   words, so 24 pile up.
2. **Minimum depth.**
   $$\begin{aligned} \text{depth} &\ge 120\left(1 - \frac{80}{100}\right) \\ &= 24 \end{aligned}$$
3. **Add margin.** A few more cover the synchronizer delay before the writer sees the read pointer move: 32 is a natural choice.
4. **Between bursts.** The reader must have time to empty it, or it overflows on the next.

**Answer:** at least 24 words; 32 is a natural choice.
:::
:::

::: check
kind: parts
given: T = 1 | 2 | 5; t = 20 | 50; f = 10 | 100

## A synchronizer clocked with a {T} ns period uses flops with τ = {t} ps; each extra stage adds one period of resolution time.

::: part
answer: T*1000/t/ln(10)
tolerance: 0.5%

### By how many powers of ten does one extra stage multiply the MTBF?

::: misses
- [T*1000/t] That's the exponent of e; convert to powers of ten.
- [any] $\frac{T}{\tau\ln10}$.
:::
:::

::: part
answer: exp(T*1000/t)
tolerance: 0.5%

### By what factor, exactly?

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

::: check
kind: parts
given: B = 64 | 200 | 1000; fw = 100 | 250; fr = 40 | 80

## A writer at {fw} MHz sends bursts of {B} words to a reader at {fr} MHz.

::: part
answer: B*(1 - fr/fw)
tolerance: 0.5%

### What's the least FIFO depth, before margin?

::: misses
- [B] The reader drains words during the burst.
- [B*fr/fw] That's what the reader removes; the FIFO holds the rest.
- [any] $B\left(1 - \frac{f_r}{f_w}\right)$.
:::
:::

::: part
answer: B*(1 - fr/fw)/fr
unit: µs
tolerance: 0.5%

### How long does the reader take to empty that backlog after the burst?

::: misses
- [any] The backlog over the read rate.
:::
:::
:::

::: check
given: fd = 25 | 100 | 400; fs = 10 | 50 | 200
answer: 4*1000/fd + 4*1000/fs
unit: ns
tolerance: 0.5%

## A four-phase handshake with two-flop synchronizers carries words from a {fs} MHz domain to a {fd} MHz domain. About how long does each word take?

::: right
Right. Four synchronizations of two clocks each: $4T_\text{dst} + 4T_\text{src}$ = {answer}.
:::

::: misses
- [2*1000/fd + 2*1000/fs] There are four handshake edges, each synchronized in two clocks.
- [any] $4T_\text{dst} + 4T_\text{src}$.
:::
:::

::: check
kind: choice

## A 4-bit write pointer must be passed to another clock domain to compute "FIFO full". What's the safe way?

- [ ] Synchronize each binary bit with its own two-flop synchronizer
- [x] Convert it to Gray code, then synchronize each bit
- [ ] Sample it twice and compare
- [ ] Use a single flip-flop clocked faster

::: right
Right. A Gray-coded pointer changes one bit per step, so the receiver sees either the old or the new value, never a mixture.
:::

::: misses
- [1] Binary pointers change several bits at once, and separate synchronizers can disagree.
- [3] Two samples can both be mixtures.
- [4] A faster flip-flop doesn't stop the bits disagreeing.
:::
:::

::: check
kind: derivation
vars: t = 0.0000000001..0.00000001, u = 0.000000000001..0.0000000001, T = 0.000000000001..0.0000000001, c = 1000000..5000000000, d = 1000..1000000000

## A synchronizer gives resolution time t, with flops of time constant u and aperture T, at clock c and data rate d.

::: step
answer: exp(t/u)/(T*c*d)

### What's its MTBF?
:::

::: step
answer: exp((t + 1/c)/u)/(T*c*d)

### What's it with one more stage (one more clock period of resolution)?
:::

::: right
Right. Each stage multiplies the MTBF by $e^{T_\text{clk}/\tau}$.
:::
:::

::: check
kind: python

## Write `fifo_depth(B, fw, fr, sync=2)` returning the smallest power-of-two FIFO depth that holds a burst of B words written at fw while being read at fr, plus `sync` words of margin for the pointer synchronizers.

```python starter
def fifo_depth(B, fw, fr, sync=2):
    return B

print(fifo_depth(120, 100e6, 80e6))
```

```python solution
import math

def fifo_depth(B, fw, fr, sync=2):
    need = math.ceil(B * (1 - fr / fw) - 1e-9) + sync
    return 2 ** math.ceil(math.log2(need))

print(fifo_depth(120, 100e6, 80e6))
```

```python tests
assert fifo_depth(120, 100e6, 80e6) == 32, f"24 + 2 rounds up to 32; got {fifo_depth(120, 100e6, 80e6)}"
assert fifo_depth(1000, 250e6, 50e6) == 1024, "800 + 2 rounds up to 1024."
assert fifo_depth(64, 100e6, 99e6, 0) == 1, "A reader almost as fast needs almost nothing."
```

::: right
Right. Power-of-two depths keep the Gray-coded pointers simple.
:::
:::

::: explain
## Why can't a multi-bit value be passed between clock domains by synchronizing each bit separately?

::: model
A synchronizer only guarantees that its output settles to a clean 0 or 1; when the input changes near the sampling edge, it may resolve to either the old or the new value. With several bits changing at once, each synchronizer makes that choice independently, so some bits may show the old value and others the new, producing a combination that was never sent. The fix is to make sure only one bit can change at a time, as Gray code does, or to hold the data steady and synchronize just a single control signal that says when it's valid, as a handshake does.
:::
:::

::: practice

::: check
given: B = 50 | 500; fw = 120 | 200; fr = 30 | 90
answer: B*(1 - fr/fw)
tolerance: 0.5%

## Burst {B} at {fw} MHz, read at {fr} MHz. Least depth?

::: misses
- [any] $B\left(1 - \frac{f_r}{f_w}\right)$.
:::
:::
:::
