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 73

Go to most recent revision | 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 2 zero_gravi
 
95
  -- divider core --
96
  signal remainder        : std_ulogic_vector(data_width_c-1 downto 0);
97
  signal quotient         : std_ulogic_vector(data_width_c-1 downto 0);
98
  signal div_sub          : std_ulogic_vector(data_width_c   downto 0);
99
  signal div_sign_comp_in : std_ulogic_vector(data_width_c-1 downto 0);
100
  signal div_sign_comp    : std_ulogic_vector(data_width_c-1 downto 0);
101
  signal div_res          : std_ulogic_vector(data_width_c-1 downto 0);
102
 
103
  -- multiplier core --
104
  signal mul_product    : std_ulogic_vector(63 downto 0);
105 12 zero_gravi
  signal mul_do_add     : std_ulogic_vector(data_width_c downto 0);
106 2 zero_gravi
  signal mul_sign_cycle : std_ulogic;
107
  signal mul_p_sext     : std_ulogic;
108 19 zero_gravi
  signal mul_op_x       : signed(32 downto 0); -- for using DSPs
109
  signal mul_op_y       : signed(32 downto 0); -- for using DSPs
110 2 zero_gravi
 
111
begin
112
 
113
  -- Co-Processor Controller ----------------------------------------------------------------
114
  -- -------------------------------------------------------------------------------------------
115
  coprocessor_ctrl: process(rstn_i, clk_i)
116
  begin
117
    if (rstn_i = '0') then
118
      state        <= IDLE;
119 56 zero_gravi
      div_opy      <= (others => def_rst_val_c);
120
      cnt          <= (others => def_rst_val_c);
121
      cp_op_ff     <= (others => def_rst_val_c);
122
      start_div    <= '0';
123 69 zero_gravi
      out_en       <= '0';
124
      valid_o      <= '0';
125 56 zero_gravi
      div_res_corr <= def_rst_val_c;
126 2 zero_gravi
    elsif rising_edge(clk_i) then
127
      -- defaults --
128 56 zero_gravi
      start_div <= '0';
129 69 zero_gravi
      out_en    <= '0';
130
      valid_o   <= '0';
131 2 zero_gravi
 
132
      -- FSM --
133
      case state is
134 69 zero_gravi
 
135 2 zero_gravi
        when IDLE =>
136 56 zero_gravi
          cp_op_ff <= cp_op;
137 69 zero_gravi
          cnt      <= "11110";
138 19 zero_gravi
          if (start_i = '1') then
139 61 zero_gravi
            if (operation = '1') and (DIVISION_EN = true) then -- division
140 69 zero_gravi
              start_div <= '1';
141
              state     <= DIV_PREPROCESS;
142
            else -- multiplication
143 56 zero_gravi
              if (FAST_MUL_EN = true) then
144 69 zero_gravi
                valid_o <= '1';
145
                state   <= FINALIZE;
146 56 zero_gravi
              else
147
                state <= PROCESSING;
148
              end if;
149
            end if;
150 2 zero_gravi
          end if;
151
 
152 56 zero_gravi
        when DIV_PREPROCESS =>
153 73 zero_gravi
          -- check relevant input signs for result sign compensation --
154
          if (cp_op = cp_op_div_c) then -- signed div operation
155
            div_res_corr <= (rs1_i(rs1_i'left) xor rs2_i(rs2_i'left)) and or_reduce_f(rs2_i); -- different signs AND rs2 not zero
156
          elsif (cp_op = cp_op_rem_c) then -- signed rem operation
157 69 zero_gravi
            div_res_corr <= rs1_i(rs1_i'left);
158 2 zero_gravi
          else
159 69 zero_gravi
            div_res_corr <= '0';
160 2 zero_gravi
          end if;
161 69 zero_gravi
          -- abs(rs2) --
162
          if ((rs2_i(rs2_i'left) and rs2_is_signed) = '1') then -- signed division?
163
            div_opy <= std_ulogic_vector(0 - unsigned(rs2_i)); -- make positive
164
          else
165
            div_opy <= rs2_i;
166
          end if;
167
          state <= PROCESSING;
168 2 zero_gravi
 
169
        when PROCESSING =>
170
          cnt <= std_ulogic_vector(unsigned(cnt) - 1);
171 71 zero_gravi
          if (cnt = "00000") or (ctrl_i(ctrl_trap_c) = '1') then -- abort on trap
172 69 zero_gravi
            valid_o <= '1';
173
            state   <= FINALIZE;
174 2 zero_gravi
          end if;
175
 
176
        when FINALIZE =>
177 69 zero_gravi
          out_en <= '1';
178
          state  <= IDLE;
179 2 zero_gravi
 
180 69 zero_gravi
        when others =>
181 39 zero_gravi
          state <= IDLE;
182 2 zero_gravi
      end case;
183
    end if;
184
  end process coprocessor_ctrl;
185
 
186 36 zero_gravi
  -- co-processor command --
187
  cp_op <= ctrl_i(ctrl_ir_funct3_2_c downto ctrl_ir_funct3_0_c);
188
 
189 56 zero_gravi
  -- operation: 0=mul, 1=div --
190
  operation <= '1' when (cp_op(2) = '1') else '0';
191 2 zero_gravi
 
192
  -- opx (rs1) signed? --
193 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';
194 2 zero_gravi
 
195
  -- opy (rs2) signed? --
196 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';
197 2 zero_gravi
 
198 56 zero_gravi
  -- start MUL operation (do it fast!) --
199
  start_mul <= '1' when (state = IDLE) and (start_i = '1') and (operation = '0') else '0';
200 2 zero_gravi
 
201 56 zero_gravi
 
202 36 zero_gravi
  -- Multiplier Core (signed/unsigned) ------------------------------------------------------
203 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
204 61 zero_gravi
  -- iterative multiplication (bit-serial) --
205
  multiplier_core_serial:
206
  if (FAST_MUL_EN = false) generate
207
    multiplier_core: process(rstn_i, clk_i)
208
    begin
209
      if (rstn_i = '0') then
210
        mul_product <= (others => def_rst_val_c);
211
      elsif rising_edge(clk_i) then
212 56 zero_gravi
        if (start_mul = '1') then -- start new multiplication
213 61 zero_gravi
          mul_product(63 downto 32) <= (others => '0');
214
          mul_product(31 downto 00) <= rs2_i;
215 56 zero_gravi
        elsif (state = PROCESSING) or (state = FINALIZE) then -- processing step or sign-finalization step
216 61 zero_gravi
          mul_product(63 downto 31) <= mul_do_add(32 downto 0);
217
          mul_product(30 downto 00) <= mul_product(31 downto 1);
218 12 zero_gravi
        end if;
219 56 zero_gravi
      end if;
220 61 zero_gravi
    end process multiplier_core;
221
  end generate;
222 56 zero_gravi
 
223 69 zero_gravi
  -- parallel multiplication (using DSP blocks) --
224 61 zero_gravi
  multiplier_core_dsp:
225
  if (FAST_MUL_EN = true) generate
226
    multiplier_core: process(clk_i)
227 69 zero_gravi
      variable tmp_v : signed(65 downto 0);
228 61 zero_gravi
    begin
229
      if rising_edge(clk_i) then
230 56 zero_gravi
        if (start_mul = '1') then
231
          mul_op_x <= signed((rs1_i(rs1_i'left) and rs1_is_signed) & rs1_i);
232
          mul_op_y <= signed((rs2_i(rs2_i'left) and rs2_is_signed) & rs2_i);
233 12 zero_gravi
        end if;
234 69 zero_gravi
        tmp_v := mul_op_x * mul_op_y;
235
        mul_product <= std_ulogic_vector(tmp_v(63 downto 0));
236
        --mul_buf_ff  <= mul_op_x * mul_op_y;
237
        --mul_product <= std_ulogic_vector(mul_buf_ff(63 downto 0)); -- let the register balancing do the magic here
238 2 zero_gravi
      end if;
239 61 zero_gravi
    end process multiplier_core;
240
  end generate;
241 2 zero_gravi
 
242 61 zero_gravi
  -- do another addition (bit-serial) --
243 56 zero_gravi
  mul_update: process(mul_product, mul_sign_cycle, mul_p_sext, rs1_is_signed, rs1_i)
244 2 zero_gravi
  begin
245 56 zero_gravi
    -- current bit of rs2_i to take care of --
246 12 zero_gravi
    if (mul_product(0) = '1') then -- multiply with 1
247
      if (mul_sign_cycle = '1') then -- for signed operations only: take care of negative weighted MSB -> multiply with -1
248 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));
249 12 zero_gravi
      else -- multiply with +1
250 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));
251 2 zero_gravi
      end if;
252 12 zero_gravi
    else -- multiply with 0
253 2 zero_gravi
      mul_do_add <= mul_p_sext & mul_product(63 downto 32);
254
    end if;
255
  end process mul_update;
256
 
257
  -- sign control --
258 56 zero_gravi
  mul_sign_cycle <= rs2_is_signed when (state = FINALIZE) else '0';
259
  mul_p_sext     <= mul_product(mul_product'left) and rs1_is_signed;
260 2 zero_gravi
 
261
 
262 12 zero_gravi
  -- Divider Core (unsigned) ----------------------------------------------------------------
263 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
264 61 zero_gravi
  divider_core_serial:
265
  if (DIVISION_EN = true) generate
266
    divider_core: process(rstn_i, clk_i)
267
    begin
268
      if (rstn_i = '0') then
269
        quotient  <= (others => def_rst_val_c);
270
        remainder <= (others => def_rst_val_c);
271
      elsif rising_edge(clk_i) then
272
        if (start_div = '1') then -- start new division
273 69 zero_gravi
          if ((rs1_i(rs1_i'left) and rs1_is_signed) = '1') then -- signed division?
274
            quotient <= std_ulogic_vector(0 - unsigned(rs1_i)); -- make positive
275
          else
276
            quotient <= rs1_i;
277
          end if;
278 61 zero_gravi
          remainder <= (others => '0');
279
        elsif (state = PROCESSING) or (state = FINALIZE) then -- running?
280
          quotient <= quotient(30 downto 0) & (not div_sub(32));
281
          if (div_sub(32) = '0') then -- still overflowing
282
            remainder <= div_sub(31 downto 0);
283
          else -- underflow
284
            remainder <= remainder(30 downto 0) & quotient(31);
285
          end if;
286 2 zero_gravi
        end if;
287
      end if;
288 61 zero_gravi
    end process divider_core;
289 2 zero_gravi
 
290 61 zero_gravi
    -- try another subtraction --
291
    div_sub <= std_ulogic_vector(unsigned('0' & remainder(30 downto 0) & quotient(31)) - unsigned('0' & div_opy));
292 2 zero_gravi
 
293 61 zero_gravi
    -- result sign compensation --
294 73 zero_gravi
    div_sign_comp_in <= quotient when (cp_op = cp_op_div_c) or (cp_op = cp_op_divu_c) else remainder;
295 61 zero_gravi
    div_sign_comp    <= std_ulogic_vector(0 - unsigned(div_sign_comp_in));
296 73 zero_gravi
    div_res          <= div_sign_comp when (div_res_corr = '1') else div_sign_comp_in;
297 61 zero_gravi
  end generate;
298 2 zero_gravi
 
299 61 zero_gravi
  -- no divider --
300
  divider_core_serial_none:
301
  if (DIVISION_EN = false) generate
302 73 zero_gravi
    remainder        <= (others => '0');
303
    quotient         <= (others => '0');
304
    div_sub          <= (others => '0');
305
    div_sign_comp_in <= (others => '0');
306
    div_sign_comp    <= (others => '0');
307
    div_res          <= (others => '0');
308 61 zero_gravi
  end generate;
309 2 zero_gravi
 
310 61 zero_gravi
 
311 2 zero_gravi
  -- Data Output ----------------------------------------------------------------------------
312
  -- -------------------------------------------------------------------------------------------
313 73 zero_gravi
  operation_result: process(out_en, cp_op_ff, mul_product, div_res, div_sign_comp_in)
314 2 zero_gravi
  begin
315 69 zero_gravi
    if (out_en = '1') then
316
      case cp_op_ff is
317
        when cp_op_mul_c =>
318
          res_o <= mul_product(31 downto 00);
319
        when cp_op_mulh_c | cp_op_mulhsu_c | cp_op_mulhu_c =>
320
          res_o <= mul_product(63 downto 32);
321 73 zero_gravi
        when cp_op_div_c | cp_op_rem_c =>
322 69 zero_gravi
          res_o <= div_res;
323 73 zero_gravi
        when others => -- cp_op_divu_c | cp_op_remu_c
324
          res_o <= div_sign_comp_in;
325 69 zero_gravi
      end case;
326
    else
327 47 zero_gravi
      res_o <= (others => '0');
328 2 zero_gravi
    end if;
329
  end process operation_result;
330
 
331
 
332
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.