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 56

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 Co-Processor: Integer Multiplier/Divider Unit (RISC-V "M" Extension)>>       #
3 2 zero_gravi
-- # ********************************************************************************************* #
4 56 zero_gravi
-- # Multiplier and Divider unit. Implements the RISC-V M CPU extension.                           #
5
-- #                                                                                               #
6
-- # Multiplier core (signed/unsigned) uses classical serial algorithm. Unit atency: 31+3 cycles   #
7
-- # Divider core (unsigned) uses classical serial algorithm. Unit latency: 32+4 cycles            #
8
-- #                                                                                               #
9
-- # Multiplications can be mapped to DSP blocks (faster!) when FAST_MUL_EN = true.                #
10
-- # Unit latency: 3 cycles                                                                        #
11 2 zero_gravi
-- # ********************************************************************************************* #
12
-- # BSD 3-Clause License                                                                          #
13
-- #                                                                                               #
14 44 zero_gravi
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
15 2 zero_gravi
-- #                                                                                               #
16
-- # Redistribution and use in source and binary forms, with or without modification, are          #
17
-- # permitted provided that the following conditions are met:                                     #
18
-- #                                                                                               #
19
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
20
-- #    conditions and the following disclaimer.                                                   #
21
-- #                                                                                               #
22
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
23
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
24
-- #    provided with the distribution.                                                            #
25
-- #                                                                                               #
26
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
27
-- #    endorse or promote products derived from this software without specific prior written      #
28
-- #    permission.                                                                                #
29
-- #                                                                                               #
30
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
31
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
32
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
33
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
34
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
35
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
36
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
37
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
38
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
39
-- # ********************************************************************************************* #
40
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
41
-- #################################################################################################
42
 
43
library ieee;
44
use ieee.std_logic_1164.all;
45
use ieee.numeric_std.all;
46
 
47
library neorv32;
48
use neorv32.neorv32_package.all;
49
 
50
entity neorv32_cpu_cp_muldiv is
51 19 zero_gravi
  generic (
52
    FAST_MUL_EN : boolean := false -- use DSPs for faster multiplication
53
  );
54 2 zero_gravi
  port (
55
    -- global control --
56
    clk_i   : in  std_ulogic; -- global clock, rising edge
57
    rstn_i  : in  std_ulogic; -- global reset, low-active, async
58
    ctrl_i  : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
59 36 zero_gravi
    start_i : in  std_ulogic; -- trigger operation
60 2 zero_gravi
    -- data input --
61
    rs1_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
62
    rs2_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 2
63
    -- result and status --
64
    res_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- operation result
65
    valid_o : out std_ulogic -- data output valid
66
  );
67
end neorv32_cpu_cp_muldiv;
68
 
69
architecture neorv32_cpu_cp_muldiv_rtl of neorv32_cpu_cp_muldiv is
70
 
71 44 zero_gravi
  -- operations --
72
  constant cp_op_mul_c    : std_ulogic_vector(2 downto 0) := "000"; -- mul
73
  constant cp_op_mulh_c   : std_ulogic_vector(2 downto 0) := "001"; -- mulh
74
  constant cp_op_mulhsu_c : std_ulogic_vector(2 downto 0) := "010"; -- mulhsu
75
  constant cp_op_mulhu_c  : std_ulogic_vector(2 downto 0) := "011"; -- mulhu
76
  constant cp_op_div_c    : std_ulogic_vector(2 downto 0) := "100"; -- div
77
  constant cp_op_divu_c   : std_ulogic_vector(2 downto 0) := "101"; -- divu
78
  constant cp_op_rem_c    : std_ulogic_vector(2 downto 0) := "110"; -- rem
79
  constant cp_op_remu_c   : std_ulogic_vector(2 downto 0) := "111"; -- remu
80
 
81 2 zero_gravi
  -- controller --
82 56 zero_gravi
  type state_t is (IDLE, DIV_PREPROCESS, PROCESSING, FINALIZE, COMPLETED);
83 2 zero_gravi
  signal state         : state_t;
84
  signal cnt           : std_ulogic_vector(4 downto 0);
85
  signal cp_op         : std_ulogic_vector(2 downto 0); -- operation to execute
86 39 zero_gravi
  signal cp_op_ff      : std_ulogic_vector(2 downto 0); -- operation that was executed
87 56 zero_gravi
  signal start_div     : std_ulogic;
88
  signal start_mul     : std_ulogic;
89 2 zero_gravi
  signal operation     : std_ulogic;
90 56 zero_gravi
  signal div_opx       : std_ulogic_vector(data_width_c-1 downto 0);
91
  signal div_opy       : std_ulogic_vector(data_width_c-1 downto 0);
92
  signal rs1_is_signed : std_ulogic;
93
  signal rs2_is_signed : std_ulogic;
94 6 zero_gravi
  signal opy_is_zero   : std_ulogic;
95 2 zero_gravi
  signal div_res_corr  : std_ulogic;
96 39 zero_gravi
  signal valid         : std_ulogic;
97 2 zero_gravi
 
98
  -- divider core --
99
  signal remainder        : std_ulogic_vector(data_width_c-1 downto 0);
100
  signal quotient         : std_ulogic_vector(data_width_c-1 downto 0);
101
  signal div_sub          : std_ulogic_vector(data_width_c   downto 0);
102
  signal div_sign_comp_in : std_ulogic_vector(data_width_c-1 downto 0);
103
  signal div_sign_comp    : std_ulogic_vector(data_width_c-1 downto 0);
104
  signal div_res          : std_ulogic_vector(data_width_c-1 downto 0);
105
 
106
  -- multiplier core --
107 56 zero_gravi
  signal mul_product_p  : std_ulogic_vector(63 downto 0);
108
  signal mul_product_s  : std_ulogic_vector(63 downto 0);
109 2 zero_gravi
  signal mul_product    : std_ulogic_vector(63 downto 0);
110 12 zero_gravi
  signal mul_do_add     : std_ulogic_vector(data_width_c downto 0);
111 2 zero_gravi
  signal mul_sign_cycle : std_ulogic;
112
  signal mul_p_sext     : std_ulogic;
113 19 zero_gravi
  signal mul_op_x       : signed(32 downto 0); -- for using DSPs
114
  signal mul_op_y       : signed(32 downto 0); -- for using DSPs
115
  signal mul_buf_ff     : signed(65 downto 0); -- for using DSPs
116 2 zero_gravi
 
117
begin
118
 
119
  -- Co-Processor Controller ----------------------------------------------------------------
120
  -- -------------------------------------------------------------------------------------------
121
  coprocessor_ctrl: process(rstn_i, clk_i)
122
  begin
123
    if (rstn_i = '0') then
124
      state        <= IDLE;
125 56 zero_gravi
      div_opx      <= (others => def_rst_val_c);
126
      div_opy      <= (others => def_rst_val_c);
127
      cnt          <= (others => def_rst_val_c);
128
      cp_op_ff     <= (others => def_rst_val_c);
129
      start_div    <= '0';
130 39 zero_gravi
      valid        <= '0';
131 56 zero_gravi
      div_res_corr <= def_rst_val_c;
132
      opy_is_zero  <= def_rst_val_c;
133 2 zero_gravi
    elsif rising_edge(clk_i) then
134
      -- defaults --
135 56 zero_gravi
      start_div <= '0';
136
      valid     <= '0';
137 2 zero_gravi
 
138
      -- FSM --
139
      case state is
140
        when IDLE =>
141 56 zero_gravi
          cp_op_ff <= cp_op;
142 19 zero_gravi
          if (start_i = '1') then
143 56 zero_gravi
            if (operation = '1') then -- division
144
              cnt <= "11111";
145
              state <= DIV_PREPROCESS;
146
            else
147
              cnt <= "11110";
148
              if (FAST_MUL_EN = true) then
149
                state <= FINALIZE;
150
              else
151
                state <= PROCESSING;
152
              end if;
153
            end if;
154 2 zero_gravi
          end if;
155
 
156 56 zero_gravi
        when DIV_PREPROCESS =>
157
          -- check rlevatn input signs --
158 6 zero_gravi
          if (cp_op = cp_op_div_c) then -- result sign compensation for div?
159 56 zero_gravi
            div_res_corr <= rs1_i(rs1_i'left) xor rs2_i(rs2_i'left);
160 6 zero_gravi
          elsif (cp_op = cp_op_rem_c) then -- result sign compensation for rem?
161 56 zero_gravi
            div_res_corr <= rs1_i(rs1_i'left);
162 2 zero_gravi
          else
163
            div_res_corr <= '0';
164
          end if;
165 56 zero_gravi
          -- divide by zero? --
166
          opy_is_zero <= not or_all_f(rs2_i); -- set if rs2 = 0
167
          -- abs(rs1) --
168
          if ((rs1_i(rs1_i'left) and rs1_is_signed) = '1') then -- signed division?
169
            div_opx <= std_ulogic_vector(0 - unsigned(rs1_i)); -- make positive
170 6 zero_gravi
          else
171 56 zero_gravi
            div_opx <= rs1_i;
172 6 zero_gravi
          end if;
173 56 zero_gravi
          -- abs(rs2) --
174
          if ((rs2_i(rs2_i'left) and rs2_is_signed) = '1') then -- signed division?
175
            div_opy <= std_ulogic_vector(0 - unsigned(rs2_i)); -- make positive
176
          else
177
            div_opy <= rs2_i;
178
          end if;
179 12 zero_gravi
          --
180 56 zero_gravi
          start_div <= '1';
181
          state     <= PROCESSING;
182 2 zero_gravi
 
183
        when PROCESSING =>
184
          cnt <= std_ulogic_vector(unsigned(cnt) - 1);
185
          if (cnt = "00000") then
186
            state <= FINALIZE;
187
          end if;
188
 
189
        when FINALIZE =>
190
          state <= COMPLETED;
191
 
192
        when COMPLETED =>
193 39 zero_gravi
          valid <= '1';
194
          state <= IDLE;
195 2 zero_gravi
      end case;
196
    end if;
197
  end process coprocessor_ctrl;
198
 
199 36 zero_gravi
  -- co-processor command --
200
  cp_op <= ctrl_i(ctrl_ir_funct3_2_c downto ctrl_ir_funct3_0_c);
201
 
202 56 zero_gravi
  -- operation: 0=mul, 1=div --
203
  operation <= '1' when (cp_op(2) = '1') else '0';
204 2 zero_gravi
 
205
  -- opx (rs1) signed? --
206 56 zero_gravi
  rs1_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';
207 2 zero_gravi
 
208
  -- opy (rs2) signed? --
209 56 zero_gravi
  rs2_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';
210 2 zero_gravi
 
211 56 zero_gravi
  -- start MUL operation (do it fast!) --
212
  start_mul <= '1' when (state = IDLE) and (start_i = '1') and (operation = '0') else '0';
213 2 zero_gravi
 
214 56 zero_gravi
 
215 36 zero_gravi
  -- Multiplier Core (signed/unsigned) ------------------------------------------------------
216 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
217 56 zero_gravi
  multiplier_core_serial: process(rstn_i, clk_i)
218 2 zero_gravi
  begin
219 56 zero_gravi
    if (rstn_i = '0') then
220
      mul_product_s <= (others => def_rst_val_c);
221
    elsif rising_edge(clk_i) then
222 12 zero_gravi
      if (FAST_MUL_EN = false) then -- use small iterative computation
223 56 zero_gravi
        if (start_mul = '1') then -- start new multiplication
224
          mul_product_s(63 downto 32) <= (others => '0');
225
          mul_product_s(31 downto 00) <= rs2_i;
226
        elsif (state = PROCESSING) or (state = FINALIZE) then -- processing step or sign-finalization step
227
          mul_product_s(63 downto 31) <= mul_do_add(32 downto 0);
228
          mul_product_s(30 downto 00) <= mul_product_s(31 downto 1);
229 12 zero_gravi
        end if;
230 56 zero_gravi
      end if;
231
    end if;
232
  end process multiplier_core_serial;
233
 
234
  multiplier_core_dsp: process(clk_i)
235
  begin
236
    if rising_edge(clk_i) then
237
      if (FAST_MUL_EN = true) then -- use direct approach using DSP blocks
238
        if (start_mul = '1') then
239
          mul_op_x <= signed((rs1_i(rs1_i'left) and rs1_is_signed) & rs1_i);
240
          mul_op_y <= signed((rs2_i(rs2_i'left) and rs2_is_signed) & rs2_i);
241 12 zero_gravi
        end if;
242 56 zero_gravi
        mul_buf_ff    <= mul_op_x * mul_op_y;
243
        mul_product_p <= std_ulogic_vector(mul_buf_ff(63 downto 0)); -- let the register balancing do the magic here
244 2 zero_gravi
      end if;
245
    end if;
246 56 zero_gravi
  end process multiplier_core_dsp;
247 2 zero_gravi
 
248 56 zero_gravi
  mul_product <= mul_product_p when (FAST_MUL_EN = true) else mul_product_s;
249
 
250
  -- do another addition --
251
  mul_update: process(mul_product, mul_sign_cycle, mul_p_sext, rs1_is_signed, rs1_i)
252 2 zero_gravi
  begin
253 56 zero_gravi
    -- current bit of rs2_i to take care of --
254 12 zero_gravi
    if (mul_product(0) = '1') then -- multiply with 1
255
      if (mul_sign_cycle = '1') then -- for signed operations only: take care of negative weighted MSB -> multiply with -1
256 56 zero_gravi
        mul_do_add <= std_ulogic_vector(unsigned(mul_p_sext & mul_product(63 downto 32)) - unsigned((rs1_i(rs1_i'left) and rs1_is_signed) & rs1_i));
257 12 zero_gravi
      else -- multiply with +1
258 56 zero_gravi
        mul_do_add <= std_ulogic_vector(unsigned(mul_p_sext & mul_product(63 downto 32)) + unsigned((rs1_i(rs1_i'left) and rs1_is_signed) & rs1_i));
259 2 zero_gravi
      end if;
260 12 zero_gravi
    else -- multiply with 0
261 2 zero_gravi
      mul_do_add <= mul_p_sext & mul_product(63 downto 32);
262
    end if;
263
  end process mul_update;
264
 
265
  -- sign control --
266 56 zero_gravi
  mul_sign_cycle <= rs2_is_signed when (state = FINALIZE) else '0';
267
  mul_p_sext     <= mul_product(mul_product'left) and rs1_is_signed;
268 2 zero_gravi
 
269
 
270 12 zero_gravi
  -- Divider Core (unsigned) ----------------------------------------------------------------
271 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
272 56 zero_gravi
  divider_core: process(rstn_i, clk_i)
273 2 zero_gravi
  begin
274 56 zero_gravi
    if (rstn_i = '0') then
275
      quotient  <= (others => def_rst_val_c);
276
      remainder <= (others => def_rst_val_c);
277
    elsif rising_edge(clk_i) then
278
      if (start_div = '1') then -- start new division
279
        quotient  <= div_opx;
280 2 zero_gravi
        remainder <= (others => '0');
281 56 zero_gravi
      elsif (state = PROCESSING) or (state = FINALIZE) then -- running?
282 2 zero_gravi
        quotient <= quotient(30 downto 0) & (not div_sub(32));
283
        if (div_sub(32) = '0') then -- still overflowing
284
          remainder <= div_sub(31 downto 0);
285
        else -- underflow
286
          remainder <= remainder(30 downto 0) & quotient(31);
287
        end if;
288
      end if;
289
    end if;
290
  end process divider_core;
291
 
292 56 zero_gravi
  -- try another subtraction --
293
  div_sub <= std_ulogic_vector(unsigned('0' & remainder(30 downto 0) & quotient(31)) - unsigned('0' & div_opy));
294 2 zero_gravi
 
295 56 zero_gravi
  -- result sign compensation --
296
  div_sign_comp_in <= quotient when (cp_op = cp_op_div_c) else remainder;
297
  div_sign_comp    <= std_ulogic_vector(0 - unsigned(div_sign_comp_in));
298
  div_res          <= div_sign_comp when (div_res_corr = '1') and (opy_is_zero = '0') else div_sign_comp_in;
299 2 zero_gravi
 
300
 
301
  -- Data Output ----------------------------------------------------------------------------
302
  -- -------------------------------------------------------------------------------------------
303 56 zero_gravi
  operation_result: process(rstn_i, clk_i)
304 2 zero_gravi
  begin
305 56 zero_gravi
    if (rstn_i = '0') then
306
      res_o <= (others => def_rst_val_c);
307
    elsif rising_edge(clk_i) then
308 47 zero_gravi
      res_o <= (others => '0');
309
      if (valid = '1') then
310
        case cp_op_ff is
311
          when cp_op_mul_c =>
312
            res_o <= mul_product(31 downto 00);
313
          when cp_op_mulh_c | cp_op_mulhsu_c | cp_op_mulhu_c =>
314
            res_o <= mul_product(63 downto 32);
315
          when cp_op_div_c =>
316 6 zero_gravi
            res_o <= div_res;
317 47 zero_gravi
          when cp_op_divu_c =>
318
            res_o <= quotient;
319
          when cp_op_rem_c =>
320
            if (opy_is_zero = '0') then
321
              res_o <= div_res;
322
            else
323 56 zero_gravi
              res_o <= rs1_i;
324 47 zero_gravi
            end if;
325
          when others => -- cp_op_remu_c
326
            res_o <= remainder;
327
        end case;
328
      end if;
329 2 zero_gravi
    end if;
330
  end process operation_result;
331
 
332 40 zero_gravi
  -- status output --
333
  valid_o <= valid;
334 2 zero_gravi
 
335 40 zero_gravi
 
336 2 zero_gravi
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.