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

Subversion Repositories plasma

[/] [plasma/] [tags/] [V2_1/] [vhdl/] [mult.vhd] - Blame information for rev 99

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
-- AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
4
-- 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
--    Implements the multiplication and division unit.
11 90 rhoads
--    Division takes 32 clock cycles.
12
--    Multiplication normally takes 16 clock cycles.
13
--    if b <= 0xffff then mult in 8 cycles. 
14
--    if b <= 0xff then mult in 4 cycles. 
15 97 rhoads
--
16
-- For multiplication set reg_b = 0x00000000 & b.  The 64-bit result
17
-- will be in reg_b.  The lower bits of reg_b contain the upper 
18
-- bits of b that have not yet been multiplied.  For 16 clock cycles
19
-- shift reg_b two bits to the right.  Use the lowest two bits of reg_b 
20
-- to multiply by two bits at a time and add the result to the upper
21
-- 32-bits of reg_b (using C syntax):
22
--    reg_b = (reg_b >> 2) + (((reg_b & 3) * reg_a) << 32);
23
--
24
-- For division set reg_b = '0' & b & 30_ZEROS.  The answer will be
25
-- in answer_reg and the remainder in reg_a.  For 32 clock cycles
26
-- (using C syntax):
27
--    answer_reg = (answer_reg << 1);
28
--    if (reg_a >= reg_b) {
29
--       answer_reg += 1;
30
--       reg_a -= reg_b;
31
--    }
32
--    reg_b = reg_b >> 1;
33 2 rhoads
---------------------------------------------------------------------
34
library ieee;
35
use ieee.std_logic_1164.all;
36 90 rhoads
use ieee.std_logic_unsigned.all;
37 39 rhoads
use work.mlite_pack.all;
38 2 rhoads
 
39
entity mult is
40 47 rhoads
   generic(adder_type : string := "GENERIC");
41 2 rhoads
   port(clk       : in std_logic;
42
        a, b      : in std_logic_vector(31 downto 0);
43
        mult_func : in mult_function_type;
44
        c_mult    : out std_logic_vector(31 downto 0);
45
        pause_out : out std_logic);
46
end; --entity mult
47
 
48
architecture logic of mult is
49
--   type mult_function_type is (
50
--      mult_nothing, mult_read_lo, mult_read_hi, mult_write_lo, 
51
--      mult_write_hi, mult_mult, mult_divide, mult_signed_divide);
52 47 rhoads
   signal do_mult_reg   : std_logic;
53 2 rhoads
   signal do_signed_reg : std_logic;
54
   signal count_reg     : std_logic_vector(5 downto 0);
55
   signal reg_a         : std_logic_vector(31 downto 0);
56
   signal reg_b         : std_logic_vector(63 downto 0);
57
   signal answer_reg    : std_logic_vector(31 downto 0);
58 90 rhoads
   signal aa, bb        : std_logic_vector(33 downto 0);
59
   signal sum           : std_logic_vector(33 downto 0);
60
   signal reg_a_times3  : std_logic_vector(33 downto 0);
61 2 rhoads
begin
62
 
63
--multiplication/division unit
64
mult_proc: process(clk, a, b, mult_func,
65 47 rhoads
                   do_mult_reg, do_signed_reg, count_reg,
66 90 rhoads
                   reg_a, reg_b, answer_reg, sum, reg_a_times3)
67 47 rhoads
   variable do_mult_temp   : std_logic;
68 2 rhoads
   variable do_signed_temp : std_logic;
69
   variable count_temp     : std_logic_vector(5 downto 0);
70
   variable a_temp         : std_logic_vector(31 downto 0);
71
   variable b_temp         : std_logic_vector(63 downto 0);
72
   variable answer_temp    : std_logic_vector(31 downto 0);
73
   variable start          : std_logic;
74
   variable do_write       : std_logic;
75
   variable do_hi          : std_logic;
76 45 rhoads
   variable sign_extend    : std_logic;
77 99 rhoads
   variable a_neg          : std_logic_vector(31 downto 0);
78
   variable b_neg          : std_logic_vector(31 downto 0);
79 2 rhoads
 
80
begin
81 47 rhoads
   do_mult_temp   := do_mult_reg;
82 2 rhoads
   do_signed_temp := do_signed_reg;
83
   count_temp     := count_reg;
84
   a_temp         := reg_a;
85
   b_temp         := reg_b;
86
   answer_temp    := answer_reg;
87 47 rhoads
   sign_extend    := do_signed_reg and do_mult_reg;
88 2 rhoads
   start          := '0';
89
   do_write       := '0';
90
   do_hi          := '0';
91
 
92
   case mult_func is
93
   when mult_read_lo =>
94
   when mult_read_hi =>
95
      do_hi := '1';
96
   when mult_write_lo =>
97
      do_write := '1';
98
   when mult_write_hi =>
99
      do_write := '1';
100
      do_hi := '1';
101
   when mult_mult =>
102
      start := '1';
103 47 rhoads
      do_mult_temp := '1';
104 45 rhoads
      do_signed_temp := '0';
105
   when mult_signed_mult =>
106
      start := '1';
107 47 rhoads
      do_mult_temp := '1';
108 99 rhoads
      do_signed_temp := '1';
109 2 rhoads
   when mult_divide =>
110
      start := '1';
111 47 rhoads
      do_mult_temp := '0';
112 2 rhoads
      do_signed_temp := '0';
113
   when mult_signed_divide =>
114
      start := '1';
115 47 rhoads
      do_mult_temp := '0';
116 23 rhoads
      do_signed_temp := a(31) xor b(31);
117 2 rhoads
   when others =>
118
   end case;
119
 
120
   if start = '1' then
121
      count_temp := "000000";
122
      answer_temp := ZERO;
123 99 rhoads
      a_neg := bv_negate(a);
124
      b_neg := bv_negate(b);
125 47 rhoads
      if do_mult_temp = '0' then
126 18 rhoads
         b_temp(63) := '0';
127 99 rhoads
         if mult_func /= mult_signed_divide or a(31) = '0' then
128
            a_temp := a;
129
         else
130
            a_temp := a_neg;
131
         end if;
132 23 rhoads
         if mult_func /= mult_signed_divide or b(31) = '0' then
133 2 rhoads
            b_temp(62 downto 31) := b;
134
         else
135 99 rhoads
            b_temp(62 downto 31) := b_neg;
136 23 rhoads
         end if;
137 99 rhoads
         b_temp(30 downto 0) := ZERO(30 downto 0);
138
      else --multiply
139
         if do_signed_temp = '0' or b(31) = '0' then
140 23 rhoads
            a_temp := a;
141 99 rhoads
            b_temp(31 downto 0) := b;
142 23 rhoads
         else
143 99 rhoads
            a_temp := a_neg;
144
            b_temp(31 downto 0) := b_neg;
145 2 rhoads
         end if;
146 99 rhoads
         b_temp(63 downto 32) := ZERO;
147 2 rhoads
      end if;
148
   elsif do_write = '1' then
149
      if do_hi = '0' then
150
         b_temp(31 downto 0) := a;
151
      else
152
         b_temp(63 downto 32) := a;
153
      end if;
154
   end if;
155
 
156 90 rhoads
   if do_mult_reg = '0' then  --division
157
      aa <= (reg_a(31) and sign_extend) & (reg_a(31) and sign_extend) & reg_a;
158
      bb <= reg_b(33 downto 0);
159
   else                       --multiplication two-bits at a time
160
      case reg_b(1 downto 0) is
161
      when "00" =>
162
         aa <= "00" & ZERO;
163
      when "01" =>
164
         aa <= (reg_a(31) and sign_extend) & (reg_a(31) and sign_extend) & reg_a;
165
      when "10" =>
166
         aa <= (reg_a(31) and sign_extend) & reg_a & '0';
167
      when others =>
168
         aa <= reg_a_times3;
169
      end case;
170
      bb <= (reg_b(63) and sign_extend) & (reg_b(63) and sign_extend) & reg_b(63 downto 32);
171 2 rhoads
   end if;
172
 
173
   if count_reg(5) = '0' and start = '0' then
174
      count_temp := bv_inc6(count_reg);
175 90 rhoads
      if do_mult_reg = '0' then          --division
176 2 rhoads
         answer_temp(31 downto 1) := answer_reg(30 downto 0);
177 23 rhoads
         if reg_b(63 downto 32) = ZERO and sum(32) = '0' then
178 2 rhoads
            a_temp := sum(31 downto 0);  --aa=aa-bb;
179
            answer_temp(0) := '1';
180
         else
181
            answer_temp(0) := '0';
182
         end if;
183
         if count_reg /= "011111" then
184
            b_temp(62 downto 0) := reg_b(63 downto 1);
185 90 rhoads
         else                            --done with divide
186 2 rhoads
            b_temp(63 downto 32) := a_temp;
187 23 rhoads
            if do_signed_reg = '0' then
188
               b_temp(31 downto 0) := answer_temp;
189
            else
190
               b_temp(31 downto 0) := bv_negate(answer_temp);
191
            end if;
192 2 rhoads
         end if;
193
      else  -- mult_mode
194 90 rhoads
         b_temp(63 downto 30) := sum;
195
         b_temp(29 downto 0) := reg_b(31 downto 2);
196
         if count_reg = "001000" and sign_extend = '0' and   --early stop
197 7 rhoads
               reg_b(15 downto 0) = ZERO(15 downto 0) then
198 2 rhoads
            count_temp := "111111";
199
            b_temp(31 downto 0) := reg_b(47 downto 16);
200
         end if;
201 90 rhoads
         if count_reg = "000100" and sign_extend = '0' and   --early stop
202 7 rhoads
               reg_b(23 downto 0) = ZERO(23 downto 0) then
203
            count_temp := "111111";
204
            b_temp(31 downto 0) := reg_b(55 downto 24);
205
         end if;
206 90 rhoads
         count_temp(5) := count_temp(4);
207 2 rhoads
      end if;
208
   end if;
209
 
210
   if rising_edge(clk) then
211 47 rhoads
      do_mult_reg <= do_mult_temp;
212 2 rhoads
      do_signed_reg <= do_signed_temp;
213
      count_reg <= count_temp;
214
      reg_a <= a_temp;
215
      reg_b <= b_temp;
216
      answer_reg <= answer_temp;
217 90 rhoads
      if start = '1' then
218 99 rhoads
         reg_a_times3 <= ((a_temp(31) and do_signed_temp) & a_temp & '0') +
219
            ((a_temp(31) and do_signed_temp) & (a_temp(31) and do_signed_temp) & a_temp);
220 90 rhoads
      end if;
221 2 rhoads
   end if;
222
 
223
   if count_reg(5) = '0' and mult_func/= mult_nothing and start = '0' then
224
      pause_out <= '1';
225
   else
226
      pause_out <= '0';
227
   end if;
228 47 rhoads
   case mult_func is
229
   when mult_read_lo =>
230 2 rhoads
      c_mult <= reg_b(31 downto 0);
231 47 rhoads
   when mult_read_hi =>
232 2 rhoads
      c_mult <= reg_b(63 downto 32);
233 47 rhoads
   when others =>
234 2 rhoads
      c_mult <= ZERO;
235 47 rhoads
   end case;
236 2 rhoads
 
237
end process;
238
 
239 47 rhoads
 
240
   generic_adder:
241
   if adder_type /= "ALTERA" generate
242 90 rhoads
      sum <= (aa + bb) when do_mult_reg = '1' else
243
             (aa - bb);
244 47 rhoads
   end generate; --generic_adder
245
 
246
   --For Altera
247
   lpm_adder:
248
   if adder_type = "ALTERA" generate
249
      lpm_add_sub_component : lpm_add_sub
250
      GENERIC MAP (
251 90 rhoads
         lpm_width => 34,
252 47 rhoads
         lpm_direction => "UNUSED",
253
         lpm_type => "LPM_ADD_SUB",
254
         lpm_hint => "ONE_INPUT_IS_CONSTANT=NO"
255
      )
256
      PORT MAP (
257
         dataa => aa,
258
         add_sub => do_mult_reg,
259
         datab => bb,
260
         result => sum
261
      );
262
   end generate; --lpm_adder
263
 
264 2 rhoads
end; --architecture logic
265
 

powered by: WebSVN 2.1.0

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