Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 37 ยท Synthesis and Verification

Timing, Metastability and Clock Domain Crossing

Understand setup and hold time, critical paths and maximum clock frequency, metastability, two-flop synchronizers, Gray-coded pointers and handshakes for clock domain crossing.

A design that is logically correct can still fail on real silicon because signals need time to travel through gates and wires. Digital timing is the discipline of proving that every signal arrives before the clock edge that must capture it, and that signals crossing between unrelated clocks do not corrupt data. Simulation with zero-delay RTL cannot see these problems, so you must understand them yourself.

This lesson covers setup and hold time, the critical path and maximum clock frequency, metastability, and the standard techniques for clock domain crossing (CDC). The Verilog is read-only (no simulator or timing tool runs on this site); the C programs are labelled stand-ins that compute timing budgets and model the corruption risk.

Setup and hold

A flip-flop samples its input at the clock edge. To do so reliably, the input must be stable for:

  • setup time (t_su): a window before the edge,
  • hold time (t_h): a window after the edge.

If the data changes inside that window, the flip-flop may capture the wrong value or enter metastability (below).

flowchart LR
  A["Launch flop<br/>clock to Q"] --> B["Combinational logic<br/>gate and wire delay"]
  B --> C["Capture flop<br/>setup time"]

The path from one flip-flop to the next must fit within a clock period.

The timing equation

For a path from launching flop to capturing flop, with the same clock:

TEXT
T_clk >= t_clk_to_q + t_logic + t_setup + t_skew_margin
  • t_clk_to_q: delay from the launching clock edge to the new Q value,
  • t_logic: the sum of gate and routing delays along the longest path (the critical path),
  • t_setup: the capturing flop's setup time,
  • skew and uncertainty are added as margin.

The maximum clock frequency is 1 / T_clk_min. Setup violations are cured by slowing the clock, shortening the logic (restructure, pipeline, retime) or using faster cells. Hold violations are independent of clock speed: they occur when the new data races through a very short path and arrives too soon after the edge. They are fixed by inserting delay (buffers) and are normally handled automatically by the place-and-route tools.

The first stand-in computes the critical path of a few candidate paths and the resulting maximum frequency:

C Stand-in: critical path and maximum clock frequency

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The multiplier path sets the limit at about 6.8 ns (147 MHz); every other path would run faster, but the slowest path decides the clock for the whole clock domain. To go faster you split the multiplier across two stages.

Pipelining

Insert registers in the middle of a long combinational path: each stage now has half the logic, so the clock can nearly double, at the cost of one extra cycle of latency and extra flip-flops. Throughput (results per second) goes up; latency (time for one result) stays similar or grows:

VERILOG
// stage 1 and stage 2 of a pipelined multiply-add
always @(posedge clk) begin
  prod_r <= a * b;          // stage 1: multiplier only
  sum_r  <= prod_r + c_r;   // stage 2: adder only (c_r is c delayed to match)
end

Remember to delay side inputs by the same number of stages, or the data of different operations will be combined.

Metastability

If a signal changes inside the setup/hold window, the flip-flop's internal node can settle at an in-between voltage for an unpredictable time before resolving to 0 or 1. This is metastability. It cannot be avoided for asynchronous signals (a button press, a signal from another clock) because they can change at any moment relative to your clock. It can only be made very unlikely to cause failures.

The standard cure is a synchronizer: two (or more) flip-flops in series clocked by the destination clock. The first may go metastable but has a full clock period to resolve before the second samples it:

VERILOG
module sync2 (
  input  clk, rst_n, async_in,
  output sync_out
);
  reg meta, stable;
  always @(posedge clk or negedge rst_n)
    if (!rst_n) begin meta <= 1'b0; stable <= 1'b0; end
    else        begin meta <= async_in; stable <= meta; end
  assign sync_out = stable;
endmodule

Mean time between failures grows exponentially with the extra resolution time, so two flops are enough for most designs and three for very fast ones.

Crossing between clock domains

Signals between two clocks (clk_a and clk_b) need a protocol, not just a wire:

  1. Single-bit level or slow signal: two-flop synchronizer. The signal must stay stable for at least two destination clock periods, otherwise it can be missed.
  2. Single-bit pulse: convert to a toggle in the source domain, synchronize the toggle, and detect the edge in the destination domain.
  3. Multi-bit data: never run each bit through its own synchronizer. Bits may be captured in different cycles, producing a value that never existed. Use a handshake (request/acknowledge, data held stable until acknowledged), a FIFO with Gray-coded pointers, or a multi-cycle path protocol.

The reason multi-bit buses fail is easy to show. A binary counter going from 3 (011) to 4 (100) changes all three bits at the same moment. If the destination samples while the bits are changing, it can capture any mixture: 000, 111, 110... Gray code changes exactly one bit per step, so a mid-transition sample is always the old or the new value. The next program tries every possible capture mixture for each transition:

C Stand-in: why Gray code is safe to sample across clocks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Gray code never produces a bogus value, while binary transitions such as 3 to 4 and 7 to 0 allow six wrong values each. That is why asynchronous FIFOs pass Gray-coded read and write pointers across the domain boundary (see the FIFO project).

A handshake in one picture

sequenceDiagram
  participant A as Domain A (source)
  participant B as Domain B (destination)
  A->>A: put data on bus, hold stable
  A->>B: req = 1 (through 2-flop synchronizer)
  B->>B: capture data
  B->>A: ack = 1 (through 2-flop synchronizer)
  A->>A: release data, req = 0
  B->>A: ack = 0

Four-phase handshake: data never changes while the other side may sample it.

It is slow (several cycles per transfer) but robust for control words and configuration.

Reset synchronization

Asynchronous reset assertion is safe, but its release must be aligned to each clock domain:

VERILOG
always @(posedge clk or negedge rst_async_n)
  if (!rst_async_n) {rst_n, rst_meta} <= 2'b00;
  else              {rst_n, rst_meta} <= {rst_meta, 1'b1};

Here rst_n asserts immediately and releases two clocks after the external reset ends, so all flip-flops leave reset in the same cycle.

Tools and constraints

Real timing closure is checked by static timing analysis (STA) using constraints written in SDC format (create_clock, set_input_delay, set_false_path, set_clock_groups). CDC checking tools (Questa CDC, SpyGlass, Vivado's report) flag unsynchronized crossings. Never skip them: bugs here appear only on some boards or temperatures.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What limits the maximum clock frequency of a synchronous design?

    Every path must fit within one clock period, so the slowest path determines the clock.
  2. 2. What does a two-flop synchronizer do?

    Metastability cannot be avoided for asynchronous inputs, but extra resolution time makes failures extremely rare.
  3. 3. Why must multi-bit buses not be synchronized bit by bit?

    Each bit resolves independently, so a transition of several bits can be captured as an inconsistent mixture.
  4. 4. Why are Gray-coded pointers used in asynchronous FIFOs?

    Gray code limits each step to a single bit change, which makes sampling mid-transition harmless.

Summary

  • A path must satisfy T_clk >= clk-to-Q + logic + setup; the critical path sets fmax. Pipelining trades latency for frequency.
  • Asynchronous or cross-domain signals can go metastable; use a two-flop synchronizer.
  • Never synchronize a multi-bit bus bit by bit: use handshakes, Gray-coded pointers in FIFOs, or hold the data stable.
  • Synchronize reset release in each domain, and verify with STA and CDC tools.

Next: Assertions and Functional Coverage