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 44

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

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