OpenCores
URL https://opencores.org/ocsvn/ion/ion/trunk

Subversion Repositories ion

[/] [ion/] [trunk/] [vhdl/] [mips_mult.vhdl] - Blame information for rev 212

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

Line No. Rev Author Line
1 12 ja_rd
--------------------------------------------------------------------------------
2
-- mips_mult.vhdl -- multiplier from Plasma project, slightly modified.
3
--
4
-- The original file from Plasma has been adapted to the Ion core. Changes are
5
-- tagged with '@ion'. There are a few notes at the end of the file with the
6
-- rationale for the changes -- useful only if any trouble shows up later.
7
-- The structure has not changed, only a few implementation details.
8
--------------------------------------------------------------------------------
9
---------------------------------------------------------------------
10
-- TITLE: Multiplication and Division Unit
11
-- AUTHORS: Steve Rhoads (rhoadss@yahoo.com)
12
-- DATE CREATED: 1/31/01
13
-- FILENAME: mult.vhd
14
-- PROJECT: Plasma CPU core
15
-- COPYRIGHT: Software placed into the public domain by the author.
16
--    Software 'as is' without warranty.  Author liable for nothing.
17
-- DESCRIPTION:
18
--    Implements the multiplication and division unit in 32 clocks.
19
--
20
--    To reduce space, compile your code using the flag "-mno-mul" which 
21
--    will use software base routines in math.c if USE_SW_MULT is defined.
22
--    Then remove references to the entity mult in mlite_cpu.vhd.
23
--
24
-- MULTIPLICATION
25
-- long64 answer = 0
26
-- for(i = 0; i < 32; ++i)
27
-- {
28
--    answer = (answer >> 1) + (((b&1)?a:0) << 31);
29
--    b = b >> 1;
30
-- }
31
--
32
-- DIVISION
33
-- long upper=a, lower=0;
34
-- a = b << 31;
35
-- for(i = 0; i < 32; ++i)
36
-- {
37
--    lower = lower << 1;
38
--    if(upper >= a && a && b < 2)
39
--    {
40
--       upper = upper - a;
41
--       lower |= 1;
42
--    }
43
--    a = ((b&2) << 30) | (a >> 1);
44
--    b = b >> 1;
45
-- }
46
---------------------------------------------------------------------
47
library ieee;
48
use ieee.std_logic_1164.all;
49
use ieee.std_logic_unsigned.all;
50
use IEEE.std_logic_arith.all;
51
use work.mips_pkg.all;
52
 
53
entity mips_mult is
54
   generic(mult_type  : string := "DEFAULT");
55
   port(clk       : in std_logic;
56
        reset_in  : in std_logic;
57
        a, b      : in std_logic_vector(31 downto 0);
58
        mult_func : in t_mult_function;
59
        c_mult    : out std_logic_vector(31 downto 0);
60
        pause_out : out std_logic);
61
end; --entity mult
62
 
63
architecture logic of mips_mult is
64
 
65
   constant MODE_MULT : std_logic := '1';
66
   constant MODE_DIV  : std_logic := '0';
67
 
68
   signal mode_reg    : std_logic;
69
   signal negate_reg  : std_logic;
70
   signal sign_reg    : std_logic;
71
   signal sign2_reg   : std_logic;
72
   signal count_reg   : std_logic_vector(5 downto 0);
73
   signal aa_reg      : std_logic_vector(31 downto 0);
74
   signal bb_reg      : std_logic_vector(31 downto 0);
75
   signal upper_reg   : std_logic_vector(31 downto 0);
76
   signal lower_reg   : std_logic_vector(31 downto 0);
77
 
78
   signal a_neg       : std_logic_vector(31 downto 0);
79
   signal b_neg       : std_logic_vector(31 downto 0);
80
   signal sum         : std_logic_vector(32 downto 0);
81
   signal sum_a       : std_logic_vector(32 downto 0);
82
   signal sum_b       : std_logic_vector(32 downto 0);
83
 
84
begin
85
 
86
   -- @ion Output mux no longer uses function bv_negate. Removing one input that
87
   -- is no longer needed, even if constant, may help in some FPGA architectures 
88
   -- too.
89
   -- See @note2
90
   -- Result
91
   c_mult <= lower_reg              when mult_func = MULT_READ_LO and
92
                                         negate_reg = '0' else
93
             not(lower_reg) + 1     when mult_func = MULT_READ_LO and
94
             --bv_negate(lower_reg)   when mult_func = MULT_READ_LO and 
95
                                         negate_reg = '1' else
96
             upper_reg;             -- when mult_func = MULT_READ_HI else 
97
             --ZERO;
98
 
99
   -- @ion Stall pipeline while operation completes even if output is not needed
100
   -- immediately.
101
   -- See @note3
102
   pause_out <= '1' when (count_reg(5 downto 0) /= "000000") else '0'; --and 
103
             --(mult_func = MULT_READ_LO or mult_func = MULT_READ_HI) else '0';
104
 
105
   -- ABS and remainder signals
106
   a_neg <= not(a) + 1; --bv_negate(a); -- @ion @note2
107
   b_neg <= not(b) + 1; --bv_negate(b); -- @ion @note2
108
 
109
   -- @ion Replaced function bv_adder with straight vector code
110
   --sum <= bv_adder(upper_reg, aa_reg, mode_reg);
111
   sum_a <= ('0' & upper_reg); -- No sign extension: MSB of sum is special
112
   sum_b <= ('0' & aa_reg);
113
   with mode_reg select sum <=
114
        sum_a + sum_b when '1',
115
        sum_a - sum_b when others;
116
 
117
   --multiplication/division unit
118
   mult_proc: process(clk, reset_in, a, b, mult_func,
119
      a_neg, b_neg, sum, sign_reg, mode_reg, negate_reg,
120
      count_reg, aa_reg, bb_reg, upper_reg, lower_reg)
121
      variable count : std_logic_vector(2 downto 0);
122
   begin
123
      count := "001";
124 21 ja_rd
      -- @ion Old asynchronous reset converted to synchronous, for consistency
125
      -- (Code indenting mangled by the new 'if' level)
126
      --if reset_in = '1' then
127
      if rising_edge(clk) then
128
      if reset_in = '1' then
129 12 ja_rd
         mode_reg <= '0';
130
         negate_reg <= '0';
131
         sign_reg <= '0';
132
         sign2_reg <= '0';
133
         count_reg <= "000000";
134
         aa_reg <= ZERO;
135
         bb_reg <= ZERO;
136
         upper_reg <= ZERO;
137
         lower_reg <= ZERO;
138 21 ja_rd
      --elsif rising_edge(clk) then
139
      else
140 12 ja_rd
         case mult_func is
141
            when MULT_WRITE_LO =>
142
               lower_reg <= a;
143
               negate_reg <= '0';
144
            when MULT_WRITE_HI =>
145
               upper_reg <= a;
146
               negate_reg <= '0';
147
            when MULT_MULT =>
148
               mode_reg <= MODE_MULT;
149
               aa_reg <= a;
150
               bb_reg <= b;
151
               upper_reg <= ZERO;
152
               count_reg <= "100000";
153
               negate_reg <= '0';
154
               sign_reg <= '0';
155
               sign2_reg <= '0';
156
            when MULT_SIGNED_MULT =>
157
               mode_reg <= MODE_MULT;
158
               if b(31) = '0' then
159
                  aa_reg <= a;
160
                  bb_reg <= b;
161
                  sign_reg <= a(31);
162
               else
163
                  aa_reg <= a_neg;
164
                  bb_reg <= b_neg;
165
                  sign_reg <= a_neg(31);
166
               end if;
167
               sign2_reg <= '0';
168
               upper_reg <= ZERO;
169
               count_reg <= "100000";
170
               negate_reg <= '0';
171
            when MULT_DIVIDE =>
172
               mode_reg <= MODE_DIV;
173
               aa_reg <= b(0) & ZERO(30 downto 0);
174
               bb_reg <= b;
175
               upper_reg <= a;
176
               count_reg <= "100000";
177
               negate_reg <= '0';
178
            when MULT_SIGNED_DIVIDE =>
179
               mode_reg <= MODE_DIV;
180
               if b(31) = '0' then
181
                  aa_reg(31) <= b(0);
182
                  bb_reg <= b;
183
               else
184
                  aa_reg(31) <= b_neg(0);
185
                  bb_reg <= b_neg;
186
               end if;
187
               if a(31) = '0' then
188
                  upper_reg <= a;
189
               else
190
                  upper_reg <= a_neg;
191
               end if;
192
               aa_reg(30 downto 0) <= ZERO(30 downto 0);
193
               count_reg <= "100000";
194
               negate_reg <= a(31) xor b(31);
195
            when others =>
196
 
197
               if count_reg /= "000000" then
198
                  if mode_reg = MODE_MULT then
199
                     -- Multiplication
200
                     if bb_reg(0) = '1' then
201
                        upper_reg <= (sign_reg xor sum(32)) & sum(31 downto 1);
202
                        lower_reg <= sum(0) & lower_reg(31 downto 1);
203
                        sign2_reg <= sign2_reg or sign_reg;
204
                        sign_reg <= '0';
205
                        bb_reg <= '0' & bb_reg(31 downto 1);
206
                     -- The following six lines are optional for speedup
207
                     --elsif bb_reg(3 downto 0) = "0000" and sign2_reg = '0' and 
208
                     --      count_reg(5 downto 2) /= "0000" then
209
                     --   upper_reg <= "0000" & upper_reg(31 downto 4);
210
                     --   lower_reg <=  upper_reg(3 downto 0) & lower_reg(31 downto 4);
211
                     --   count := "100";
212
                     --   bb_reg <= "0000" & bb_reg(31 downto 4);
213
                     else
214
                        upper_reg <= sign2_reg & upper_reg(31 downto 1);
215
                        lower_reg <= upper_reg(0) & lower_reg(31 downto 1);
216
                        bb_reg <= '0' & bb_reg(31 downto 1);
217
                     end if;
218
                  else
219
                     -- Division
220
                     if sum(32) = '0' and aa_reg /= ZERO and
221
                           bb_reg(31 downto 1) = ZERO(31 downto 1) then
222
                        upper_reg <= sum(31 downto 0);
223
                        lower_reg(0) <= '1';
224
                     else
225
                        lower_reg(0) <= '0';
226
                     end if;
227
                     aa_reg <= bb_reg(1) & aa_reg(31 downto 1);
228
                     lower_reg(31 downto 1) <= lower_reg(30 downto 0);
229
                     bb_reg <= '0' & bb_reg(31 downto 1);
230
                  end if;
231
                  count_reg <= count_reg - count;
232
               end if; --count
233
 
234
         end case;
235 21 ja_rd
 
236
      end if;
237 12 ja_rd
      end if;
238
 
239
   end process;
240
 
241
end; --architecture logic
242
 
243
--------------------------------------------------------------------------------
244
-- @note1 : bv_adder function removed
245
-- This function was a slightly modified adder/substractor coded in a bitwise
246
-- manner that made it hard for synth tools to recognize it as such. At least
247
-- that's what I think. Replacing it with straigth code results in smaller and
248
-- faster logic (about 23% faster).
249
--
250
-- @note2 : bv_negate function removed
251
-- This function computed a 2's complement bitwise. Removed on the same grounds
252
-- as @note1 but with no apparent improvement in synthesis results.
253
--
254
-- @note3 : pause_out active until operation complete
255
-- The original Plasma module allowed the pipeline and the multiplier to run
256
-- concurrently until the multiplier result was needed, and only then the
257 21 ja_rd
-- pipeline was stalled if the mul/div operation had not finished yet.
258 12 ja_rd
-- We want to make sure we can abort a mul/div so for the time being we stall 
259
-- until the operation is complete.
260 21 ja_rd
-- I *think* that's what the libraries and the toolchain assume anyway.
261
-- Note that if we later want to change this, the parent module will need 
262 12 ja_rd
-- changes too (logic for p1_muldiv_running).
263
--------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.