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 |
73 |
zero_gravi |
-- # Register zero (x0) is a "normal" physical register that should be initialized to zero by #
|
8 |
|
|
-- # the early boot code. However, it is always set to zero when written. #
|
9 |
45 |
zero_gravi |
-- # #
|
10 |
|
|
-- # The register file uses synchronous read accesses and a *single* (multiplexed) address port #
|
11 |
65 |
zero_gravi |
-- # for writing and reading rd/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 |
73 |
zero_gravi |
-- # Copyright (c) 2022, 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 |
73 |
zero_gravi |
alu_i : in std_ulogic_vector(data_width_c-1 downto 0); -- ALU result
|
62 |
2 |
zero_gravi |
mem_i : in std_ulogic_vector(data_width_c-1 downto 0); -- memory read data
|
63 |
73 |
zero_gravi |
csr_i : in std_ulogic_vector(data_width_c-1 downto 0); -- CSR read data
|
64 |
|
|
pc2_i : in std_ulogic_vector(data_width_c-1 downto 0); -- next PC
|
65 |
2 |
zero_gravi |
-- data output --
|
66 |
|
|
rs1_o : out std_ulogic_vector(data_width_c-1 downto 0); -- operand 1
|
67 |
65 |
zero_gravi |
rs2_o : out std_ulogic_vector(data_width_c-1 downto 0) -- operand 2
|
68 |
2 |
zero_gravi |
);
|
69 |
|
|
end neorv32_cpu_regfile;
|
70 |
|
|
|
71 |
|
|
architecture neorv32_cpu_regfile_rtl of neorv32_cpu_regfile is
|
72 |
|
|
|
73 |
|
|
-- register file --
|
74 |
|
|
type reg_file_t is array (31 downto 0) of std_ulogic_vector(data_width_c-1 downto 0);
|
75 |
|
|
type reg_file_emb_t is array (15 downto 0) of std_ulogic_vector(data_width_c-1 downto 0);
|
76 |
49 |
zero_gravi |
signal reg_file : reg_file_t;
|
77 |
|
|
signal reg_file_emb : reg_file_emb_t;
|
78 |
|
|
signal rf_wdata : std_ulogic_vector(data_width_c-1 downto 0); -- actual write-back data
|
79 |
73 |
zero_gravi |
signal rd_is_x0 : std_ulogic; -- writing to x0?
|
80 |
49 |
zero_gravi |
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 |
2 |
zero_gravi |
|
83 |
|
|
begin
|
84 |
|
|
|
85 |
45 |
zero_gravi |
-- Data Input Mux -------------------------------------------------------------------------
|
86 |
2 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
87 |
73 |
zero_gravi |
input_mux: process(rd_is_x0, ctrl_i, alu_i, mem_i, csr_i, pc2_i)
|
88 |
62 |
zero_gravi |
begin
|
89 |
73 |
zero_gravi |
if (rd_is_x0 = '1') then -- write zero if accessing x0 to "emulate" it is hardwired to zero
|
90 |
|
|
rf_wdata <= (others => '0'); -- TODO: FIXME! but how???
|
91 |
62 |
zero_gravi |
else
|
92 |
73 |
zero_gravi |
case ctrl_i(ctrl_rf_mux1_c downto ctrl_rf_mux0_c) is
|
93 |
|
|
when rf_mux_alu_c => rf_wdata <= alu_i; -- ALU result
|
94 |
|
|
when rf_mux_mem_c => rf_wdata <= mem_i; -- memory read data
|
95 |
|
|
when rf_mux_csr_c => rf_wdata <= csr_i; -- CSR read data
|
96 |
|
|
when rf_mux_npc_c => rf_wdata <= pc2_i; -- next PC (branch return/link address)
|
97 |
|
|
when others => rf_wdata <= alu_i;
|
98 |
|
|
end case;
|
99 |
62 |
zero_gravi |
end if;
|
100 |
|
|
end process input_mux;
|
101 |
45 |
zero_gravi |
|
102 |
|
|
|
103 |
|
|
-- Register File Access -------------------------------------------------------------------
|
104 |
|
|
-- -------------------------------------------------------------------------------------------
|
105 |
73 |
zero_gravi |
reg_file_rv32i: -- normal register file with 32 registers
|
106 |
|
|
if (CPU_EXTENSION_RISCV_E = false) generate
|
107 |
|
|
rf_access: process(clk_i)
|
108 |
|
|
begin
|
109 |
|
|
if rising_edge(clk_i) then -- sync read and write
|
110 |
62 |
zero_gravi |
if (ctrl_i(ctrl_rf_wb_en_c) = '1') then
|
111 |
49 |
zero_gravi |
reg_file(to_integer(unsigned(opa_addr(4 downto 0)))) <= rf_wdata;
|
112 |
2 |
zero_gravi |
end if;
|
113 |
73 |
zero_gravi |
rs1_o <= reg_file(to_integer(unsigned(opa_addr(4 downto 0))));
|
114 |
|
|
rs2_o <= reg_file(to_integer(unsigned(opb_addr(4 downto 0))));
|
115 |
|
|
end if;
|
116 |
|
|
end process rf_access;
|
117 |
|
|
|
118 |
|
|
-- writing to x0? --
|
119 |
|
|
rd_is_x0 <= not or_reduce_f(ctrl_i(ctrl_rf_rd_adr4_c downto ctrl_rf_rd_adr0_c));
|
120 |
|
|
end generate;
|
121 |
|
|
|
122 |
|
|
reg_file_rv32e: -- embedded register file with 16 registers
|
123 |
|
|
if (CPU_EXTENSION_RISCV_E = true) generate
|
124 |
|
|
rf_access: process(clk_i)
|
125 |
|
|
begin
|
126 |
|
|
if rising_edge(clk_i) then -- sync read and write
|
127 |
62 |
zero_gravi |
if (ctrl_i(ctrl_rf_wb_en_c) = '1') then
|
128 |
49 |
zero_gravi |
reg_file_emb(to_integer(unsigned(opa_addr(3 downto 0)))) <= rf_wdata;
|
129 |
2 |
zero_gravi |
end if;
|
130 |
73 |
zero_gravi |
rs1_o <= reg_file_emb(to_integer(unsigned(opa_addr(3 downto 0))));
|
131 |
|
|
rs2_o <= reg_file_emb(to_integer(unsigned(opb_addr(3 downto 0))));
|
132 |
2 |
zero_gravi |
end if;
|
133 |
73 |
zero_gravi |
end process rf_access;
|
134 |
2 |
zero_gravi |
|
135 |
73 |
zero_gravi |
-- writing to x0? --
|
136 |
|
|
rd_is_x0 <= not or_reduce_f(ctrl_i(ctrl_rf_rd_adr3_c downto ctrl_rf_rd_adr0_c));
|
137 |
|
|
end generate;
|
138 |
|
|
|
139 |
58 |
zero_gravi |
-- access addresses --
|
140 |
73 |
zero_gravi |
opa_addr <= ctrl_i(ctrl_rf_rd_adr4_c downto ctrl_rf_rd_adr0_c) when (ctrl_i(ctrl_rf_wb_en_c) = '1') else
|
141 |
|
|
ctrl_i(ctrl_rf_rs1_adr4_c downto ctrl_rf_rs1_adr0_c); -- rd/rs1
|
142 |
45 |
zero_gravi |
opb_addr <= ctrl_i(ctrl_rf_rs2_adr4_c downto ctrl_rf_rs2_adr0_c); -- rs2
|
143 |
39 |
zero_gravi |
|
144 |
|
|
|
145 |
2 |
zero_gravi |
end neorv32_cpu_regfile_rtl;
|