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 6

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 2 zero_gravi
-- # ********************************************************************************************* #
8
-- # BSD 3-Clause License                                                                          #
9
-- #                                                                                               #
10
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
11
-- #                                                                                               #
12
-- # Redistribution and use in source and binary forms, with or without modification, are          #
13
-- # permitted provided that the following conditions are met:                                     #
14
-- #                                                                                               #
15
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
16
-- #    conditions and the following disclaimer.                                                   #
17
-- #                                                                                               #
18
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
19
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
20
-- #    provided with the distribution.                                                            #
21
-- #                                                                                               #
22
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
23
-- #    endorse or promote products derived from this software without specific prior written      #
24
-- #    permission.                                                                                #
25
-- #                                                                                               #
26
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
27
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
28
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
29
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
30
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
31
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
32
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
33
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
34
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
35
-- # ********************************************************************************************* #
36
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
37
-- #################################################################################################
38
 
39
library ieee;
40
use ieee.std_logic_1164.all;
41
use ieee.numeric_std.all;
42
 
43
library neorv32;
44
use neorv32.neorv32_package.all;
45
 
46
entity neorv32_cpu_cp_muldiv is
47
  port (
48
    -- global control --
49
    clk_i   : in  std_ulogic; -- global clock, rising edge
50
    rstn_i  : in  std_ulogic; -- global reset, low-active, async
51
    ctrl_i  : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
52
    -- data input --
53
    rs1_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
54
    rs2_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 2
55
    -- result and status --
56
    res_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- operation result
57
    valid_o : out std_ulogic -- data output valid
58
  );
59
end neorv32_cpu_cp_muldiv;
60
 
61
architecture neorv32_cpu_cp_muldiv_rtl of neorv32_cpu_cp_muldiv is
62
 
63 6 zero_gravi
  -- constants --
64
  constant all_zero_c : std_ulogic_vector(data_width_c-1 downto 0) := (others => '0');
65
 
66 2 zero_gravi
  -- controller --
67
  type state_t is (IDLE, DECODE, INIT_OPX, INIT_OPY, PROCESSING, FINALIZE, COMPLETED);
68
  signal state         : state_t;
69
  signal cnt           : std_ulogic_vector(4 downto 0);
70
  signal cp_op         : std_ulogic_vector(2 downto 0); -- operation to execute
71
  signal start         : std_ulogic;
72
  signal operation     : std_ulogic;
73
  signal opx, opy      : std_ulogic_vector(data_width_c-1 downto 0); -- input operands
74
  signal opx_is_signed : std_ulogic;
75
  signal opy_is_signed : std_ulogic;
76 6 zero_gravi
  signal opy_is_zero   : std_ulogic;
77 2 zero_gravi
  signal div_res_corr  : std_ulogic;
78
 
79
  -- divider core --
80
  signal remainder        : std_ulogic_vector(data_width_c-1 downto 0);
81
  signal quotient         : std_ulogic_vector(data_width_c-1 downto 0);
82
  signal div_sub          : std_ulogic_vector(data_width_c   downto 0);
83
  signal div_sign_comp_in : std_ulogic_vector(data_width_c-1 downto 0);
84
  signal div_sign_comp    : std_ulogic_vector(data_width_c-1 downto 0);
85
  signal div_res          : std_ulogic_vector(data_width_c-1 downto 0);
86
 
87
  -- multiplier core --
88
  signal mul_product    : std_ulogic_vector(63 downto 0);
89
  signal mul_do_add     : std_ulogic_vector(32 downto 0);
90
  signal mul_sign_cycle : std_ulogic;
91
  signal mul_p_sext     : std_ulogic;
92
 
93
begin
94
 
95
  -- Co-Processor Controller ----------------------------------------------------------------
96
  -- -------------------------------------------------------------------------------------------
97
  coprocessor_ctrl: process(rstn_i, clk_i)
98
  begin
99
    if (rstn_i = '0') then
100
      state        <= IDLE;
101
      cp_op        <= (others => '0');
102 3 zero_gravi
      opx          <= (others => '0');
103
      opy          <= (others => '0');
104
      cnt          <= (others => '0');
105
      start        <= '0';
106 2 zero_gravi
      valid_o      <= '0';
107 3 zero_gravi
      div_res_corr <= '0';
108 6 zero_gravi
      opy_is_zero  <= '0';
109 2 zero_gravi
    elsif rising_edge(clk_i) then
110
      -- defaults --
111
      start   <= '0';
112
      valid_o <= '0';
113
 
114
      -- FSM --
115
      case state is
116
        when IDLE =>
117
          opx   <= rs1_i;
118
          opy   <= rs2_i;
119 4 zero_gravi
          cp_op <= ctrl_i(ctrl_cp_cmd2_c downto ctrl_cp_cmd0_c);
120 2 zero_gravi
          if (ctrl_i(ctrl_cp_use_c) = '1') and (ctrl_i(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) = cp_sel_muldiv_c) then
121
            state <= DECODE;
122
          end if;
123
 
124
        when DECODE =>
125 4 zero_gravi
          cnt <= "11111";
126 6 zero_gravi
          if (cp_op = cp_op_div_c) then -- result sign compensation for div?
127 2 zero_gravi
            div_res_corr <= opx(opx'left) xor opy(opy'left);
128 6 zero_gravi
          elsif (cp_op = cp_op_rem_c) then -- result sign compensation for rem?
129
            div_res_corr <= opx(opx'left);
130 2 zero_gravi
          else
131
            div_res_corr <= '0';
132
          end if;
133 6 zero_gravi
--        if (cp_op = cp_op_div_c) and (opy = all_zero_c) then -- *divide* by 0?
134
          if (opy = all_zero_c) then -- *divide* by 0?
135
            opy_is_zero <= '1';
136
          else
137
            opy_is_zero <= '0';
138
          end if;
139 2 zero_gravi
          if (operation = '1') then -- division
140
            state <= INIT_OPX;
141
          else -- multiplication
142
            start <= '1';
143
            state <= PROCESSING;
144
          end if;
145
 
146
        when INIT_OPX =>
147
          if ((opx(opx'left) and opx_is_signed) = '1') then -- signed division?
148
            opx <= div_sign_comp; -- make positive
149
          end if;
150
          state <= INIT_OPY;
151
 
152
        when INIT_OPY =>
153
          start <= '1';
154
          if ((opy(opy'left) and opy_is_signed) = '1') then -- signed division?
155
            opy <= div_sign_comp; -- make positive
156
          end if;
157
          state <= PROCESSING;
158
 
159
        when PROCESSING =>
160
          cnt <= std_ulogic_vector(unsigned(cnt) - 1);
161
          if (cnt = "00000") then
162
            state <= FINALIZE;
163
          end if;
164
 
165
        when FINALIZE =>
166
          state <= COMPLETED;
167
 
168
        when COMPLETED =>
169
          valid_o <= '1';
170
          state   <= IDLE;
171
      end case;
172
    end if;
173
  end process coprocessor_ctrl;
174
 
175
  -- operation --
176
  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';
177
 
178
  -- opx (rs1) signed? --
179
  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';
180
 
181
  -- opy (rs2) signed? --
182
  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';
183
 
184
 
185
  -- Multiplier Core ------------------------------------------------------------------------
186
  -- -------------------------------------------------------------------------------------------
187
  multiplier_core: process(clk_i)
188
  begin
189
    if rising_edge(clk_i) then
190
      if (start = '1') then -- start new multiplication
191 6 zero_gravi
        mul_product(63 downto 32) <= (others => '0');
192 2 zero_gravi
        mul_product(31 downto 00) <= opy;
193
      elsif ((state = PROCESSING) or (state = FINALIZE)) and (operation = '0') then
194
        mul_product(63 downto 31) <= mul_do_add(32 downto 0);
195
        mul_product(30 downto 00) <= mul_product(31 downto 1);
196
      end if;
197
    end if;
198
  end process multiplier_core;
199
 
200
  -- MUL: do another addition --
201 4 zero_gravi
  mul_update: process(mul_product, mul_sign_cycle, mul_p_sext, opx_is_signed, opx)
202 2 zero_gravi
  begin
203
    if (mul_product(0) = '1') then
204
      if (mul_sign_cycle = '1') then -- for signed operation only: take care of negative weighted MSB
205 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));
206 2 zero_gravi
      else
207 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));
208 2 zero_gravi
      end if;
209
    else
210
      mul_do_add <= mul_p_sext & mul_product(63 downto 32);
211
    end if;
212
  end process mul_update;
213
 
214
  -- sign control --
215
  mul_sign_cycle <= opy_is_signed when (state = FINALIZE) else '0';
216
  mul_p_sext     <= mul_product(mul_product'left) and opx_is_signed;
217
 
218
 
219
  -- Divider Core ---------------------------------------------------------------------------
220
  -- -------------------------------------------------------------------------------------------
221
  divider_core: process(clk_i)
222
  begin
223
    if rising_edge(clk_i) then
224
      if (start = '1') then -- start new division
225
        quotient  <= opx;
226
        remainder <= (others => '0');
227
      elsif ((state = PROCESSING) or (state = FINALIZE)) and (operation = '1') then -- running?
228
        quotient <= quotient(30 downto 0) & (not div_sub(32));
229
        if (div_sub(32) = '0') then -- still overflowing
230
          remainder <= div_sub(31 downto 0);
231
        else -- underflow
232
          remainder <= remainder(30 downto 0) & quotient(31);
233
        end if;
234
      end if;
235
    end if;
236
  end process divider_core;
237
 
238
  -- DIV: try another subtraction --
239
  div_sub <= std_ulogic_vector(unsigned('0' & remainder(30 downto 0) & quotient(31)) - unsigned('0' & opy));
240
 
241
  -- Div sign compensation --
242
  div_sign_comp_in <= opx when (state = INIT_OPX) else
243
                      opy when (state = INIT_OPY) else
244
                      quotient when ((cp_op = cp_op_div_c) or (cp_op = cp_op_divu_c)) else remainder;
245
  div_sign_comp <= std_ulogic_vector(0 - unsigned(div_sign_comp_in));
246
 
247
  -- result sign correction --
248 6 zero_gravi
  div_res <= div_sign_comp when (div_res_corr = '1') and (opy_is_zero = '0') else div_sign_comp_in;
249 2 zero_gravi
 
250
 
251
  -- Data Output ----------------------------------------------------------------------------
252
  -- -------------------------------------------------------------------------------------------
253
  operation_result: process(clk_i)
254
  begin
255
    if rising_edge(clk_i) then
256 6 zero_gravi
      res_o <= (others => '0'); -- default
257 2 zero_gravi
      case cp_op is
258
        when cp_op_mul_c =>
259
          res_o <= mul_product(31 downto 00);
260
        when cp_op_mulh_c | cp_op_mulhsu_c | cp_op_mulhu_c =>
261
          res_o <= mul_product(63 downto 32);
262
        when cp_op_div_c =>
263
          res_o <= div_res;
264
        when cp_op_divu_c =>
265
          res_o <= quotient;
266
        when cp_op_rem_c =>
267 6 zero_gravi
          if (opy_is_zero = '0') then
268
            res_o <= div_res;
269
          else
270
            res_o <= opx;
271
          end if;
272 2 zero_gravi
        when cp_op_remu_c =>
273
          res_o <= remainder;
274
        when others => -- undefined
275 3 zero_gravi
          res_o <= (others => '0');
276 2 zero_gravi
      end case;
277
    end if;
278
  end process operation_result;
279
 
280
 
281
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.