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

Subversion Repositories neorv32

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

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

Line No. Rev Author Line
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 65 zero_gravi
-- # Register zero (r0/x0) is a "normal" physical register that has to be initialized to zero by   #
8
-- # the early boot code. Register zero 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 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 65 zero_gravi
    rs2_o  : out std_ulogic_vector(data_width_c-1 downto 0)  -- operand 2
66 2 zero_gravi
  );
67
end neorv32_cpu_regfile;
68
 
69
architecture neorv32_cpu_regfile_rtl of neorv32_cpu_regfile is
70
 
71
  -- register file --
72
  type   reg_file_t is array (31 downto 0) of std_ulogic_vector(data_width_c-1 downto 0);
73
  type   reg_file_emb_t is array (15 downto 0) of std_ulogic_vector(data_width_c-1 downto 0);
74 49 zero_gravi
  signal reg_file     : reg_file_t;
75
  signal reg_file_emb : reg_file_emb_t;
76
  signal rf_wdata     : std_ulogic_vector(data_width_c-1 downto 0); -- actual write-back data
77
  signal rd_is_r0     : std_ulogic; -- writing to r0?
78
  signal dst_addr     : std_ulogic_vector(4 downto 0); -- destination address
79
  signal opa_addr     : std_ulogic_vector(4 downto 0); -- rs1/dst address
80
  signal opb_addr     : std_ulogic_vector(4 downto 0); -- rs2 address
81 62 zero_gravi
  signal rs1, rs2     : std_ulogic_vector(data_width_c-1 downto 0); -- read data
82 2 zero_gravi
 
83
begin
84
 
85 45 zero_gravi
  -- Data Input Mux -------------------------------------------------------------------------
86 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
87 62 zero_gravi
  input_mux: process(rd_is_r0, ctrl_i, alu_i, mem_i)
88
  begin
89
    if (rd_is_r0 = '1') then -- write zero if accessing x0 to "emulate" it is hardwired to zero
90
      rf_wdata <= (others => '0');
91
    else
92
      if (ctrl_i(ctrl_rf_in_mux_c) = '0') then
93
        rf_wdata <= alu_i;
94
      else
95
        rf_wdata <= mem_i;
96
      end if;
97
    end if;
98
  end process input_mux;
99 45 zero_gravi
 
100 62 zero_gravi
  -- check if we are writing to x0 --
101
  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)));
102 45 zero_gravi
 
103 62 zero_gravi
 
104 45 zero_gravi
  -- Register File Access -------------------------------------------------------------------
105
  -- -------------------------------------------------------------------------------------------
106 2 zero_gravi
  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 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 47 zero_gravi
        rs1 <= reg_file(to_integer(unsigned(opa_addr(4 downto 0))));
114
        rs2 <= reg_file(to_integer(unsigned(opb_addr(4 downto 0))));
115 2 zero_gravi
      else -- embedded register file with 16 entries
116 62 zero_gravi
        if (ctrl_i(ctrl_rf_wb_en_c) = '1') then
117 49 zero_gravi
          reg_file_emb(to_integer(unsigned(opa_addr(3 downto 0)))) <= rf_wdata;
118 2 zero_gravi
        end if;
119 47 zero_gravi
        rs1 <= reg_file_emb(to_integer(unsigned(opa_addr(3 downto 0))));
120
        rs2 <= reg_file_emb(to_integer(unsigned(opb_addr(3 downto 0))));
121 2 zero_gravi
      end if;
122
    end if;
123
  end process rf_access;
124
 
125 58 zero_gravi
  -- access addresses --
126 62 zero_gravi
  dst_addr <= ctrl_i(ctrl_rf_rd_adr4_c downto ctrl_rf_rd_adr0_c);
127
  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
128 45 zero_gravi
  opb_addr <= ctrl_i(ctrl_rf_rs2_adr4_c downto ctrl_rf_rs2_adr0_c); -- rs2
129 39 zero_gravi
 
130 47 zero_gravi
  -- data output --
131
  rs1_o <= rs1;
132
  rs2_o <= rs2;
133 39 zero_gravi
 
134 47 zero_gravi
 
135 2 zero_gravi
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.