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 74

Details | Compare with Previous | View Log

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