|
| 1 | + |
| 2 | +// Copyright 2018 ETH Zurich and University of Bologna. |
| 3 | +// Copyright and related rights are licensed under the Solderpad Hardware |
| 4 | +// License, Version 0.51 (the "License"); you may not use this file except in |
| 5 | +// compliance with the License. You may obtain a copy of the License at |
| 6 | +// http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law |
| 7 | +// or agreed to in writing, software, hardware and materials distributed under |
| 8 | +// this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 9 | +// CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 | +// specific language governing permissions and limitations under the License. |
| 11 | + |
| 12 | +//////////////////////////////////////////////////////////////////////////////// |
| 13 | +// Engineer: Francesco Conti - [email protected] // |
| 14 | +// // |
| 15 | +// Additional contributions by: // |
| 16 | +// Michael Gautschi - [email protected] // |
| 17 | +// Davide Schiavone - [email protected] // |
| 18 | +// Noam Gallmann - [email protected] // |
| 19 | +// // |
| 20 | +// Design Name: RISC-V register file // |
| 21 | +// Project Name: RI5CY // |
| 22 | +// Language: SystemVerilog // |
| 23 | +// // |
| 24 | +// Description: Register file with 31x 32 bit wide registers. Register 0 // |
| 25 | +// is fixed to 0. This register file is optimized for FPGAs // |
| 26 | +// featuring distributed RAM-enabled logic cells. // |
| 27 | +// Also supports the fp-register file now if FPU=1 // |
| 28 | +// If PULP_ZFINX is 1, floating point operations take values // |
| 29 | +// from the X register file // |
| 30 | +// // |
| 31 | +//////////////////////////////////////////////////////////////////////////////// |
| 32 | + |
| 33 | +module cv32e40p_register_file |
| 34 | +#( |
| 35 | + parameter ADDR_WIDTH = 5, |
| 36 | + parameter DATA_WIDTH = 32, |
| 37 | + parameter FPU = 0, |
| 38 | + parameter PULP_ZFINX = 0 |
| 39 | +) |
| 40 | +( |
| 41 | + // Clock and Reset |
| 42 | + input logic clk, |
| 43 | + input logic rst_n, |
| 44 | + |
| 45 | + input logic scan_cg_en_i, |
| 46 | + |
| 47 | + //Read port R1 |
| 48 | + input logic [ADDR_WIDTH-1:0] raddr_a_i, |
| 49 | + output logic [DATA_WIDTH-1:0] rdata_a_o, |
| 50 | + |
| 51 | + //Read port R2 |
| 52 | + input logic [ADDR_WIDTH-1:0] raddr_b_i, |
| 53 | + output logic [DATA_WIDTH-1:0] rdata_b_o, |
| 54 | + |
| 55 | + //Read port R3 |
| 56 | + input logic [ADDR_WIDTH-1:0] raddr_c_i, |
| 57 | + output logic [DATA_WIDTH-1:0] rdata_c_o, |
| 58 | + |
| 59 | + // Write port W1 |
| 60 | + input logic [ADDR_WIDTH-1:0] waddr_a_i, |
| 61 | + input logic [DATA_WIDTH-1:0] wdata_a_i, |
| 62 | + input logic we_a_i, |
| 63 | + |
| 64 | + // Write port W2 |
| 65 | + input logic [ADDR_WIDTH-1:0] waddr_b_i, |
| 66 | + input logic [DATA_WIDTH-1:0] wdata_b_i, |
| 67 | + input logic we_b_i |
| 68 | +); |
| 69 | + |
| 70 | + // The register values are stored in two separate RAM blocks each featuring 1 sync-write and |
| 71 | + // 3 async-read ports. A set of 1-bit flip-flops keeps track of which RAM block contains the valid |
| 72 | + // entry for each register. |
| 73 | + // The integer register file occupies adresses 0-31. If enabled, the floating-point registers are |
| 74 | + // located at addresses 32-63. |
| 75 | + |
| 76 | + // number of integer registers |
| 77 | + localparam NUM_WORDS = 2**(ADDR_WIDTH-1); |
| 78 | + // number of floating point registers |
| 79 | + localparam NUM_FP_WORDS = 2**(ADDR_WIDTH-1); |
| 80 | + localparam NUM_TOT_WORDS = FPU ? ( PULP_ZFINX ? NUM_WORDS : NUM_WORDS + NUM_FP_WORDS ) : NUM_WORDS; |
| 81 | + |
| 82 | + // integer and floating-point register file |
| 83 | + // distributed RAM blocks |
| 84 | + logic [DATA_WIDTH-1:0] mem_a [NUM_TOT_WORDS]; |
| 85 | + logic [DATA_WIDTH-1:0] mem_b [NUM_TOT_WORDS]; |
| 86 | + |
| 87 | + // distributed RAM block selectors |
| 88 | + logic [NUM_TOT_WORDS-1:0] mem_block_sel; |
| 89 | + logic [NUM_TOT_WORDS-1:0] mem_block_sel_q; |
| 90 | + |
| 91 | + // write enable signals for all registers |
| 92 | + logic [NUM_TOT_WORDS-1:0] we_a_dec; |
| 93 | + logic [NUM_TOT_WORDS-1:0] we_b_dec; |
| 94 | + |
| 95 | + //----------------------------------------------------------------------------- |
| 96 | + //-- READ : Read address decoder RAD |
| 97 | + //----------------------------------------------------------------------------- |
| 98 | + |
| 99 | + // Read from the block corresponding to the write port that last wrote to the corresponding |
| 100 | + // address. |
| 101 | + if (FPU == 1 && PULP_ZFINX == 0) begin |
| 102 | + assign rdata_a_o = (raddr_a_i == '0) ? '0 : |
| 103 | + mem_block_sel_q[raddr_a_i[5:0]] ? mem_b[raddr_a_i[5:0]] : mem_a[raddr_a_i[5:0]]; |
| 104 | + assign rdata_b_o = (raddr_b_i == '0) ? '0 : |
| 105 | + mem_block_sel_q[raddr_b_i[5:0]] ? mem_b[raddr_b_i[5:0]] : mem_a[raddr_b_i[5:0]]; |
| 106 | + assign rdata_c_o = (raddr_c_i == '0) ? '0 : |
| 107 | + mem_block_sel_q[raddr_c_i[5:0]] ? mem_b[raddr_c_i[5:0]] : mem_a[raddr_c_i[5:0]]; |
| 108 | + end else begin |
| 109 | + assign rdata_a_o = (raddr_a_i == '0) ? '0 : |
| 110 | + mem_block_sel_q[raddr_a_i[4:0]] ? mem_b[raddr_a_i[4:0]] : mem_a[raddr_a_i[4:0]]; |
| 111 | + assign rdata_b_o = (raddr_b_i == '0) ? '0 : |
| 112 | + mem_block_sel_q[raddr_b_i[4:0]] ? mem_b[raddr_b_i[4:0]] : mem_a[raddr_b_i[4:0]]; |
| 113 | + assign rdata_c_o = (raddr_c_i == '0) ? '0 : |
| 114 | + mem_block_sel_q[raddr_c_i[4:0]] ? mem_b[raddr_c_i[4:0]] : mem_a[raddr_c_i[4:0]]; |
| 115 | + end |
| 116 | + |
| 117 | + //----------------------------------------------------------------------------- |
| 118 | + //-- WRITE : Write Address Decoder (WAD) |
| 119 | + //----------------------------------------------------------------------------- |
| 120 | + |
| 121 | + always_comb begin : we_a_decoder |
| 122 | + for (int i = 0; i < NUM_TOT_WORDS; i++) begin |
| 123 | + if (waddr_a_i == i) begin |
| 124 | + we_a_dec[i] = we_a_i; |
| 125 | + end else begin |
| 126 | + we_a_dec[i] = 1'b0; |
| 127 | + end |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + always_comb begin : we_b_decoder |
| 132 | + for (int i=0; i<NUM_TOT_WORDS; i++) begin |
| 133 | + if (waddr_b_i == i) begin |
| 134 | + we_b_dec[i] = we_b_i; |
| 135 | + end else begin |
| 136 | + we_b_dec[i] = 1'b0; |
| 137 | + end |
| 138 | + end |
| 139 | + end |
| 140 | + |
| 141 | + // update block selector: |
| 142 | + // signal mem_block_sel records where the current valid value is stored. |
| 143 | + // if port a and b try to write to the same address simultaneously, write port b has priority. |
| 144 | + always_comb begin |
| 145 | + mem_block_sel[0] = '0; |
| 146 | + for (int i = 1; i<NUM_TOT_WORDS; i++) begin |
| 147 | + if (we_b_dec[i] == 1'b1) begin |
| 148 | + mem_block_sel[i] = 1'b1; |
| 149 | + end else if (we_a_dec[i] == 1'b1) begin |
| 150 | + mem_block_sel[i] = 1'b0; |
| 151 | + end else begin |
| 152 | + mem_block_sel[i] = mem_block_sel_q[i]; |
| 153 | + end |
| 154 | + end |
| 155 | + end |
| 156 | + |
| 157 | + // block selector flops |
| 158 | + always_ff @(posedge clk) begin |
| 159 | + if (rst_n == 1'b0) begin |
| 160 | + mem_block_sel_q <= '0; |
| 161 | + end else begin |
| 162 | + mem_block_sel_q <= mem_block_sel; |
| 163 | + end |
| 164 | + end |
| 165 | + |
| 166 | + always_ff @(posedge clk) begin : regs_a |
| 167 | + if(we_a_i) begin |
| 168 | + mem_a[waddr_a_i] <= wdata_a_i; |
| 169 | + end |
| 170 | + end |
| 171 | + |
| 172 | + always_ff @(posedge clk) begin : regs_b |
| 173 | + if(we_b_i) begin |
| 174 | + mem_b[waddr_b_i] <= wdata_b_i; |
| 175 | + end |
| 176 | + end |
| 177 | + |
| 178 | +endmodule |
0 commit comments