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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_cpu_cp_shifter.vhd] - Blame information for rev 62

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

Line No. Rev Author Line
1 61 zero_gravi
-- #################################################################################################
2
-- # << NEORV32 - CPU Co-Processor: Shifter (CPU Core ISA) >>                                      #
3
-- # ********************************************************************************************* #
4
-- # Bit-shift unit for base ISA.                                                                  #
5
-- # FAST_SHIFT_EN = false (default): Use bit-serial shifter architecture (small but slow)         #
6
-- # FAST_SHIFT_EN = true: Use barrel shifter architecture (large but fast)                        #
7
-- # ********************************************************************************************* #
8
-- # BSD 3-Clause License                                                                          #
9
-- #                                                                                               #
10
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
11
-- #                                                                                               #
12
-- # Redistribution and use in source and binary forms, with or without modification, are          #
13
-- # permitted provided that the following conditions are met:                                     #
14
-- #                                                                                               #
15
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
16
-- #    conditions and the following disclaimer.                                                   #
17
-- #                                                                                               #
18
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
19
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
20
-- #    provided with the distribution.                                                            #
21
-- #                                                                                               #
22
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
23
-- #    endorse or promote products derived from this software without specific prior written      #
24
-- #    permission.                                                                                #
25
-- #                                                                                               #
26
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
27
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
28
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
29
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
30
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
31
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
32
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
33
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
34
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
35
-- # ********************************************************************************************* #
36
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
37
-- #################################################################################################
38
 
39
library ieee;
40
use ieee.std_logic_1164.all;
41
use ieee.numeric_std.all;
42
 
43
library neorv32;
44
use neorv32.neorv32_package.all;
45
 
46
entity neorv32_cpu_cp_shifter is
47
  generic (
48 62 zero_gravi
    FAST_SHIFT_EN : boolean -- use barrel shifter for shift operations
49 61 zero_gravi
  );
50
  port (
51
    -- global control --
52
    clk_i   : in  std_ulogic; -- global clock, rising edge
53
    rstn_i  : in  std_ulogic; -- global reset, low-active, async
54
    ctrl_i  : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
55
    start_i : in  std_ulogic; -- trigger operation
56
    -- data input --
57
    rs1_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
58
    rs2_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 2
59
    imm_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- immediate
60
    -- result and status --
61
    res_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- operation result
62
    valid_o : out std_ulogic -- data output valid
63
  );
64
end neorv32_cpu_cp_shifter;
65
 
66
architecture neorv32_cpu_cp_shifter_rtl of neorv32_cpu_cp_shifter is
67
 
68
  -- operands --
69
  signal shift_amount : std_ulogic_vector(index_size_f(data_width_c)-1 downto 0);
70
 
71
  -- serial shifter --
72
  type shifter_t is record
73
    busy    : std_ulogic;
74
    busy_ff : std_ulogic;
75
    done    : std_ulogic;
76
    cnt     : std_ulogic_vector(index_size_f(data_width_c)-1 downto 0);
77
    sreg    : std_ulogic_vector(data_width_c-1 downto 0);
78
  end record;
79
  signal shifter : shifter_t;
80
 
81
  -- barrel shifter --
82
  type bs_level_t is array (index_size_f(data_width_c) downto 0) of std_ulogic_vector(data_width_c-1 downto 0);
83
  signal bs_level  : bs_level_t;
84
  signal bs_result : std_ulogic_vector(data_width_c-1 downto 0);
85
 
86
begin
87
 
88
  -- Shift Amount ---------------------------------------------------------------------------
89
  -- -------------------------------------------------------------------------------------------
90
  shift_amount <= imm_i(index_size_f(data_width_c)-1 downto 0) when (ctrl_i(ctrl_alu_opb_mux_c) = '1') else -- immediate source
91
                  rs2_i(index_size_f(data_width_c)-1 downto 0); -- register source
92
 
93
 
94
  -- Iterative Shifter Core (small but slow) ------------------------------------------------
95
  -- -------------------------------------------------------------------------------------------
96
  serial_shifter_sync:
97
  if (FAST_SHIFT_EN = false) generate
98
    shifter_unit_sync: process(rstn_i, clk_i)
99
    begin
100
      if (rstn_i = '0') then
101
        shifter.busy    <= '0';
102
        shifter.busy_ff <= def_rst_val_c;
103
        shifter.sreg    <= (others => def_rst_val_c);
104
        shifter.cnt     <= (others => def_rst_val_c);
105
      elsif rising_edge(clk_i) then
106
        shifter.busy_ff <= shifter.busy;
107
        if (start_i = '1') then
108
          shifter.busy <= '1';
109
        elsif (shifter.done = '1') then
110
          shifter.busy <= '0';
111
        end if;
112
        --
113
        if (start_i = '1') then -- trigger new shift
114
          shifter.sreg <= rs1_i; -- shift operand
115
          shifter.cnt  <= shift_amount; -- shift amount
116
        elsif (or_reduce_f(shifter.cnt) = '1') then -- running shift (cnt != 0)
117
          shifter.cnt <= std_ulogic_vector(unsigned(shifter.cnt) - 1);
118
          if (ctrl_i(ctrl_alu_shift_dir_c) = '0') then -- SLL: shift left logical
119
            shifter.sreg <= shifter.sreg(shifter.sreg'left-1 downto 0) & '0';
120
          else -- SRL: shift right logical / SRA: shift right arithmetical
121
            shifter.sreg <= (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) & shifter.sreg(shifter.sreg'left downto 1);
122
          end if;
123
        end if;
124
      end if;
125
    end process shifter_unit_sync;
126
  end generate;
127
 
128
  -- shift control --
129
  serial_shifter_ctrl:
130
  if (FAST_SHIFT_EN = false) generate
131
    shifter.done <= not or_reduce_f(shifter.cnt(shifter.cnt'left downto 1));
132
    valid_o      <= shifter.busy and shifter.done;
133
    res_o        <= shifter.sreg when (shifter.busy = '0') and (shifter.busy_ff = '1') else (others => '0');
134
  end generate;
135
 
136
 
137
  -- Barrel Shifter Core (fast but large) ---------------------------------------------------
138
  -- -------------------------------------------------------------------------------------------
139
  barrel_shifter_async:
140
  if (FAST_SHIFT_EN = true) generate
141
    shifter_unit_async: process(rs1_i, shift_amount, ctrl_i, bs_level)
142
    begin
143
      -- input level: convert left shifts to right shifts --
144
      if (ctrl_i(ctrl_alu_shift_dir_c) = '0') then -- is left shift?
145
        bs_level(index_size_f(data_width_c)) <= bit_rev_f(rs1_i); -- reverse bit order of input operand
146
      else
147
        bs_level(index_size_f(data_width_c)) <= rs1_i;
148
      end if;
149
 
150
      -- shifter array --
151
      for i in index_size_f(data_width_c)-1 downto 0 loop
152
        if (shift_amount(i) = '1') then
153
          bs_level(i)(data_width_c-1 downto data_width_c-(2**i)) <= (others => (bs_level(i+1)(data_width_c-1) and ctrl_i(ctrl_alu_shift_ar_c)));
154
          bs_level(i)((data_width_c-(2**i))-1 downto 0) <= bs_level(i+1)(data_width_c-1 downto 2**i);
155
        else
156
          bs_level(i) <= bs_level(i+1);
157
        end if;
158
      end loop;
159
 
160
      -- re-convert original left shifts --
161
      if (ctrl_i(ctrl_alu_shift_dir_c) = '0') then
162
        bs_result <= bit_rev_f(bs_level(0));
163
      else
164
        bs_result <= bs_level(0);
165
      end if;
166
    end process shifter_unit_async;
167
  end generate;
168
 
169
  -- output register --
170
  barrel_shifter_sync:
171
  if (FAST_SHIFT_EN = true) generate
172
    shifter_unit_sync: process(clk_i)
173
    begin
174
      if rising_edge(clk_i) then
175
        res_o <= (others => '0');
176
        if (start_i = '1') then
177
          res_o <= bs_result;
178
        end if;
179
      end if;
180
    end process shifter_unit_sync;
181
  end generate;
182
 
183
  -- shift control --
184
  barrel_shifter_ctrl:
185
  if (FAST_SHIFT_EN = true) generate
186
    valid_o <= start_i;
187
  end generate;
188
 
189
 
190
end neorv32_cpu_cp_shifter_rtl;

powered by: WebSVN 2.1.0

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