Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 37 ยท Design Patterns

Clock Enables and Clock-Domain Crossing

Slow parts of a design with clock enables instead of derived clocks, and move signals between clock domains safely with two-flip-flop synchronizers and Gray code.

Real systems contain things that run at different rates: a 100 MHz core, a 1 kHz display refresh, a 9600 baud serial line, a button pressed by a human. This lesson covers the two disciplined ways to deal with that: clock enables, for parts that are slower but still on the same clock, and synchronizers, for signals that cross between truly different clocks.

VHDL is read-only on this site, and the C programs model counters, edges and the metastability problem so you can experiment.

Slow logic without a slow clock

The tempting way to make something run at 1 kHz is to divide the clock and use the divided signal as a clock:

VHDL
-- DON'T: a derived clock built from logic
if rising_edge(clk) then
  div <= div + 1;
end if;
slow_clk <= div(16);
process (slow_clk) ...   -- clocked by a logic signal

This creates a new clock domain, which the clock network and timing analysis handle poorly: skew and glitches appear, and crossing between clk and slow_clk becomes a problem. The recommended pattern keeps one clock and uses a one-cycle enable pulse (a tick):

VHDL
signal tick : std_logic;
signal div  : unsigned(16 downto 0) := (others => '0');
...
process (clk)
begin
  if rising_edge(clk) then
    if div = 99_999 then            -- 100 MHz / 100,000 = 1 kHz
      div  <= (others => '0');
      tick <= '1';
    else
      div  <= div + 1;
      tick <= '0';
    end if;
  end if;
end process;

-- the "slow" logic still runs on clk, but only acts on tick
process (clk)
begin
  if rising_edge(clk) then
    if tick = '1' then
      digit <= digit + 1;
    end if;
  end if;
end process;

Everything stays in one clock domain, timing is analyzed normally, and FPGA flip-flops have a built-in clock-enable pin, so it costs no extra logic.

C C stand-in: a 1-cycle tick from a fast clock

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.

Clock-domain crossing (CDC)

Sometimes you really have two independent clocks: a sensor clock and a system clock, a bus and a core. A signal moving between them can change at any instant relative to the receiving clock, including right at the sampling edge. A flip-flop that samples a changing input can enter a metastable state: its output hovers between 0 and 1 for an unpredictable time before resolving. If another stage reads the output meanwhile, the error spreads.

You cannot prevent metastability; you reduce the probability that it reaches your logic to once in thousands of years. The tool is the two-flip-flop synchronizer:

VHDL
signal meta, sync : std_logic;
attribute async_reg : string;
attribute async_reg of meta : signal is "true";
attribute async_reg of sync : signal is "true";
...
process (clk_dst)
begin
  if rising_edge(clk_dst) then
    meta <= async_in;     -- may go metastable
    sync <= meta;         -- has a full clock cycle to settle
  end if;
end process;

The first flip-flop may go metastable; it has a whole clock period to settle before the second flip-flop samples it. The async_reg attribute (an AMD/Xilinx name; other vendors have equivalents) tells the tools to place the two flip-flops next to each other and not to optimize across them. Never put logic between them, and never use meta elsewhere.

What a synchronizer can and cannot do

It safely carries one bit whose changes are slow compared to the destination clock. It adds 2 destination clock cycles of latency, and it can miss a pulse shorter than a destination clock period. For that case, stretch the pulse in the source domain or use a toggle-based handshake.

C C stand-in: a pulse shorter than the destination clock can be missed

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 short pulse (one sample) falls between destination edges and is lost; the long pulse is seen. Design rule: a signal crossing to a slower clock must stay stable for at least a destination period or two.

Multi-bit crossings

Never pass a multi-bit bus through separate synchronizers: the bits can arrive on different clock edges, producing a value that never existed (for example 0111 -> 1000 captured as 1111). Solutions:

  1. Gray code: only one bit changes per step, so even a skewed capture gives either the old or the new value. Used for counters and FIFO pointers.
  2. Handshake: hold the data stable, raise a request, synchronize the request, capture the data, send an acknowledge back.
  3. Asynchronous FIFO: a dual-clock memory with Gray-coded pointers (the standard answer for streams).
C C stand-in: every value a skewed capture can see

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.

In binary 3 -> 4 (011 -> 100) all three bits change, so a skewed capture can return any of eight values (0 to 7), most of them wrong. The Gray version changes one bit and can only return 3 or 4: either the old or the new value, both legitimate.

Asynchronous reset deassertion

A related trap: an asynchronous reset released at a random moment can leave some flip-flops out of reset one cycle earlier than others. The fix is to assert reset asynchronously but release it synchronously through a small synchronizer, one per clock domain.

CDC bugs do not appear in functional simulation (zero-delay models never produce metastability) and they appear randomly on hardware. Keep the number of crossings small, document each one, and use the vendor's CDC checker.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why prefer a clock enable over a divided clock signal?

    Derived clocks create new domains with skew and crossing issues; an enable pulse stays in the original domain.
  2. 2. What does a two-flip-flop synchronizer reduce?

    The first flip-flop may go metastable; the second samples it one full period later, after it has probably settled.
  3. 3. Why not synchronize each bit of a counter separately?

    Use Gray code, a handshake or an asynchronous FIFO for multi-bit values.
  4. 4. A one-cycle source pulse crosses into a slower clock domain. What may happen?

    A pulse shorter than the destination period can fall between sampling edges.

Summary

  • Use a clock-enable tick, not a derived clock, for slower logic on the same clock.
  • Cross between asynchronous clocks only through synchronizers; the first flip-flop may go metastable.
  • A synchronizer works for one slowly-changing bit; stretch short pulses.
  • Multi-bit buses cross with Gray code, handshakes or an asynchronous FIFO.
  • Release asynchronous reset synchronously; CDC bugs do not show up in zero-delay simulation.

Next: FIFOs and Handshakes