---
title: Timing lab
heading: Slack, pipelining, synchronizers and timing yield, in Python
eyebrow: C7 · Lesson 34 · Timing lab
xp: 50
---

::: hook
A design that works in simulation can still fail in silicon because a path is too slow, a hold check is violated, a synchronizer is too short, or the manufactured gates come out slower than the typical ones. Timing engineers answer all four with arithmetic on delays. In this lab you'll compute setup and hold slack with clock skew, cut a chain of logic into the best pipeline stages, size a synchronizer, and estimate how many chips meet a clock target when every gate's delay varies.

::: play
- The curves are a pipeline's throughput and latency against its number of stages, for a fixed amount of logic plus a fixed overhead per stage.
- Throughput rises with depth but levels off: each stage pays the register overhead again.
- Latency only grows. Find the depth where adding a stage buys little throughput for a lot of latency.
:::
:::

```sim plot
view: graph
x: k = 1..20
y: r = 0..3
slider: L = 2..20 = 9 ns
slider: o = 0.1..2 step 0.1 = 1 ns
plot: 1/(L/k + o) | label: throughput, results per ns | samples: 200
plot: k*(L/k + o)/L | color: ch2 | label: latency relative to the logic alone | samples: 200
readout: throughput limit, per ns = 1/o | digits: 3
```

::: predict
## A path's delay is the sum of ten stages, each 0.95 ns on average with a standard deviation of 0.1 ns, independent. What fraction of chips meet a 10 ns clock?

- [ ] Half
- [ ] All of them, since the average is 9.5 ns
- [x] About 94%
- [ ] About 68%

::: reveal
About 94%. The sum's mean is 9.5 ns and its standard deviation is $0.1\sqrt{10}$ = 0.32 ns, so 10 ns is 1.58 standard deviations above the mean: $\Phi(1.58)$ = 0.943. The 6% that fail must be sold at a lower clock or discarded, which is why designers add margin and why chips are speed-binned.
:::
:::

::: nameit
## Slack

For a path between registers with clock period T and skew s (positive when the capture clock is late):

$$\text{setup slack} = T + s - (t_{pcq} + t_{pd} + t_\text{setup}), \qquad \text{hold slack} = t_{ccq} + t_{cd} - (t_\text{hold} + s).$$

Negative setup slack means the clock must slow down; negative hold slack can't be fixed by the clock at all.

## Pipelining

Logic of total delay L split into k stages, each paying overhead o: the period is $\frac Lk + o$ if the split is even, the throughput its reciprocal, and the latency $L + ko$. Real logic comes in pieces that can't be cut anywhere, and the best split into k stages minimizes the largest stage's delay. Bisection on that maximum, checking whether a greedy pass fits the pieces into k stages, finds it.

## Synchronizers

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

with $t_r$ the resolution time: a clock period less setup, per extra stage.

## Timing yield

If a path is the sum of n independent stage delays with mean μ and standard deviation σ, its delay is approximately normal with mean nμ and standard deviation $\sigma\sqrt n$, and the fraction of chips meeting period T is $\Phi\left(\frac{T - n\mu}{\sigma\sqrt n}\right)$.
:::

::: example
## Split logic blocks of 3, 1, 4, 1, 5, 9, 2 and 6 ns, in order, into three pipeline stages for the fastest clock.

::: solution
1. **Lower bounds.** The 9 ns block alone sets a lower bound, and the total is 31 ns, so a stage must hold at least
   $$\frac{31}{3} = 10.3\ \text{ns}$$
2. **Bisect on the stage limit.** Packing blocks greedily, 13 ns doesn't fit in three stages, and 14 ns does, for example as 3 + 1 + 4 + 1 + 5 = 14, then 9 + 2 = 11, then 6.
3. **The clock.** The best clock period is 14 ns plus the register overhead: the uneven blocks cost 3.7 ns against an ideal split.

**Answer:** 3 + 1 + 4 + 1 + 5, then 9 + 2, then 6, for a 14 ns period plus the register overhead.
:::
:::

::: example
## A 1 GHz path has $t_{pcq}$ = 80 ps, 750 ps of logic and 40 ps of setup; its shortest path has $t_{ccq}$ = 50 ps and 30 ps of logic, against 60 ps of hold. The capture clock is 100 ps late. Find both slacks.

::: solution
1. **Setup slack.**
   $$\begin{aligned} &1000 + 100 - (80 + 750 + 40) \\ &= 230\ \text{ps} \end{aligned}$$
   comfortable.
2. **Hold slack.**
   $$\begin{aligned} &50 + 30 - (60 + 100) \\ &= -80\ \text{ps} \end{aligned}$$
   a violation.
3. **The skew.** The skew that helped setup by 100 ps hurt hold by the same amount.
4. **The fix.** About 80 ps of delay buffers in the short path, which leaves setup at 230 ps since the long path is a different path.

**Answer:** the setup slack is 230 ps and the hold slack −80 ps, a violation.
:::
:::

::: example
## Ten stages of 0.95 ns mean and 0.1 ns standard deviation must meet a 10 ns clock. What's the timing yield, and what clock gives 99.9%?

::: solution
1. **Timing yield.**
   $$\begin{aligned} \text{yield} &= \Phi\left(\frac{10 - 9.5}{0.316}\right) \\ &= \Phi(1.58) \\ &= 94.3\% \end{aligned}$$
2. **Clock for 99.9%.** The clock must sit 3.09 standard deviations above the mean:
   $$\begin{aligned} T &= 9.5 + 3.09 \times 0.316 \\ &= 10.48\ \text{ns} \end{aligned}$$
3. **Long paths.** Independent variations average out along a path, so long paths have relatively less spread than short ones.
4. **Die-wide variation.** Correlated variation, such as a whole die running slow, doesn't average out, and needs separate margin.

**Answer:** the timing yield is 94.3%, and a 10.48 ns clock gives 99.9%.
:::
:::

::: check
kind: python

## Step 1. Write `slacks(T, skew, tpcq, tpd, tsu, tccq, tcd, thold)` returning `(setup_slack, hold_slack)`.

```python starter
def slacks(T, skew, tpcq, tpd, tsu, tccq, tcd, thold):
    return T - (tpcq + tpd + tsu), tccq + tcd - thold

print(slacks(1000, 100, 80, 750, 40, 50, 30, 60))
```

```python solution
def slacks(T, skew, tpcq, tpd, tsu, tccq, tcd, thold):
    return T + skew - (tpcq + tpd + tsu), tccq + tcd - (thold + skew)

print(slacks(1000, 100, 80, 750, 40, 50, 30, 60))
```

```python tests
assert slacks(1000, 100, 80, 750, 40, 50, 30, 60) == (230, -80), "Setup 230 ps, hold −80 ps."
assert slacks(1000, 0, 80, 750, 40, 50, 30, 60) == (130, 20), "Without skew both pass."
```

::: right
Right. Skew moves slack from one check to the other.
:::
:::

::: check
kind: python

## Step 2. Write `best_split(delays, k)` returning the smallest possible maximum stage delay when the list of block delays is cut, in order, into at most k contiguous stages. Use bisection on the maximum with a greedy feasibility check (assume whole-number delays).

```python starter
def best_split(delays, k):
    return sum(delays) / k

print(best_split([3, 1, 4, 1, 5, 9, 2, 6], 3))
```

```python solution
def best_split(delays, k):
    def fits(m):
        stages, s = 1, 0
        for d in delays:
            if s + d > m:
                stages, s = stages + 1, d
            else:
                s += d
        return stages <= k
    lo, hi = max(delays), sum(delays)
    while lo < hi:
        mid = (lo + hi) // 2
        if fits(mid):
            hi = mid
        else:
            lo = mid + 1
    return lo

print(best_split([3, 1, 4, 1, 5, 9, 2, 6], 3))
```

```python tests
d = [3, 1, 4, 1, 5, 9, 2, 6]
assert best_split(d, 3) == 14 and best_split(d, 2) == 17, f"Expected 14 and 17; got {best_split(d, 3)}, {best_split(d, 2)}"
assert best_split(d, 8) == 9 and best_split([2] * 6, 3) == 4, "One block per stage; an even split."
```

::: right
Right. The answer can't beat the biggest block, and bisection finds the smallest limit that packs.
:::
:::

::: check
kind: python

## Step 3. Write `pipeline(L, k, o)` returning `(period, throughput, latency)` for logic of total delay L split evenly into k stages with overhead o per stage.

```python starter
def pipeline(L, k, o):
    return L / k, k / L, L

print(pipeline(9, 3, 1))
```

```python solution
def pipeline(L, k, o):
    period = L / k + o
    return period, 1 / period, k * period

print(pipeline(9, 3, 1))
```

```python tests
assert pipeline(9, 3, 1) == (4.0, 0.25, 12.0), "Lesson 27: 4 ns, 250 MHz, 12 ns."
assert pipeline(9, 9, 1) == (2.0, 0.5, 18.0), "Nine stages: 2 ns, 500 MHz, 18 ns."
```

::: right
Right. Throughput approaches $1/o$; latency grows by o per stage.
:::
:::

::: check
kind: python

## Step 4. Write `mtbf(T, tsu, tau, T0, fd, stages=2)` returning a synchronizer's MTBF in seconds, where each stage after the first adds a clock period T of resolution time, less setup: $t_r$ = (stages − 1)(T − tsu).

```python starter
import math

def mtbf(T, tsu, tau, T0, fd, stages=2):
    return math.exp(T / tau) / (T0 / T * fd)

print(mtbf(1e-9, 50e-12, 30e-12, 50e-12, 1e8))
```

```python solution
import math

def mtbf(T, tsu, tau, T0, fd, stages=2):
    tr = (stages - 1) * (T - tsu)
    return math.exp(tr / tau) / (T0 * (1 / T) * fd)

print(mtbf(1e-9, 50e-12, 30e-12, 50e-12, 1e8))
```

```python tests
m2 = mtbf(1e-9, 50e-12, 30e-12, 50e-12, 1e8)
assert abs(m2 / 1.1315887e7 - 1) < 1e-6, f"Two stages: about 1.13e7 s; got {m2}"
m3 = mtbf(1e-9, 50e-12, 30e-12, 50e-12, 1e8, 3)
assert m3 / m2 > 1e13, "A third stage multiplies the MTBF enormously."
```

::: right
Right. The exponent makes the stage count the designer's strongest lever.
:::
:::

::: check
kind: python

## Step 5. Write `timing_yield(n, mu, sigma, T)` returning the fraction of chips whose path, a sum of n independent normal stage delays, meets the period T. Use `math.erf`.

```python starter
def timing_yield(n, mu, sigma, T):
    return 1.0 if n * mu <= T else 0.0

print(timing_yield(10, 0.95, 0.1, 10))
```

```python solution
import math

def timing_yield(n, mu, sigma, T):
    z = (T - n * mu) / (sigma * math.sqrt(n))
    return 0.5 * (1 + math.erf(z / math.sqrt(2)))

print(timing_yield(10, 0.95, 0.1, 10))
```

```python tests
assert abs(timing_yield(10, 0.95, 0.1, 10) - 0.943077) < 1e-5, f"Expected 94.3%; got {timing_yield(10, 0.95, 0.1, 10)}"
assert abs(timing_yield(10, 0.95, 0.1, 9.5) - 0.5) < 1e-12, "At the mean, half pass."
assert abs(timing_yield(1, 0.9, 0.03, 1.0) - 0.999571) < 1e-5, "A short path with a wide margin."
```

::: right
Right. Margin measured in standard deviations is what sets yield.
:::
:::

::: check
kind: parts
given: n = 4 | 16 | 64; m = 0.5 | 1; s = 0.05 | 0.1

## A path is the sum of {n} independent stages, each {m} ns on average with standard deviation {s} ns.

::: part
answer: s*sqrt(n)
unit: ns
tolerance: 0.5%

### What's the path delay's standard deviation?

::: misses
- [s*n] Independent variations add in quadrature.
- [any] $\sigma\sqrt n$.
:::
:::

::: part
answer: n*m + 3*s*sqrt(n)
unit: ns
tolerance: 0.5%

### What period gives a yield of about 99.87%, three standard deviations of margin?

::: misses
- [any] $n\mu + 3\sigma\sqrt n$.
:::
:::
:::

::: explain
## Why does a longer path have relatively less timing variation than a short one, when its stages vary independently?

::: model
Each stage's delay is a little faster or slower than average, at random. Along a path of many stages, the fast ones and slow ones partly cancel, so the total's standard deviation grows only as the square root of the number of stages, while the total delay itself grows in proportion to it. The spread relative to the mean therefore shrinks as one over the square root of the length. This only holds for independent variation; variation that affects every gate on a chip the same way, like a slow wafer or a hot die, doesn't average out and needs its own margin.
:::
:::

::: practice

::: check
given: n = 9 | 25 | 100; s = 0.02 | 0.08
answer: s*sqrt(n)
unit: ns
tolerance: 0.5%

## {n} stages, each σ = {s} ns. Path σ?

::: misses
- [any] $\sigma\sqrt n$.
:::
:::
:::
