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

Subversion Repositories ion

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

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
      if reset_in = '1' then
125
         mode_reg <= '0';
126
         negate_reg <= '0';
127
         sign_reg <= '0';
128
         sign2_reg <= '0';
129
         count_reg <= "000000";
130
         aa_reg <= ZERO;
131
         bb_reg <= ZERO;
132
         upper_reg <= ZERO;
133
         lower_reg <= ZERO;
134
      elsif rising_edge(clk) then
135
         case mult_func is
136
            when MULT_WRITE_LO =>
137
               lower_reg <= a;
138
               negate_reg <= '0';
139
            when MULT_WRITE_HI =>
140
               upper_reg <= a;
141
               negate_reg <= '0';
142
            when MULT_MULT =>
143
               mode_reg <= MODE_MULT;
144
               aa_reg <= a;
145
               bb_reg <= b;
146
               upper_reg <= ZERO;
147
               count_reg <= "100000";
148
               negate_reg <= '0';
149
               sign_reg <= '0';
150
               sign2_reg <= '0';
151
            when MULT_SIGNED_MULT =>
152
               mode_reg <= MODE_MULT;
153
               if b(31) = '0' then
154
                  aa_reg <= a;
155
                  bb_reg <= b;
156
                  sign_reg <= a(31);
157
               else
158
                  aa_reg <= a_neg;
159
                  bb_reg <= b_neg;
160
                  sign_reg <= a_neg(31);
161
               end if;
162
               sign2_reg <= '0';
163
               upper_reg <= ZERO;
164
               count_reg <= "100000";
165
               negate_reg <= '0';
166
            when MULT_DIVIDE =>
167
               mode_reg <= MODE_DIV;
168
               aa_reg <= b(0) & ZERO(30 downto 0);
169
               bb_reg <= b;
170
               upper_reg <= a;
171
               count_reg <= "100000";
172
               negate_reg <= '0';
173
            when MULT_SIGNED_DIVIDE =>
174
               mode_reg <= MODE_DIV;
175
               if b(31) = '0' then
176
                  aa_reg(31) <= b(0);
177
                  bb_reg <= b;
178
               else
179
                  aa_reg(31) <= b_neg(0);
180
                  bb_reg <= b_neg;
181
               end if;
182
               if a(31) = '0' then
183
                  upper_reg <= a;
184
               else
185
                  upper_reg <= a_neg;
186
               end if;
187
               aa_reg(30 downto 0) <= ZERO(30 downto 0);
188
               count_reg <= "100000";
189
               negate_reg <= a(31) xor b(31);
190
            when others =>
191
 
192
               if count_reg /= "000000" then
193
                  if mode_reg = MODE_MULT then
194
                     -- Multiplication
195
                     if bb_reg(0) = '1' then
196
                        upper_reg <= (sign_reg xor sum(32)) & sum(31 downto 1);
197
                        lower_reg <= sum(0) & lower_reg(31 downto 1);
198
                        sign2_reg <= sign2_reg or sign_reg;
199
                        sign_reg <= '0';
200
                        bb_reg <= '0' & bb_reg(31 downto 1);
201
                     -- The following six lines are optional for speedup
202
                     --elsif bb_reg(3 downto 0) = "0000" and sign2_reg = '0' and 
203
                     --      count_reg(5 downto 2) /= "0000" then
204
                     --   upper_reg <= "0000" & upper_reg(31 downto 4);
205
                     --   lower_reg <=  upper_reg(3 downto 0) & lower_reg(31 downto 4);
206
                     --   count := "100";
207
                     --   bb_reg <= "0000" & bb_reg(31 downto 4);
208
                     else
209
                        upper_reg <= sign2_reg & upper_reg(31 downto 1);
210
                        lower_reg <= upper_reg(0) & lower_reg(31 downto 1);
211
                        bb_reg <= '0' & bb_reg(31 downto 1);
212
                     end if;
213
                  else
214
                     -- Division
215
                     if sum(32) = '0' and aa_reg /= ZERO and
216
                           bb_reg(31 downto 1) = ZERO(31 downto 1) then
217
                        upper_reg <= sum(31 downto 0);
218
                        lower_reg(0) <= '1';
219
                     else
220
                        lower_reg(0) <= '0';
221
                     end if;
222
                     aa_reg <= bb_reg(1) & aa_reg(31 downto 1);
223
                     lower_reg(31 downto 1) <= lower_reg(30 downto 0);
224
                     bb_reg <= '0' & bb_reg(31 downto 1);
225
                  end if;
226
                  count_reg <= count_reg - count;
227
               end if; --count
228
 
229
         end case;
230
 
231
      end if;
232
 
233
   end process;
234
 
235
end; --architecture logic
236
 
237
--------------------------------------------------------------------------------
238
-- @note1 : bv_adder function removed
239
-- This function was a slightly modified adder/substractor coded in a bitwise
240
-- manner that made it hard for synth tools to recognize it as such. At least
241
-- that's what I think. Replacing it with straigth code results in smaller and
242
-- faster logic (about 23% faster).
243
--
244
-- @note2 : bv_negate function removed
245
-- This function computed a 2's complement bitwise. Removed on the same grounds
246
-- as @note1 but with no apparent improvement in synthesis results.
247
--
248
-- @note3 : pause_out active until operation complete
249
-- The original Plasma module allowed the pipeline and the multiplier to run
250
-- concurrently until the multiplier result was needed, and only then the
251
-- pipeline was stalled.
252
-- We want to make sure we can abort a mul/div so for the time being we stall 
253
-- until the operation is complete.
254
-- note that if we later want to change this, the parent module will need 
255
-- changes too (logic for p1_muldiv_running).
256
--------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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