1 |
2 |
zero_gravi |
-- #################################################################################################
|
2 |
|
|
-- # << NEORV32 - CPU Register File >> #
|
3 |
|
|
-- # ********************************************************************************************* #
|
4 |
|
|
-- # General purpose data registers. 32 entries for normal mode, 16 entries for embedded mode when #
|
5 |
13 |
zero_gravi |
-- # RISC-V M extension is enabled. x0 output is allways set to zero. #
|
6 |
2 |
zero_gravi |
-- # ********************************************************************************************* #
|
7 |
|
|
-- # BSD 3-Clause License #
|
8 |
|
|
-- # #
|
9 |
|
|
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
10 |
|
|
-- # #
|
11 |
|
|
-- # Redistribution and use in source and binary forms, with or without modification, are #
|
12 |
|
|
-- # permitted provided that the following conditions are met: #
|
13 |
|
|
-- # #
|
14 |
|
|
-- # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
15 |
|
|
-- # conditions and the following disclaimer. #
|
16 |
|
|
-- # #
|
17 |
|
|
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
18 |
|
|
-- # conditions and the following disclaimer in the documentation and/or other materials #
|
19 |
|
|
-- # provided with the distribution. #
|
20 |
|
|
-- # #
|
21 |
|
|
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
22 |
|
|
-- # endorse or promote products derived from this software without specific prior written #
|
23 |
|
|
-- # permission. #
|
24 |
|
|
-- # #
|
25 |
|
|
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
26 |
|
|
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
27 |
|
|
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
28 |
|
|
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
29 |
|
|
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
30 |
|
|
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
31 |
|
|
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
32 |
|
|
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
33 |
|
|
-- # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
34 |
|
|
-- # ********************************************************************************************* #
|
35 |
|
|
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
36 |
|
|
-- #################################################################################################
|
37 |
|
|
|
38 |
|
|
library ieee;
|
39 |
|
|
use ieee.std_logic_1164.all;
|
40 |
|
|
use ieee.numeric_std.all;
|
41 |
|
|
|
42 |
|
|
library neorv32;
|
43 |
|
|
use neorv32.neorv32_package.all;
|
44 |
|
|
|
45 |
|
|
entity neorv32_cpu_regfile is
|
46 |
|
|
generic (
|
47 |
|
|
CPU_EXTENSION_RISCV_E : boolean := false -- implement embedded RF extension?
|
48 |
|
|
);
|
49 |
|
|
port (
|
50 |
|
|
-- global control --
|
51 |
|
|
clk_i : in std_ulogic; -- global clock, rising edge
|
52 |
|
|
ctrl_i : in std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
|
53 |
|
|
-- data input --
|
54 |
|
|
mem_i : in std_ulogic_vector(data_width_c-1 downto 0); -- memory read data
|
55 |
|
|
alu_i : in std_ulogic_vector(data_width_c-1 downto 0); -- ALU result
|
56 |
|
|
csr_i : in std_ulogic_vector(data_width_c-1 downto 0); -- CSR read data
|
57 |
|
|
pc_i : in std_ulogic_vector(data_width_c-1 downto 0); -- current pc
|
58 |
|
|
-- data output --
|
59 |
|
|
rs1_o : out std_ulogic_vector(data_width_c-1 downto 0); -- operand 1
|
60 |
|
|
rs2_o : out std_ulogic_vector(data_width_c-1 downto 0) -- operand 2
|
61 |
|
|
);
|
62 |
|
|
end neorv32_cpu_regfile;
|
63 |
|
|
|
64 |
|
|
architecture neorv32_cpu_regfile_rtl of neorv32_cpu_regfile is
|
65 |
|
|
|
66 |
|
|
-- register file --
|
67 |
|
|
type reg_file_t is array (31 downto 0) of std_ulogic_vector(data_width_c-1 downto 0);
|
68 |
|
|
type reg_file_emb_t is array (15 downto 0) of std_ulogic_vector(data_width_c-1 downto 0);
|
69 |
|
|
signal reg_file : reg_file_t;
|
70 |
|
|
signal reg_file_emb : reg_file_emb_t;
|
71 |
|
|
signal rf_write_data : std_ulogic_vector(data_width_c-1 downto 0); -- actual write-back data
|
72 |
13 |
zero_gravi |
signal valid_wr : std_ulogic; -- writing not to r0
|
73 |
2 |
zero_gravi |
signal rs1_read : std_ulogic_vector(data_width_c-1 downto 0); -- internal operand rs1
|
74 |
|
|
signal rs2_read : std_ulogic_vector(data_width_c-1 downto 0); -- internal operand rs2
|
75 |
|
|
|
76 |
|
|
-- reading from r0? --
|
77 |
|
|
signal rs1_clear, rs2_clear : std_ulogic;
|
78 |
|
|
|
79 |
12 |
zero_gravi |
|
80 |
|
|
-- attributes - these are *NOT mandatory*; just for footprint / timing optimization --
|
81 |
|
|
-- -------------------------------------------------------------------------------- --
|
82 |
|
|
|
83 |
|
|
-- lattice radiant --
|
84 |
|
|
attribute syn_ramstyle : string;
|
85 |
|
|
attribute syn_ramstyle of reg_file : signal is "no_rw_check";
|
86 |
|
|
attribute syn_ramstyle of reg_file_emb : signal is "no_rw_check";
|
87 |
|
|
|
88 |
|
|
-- intel quartus prime --
|
89 |
|
|
attribute ramstyle : string;
|
90 |
|
|
attribute ramstyle of reg_file : signal is "no_rw_check";
|
91 |
|
|
attribute ramstyle of reg_file_emb : signal is "no_rw_check";
|
92 |
|
|
|
93 |
2 |
zero_gravi |
begin
|
94 |
|
|
|
95 |
|
|
-- Input mux ------------------------------------------------------------------------------
|
96 |
|
|
-- -------------------------------------------------------------------------------------------
|
97 |
|
|
input_mux: process(ctrl_i, mem_i, alu_i, pc_i, csr_i)
|
98 |
|
|
begin
|
99 |
|
|
case ctrl_i(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) is
|
100 |
|
|
when "00" => rf_write_data <= alu_i;
|
101 |
|
|
when "01" => rf_write_data <= mem_i;
|
102 |
|
|
when "10" => rf_write_data <= pc_i;
|
103 |
|
|
when others => rf_write_data <= csr_i;
|
104 |
|
|
end case;
|
105 |
|
|
end process input_mux;
|
106 |
|
|
|
107 |
13 |
zero_gravi |
-- only write if destination is not x0 (pretty irrelevant, but might save some power) --
|
108 |
|
|
valid_wr <= or_all_f(ctrl_i(ctrl_rf_rd_adr4_c downto ctrl_rf_rd_adr0_c)) when (CPU_EXTENSION_RISCV_E = false) else
|
109 |
|
|
or_all_f(ctrl_i(ctrl_rf_rd_adr3_c downto ctrl_rf_rd_adr0_c));
|
110 |
2 |
zero_gravi |
|
111 |
13 |
zero_gravi |
|
112 |
2 |
zero_gravi |
-- Register file read/write access --------------------------------------------------------
|
113 |
|
|
-- -------------------------------------------------------------------------------------------
|
114 |
|
|
rf_access: process(clk_i)
|
115 |
|
|
begin
|
116 |
9 |
zero_gravi |
if rising_edge(clk_i) then -- sync read and write
|
117 |
2 |
zero_gravi |
if (CPU_EXTENSION_RISCV_E = false) then -- normal register file with 32 entries
|
118 |
|
|
-- check if reading from r0 --
|
119 |
|
|
rs1_clear <= '0';
|
120 |
|
|
rs2_clear <= '0';
|
121 |
|
|
if (ctrl_i(ctrl_rf_rs1_adr4_c downto ctrl_rf_rs1_adr0_c) = "00000") then
|
122 |
|
|
rs1_clear <= '1';
|
123 |
|
|
end if;
|
124 |
|
|
if (ctrl_i(ctrl_rf_rs2_adr4_c downto ctrl_rf_rs2_adr0_c) = "00000") then
|
125 |
|
|
rs2_clear <= '1';
|
126 |
|
|
end if;
|
127 |
|
|
-- write --
|
128 |
13 |
zero_gravi |
if (ctrl_i(ctrl_rf_wb_en_c) = '1') and (valid_wr = '1') then -- valid write-back
|
129 |
2 |
zero_gravi |
reg_file(to_integer(unsigned(ctrl_i(ctrl_rf_rd_adr4_c downto ctrl_rf_rd_adr0_c)))) <= rf_write_data;
|
130 |
|
|
end if;
|
131 |
|
|
-- read --
|
132 |
|
|
rs1_read <= reg_file(to_integer(unsigned(ctrl_i(ctrl_rf_rs1_adr4_c downto ctrl_rf_rs1_adr0_c))));
|
133 |
|
|
rs2_read <= reg_file(to_integer(unsigned(ctrl_i(ctrl_rf_rs2_adr4_c downto ctrl_rf_rs2_adr0_c))));
|
134 |
|
|
|
135 |
|
|
else -- embedded register file with 16 entries
|
136 |
|
|
-- check if reading from r0 --
|
137 |
|
|
rs1_clear <= '0';
|
138 |
|
|
rs2_clear <= '0';
|
139 |
|
|
if (ctrl_i(ctrl_rf_rs1_adr3_c downto ctrl_rf_rs1_adr0_c) = "0000") then
|
140 |
|
|
rs1_clear <= '1';
|
141 |
|
|
end if;
|
142 |
|
|
if (ctrl_i(ctrl_rf_rs2_adr3_c downto ctrl_rf_rs2_adr0_c) = "0000") then
|
143 |
|
|
rs2_clear <= '1';
|
144 |
|
|
end if;
|
145 |
|
|
-- write --
|
146 |
13 |
zero_gravi |
if (ctrl_i(ctrl_rf_wb_en_c) = '1') and (valid_wr = '1') then -- valid write-back
|
147 |
2 |
zero_gravi |
reg_file_emb(to_integer(unsigned(ctrl_i(ctrl_rf_rd_adr3_c downto ctrl_rf_rd_adr0_c)))) <= rf_write_data;
|
148 |
|
|
end if;
|
149 |
|
|
-- read --
|
150 |
|
|
rs1_read <= reg_file_emb(to_integer(unsigned(ctrl_i(ctrl_rf_rs1_adr3_c downto ctrl_rf_rs1_adr0_c))));
|
151 |
|
|
rs2_read <= reg_file_emb(to_integer(unsigned(ctrl_i(ctrl_rf_rs2_adr3_c downto ctrl_rf_rs2_adr0_c))));
|
152 |
|
|
end if;
|
153 |
|
|
end if;
|
154 |
|
|
end process rf_access;
|
155 |
|
|
|
156 |
|
|
|
157 |
|
|
-- Check if reading from r0 ---------------------------------------------------------------
|
158 |
|
|
-- -------------------------------------------------------------------------------------------
|
159 |
|
|
rs1_o <= (others => '0') when ((rs1_clear or ctrl_i(ctrl_rf_clear_rs1_c)) = '1') else rs1_read;
|
160 |
|
|
rs2_o <= (others => '0') when ((rs2_clear or ctrl_i(ctrl_rf_clear_rs2_c)) = '1') else rs2_read;
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
end neorv32_cpu_regfile_rtl;
|