6. Register File

This lab implements a register file in SystemVerilog and brings the design to the Urbana board. SystemVerilog has built-in features that let us implement whole registers in a few lines of code.

Listing 6.1 Module register_en which implements an enabled 64-bit register.
1module register_en( input  logic        i_clk,
2                    input  logic        i_en,
3                    input  logic [63:0] i_d,
4                    output logic [63:0] o_q );
5  always_ff @(posedge i_clk)
6  begin
7    if(i_en) o_q <= i_d;
8  end
9endmodule

For example, Listing 6.1 shows an enabled 64-bit register. On the rising edge of i_clk, the register sets the internal 64-bit state to i_d when i_en is asserted. Otherwise, the register holds its state and drives o_q. These features accelerate development in production settings. However, we aim for end-to-end understanding with a bottom-up approach. So we build the register file from single-bit SR latches.

../_images/register_file_4.svg

Fig. 6.1 Register file with two read ports and one write port. The register file contains four 4-bit registers.

Fig. 6.1 shows a register file with four internal 4-bit registers. External logic reads through two ports and writes through one port. An interactive implementation of the targeted register file is available on CircuitVerse.

6.1. Building Blocks

From the provided schematic, the building blocks are:

  1. A single 2:4 decoder:

    module decoder_2_4( input  logic [1:0] i_binary,
                        output logic [3:0] o_one_hot );
    
  2. Two 4:1 multiplexers:

    module mux_4( input  logic [1:0] i_s,
                  input  logic [3:0] i_in0,
                  input  logic [3:0] i_in1,
                  input  logic [3:0] i_in2,
                  input  logic [3:0] i_in3,
                  output logic [3:0] o_out );
    
  3. Four enabled 4-bit registers:

    module register_en_4( input  logic       i_clk,
                          input  logic       i_en,
                          input  logic [3:0] i_d,
                          output logic [3:0] o_q );
    

    We implement the register file using SR latches. First, implement a standalone SR latch (module sr_latch). Then use it to build a D latch (module d_latch). Next, two D latches form a rising-edge triggered D flip-flop (module d_flip_flop). From that, derive an enabled flip-flop (module d_flip_flop_en). Four enabled D flip-flops make an enabled 4-bit register (module register_en_4).

Hint

We implemented the modules sr_latch, d_latch and d_flip_flop together with fitting testbenches in the lectures. You may use these implementations in this task.

Tasks

  1. Implement the module decoder_2_4 in decoder_2_4.sv. Test it with the testbench decoder_2_4_tb in decoder_2_4_tb.sv.

  2. Implement the module mux_4 in mux_4.sv. You may reuse code from the ALU lab (see Section 5).

  3. Implement and test the following modules:

    • sr_latch in sr_latch.sv.

    • d_latch in d_latch.sv.

    • d_flip_flop in d_flip_flop.sv.

    • d_flip_flop_en in d_flip_flop_en.sv.

    • register_en_4 in register_en_4.sv.

6.2. Four-bit Register File

This task combines the building blocks into the four-bit register file. We’ll use the following SystemVerilog declaration for the register file:

Listing 6.2.1 SystemVerilog declaration of the four-bit register file.
module register_file_4( input  logic       i_clk,
                        input  logic [1:0] i_reg_read_0,
                        input  logic [1:0] i_reg_read_1,
                        input  logic [1:0] i_reg_write,
                        input  logic [3:0] i_port_write,
                        input  logic       i_write_enable,
                        output logic [3:0] o_port_read_0,
                        output logic [3:0] o_port_read_1 );

An unfinished testbench is provided: register_file_4_tb.sv.

Tasks

  1. Implement the module register_file_4 in the file register_file_4.sv.

  2. Provide the internal register values in the comments of the testbench register_file_4_tb. Use xxxx if the register was never written. Uncomment and finish the assertions in the testbench.

6.3. Display Driver

In Section 4.3 and Section 5 we showed at most two different digits at a time: one on display 4 and one on display 3. This worked without a clock since the two display modules have separate segment signals. Deploying the register file requires six digits. Thus, we have to show different digits on the displays of the same module.

The trick is time multiplexing. At any point in time, the driver enables only one display per module and drives the module’s segment signals with the pattern for this display. Then it moves on to the next display, and so on. If the driver cycles through the displays fast enough, our eyes don’t notice the switching and we see all digits at once.

Our driver divides time into four slots. In slot \(s\), it lights display \(s\) of the module driven by D1_SEG and display \(s+4\) of the module driven by D0_SEG. For example, slot 0 lights displays 0 and 4 by setting D1_AN = 4'b1110 and D0_AN = 4'b1110. A free-running 18-bit counter, clocked by the board’s 100 MHz clock, provides the slot through its two most significant bits. Thus, the driver moves on to the next slot every \(2^{16}\) clock cycles, i.e., every 655.36 µs, and refreshes all displays about 381 times per second. The driver has the following declaration:

Listing 6.3.1 SystemVerilog declaration of the display driver.
module display_driver( input  logic        i_clk,
                       input  logic [31:0] i_values,
                       input  logic [7:0]  i_enable,
                       output logic [7:0]  o_d0_seg,
                       output logic [7:0]  o_d1_seg,
                       output logic [3:0]  o_d0_an,
                       output logic [3:0]  o_d1_an );

i_values holds the eight hex digits: bits [4k+3:4k] belong to display \(k\). For example, display 0 shows i_values[3:0] and display 7 shows i_values[31:28]. Setting i_enable[k] to 1 turns display \(k\) on. Disabled displays stay off, i.e., their display-enable signals are always 1. The outputs connect directly to the board’s signals D0_SEG, D1_SEG, D0_AN and D1_AN. A template is provided: display_driver.sv.

Tasks

  1. Implement the module display_driver in the file display_driver.sv. Use the template and your seven-segment decoder from Section 4.

  2. Test your implementation in the testbench display_driver_tb in the file display_driver_tb.sv. Use a 100 MHz clock. Check all eight displays and at least one configuration in which some of the displays are disabled.

6.4. Register File in Practice

../_images/pic_reg_file.jpeg

Fig. 6.4.1 Picture of the deployed Register File. Shown on the left are the selected registers 3 and 1, which hold the mystery values 4 and 2. The write-port is set to F and is supposed to be written into register 1 when the button is used.

Now deploy the register file. Use the 100 MHz board clock CLK_100MHZ for i_clk. Use the first four switches to select the two read-register IDs. The next two switches select the ID of the write-register. The remaining four switches provide the 4-bit write data. Wire SW[1:0] to i_reg_read_0, SW[3:2] to i_reg_read_1, SW[5:4] to i_reg_write, and SW[9:6] to i_port_write. Use push-button BTN0 as the write-enable and treat high as asserted. An example configuration can be seen in Fig. 6.4.1

Display the read-register IDs on display 0 and 1 and the write-register ID on display 2. Show the write-port data on display 3 and the read-port data on display 6 and 7. Use your display_driver from Section 6.3 and keep displays 4 and 5 off. Lastly, drive LED[0] when the write-enable is asserted. A template for register_file_top is provided: register_file_top.sv.

Tasks

  1. Finish register_file_top using the template and write the corresponding register_file.xdc script.

  2. Program the FPGA of a Urbana board. Demonstrate the design to the teaching personnel. Include a scan of the tailored coversheet in your submission.