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

Subversion Repositories mlite

[/] [mlite/] [trunk/] [vhdl/] [mult.vhd] - Blame information for rev 97

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 2 rhoads
 
78
begin
79 47 rhoads
   do_mult_temp   := do_mult_reg;
80 2 rhoads
   do_signed_temp := do_signed_reg;
81
   count_temp     := count_reg;
82
   a_temp         := reg_a;
83
   b_temp         := reg_b;
84
   answer_temp    := answer_reg;
85 47 rhoads
   sign_extend    := do_signed_reg and do_mult_reg;
86 2 rhoads
   start          := '0';
87
   do_write       := '0';
88
   do_hi          := '0';
89
 
90
   case mult_func is
91
   when mult_read_lo =>
92
   when mult_read_hi =>
93
      do_hi := '1';
94
   when mult_write_lo =>
95
      do_write := '1';
96
   when mult_write_hi =>
97
      do_write := '1';
98
      do_hi := '1';
99
   when mult_mult =>
100
      start := '1';
101 47 rhoads
      do_mult_temp := '1';
102 45 rhoads
      do_signed_temp := '0';
103
   when mult_signed_mult =>
104
      start := '1';
105 47 rhoads
      do_mult_temp := '1';
106 45 rhoads
      do_signed_temp := a(31) xor b(31);
107 2 rhoads
   when mult_divide =>
108
      start := '1';
109 47 rhoads
      do_mult_temp := '0';
110 2 rhoads
      do_signed_temp := '0';
111
   when mult_signed_divide =>
112
      start := '1';
113 47 rhoads
      do_mult_temp := '0';
114 23 rhoads
      do_signed_temp := a(31) xor b(31);
115 2 rhoads
   when others =>
116
   end case;
117
 
118
   if start = '1' then
119
      count_temp := "000000";
120
      answer_temp := ZERO;
121 47 rhoads
      if do_mult_temp = '0' then
122 18 rhoads
         b_temp(63) := '0';
123 23 rhoads
         if mult_func /= mult_signed_divide or b(31) = '0' then
124 2 rhoads
            b_temp(62 downto 31) := b;
125
         else
126
            b_temp(62 downto 31) := bv_negate(b);
127 23 rhoads
         end if;
128
         if mult_func /= mult_signed_divide or a(31) = '0' then
129
            a_temp := a;
130
         else
131 2 rhoads
            a_temp := bv_negate(a);
132
         end if;
133
         b_temp(30 downto 0) := ZERO(30 downto 0);
134
      else --multiply
135 23 rhoads
         a_temp := a;
136 7 rhoads
         b_temp := ZERO & b;
137 2 rhoads
      end if;
138
   elsif do_write = '1' then
139
      if do_hi = '0' then
140
         b_temp(31 downto 0) := a;
141
      else
142
         b_temp(63 downto 32) := a;
143
      end if;
144
   end if;
145
 
146 90 rhoads
   if do_mult_reg = '0' then  --division
147
      aa <= (reg_a(31) and sign_extend) & (reg_a(31) and sign_extend) & reg_a;
148
      bb <= reg_b(33 downto 0);
149
   else                       --multiplication two-bits at a time
150
      case reg_b(1 downto 0) is
151
      when "00" =>
152
         aa <= "00" & ZERO;
153
      when "01" =>
154
         aa <= (reg_a(31) and sign_extend) & (reg_a(31) and sign_extend) & reg_a;
155
      when "10" =>
156
         aa <= (reg_a(31) and sign_extend) & reg_a & '0';
157
      when others =>
158
         aa <= reg_a_times3;
159
      end case;
160
      bb <= (reg_b(63) and sign_extend) & (reg_b(63) and sign_extend) & reg_b(63 downto 32);
161 2 rhoads
   end if;
162
 
163
   if count_reg(5) = '0' and start = '0' then
164
      count_temp := bv_inc6(count_reg);
165 90 rhoads
      if do_mult_reg = '0' then          --division
166 2 rhoads
         answer_temp(31 downto 1) := answer_reg(30 downto 0);
167 23 rhoads
         if reg_b(63 downto 32) = ZERO and sum(32) = '0' then
168 2 rhoads
            a_temp := sum(31 downto 0);  --aa=aa-bb;
169
            answer_temp(0) := '1';
170
         else
171
            answer_temp(0) := '0';
172
         end if;
173
         if count_reg /= "011111" then
174
            b_temp(62 downto 0) := reg_b(63 downto 1);
175 90 rhoads
         else                            --done with divide
176 2 rhoads
            b_temp(63 downto 32) := a_temp;
177 23 rhoads
            if do_signed_reg = '0' then
178
               b_temp(31 downto 0) := answer_temp;
179
            else
180
               b_temp(31 downto 0) := bv_negate(answer_temp);
181
            end if;
182 2 rhoads
         end if;
183
      else  -- mult_mode
184 90 rhoads
         b_temp(63 downto 30) := sum;
185
         b_temp(29 downto 0) := reg_b(31 downto 2);
186
         if count_reg = "001000" and sign_extend = '0' and   --early stop
187 7 rhoads
               reg_b(15 downto 0) = ZERO(15 downto 0) then
188 2 rhoads
            count_temp := "111111";
189
            b_temp(31 downto 0) := reg_b(47 downto 16);
190
         end if;
191 90 rhoads
         if count_reg = "000100" and sign_extend = '0' and   --early stop
192 7 rhoads
               reg_b(23 downto 0) = ZERO(23 downto 0) then
193
            count_temp := "111111";
194
            b_temp(31 downto 0) := reg_b(55 downto 24);
195
         end if;
196 90 rhoads
         count_temp(5) := count_temp(4);
197 2 rhoads
      end if;
198
   end if;
199
 
200
   if rising_edge(clk) then
201 47 rhoads
      do_mult_reg <= do_mult_temp;
202 2 rhoads
      do_signed_reg <= do_signed_temp;
203
      count_reg <= count_temp;
204
      reg_a <= a_temp;
205
      reg_b <= b_temp;
206
      answer_reg <= answer_temp;
207 90 rhoads
      if start = '1' then
208
         reg_a_times3 <= ((a(31) and do_signed_temp) & a & '0') +
209
            ((a(31) and do_signed_temp) & (a(31) and do_signed_temp) & a);
210
      end if;
211 2 rhoads
   end if;
212
 
213
   if count_reg(5) = '0' and mult_func/= mult_nothing and start = '0' then
214
      pause_out <= '1';
215
   else
216
      pause_out <= '0';
217
   end if;
218 47 rhoads
   case mult_func is
219
   when mult_read_lo =>
220 2 rhoads
      c_mult <= reg_b(31 downto 0);
221 47 rhoads
   when mult_read_hi =>
222 2 rhoads
      c_mult <= reg_b(63 downto 32);
223 47 rhoads
   when others =>
224 2 rhoads
      c_mult <= ZERO;
225 47 rhoads
   end case;
226 2 rhoads
 
227
end process;
228
 
229 47 rhoads
 
230
   generic_adder:
231
   if adder_type /= "ALTERA" generate
232 90 rhoads
      sum <= (aa + bb) when do_mult_reg = '1' else
233
             (aa - bb);
234 47 rhoads
   end generate; --generic_adder
235
 
236
   --For Altera
237
   lpm_adder:
238
   if adder_type = "ALTERA" generate
239
      lpm_add_sub_component : lpm_add_sub
240
      GENERIC MAP (
241 90 rhoads
         lpm_width => 34,
242 47 rhoads
         lpm_direction => "UNUSED",
243
         lpm_type => "LPM_ADD_SUB",
244
         lpm_hint => "ONE_INPUT_IS_CONSTANT=NO"
245
      )
246
      PORT MAP (
247
         dataa => aa,
248
         add_sub => do_mult_reg,
249
         datab => bb,
250
         result => sum
251
      );
252
   end generate; --lpm_adder
253
 
254 2 rhoads
end; --architecture logic
255
 

powered by: WebSVN 2.1.0

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