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 36

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