---
title: Synchronous design, timing and clock domains
heading: Why almost all digital design is synchronous, the setup and hold checks every register-to-register path must pass, slack and the maximum clock rate, metastability when a signal arrives from another clock domain, the two-flip-flop synchronizer and its mean time between failures, and safe ways to pass whole buses across
eyebrow: D10 · Lesson 8 · Timing and clock domains
xp: 40
---

::: hook
A synchronous design makes one promise: every flip-flop changes only at a clock edge, and every signal has a full clock period to settle before the next edge samples it. Keep that promise and a design with millions of gates behaves predictably, which is why static timing analysis can check it path by path. Break it, by letting a signal arrive from a different clock or from a button, and a flip-flop can be caught mid-change and hover between 0 and 1 for an unpredictable time. That state, **metastability**, can't be prevented, only made vanishingly rare.
:::

::: play
- An asynchronous input A, changing with no relation to the clock, passes through two flip-flops: S1 samples it, S2 samples S1.
- In real hardware S1 may go metastable when A changes right at a clock edge; S2 gives it a whole clock period to resolve before anything uses it. The logic simulator here shows the ideal behavior: the two-cycle delay through the synchronizer.
- Everything downstream reads only S2, which is synchronous to the clock.
:::

```sim logic
clock CLK 8
input A
output S2
always @(posedge CLK) begin
  S1 <= A;
  S2 <= S1;
end
stim A 0@0 1@11 0@29 1@43
wave: CLK, A, S1, S2
time: 64
```

::: predict
## A synchronizer flip-flop has a resolution time constant τ = 50 ps. Compared with allowing 0.5 ns to resolve, how much does allowing 3 ns improve the mean time between failures?

- [ ] Six times
- [ ] About 36 times
- [x] About $10^{22}$ times
- [ ] Not at all

::: reveal
The chance that a metastable flip-flop is still undecided after a time $t_r$ falls as $e^{-t_r/\tau}$, so the MTBF grows as $e^{t_r/\tau}$: 2.5 ns more is 50 more time constants, a factor of $e^{50}$ = 5 × 10²¹. With a 250 MHz clock and data changing a million times a second, 0.5 ns gives a failure every 0.09 s; 3 ns, about once in 10¹³ years.
:::
:::

::: nameit
## Setup and hold

For every path from one flip-flop to the next:

- **setup**: the data must arrive a setup time $t_{su}$ before the next edge: $T \ge t_{cq} + t_\text{logic,max} + t_{su}$, giving the maximum clock rate. **Slack** is the period minus the required time; negative slack fails.
- **hold**: the data must not change until a hold time $t_h$ after the edge: $t_{cq} + t_\text{logic,min} \ge t_h$, independent of the clock rate, and fixed by adding delay to short paths.

**Clock skew**, the difference in when the edge reaches the two flip-flops, eats into one check or the other. Static timing analysis checks every path against both.

## Metastability

A flip-flop whose input changes inside its setup-and-hold window may hang between 0 and 1 before resolving. The mean time between failures of a synchronizer allowing a time $t_r$ to resolve is

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

with device constants τ and $T_0$ (around 50 ps and 1 ns in modern processes).

## Crossing clock domains

- A single bit crosses through a **two-flip-flop synchronizer**, which gives the first flip-flop a whole period to resolve.
- A multi-bit value can't be synchronized bit by bit, since its bits could be caught in different cycles. Use a **Gray-coded** counter (only one bit changes at a time), a **handshake** that holds the data steady while a synchronized flag crosses, or an **asynchronous FIFO** (S3).
:::

::: example
## A path has 0.3 ns clock-to-output, 3.2 ns of logic and 0.2 ns setup. What's its maximum clock, and its slack at 250 MHz?

::: solution
1. **Path delay.**
   $$\begin{aligned} t &= 0.3 + 3.2 + 0.2 \\ &= 3.7\ \text{ns} \end{aligned}$$
2. **Maximum clock.**
   $$\frac{1}{3.7\ \text{ns}} = 270\ \text{MHz}$$
3. **Slack at 250 MHz.** The period is 4.0 ns:
   $$4.0 - 3.7 = 0.3\ \text{ns}$$

**Answer:** a maximum clock of 270 MHz, and 0.3 ns of slack at 250 MHz.
:::
:::

::: example
## A flip-flop with 0.1 ns clock-to-output feeds another through a 0.05 ns wire. The receiving flip-flop needs 0.2 ns of hold. Does it pass?

::: solution
1. **When the data changes.**
   $$\begin{aligned} t &= 0.1 + 0.05 \\ &= 0.15\ \text{ns} \end{aligned}$$
   after the edge.
2. **Compare with the hold window.** The data changes before the 0.2 ns hold window closes:
   $$0.2 - 0.15 = 0.05\ \text{ns}$$
   a hold violation of 0.05 ns.
3. **The fix.** Add delay to the path, since slowing the clock doesn't help hold.

**Answer:** it fails, with a hold violation of 0.05 ns.
:::
:::

::: example
## A single flip-flop synchronizes a 1 MHz signal into a 100 MHz domain with only 0.5 ns to settle. With τ = 50 ps and $T_0$ = 0.1 ns, what is its MTBF?

::: solution
1. **Settling time in time constants.**
   $$\begin{aligned} \frac{t}{\tau} &= \frac{0.5\ \text{ns}}{50\ \text{ps}} \\ &= 10 \end{aligned}$$
2. **MTBF.**
   $$\begin{aligned} \text{MTBF} &= \frac{e^{t/\tau}}{T_0 f_\text{clk} f_\text{data}} \\ &= \frac{e^{10}}{10^{-10} \times 10^8 \times 10^6}\ \text{s} \\ &= 2.2\ \text{s} \end{aligned}$$
   It fails every couple of seconds.
3. **A second flip-flop.** It adds a whole 10 ns period of settling, multiplying the MTBF by
   $$e^{10\ \text{ns}/50\ \text{ps}} = e^{200}$$
   effectively never.

**Answer:** an MTBF of 2.2 s, a failure every couple of seconds; a second flip-flop multiplies it by $e^{200}$.
:::
:::

::: check
given: c = 0.1 | 0.3; l = 1.5 | 3.2 | 6; s = 0.1 | 0.2
answer: 1000/(c + l + s)
unit: MHz
tolerance: 0.5%

## A path has {c} ns clock-to-output, {l} ns of logic and {s} ns of setup time. What's the fastest clock it allows?

::: right
Right. $1/(t_{cq} + t_\text{logic} + t_{su})$ = {answer} MHz.
:::

::: misses
- [1000/l] The flip-flops' own times count too.
- [any] Not yet. One over the total path time.
:::
:::

::: check
given: f = 100 | 200 | 400; c = 0.2 | 0.3; l = 1 | 2 | 3.5; s = 0.1 | 0.15
answer: 1000/f - (c + l + s)
unit: ns
tolerance: 0.5%

## A {f} MHz design has a path with {c} ns clock-to-output, {l} ns of logic and {s} ns setup. What's its setup slack?

::: right
Right. Period minus path: {answer} ns.
:::

::: misses
- [any] Not yet. Clock period minus the time the data needs.
:::
:::

::: check
given: t = 1 | 2 | 3; f = 100 | 250; d = 0.1 | 1 | 10
answer: exp(t/0.05)/(1e-9*f*1e6*d*1e6)
unit: s
tolerance: 1%

## A synchronizer with τ = 50 ps and $T_0$ = 1 ns allows {t} ns to resolve, on a {f} MHz clock with data changing {d} million times a second. What's its MTBF?

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

::: misses
- [any] Not yet. The MTBF formula.
:::
:::

::: check
given: c = 0.08 | 0.1 | 0.15; m = 0.03 | 0.05 | 0.2; h = 0.1 | 0.25
answer: c + m - h
unit: ns
tolerance: 0.5%

## A flip-flop with {c} ns clock-to-output feeds another through {m} ns of logic, and the second needs {h} ns of hold. What's the hold slack?

::: right
Right. $t_{cq} + t_\text{min} - t_h$ = {answer} ns.
:::

::: misses
- [any] Not yet. When the data changes, minus when it may.
:::
:::

::: check
kind: parts
given: c = 0.2 | 0.4; l = 2 | 3.5 | 6; s = 0.1 | 0.3; f = 100 | 200

## A path has {c} ns clock-to-output, {l} ns of logic and {s} ns of setup, clocked at {f} MHz.

::: part
answer: 1000/(c + l + s)
unit: MHz
tolerance: 0.5%

### What is its maximum clock?

::: misses
- [any] 1 ÷ (sum).
:::
:::

::: part
answer: 1000/f - (c + l + s)
unit: ns
tolerance: 0.5%

### What is its slack at {f} MHz?

::: misses
- [any] Period less the path.
:::
:::
:::

::: check
kind: parts
given: t = 0.3 | 0.5 | 1; T = 10 | 20 | 40; fd = 1 | 10

## A synchronizer has τ = 50 ps and $T_0$ = 0.1 ns, with {t} ns to settle, on a 100 MHz clock sampling data that changes {fd} million times a second.

::: part
answer: exp(t/0.05)/(1e-10*1e8*fd*1e6)
unit: s
tolerance: 1%

### What is its MTBF?

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

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

### By how many powers of ten does a second flip-flop, adding {T} ns of settling, raise it?

::: misses
- [any] $e^{T/\tau}$ is $10^{T/(\tau\ln 10)}$.
:::
:::
:::

::: explain
## Why can't a multi-bit counter value be passed between clock domains through a synchronizer on each bit?

::: model
When a binary counter increments, several bits can change at once, for example from 0111 to 1000. A receiving clock edge that arrives while those bits are changing may catch some of them old and some new, and each bit's synchronizer resolves independently, so the receiver can see a value such as 1111 or 0000 that the counter never held. Synchronizing each bit guarantees each bit is clean, but not that they're consistent with each other. Gray-coding the counter, so only one bit changes per step, means any capture is either the old value or the new one, and a handshake or FIFO can hold whole words steady while a single synchronized flag crosses.
:::
:::

::: practice

::: check
given: c = 0.05 | 0.5; l = 0.5 | 10; s = 0.05 | 0.3
answer: 1000/(c + l + s)
unit: MHz
tolerance: 0.5%

## $f_\max$: {c} + {l} + {s} ns?

::: misses
- [any] One over the path time.
:::
:::

::: check
given: t = 0.5 | 4; f = 50 | 500; d = 0.01 | 50
answer: exp(t/0.05)/(1e-9*f*1e6*d*1e6)
unit: s
tolerance: 1%

## MTBF: {t} ns to resolve, {f} MHz clock, {d} M changes/s (τ = 50 ps, $T_0$ = 1 ns)?

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