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 4

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
-- # Multiplier core (signed/unsigned) uses serial algorithm. -> 32+8 cycles latency               #
6
-- # Divider core (unsigned) uses serial algorithm. -> 32+8 cycles latency                         #
7
-- # ********************************************************************************************* #
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
  -- controller --
64
  type state_t is (IDLE, DECODE, INIT_OPX, INIT_OPY, PROCESSING, FINALIZE, COMPLETED);
65
  signal state         : state_t;
66
  signal cnt           : std_ulogic_vector(4 downto 0);
67
  signal cp_op         : std_ulogic_vector(2 downto 0); -- operation to execute
68
  signal start         : std_ulogic;
69
  signal operation     : std_ulogic;
70
  signal opx, opy      : std_ulogic_vector(data_width_c-1 downto 0); -- input operands
71
  signal opx_is_signed : std_ulogic;
72
  signal opy_is_signed : std_ulogic;
73
  signal div_res_corr  : std_ulogic;
74
 
75
  -- divider core --
76
  signal remainder        : std_ulogic_vector(data_width_c-1 downto 0);
77
  signal quotient         : std_ulogic_vector(data_width_c-1 downto 0);
78
  signal div_sub          : std_ulogic_vector(data_width_c   downto 0);
79
  signal div_sign_comp_in : std_ulogic_vector(data_width_c-1 downto 0);
80
  signal div_sign_comp    : std_ulogic_vector(data_width_c-1 downto 0);
81
  signal div_res          : std_ulogic_vector(data_width_c-1 downto 0);
82
 
83
  -- multiplier core --
84
  signal mul_product    : std_ulogic_vector(63 downto 0);
85
  signal mul_do_add     : std_ulogic_vector(32 downto 0);
86
  signal mul_sign_cycle : std_ulogic;
87
  signal mul_p_sext     : std_ulogic;
88
 
89
begin
90
 
91
  -- Co-Processor Controller ----------------------------------------------------------------
92
  -- -------------------------------------------------------------------------------------------
93
  coprocessor_ctrl: process(rstn_i, clk_i)
94
  begin
95
    if (rstn_i = '0') then
96
      state        <= IDLE;
97
      cp_op        <= (others => '0');
98 3 zero_gravi
      opx          <= (others => '0');
99
      opy          <= (others => '0');
100
      cnt          <= (others => '0');
101
      start        <= '0';
102 2 zero_gravi
      valid_o      <= '0';
103 3 zero_gravi
      div_res_corr <= '0';
104 2 zero_gravi
    elsif rising_edge(clk_i) then
105
      -- defaults --
106
      start   <= '0';
107
      valid_o <= '0';
108
 
109
      -- FSM --
110
      case state is
111
        when IDLE =>
112
          opx   <= rs1_i;
113
          opy   <= rs2_i;
114 4 zero_gravi
          cp_op <= ctrl_i(ctrl_cp_cmd2_c downto ctrl_cp_cmd0_c);
115 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
116
            state <= DECODE;
117
          end if;
118
 
119
        when DECODE =>
120 4 zero_gravi
          cnt <= "11111";
121 2 zero_gravi
          if (cp_op = cp_op_div_c) or (cp_op = cp_op_rem_c) then -- result sign compensation for div?
122
            div_res_corr <= opx(opx'left) xor opy(opy'left);
123
          else
124
            div_res_corr <= '0';
125
          end if;
126
          if (operation = '1') then -- division
127
            state <= INIT_OPX;
128
          else -- multiplication
129
            start <= '1';
130
            state <= PROCESSING;
131
          end if;
132
 
133
        when INIT_OPX =>
134
          if ((opx(opx'left) and opx_is_signed) = '1') then -- signed division?
135
            opx <= div_sign_comp; -- make positive
136
          end if;
137
          state <= INIT_OPY;
138
 
139
        when INIT_OPY =>
140
          start <= '1';
141
          if ((opy(opy'left) and opy_is_signed) = '1') then -- signed division?
142
            opy <= div_sign_comp; -- make positive
143
          end if;
144
          state <= PROCESSING;
145
 
146
        when PROCESSING =>
147
          cnt <= std_ulogic_vector(unsigned(cnt) - 1);
148
          if (cnt = "00000") then
149
            state <= FINALIZE;
150
          end if;
151
 
152
        when FINALIZE =>
153
          state <= COMPLETED;
154
 
155
        when COMPLETED =>
156
          valid_o <= '1';
157
          state   <= IDLE;
158
      end case;
159
    end if;
160
  end process coprocessor_ctrl;
161
 
162
  -- operation --
163
  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';
164
 
165
  -- opx (rs1) signed? --
166
  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';
167
 
168
  -- opy (rs2) signed? --
169
  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';
170
 
171
 
172
  -- Multiplier Core ------------------------------------------------------------------------
173
  -- -------------------------------------------------------------------------------------------
174
  multiplier_core: process(clk_i)
175
  begin
176
    if rising_edge(clk_i) then
177
      if (start = '1') then -- start new multiplication
178
        mul_product(63 downto 32) <= (others => (opy(opy'left) and opy_is_signed)); -- sign extension
179
        mul_product(31 downto 00) <= opy;
180
      elsif ((state = PROCESSING) or (state = FINALIZE)) and (operation = '0') then
181
        mul_product(63 downto 31) <= mul_do_add(32 downto 0);
182
        mul_product(30 downto 00) <= mul_product(31 downto 1);
183
      end if;
184
    end if;
185
  end process multiplier_core;
186
 
187
  -- MUL: do another addition --
188 4 zero_gravi
  mul_update: process(mul_product, mul_sign_cycle, mul_p_sext, opx_is_signed, opx)
189 2 zero_gravi
  begin
190
    if (mul_product(0) = '1') then
191
      if (mul_sign_cycle = '1') then -- for signed operation only: take care of negative weighted MSB
192
        mul_do_add <= std_ulogic_vector(unsigned(mul_p_sext & mul_product(63 downto 32)) - unsigned((opx(opy'left) and opx_is_signed) & opx));
193
      else
194
        mul_do_add <= std_ulogic_vector(unsigned(mul_p_sext & mul_product(63 downto 32)) + unsigned((opx(opy'left) and opx_is_signed) & opx));
195
      end if;
196
    else
197
      mul_do_add <= mul_p_sext & mul_product(63 downto 32);
198
    end if;
199
  end process mul_update;
200
 
201
  -- sign control --
202
  mul_sign_cycle <= opy_is_signed when (state = FINALIZE) else '0';
203
  mul_p_sext     <= mul_product(mul_product'left) and opx_is_signed;
204
 
205
 
206
  -- Divider Core ---------------------------------------------------------------------------
207
  -- -------------------------------------------------------------------------------------------
208
  divider_core: process(clk_i)
209
  begin
210
    if rising_edge(clk_i) then
211
      if (start = '1') then -- start new division
212
        quotient  <= opx;
213
        remainder <= (others => '0');
214
      elsif ((state = PROCESSING) or (state = FINALIZE)) and (operation = '1') then -- running?
215
        quotient <= quotient(30 downto 0) & (not div_sub(32));
216
        if (div_sub(32) = '0') then -- still overflowing
217
          remainder <= div_sub(31 downto 0);
218
        else -- underflow
219
          remainder <= remainder(30 downto 0) & quotient(31);
220
        end if;
221
      end if;
222
    end if;
223
  end process divider_core;
224
 
225
  -- DIV: try another subtraction --
226
  div_sub <= std_ulogic_vector(unsigned('0' & remainder(30 downto 0) & quotient(31)) - unsigned('0' & opy));
227
 
228
  -- Div sign compensation --
229
  div_sign_comp_in <= opx when (state = INIT_OPX) else
230
                      opy when (state = INIT_OPY) else
231
                      quotient when ((cp_op = cp_op_div_c) or (cp_op = cp_op_divu_c)) else remainder;
232
  div_sign_comp <= std_ulogic_vector(0 - unsigned(div_sign_comp_in));
233
 
234
  -- result sign correction --
235
  div_res <= div_sign_comp when (div_res_corr = '1') else div_sign_comp_in;
236
 
237
 
238
  -- Data Output ----------------------------------------------------------------------------
239
  -- -------------------------------------------------------------------------------------------
240
  operation_result: process(clk_i)
241
  begin
242
    if rising_edge(clk_i) then
243
      case cp_op is
244
        when cp_op_mul_c =>
245
          res_o <= mul_product(31 downto 00);
246
        when cp_op_mulh_c | cp_op_mulhsu_c | cp_op_mulhu_c =>
247
          res_o <= mul_product(63 downto 32);
248
        when cp_op_div_c =>
249
          res_o <= div_res;
250
        when cp_op_divu_c =>
251
          res_o <= quotient;
252
        when cp_op_rem_c =>
253
          res_o <= div_res;
254
        when cp_op_remu_c =>
255
          res_o <= remainder;
256
        when others => -- undefined
257 3 zero_gravi
          res_o <= (others => '0');
258 2 zero_gravi
      end case;
259
    end if;
260
  end process operation_result;
261
 
262
 
263
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.