Planning Clock Domains

The timer raises its alarm and the processor never hears it. Both blocks work. The fault lies between them, where one clock hands a signal to another.

You Will Be Able To

  • Sort the flip-flops of a chip into clock domains, and say which pairs of clocks are related.
  • Explain what a two-flop synchronizer carries across, and three things it can lose.
  • Choose a crossing for a level, an event, a word and a stream.
Lesson
S5.3
Time
26 min
Runs on
Your browser
Check
6 questions

The timer engineer shows you a clean pulse on the alarm wire. The processor engineer shows you three quiet samples of the same wire. Neither trace is wrong. How can both be true?

Before You Start

You need two earlier pages. Clock Sources and the Chip-Level Clock Tree gives an oscillator and the period of a clock. Dividers and Clock Gates gives a divided clock.

This page plans crossings. It does not build them. The circuits are in the track on RTL design. RTL is short for register transfer level, the usual way to write hardware. You need its first two pages on crossings: a flip-flop that cannot decide and two flip-flops in a row. The rest of that module is linked where it is used.

The alarm on this page is a service request of a peripheral. The Anatomy of a Peripheral says what that is.

Five questions this page answers
Question
1What is a clock domain?
2When are two clocks related?
3What can a two-flop synchronizer lose?
4Why must the bits of a word not be synchronized one by one?
5Which crossing fits a level, an event, a word and a stream?

The Idea: Who Shares an Edge, and What Must Survive

A chip has more than one clock. The processor runs on a fast one. Slow peripherals run on a divided one. A real-time clock keeps the time of day on a small oscillator of its own, also while the rest sleeps.

All flip-flops that use one clock form a clock domain. Inside a domain life is simple. Every signal has one period to settle, and a tool can check that it does.

Between two domains it depends on the clocks. If one clock is made from the other, their edges keep a fixed distance, and signals between them can still be checked. If the clocks come from two oscillators, the edges drift past each other. A signal from the other side can then change at any moment, also right on the edge that samples it.

A flip-flop whose input changes on its edge can hover between 0 and 1 for a while. That is metastability. The standard cure is a synchronizer: two flip-flops in a row on the receiving clock. The first may hover. The second looks one period later, when the first has almost surely settled.

Here is the part that plans get wrong. A synchronizer makes a signal safe. It does not make it complete. It takes samples, and whatever happens between two samples is gone. So for every crossing you must say what has to survive. It may be a level, a single event, a whole word, or every word of a stream. That answer picks the circuit.

New Words

New words on this page
WordMeaning
Clock domainAll the flip-flops that use one clock.
Related clocksTwo clocks whose edges keep a fixed, known distance, because one source makes both. A clock and its divided clock are related.
Unrelated clocksTwo clocks from different oscillators. Their edges drift past each other. Often called asynchronous clocks.
Clock domain crossing (CDC)A signal that leaves a flip-flop of one clock domain and enters a flip-flop of another.
MetastabilityThe state of a flip-flop that hovers between 0 and 1 because its input changed on its clock edge.
Two-flop synchronizerTwo flip-flops in a row on the receiving clock. The first takes the signal in. The second gives the first a period to settle.
Level signalA signal that stays at its value until the receiver has acted on it.
Event signalA short pulse that means "this happened once".
Real-time clock (RTC)A block that keeps the time of day. It runs on a slow oscillator of its own.
Domain mapA drawing of the blocks of a chip, each marked with its clock. Every signal between two clocks is marked as a crossing.
The three clocks of this page
ClockPeriodMade byRelated to the core clock?
Core clock10 nsThe clock rootIt is the core clock.
Peripheral clock20 nsA divider by two on the core clockYes: every edge follows a core edge.
RTC clock14 nsThe oscillator of the real-time clock blockNo: another oscillator.

Lab choices. A real RTC oscillator is far slower. The numbers are small so that a table can hold them.

One Picture: The Domain Map

A Domain Map of Four BlocksStep through the picture. Each box is named with the clock that its flip-flops use.
  • Power
  • Clock / reset
  • Processor
  • Peripheral
A Domain Map of Four BlocksStep through the picture. Each box is named with the clock that its flip-flops use. component Real-time clockblock RTC clock, 14 ns,from its ownoscillator component Synchronizer two flip-flops on thecore clock component Processor andbus core clock, 10 ns component Peripheralregisters peripheral clock, 20ns: the core clockdivided by two alarm: unrelatedclocksone domainregister access:related clocks
  1. component Real-time clock blockRTC clock, 14 ns, from its own oscillator
    • alarm: unrelated clocks → Synchronizer
  2. component Processor and buscore clock, 10 ns
    • register access: related clocks → Peripheral registers
  3. component Peripheral registersperipheral clock, 20 ns: the core clock divided by two
  4. component Synchronizertwo flip-flops on the core clock
    • one domain → Processor and bus
  1. Name the clock of every box

    Four blocks, three clocks. The processor and the bus share the core clock. The peripheral registers use that clock divided by two. The real-time clock block has an oscillator of its own.

  2. A related pair

    The peripheral clock is made from the core clock. Every edge of it comes a fixed time after a core edge. A signal between these two boxes can be timed like any other.

  3. An unrelated pair

    The RTC clock comes from another oscillator. Its edges drift past the core edges. The alarm can change at any moment of a core period.

  4. The synchronizer

    So the alarm does not go straight to the processor. It first passes two flip-flops in a row. Both use the core clock: the synchronizer belongs to the domain that receives.

  5. The plan

    Every arrow between two boxes gets a label: related or unrelated. Every unrelated arrow gets a second label: what must survive the crossing. That list is the clock plan.

Read the diagram as text
  • Real-time clock block: RTC clock, 14 ns, from its own oscillator
  • Synchronizer: two flip-flops on the core clock
  • Processor and bus: core clock, 10 ns
  • Peripheral registers: peripheral clock, 20 ns: the core clock divided by two
  • Real-time clock block → Synchronizer: alarm: unrelated clocks
  • Synchronizer → Processor and bus: one domain
  • Processor and bus → Peripheral registers: register access: related clocks

A clock plan for four blocks of ap_soc. The three periods are choices of our lab chip, and the next page uses the same three.

The Smallest Example, Worked by Hand

The core clock rises at 5, 15, 25, 35 and 45 ns. At 22 ns the real-time clock block raises two wires. The alarm is a level: it stays at 1. The tick is an event: it falls again after 2 ns, at 24 ns.

A level and a pulse through two synchronizers, by hand
Core edgeAlarm, first flip-flopAlarm, second flip-flopTick, first flip-flopTick, second flip-flop
15 ns0000
25 ns1: the alarm is high00: the tick fell at 240
35 ns11: the core sees the alarm00
45 ns1100

The level arrives, two edges after it was raised. The pulse began at 22 and ended at 24, and no core edge fell in that time. No flip-flop ever saw it. A second flip-flop cannot bring back what the first one never sampled.

A pulse is safe only if it is wider than one period of the receiving clock. A common rule of thumb asks for one and a half periods.

Now a word of two bits. It changes from 01 to 10. Its two wires differ in length, so bit 1 changes at 64 ns and bit 0 at 66 ns. A core edge falls at 65 ns, between the two.

One word, two wires, by hand
Core edgeBit 1 on its wireBit 0 on its wireWhat the first flip-flops take
55 ns0101: the old word
65 ns1: it changed at 641: it changes at 66, too late11: a word nobody sent
75 ns1010: the new word

For one period the receiver holds 11. The sender never sent it. Each bit crossed safely, and the word did not.

The Rules

  1. Rule 1: Flip-flops that use one clock form one clock domain.

    A synchronizer belongs to the domain of its own clock: the receiving one.

  2. Rule 2: Two clocks are related only if one source makes both.

    Two oscillators with the same number on them are unrelated. Their edges drift.

  3. Rule 3: Every signal between unrelated clocks enters through a synchronizer.

    Nothing in the receiving domain may use the first flip-flop.

  4. Rule 4: A synchronizer carries a level that lasts longer than one receiving period.

    A shorter pulse can fall between two edges and be lost.

  5. Rule 5: The bits of a word are never synchronized one by one.

    They can land on different edges and make a word that was never sent.

  6. Rule 6: Choose the crossing by what must survive.

    The table below gives one circuit for each kind of signal.

What must survive, and the crossing for it
The signal isWhat must surviveThe crossingBuilt in
A levelIts latest valueA two-flop synchronizer.The Two-Flop Synchronizer
An eventEvery single occurrenceThe sender turns the pulse into a level. It waits for an answer from the receiver before it sends the next.Pulse, Toggle and Handshake
A wordAll bits, from the same momentThe sender holds the word still. Only one "take it now" bit is synchronized.Pulse, Toggle and Handshake
A streamEvery word, in orderA memory with a write side on one clock and a read side on the other.The Asynchronous FIFO
Sources for These Rules

Rule 3, metastability and the two-flop synchronizer: Mehta, ASIC/SoC Functional Design Verification, chapter 8, sections 8.2 and 8.3.1. Also Harris and Harris, Digital Design and Computer Architecture, ARM edition, section 3.5.5. And Weste and Harris, CMOS VLSI Design, fourth edition, section 10.6.2.

Rule 4, a pulse lost between two edges of a slower clock, and the rule of thumb of one and a half periods: Mehta, ASIC/SoC Functional Design Verification, chapter 8, section 8.3.3.

Rule 5, a word of several bits: Mehta, ASIC/SoC Functional Design Verification, chapter 8, section 8.3.4. A stream through a memory with two clocks: section 8.3.5.

Rules 1, 2 and 6 are how a clock plan is written. They are design practice, not rules of a standard.

Now Run It

The lab at the end of this page has two free-running clocks and no bus. A few lines of test code play the real-time clock block. Three two-flop synchronizers receive on the core clock.

Printed by the lab on Icarus. Times are in nanoseconds. Each sample is taken 1 ns after a core edge.
sent t=22 alarm=1 tick=1
sample t=26 alarm_1=1 alarm_seen=0 tick_seen=0
sample t=36 alarm_1=1 alarm_seen=1 tick_seen=0
sample t=46 alarm_1=1 alarm_seen=1 tick_seen=0
core edges that saw tick=0
sent t=66 mode=10
sample t=76 mode_seen=11
sample t=86 mode_seen=10

Rule 3 at work: at 26 ns the first flip-flop holds the alarm and the second does not yet. At 36 ns the core domain sees it.

Rule 4 at work: tick_seen is 0 in all three samples. The next row counts no core edge that saw the tick. The pulse of 2 ns is gone.

Rule 5 at work: the word 10 is complete at 66 ns. At 76 ns the receiving side holds 11. One period later it holds 10.

This lab leaves the main thing out. A simulator of 0 and 1 cannot show a flip-flop that hovers. So the lab shows what a synchronizer loses, and not what it is for. The 2 ns between the two bits of the word is a lab number.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Few clock domainsA clock for every blockFew crossings to design and check. Blocks share a rate that some do not need.
A divided clock for slow blocksA second oscillator for themThe two clocks stay related, so no synchronizer is needed. The rates are tied together.
One crossing for a whole bus transferA synchronizer on every bus wireAddress, data and direction arrive as one word. The transfer waits a few periods.
The alarm as a levelThe alarm as a pulseIt cannot be lost. Someone must clear it after it was served.

The third row is why a crossing is put where a transfer can wait. A bus that is cut in the middle of its handshake leaves two domains to decide which write happened.

The Trap: Two True Traces

Back to the two engineers. The timer engineer probed the alarm wire and saw a pulse, 2 ns wide. The processor engineer looked at core edges and saw 0 three times. Both are right. The pulse lived and died between two edges.

The fix is not a third flip-flop. The fix is to ask what the alarm means. If it means "service is due", make it a level that stays until it is cleared. If it means "count one more", a level is not enough either: two events in one period would merge into one.

The second trap is a label. Two oscillators on the board both say 50 MHz, so someone treats their clocks as one domain. They are unrelated. Each drifts a little, and sooner or later an edge of one lands on an edge of the other.

At Scale

This page3 clocks, 2 crossings
A phone chiphundreds of clocks, thousands of crossings

This section looks ahead. Our map has two crossings. A large chip has thousands, and nobody finds them by reading.

  1. A list of every crossing

    A plan lists each crossing with its two clocks, what must survive, and its circuit. A tool finds the crossings in the design, and each must match a row of the list.

  2. Modes change the map

    A clock mux can turn a related pair into an unrelated one. The list is checked again for every setting of every mux.

  3. The timing check must know

    The program that checks timing treats all clocks as related until it is told otherwise. The next page tells it, and shows what its report then leaves out.

  4. Resets cross too

    A reset that is released in one domain reaches flip-flops of another. Reset Across Clock Domains builds the circuit for it.

Try It

Run the lab. A level, a pulse of 2 ns and a word of two bits cross from the RTC clock to the core clock.

Change the width of the pulse from 2 to 12. Predict the last of the first five rows before you run it.

Let both bits of the word change at the same moment. Does the receiver still see 11?

Names Printed by the Lab
NameMeaning
sentThe moment at which the sending side changes a signal, and its new value.
sampleA look at the receiving side, 1 ns after a rising edge of the core clock.
alarm_1The first flip-flop of the synchronizer for the alarm.
alarm_seen, tick_seen, mode_seenThe second flip-flop of each synchronizer: what the core domain may use.
core edges that saw tickThe number of core edges at which tick_seen was 1.
RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: sent t=22 alarm=1 tick=1 sample t=26 alarm_1=1 alarm_seen=0 tick_seen=0 sample t=36 alarm_1=1 alarm_seen=1 tick_seen=0 sample t=46 alarm_1=1 alarm_seen=1 tick_seen=0 core edges that saw tick=0 sent t=66 mode=10 sample t=76 mode_seen=11 sample t=86 mode_seen=10

Check Yourself

Predict the outputIn this run the pulse is 12 ns wide: it is high from 22 ns to 34 ns. The run ends after the pulse. What is the last row the lab prints?
`timescale 1ns/1ns
module tb;
  bit core_clk, rtc_clk;
  always #5 core_clk = ~core_clk;              // the core clock: period 10 ns, rising at 5, 15, 25 ...
  always #7 rtc_clk = ~rtc_clk;                // the clock of the real-time clock block: period 14 ns

  // Three things the real-time clock block sends.
  logic alarm = 0;                             // a level: it stays high until someone serves it
  logic tick = 0;                              // a pulse: high for 12 ns
  logic [1:0] mode = 2'b01;                    // a word of two bits

  // The receiving side: two flip-flops in a row for every bit, on the core clock.
  logic alarm_1 = 0, alarm_seen = 0, tick_1 = 0, tick_seen = 0;
  logic [1:0] mode_1 = 2'b01, mode_seen = 2'b01;
  int tick_samples = 0;
  always @(posedge core_clk) begin
    alarm_1 <= alarm;  alarm_seen <= alarm_1;
    tick_1  <= tick;   tick_seen  <= tick_1;
    mode_1  <= mode;   mode_seen  <= mode_1;
    if (tick_seen) tick_samples++;
  end

  initial begin
    repeat (2) @(negedge core_clk);
    @(posedge rtc_clk);                        // an edge of the sending clock, at 21 ns
    #1 alarm = 1;
    tick = 1;
    $display("sent t=%0d alarm=1 tick=1", $time);
    #12 tick = 0;
    repeat (2) begin
      @(posedge core_clk);
      #1 $display("sample t=%0d alarm_1=%b alarm_seen=%b tick_seen=%b", $time, alarm_1, alarm_seen, tick_seen);
    end
    @(posedge core_clk);
    #1 $display("core edges that saw tick=%0d", tick_samples);
    $finish;
  end
endmodule
ConceptWhat is a clock domain?
ConceptWhich two clocks are related?
ConceptWhat can a two-flop synchronizer lose?
ConceptWhy are the bits of a word not synchronized one by one?
ConceptEvery word of a stream must cross, in order. Which crossing fits?

Common Mistakes

Wrong Treat two oscillators of the same rate as one domain.

Their edges drift past each other.

Fix Trace each clock to its source. One source, or unrelated.

Wrong Send a short pulse through a two-flop synchronizer.

It can fall between two edges of the receiving clock.

Fix Send a level, and clear it after it was served.

Wrong Put a synchronizer on every bit of a word.

The bits can land on different edges.

Fix Hold the word still and synchronize one "take it now" bit.

Remember

  • Flip-flops that use one clock form a clock domain. Clocks are related only if one source makes both.
  • A signal between unrelated clocks enters through a two-flop synchronizer in the receiving domain.
  • A synchronizer samples: it can lose a short pulse, and it can tear a word apart.
  • Plan every crossing by what must survive: a level, an event, a word or a stream.

Practice

Report a problem with this page