OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_cpu_regfile.vhd] - Blame information for rev 36

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.