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 74

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 71 zero_gravi
-- # Copyright (c) 2022, Stephan Nolting. All rights reserved.                                     #
11 61 zero_gravi
-- #                                                                                               #
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 66 zero_gravi
    shamt_i : in  std_ulogic_vector(index_size_f(data_width_c)-1 downto 0); -- shift amount
59 61 zero_gravi
    -- result and status --
60
    res_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- operation result
61
    valid_o : out std_ulogic -- data output valid
62
  );
63
end neorv32_cpu_cp_shifter;
64
 
65
architecture neorv32_cpu_cp_shifter_rtl of neorv32_cpu_cp_shifter is
66
 
67
  -- serial shifter --
68
  type shifter_t is record
69
    busy    : std_ulogic;
70
    busy_ff : std_ulogic;
71
    done    : std_ulogic;
72
    cnt     : std_ulogic_vector(index_size_f(data_width_c)-1 downto 0);
73
    sreg    : std_ulogic_vector(data_width_c-1 downto 0);
74 66 zero_gravi
    res     : std_ulogic_vector(data_width_c-1 downto 0);
75 61 zero_gravi
  end record;
76
  signal shifter : shifter_t;
77
 
78
  -- barrel shifter --
79
  type bs_level_t is array (index_size_f(data_width_c) downto 0) of std_ulogic_vector(data_width_c-1 downto 0);
80
  signal bs_level  : bs_level_t;
81
  signal bs_result : std_ulogic_vector(data_width_c-1 downto 0);
82
 
83
begin
84
 
85
  -- Iterative Shifter Core (small but slow) ------------------------------------------------
86
  -- -------------------------------------------------------------------------------------------
87
  serial_shifter_sync:
88
  if (FAST_SHIFT_EN = false) generate
89
    shifter_unit_sync: process(rstn_i, clk_i)
90
    begin
91
      if (rstn_i = '0') then
92
        shifter.busy    <= '0';
93
        shifter.busy_ff <= def_rst_val_c;
94
        shifter.sreg    <= (others => def_rst_val_c);
95
        shifter.cnt     <= (others => def_rst_val_c);
96
      elsif rising_edge(clk_i) then
97
        shifter.busy_ff <= shifter.busy;
98
        if (start_i = '1') then
99
          shifter.busy <= '1';
100 71 zero_gravi
        elsif (shifter.done = '1') or (ctrl_i(ctrl_trap_c) = '1') then -- abort on trap
101 61 zero_gravi
          shifter.busy <= '0';
102
        end if;
103
        --
104
        if (start_i = '1') then -- trigger new shift
105
          shifter.sreg <= rs1_i; -- shift operand
106 66 zero_gravi
          shifter.cnt  <= shamt_i; -- shift amount
107 61 zero_gravi
        elsif (or_reduce_f(shifter.cnt) = '1') then -- running shift (cnt != 0)
108
          shifter.cnt <= std_ulogic_vector(unsigned(shifter.cnt) - 1);
109 74 zero_gravi
          if (ctrl_i(ctrl_ir_funct3_2_c) = '0') then -- SLL: shift left logical
110 61 zero_gravi
            shifter.sreg <= shifter.sreg(shifter.sreg'left-1 downto 0) & '0';
111
          else -- SRL: shift right logical / SRA: shift right arithmetical
112 74 zero_gravi
            shifter.sreg <= (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_ir_funct12_10_c)) & shifter.sreg(shifter.sreg'left downto 1);
113 61 zero_gravi
          end if;
114
        end if;
115
      end if;
116
    end process shifter_unit_sync;
117
  end generate;
118
 
119 66 zero_gravi
  -- shift control/output --
120 61 zero_gravi
  serial_shifter_ctrl:
121
  if (FAST_SHIFT_EN = false) generate
122 74 zero_gravi
    shifter.done <= '1' when (or_reduce_f(shifter.cnt(shifter.cnt'left downto 1)) = '0') else '0';
123 61 zero_gravi
    valid_o      <= shifter.busy and shifter.done;
124
    res_o        <= shifter.sreg when (shifter.busy = '0') and (shifter.busy_ff = '1') else (others => '0');
125
  end generate;
126
 
127
 
128
  -- Barrel Shifter Core (fast but large) ---------------------------------------------------
129
  -- -------------------------------------------------------------------------------------------
130
  barrel_shifter_async:
131
  if (FAST_SHIFT_EN = true) generate
132 66 zero_gravi
    shifter_unit_async: process(rs1_i, shamt_i, ctrl_i, bs_level)
133 61 zero_gravi
    begin
134
      -- input level: convert left shifts to right shifts --
135 74 zero_gravi
      if (ctrl_i(ctrl_ir_funct3_2_c) = '0') then -- is left shift?
136 61 zero_gravi
        bs_level(index_size_f(data_width_c)) <= bit_rev_f(rs1_i); -- reverse bit order of input operand
137
      else
138
        bs_level(index_size_f(data_width_c)) <= rs1_i;
139
      end if;
140
 
141
      -- shifter array --
142
      for i in index_size_f(data_width_c)-1 downto 0 loop
143 66 zero_gravi
        if (shamt_i(i) = '1') then
144 74 zero_gravi
          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_ir_funct12_10_c)));
145 61 zero_gravi
          bs_level(i)((data_width_c-(2**i))-1 downto 0) <= bs_level(i+1)(data_width_c-1 downto 2**i);
146
        else
147
          bs_level(i) <= bs_level(i+1);
148
        end if;
149
      end loop;
150
 
151
      -- re-convert original left shifts --
152 74 zero_gravi
      if (ctrl_i(ctrl_ir_funct3_2_c) = '0') then
153 61 zero_gravi
        bs_result <= bit_rev_f(bs_level(0));
154
      else
155
        bs_result <= bs_level(0);
156
      end if;
157
    end process shifter_unit_async;
158
  end generate;
159
 
160
  -- output register --
161
  barrel_shifter_sync:
162
  if (FAST_SHIFT_EN = true) generate
163
    shifter_unit_sync: process(clk_i)
164
    begin
165
      if rising_edge(clk_i) then
166
        res_o <= (others => '0');
167
        if (start_i = '1') then
168
          res_o <= bs_result;
169
        end if;
170
      end if;
171
    end process shifter_unit_sync;
172
  end generate;
173
 
174 66 zero_gravi
  -- shift control/output --
175 61 zero_gravi
  barrel_shifter_ctrl:
176
  if (FAST_SHIFT_EN = true) generate
177
    valid_o <= start_i;
178
  end generate;
179
 
180
 
181
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.