4. Field Programmable Gate Arrays

It’s time to get our hands on some actual hardware 📟 and put our new logic-design skills to the test. Most hardware components we use day-to-day have a fixed design. This means the gates and their wiring are hardwired. In other words, the design and purpose of such a device remain the same throughout its operating life. For example, suppose you buy a processor. This could be the Broadcom BCM2712 used in the Raspberry Pi 5 which we’ll use as our development platform when we discuss the Arm architecture. We can use this processor by running our software on it. Software consists of instructions that the processor executes. However, we cannot change the design of the processor itself.

In contrast, field-programmable gate arrays (FPGAs) allow us to (re)configure or “program” the logic fabric itself. This means that we can decide which logic gates to use and how they should be connected. The same manufactured FPGA could, for example, implement a tailored microprocessor one day and act as a graphics processing unit the next. In fact, FPGAs are often used as a primary platform for prototyping processor designs. For example, the RISC-V Exchange lists entire CPU-core IP blocks, including open-source options.

In this section we’ll use the AMD Urbana board, which is built around AMD’s Spartan-7 FPGA. Additional information is available through the Urbana Board Reference Manual. We will use the open source toolchain openXC7 to configure the board.

Hint

Compile all your modules with Icarus Verilog using flags -Wall -Winfloop -g2012 before programming an FPGA. Additionally, write testbenches and run simulations before using openXC7 to program the board.

4.1. RTL Modeling

In Section 3 we specified our hardware modules with elementary logic gates. Given an abstract description of a function, we had to manually lower it to logic gates. This process is cumbersome, error-prone and time-consuming, especially when aiming for an optimized circuit that may have multiple viable realizations. From now on, we’ll harness more powerful SystemVerilog constructs under the umbrella of register-transfer-level (RTL) modeling. This lets us write more abstract code and use a synthesis tool to generate the gate-level netlist. Even though RTL modeling greatly simplifies our work, we should keep in mind the gate-level results that synthesis will produce.

We discuss RTL modeling in class. All details can be found in the IEEE Standard for SystemVerilog. Further, Ch. 4 of the SystemVerilog Guide from Harvard’s CS141 course provides a gentler introduction.

4.2. 7-Segment Display Decoder

../_images/decoder_0010.jpg

Fig. 4.2.1 Picture of the finished 7-segment display decoder where only switch SW1 is active. The decoder shows the hexadecimal value selected by switches SW3, SW2, SW1 and SW0 on display 0, the rightmost display. Thus, 0010 is the visualized binary number which corresponds to the shown hex number 2.

Let’s start our FPGA journey by designing a decoder that connects some of the board’s switches to a hexadecimal display. For this we’ll use the first four switches SW3, SW2, SW1, and SW0. Each switch can either be 0 (down) or 1 (up). We may represent a hexadecimal number alternatively through exactly four binary digits. Thus, in our design, the configuration of the four switches will encode a single hexadecimal number.

The board has two four-digit display modules, labelled DISP1 and DISP2. We number the individual seven-segment displays from right to left, starting at display 0. Each display contains seven segments. In this task, we’ll use only display 0, the rightmost display. The segments of the display are active low. Drive a segment high (1) to turn it off, low (0) to turn it on.

../_images/display.svg

Fig. 4.2.2 Illustration of the hexadecimal digits 0-F when shown on a seven-segment display of the Urbana board. The numbers show segment indices.

Fig. 4.2.2 illustrates all sixteen outputs our decoder might have. Our decoder takes a four-bit binary number, i_binary_number[3:0], and produces seven output bits, o_display[6:0]. Each output bit controls the segment with the same index in Fig. 4.2.2. The corresponding (incomplete) truth table for our decoder is given in Table 4.2.1.

Table 4.2.1 Truth table of the decoder.

Hex Number

i_binary_number[3:0]

o_display[6:0]

0

4'b0000

7'b100_0000

1

4'b0001

7'b111_1001

2

4'b0010

7'b010_0100

3

4'b0011

7'b011_0000

4

4'b0100

7'b???_????

5

4'b0101

7'b???_????

6

4'b0110

7'b???_????

7

4'b0111

7'b???_????

8

4'b1000

7'b???_????

9

4'b1001

7'b???_????

A

4'b1010

7'b???_????

B

4'b1011

7'b???_????

C

4'b1100

7'b???_????

D

4'b1101

7'b???_????

E

4'b1110

7'b???_????

F

4'b1111

7'b???_????

To show the hexadecimal digit 0, segments 0–5 must be on and segment 6 must be off. Thus, we set bits 0–5 to 0 and bit 6 to 1. This gives o_display[6:0] = 7'b100_0000. To show the hexadecimal digit 1, only segments 1 and 2 must be on. We therefore set bits 1 and 2 to 0 and the remaining segment bits to 1, giving o_display[6:0] = 7'b111_1001.

Once again, to kickstart your work, templates for your modules are provided. The template for the decoder module is located in Listing 4.2.1. This template only provides the decoding cases for the two binary numbers \(0000_2\) and \(0001_2\). The remaining 14 numbers shown in Table 4.2.1 have to be added still.

Listing 4.2.1 Template for the module decoder.
 1/**
 2 * Decoder for a seven-segment display.
 3 *
 4 * @param i_binary_number input binary number.
 5 * @param o_display output bits driving the seven-segment display.
 6 **/
 7module decoder( input  logic [3:0] i_binary_number,
 8                output logic [6:0] o_display );
 9  always_comb
10  begin
11    case( i_binary_number )
12      4'b0000: o_display[6:0] = 7'b100_0000; // 0
13      4'b0001: o_display[6:0] = 7'b111_1001; // 1
14      // TODO: add remaining hex digits 2-F
15      default: o_display[6:0] = 7'bxxx_xxxx;
16    endcase
17  end
18endmodule

Listing 4.2.2 provides an incomplete template for the top-level module decoder_top. Complete it by connecting the switch inputs SW[3:0] to the decoder input i_binary_number[3:0] and the decoder output o_display[6:0] to the board’s segment signals D1_SEG[6:0].

Listing 4.2.2 Template for the module decoder_top.
 1/**
 2 * Top level module for the urbana board
 3 * The decoder shows the binary number represented
 4 * by SW3-SW0 on display 0 as a hex number.
 5 * 
 6 * @param SW switch signals
 7 * @param D1_SEG active-low segments [6:0] and decimal point [7] for displays 0-3.
 8 * @param D0_AN active-low display-enable signals for displays 4-7.
 9 * @param D1_AN active-low display-enable signals for displays 0-3.
10 **/
11module decoder_top( input  logic [3:0] SW,
12                    output logic [7:0] D1_SEG,
13                    output logic [3:0] D0_AN,
14                    output logic [3:0] D1_AN );
15
16  // TODO: instantiate your decoder and set the display controls.
17endmodule
The two display modules with display IDs 7 to 0 from left to right. D0_AN bits 3 to 0 select displays 7 to 4, and D1_AN bits 3 to 0 select displays 3 to 0. The example selects display 0 with D1_AN = 4'b1110.

Fig. 4.2.3 Display IDs and display-enable signals on the Urbana board, viewed from the front with the slide switches below the display modules.

The display-enable signals D0_AN[3:0] and D1_AN[3:0] shown in Fig. 4.2.3 are active low. D1_AN controls displays 0–3, and D0_AN controls displays 4–7. Within each module, bit 3 selects the leftmost display and bit 0 the rightmost display. For this exercise, set D1_AN = 4'b1110 to select display 0 and keep displays 1–3 blank. Set D0_AN = 4'b1111 to keep displays 4–7 blank. D1_SEG[7] drives the decimal point. Set it to 1 to keep the point off.

An XDC (Xilinx Design Constraints) file is a text file containing Tcl-based commands that specify physical and timing constraints. It maps the ports of our top-level SystemVerilog module to physical FPGA pins using PACKAGE_PIN and selects their electrical I/O standards using IOSTANDARD. Port names in get_ports must match those in the top-level module. An initial constraints-file template is given in Listing 4.2.3. For example, line 2 maps the top-level port SW[0] to FPGA package pin G1. Additional signal names are available from the Urbana Board Reference Manual.

Listing 4.2.3 Template for the decoder.xdc script.
 1# Slide switches SW3..SW0
 2set_property -dict {PACKAGE_PIN G1 IOSTANDARD LVCMOS25} [get_ports {SW[0]}]
 3# TODO: add more switches
 4
 5# 7-segment display 0-3
 6set_property -dict {PACKAGE_PIN F3 IOSTANDARD LVCMOS25} [get_ports {D1_SEG[0]}]
 7# TODO: add remaining ports
 8
 9# 7-segment anode 0/1
10set_property -dict {PACKAGE_PIN B3 IOSTANDARD LVCMOS25} [get_ports {D0_AN[0]}]
11# TODO: add D0_AN 1-3
12
13set_property -dict {PACKAGE_PIN H5 IOSTANDARD LVCMOS25} [get_ports {D1_AN[0]}]
14# TODO: add D1_AN 1-3

Tasks

  1. Add the missing encodings for the seven-segment display to Table 4.2.1.

  2. Implement the module decoder. Use the template in Listing 4.2.1.

  3. Finish the implementation of the module decoder_top and the decoder.xdc script. Use Listing 4.2.2 and Listing 4.2.3 as a template.

  4. Compile your modules using the openXC7 build files and program the FPGA on the Urbana board.

  5. Make sure that the board shows the correct hexadecimal numbers in display 0 for all combinations of the switches SW0, SW1, SW2, SW3. Provide three pictures of the programmed FPGA for the hex numbers 7, B and E. One should see the hex display and the switches in your pictures. An example is given in Fig. 4.2.1.

4.3. Tiny Calculator

Now that we have a way to display four-bit binary numbers, let’s take it up a notch. In this task we’ll implement a tiny calculator that takes two four-bit binary numbers \(A_{[3:0]}\) and \(B_{[3:0]}\) as inputs. We map the first four switches, SW[3:0], to \(A_{[3:0]}\) and the next four, SW[7:4], to \(B_{[3:0]}\). Our tiny calculator computes the five-bit result \(C_{[4:0]} = A_{[3:0]} + B_{[3:0]}\). The result \(C_{[4:0]}\) has five bits due to the carry-out. Thus, we use two hex digits for \(C_{[4:0]}\): one for the four bits [3:0] and one for the carry-out [4].

So far we have only used display 0. All displays of a display module share the same segment signals: D0_SEG[7:0] drives displays 4–7 and D1_SEG[7:0] drives displays 0–3. If we enable several displays of the same module, all of them show the same pattern. However, the two modules are independent. This means that we can show two different digits at the same time by using one display per module. We use display 4 (enabled through D0_AN[0]) and display 3 (enabled through D1_AN[3]), the two displays in the middle of the board.

Two displays are not enough to show \(A_{[3:0]}\), \(B_{[3:0]}\) and \(C_{[4:0]}\) at once. Therefore, switch SW15 selects what the two displays show:

  • SW15 down (0): display 4 shows \(B_{[3:0]}\) and display 3 shows \(A_{[3:0]}\).

  • SW15 up (1): display 4 shows the carry-out \(C_{[4]}\) and display 3 shows \(C_{[3:0]}\).

For example, setting \(A_{[3:0]} = 0011_2\) and \(B_{[3:0]} = 0010_2\) shows 23 on displays 4 and 3 if SW15 is down. Flipping SW15 up shows the result \(C_{[4:0]} = 00101_2 = 05_{16}\) as 05. An example is shown in Fig. 4.3.1 and Fig. 4.3.2.

../_images/calculator.jpg

Fig. 4.3.1 Picture of the tiny calculator showing its inputs. Only switches SW0, SW1 and SW5 are up (1). This means \(A_{[3:0]} = 0011_2\) and \(B_{[3:0]} = 0010_2\), shown as 2 and 3.

../_images/calculator_result.jpg

Fig. 4.3.2 Same configuration as in Fig. 4.3.1 but with SW15 up (1). The displays now show the result \(C_{[4:0]} = 00101_2\) as 05.

Table 4.3.1 Inputs and expected segment patterns (incomplete) for testing tiny_calculator.

SW[3:0]

SW[7:4]

SW15 = 0

SW15 = 1

Display 4

Display 3

Display 4

Display 3

D0_SEG[6:0]

D1_SEG[6:0]

D0_SEG[6:0]

D1_SEG[6:0]

0000

0000

100_0000

100_0000

100_0000

100_0000

0011

0010

???_????

???_????

???_????

???_????

0111

0001

???_????

???_????

???_????

???_????

1000

1000

???_????

???_????

???_????

???_????

1111

1111

???_????

???_????

???_????

???_????

Listing 4.3.1 Template for the module tiny_calculator.
 1/**
 2 * Top-level entry of the tiny calculator.
 3 * The calculator adds the 4-bit binary numbers in SW[3:0] and SW[7:4].
 4 * Switch SW15 selects what displays 4 and 3 show:
 5 *   SW15 = 0: display 4 shows input B (SW[7:4]), display 3 shows input A (SW[3:0]).
 6 *   SW15 = 1: display 4 shows the carry-out, display 3 shows the lower four bits of the sum.
 7 *
 8 * @param SW bits of switches SW7, SW6, SW5, SW4, SW3, SW2, SW1 and SW0.
 9 * @param SW_VIEW bit of switch SW15 which selects between inputs and result.
10 * @param D0_SEG active-low segments [6:0] and decimal point [7] for displays 4-7.
11 * @param D1_SEG active-low segments [6:0] and decimal point [7] for displays 0-3.
12 * @param D0_AN active-low display-enable signals for displays 4-7.
13 * @param D1_AN active-low display-enable signals for displays 0-3.
14 **/
15module tiny_calculator( input  logic [7:0]   SW,
16                        input  logic [15:15] SW_VIEW,
17                        output logic [7:0]   D0_SEG,
18                        output logic [7:0]   D1_SEG,
19                        output logic [3:0]   D0_AN,
20                        output logic [3:0]   D1_AN );
21  
22  // TODO: use your adder and connect the inputs and outputs
23
24endmodule

Tasks

  1. Implement tiny_calculator using Listing 4.3.1 and your four-bit ripple-carry adder from Section 3.3.

  2. Complete Table 4.3.1. Write tiny_calculator_tb and check displays 4 and 3 for both settings of SW15 for each input configuration.

  3. Adapt the build settings and constraints for tiny_calculator. Compile using openXC7 and program the FPGA on the Urbana board.

  4. Verify the five configurations in Table 4.3.1 on the board. Provide two pictures of each configuration showing the displays and switches: one with SW15 down and one with SW15 up.