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

Subversion Repositories neorv32

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

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
-- # 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
    CPU_EXTENSION_RISCV_E : boolean := false -- implement embedded RF extension?
55
  );
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 rf_we        : std_ulogic;
80
  signal dst_addr     : std_ulogic_vector(4 downto 0); -- destination address
81
  signal opa_addr     : std_ulogic_vector(4 downto 0); -- rs1/dst address
82
  signal opb_addr     : std_ulogic_vector(4 downto 0); -- rs2 address
83
  signal rs1, rs2     : std_ulogic_vector(data_width_c-1 downto 0);
84 2 zero_gravi
 
85 47 zero_gravi
  -- comparator --
86
  signal cmp_opx : std_ulogic_vector(data_width_c downto 0);
87
  signal cmp_opy : std_ulogic_vector(data_width_c downto 0);
88
 
89 2 zero_gravi
begin
90
 
91 45 zero_gravi
  -- Data Input Mux -------------------------------------------------------------------------
92 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
93 49 zero_gravi
  rf_wdata <= alu_i when (ctrl_i(ctrl_rf_in_mux_c) = '0') else mem_i;
94 45 zero_gravi
 
95
 
96
  -- Register File Access -------------------------------------------------------------------
97
  -- -------------------------------------------------------------------------------------------
98 2 zero_gravi
  rf_access: process(clk_i)
99
  begin
100 9 zero_gravi
    if rising_edge(clk_i) then -- sync read and write
101 2 zero_gravi
      if (CPU_EXTENSION_RISCV_E = false) then -- normal register file with 32 entries
102 36 zero_gravi
        if (rf_we = '1') then
103 49 zero_gravi
          reg_file(to_integer(unsigned(opa_addr(4 downto 0)))) <= rf_wdata;
104 2 zero_gravi
        end if;
105 47 zero_gravi
        rs1 <= reg_file(to_integer(unsigned(opa_addr(4 downto 0))));
106
        rs2 <= reg_file(to_integer(unsigned(opb_addr(4 downto 0))));
107 2 zero_gravi
      else -- embedded register file with 16 entries
108 36 zero_gravi
        if (rf_we = '1') then
109 49 zero_gravi
          reg_file_emb(to_integer(unsigned(opa_addr(3 downto 0)))) <= rf_wdata;
110 2 zero_gravi
        end if;
111 47 zero_gravi
        rs1 <= reg_file_emb(to_integer(unsigned(opa_addr(3 downto 0))));
112
        rs2 <= reg_file_emb(to_integer(unsigned(opb_addr(3 downto 0))));
113 2 zero_gravi
      end if;
114
    end if;
115
  end process rf_access;
116
 
117 39 zero_gravi
  -- check if we are writing to x0 --
118
  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
119
              not or_all_f(ctrl_i(ctrl_rf_rd_adr3_c downto ctrl_rf_rd_adr0_c));
120
 
121 45 zero_gravi
  -- valid RF write access? --
122 39 zero_gravi
  rf_we <= (ctrl_i(ctrl_rf_wb_en_c) and (not rd_is_r0)) or ctrl_i(ctrl_rf_r0_we_c);
123
 
124 58 zero_gravi
  -- access addresses --
125 39 zero_gravi
  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?
126 45 zero_gravi
  opa_addr <= dst_addr when (rf_we = '1') else ctrl_i(ctrl_rf_rs1_adr4_c downto ctrl_rf_rs1_adr0_c); -- rd/rs1
127
  opb_addr <= ctrl_i(ctrl_rf_rs2_adr4_c downto ctrl_rf_rs2_adr0_c); -- rs2
128 39 zero_gravi
 
129 47 zero_gravi
  -- data output --
130
  rs1_o <= rs1;
131
  rs2_o <= rs2;
132 39 zero_gravi
 
133 47 zero_gravi
 
134
  -- Comparator Unit (for conditional branches) ---------------------------------------------
135
  -- -------------------------------------------------------------------------------------------
136
  cmp_opx <= (rs1(rs1'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & rs1;
137
  cmp_opy <= (rs2(rs2'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & rs2;
138
 
139
  cmp_o(cmp_equal_c) <= '1' when (rs1 = rs2) else '0';
140
  cmp_o(cmp_less_c)  <= '1' when (signed(cmp_opx) < signed(cmp_opy)) else '0';
141
 
142
 
143 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.