Laboratory Exercise 6 The purpose of this exercise is to investigate latches, flip-flops, and counters. Part I Altera FPGAs include flip-flops that are available for implementing a user s circuit. We will show how to make use of these flip-flops in Parts IV to VII of this exercise. But first we will show how storage elements can be created in an FPGA without using its dedicated flip-flops. Figure 1 depicts a gated latch circuit. A style of VHL code that uses logic expressions to describe this circuit is given in Figure 2. If this latch is implemented in an FPGA that has 4-input lookup tables (LUTs), then only one lookup table is needed, as shown in Figure 3a. _g a () _g b Figure 1. A gated latch circuit. - - A gated latch desribed the hard way LIBAY ieee; UE ieee.std logic 1164.all; ENTITY part1 I POT (,, : IN T LOGIC; : OUT T LOGIC); EN part1; ACHITECTUE tructural OF part1 I IGNAL g, g, a, b : T LOGIC ; ATTIBUTE keep : boolean; ATTIBUTE keep of g, g, a, b : IGNAL I true; BEGIN g <= AN ; g <= AN ; a <=NOT(g O b); b <=NOT(g O a); <= a; EN tructural; Figure 2. pecifying the latch by using logic expressions. 1
Although the latch can be correctly realized in one 4-input LUT, this implementation does not allow its internal signals, such as g and g, to be observed, because they are not provided as outputs from the LUT. To preserve these internal signals in the implemented circuit, it is necessary to include a compiler directive in the code. In Figure 2 the directive keep is included by using a VHL ATTIBUTE statement; it instructs the uartus II compiler to use separate logic elements for each of the signals g, g,a, and b. Compiling the code produces the circuit with four s depicted in Figure 3b. a () (a) Using one 4-input lookup table for the latch. _g a () _g b (b) Using four 4-input lookup tables for the latch. Figure 3. Implementation of the latch from Figure 1. Create a uartus II project for the latch circuit as follows. 1. Create a new project for the latch. elect as the target chip the Cyclone II EP2C35F672C6, which is the FPGA chip on the Altera E2 board. 2. Generate a VHL file with the code in Figure 2 and include it in the project. 3. Compile the code. Use the uartus II TL Viewer tool to examine the gate-level circuit produced from the code, and use the Technology Viewer tool to verify that the latch is implemented as shown in Figure 3b. 4. Create a Vector Waveform File (.vwf) which specifies the inputs and outputs of the circuit. raw waveforms for the and inputs and use the imulator to produce the corresponding waveforms for g, g,a, and b. Verify that the latch works as expected using both functional and timing simulation. Part II Figure 4 shows the circuit for a gated latch. 2
_g a () _g b Figure 4. Circuit for a gated latch. Perform the following steps: 1. Create a new uartus II project. Generate a VHL file using the style of code in Figure 2 for the gated latch. Use the keep directive to ensure that separate logic elements are used to implement the signals, g, g,a, and b. 2. elect as the target chip the Cyclone II EP2C35F672C6 and compile the code. Use the Technology Viewer tool to examine the implemented circuit. 3. Verify that the latch works properly for all input conditions by using functional simulation. Examine the timing characteristics of the circuit by using timing simulation. 4. Create a new uartus II project which will be used for implementation of the gated latch on the E2 board. This project should consist of a top-level entity that contains the appropriate input and output ports (pins) for the E2 board. Instantiate your latch in this top-level entity. Use switch W 0 to drive the input of the latch, and use W 1 as the input. Connect the output to LE 0. 5. Test the functionality of your circuit by toggling the and switches and observing the output. Part III Figure 5 shows the circuit for a master-slave flip-flop. Master lave m s Figure 5. Circuit for a master-slave flip-flop. Perform the following. 1. Create a new uartus II project. Generate a VHL file that instantiates two copies of your gated latch entity from Part II to implement the master-slave flip-flop. 3
2. Include in your project the appropriate input and output ports for the Altera E2 board. Use switch W 0 to drive the input of the flip-flop, and use W 1 as the input. Connect the output to LE 0. 3. Use the Technology Viewer to examine the flip-flop circuit, and use simulation to verify its correct operation. 4. ownload the circuit onto the Altera board and test its functionality by toggling the and switches and observing the output. Part IV Figure 6 shows a circuit with three different storage elements: a gated latch, a positive-edge triggered flipflop, and a negative-edge triggered flip-flop. a b c Figure 6. Circuit and waveforms for Part IV. Implement and simulate this circuit using uartus II software as follows: 1. Create a new project. 2. Write a VHL file that instantiates the three storage elements. For this part you should no longer use the keep directive (that is, the VHL ATTIBUTE statement) from Parts I to III. Figure 7 gives a behavioral style of VHL code that specifies the gated latch in Figure 4. This latch can be implemented in one 4-input lookup table. Use a similar style of code to specify the flip-flops in Figure 6. 3. Compile your code and use the Technology Viewer to examine the implemented circuit. Verify that the latch uses one lookup table and that the flip-flops are implemented using the flip-flops provided in the target FPGA. 4
4. Create a Vector Waveform File (.vwf) which specifies the inputs and outputs of the circuit. raw the inputs and as indicated in Figure 6. Use functional simulation to obtain the three output signals. Observe the different behavior of the three storage elements. LIBAY ieee ; UE ieee.std logic 1164.all ; ENTITY latch I POT (, : IN T LOGIC ; : OUT T LOGIC) ; EN latch ; ACHITECTUE Behavior OF latch I BEGIN POCE (, ) BEGIN IF = 1 THEN <=; EN IF ; EN POCE ; EN Behavior ; Part V Figure 7. A behavioral style of VHL code that specifies a gated latch. Consider the circuit in Figure 8. It is a 4-bit synchronous counter which uses four T-type flip-flops. The counter increments its count on each positive edge of the clock if the Enable signal is asserted. The counter is reset to 0 by using the eset signal. You are to implement a 16-bit counter of this type. Enable T T T T eset Figure 8. A 4-bit counter. 1. Write a VHL file that defines a 16-bit counter by using the structure depicted in Figure 8, and compile the circuit. How many logic elements (LEs) are used to implement your circuit? What is the maximum frequency, Fmax, at which your circuit can be operated? 2. imulate your circuit to verify its correctness. 3. Augment your VHL file to use the pushbutton KEY 0 as the input, switches W 1 and W 0 as Enable and eset inputs, and 7-segment displays HEX3-0 to display the hexadecimal count as your circuit operates. Make the necessary pin assignments and compile the circuit. 4. Implement your circuit on the E2 board and test its functionality by operating the implemented switches. 5
5. Implement a 4-bit version of your circuit and use the uartus II TL Viewer to see how uartus II software synthesized your circuit. What are the differences in comparison with Figure 8? Part VI implify your VHL code so that the counter specification is based on the VHL statement: <= +1; Compile a 16-bit version of this counter and compare the number of LEs needed and the Fmax that is attainable. Use the TL Viewer to see the structure of this implementation and comment on the differences with the design from Part V. Part VII Use an LPM from the Library of Parameterized modules to implement a 16-bit counter. Choose the LPM options to be consistent with the above design, i. e. with enable and synchronous clear. How does this version compare with the previous designs? Copyright c 2005 Altera Corporation. 6