Seyed Masoud Hosseini · Overview · Study log · Ideas · Transcript · RSS feed

FPGA & Verilog Design · Lecture 10 of 12 · 13:26

Part 10: Metastability and Clock Domain Crossing

Introduction to FPGA Part 10 - Metastability and Clock Domain Crossing | Digi-Key Electronics on YouTube

Study guide

What this lecture covers

This lecture addresses what can go wrong when a signal isn't safely aligned to the clock sampling it - a concern for asynchronous inputs and for any design using more than one clock. It explains setup and hold time, what metastability is, and why crossing between clock domains without care is risky.

By the end you can describe why a D flip-flop needs its input held steady around a clock edge, build a synchronizer from chained flip-flops, recognize why the series' earlier clock dividers were technically bad practice, build a better divider using a single "tick" pulse, and explain at a conceptual level how an asynchronous FIFO lets data cross clock domains safely. It closes the introductory portion of the series and previews building a RISC-V soft core processor next.

Key ideas

  • Setup and hold time: a flip-flop's input must remain stable for a defined window before (setup) and after (hold) the clock edge; a change during that window is a timing violation.
  • Metastability: a timing violation can leave a flip-flop's output in an unknown, unstable state for an indeterminate time before it settles - a probabilistic failure, not a deterministic one.
  • Synchronizer: chaining two (or more) flip-flops in series on an asynchronous or cross-domain signal greatly reduces the chance that metastability propagates to the rest of the design, at the cost of one extra clock cycle of latency per stage.
  • Clock domain: all logic clocked from one signal (or a divided version of it); crossing between domains without a synchronizer risks metastability.
  • Bad clock divider practice: dividing a clock by feeding one flip-flop's output into another's clock input effectively creates a new, uncontrolled clock domain.
  • Tick-based clock divider: instead of generating a divided clock signal, a counter produces a single-cycle "tick" pulse that the main clocked logic checks conditionally, keeping the whole design in one clock domain.
  • Asynchronous FIFO: a dual-clocked block RAM-based queue (write clock and read clock) that lets data move safely between clock domains, using internal synchronizers on the read/write address pointers.

Walkthrough

Setup, hold time, and metastability (0:00)

The lecture defines the setup and hold windows around a flip-flop's clock edge, explains that a violation can cause metastability - an unpredictable, temporarily unstable output - and notes that this is probabilistic and generally too fast to observe directly on an oscilloscope, referencing an external experiment that demonstrates it by deliberately shifting clock phase.

Building a synchronizer (4:08)

The fix for sampling an asynchronous or cross-domain signal is a chain of flip-flops (a synchronizer): each additional stage reduces the chance that metastability reaches the final output, at the cost of extra clock-cycle latency.

Clock domains and why crossing them is risky (6:11)

The lecture defines a clock domain and explains clock domain crossing - sampling a signal generated by one clock (say, an external 5 MHz SPI signal) with logic clocked by another (the 12 MHz system clock) - as a common source of hard-to-debug, intermittent failures, recommending a synchronizer at the receiving domain and, where possible, avoiding domain crossings altogether.

A better clock divider design (7:14)

The lecture revisits the series' earlier clock dividers, noting that generating a divided clock signal from flip-flop outputs effectively creates an unmanaged second clock domain. The improved design instead counts up to a parameterized maximum and asserts a one-cycle "tick" output, which downstream logic checks with a simple conditional inside its existing clocked always block - keeping the whole design in a single, well-behaved clock domain.

Introducing FIFOs for clock domain crossing (9:20)

A FIFO (first-in, first-out queue) is introduced as a memory-based way to pass data between a sender and receiver safely: elements are read in the order written, and code must track full/empty conditions to avoid reading from an empty FIFO or writing to a full one.

Asynchronous FIFOs and the challenge (11:24)

Using the iCE40's dual-clock block RAM (separate read and write clocks) and Clifford Cummings' well-known FIFO design as a reference, the lecture explains how synchronizer circuits on the internal read/write address pointers make the FIFO safe to use across two independent clock domains. The challenge is to implement this FIFO in Verilog and write a test bench proving it correctly handles full and empty conditions.

Before you watch

  • Complete Part 9 ("Phase-Locked Loop (PLL) and Glitches"); this lecture assumes familiarity with propagation delay and builds on that intuition for timing violations.
  • Recall the clock dividers built in Parts 4 and 6, since this lecture explains why that earlier approach should be improved.

Check your understanding

  1. What is the difference between a setup time violation and a hold time violation?
  2. Why does adding a second flip-flop to a synchronizer reduce the risk of metastability reaching the rest of the design?
  3. Why does the lecture consider the series' earlier clock-divider designs bad practice, and how does the tick-based divider avoid that problem?
  4. What conditions must a FIFO implementation guard against to avoid corrupting its data?
  5. Why does an asynchronous FIFO need synchronizers on its internal read and write address pointers?

From the YouTube description

A field-programmable gate array (FPGA) is an integrated circuit (IC) that lets you implement custom digital circuits. You can use an FPGA to create optimized digital logic for things like digital signal processing (DSP), machine learning, and cryptocurrency mining. Because of the FPGA’s flexibility, you can often implement entire processors using its digital logic. You can find FPGAs in consumer electronics, satellites, and in servers used to perform specialized calculations.

In this series, we will see how an FPGA works and demonstrate how to create custom digital logic using the Verilog hardware description language (HDL).

Previously, we showed how to use a phase-locked loop (PLL) to increase clock speed as well as introduced the concept of glitches. In this episode, we examine how setup and hold time violations can cause metastability in flip-flops.

The solution to the challenge at the end of the episode can be found here: https://www.digikey.com/en/maker/projects/introduction-to-fpga-part-10-metastability-and-fifo/74884ed134474e008a1e444ea9dacb0f

All code examples and solutions for this series can be found here: https://github.com/ShawnHymel/introduction-to-fpga

Colin O’Flynn’s metastability experiment: https://colinoflynn.com/2020/12/experimenting-with-metastability-and-multiple-clocks-on-fpgas

Better clock divider example: https://github.com/ShawnHymel/introduction-to-fpga/tree/main/10-metastability/example-02-better-clock-divider

Better button debounce design: https://github.com/ShawnHymel/introduction-to-fpga/tree/main/10-metastability/example-03-better-debouncer

Clifford Cummings’s FIFO paper: http://www.sunburst-design.com/papers/CummingsSNUG2002SJ_FIFO1.pdf

A flip-flop requires the input signal to be steady for some time before the clock edge (setup time) and remain steady for some time after the clock edge (hold time). If the input signal transitions during the setup or hold windows, then it is considered a timing violation. This could potentially cause metastability on the flip-flop, where the output remains in an unknown state for some indeterminate amount of time.

Normally, the metastable output will settle on logic high or logic low quickly (within a few nanoseconds), but it is not guaranteed. Metastable events can cause potentially catastrophic failures in a design and can be very difficult to track down due to their probabilistic nature.

The standard fix to mitigate the likelihood and impact of metastability is to use a synchronizer circuit, which consists of 2 or more flip-flops chained together. The input of one flip-flop samples the output of another. Note that this introduces an extra clock cycle of delay for each flip-flop you put in the chain.

A first-in, first-out (FIFO) system is one way to pass data from one class domain to another. Elements are read from the FIFO in the order in which they were written. We can use dual-port block RAM to construct a FIFO in our iCE40. Your challenge is to implement Clifford Cummings’s FIFO design and test it via simulation.

Product Links:
https://www.digikey.com/en/products/detail/lattice-semiconductor-corporation/ICE40HX1K-STICK-EVN/4289604

Related Videos:
https://www.youtube.com/watch?v=z8Oldd-nrfs
https://www.youtube.com/watch?v=5kNXX67mchE
https://www.youtube.com/watch?v=iwcxLQ6AB88

Related Project Links:
https://www.digikey.com/en/maker/projects/introduction-to-fpga-part-10-metastability-and-fifo/74884ed134474e008a1e444ea9dacb0f

Related Articles:
https://www.digikey.com/en/pdf/r/renesas-electronics-america/powering-fpga-applications
https://www.digikey.com/en/videos/d/dsp/edge-machine-deep-learning-on-fpga

Learn more:
Maker.io - https://www.digikey.com/en/maker
Digi-Key’s Blog – TheCircuit https://www.digikey.com/en/blog
Connect with Digi-Key on Facebook https://www.facebook.com/digikey.electronics/
And follow us on Twitter https://twitter.com/digikey

← Part 9: Phase-Locked Loop (PLL) and Glitches · Part 11: RISC-V Softcore Processor →