This website uses cookies. By using this site, you consent to the use of cookies. For more information, please take a look at our Privacy Policy.
Home > FPGA Technical Tutorials > Learning FPGAs > Sequential Logic > Timing and Metastability

Timing and Metastability

FONT SIZE : AAA

When thinking about designs, it is easiest to assume that the DFF takes an instanta‐ neous snapshot of its input at the rising edge of a clock. However, that isn’t true in practice. The DFF requires that its input doesn’t change for a small period of time around the rising edge of the clock.

The time required for it to be stable before the rising edge is known as the setup time, and the time after the edge is known as the hold time.

During the time around the edge, if the D input changes, the value of Q is not guaran‐ teed to be valid. However, it gets worse. Not only can Q not be the value that D was, but it also can do some weird things such as oscillate between 0 and 1 or even get stuck somewhere in the middle. This instability can then propagate through your design, making other DFFs unstable.

In Figure 4-5, D is stable through the setup and hold times of the first two edges, so Q properly copies the value of D. However, on the last edge, D changes during the setup time, so the value of Q is unknown.

image.png

So how do we make sure that our designs don’t violate the flip-flop’s timing require‐ ments? For the most part, the tools take care of it. Let’s look at an example of two flipflops connected by an inverter, as shown in Figure 4-6.

Two DFFs connected through an inverter.png

So what will happen here when Q of the first flip-flop is 0, but D is 1 and there is a rising edge?

Let’s quickly define the terms that you haven’t seen yet. If you look back at the sche‐ matic for this simple circuit, you’ll see that both DFFs have the same clock. It’s com‐ mon for all the DFFs in your entire design to have the same clock. However, the DFFs are in different locations, and the clock signal originates from a single point. It takes time for the clock signal to reach each DFF. The difference in arrival time of the clock leads to clock skew. This means that the rising edges of the clock are not exactly the same for each DFF. Also note that the skew can be both positive and negative, depending how the circuit is actually laid out.

In Figure 4-7, Clock 1 refers to the clock seen at the input of DFF 1, and Clock 2 refers to the clock seen at DFF 2. Both clocks originate from the same source.

Clock skew.png

Earlier we also assumed that the DFF copied the D value to Q immediately at the rising edge. However, that’s not the case. There is a small amount of time between the rising edge and when Q actually changes. This is known as the clock-to-Q propagation delay, as shown in Figure 4-8.

Clock-to-Q delay.png

Once Q changes, the new value needs to propagate through the circuit. The time it takes for the old value to be contaminated with the new value is called the contamina‐ tion delay.

However, after the contamination delay, the value may toggle a few times before set‐ tling on the correct value. The time it takes after Q changes to when the output is sta‐ ble and correct is called the combinational logic propagation delay. This delay is a function of your circuit design. The more logic you stick between two flip-flops, the longer this will be. It ultimately dictates the maximum clock speed at which you can run your design. Figure 4-9 illustrates contamination delay and combinational logic propagation delay.

Logic delays.png

If we ignore clock skew, to satisfy hold time we need to make sure that the clock-to-Q delay plus the contamination delay is equal to or greater than the hold time. Note that the clock-to-Q and the contamination delays are a function of how the circuit is laid out and not of the clock frequency. Because of this, we have to fully rely on the tools to make sure hold time is being met.

Again ignoring clock skew, to satisfy setup time, we need to ensure that the period of the clock minus the setup time is greater than or equal to the clock-to-Q delay plus the combinational logic delay. In this case, the period of the clock plays a role in tim‐ ing. If for some reason the tools can’t meet timing on your design, they will prioritize satisfying the hold time over the setup time. This is because you can always satisfy the setup time by reducing your clock frequency (which increases the clock period)

Take a look at all of these constraints put together in Figure 4-10.

Timing of the DFF chain.png

Notice that in this example the circuit would be functional because the inputs to the flip-flops never change between the setup and hold times around each rising edge.

External Inputs

Luckily for us, the tools will figure out all these timing issues and attempt to lay out your design in such a way that it will all work. However, they can figure out the tim‐ ing only when things are clearly defined. In some cases, the tools can’t satisfy timing for you. One case is when you have external inputs.

You can synchronize some external inputs to the system clock so that you don’t have to worry about the following issues. However, some inputs are generated from a dif‐ ferent system clock or have no clock at all.

In fact, in our previous example with the counter, we used the DIP switches as an input directly to the counter. What happens if we flip one of the switches right on the rising edge of the clock? There is no way we can possibly synchronize our switch flips. This means we will eventually violate timing and cause stability issues if we flip it enough.

So how do we solve this issue? It is quite simple: we can use a simple circuit known as a synchronizer, shown in Figure 4-11.

Basic synchronizer.png


The most basic synchronizer is two flip-flops connected together. You feed the unsynchronized input into the leftmost one, and the output on the right will be synchronized to the clock. This works because even if the first flip-flop becomes unstable, it is pretty unlikely that it will also cause the next to become unstable. How‐ ever, there is still a chance that they both become unstable. To further reduce the pos‐ sibility of instability, you can chain more than two DFFs together. The more you chain, the lower the probability of instability at the end. However, the payoff quickly diminishes after just two. This is something that all unsynchronized inputs in any digital system suffer from, and there is always that small chance that a value will be read incorrectly, or worse, the instability propagates. However, systems are designed so that their mean time between failures, or MTBF, is ridiculously high. Even just two flip-flops chained like this can have an MTBF of millions of years, depending on the input and flip-flop characteristics.

Add the Components

Open the Components Library, and go to Project → Add Components. We need three of the components for this project: the Pipeline, Edge Detector, and Button Condi‐ tioner. All of these can be found under the Miscellaneous category, as shown in Figure 4-12.

Components Library.png

The Pipeline component is simply a chain of DFFs like the synchronizer. The length of the chain is configurable using parameters. Let’s take a look at the source:

module pipeline #(

 DEPTH = 1 : DEPTH > 0 / number of stages

 )(

 input clk, / clock

 input in, / input

 output out / output

 ) { 

 dff pipe[DEPTH] (.clk(clk));

 var i; 

 always {

 / in goes into the start of the pipe

 pipe.d[0] = in;

/ out is the end of the pipe

 out = pipe.q[pipe.WIDTH-1];  

 / for each intermediate state

 for (i = 1; i < DEPTH; i++)

 pipe.d[i] = pipe.q[i-1]; / copy from previous

 }

}

The parameter DEPTH is used as the size of the array of DFFs, pipe. Even though DEPTH can take different values, it is constant at synthesis time, so you can use it as array sizes and indices.

Looking in the always block, the first line connects the input, in, to the first dff in the chain. The second line connects the output, out, to the output of the last flip-flop. To index the last flip-flop, we use the WIDTH attribute of the array. The WIDTH attribute for a single-dimensional array is simply a number. In this case, it will be the same value as DEPTH. For multidimensional arrays, WIDTH will be a single-dimensional array with each entry being the width of a different dimension. For example, dff array[3] [5] will have a WIDTH attribute of {5, 3}, so that array.WIDTH[0] is equal to 3, and array.WIDTH[1] is 5.

There is a new type used in this module too, var. This stands for variable and is used to store a value that won’t really be in the hardware design but is useful in the descrip‐ tion. The most common use for a var is as the index of a for loop.

Now let’s take a look at the for loop:

for (i = 1; i < DEPTH; i++)

pipe.d[i] = pipe.q[i-1]; / copy from previous

For loops take a similar form to C/C++ or Java for loops. They have the syntax for (init_expr; condition_expr; operation) statement. Only the type var should be used in for loops. The init_expr sets the initial value of the var—in this case, 1. The condition_expr is evaluated before each iteration to see whether the loop should continue. Finally, operation specifies how the var will change on each iteration

Loops in Lucid my seem similar to software for loops you might be familiar with. However, they are implemented differently. for loops should be thought of as short‐ hand for writing everything out. The synthesizer will essentially unroll the for loop, duplicating the hardware required for each iteration. Unlike code, they don’t make your design any smaller. They only make the design files smaller and easier to maintain.

In this case, we couldn’t unroll the loop by hand because the number of iterations is specified by the parameter DEPTH. However, DEPTH is a constant (although it may have different values for different instances), so the tools can unroll it. for loops can be used only when they have a constant number of iterations that can be determined at synthesis time.

This for loop will simply connect the output of the previous flip-flop to the input of the next, creating the chain. Note that if DEPTH is 1, which is allowed with our con‐ straint, the for loop will be ignored because the condition_expr will fail on the first iteration. This is what we want, because the input and output of the single dff would already be connected to the module’s ports.

Take a look at Figure 4-13 to see how this will look in hardware.

Hardware pipeline.png

Instantiating a Module

Now that you have a solid understanding of the pipeline component, let’s use it in the running total project to synchronize the switch inputs with the clock:

module mojo_top (

 input clk, / 50MHz clock

 input rst_n, / reset button (active low)

 output led [8], / 8 user controllable LEDs

 input cclk, / configuration clock, AVR ready when high

 output spi_miso, / AVR SPI MISO

 input spi_ss, / AVR SPI Slave Select

 input spi_mosi, / AVR SPI MOSI

 input spi_sck, / AVR SPI Clock

 output spi_channel [4], / AVR general purpose pins

 input avr_tx, / AVR TX (FPGA RX)

 output avr_rx, / AVR RX (FPGA TX)

 input avr_rx_busy, / AVR RX buffer full

 output io_led [3][8], / LEDs on IO Shield

 output io_seg [8], / 7-segment LEDs on IO Shield

 output io_sel [4], / Digit select on IO Shield

 input io_button [5], / 5 buttons on IO Shield

 input io_dip [3][8] / DIP switches on IO Shield

 ) {

sig rst; / reset signal


 .clk(clk) {

 / The reset conditioner is used to synchronize the reset signal to the FPGA

 / clock. This ensures the entire FPGA comes out of reset at the same time.

 reset_conditioner reset_cond;

 / create a 3x8 array of pipelines with a depth of 2

 pipeline switch_cond[3][8] (#DEPTH(2));


 .rst(rst) {

 dff running_total[24]; / Running total, 24 bits wide

 }

 }


 always {

 reset_cond.in = ~rst_n; / input raw inverted reset signal

 rst = reset_cond.out; / conditioned reset


 led = 8h00; / turn LEDs off

 spi_miso = bz; / not using SPI

 spi_channel = bzzzz; / not using flags

 avr_rx = bz; / not using serial port


 io_seg = 8hff; / turn segments off

 io_sel = 4hf; / select no digits


 switch_cond.in = io_dip; / connect the raw switch input to the conditioner


 / use the conditioned switch input

 running_total.d = running_total.q + switch_cond.out[0];

 io_led =

 {running_total.q[16+:8], running_total.q[8+:8], running_total.q[0+:8]};

 }

}

To instantiate the module, we use its name followed by the name of our instance. Note that the pipeline module has a clk input but not a rst input. This means we can put in the port connection block for clk, but not with running_total in the rst port connection block. We also need the pipeline to be an array of pipelines. To do this, we can tack on the array size to the name. This will duplicate the pipeline module for each element in the array. Because the entire array is in the clk port connection block, each individual element will have its clk input connected to clk:

.clk(clk) {

.

/ create a 3x8 array of pipelines with a depth of 2

pipeline switch_cond[3][8] (#DEPTH(2));

.

}

If you instantiate an array of modules and don’t connect a port at instantiation, that port will be packed into an array. This is what happens to switch_cond.in and switch_cond.out. While they are typically a single bit, because switch_cond is an array, they become 3 x 8 arrays themselves. This is convenient because we can then directly assign io_dip to switch_cond.in since they have the same dimensions. This also makes switch_cond.out a direct drop-in replacement for io_dip.

The default depth of the pipeline is 1, which is pretty useless because you could just use a DFF directly. We need to override this so that we get a chain of two DFFs. To specify a parameter’s value, you specify it basically the same way as you do a port con‐ nection, except instead of leading the name with a period (.), you use #. You can even add parameter values to port connection blocks if everything in the block has the same parameter name. If we didn’t instantiate the pipeline module in the port con‐ nection block, we could use the following:

      pipeline switch_cond[3][8] (#DEPTH(2), .clk(clk));

The order of parameters and port connections don’t matter, but typically parameters are assigned first.

If you try changing DEPTH to an invalid value such as 0, you will get an error from the failing constraint:

    Line 28, Column 13 : The constraint "DEPTH>0" for parameter "DEPTH" with 

     value "0: {0}" failed

If you build and load this project, it should behave basically the same as before— except this time we don’t have to worry about metastability issues.

Checking Timing

How do we know that the tools were able to meet the timing requirements? If you look at the output when you build your project, a little bit above the end there is a section that looks something like this:

image.png

All constraints were met.

The important line here is the All constrains were met. part. This means the tools were able to route our design so that it should work under all operating conditions that the FPGA is designed to work in. If there was a problem, the Timing Errors and Timing Score columns would have nonzero values.

We will cover later what to do if timing fails, but for most designs running at 50 MHz, timing won’t be an issue. In the preceding case, the Best Case Achievable time of 5.539 ns means this design could run at 180 MHz (1/5.539 ns) without problems.

Also how do the tools know that the clock is 50 MHz? We told them! If you look under the Constraints section of the project tree, you will find a file called mojo.ucf. This file is where we tell the tools what pins on the FPGA to use for each signal inter‐ nal to the Mojo as well as the frequency of the clock. The file io_shield.ucf contains the pin constraints used by the IO Shield.

Don’t worry about the user constraints €le (UCF) details for now: we will get to them later.

Bouncy Buttons and Edges

We are going to modify the project further so that it adds the switch value only when we push a button. But first, you need to know some stuff about buttons. We could try to synchronize the button input as we did with the switches, but buttons have another issue: they often bounce when you press them. The contacts, instead of staying closed, hit each other and can then separate before slamming back together, as shown in Figure 4-14. This can happen a few times or none at all. To compensate for this, we need to register button presses only when they have been pressed and continue to be pressed for a small amount of time.

Button bouncing.png

Luckily, the Button Conditioner component takes care of this for us.

Feel free to take a look at the contents of the module located in the button_condi‐ tioner.luc file. It works by first passing the button input through a pipeline, as we did with the switches. If the synchronized output is 1 (the button is assumed active high), a counter is incremented until it reaches its maximum value. When it reaches the maximum value, the button conditioner outputs 1. If at any point the button input is 0, the counter is reset and the output of the conditioner is 0.

The button conditioner will output 1 when the button is pressed, and 0 when it isn’t. However, we want to add the number only once for each time the button is pressed. To do this, we need to be able to detect when the button goes from 0 to 1. This is easy to do with a single DFF. You simply feed the value into the DFF. The output of the DFF will then be the value of the button from the last clock cycle. If the button was 0 on the last clock cycle, but is now 1, we know it was just pressed.

However, we don’t need to do this because another component does this for us: the Edge Detector component.

If you take a look at its source, you’ll see it does basically what was just described. The one main difference is that you can configure it so that it will detect rising, falling, or both types of edges using parameters.

Running Total

We now have all the pieces we need to make a more useful running total design.

First, we need to instantiate the button conditioner and edge detectors:

 .clk(clk) {

 / The reset conditioner is used to synchronize the reset signal to the FPGA

 / clock. This ensures the entire FPGA comes out of reset at the same time.

 reset_conditioner reset_cond; 

 / create a 3x8 array of pipelines with a depth of 2

 pipeline switch_cond[3][8] (#DEPTH(2));

 button_conditioner btn_cond;

 edge_detector edge (#RISE(1), #FALL(0)); 

 .rst(rst) {

 dff running_total[24]; / Running total, 24 bits wide

 }

 }

Again, both of these don’t have rst inputs, so we can stick them in the clk port con‐ nection block. The default parameter values for the button conditioner are perfect, so we don’t need to override them. However, the edge detector needs to be told to look for only rising edges.

Finally, we need to connect it all up in the always block:

always {

 reset_cond.in = ~rst_n; / input raw inverted reset signal

 rst = reset_cond.out; / conditioned reset 

 led = 8h00; / turn LEDs off

 spi_miso = bz; / not using SPI

spi_channel = bzzzz; / not using flags

 avr_rx = bz; / not using serial port 

 io_seg = 8hff; / turn segments off

 io_sel = 4hf; / select no digits 

 switch_cond.in = io_dip; / connect the raw switch input to the conditioner 

 btn_cond.in = io_button[4]; / io_button[4] is the "right" button

 edge.in = btn_cond.out; / connect the button to the edge detector 

 if (edge.out) / only add when the button is pressed

 / use the conditioned switch input

 running_total.d = running_total.q + switch_cond.out[0]; 

 io_led =

 {running_total.q[16+:8], running_total.q[8+:8], running_total.q[0+:8]};

 }

By using the if statement, we are able to assign running_total.d the new sum only when edge.out is 1. You may remember from before that under all circumstances, you need to assign a signal a value. The one exception to this rule is the d input of the DFF. This is because a DFF is able to save its last value, while a basic wire can’t. If you don’t assign it a value, the q output will stay the same.

Build and load the project onto your Mojo. By setting the DIP switches and pressing the “right” button, you can now add individual numbers to the running total. If you want to reset the total back to 0, press the button on the Mojo itself (not on the IO Shield).

Congratulations on completing the working running total design! This design is not only functional, but also quite robust, with all the proper precautions taken to clean up the external signals. You can try to connect io_button[4] to edge.in directly to see what kind of bad behavior you get when you use the external input directly. In my testing, the button press would be ignored about every third press. This obviously isn’t an issue of bouncing (which would cause multiple additions per press), so I must be violating timing and causing stability issues. A tricky thing with timing violations is that the behavior it can produce can seem random and unrelated to the input caus‐ ing it (as the instability can propagate).

  • XC4020E-4HQ240I

    Manufacturer:Xilinx

  • FPGA XC4000E Family 20K Gates 1862 Cells 0.35um Technology 5V 240-Pin HSPQFP EP
  • Product Categories: Condensateurs électrolytiques en aluminium

    Lifecycle:Obsolete -

    RoHS: No RoHS

  • XC4VSX25-10FFG668C

    Manufacturer:Xilinx

  • FPGA Virtex-4 SX Family 23040 Cells 90nm Technology 1.2V 668-Pin FCBGA
  • Product Categories: FPGAs

    Lifecycle:Active Active

    RoHS:

  • XC4VSX25-11FFG668C

    Manufacturer:Xilinx

  • FPGA Virtex-4 SX Family 23040 Cells 90nm Technology 1.2V 668-Pin FCBGA
  • Product Categories: FPGAs (Field Programmable Gate Array)

    Lifecycle:Active Active

    RoHS:

  • XC2018-70PC84C

    Manufacturer:Xilinx

  • LOGIC CELL ARRAY Field Programmable Gate Array
  • Product Categories:

    Lifecycle:Obsolete -

    RoHS: No RoHS

  • XC4VSX35-11FFG668C

    Manufacturer:Xilinx

  • FPGA Virtex-4 SX Family 34560 Cells 90nm Technology 1.2V 668-Pin FCBGA
  • Product Categories: FPGAs (Field Programmable Gate Array)

    Lifecycle:Active Active

    RoHS:

Need Help?

Support

If you have any questions about the product and related issues, Please contact us.