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.
- 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
- call edge k: clk → launch flop
- 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
- data new value: launch flop → long path: 21 levels
- data new value: long path: 21 levels → capture flop
- 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
- call edge k+1: clk → capture flop
- 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
- data new value: launch flop → short path: 1 level
- 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.
| Term | What it is | On this flow |
|---|---|---|
| Clock-to-Q | time from the edge until the flop output changes | not reported: no cell library |
| Setup time | D stable before the edge | not reported |
| Hold time | D stable after the edge | not reported |
| Clock period | time between rising edges; a constraint you give the tools | not used: there is no constraint |
| Critical path | the path with the largest delay | its cell count is the levels number |
| Levels of logic | cells on the longest path between flops or ports | reported 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.
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.
| Design | Cells | Flops | Levels of logic on this flow |
|---|---|---|---|
an 8-bit adder alone, a + b with a 9-bit sum | 38 | 0 | 15 |
| a 9-bit adder alone, 10-bit sum | 43 | 0 | 17 |
| add4, one register at the end (the lab) | 125 | 10 | 21 |
| add4, a register after each level of adders | 146 | 28 | 17 |
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.
| Port | Direction | Width | Meaning |
|---|---|---|---|
| clk | input | 1 | clock |
| rst_n | input | 1 | reset, asynchronous, active-low |
| a, b, c, d | input | [7:0] | the four operands |
| sum | output | [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
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.
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.
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.
Synthesize the design to measure its cells and inspect the mapped gates.
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 wrongOriginal 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
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.