Clock period, setup and hold in one picture

See why the longest path between two flops sets the clock period, why a short path can fail at any period, and what the levels-of-logic number on this flow does and does not tell you.

Lesson
R4.1
Time
25 min
Runs on
Your browser
Check
5 questions

A filter block passed every simulation at every clock period the team tried, because RTL simulation has no gate delays. In the chip, one path through a long chain of adders arrived after the next clock edge. The receiving flop kept a stale value now and then. Synthesis had counted the levels on that path, and nobody read the number.

One clock period, two flops, a long path and a short oneArrows carry data from the launch flop to the capture flop, and the clock to both. The long path is the adder's carry chain; the short path is any path with one cell.
One clock period, two flops, a long path and a short oneArrows carry data from the launch flop to the capture flop, and the clock to both. The long path is the adder's carry chain; the short path is any path with one cell. clock clk edge k, then edge k+1one period later flop launch flop Q changes clock-to-Qafter edge k logic long path: 21levels input a[0] throughthe carry chain logic short path: 1level settles soon afterany edge flop capture flop D stable from setupbefore edge k+1 tohold after it edge kQDQDedge k+1
  1. Edge k launches

    The launch flop takes its input at edge k. Its output changes a little later: the clock-to-Q delay.

    Follow the calls, values and waits
    1. call edge k: clk → launch flop
  2. The long path

    The new value ripples through 21 cells. Each cell adds its delay, so the arrival time grows with the levels.

    Follow the calls, values and waits
    1. data new value: launch flop → long path: 21 levels
    2. data new value: long path: 21 levels → capture flop
  3. Edge k+1 captures: setup

    D must be stable a setup time before edge k+1. So the period must cover clock-to-Q, the path delay and the setup time. A slower clock fixes a setup failure.

    Follow the calls, values and waits
    1. call edge k+1: clk → capture flop
  4. The short path: hold

    Edge k+1 also launches the next value. Through one cell it reaches the capture flop quickly. D must stay put for a hold time after the edge, and no clock period changes this race.

    Follow the calls, values and waits
    1. data new value: launch flop → short path: 1 level
    2. data new value: short path: 1 level → capture flop
Read the diagram as text
  • clk: edge k, then edge k+1 one period later
  • launch flop: Q changes clock-to-Q after edge k
  • long path: 21 levels: input a[0] through the carry chain
  • short path: 1 level: settles soon after any edge
  • capture flop: D stable from setup before edge k+1 to hold after it
  • clk → launch flop: edge k
  • launch flop → long path: 21 levels: Q
  • long path: 21 levels → capture flop: D
  • launch flop → short path: 1 level: Q
  • short path: 1 level → capture flop: D
  • clk → capture flop: edge k+1

Setup is checked against the next edge, so the longest path sets the period. Hold is checked against the same edge, so the shortest path can fail at any period.

What the clock period has to cover

A flop takes its input at the clock edge. In silicon it needs that input stable for a short time before the edge, the setup time, and a short time after it, the hold time. If D changes inside that window, the flop may store the wrong value or hover between 0 and 1 for a while.

Between two flops sits a path of combinational logic. The value leaves the first flop at one edge and must reach the second before the next edge, minus the setup time. So the period must be at least clock-to-Q plus the path delay plus setup, for every path. The slowest path is the critical path, and it sets the highest clock frequency.

Hold works the other way. It compares the fastest path against the same edge, and the period does not appear in the check. A hold failure stays at any clock speed. The layout flow fixes it by adding delay cells to short paths.

TermWhat it isOn this flow
Clock-to-Qtime from the edge until the flop output changesnot reported: no cell library
Setup timeD stable before the edgenot reported
Hold timeD stable after the edgenot reported
Clock periodtime between rising edges; a constraint you give the toolsnot used: there is no constraint
Critical paththe path with the largest delayits cell count is the levels number
Levels of logiccells on the longest path between flops or portsreported by Synthesize

A static timing analyser (STA) reports the first five, given a cell library, a placed design and a clock constraint.

RTL simulation cannot see any of this. Its flops change at the edge with no delay, and its gates have none either. A design that passes at one period passes at every period.

What the levels number counts

Synthesize maps the design to generic gates and then runs ltp -noff. It finds the longest chain of cells from a flop output or input port to a flop input or output port. The answer is a count of cells, which this site calls levels of logic on this flow. For the lab design, Yosys 0.70 prints the path below. The excerpt cuts the middle rows and the routing notes.

The longest path of the one-stage adder: from input bit a[0] to the flop that holds sum[9].
Longest topological path in add4 (length=21):
    0: \a [0]
    1: $abc$336$new_n116
    ...
   21: $auto$maccmap.cc:240:synth$8.CO [8]
   ff: \sum [9]

The count has no delays in it, no wires and no technology. A NAND and an XOR count the same. It cannot give a frequency, find a hold problem or see a long wire. What it can do is rank paths: more cells in series usually means more delay, on any technology.

DesignCellsFlopsLevels of logic on this flow
an 8-bit adder alone, a + b with a 9-bit sum38015
a 9-bit adder alone, 10-bit sum43017
add4, one register at the end (the lab)1251021
add4, a register after each level of adders1462817

Measured with Yosys 0.70 on this flow. The one-stage tree is 21 levels, fewer than 15 + 17, because the synthesizer overlaps the two carry chains.

Cut the long path and read the number again

Two levels of adders sit between the inputs and the one register. Put a register between them and each stage has one adder. The longest path then starts at a new flop, s01_q[1], and has 17 levels.

One stage

assign s01 = a + b;
assign s23 = c + d;
always_ff @(posedge clk ...)
  sum <= s01 + s23;

125 cells, 10 flops, 21 levels. The sum appears one edge after the inputs.

Two stages

always_ff @(posedge clk ...) begin
  s01_q <= a + b;
  s23_q <= c + d;
  sum   <= s01_q + s23_q;
end

146 cells, 28 flops, 17 levels. The sum appears two edges after the inputs.

Read the trade in the numbers. The path lost 4 levels. The design gained 18 flops and one edge of delay, called latency. The new longest stage is the 9-bit adder, so a third register in front of the first adders would not lower the count. R4.3 shows the method in full.

Design brief
PortDirectionWidthMeaning
clkinput1clock
rst_ninput1reset, asynchronous, active-low
a, b, c, dinput[7:0]the four operands
sumoutput[9:0]registered; sum = a + b + c + d, never truncated: 1020 at most
Latency
one edge as shipped; two after the cut. The bench holds each input set for three edges, so both pass
Parameters
Measure: cells, flops and levels of logic on this flow, before and after the cut
Corner cases
every input at 255

The lab ships the one-stage tree. Run it: 24 sums checked, 0 wrong. Synthesize: 21 levels. Now register a + b and c + d as s01_q and s23_q, and add those to make sum. Run again: still 0 wrong. Synthesize: 17 levels and 28 flops.

Asked in interviews
  1. What limits the clock frequency of a design?

    The slowest register-to-register path. The period must cover the launch flop's clock-to-Q delay, the logic delay on the path and the capture flop's setup time. That path is the critical path.

  2. What is the difference between setup and hold?

    Setup is how long D must be stable before the edge, so it fails when a path is too slow; a slower clock fixes it. Hold is how long D must stay after the edge, so it fails when a path is too fast, at any clock period.

  3. Can RTL simulation find a timing violation?

    No. RTL simulation has no cell or wire delays, so every path finishes in zero time. Static timing analysis on the synthesized and placed design finds them. Gate-level simulation with delays can show some, but STA is the check.

Try it

Each input set is held for three edges and checked at the third. Four sets are printed and 20 random ones are only counted. Every sum is right, including the largest, 1020.

Synthesize reports 125 cells, 10 flops and 21 levels of logic. After the cut it reports 146 cells, 28 flops and 17 levels, and the bench still prints 0 wrong. The function did not change; only the latency and the longest path did.

RTLIcarus Verilog · in your browser · Lint: Verilator service · Synthesize: Yosys service
Edit the design, then Run, Lint or Synthesize
Expected: e4 10+20+30+40 = 100 (want 100) e7 255+255+255+255 = 1020 (want 1020) e10 200+100+50+25 = 375 (want 375) e13 1+2+3+4 = 10 (want 10) 24 sums checked, 0 wrong

Original design · 125 cells · 10 flip-flops · 0 latches · 21 logic levels. Synthesis uses Yosys; counts depend on the toolchain.

Read included file: tb.sv
`timescale 1ns/1ps
module tb;
  logic clk = 0;
  always #5 clk = ~clk;                   // rising edges at 5, 15, 25, ...
  logic rst_n = 1;                        // falls at 1 ns: a real edge, so an asynchronous reset acts before any clock
  int edge_n = -1;
  always @(posedge clk) edge_n = edge_n + 1;
  logic [7:0] a = 0, b = 0, c = 0, d = 0;
  logic [9:0] sum;
  int checked = 0, wrong = 0;
  bit quiet = 0;
  // Apply one set of inputs, hold it for three edges, then compare sum with the model.
  task automatic item(input int w, input int x, input int y, input int z);
    int want;
    a = w; b = x; c = y; d = z;
    want = w + x + y + z;
    repeat (3) tick();
    checked++;
    if (sum != want) wrong++;
    if (!quiet) $display("e%0d %0d+%0d+%0d+%0d = %0d (want %0d)", edge_n, a, b, c, d, sum, want);
  endtask
  add4 dut (.clk(clk), .rst_n(rst_n), .a(a), .b(b), .c(c), .d(d), .sum(sum));
  // Advance to the next rising edge, wait for the design to settle, then print one line.
  task automatic tick();
    @(posedge clk); #1;
    // the lines are printed by item()
  endtask
  initial begin
    #1 rst_n = 0; tick(); tick(); rst_n = 1;   // asserted at 1 ns, edges 0 and 1 in reset, released 1 ns after edge 1
    item(10, 20, 30, 40);
    item(255, 255, 255, 255);             // the largest sum: 1020 needs 10 bits
    item(200, 100, 50, 25);
    item(1, 2, 3, 4);
    quiet = 1;                            // 20 more, random, counted but not printed
    repeat (20) item($urandom_range(255), $urandom_range(255), $urandom_range(255), $urandom_range(255));
    $display("%0d sums checked, %0d wrong", checked, wrong);
    $finish(0);                             // quiet: no "$finish called" line
  end
endmodule

Check yourself

Predict the outputThis adder tree has a register after each level of adders and no reset. The bench applies one input set before the first edge and another before the second, then prints. What is the output line?
module add4p (input logic clk, input logic [7:0] a, b, c, d, output logic [9:0] sum);
  logic [8:0] s01_q, s23_q;
  always_ff @(posedge clk) begin
    s01_q <= a + b;               // stage 1: two pair sums
    s23_q <= c + d;
    sum   <= s01_q + s23_q;       // stage 2: the final sum
  end
endmodule
module tb;
  logic clk = 0;
  logic [7:0] a, b, c, d;
  logic [9:0] sum;
  add4p dut (.clk(clk), .a(a), .b(b), .c(c), .d(d), .sum(sum));
  initial begin
    a = 10; b = 20; c = 30; d = 40;
    #1 clk = 1; #1 clk = 0;       // first edge
    a = 1; b = 1; c = 1; d = 1;
    #1 clk = 1; #1 clk = 0;       // second edge
    $display("sum=%0d", sum);
    $finish(0);
  end
endmodule
Spot the bugEach option is the body of a combinational four-input adder with a 10-bit sum. The bench checks 255 four times (1020) and 200 + 100 + 50 + 25 (375). Which body fails?
ConceptA design fails setup on one path at 400 MHz and hold on another path. The team lowers the clock to 200 MHz. What happens?
ConceptSynthesize reports 21 levels of logic for a design. What does that number tell you?
ConceptYour RTL passes every test with the bench clock at 1 ns and at 100 ns. What does that say about the chip's top clock frequency?

Remember

  • A flop needs D stable for a setup time before the edge and a hold time after it.
  • The period must cover clock-to-Q, the path delay and setup for every path; the slowest path, the critical path, sets the frequency.
  • Hold compares a fast path with the same edge, so a slower clock never fixes it.
  • Levels of logic on this flow are a cell count on the longest path (ltp -noff): no delays, no wires, no frequency, but a fair ranking of paths.
  • A pipeline register cuts the longest path: the four-input adder goes from 21 levels and 10 flops to 17 levels and 28 flops, and its latency grows by one edge.