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 39

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 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
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
12
-- #                                                                                               #
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 2 zero_gravi
  -- controller --
72 22 zero_gravi
  type state_t is (IDLE, DECODE, INIT_OPX, INIT_OPY, PROCESSING, FINALIZE, COMPLETED, FAST_MUL);
73 2 zero_gravi
  signal state         : state_t;
74
  signal cnt           : std_ulogic_vector(4 downto 0);
75
  signal cp_op         : std_ulogic_vector(2 downto 0); -- operation to execute
76 39 zero_gravi
  signal cp_op_ff      : std_ulogic_vector(2 downto 0); -- operation that was executed
77 2 zero_gravi
  signal start         : std_ulogic;
78
  signal operation     : std_ulogic;
79
  signal opx, opy      : std_ulogic_vector(data_width_c-1 downto 0); -- input operands
80
  signal opx_is_signed : std_ulogic;
81
  signal opy_is_signed : std_ulogic;
82 6 zero_gravi
  signal opy_is_zero   : std_ulogic;
83 2 zero_gravi
  signal div_res_corr  : std_ulogic;
84 39 zero_gravi
  signal valid         : std_ulogic;
85 2 zero_gravi
 
86
  -- divider core --
87
  signal remainder        : std_ulogic_vector(data_width_c-1 downto 0);
88
  signal quotient         : std_ulogic_vector(data_width_c-1 downto 0);
89
  signal div_sub          : std_ulogic_vector(data_width_c   downto 0);
90
  signal div_sign_comp_in : std_ulogic_vector(data_width_c-1 downto 0);
91
  signal div_sign_comp    : std_ulogic_vector(data_width_c-1 downto 0);
92
  signal div_res          : std_ulogic_vector(data_width_c-1 downto 0);
93
 
94
  -- multiplier core --
95
  signal mul_product    : std_ulogic_vector(63 downto 0);
96 12 zero_gravi
  signal mul_do_add     : std_ulogic_vector(data_width_c downto 0);
97 2 zero_gravi
  signal mul_sign_cycle : std_ulogic;
98
  signal mul_p_sext     : std_ulogic;
99 19 zero_gravi
  signal mul_op_x       : signed(32 downto 0); -- for using DSPs
100
  signal mul_op_y       : signed(32 downto 0); -- for using DSPs
101
  signal mul_buf_ff     : signed(65 downto 0); -- for using DSPs
102
  signal mul_buf2_ff    : signed(65 downto 0); -- for using DSPs
103 2 zero_gravi
 
104
begin
105
 
106
  -- Co-Processor Controller ----------------------------------------------------------------
107
  -- -------------------------------------------------------------------------------------------
108
  coprocessor_ctrl: process(rstn_i, clk_i)
109
  begin
110
    if (rstn_i = '0') then
111
      state        <= IDLE;
112 3 zero_gravi
      opx          <= (others => '0');
113
      opy          <= (others => '0');
114
      cnt          <= (others => '0');
115
      start        <= '0';
116 39 zero_gravi
      valid        <= '0';
117 3 zero_gravi
      div_res_corr <= '0';
118 6 zero_gravi
      opy_is_zero  <= '0';
119 39 zero_gravi
      cp_op_ff     <= (others => '0');
120 2 zero_gravi
    elsif rising_edge(clk_i) then
121
      -- defaults --
122 39 zero_gravi
      start    <= '0';
123
      valid    <= '0';
124
      cp_op_ff <= cp_op;
125 2 zero_gravi
 
126
      -- FSM --
127
      case state is
128
        when IDLE =>
129 19 zero_gravi
          opx <= rs1_i;
130
          opy <= rs2_i;
131
          if (start_i = '1') then
132 2 zero_gravi
            state <= DECODE;
133
          end if;
134
 
135
        when DECODE =>
136 12 zero_gravi
          --
137 6 zero_gravi
          if (cp_op = cp_op_div_c) then -- result sign compensation for div?
138 2 zero_gravi
            div_res_corr <= opx(opx'left) xor opy(opy'left);
139 6 zero_gravi
          elsif (cp_op = cp_op_rem_c) then -- result sign compensation for rem?
140
            div_res_corr <= opx(opx'left);
141 2 zero_gravi
          else
142
            div_res_corr <= '0';
143
          end if;
144 12 zero_gravi
          --
145
          if (or_all_f(opy) = '0') then -- *divide* by 0?
146 6 zero_gravi
            opy_is_zero <= '1';
147
          else
148
            opy_is_zero <= '0';
149
          end if;
150 12 zero_gravi
          --
151 22 zero_gravi
          cnt   <= "11111";
152 2 zero_gravi
          if (operation = '1') then -- division
153
            state <= INIT_OPX;
154
          else -- multiplication
155 22 zero_gravi
            start <= '1';
156
            if (FAST_MUL_EN = true) then
157
              state <= FAST_MUL;
158 12 zero_gravi
            else
159 22 zero_gravi
              state <= PROCESSING;
160 12 zero_gravi
            end if;
161 2 zero_gravi
          end if;
162
 
163
        when INIT_OPX =>
164
          if ((opx(opx'left) and opx_is_signed) = '1') then -- signed division?
165
            opx <= div_sign_comp; -- make positive
166
          end if;
167
          state <= INIT_OPY;
168
 
169
        when INIT_OPY =>
170
          start <= '1';
171
          if ((opy(opy'left) and opy_is_signed) = '1') then -- signed division?
172
            opy <= div_sign_comp; -- make positive
173
          end if;
174
          state <= PROCESSING;
175
 
176
        when PROCESSING =>
177
          cnt <= std_ulogic_vector(unsigned(cnt) - 1);
178
          if (cnt = "00000") then
179
            state <= FINALIZE;
180
          end if;
181
 
182 22 zero_gravi
        when FAST_MUL =>
183
          state <= FINALIZE;
184
 
185 2 zero_gravi
        when FINALIZE =>
186
          state <= COMPLETED;
187
 
188
        when COMPLETED =>
189 39 zero_gravi
          valid <= '1';
190
          state <= IDLE;
191 2 zero_gravi
      end case;
192
    end if;
193
  end process coprocessor_ctrl;
194
 
195 36 zero_gravi
  -- co-processor command --
196
  cp_op <= ctrl_i(ctrl_ir_funct3_2_c downto ctrl_ir_funct3_0_c);
197
 
198 2 zero_gravi
  -- operation --
199
  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';
200
 
201
  -- opx (rs1) signed? --
202
  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';
203
 
204
  -- opy (rs2) signed? --
205
  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';
206
 
207
 
208 36 zero_gravi
  -- Multiplier Core (signed/unsigned) ------------------------------------------------------
209 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
210
  multiplier_core: process(clk_i)
211
  begin
212
    if rising_edge(clk_i) then
213 36 zero_gravi
      -- ---------------------------------------------------------
214 12 zero_gravi
      if (FAST_MUL_EN = false) then -- use small iterative computation
215
        if (start = '1') then -- start new multiplication
216
          mul_product(63 downto 32) <= (others => '0');
217
          mul_product(31 downto 00) <= opy;
218
        elsif ((state = PROCESSING) or (state = FINALIZE)) and (operation = '0') then
219
          mul_product(63 downto 31) <= mul_do_add(32 downto 0);
220
          mul_product(30 downto 00) <= mul_product(31 downto 1);
221
        end if;
222 36 zero_gravi
      -- ---------------------------------------------------------
223 12 zero_gravi
      else -- use direct approach using (several!) DSP blocks
224
        if (start = '1') then
225 19 zero_gravi
          mul_op_x <= signed((opx(opx'left) and opx_is_signed) & opx);
226
          mul_op_y <= signed((opy(opy'left) and opy_is_signed) & opy);
227 12 zero_gravi
        end if;
228 19 zero_gravi
        mul_buf_ff <= mul_op_x * mul_op_y;
229
        if (dsp_add_reg_stage_c = true) then -- add another reg stage?
230
          mul_buf2_ff <= mul_buf_ff;
231
          mul_product <= std_ulogic_vector(mul_buf2_ff(63 downto 0)); -- let the register balancing do the magic here
232
        else
233
          mul_product <= std_ulogic_vector(mul_buf_ff(63 downto 0)); -- let the register balancing do the magic here
234
        end if;
235 2 zero_gravi
      end if;
236
    end if;
237
  end process multiplier_core;
238
 
239
  -- MUL: do another addition --
240 4 zero_gravi
  mul_update: process(mul_product, mul_sign_cycle, mul_p_sext, opx_is_signed, opx)
241 2 zero_gravi
  begin
242 12 zero_gravi
    -- current bit of opy to take care of --
243
    if (mul_product(0) = '1') then -- multiply with 1
244
      if (mul_sign_cycle = '1') then -- for signed operations only: take care of negative weighted MSB -> multiply with -1
245 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));
246 12 zero_gravi
      else -- multiply with +1
247 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));
248 2 zero_gravi
      end if;
249 12 zero_gravi
    else -- multiply with 0
250 2 zero_gravi
      mul_do_add <= mul_p_sext & mul_product(63 downto 32);
251
    end if;
252
  end process mul_update;
253
 
254
  -- sign control --
255
  mul_sign_cycle <= opy_is_signed when (state = FINALIZE) else '0';
256
  mul_p_sext     <= mul_product(mul_product'left) and opx_is_signed;
257
 
258
 
259 12 zero_gravi
  -- Divider Core (unsigned) ----------------------------------------------------------------
260 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
261
  divider_core: process(clk_i)
262
  begin
263
    if rising_edge(clk_i) then
264
      if (start = '1') then -- start new division
265
        quotient  <= opx;
266
        remainder <= (others => '0');
267
      elsif ((state = PROCESSING) or (state = FINALIZE)) and (operation = '1') then -- running?
268
        quotient <= quotient(30 downto 0) & (not div_sub(32));
269
        if (div_sub(32) = '0') then -- still overflowing
270
          remainder <= div_sub(31 downto 0);
271
        else -- underflow
272
          remainder <= remainder(30 downto 0) & quotient(31);
273
        end if;
274
      end if;
275
    end if;
276
  end process divider_core;
277
 
278
  -- DIV: try another subtraction --
279
  div_sub <= std_ulogic_vector(unsigned('0' & remainder(30 downto 0) & quotient(31)) - unsigned('0' & opy));
280
 
281
  -- Div sign compensation --
282
  div_sign_comp_in <= opx when (state = INIT_OPX) else
283
                      opy when (state = INIT_OPY) else
284
                      quotient when ((cp_op = cp_op_div_c) or (cp_op = cp_op_divu_c)) else remainder;
285
  div_sign_comp <= std_ulogic_vector(0 - unsigned(div_sign_comp_in));
286
 
287
  -- result sign correction --
288 6 zero_gravi
  div_res <= div_sign_comp when (div_res_corr = '1') and (opy_is_zero = '0') else div_sign_comp_in;
289 2 zero_gravi
 
290
 
291
  -- Data Output ----------------------------------------------------------------------------
292
  -- -------------------------------------------------------------------------------------------
293 39 zero_gravi
  operation_result: process(valid, cp_op_ff, mul_product, div_res, quotient, opy_is_zero, rs1_i, remainder)
294 2 zero_gravi
  begin
295 39 zero_gravi
    if (valid = '1') then
296
      valid_o <= '1';
297
      case cp_op_ff is
298 2 zero_gravi
        when cp_op_mul_c =>
299
          res_o <= mul_product(31 downto 00);
300
        when cp_op_mulh_c | cp_op_mulhsu_c | cp_op_mulhu_c =>
301
          res_o <= mul_product(63 downto 32);
302
        when cp_op_div_c =>
303
          res_o <= div_res;
304
        when cp_op_divu_c =>
305
          res_o <= quotient;
306
        when cp_op_rem_c =>
307 6 zero_gravi
          if (opy_is_zero = '0') then
308
            res_o <= div_res;
309
          else
310 39 zero_gravi
            res_o <= rs1_i;
311 6 zero_gravi
          end if;
312 39 zero_gravi
        when others => -- cp_op_remu_c
313 2 zero_gravi
          res_o <= remainder;
314
      end case;
315 39 zero_gravi
    else
316
      valid_o <= '0';
317
      res_o   <= (others => '0');
318 2 zero_gravi
    end if;
319
  end process operation_result;
320
 
321
 
322
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.