Register File

  1. Introduction

    Register File is a memory space present within the CPU. It is used by the CPU to fetch and hold the data from the secondary memory devices. It is faster compared to other memory devices as it is present within the processor. In RISC cores the register file is larger in size compared to CISC. Register file can be a static random access memory. Within these SRAM there are bits of memory labeled according to a binary code which will specify whether it is active or inactive.

    A Register File Read operation functions as follows:
    • Any value provided on output selection port (4-bit input Sel_o1) is used to select the content of the corresponding register to provide as output on the 32-bit output port Op1 on the positive edge of clock when the read enable switch is high.
    • Any value provided on output selection port (4-bit input Sel_o2) is used to select the content of the corresponding register to provide as output on the 32-bit output port Op2 on the positive edge of clock when the read enable switch is high.

    Figure 1. Block Diagram of Register File Read operation


    A Register File Write operation functions as follows:
    • Any value provided on the input selection port (4-bit input Sel_i1) is used to select the corresponding register into which the value from the 32-bit input port 1 is written to on the positive edge of clock when the write enable switch is high.

    Figure 2. Block Diagram of Register File Write operation

  2. Verilog Module

    Figure 3 presents the Verilog module of the Register File. This Register File can store sixteen 32-bit values. The Register File module consists of a 32-bit data input line, Ip1 and two 32-bit data output lines, Op1 and Op2. The module is clocked using the 1-bit input clock line clk. The module also has a 1-bit enable line, EN and a 1-bit active high reset line, rst.

    The Register File module also has two 4-bit output selection ports Sel_o1 and Sel_o2, and one 4-bit input selection port Sel_i1.

    The 1-bit RD line is used to signal a data read operation on the Register File and the 1-bit WR line is used to signal a data write operation on the Register File. Both the RD and WR lines are active high.

    When the RD line is high and there are valid register numbers in the output selection ports Sel_o1 and Sel_o2 then the Register File outputs the values in the corresponding registers to the output ports Op1 and Op2.

    When the WR line is high and there is a valid register number in the input selection port Sel_i1 then the Register File writes the value on the input port Ip1 to the corresponding register in the Register File.

    Figure 3. Verilog module of the Register File

  3. Verilog Code for Register File (regFile.v)


    1. module regFile( Ip1,
    2. sel_i1,
    3. Op1,
    4. sel_o1,
    5. Op2,
    6. sel_o2,
    7. RD,
    8. WR,
    9. rst,
    10. EN,
    11. clk
    12. );
    13. input [31:0] Ip1;
    14. input [3:0] sel_i1,
    15. sel_o1,
    16. sel_o2;
    17. input RD,
    18. WR;
    19. input EN,
    20. clk,
    21. rst;
    22. output [31:0] Op1,
    23. Op2;
    24. reg [31:0] Op1,
    25. Op2;
    26. reg [31:0] regFile [0:15];
    27. integer i;
    28. wire sen;
    29. assign sen = clk || rst;
    30. always @ (posedge sen)
    31. begin
    32. if (EN == 1)
    33. begin
    34. if (rst == 1) //If at reset
    35. begin
    36. for (i = 0; i < 16; i = i + 1) begin
    37. regFile [i] = 32'h0;
    38. end
    39. Op1 = 32'hx; 
    40. end
    41. else if (rst == 0) //If not at reset
    42. begin
    43. case ({RD,WR})
    44. 2'b00: begin
    45. end
    46. 2'b01: begin //If Write only
    47. regFile [sel_i1] = Ip1;
    48. end
    49. 2'b10: begin //If Read only
    50. Op1 = regFile [sel_o1];
    51. Op2 = regFile [sel_o2];
    52. end
    53. 2'b11: begin //If both active
    54. Op1 = regFile [sel_o1];
    55. Op2 = regFile [sel_o2];
    56. regFile [sel_i1] = Ip1;
    57. end
    58. default: begin //If undefined
    59. end
    60. endcase
    61. end
    62. else
    63. end
    64. else
    65. end
    66. endmodule
    Figure 4. Verilog code for Register File

  4. Verilog Test Bench for Register File (regFile_tb.v)



    1. `timescale 1ns / 1ps
    2. module regFile_tb;
    3. // Inputs
    4. reg [31:0] Ip1;
    5. reg [3:0] sel_i1;
    6. reg [3:0] sel_o1;
    7. reg [3:0] sel_o2;
    8. reg RD;
    9. reg WR;
    10. reg rst;
    11. reg EN;
    12. reg clk;
    13. // Outputs
    14. wire [31:0] Op1;
    15. wire [31:0] Op2;
    16. // Instantiate the Unit Under Test (UUT)
    17. regFile uut (
    18. .Ip1(Ip1),
    19. .sel_i1(sel_i1),
    20. .Op1(Op1),
    21. .sel_o1(sel_o1),
    22. .Op2(Op2),
    23. .sel_o2(sel_o2),
    24. .RD(RD),
    25. .WR(WR),
    26. .rst(rst),
    27. .EN(EN),
    28. .clk(clk)
    29. );
    30. initial begin
    31. // Initialize Inputs
    32. Ip1 = 32'b0;
    33. sel_i1 = 4'b0;
    34. sel_o1 = 4'b0;
    35. sel_o2 = 4'b0;
    36. RD = 1'b0;
    37. WR = 1'b0;
    38. rst = 1'b1;
    39. EN = 1'b0;
    40. clk = 1'b0;
    41. // Wait 100 ns for global reset to finish
    42. #100;
    43. // Add stimulus here
    44. rst = 1'b0;
    45. EN = 1'b1;
    46. #20;
    47. WR = 1'b1;
    48. RD = 1'b0;
    49. Ip1 = 32'habcd_efab;
    50. sel_i1 = 4'h0;
    51. #20;
    52. Ip1 = 32'h0123_4567;
    53. sel_i1 = 4'h1;
    54. #20;
    55. WR = 1'b0;
    56. RD = 1'b1;
    57. sel_o1 = 4'h0;
    58. sel_o2 = 4'h1;
    59. end
    60. always begin
    61. #10;
    62. clk = ~clk;
    63. end
    64. endmodule
    Figure 5. Verilog Test-bench for Register File

  5. Timing Diagram

    Figure 6. Timing diagram of Register File