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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_cpu_cp_muldiv.vhd] - Blame information for rev 12

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

Line No. Rev Author Line
1 2 zero_gravi
-- #################################################################################################
2
-- # << NEORV32 - CPU Co-Processor: MULDIV unit >>                                                 #
3
-- # ********************************************************************************************* #
4
-- # Multiplier and Divider unit. Implements the RISC-V RV32-M CPU extension.                      #
5 6 zero_gravi
-- # Multiplier core (signed/unsigned) uses serial algorithm. -> 32+4 cycles latency               #
6
-- # Divider core (unsigned) uses serial algorithm. -> 32+6 cycles latency                         #
7 2 zero_gravi
-- # ********************************************************************************************* #
8
-- # BSD 3-Clause License                                                                          #
9
-- #                                                                                               #
10
-- # Copyright (c) 2020, 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_muldiv is
47
  port (
48
    -- global control --
49
    clk_i   : in  std_ulogic; -- global clock, rising edge
50
    rstn_i  : in  std_ulogic; -- global reset, low-active, async
51
    ctrl_i  : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
52
    -- data input --
53
    rs1_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
54
    rs2_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 2
55
    -- result and status --
56
    res_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- operation result
57
    valid_o : out std_ulogic -- data output valid
58
  );
59
end neorv32_cpu_cp_muldiv;
60
 
61
architecture neorv32_cpu_cp_muldiv_rtl of neorv32_cpu_cp_muldiv is
62
 
63 12 zero_gravi
  -- configuration - still experimental --
64
  constant FAST_MUL_EN : boolean := false; -- use DSPs for faster multiplication
65 6 zero_gravi
 
66 2 zero_gravi
  -- controller --
67
  type state_t is (IDLE, DECODE, INIT_OPX, INIT_OPY, PROCESSING, FINALIZE, COMPLETED);
68
  signal state         : state_t;
69
  signal cnt           : std_ulogic_vector(4 downto 0);
70
  signal cp_op         : std_ulogic_vector(2 downto 0); -- operation to execute
71
  signal start         : std_ulogic;
72
  signal operation     : std_ulogic;
73
  signal opx, opy      : std_ulogic_vector(data_width_c-1 downto 0); -- input operands
74
  signal opx_is_signed : std_ulogic;
75
  signal opy_is_signed : std_ulogic;
76 6 zero_gravi
  signal opy_is_zero   : std_ulogic;
77 2 zero_gravi
  signal div_res_corr  : std_ulogic;
78
 
79
  -- divider core --
80
  signal remainder        : std_ulogic_vector(data_width_c-1 downto 0);
81
  signal quotient         : std_ulogic_vector(data_width_c-1 downto 0);
82
  signal div_sub          : std_ulogic_vector(data_width_c   downto 0);
83
  signal div_sign_comp_in : std_ulogic_vector(data_width_c-1 downto 0);
84
  signal div_sign_comp    : std_ulogic_vector(data_width_c-1 downto 0);
85
  signal div_res          : std_ulogic_vector(data_width_c-1 downto 0);
86
 
87
  -- multiplier core --
88
  signal mul_product    : std_ulogic_vector(63 downto 0);
89 12 zero_gravi
  signal mul_do_add     : std_ulogic_vector(data_width_c downto 0);
90 2 zero_gravi
  signal mul_sign_cycle : std_ulogic;
91
  signal mul_p_sext     : std_ulogic;
92 12 zero_gravi
  signal mul_op_x       : std_ulogic_vector(32 downto 0);
93
  signal mul_op_y       : std_ulogic_vector(32 downto 0);
94
  signal mul_buf_ff0    : std_ulogic_vector(65 downto 0);
95
  signal mul_buf_ff1    : std_ulogic_vector(65 downto 0);
96 2 zero_gravi
 
97
begin
98
 
99
  -- Co-Processor Controller ----------------------------------------------------------------
100
  -- -------------------------------------------------------------------------------------------
101
  coprocessor_ctrl: process(rstn_i, clk_i)
102
  begin
103
    if (rstn_i = '0') then
104
      state        <= IDLE;
105
      cp_op        <= (others => '0');
106 3 zero_gravi
      opx          <= (others => '0');
107
      opy          <= (others => '0');
108
      cnt          <= (others => '0');
109
      start        <= '0';
110 2 zero_gravi
      valid_o      <= '0';
111 3 zero_gravi
      div_res_corr <= '0';
112 6 zero_gravi
      opy_is_zero  <= '0';
113 2 zero_gravi
    elsif rising_edge(clk_i) then
114
      -- defaults --
115
      start   <= '0';
116
      valid_o <= '0';
117
 
118
      -- FSM --
119
      case state is
120
        when IDLE =>
121
          opx   <= rs1_i;
122
          opy   <= rs2_i;
123 4 zero_gravi
          cp_op <= ctrl_i(ctrl_cp_cmd2_c downto ctrl_cp_cmd0_c);
124 2 zero_gravi
          if (ctrl_i(ctrl_cp_use_c) = '1') and (ctrl_i(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) = cp_sel_muldiv_c) then
125
            state <= DECODE;
126
          end if;
127
 
128
        when DECODE =>
129 12 zero_gravi
          --
130 6 zero_gravi
          if (cp_op = cp_op_div_c) then -- result sign compensation for div?
131 2 zero_gravi
            div_res_corr <= opx(opx'left) xor opy(opy'left);
132 6 zero_gravi
          elsif (cp_op = cp_op_rem_c) then -- result sign compensation for rem?
133
            div_res_corr <= opx(opx'left);
134 2 zero_gravi
          else
135
            div_res_corr <= '0';
136
          end if;
137 12 zero_gravi
          --
138
          if (or_all_f(opy) = '0') then -- *divide* by 0?
139 6 zero_gravi
            opy_is_zero <= '1';
140
          else
141
            opy_is_zero <= '0';
142
          end if;
143 12 zero_gravi
          --
144 2 zero_gravi
          if (operation = '1') then -- division
145 12 zero_gravi
            cnt   <= "11111";
146 2 zero_gravi
            state <= INIT_OPX;
147
          else -- multiplication
148 12 zero_gravi
            if (FAST_MUL_EN = false) then
149
              cnt <= "11111";
150
            else
151
              cnt <= "00101"; -- FIXME
152
            end if;
153 2 zero_gravi
            start <= '1';
154
            state <= PROCESSING;
155
          end if;
156
 
157
        when INIT_OPX =>
158
          if ((opx(opx'left) and opx_is_signed) = '1') then -- signed division?
159
            opx <= div_sign_comp; -- make positive
160
          end if;
161
          state <= INIT_OPY;
162
 
163
        when INIT_OPY =>
164
          start <= '1';
165
          if ((opy(opy'left) and opy_is_signed) = '1') then -- signed division?
166
            opy <= div_sign_comp; -- make positive
167
          end if;
168
          state <= PROCESSING;
169
 
170
        when PROCESSING =>
171
          cnt <= std_ulogic_vector(unsigned(cnt) - 1);
172
          if (cnt = "00000") then
173
            state <= FINALIZE;
174
          end if;
175
 
176
        when FINALIZE =>
177
          state <= COMPLETED;
178
 
179
        when COMPLETED =>
180
          valid_o <= '1';
181
          state   <= IDLE;
182
      end case;
183
    end if;
184
  end process coprocessor_ctrl;
185
 
186
  -- operation --
187
  operation <= '1' when (cp_op = cp_op_div_c) or (cp_op = cp_op_divu_c) or (cp_op = cp_op_rem_c) or (cp_op = cp_op_remu_c) else '0';
188
 
189
  -- opx (rs1) signed? --
190
  opx_is_signed <= '1' when (cp_op = cp_op_mulh_c) or (cp_op = cp_op_mulhsu_c) or (cp_op = cp_op_div_c) or (cp_op = cp_op_rem_c) else '0';
191
 
192
  -- opy (rs2) signed? --
193
  opy_is_signed <= '1' when (cp_op = cp_op_mulh_c) or (cp_op = cp_op_div_c) or (cp_op = cp_op_rem_c) else '0';
194
 
195
 
196 12 zero_gravi
  -- Multiplier Core (signed) ---------------------------------------------------------------
197 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
198
  multiplier_core: process(clk_i)
199
  begin
200
    if rising_edge(clk_i) then
201 12 zero_gravi
      if (FAST_MUL_EN = false) then -- use small iterative computation
202
        if (start = '1') then -- start new multiplication
203
          mul_product(63 downto 32) <= (others => '0');
204
          mul_product(31 downto 00) <= opy;
205
        elsif ((state = PROCESSING) or (state = FINALIZE)) and (operation = '0') then
206
          mul_product(63 downto 31) <= mul_do_add(32 downto 0);
207
          mul_product(30 downto 00) <= mul_product(31 downto 1);
208
        end if;
209
      else -- use direct approach using (several!) DSP blocks
210
        if (start = '1') then
211
          mul_op_x <= (opx(opx'left) and opx_is_signed) & opx;
212
          mul_op_y <= (opy(opy'left) and opy_is_signed) & opy;
213
        end if;
214
        mul_buf_ff0 <= std_ulogic_vector(signed(mul_op_x) * signed(mul_op_y));
215
        mul_buf_ff1 <= mul_buf_ff0;
216
        mul_product <= mul_buf_ff1(63 downto 0); -- let the register balancing do the magic here
217 2 zero_gravi
      end if;
218
    end if;
219
  end process multiplier_core;
220
 
221
  -- MUL: do another addition --
222 4 zero_gravi
  mul_update: process(mul_product, mul_sign_cycle, mul_p_sext, opx_is_signed, opx)
223 2 zero_gravi
  begin
224 12 zero_gravi
    -- current bit of opy to take care of --
225
    if (mul_product(0) = '1') then -- multiply with 1
226
      if (mul_sign_cycle = '1') then -- for signed operations only: take care of negative weighted MSB -> multiply with -1
227 6 zero_gravi
        mul_do_add <= std_ulogic_vector(unsigned(mul_p_sext & mul_product(63 downto 32)) - unsigned((opx(opx'left) and opx_is_signed) & opx));
228 12 zero_gravi
      else -- multiply with +1
229 6 zero_gravi
        mul_do_add <= std_ulogic_vector(unsigned(mul_p_sext & mul_product(63 downto 32)) + unsigned((opx(opx'left) and opx_is_signed) & opx));
230 2 zero_gravi
      end if;
231 12 zero_gravi
    else -- multiply with 0
232 2 zero_gravi
      mul_do_add <= mul_p_sext & mul_product(63 downto 32);
233
    end if;
234
  end process mul_update;
235
 
236
  -- sign control --
237
  mul_sign_cycle <= opy_is_signed when (state = FINALIZE) else '0';
238
  mul_p_sext     <= mul_product(mul_product'left) and opx_is_signed;
239
 
240
 
241 12 zero_gravi
  -- Divider Core (unsigned) ----------------------------------------------------------------
242 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
243
  divider_core: process(clk_i)
244
  begin
245
    if rising_edge(clk_i) then
246
      if (start = '1') then -- start new division
247
        quotient  <= opx;
248
        remainder <= (others => '0');
249
      elsif ((state = PROCESSING) or (state = FINALIZE)) and (operation = '1') then -- running?
250
        quotient <= quotient(30 downto 0) & (not div_sub(32));
251
        if (div_sub(32) = '0') then -- still overflowing
252
          remainder <= div_sub(31 downto 0);
253
        else -- underflow
254
          remainder <= remainder(30 downto 0) & quotient(31);
255
        end if;
256
      end if;
257
    end if;
258
  end process divider_core;
259
 
260
  -- DIV: try another subtraction --
261
  div_sub <= std_ulogic_vector(unsigned('0' & remainder(30 downto 0) & quotient(31)) - unsigned('0' & opy));
262
 
263
  -- Div sign compensation --
264
  div_sign_comp_in <= opx when (state = INIT_OPX) else
265
                      opy when (state = INIT_OPY) else
266
                      quotient when ((cp_op = cp_op_div_c) or (cp_op = cp_op_divu_c)) else remainder;
267
  div_sign_comp <= std_ulogic_vector(0 - unsigned(div_sign_comp_in));
268
 
269
  -- result sign correction --
270 6 zero_gravi
  div_res <= div_sign_comp when (div_res_corr = '1') and (opy_is_zero = '0') else div_sign_comp_in;
271 2 zero_gravi
 
272
 
273
  -- Data Output ----------------------------------------------------------------------------
274
  -- -------------------------------------------------------------------------------------------
275
  operation_result: process(clk_i)
276
  begin
277
    if rising_edge(clk_i) then
278 6 zero_gravi
      res_o <= (others => '0'); -- default
279 2 zero_gravi
      case cp_op is
280
        when cp_op_mul_c =>
281
          res_o <= mul_product(31 downto 00);
282
        when cp_op_mulh_c | cp_op_mulhsu_c | cp_op_mulhu_c =>
283
          res_o <= mul_product(63 downto 32);
284
        when cp_op_div_c =>
285
          res_o <= div_res;
286
        when cp_op_divu_c =>
287
          res_o <= quotient;
288
        when cp_op_rem_c =>
289 6 zero_gravi
          if (opy_is_zero = '0') then
290
            res_o <= div_res;
291
          else
292
            res_o <= opx;
293
          end if;
294 2 zero_gravi
        when cp_op_remu_c =>
295
          res_o <= remainder;
296
        when others => -- undefined
297 3 zero_gravi
          res_o <= (others => '0');
298 2 zero_gravi
      end case;
299
    end if;
300
  end process operation_result;
301
 
302
 
303
end neorv32_cpu_cp_muldiv_rtl;

powered by: WebSVN 2.1.0

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