---
title: "Timing lab: a static timing analyzer"
heading: Arrival times, required times, slack and the critical path of a gate netlist
eyebrow: S3 · Lesson 49 · Timing lab
xp: 60
---

::: hook
Lesson 16 computed slack for one path at a time. A real design has billions of paths, far too many to list, yet a static timing analyzer finds the worst one in time proportional to the number of gates. The trick is that the latest arrival at a net depends only on the latest arrivals at its gate's inputs, so one forward pass in topological order gives every net's arrival time, and one backward pass gives every net's required time. In this lab you'll write that analyzer for a gate netlist: arrival times, required times and slack, and the critical path a timing report prints.
:::

::: predict
## A netlist has 18 gates, and between its inputs and outputs there are 84 distinct paths. How many gate-delay additions does a static timing analyzer need to find every net's latest arrival time?

- [ ] At least 84, one per path
- [x] About one per gate input: a few dozen
- [ ] 18 × 84
- [ ] $2^{18}$

::: reveal
One per gate input. Each gate's arrival is the maximum over its inputs' arrivals, plus its delay, and those input arrivals are already known when the gates are visited in topological order. The number of paths can grow exponentially with the depth of the logic (here 84 paths, in large designs astronomically many), but the work stays proportional to the size of the netlist. That's what makes static timing analysis practical for whole chips.
:::
:::

::: nameit
## The netlist

A netlist is a dictionary from each gate's output net to its delay and its input nets:

```python
FA = {'x1': (3, ['a', 'b']), 'a1': (2, ['a', 'b']),
      'x2': (3, ['x1', 'c']), 'a2': (2, ['x1', 'c']),
      'o1': (2, ['a1', 'a2'])}
```

This is a full adder: x2 is the sum and o1 the carry. Nets that no gate drives (a, b, c) are primary inputs, arriving at time 0 unless given.

## Forward: arrival times

$$\text{AT}(g) = \max_{n \in \text{inputs}(g)} \text{AT}(n) + d_g$$

Visit gates in topological order, or compute each net's arrival by recursion with memoization, so each is computed once.

## Backward: required times and slack

Each output must arrive by the clock period T. A net that feeds several gates must arrive early enough for the most demanding one:

$$\text{RT}(n) = \min\left(T \text{ if } n \text{ is an output},\ \min_{g \in \text{fanout}(n)} \text{RT}(g) - d_g\right)$$

Slack = RT − AT. A negative slack anywhere means some path through that net is too slow. The slack of a net equals T minus the longest complete path through it.

## The critical path

From an output, step back each time to the input that arrives last (the first one listed, on a tie), until reaching a primary input. The delays along that path add up to the output's arrival time.

## What you'll write

1. `arrival_times(gates, inputs_at=None)`: a dict of every net's arrival time.
2. `required_and_slack(gates, arrivals, outputs, T)`: a dict of slack for every net that reaches an output.
3. `critical_path(gates, arrivals, out)`: the list of nets from a primary input to `out`.

The tests check your answers on the full adder and against brute-force enumeration of every path in randomly generated netlists.
:::

::: example
## Find the arrival times and slacks in the full adder with T = 8.

::: solution
1. **Forward pass: arrival times.** The inputs arrive at 0, so x1 = 3 and a1 = 2, and then
   $$\begin{aligned} x_2 &= \max(3, 0) + 3 \\ &= 6 \\ a_2 &= \max(3, 0) + 2 \\ &= 5 \\ o_1 &= \max(2, 5) + 2 \\ &= 7 \end{aligned}$$
2. **Backward pass: required times.** From 8 at both outputs, a1 and a2 must be ready by $8 - 2 = 6$, and
   $$\begin{aligned} r_{x_1} &= \min(8 - 3, 6 - 2) \\ &= 4 \\ r_c &= \min(5, 4) \\ &= 4 \\ r_{a,b} &= \min(4 - 3, 6 - 2) \\ &= 1 \end{aligned}$$
3. **Slacks.** Required minus arrival: o1 1, x2 2, a2 1, a1 4, x1 1, a 1, b 1, c 4.
4. **Critical path.** The nets with the least slack, a (or b), x1, a2 and o1, form the critical path.

**Answer:** arrivals x1 3, a1 2, x2 6, a2 5, o1 7; slacks o1 1, x2 2, a2 1, a1 4, x1 1, a 1, b 1 and c 4, with the critical path through a, x1, a2 and o1.
:::
:::

::: example
## The clock tightens to T = 6. Which nets fail, and what would fix them?

::: solution
1. **New slacks.** Every slack drops by 2: o1, a2, x1, a and b go to −1, x2 to 0, and a1 and c stay positive at 2.
2. **The failing path.** It is a → x1 → a2 → o1, the carry.
3. **Fixes.** A faster AND for a2 (1 instead of 2), or a carry computed from a, b and c directly (the carry-lookahead idea of lesson 8), would recover the cycle.
4. **What wouldn't help.** Speeding up a1, since it already has slack.

**Answer:** o1, a2, x1, a and b fail at −1 along the carry path; a faster a2 or a lookahead carry fixes it.
:::
:::

::: check
kind: python

## Step 1. Write `arrival_times(gates, inputs_at)`: the latest arrival time of every net.

```python starter
def arrival_times(gates, inputs_at=None):
    # gates: {net: (delay, [input nets])}; nets not in gates are primary inputs.
    # Return {net: latest arrival time} for every net, inputs included.
    return {}

FA = {'x1': (3, ['a', 'b']), 'a1': (2, ['a', 'b']), 'x2': (3, ['x1', 'c']), 'a2': (2, ['x1', 'c']), 'o1': (2, ['a1', 'a2'])}
print(arrival_times(FA))
```

```python solution
def arrival_times(gates, inputs_at=None):
    inputs_at = inputs_at or {}
    arr = {}
    def at(net):
        if net in arr:
            return arr[net]
        if net not in gates:
            arr[net] = inputs_at.get(net, 0.0)
        else:
            d, ins = gates[net]
            arr[net] = max(at(n) for n in ins) + d
        return arr[net]
    for g in gates:
        at(g)
    return arr

FA = {'x1': (3, ['a', 'b']), 'a1': (2, ['a', 'b']), 'x2': (3, ['x1', 'c']), 'a2': (2, ['x1', 'c']), 'o1': (2, ['a1', 'a2'])}
print(arrival_times(FA))
```

```python tests
import random
FA = {'x1': (3, ['a', 'b']), 'a1': (2, ['a', 'b']), 'x2': (3, ['x1', 'c']), 'a2': (2, ['x1', 'c']), 'o1': (2, ['a1', 'a2'])}
def rand_netlist(seed):
    rng = random.Random(seed)
    nets = ['i0', 'i1', 'i2', 'i3']
    g = {}
    for k in range(18):
        name = f'g{k}'
        g[name] = (rng.randint(1, 9), rng.sample(nets, rng.randint(1, min(3, len(nets)))))
        nets.append(name)
    used = {n for d, ins in g.values() for n in ins}
    outs = [n for n in g if n not in used]
    return g, outs
def all_paths(g, outs):
    paths = []
    def walk(net, suffix):
        if net not in g:
            paths.append([net] + suffix)
            return
        for n in g[net][1]:
            walk(n, [net] + suffix)
    for o in outs:
        walk(o, [])
    return paths
def plen(g, p, ia):
    return ia.get(p[0], 0.0) + sum(g[n][0] for n in p[1:])
arr = arrival_times(FA)
want = {'a': 0, 'b': 0, 'c': 0, 'x1': 3, 'a1': 2, 'x2': 6, 'a2': 5, 'o1': 7}
for n, v in want.items():
    assert n in arr and abs(arr[n] - v) < 1e-12, f"In the full adder, {n} should arrive at {v}; got {arr.get(n)}."
arr = arrival_times(FA, {'c': 4})
assert abs(arr['o1'] - 8) < 1e-12 and abs(arr['x2'] - 7) < 1e-12, f"With c arriving at 4, x2 should arrive at 7 and o1 at 8; got {arr['x2']} and {arr['o1']}."
for seed in range(6):
    g, outs = rand_netlist(seed)
    ia = {'i1': 2.5, 'i3': 1.0}
    arr = arrival_times(g, ia)
    for o in outs:
        brute = max(plen(g, p, ia) for p in all_paths(g, [o]))
        assert abs(arr[o] - brute) < 1e-9, f"Random netlist {seed}: {o} should arrive at {brute} (longest path); got {arr[o]}."
```

::: right
Right. One pass in topological order (here, by memoized recursion) finds the longest path to every net, however many paths there are.
:::
:::

::: check
kind: python

## Step 2. Write `required_and_slack(gates, arrivals, outputs, T)`: required times backward from the outputs, and each net's slack.

```python starter
def arrival_times(gates, inputs_at=None):
    inputs_at = inputs_at or {}
    arr = {}
    def at(net):
        if net in arr:
            return arr[net]
        if net not in gates:
            arr[net] = inputs_at.get(net, 0.0)
        else:
            d, ins = gates[net]
            arr[net] = max(at(n) for n in ins) + d
        return arr[net]
    for g in gates:
        at(g)
    return arr

def required_and_slack(gates, arrivals, outputs, T):
    # Required time T at each output; work backward through every fanout.
    # Return {net: slack} for every net that reaches an output.
    return {}

FA = {'x1': (3, ['a', 'b']), 'a1': (2, ['a', 'b']), 'x2': (3, ['x1', 'c']), 'a2': (2, ['x1', 'c']), 'o1': (2, ['a1', 'a2'])}
print(required_and_slack(FA, arrival_times(FA), ['x2', 'o1'], 8))
```

```python solution
def arrival_times(gates, inputs_at=None):
    inputs_at = inputs_at or {}
    arr = {}
    def at(net):
        if net in arr:
            return arr[net]
        if net not in gates:
            arr[net] = inputs_at.get(net, 0.0)
        else:
            d, ins = gates[net]
            arr[net] = max(at(n) for n in ins) + d
        return arr[net]
    for g in gates:
        at(g)
    return arr

def required_and_slack(gates, arrivals, outputs, T):
    fanout = {}
    for g, (d, ins) in gates.items():
        for n in ins:
            fanout.setdefault(n, []).append(g)
    req = {}
    def rq(net):
        if net in req:
            return req[net]
        r = T if net in outputs else float('inf')
        for g in fanout.get(net, []):
            r = min(r, rq(g) - gates[g][0])
        req[net] = r
        return r
    for n in arrivals:
        rq(n)
    return {n: req[n] - arrivals[n] for n in arrivals if req[n] != float('inf')}

FA = {'x1': (3, ['a', 'b']), 'a1': (2, ['a', 'b']), 'x2': (3, ['x1', 'c']), 'a2': (2, ['x1', 'c']), 'o1': (2, ['a1', 'a2'])}
print(required_and_slack(FA, arrival_times(FA), ['x2', 'o1'], 8))
```

```python tests
import random
FA = {'x1': (3, ['a', 'b']), 'a1': (2, ['a', 'b']), 'x2': (3, ['x1', 'c']), 'a2': (2, ['x1', 'c']), 'o1': (2, ['a1', 'a2'])}
def rand_netlist(seed):
    rng = random.Random(seed)
    nets = ['i0', 'i1', 'i2', 'i3']
    g = {}
    for k in range(18):
        name = f'g{k}'
        g[name] = (rng.randint(1, 9), rng.sample(nets, rng.randint(1, min(3, len(nets)))))
        nets.append(name)
    used = {n for d, ins in g.values() for n in ins}
    outs = [n for n in g if n not in used]
    return g, outs
def all_paths(g, outs):
    paths = []
    def walk(net, suffix):
        if net not in g:
            paths.append([net] + suffix)
            return
        for n in g[net][1]:
            walk(n, [net] + suffix)
    for o in outs:
        walk(o, [])
    return paths
def plen(g, p, ia):
    return ia.get(p[0], 0.0) + sum(g[n][0] for n in p[1:])
arr = arrival_times(FA)
sl = required_and_slack(FA, arr, ['x2', 'o1'], 8)
want = {'o1': 1, 'x2': 2, 'a2': 1, 'a1': 4, 'x1': 1, 'a': 1, 'b': 1, 'c': 4}
for n, v in want.items():
    assert n in sl and abs(sl[n] - v) < 1e-12, f"At T = 8, {n} should have slack {v}; got {sl.get(n)}."
for seed in range(6):
    g, outs = rand_netlist(seed)
    arr = arrival_times(g)
    T = 30
    sl = required_and_slack(g, arr, outs, T)
    through = {}
    for p in all_paths(g, outs):
        L = plen(g, p, {})
        for n in p:
            through[n] = max(through.get(n, -1e9), L)
    for n, L in through.items():
        assert abs(sl[n] - (T - L)) < 1e-9, f"Random netlist {seed}: {n} should have slack {T - L} (T minus the longest path through it); got {sl.get(n)}."
```

::: right
Right. Required times flow backward exactly as arrivals flow forward, taking the minimum over fanouts instead of the maximum over fanins.
:::
:::

::: check
kind: python

## Step 3. Write `critical_path(gates, arrivals, out)`: the path that sets an output's arrival time.

```python starter
def arrival_times(gates, inputs_at=None):
    inputs_at = inputs_at or {}
    arr = {}
    def at(net):
        if net in arr:
            return arr[net]
        if net not in gates:
            arr[net] = inputs_at.get(net, 0.0)
        else:
            d, ins = gates[net]
            arr[net] = max(at(n) for n in ins) + d
        return arr[net]
    for g in gates:
        at(g)
    return arr

def critical_path(gates, arrivals, out):
    # Walk back from out, always to the input that arrives last (the first on ties).
    # Return the nets from a primary input to out.
    return [out]

FA = {'x1': (3, ['a', 'b']), 'a1': (2, ['a', 'b']), 'x2': (3, ['x1', 'c']), 'a2': (2, ['x1', 'c']), 'o1': (2, ['a1', 'a2'])}
print(critical_path(FA, arrival_times(FA), 'o1'))
```

```python solution
def arrival_times(gates, inputs_at=None):
    inputs_at = inputs_at or {}
    arr = {}
    def at(net):
        if net in arr:
            return arr[net]
        if net not in gates:
            arr[net] = inputs_at.get(net, 0.0)
        else:
            d, ins = gates[net]
            arr[net] = max(at(n) for n in ins) + d
        return arr[net]
    for g in gates:
        at(g)
    return arr

def critical_path(gates, arrivals, out):
    path = [out]
    net = out
    while net in gates:
        d, ins = gates[net]
        best = ins[0]
        for n in ins[1:]:
            if arrivals[n] > arrivals[best]:
                best = n
        path.append(best)
        net = best
    return path[::-1]

FA = {'x1': (3, ['a', 'b']), 'a1': (2, ['a', 'b']), 'x2': (3, ['x1', 'c']), 'a2': (2, ['x1', 'c']), 'o1': (2, ['a1', 'a2'])}
print(critical_path(FA, arrival_times(FA), 'o1'))
```

```python tests
import random
FA = {'x1': (3, ['a', 'b']), 'a1': (2, ['a', 'b']), 'x2': (3, ['x1', 'c']), 'a2': (2, ['x1', 'c']), 'o1': (2, ['a1', 'a2'])}
def rand_netlist(seed):
    rng = random.Random(seed)
    nets = ['i0', 'i1', 'i2', 'i3']
    g = {}
    for k in range(18):
        name = f'g{k}'
        g[name] = (rng.randint(1, 9), rng.sample(nets, rng.randint(1, min(3, len(nets)))))
        nets.append(name)
    used = {n for d, ins in g.values() for n in ins}
    outs = [n for n in g if n not in used]
    return g, outs
def all_paths(g, outs):
    paths = []
    def walk(net, suffix):
        if net not in g:
            paths.append([net] + suffix)
            return
        for n in g[net][1]:
            walk(n, [net] + suffix)
    for o in outs:
        walk(o, [])
    return paths
def plen(g, p, ia):
    return ia.get(p[0], 0.0) + sum(g[n][0] for n in p[1:])
arr = arrival_times(FA)
p = critical_path(FA, arr, 'o1')
assert p == ['a', 'x1', 'a2', 'o1'], f"The critical path to o1 is a → x1 → a2 → o1; got {p}."
for seed in range(6):
    g, outs = rand_netlist(seed)
    arr = arrival_times(g)
    for o in outs:
        p = critical_path(g, arr, o)
        assert p[-1] == o and p[0] not in g, f"Random netlist {seed}: the path to {o} must run from a primary input to {o}; got {p}."
        for u, v in zip(p, p[1:]):
            assert u in g[v][1], f"Random netlist {seed}: {u} doesn't drive {v}."
        assert abs(sum(g[n][0] for n in p[1:]) - arr[o]) < 1e-9, f"Random netlist {seed}: the delays along {p} should add up to {o}'s arrival, {arr[o]}."
```

::: right
Right. Following the latest-arriving input back from an output recovers the path a timing report prints.
:::
:::

::: explain
## Why can a static timing analyzer find the worst path without listing all the paths?

::: model
The latest time a signal can arrive at a net depends only on the latest arrival times at the inputs of the gate that drives it, not on which paths led there. So if every gate is visited after all the gates that feed it, each net's arrival time is computed once, from numbers already known, and the work is one step per gate input. The same holds backward for required times. The longest path is still found, because the maximum at each gate carries the longest of the incoming paths forward, and tracing back through the latest-arriving inputs recovers it at the end. Listing paths would grow exponentially with logic depth; this grows only with the size of the netlist.
:::
:::

::: practice

::: check
given: a = 120 | 260; b = 180 | 310; d = 35 | 70
answer: (a + b + abs(a - b))/2 + d
unit: ps
tolerance: 0.5%

## A gate with delay {d} ps has inputs arriving at {a} and {b} ps. When does its output arrive?

::: misses
- [any] The later input plus the delay.
:::
:::

::: check
given: r = 400 | 650; s = 520 | 700; d = 40 | 90; e = 25 | 60
answer: (r - d + s - e - abs(r - d - s + e))/2
unit: ps
tolerance: 0.5%

## A net feeds two gates: one of delay {d} ps whose output is required by {r} ps, and one of delay {e} ps required by {s} ps. When is the net required?

::: misses
- [any] The earlier of the two (required time minus delay).
:::
:::

::: check
given: T = 500 | 800; A = 420 | 610; D = 30 | 120
answer: T - A - D
unit: ps
tolerance: 0.5%

## A net arrives at {A} ps, and the longest path from it to an output takes {D} ps more. With a {T} ps clock, what's its slack?

::: misses
- [any] T minus the longest full path through it.
:::
:::
:::
