1 |
2 |
zero_gravi |
-- #################################################################################################
|
2 |
45 |
zero_gravi |
-- # << NEORV32 - CPU General Purpose Data Register File >> #
|
3 |
2 |
zero_gravi |
-- # ********************************************************************************************* #
|
4 |
45 |
zero_gravi |
-- # General purpose data register file. 32 entries (= 1024 bit) for normal mode (RV32I), #
|
5 |
|
|
-- # 16 entries (= 512 bit) for embedded mode (RV32E) when RISC-V "E" extension is enabled. #
|
6 |
|
|
-- # #
|
7 |
|
|
-- # Register zero (r0/x0) is a "normal" physical reg that has to be initialized to zero by the #
|
8 |
|
|
-- # CPU control system. For normal operations register zero cannot be written. #
|
9 |
|
|
-- # #
|
10 |
|
|
-- # The register file uses synchronous read accesses and a *single* (multiplexed) address port #
|
11 |
|
|
-- # for writing and reading rs1 and a single read-only port for rs2. Therefore, the whole #
|
12 |
|
|
-- # register file can be mapped to a single true dual-port block RAM. #
|
13 |
2 |
zero_gravi |
-- # ********************************************************************************************* #
|
14 |
|
|
-- # BSD 3-Clause License #
|
15 |
|
|
-- # #
|
16 |
45 |
zero_gravi |
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved. #
|
17 |
2 |
zero_gravi |
-- # #
|
18 |
|
|
-- # Redistribution and use in source and binary forms, with or without modification, are #
|
19 |
|
|
-- # permitted provided that the following conditions are met: #
|
20 |
|
|
-- # #
|
21 |
|
|
-- # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
22 |
|
|
-- # conditions and the following disclaimer. #
|
23 |
|
|
-- # #
|
24 |
|
|
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
25 |
|
|
-- # conditions and the following disclaimer in the documentation and/or other materials #
|
26 |
|
|
-- # provided with the distribution. #
|
27 |
|
|
-- # #
|
28 |
|
|
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
29 |
|
|
-- # endorse or promote products derived from this software without specific prior written #
|
30 |
|
|
-- # permission. #
|
31 |
|
|
-- # #
|
32 |
|
|
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
33 |
|
|
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
34 |
|
|
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
35 |
|
|
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
36 |
|
|
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
37 |
|
|
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
38 |
|
|
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
39 |
|
|
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
40 |
|
|
-- # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
41 |
|
|
-- # ********************************************************************************************* #
|
42 |
|
|
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
43 |
|
|
-- #################################################################################################
|
44 |
|
|
|
45 |
|
|
library ieee;
|
46 |
|
|
use ieee.std_logic_1164.all;
|
47 |
|
|
use ieee.numeric_std.all;
|
48 |
|
|
|
49 |
|
|
library neorv32;
|
50 |
|
|
use neorv32.neorv32_package.all;
|
51 |
|
|
|
52 |
|
|
entity neorv32_cpu_regfile is
|
53 |
|
|
generic (
|
54 |
62 |
zero_gravi |
CPU_EXTENSION_RISCV_E : boolean -- implement embedded RF extension?
|
55 |
2 |
zero_gravi |
);
|
56 |
|
|
port (
|
57 |
|
|
-- global control --
|
58 |
|
|
clk_i : in std_ulogic; -- global clock, rising edge
|
59 |
|
|
ctrl_i : in std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
|
60 |
|
|
-- data input --
|
61 |
|
|
mem_i : in std_ulogic_vector(data_width_c-1 downto 0); -- memory read data
|
62 |
|
|
alu_i : in std_ulogic_vector(data_width_c-1 downto 0); -- ALU result
|
63 |
|
|
-- data output --
|
64 |
|
|
rs1_o : out std_ulogic_vector(data_width_c-1 downto 0); -- operand 1
|
65 |
47 |
zero_gravi |
rs2_o : out std_ulogic_vector(data_width_c-1 downto 0); -- operand 2
|
66 |
|
|
cmp_o : out std_ulogic_vector(1 downto 0) -- comparator status
|
67 |
2 |
zero_gravi |
);
|
68 |
|
|
end neorv32_cpu_regfile;
|
69 |
|
|
|
70 |
|
|
architecture neorv32_cpu_regfile_rtl of neorv32_cpu_regfile is
|
71 |
|
|
|
72 |
|
|
-- register file --
|
73 |
|
|
type reg_file_t is array (31 downto 0) of std_ulogic_vector(data_width_c-1 downto 0);
|
74 |
|
|
type reg_file_emb_t is array (15 downto 0) of std_ulogic_vector(data_width_c-1 downto 0);
|
75 |
49 |
zero_gravi |
signal reg_file : reg_file_t;
|
76 |
|
|
signal reg_file_emb : reg_file_emb_t;
|
77 |
|
|
signal rf_wdata : std_ulogic_vector(data_width_c-1 downto 0); -- actual write-back data
|
78 |
|
|
signal rd_is_r0 : std_ulogic; -- writing to r0?
|
79 |
|
|
signal dst_addr : std_ulogic_vector(4 downto 0); -- destination address
|
80 |
|
|
signal opa_addr : std_ulogic_vector(4 downto 0); -- rs1/dst address
|
81 |
|
|
signal opb_addr : std_ulogic_vector(4 downto 0); -- rs2 address
|
82 |
62 |
zero_gravi |
signal rs1, rs2 : std_ulogic_vector(data_width_c-1 downto 0); -- read data
|
83 |
2 |
zero_gravi |
|
84 |
47 |
zero_gravi |
-- comparator --
|
85 |
|
|
signal cmp_opx : std_ulogic_vector(data_width_c downto 0);
|
86 |
|
|
signal cmp_opy : std_ulogic_vector(data_width_c downto 0);
|
87 |
|
|
|
88 |
2 |
zero_gravi |
begin
|
89 |
|
|
|
90 |
45 |
zero_gravi |
-- Data Input Mux -------------------------------------------------------------------------
|
91 |
2 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
92 |
62 |
zero_gravi |
input_mux: process(rd_is_r0, ctrl_i, alu_i, mem_i)
|
93 |
|
|
begin
|
94 |
|
|
if (rd_is_r0 = '1') then -- write zero if accessing x0 to "emulate" it is hardwired to zero
|
95 |
|
|
rf_wdata <= (others => '0');
|
96 |
|
|
else
|
97 |
|
|
if (ctrl_i(ctrl_rf_in_mux_c) = '0') then
|
98 |
|
|
rf_wdata <= alu_i;
|
99 |
|
|
else
|
100 |
|
|
rf_wdata <= mem_i;
|
101 |
|
|
end if;
|
102 |
|
|
end if;
|
103 |
|
|
end process input_mux;
|
104 |
45 |
zero_gravi |
|
105 |
62 |
zero_gravi |
-- check if we are writing to x0 --
|
106 |
|
|
rd_is_r0 <= (not or_reduce_f(dst_addr(4 downto 0))) when (CPU_EXTENSION_RISCV_E = false) else (not or_reduce_f(dst_addr(3 downto 0)));
|
107 |
45 |
zero_gravi |
|
108 |
62 |
zero_gravi |
|
109 |
45 |
zero_gravi |
-- Register File Access -------------------------------------------------------------------
|
110 |
|
|
-- -------------------------------------------------------------------------------------------
|
111 |
2 |
zero_gravi |
rf_access: process(clk_i)
|
112 |
|
|
begin
|
113 |
9 |
zero_gravi |
if rising_edge(clk_i) then -- sync read and write
|
114 |
2 |
zero_gravi |
if (CPU_EXTENSION_RISCV_E = false) then -- normal register file with 32 entries
|
115 |
62 |
zero_gravi |
if (ctrl_i(ctrl_rf_wb_en_c) = '1') then
|
116 |
49 |
zero_gravi |
reg_file(to_integer(unsigned(opa_addr(4 downto 0)))) <= rf_wdata;
|
117 |
2 |
zero_gravi |
end if;
|
118 |
47 |
zero_gravi |
rs1 <= reg_file(to_integer(unsigned(opa_addr(4 downto 0))));
|
119 |
|
|
rs2 <= reg_file(to_integer(unsigned(opb_addr(4 downto 0))));
|
120 |
2 |
zero_gravi |
else -- embedded register file with 16 entries
|
121 |
62 |
zero_gravi |
if (ctrl_i(ctrl_rf_wb_en_c) = '1') then
|
122 |
49 |
zero_gravi |
reg_file_emb(to_integer(unsigned(opa_addr(3 downto 0)))) <= rf_wdata;
|
123 |
2 |
zero_gravi |
end if;
|
124 |
47 |
zero_gravi |
rs1 <= reg_file_emb(to_integer(unsigned(opa_addr(3 downto 0))));
|
125 |
|
|
rs2 <= reg_file_emb(to_integer(unsigned(opb_addr(3 downto 0))));
|
126 |
2 |
zero_gravi |
end if;
|
127 |
|
|
end if;
|
128 |
|
|
end process rf_access;
|
129 |
|
|
|
130 |
58 |
zero_gravi |
-- access addresses --
|
131 |
62 |
zero_gravi |
dst_addr <= ctrl_i(ctrl_rf_rd_adr4_c downto ctrl_rf_rd_adr0_c);
|
132 |
|
|
opa_addr <= dst_addr when (ctrl_i(ctrl_rf_wb_en_c) = '1') else ctrl_i(ctrl_rf_rs1_adr4_c downto ctrl_rf_rs1_adr0_c); -- rd/rs1
|
133 |
45 |
zero_gravi |
opb_addr <= ctrl_i(ctrl_rf_rs2_adr4_c downto ctrl_rf_rs2_adr0_c); -- rs2
|
134 |
39 |
zero_gravi |
|
135 |
47 |
zero_gravi |
-- data output --
|
136 |
|
|
rs1_o <= rs1;
|
137 |
|
|
rs2_o <= rs2;
|
138 |
39 |
zero_gravi |
|
139 |
47 |
zero_gravi |
|
140 |
|
|
-- Comparator Unit (for conditional branches) ---------------------------------------------
|
141 |
|
|
-- -------------------------------------------------------------------------------------------
|
142 |
|
|
cmp_opx <= (rs1(rs1'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & rs1;
|
143 |
|
|
cmp_opy <= (rs2(rs2'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & rs2;
|
144 |
|
|
|
145 |
|
|
cmp_o(cmp_equal_c) <= '1' when (rs1 = rs2) else '0';
|
146 |
|
|
cmp_o(cmp_less_c) <= '1' when (signed(cmp_opx) < signed(cmp_opy)) else '0';
|
147 |
|
|
|
148 |
|
|
|
149 |
2 |
zero_gravi |
end neorv32_cpu_regfile_rtl;
|