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

Subversion Repositories plasma

[/] [plasma/] [trunk/] [vhdl/] [mult.vhd] - Blame information for rev 417

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

Line No. Rev Author Line
1 2 rhoads
---------------------------------------------------------------------
2
-- TITLE: Multiplication and Division Unit
3 121 rhoads
-- AUTHORS: Steve Rhoads (rhoadss@yahoo.com)
4 2 rhoads
-- DATE CREATED: 1/31/01
5
-- FILENAME: mult.vhd
6 43 rhoads
-- PROJECT: Plasma CPU core
7 2 rhoads
-- COPYRIGHT: Software placed into the public domain by the author.
8
--    Software 'as is' without warranty.  Author liable for nothing.
9
-- DESCRIPTION:
10 139 rhoads
--    Implements the multiplication and division unit in 32 clocks.
11 97 rhoads
--
12 196 rhoads
--    To reduce space, compile your code using the flag "-mno-mul" which 
13
--    will use software base routines in math.c if USE_SW_MULT is defined.
14
--    Then remove references to the entity mult in mlite_cpu.vhd.
15
--
16 139 rhoads
-- MULTIPLICATION
17 390 rhoads
-- long64 answer = 0;
18 139 rhoads
-- for(i = 0; i < 32; ++i)
19
-- {
20
--    answer = (answer >> 1) + (((b&1)?a:0) << 31);
21
--    b = b >> 1;
22
-- }
23 97 rhoads
--
24 139 rhoads
-- DIVISION
25
-- long upper=a, lower=0;
26
-- a = b << 31;
27
-- for(i = 0; i < 32; ++i)
28
-- {
29
--    lower = lower << 1;
30
--    if(upper >= a && a && b < 2)
31
--    {
32
--       upper = upper - a;
33
--       lower |= 1;
34 97 rhoads
--    }
35 139 rhoads
--    a = ((b&2) << 30) | (a >> 1);
36
--    b = b >> 1;
37
-- }
38 2 rhoads
---------------------------------------------------------------------
39
library ieee;
40
use ieee.std_logic_1164.all;
41 90 rhoads
use ieee.std_logic_unsigned.all;
42 121 rhoads
use IEEE.std_logic_arith.all;
43 39 rhoads
use work.mlite_pack.all;
44 2 rhoads
 
45
entity mult is
46 139 rhoads
   generic(mult_type  : string := "DEFAULT");
47 2 rhoads
   port(clk       : in std_logic;
48 128 rhoads
        reset_in  : in std_logic;
49 2 rhoads
        a, b      : in std_logic_vector(31 downto 0);
50
        mult_func : in mult_function_type;
51
        c_mult    : out std_logic_vector(31 downto 0);
52
        pause_out : out std_logic);
53
end; --entity mult
54
 
55
architecture logic of mult is
56 121 rhoads
 
57 139 rhoads
   constant MODE_MULT : std_logic := '1';
58
   constant MODE_DIV  : std_logic := '0';
59 121 rhoads
 
60 139 rhoads
   signal mode_reg    : std_logic;
61
   signal negate_reg  : std_logic;
62
   signal sign_reg    : std_logic;
63
   signal sign2_reg   : std_logic;
64
   signal count_reg   : std_logic_vector(5 downto 0);
65
   signal aa_reg      : std_logic_vector(31 downto 0);
66
   signal bb_reg      : std_logic_vector(31 downto 0);
67
   signal upper_reg   : std_logic_vector(31 downto 0);
68
   signal lower_reg   : std_logic_vector(31 downto 0);
69 128 rhoads
 
70 139 rhoads
   signal a_neg       : std_logic_vector(31 downto 0);
71
   signal b_neg       : std_logic_vector(31 downto 0);
72
   signal sum         : std_logic_vector(32 downto 0);
73
 
74 2 rhoads
begin
75 121 rhoads
 
76
   -- Result
77 139 rhoads
   c_mult <= lower_reg when mult_func = MULT_READ_LO and negate_reg = '0' else
78
             bv_negate(lower_reg) when mult_func = MULT_READ_LO
79
                and negate_reg = '1' else
80
             upper_reg when mult_func = MULT_READ_HI else
81 121 rhoads
             ZERO;
82 139 rhoads
   pause_out <= '1' when (count_reg /= "000000") and
83
             (mult_func = MULT_READ_LO or mult_func = MULT_READ_HI) else '0';
84 2 rhoads
 
85 139 rhoads
   -- ABS and remainder signals
86
   a_neg <= bv_negate(a);
87
   b_neg <= bv_negate(b);
88
   sum <= bv_adder(upper_reg, aa_reg, mode_reg);
89 121 rhoads
 
90
   --multiplication/division unit
91 128 rhoads
   mult_proc: process(clk, reset_in, a, b, mult_func,
92 139 rhoads
      a_neg, b_neg, sum, sign_reg, mode_reg, negate_reg,
93
      count_reg, aa_reg, bb_reg, upper_reg, lower_reg)
94
      variable count : std_logic_vector(2 downto 0);
95 121 rhoads
   begin
96 139 rhoads
      count := "001";
97 128 rhoads
      if reset_in = '1' then
98 139 rhoads
         mode_reg <= '0';
99
         negate_reg <= '0';
100
         sign_reg <= '0';
101
         sign2_reg <= '0';
102 128 rhoads
         count_reg <= "000000";
103 139 rhoads
         aa_reg <= ZERO;
104
         bb_reg <= ZERO;
105
         upper_reg <= ZERO;
106
         lower_reg <= ZERO;
107 128 rhoads
      elsif rising_edge(clk) then
108 139 rhoads
         case mult_func is
109
            when MULT_WRITE_LO =>
110
               lower_reg <= a;
111
               negate_reg <= '0';
112
            when MULT_WRITE_HI =>
113
               upper_reg <= a;
114
               negate_reg <= '0';
115
            when MULT_MULT =>
116
               mode_reg <= MODE_MULT;
117
               aa_reg <= a;
118
               bb_reg <= b;
119
               upper_reg <= ZERO;
120
               count_reg <= "100000";
121
               negate_reg <= '0';
122
               sign_reg <= '0';
123
               sign2_reg <= '0';
124
            when MULT_SIGNED_MULT =>
125
               mode_reg <= MODE_MULT;
126
               if b(31) = '0' then
127
                  aa_reg <= a;
128
                  bb_reg <= b;
129
               else
130
                  aa_reg <= a_neg;
131
                  bb_reg <= b_neg;
132
               end if;
133 391 rhoads
               sign_reg <= a(31) xor b(31);
134 139 rhoads
               sign2_reg <= '0';
135
               upper_reg <= ZERO;
136
               count_reg <= "100000";
137
               negate_reg <= '0';
138
            when MULT_DIVIDE =>
139
               mode_reg <= MODE_DIV;
140
               aa_reg <= b(0) & ZERO(30 downto 0);
141
               bb_reg <= b;
142
               upper_reg <= a;
143
               count_reg <= "100000";
144
               negate_reg <= '0';
145
            when MULT_SIGNED_DIVIDE =>
146
               mode_reg <= MODE_DIV;
147
               if b(31) = '0' then
148
                  aa_reg(31) <= b(0);
149
                  bb_reg <= b;
150
               else
151
                  aa_reg(31) <= b_neg(0);
152
                  bb_reg <= b_neg;
153
               end if;
154
               if a(31) = '0' then
155
                  upper_reg <= a;
156
               else
157
                  upper_reg <= a_neg;
158
               end if;
159
               aa_reg(30 downto 0) <= ZERO(30 downto 0);
160
               count_reg <= "100000";
161
               negate_reg <= a(31) xor b(31);
162
            when others =>
163 121 rhoads
 
164 139 rhoads
               if count_reg /= "000000" then
165
                  if mode_reg = MODE_MULT then
166
                     -- Multiplication
167
                     if bb_reg(0) = '1' then
168
                        upper_reg <= (sign_reg xor sum(32)) & sum(31 downto 1);
169
                        lower_reg <= sum(0) & lower_reg(31 downto 1);
170
                        sign2_reg <= sign2_reg or sign_reg;
171
                        sign_reg <= '0';
172
                        bb_reg <= '0' & bb_reg(31 downto 1);
173
                     -- The following six lines are optional for speedup
174 345 rhoads
                     --elsif bb_reg(3 downto 0) = "0000" and sign2_reg = '0' and 
175
                     --      count_reg(5 downto 2) /= "0000" then
176
                     --   upper_reg <= "0000" & upper_reg(31 downto 4);
177
                     --   lower_reg <=  upper_reg(3 downto 0) & lower_reg(31 downto 4);
178
                     --   count := "100";
179
                     --   bb_reg <= "0000" & bb_reg(31 downto 4);
180 139 rhoads
                     else
181
                        upper_reg <= sign2_reg & upper_reg(31 downto 1);
182
                        lower_reg <= upper_reg(0) & lower_reg(31 downto 1);
183
                        bb_reg <= '0' & bb_reg(31 downto 1);
184
                     end if;
185
                  else
186
                     -- Division
187
                     if sum(32) = '0' and aa_reg /= ZERO and
188
                           bb_reg(31 downto 1) = ZERO(31 downto 1) then
189
                        upper_reg <= sum(31 downto 0);
190
                        lower_reg(0) <= '1';
191
                     else
192
                        lower_reg(0) <= '0';
193
                     end if;
194
                     aa_reg <= bb_reg(1) & aa_reg(31 downto 1);
195
                     lower_reg(31 downto 1) <= lower_reg(30 downto 0);
196
                     bb_reg <= '0' & bb_reg(31 downto 1);
197
                  end if;
198
                  count_reg <= count_reg - count;
199
               end if; --count
200 2 rhoads
 
201 139 rhoads
         end case;
202
 
203 121 rhoads
      end if;
204
 
205 139 rhoads
   end process;
206 121 rhoads
 
207 2 rhoads
end; --architecture logic

powered by: WebSVN 2.1.0

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