Skip to content
LumenaryAcademy
Digital 80 min

Timing and clocking

How fast a synchronous design can run, why it sometimes fails at any speed, and how signals cross safely between clocks.

Code tested · 7AI-draftedAwaiting review by an electrical engineer

Your guides: Chloe · The Physicist, Margaret · The Bench Engineer, Gideon · The Design Reviewer

MotivationH

How fast can it go?

Every Verilog simulation so far has been zero-delay: when the clock rises, flip-flops update, and the logic after them settles instantly. Real gates take time. Each one in a modern process takes tens of picoseconds, wires add more, and a path through dozens of gates can take most of a nanosecond. The clock period has to leave room for the slowest path, and that sets how fast the chip can run. This is why the same processor design is sold at several clock speeds, and why an FPGA tool, after placing and routing your design, reports a maximum frequency that may be lower than you hoped.

Timing is also where designs that pass every simulation fail on the bench. A path that is too fast can corrupt data at any clock speed. A signal arriving from another clock, or from a push-button, can catch a flip-flop mid-decision and leave it, briefly, neither 0 nor 1. This lesson gives you the vocabulary and arithmetic of timing (the setup and hold constraints, the critical path, skew and jitter), the main tool for going faster (pipelining), and the discipline for crossing between clocks safely (synchronisers and asynchronous FIFOs). It ends with how industry checks the timing of chips with billions of paths.

DefinitionsH

Delays, and the flip-flop's timing rules

A combinational block has two delays, not one.

Definition— Propagation and contamination delay

The propagation delay tpdt_{pd} of a combinational path is the maximum time from an input change until the output has settled to its final value. The contamination delay tcdt_{cd} is the minimum time from an input change until the output starts to change. Between the two, the output may glitch.

Delays vary with the input that changes, with the direction of the change, with the load, and with process, voltage and temperature, so tpdt_{pd} and tcdt_{cd} are worst-case bounds, taken from the cell library or the FPGA vendor's timing model. A flip-flop has its own timing.

Definition— Flip-flop timing

  • Clock-to-Q: after a rising clock edge, the output QQ begins to change no sooner than tccqt_{ccq} (contamination) and has settled no later than tpcqt_{pcq} (propagation).
  • Setup time tsetupt_{setup}: the input DD must be stable for at least this long before the clock edge.
  • Hold time tholdt_{hold}: DD must remain stable for at least this long after the clock edge.

Setup and hold together define a small window around each clock edge during which DD must not change. If it does, the flip-flop may capture the old value, the new one, or, rarely, neither for a while: the metastability you will meet later in this lesson.

DefinitionsH

The setup and hold constraints

Consider the basic situation of every synchronous design: a launching flip-flop, a block of combinational logic, and a capturing flip-flop, both on the same clock with period TcT_c.

clkQ (launch)D (capture)t_pcq + t_pd (latest)slacksetupholdt_ccq + t_cd (earliest)launch edgecapture edge, T_c later
Timing of one register-to-register path. After the launching edge, the new data at the capturing flip-flop's input settles at the latest after t_pcq + t_pd and must then be stable for t_setup before the next edge; what remains is the setup slack. The earliest change, t_ccq + t_cd after the edge, must not come before the capturing flip-flop's hold time has passed.

Theorem— Setup and hold constraints

With an ideal clock (no skew or jitter), the path works correctly at period TcT_c if and only if

Tc ≥ tpcq+tpd+tsetup(setup)T_c \ \ge\ t_{pcq} + t_{pd} + t_{setup} \qquad \text{(setup)}

tccq+tcd ≥ thold(hold)t_{ccq} + t_{cd} \ \ge\ t_{hold} \qquad \text{(hold)}

where tpdt_{pd} and tcdt_{cd} are the propagation and contamination delays of the logic between the two flip-flops.

Proof

Setup. After the launching edge at time 0, the launching flip-flop's output has settled by tpcqt_{pcq}, so the logic's output has settled by tpcq+tpdt_{pcq} + t_{pd}. The capturing flip-flop samples at the next edge, time TcT_c, and needs its input stable from Tc−tsetupT_c - t_{setup}. That holds for every possible input change exactly when tpcq+tpd≤Tc−tsetupt_{pcq} + t_{pd} \le T_c - t_{setup}. Hold. The same edge at time 0 also makes the capturing flip-flop sample the previous value, which must stay stable until tholdt_{hold}. The new value can begin to reach it as early as tccq+tcdt_{ccq} + t_{cd}. So the old value is safe exactly when tccq+tcd≥tholdt_{ccq} + t_{cd} \ge t_{hold}. Both bounds are tight because the delays are themselves worst cases that some input change attains.

∎

The two constraints are strikingly different. The setup constraint contains TcT_c: a setup violation can always be fixed by slowing the clock. The hold constraint does not contain TcT_c at all: it compares two delays that both start at the same clock edge. A hold violation fails at every clock frequency, which is why it must be found before the chip is made. Tools fix hold violations by adding delay (buffers) on the short path, and fix setup violations by making the long path faster or the clock slower.

Example— A shift register cannot violate setup, but it can violate hold

Connect two flip-flops directly, QQ to DD, with no logic: tpd=tcd=0t_{pd} = t_{cd} = 0. With tpcq=50t_{pcq} = 50 ps and tsetup=40t_{setup} = 40 ps, setup needs only Tc≥90T_c \ge 90 ps, which any realistic clock satisfies. Hold needs tccq≥tholdt_{ccq} \ge t_{hold}. Cell libraries are designed so that this holds for direct connections, with some margin, but only with a perfect clock. When the second flip-flop's clock arrives later than the first one's, that margin is eaten, as the next two sections show.

ComputationC

Finding the critical path

A real design has thousands of flip-flops and millions of paths between them. The setup constraint must hold for the slowest of all, the critical path, and the hold constraint for the fastest. Fortunately we do not need to list every path: in a circuit without loops (and the logic between registers has none), the latest arrival time at each signal is the latest arrival at any of its gate's inputs plus the gate's delay, computed once per gate in topological order. This is the longest-path problem on a directed acyclic graph, and it runs in time linear in the size of the circuit. Static timing analysis tools are, at heart, this computation.

R1R2R3C1C2G1 NANDG2 XORG4 INVG3 NORG5 AOI30–4540–7015–2035–5540–60flip-flops: t_ccq 30, t_pcq 50, t_setup 40, t_hold 25 (ps)
The small circuit analysed below, with each gate's contamination and propagation delays in picoseconds. Registers R1 to R3 launch on a clock edge; C1 and C2 capture on the next. The critical path, R2 through G2, G3 and G5 to C1, is drawn thick. The shortest path, R3 through the inverter G4 to C2, decides the hold check.

The program computes, for every signal, the earliest and the latest time it can change after a clock edge, recovers the critical path by walking back along the latest inputs, and then checks hold with and without clock skew, a difference between the times the clock reaches different flip-flops.

python
# Static timing analysis of a small netlist, all times in picoseconds.
T_CCQ, T_PCQ, T_SETUP, T_HOLD = 30, 50, 40, 25        # flip-flop timing

# gate: (inputs, contamination delay, propagation delay), listed in topological order
GATES = {
    "G1": (["R1", "R2"], 30, 45),            # NAND2
    "G2": (["R2", "R3"], 40, 70),            # XOR2
    "G4": (["R3"], 15, 20),                  # inverter
    "G3": (["G1", "G2"], 35, 55),            # NOR2
    "G5": (["G3", "G4", "R1"], 40, 60),      # AOI21
}
CAPTURE = {"C1": "G5", "C2": "G4"}           # capturing flip-flop: the signal on its D input

late = {r: T_PCQ for r in ("R1", "R2", "R3")}    # latest arrival: setup analysis
early = {r: T_CCQ for r in ("R1", "R2", "R3")}   # earliest arrival: hold analysis
worst_input = {}
for g, (ins, t_cd, t_pd) in GATES.items():
    worst_input[g] = max(ins, key=lambda i: late[i])
    late[g] = late[worst_input[g]] + t_pd
    early[g] = min(early[i] for i in ins) + t_cd

print("signal  earliest  latest (ps)")
for s in late:
    print(f"{s:6} {early[s]:9} {late[s]:7}")

# Walk back from the latest-arriving capture input to recover the critical path.
end = max(CAPTURE, key=lambda c: late[CAPTURE[c]])
path, s = [end], CAPTURE[end]
while s in GATES:
    path.append(s)
    s = worst_input[s]
path.append(s)
t_min = late[CAPTURE[end]] + T_SETUP
print("\ncritical path:", " -> ".join(reversed(path)))
print(f"minimum period {t_min} ps, maximum clock frequency {1e3 / t_min:.2f} GHz")

for skew in (0, 30):     # skew > 0: the capturing flip-flops' clock arrives later than the launching ones'
    print(f"\nclock skew {skew} ps")
    for c, d in CAPTURE.items():
        hold_slack = early[d] - (T_HOLD + skew)
        print(f"  {c}: hold slack {hold_slack:4} ps {'VIOLATED' if hold_slack < 0 else 'ok'}")
    print(f"  minimum period now {t_min - skew} ps")
Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

The critical path takes 235 ps to settle, so with 40 ps of setup the clock period must be at least 275 ps: about 3.6 GHz. (The path from R3 through G2 is equally long; ties are common, and tools report every path with negative or small slack, not just one.) Without skew, both hold checks pass. But if the capturing flip-flops' clock arrives 30 ps late, the short path through the inverter violates hold by 10 ps, while the setup check got easier. The fix is to add at least 10 ps of delay on that short path, typically a buffer or two, without lengthening any path that is setup-critical.

ExplanationH

Clock skew and jitter

The clock reaches thousands or millions of flip-flops through a clock distribution network: a tree of buffers, often laid out as an H-tree or a grid, designed so that every leaf sees the edge at nearly the same time. "Nearly" is the problem.

  • Skew is a fixed difference in arrival times between two flip-flops' clocks. Let δ\delta be how much later the capturing flip-flop's clock arrives than the launching one's. Then the setup constraint becomes Tc≥tpcq+tpd+tsetup−δT_c \ge t_{pcq} + t_{pd} + t_{setup} - \delta and the hold constraint tccq+tcd≥thold+δt_{ccq} + t_{cd} \ge t_{hold} + \delta. Positive skew helps setup and hurts hold; negative skew does the opposite. Designers must assume the worst sign for each check unless they control it, and some designs schedule skew deliberately ("useful skew") to borrow time between stages.
  • Jitter is the variation of the clock period from cycle to cycle, from noise in the oscillator or phase-locked loop (PLL) and in the supply of the clock buffers. It cannot be designed away, so it is subtracted from the period: a 1 GHz clock with ±10 ps of cycle-to-cycle jitter offers the logic only 980 ps in the worst case.

Timing budgets lump these into a clock uncertainty that the analysis subtracts from every period. For an FPGA, the vendor's tools know the device's clock network and compute skew and jitter themselves; you supply only the clock's frequency and the jitter of its source.

ComputationC

Pipelining

If the critical path is too long for the clock you need, you can split it with registers. A pipeline divides a long computation into stages, each finishing within one clock period, with a register between consecutive stages. Lumenary CS's From logic gates to a processor showed the idea for instruction execution; here are the numbers from the hardware side. Splitting logic of delay tt into kk equal stages gives

Tc=tk+tpcq+tsetup+tuncertainty,T_c = \frac{t}{k} + t_{pcq} + t_{setup} + t_{\text{uncertainty}},

so throughput rises nearly kk-fold while t/kt/k dominates, but every stage pays the register overhead, and latency, the time for one result, grows: k⋅Tc>tk \cdot T_c > t. The program splits 2.4 ns of logic with 110 ps of overhead per stage.

python
# Split 2.4 ns of logic into k pipeline stages. Each stage adds a register:
# clock-to-Q (50 ps) + setup (40 ps), and the clock loses 20 ps to skew and jitter.
LOGIC = 2400          # ps
OVERHEAD = 50 + 40 + 20
WIDTH = 64            # flip-flops per pipeline register, for a 64-bit data path

print("stages  period (ps)  results per ns  latency (ps)  flip-flops  time lost to overhead")
for k in (1, 2, 4, 8, 16, 32):
    period = LOGIC / k + OVERHEAD
    print(f"{k:6} {period:12.0f} {1e3 / period:15.2f} {k * period:13.0f} {k * WIDTH:11} {OVERHEAD / period:16.0%}")
Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

Two stages nearly double throughput (0.76 against 0.40 results per ns). At 32 stages, 59% of every cycle is register overhead, the latency has more than doubled, and the design has 2,048 flip-flops where it had 64, each costing area and clock power. Real stages are also never perfectly balanced; the slowest decides the period. Processor designers learned this the hard way in the early 2000s, when very deep pipelines (more than 30 stages in one well-known desktop design) reached high clock rates but spent too much power and lost too much work on every mispredicted branch, and the industry returned to shorter pipelines.

In hardware, pipelining must also keep data aligned: any value that a later stage combines with a result must be delayed by the same number of registers. The check below pipelines y=ab+cd+ey = ab + cd + e into three stages: two multiplications side by side, then the first addition, then the second; ee travels through two registers to meet the right products. The testbench compares it with the one-step version, three edges later.

verilog
// y = a*b + c*d + e, computed in one long combinational step, and in a 3-stage pipeline.
module mac_comb (input logic [15:0] a, b, c, d, e, output logic [33:0] y);
  assign y = a * b + c * d + e;
endmodule

module mac_pipe (input  logic clk, input logic [15:0] a, b, c, d, e, output logic [33:0] y);
  logic [31:0] p1, p2;            // stage 1: the two multiplications, side by side
  logic [15:0] e1, e2;            // e travels alongside, so it meets the right products
  logic [32:0] s2;                // stage 2: the first addition
  always_ff @(posedge clk) begin
    p1 <= a * b;   p2 <= c * d;   e1 <= e;     // stage 1
    s2 <= p1 + p2;                e2 <= e1;    // stage 2
    y  <= s2 + e2;                             // stage 3
  end
endmodule

module tb;
  logic clk = 0;
  logic [15:0] a, b, c, d, e;
  wire  [33:0] y_comb, y_pipe;
  mac_comb u1 (.*, .y(y_comb));
  mac_pipe u2 (.*, .y(y_pipe));
  always #5 clk = ~clk;

  logic [33:0] expected [$];      // a queue of results the pipeline still owes us
  int seed = 7, checked = 0;

  initial begin
    for (int cycle = 0; cycle < 200; cycle++) begin
      a = $random(seed); b = $random(seed); c = $random(seed); d = $random(seed); e = $random(seed);
      #1 expected.push_back(y_comb);           // what the one-step circuit says right now
      @(posedge clk); #1;
      if (cycle >= 2) begin                    // from the third edge on, a result emerges every cycle
        if (y_pipe !== expected.pop_front()) $fatal(1, "pipeline result wrong at cycle %0d", cycle);
        checked++;
      end
      if (cycle < 5)
        $display("edge %0d: this cycle's inputs -> %0d   pipeline y = %0d", cycle + 1, expected[$], y_pipe);
    end
    $display("%0d results checked: each emerges at the 3rd edge after its inputs, one per cycle", checked);
    $finish(0);
  end
endmodule

It prints:

text
edge 1: this cycle's inputs -> 2352512211   pipeline y = x
edge 2: this cycle's inputs -> 4276507378   pipeline y = x
edge 3: this cycle's inputs -> 166636875   pipeline y = 2352512211
edge 4: this cycle's inputs -> 5868249063   pipeline y = 4276507378
edge 5: this cycle's inputs -> 2064913869   pipeline y = 166636875
198 results checked: each emerges at the 3rd edge after its inputs, one per cycle

A result emerges at every edge from the third onwards, each belonging to the inputs presented three edges earlier: throughput of one per cycle, latency of three. Synthesis tools can also move registers across logic automatically to balance stages, a transformation called retiming, which is especially effective on FPGAs.

DefinitionsH

Metastability and synchronisers

The setup and hold constraints can be guaranteed only for signals timed by the same clock. A push-button, a signal from another chip, or data from another clock domain can change at any moment, including inside the setup–hold window. What happens then?

A flip-flop's storage element is a bistable loop, like a ball on a hilltop between two valleys. A clean input pushes the ball firmly into one valley. An input that changes at just the wrong moment can leave it balanced near the top, at a voltage in the forbidden band, a state called metastability. It cannot stay there forever, because noise and the loop's own gain push it off, but how long it takes to fall is random and has no upper bound. The probability that it is still unresolved after time tt falls off as e−t/τe^{-t/\tau}, where τ\tau, the resolution time constant, is a property of the flip-flop's circuit, of the order of 10 to 30 ps in modern processes.

This gives the classic estimate of the mean time between failures of a flip-flop sampling an asynchronous signal:

MTBF=etr/τT0 fclk fdata\text{MTBF} = \frac{e^{t_r / \tau}}{T_0 \, f_{\text{clk}} \, f_{\text{data}}}

where trt_r is the time allowed for resolution before the output is used, fclkf_{\text{clk}} the sampling clock frequency, fdataf_{\text{data}} the rate at which the asynchronous input changes, and T0T_0 a parameter of the flip-flop related to the width of its vulnerable window. The denominator counts how often the input lands in the window; the exponential is the chance of not resolving in time.

The standard defence is the two-flip-flop synchroniser: sample the asynchronous signal with one flip-flop, and use only the output of a second flip-flop that samples the first. The first may go metastable; the second gives it almost a whole clock period to resolve before anything uses it.

FF1FF2async input(domain A)may go metastableone periodto resolvesynchronisedto domain B logicclk_B
A two-flip-flop synchroniser. The first flip-flop samples the asynchronous signal and may occasionally go metastable; the second samples the first a clock period later, by which time it has almost certainly resolved. Only the second flip-flop's output may be used by logic in the new clock domain.

The program evaluates the formula with illustrative parameters (τ=T0=20\tau = T_0 = 20 ps, an input changing 10710^7 times per second) for a flip-flop that feeds logic directly with 200 ps to spare, and for synchronisers of two and three flip-flops.

python
import math

# Illustrative metastability parameters, of the right order for a modern process.
TAU = 20e-12                 # resolution time constant
T0 = 20e-12                  # the window around the clock edge that can cause metastability
T_SETUP, T_PCQ = 40e-12, 50e-12
F_DATA = 10e6                # the asynchronous input changes 10 million times a second
YEAR = 3.156e7               # seconds

def mtbf(t_resolve, f_clk):
    """Mean time between synchroniser failures, in seconds."""
    return math.exp(t_resolve / TAU) / (T0 * f_clk * F_DATA)

def show(seconds):
    if seconds < 60:
        return f"{seconds:.3g} s"
    if seconds < 3600:
        return f"{seconds / 60:.3g} minutes"
    if seconds < YEAR:
        return f"{seconds / 3600:.3g} hours"
    return f"{seconds / YEAR:.3g} years"

print("clock     no synchroniser    two flip-flops    three flip-flops")
for f_clk in (250e6, 500e6, 1e9, 2e9):
    stage = 1 / f_clk - T_SETUP - T_PCQ        # resolution time one extra flip-flop provides
    none = 200e-12                             # the flop feeds logic directly; 200 ps of slack is left
    print(f"{f_clk / 1e6:5.0f} MHz {show(mtbf(none, f_clk)):>15} {show(mtbf(stage, f_clk)):>17}"
          f" {show(mtbf(2 * stage, f_clk)):>19}")
print("\n(the universe is about 1.38e10 years old)")
Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

Without a synchroniser the design fails several times a second. With two flip-flops, at 500 MHz, the MTBF exceeds the age of the universe by many orders of magnitude, because trt_r appears in an exponent. But the margin shrinks quickly with frequency: at 2 GHz the resolution time is only 410 ps and two flip-flops fail every half-hour, so a third is needed. High-speed designs and FPGA tools (which report MTBF for synchronisers they recognise) make exactly this calculation.

Worked examplesH

Crossing clock domains

Large chips have many clocks: a processor core, a memory interface, a USB controller and a radio may each run from their own. Every signal that passes between two clock domains is a potential metastability and data-corruption bug, and the rules for crossing are among the most important in digital design:

  • A single control bit, held long enough for the receiving domain to see it: a two-flip-flop synchroniser in the receiving domain.
  • A short pulse: convert it to a level change (toggle a flip-flop in the sending domain), synchronise the level, and detect the change on the other side.
  • A multi-bit value: never synchronise the bits separately. They may resolve on different cycles, and the receiver may see a mixture of old and new bits that was never sent. Either send the value with a handshake (hold the data stable, synchronise a "valid" bit, and synchronise an acknowledgement back) or, for a counter, use Gray code, in which consecutive values differ in exactly one bit, so a sample caught mid-change is always either the old value or the new one.
  • A stream of data: an asynchronous FIFO.

An asynchronous FIFO is a small dual-port memory with a write pointer in the writer's domain and a read pointer in the reader's. Each side needs the other's pointer to know whether the FIFO is full or empty, so the pointers cross the boundary, in Gray code, through synchronisers. The synchronised pointer is a few cycles out of date, but always on the safe side: the writer may think the FIFO is fuller than it is, and the reader that it is emptier, so data is never overwritten or read twice. The check below is a compact eight-entry version, with a 100 MHz writer and a reader clocked at about 71 MHz with an unrelated phase; the testbench sends forty words, checks every one arrives in order, and checks that the write pointer's Gray code never changes more than one bit at a time.

verilog
// An asynchronous FIFO: data written with one clock is read with another, unrelated clock.
// Only Gray-coded pointers cross between the domains, each through a two-flip-flop synchroniser.
module async_fifo #(parameter int W = 8, A = 3) (      // 2**A = 8 entries
  input  logic         wclk, wrst_n, winc,
  input  logic [W-1:0] wdata,
  output logic         wfull,
  input  logic         rclk, rrst_n, rinc,
  output logic [W-1:0] rdata,
  output logic         rempty
);
  logic [W-1:0] mem [0:(1 << A) - 1];
  // Pointers have one extra bit, so "full" (writer a lap ahead) differs from "empty".
  logic [A:0] wbin, wgray, rbin, rgray;
  logic [A:0] rgray_w1, rgray_w2;           // read pointer, synchronised into the write domain
  logic [A:0] wgray_r1, wgray_r2;           // write pointer, synchronised into the read domain
  logic [A:0] wbin_next, wgray_next, rbin_next, rgray_next;
  logic [A-1:0] waddr, raddr;

  assign waddr      = wbin[A-1:0];
  assign raddr      = rbin[A-1:0];
  assign wbin_next  = wbin + (winc & ~wfull);
  assign wgray_next = (wbin_next >> 1) ^ wbin_next;          // binary to Gray code
  assign rbin_next  = rbin + (rinc & ~rempty);
  assign rgray_next = (rbin_next >> 1) ^ rbin_next;
  assign rdata      = mem[raddr];

  // Write domain. Full: the write pointer is exactly one lap ahead of the (synchronised) read
  // pointer, which in Gray code means the top two bits differ and the rest are equal.
  always_ff @(posedge wclk or negedge wrst_n)
    if (!wrst_n) begin
      wbin <= '0; wgray <= '0; wfull <= 1'b0; rgray_w1 <= '0; rgray_w2 <= '0;
    end else begin
      if (winc && !wfull) mem[waddr] <= wdata;
      wbin  <= wbin_next;
      wgray <= wgray_next;
      wfull <= (wgray_next == (rgray_w2 ^ {2'b11, {(A - 1){1'b0}}}));
      {rgray_w2, rgray_w1} <= {rgray_w1, rgray};               // two-flip-flop synchroniser
    end

  // Read domain. Empty: the read pointer has caught up with the (synchronised) write pointer.
  always_ff @(posedge rclk or negedge rrst_n)
    if (!rrst_n) begin
      rbin <= '0; rgray <= '0; rempty <= 1'b1; wgray_r1 <= '0; wgray_r2 <= '0;
    end else begin
      rbin   <= rbin_next;
      rgray  <= rgray_next;
      rempty <= (rgray_next == wgray_r2);
      {wgray_r2, wgray_r1} <= {wgray_r1, wgray};               // two-flip-flop synchroniser
    end
endmodule

module tb;
  localparam int N = 40;
  logic wclk = 0, rclk = 0, wrst_n = 0, rrst_n = 0, winc = 0, rinc = 0;
  logic [7:0] wdata = 0;
  wire  [7:0] rdata;
  wire  wfull, rempty;
  async_fifo dut (.*);
  always #5 wclk = ~wclk;                   // 100 MHz writer
  always #7 rclk = ~rclk;                   // about 71 MHz reader, unrelated phase

  function automatic logic [7:0] word(input int i);
    return 8'(i * 37 + 11);
  endfunction

  function automatic int bits_set(input logic [3:0] v);
    int n = 0;
    for (int i = 0; i < 4; i++) n += v[i];
    return n;
  endfunction

  int sent = 0, got = 0, full_cycles = 0;
  logic [3:0] last_wgray = 0;

  always @(posedge wclk) if (wrst_n) begin   // writer: offer word number `sent` until accepted
    if (winc && !wfull) sent++;
    if (winc && wfull)  full_cycles++;
    winc  <= (sent < N);
    wdata <= word(sent);
  end

  always @(posedge rclk) if (rrst_n) begin   // reader: always ready, checks order and values
    if (rinc && !rempty) begin
      if (rdata !== word(got)) $fatal(1, "word %0d: got %h, expected %h", got, rdata, word(got));
      got++;
    end
    rinc <= 1;
  end

  always @(negedge wclk)                     // the Gray pointer must change at most one bit at a time
    if (wrst_n) begin
      if (bits_set(dut.wgray ^ last_wgray) > 1) $fatal(1, "Gray pointer jumped by more than one bit");
      last_wgray = dut.wgray;
    end

  initial begin
    #23 wrst_n = 1; rrst_n = 1;
    wait (got == N);
    $display("%0d words crossed from the 100 MHz domain to the 71 MHz domain, in order", got);
    $display("the writer was held off by 'full' for %0d of its cycles", full_cycles);
    $display("write pointer after the run: binary %b, Gray %b", dut.wbin, dut.wgray);
    $finish(0);
  end
  initial begin #100000 $fatal(1, "timeout"); end
endmodule

It prints:

text
40 words crossed from the 100 MHz domain to the 71 MHz domain, in order
the writer was held off by 'full' for 14 of its cycles
write pointer after the run: binary 1000, Gray 1100

Because the writer is faster, the FIFO fills and the writer is held off by full for some of its cycles; nothing is lost. A simulation like this one cannot show metastability (simulators have no notion of a half-resolved flip-flop), so it verifies the logic of the crossing, not its electrical safety. That is why industry adds dedicated CDC verification tools, which find every signal crossing between domains in a design and check that each passes through an approved structure.

ExplanationH

Static timing analysis in practice

The Python program above was a toy static timing analyser: "static" because it computes worst cases from the circuit's structure, without simulating any input patterns. Industrial tools do the same on designs with billions of timing arcs, adding:

  • Constraints in the Synopsys Design Constraints (SDC) format, the industry's common language, which declare the clocks and the timing of the outside world:
text
create_clock -name core_clk -period 1.000 [get_ports clk]        ;# 1 GHz
set_clock_uncertainty 0.030 [get_clocks core_clk]                  ;# skew + jitter budget, ns
set_input_delay  0.300 -clock core_clk [get_ports data_in*]
set_output_delay 0.250 -clock core_clk [get_ports data_out*]
set_false_path -from [get_clocks usb_clk] -to [get_clocks core_clk]  ;# handled by a synchroniser
  • Exceptions: false paths (connections that exist but are never exercised in a timing-relevant way, such as clock-domain crossings handled by synchronisers) and multicycle paths (logic deliberately given two or more cycles).
  • Corners: delays depend on manufacturing variation (process), supply voltage and temperature, so timing is checked at several PVT corners: setup at the slow corner, hold at the fast one. Modern flows also model on-chip variation, the fact that two paths on the same die differ, statistically.
  • Wires: at advanced nodes, wire resistance and capacitance contribute as much delay as gates, so after layout the tools extract parasitics and recompute.

Commercial signoff tools include Synopsys PrimeTime and Cadence Tempus; OpenSTA is the open-source analyser inside the OpenROAD flow. FPGA vendors' tools run their own timing analysis after place and route and report worst negative slack (WNS) and total negative slack (TNS): a design is done when both are zero. Closing timing on a large chip is a project phase of its own, covered in Chip design.

Common misconceptionsH

Common misconceptions

Misconception

"If the design fails, just lower the clock frequency." That fixes setup violations only. Hold violations involve no clock period and fail at every frequency, including 1 Hz.

Misconception

"A two-flip-flop synchroniser eliminates metastability." It makes failures exponentially rare, not impossible, and its MTBF depends steeply on the clock frequency. A synchroniser that is ample at 200 MHz may be inadequate at 2 GHz.

Misconception

"Each bit of a bus can have its own synchroniser." The bits may resolve on different cycles and produce a value that was never sent. Use Gray code, a handshake or an asynchronous FIFO.

Misconception

"Pipelining makes the computation faster." It raises throughput; the latency of each result grows, because every stage adds register overhead. A pipeline also needs every value that travels alongside to be delayed equally.

Misconception

"Clock skew is always bad." It hurts one of setup or hold and helps the other. Tools exploit "useful skew" deliberately to balance unequal pipeline stages.

Exercises

Exercises

1. Timing and clocking

Warm-upHuman

A register-to-register path has tpcq=60t_{pcq} = 60 ps, combinational logic with tpd=640t_{pd} = 640 ps, tsetup=50t_{setup} = 50 ps, and the clock has 50 ps of uncertainty (skew plus jitter). What is the maximum clock frequency, in MHz?

2. Timing and clocking

CoreHuman

Two flip-flops are connected through a single small gate: tccq=35t_{ccq} = 35 ps, the gate's tcd=10t_{cd} = 10 ps, and thold=30t_{hold} = 30 ps. The capturing flip-flop's clock arrives 25 ps later than the launching one's. What is the hold slack, in picoseconds? (Negative means a violation.)

3. Timing and clocking

CoreHuman

Static timing analysis reports a hold violation of 15 ps on a path between two flip-flops on the same clock. Which change fixes it?

Choose one

4. Timing and clocking

CoreHuman

A block of logic has a delay of 3.0 ns. Each pipeline register adds 100 ps of overhead (clock-to-Q, setup and clock uncertainty), and the logic can be split into equal stages. What is the smallest number of stages that lets the design run at 2 GHz?

5. Timing and clocking

CoreComputational

Write the heart of a static timing analyser. gates maps each gate's name to (inputs, delay), listed in topological order (every input is either a primary input or an earlier gate); start gives the arrival times of the primary inputs (for example, register outputs at tpcqt_{pcq}). Return a dictionary of the latest arrival time of every signal, inputs included.

Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

6. Timing and clocking

StretchComputational

Write flops_needed(f_clk, target_years, ...), returning the smallest number n≥2n \ge 2 of flip-flops in a synchroniser chain whose MTBF is at least target_years. Use the lesson's model: each flip-flop after the first adds 1/fclk−overhead1/f_{\text{clk}} - \text{overhead} seconds of resolution time, so tr=(n−1)(1/fclk−overhead)t_r = (n - 1)(1/f_{\text{clk}} - \text{overhead}), and MTBF=etr/τ/(T0fclkfdata)\text{MTBF} = e^{t_r/\tau} / (T_0 f_{\text{clk}} f_{\text{data}}) seconds, with a year of 3.156×1073.156 \times 10^7 s.

Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

7. Timing and clocking

CoreHuman

A 4-bit counter in a 125 MHz domain must be read by logic in an unrelated 200 MHz domain. Which crossing is safe?

Choose one

Further reading and resources

  • OpenSTAAn open-source static timing analyser, used in the OpenROAD chip-design flow.
  • The OpenROAD projectAn open-source flow from RTL to chip layout, including timing-driven placement and signoff-style timing analysis.
  • Metastability and synchronizers: a tutorial · Ran Ginosar · 2011A widely read tutorial (IEEE Design & Test of Computers) on metastability, MTBF and synchroniser design.
  • Icarus VerilogThe simulator that runs this lesson's checks.