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

Subversion Repositories mlite

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

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
-- PROJECT: MIPS CPU core
7
-- 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
--    Normally takes 32 clock cycles.
12 7 rhoads
--    if b(31 downto 16) = ZERO(31 downto 16) then mult in 16 cycles. 
13
--    if b(31 downto 8) = ZERO(31 downto 8) then mult in 8 cycles. 
14 2 rhoads
---------------------------------------------------------------------
15
library ieee;
16
use ieee.std_logic_1164.all;
17
use work.mips_pack.all;
18
 
19
entity mult is
20
   port(clk       : in std_logic;
21
        a, b      : in std_logic_vector(31 downto 0);
22
        mult_func : in mult_function_type;
23
        c_mult    : out std_logic_vector(31 downto 0);
24
        pause_out : out std_logic);
25
end; --entity mult
26
 
27
architecture logic of mult is
28
--   type mult_function_type is (
29
--      mult_nothing, mult_read_lo, mult_read_hi, mult_write_lo, 
30
--      mult_write_hi, mult_mult, mult_divide, mult_signed_divide);
31
   signal do_div_reg    : std_logic;
32
   signal do_signed_reg : std_logic;
33
   signal count_reg     : std_logic_vector(5 downto 0);
34
   signal reg_a         : std_logic_vector(31 downto 0);
35
   signal reg_b         : std_logic_vector(63 downto 0);
36
   signal answer_reg    : std_logic_vector(31 downto 0);
37
begin
38
 
39
--multiplication/division unit
40
mult_proc: process(clk, a, b, mult_func,
41
                   do_div_reg, do_signed_reg, count_reg,
42
                   reg_a, reg_b, answer_reg)
43
   variable do_div_temp    : std_logic;
44
   variable do_signed_temp : std_logic;
45
   variable count_temp     : std_logic_vector(5 downto 0);
46
   variable a_temp         : std_logic_vector(31 downto 0);
47
   variable b_temp         : std_logic_vector(63 downto 0);
48
   variable answer_temp    : std_logic_vector(31 downto 0);
49
 
50
   variable aa, bb         : std_logic_vector(32 downto 0);
51
   variable sum            : std_logic_vector(32 downto 0);
52
   variable start          : std_logic;
53
   variable do_write       : std_logic;
54
   variable do_hi          : std_logic;
55
 
56
begin
57
   do_div_temp    := do_div_reg;
58
   do_signed_temp := do_signed_reg;
59
   count_temp     := count_reg;
60
   a_temp         := reg_a;
61
   b_temp         := reg_b;
62
   answer_temp    := answer_reg;
63
 
64
   aa             := '0' & ZERO;
65
   bb             := '0' & ZERO;
66
   sum            := '0' & ZERO;
67
   start          := '0';
68
   do_write       := '0';
69
   do_hi          := '0';
70
 
71
   case mult_func is
72
   when mult_read_lo =>
73
   when mult_read_hi =>
74
      do_hi := '1';
75
   when mult_write_lo =>
76
      do_write := '1';
77
   when mult_write_hi =>
78
      do_write := '1';
79
      do_hi := '1';
80
   when mult_mult =>
81
      start := '1';
82
      do_div_temp := '0';
83
   when mult_divide =>
84
      start := '1';
85
      do_div_temp := '1';
86
      do_signed_temp := '0';
87
   when mult_signed_divide =>
88
      start := '1';
89
      do_div_temp := '1';
90 23 rhoads
      do_signed_temp := a(31) xor b(31);
91 2 rhoads
   when others =>
92
   end case;
93
 
94
   if start = '1' then
95
      count_temp := "000000";
96
      answer_temp := ZERO;
97
      if do_div_temp = '1' then
98 18 rhoads
         b_temp(63) := '0';
99 23 rhoads
         if mult_func /= mult_signed_divide or b(31) = '0' then
100 2 rhoads
            b_temp(62 downto 31) := b;
101
         else
102
            b_temp(62 downto 31) := bv_negate(b);
103 23 rhoads
         end if;
104
         if mult_func /= mult_signed_divide or a(31) = '0' then
105
            a_temp := a;
106
         else
107 2 rhoads
            a_temp := bv_negate(a);
108
         end if;
109
         b_temp(30 downto 0) := ZERO(30 downto 0);
110
      else --multiply
111 23 rhoads
         a_temp := a;
112 7 rhoads
         b_temp := ZERO & b;
113 2 rhoads
      end if;
114
   elsif do_write = '1' then
115
      if do_hi = '0' then
116
         b_temp(31 downto 0) := a;
117
      else
118
         b_temp(63 downto 32) := a;
119
      end if;
120
   end if;
121
 
122
   if do_div_reg = '1' then
123
      bb := reg_b(32 downto 0);
124
   else
125
      bb := '0' & reg_b(63 downto 32);
126
   end if;
127 23 rhoads
   aa := '0' & reg_a;
128 2 rhoads
   sum := bv_adder(aa, bb, do_div_reg);
129
--   sum := bv_adder_lookahead(aa, bb, do_div_reg);
130
 
131
   if count_reg(5) = '0' and start = '0' then
132
      count_temp := bv_inc6(count_reg);
133
      if do_div_reg = '1' then
134
         answer_temp(31 downto 1) := answer_reg(30 downto 0);
135 23 rhoads
         if reg_b(63 downto 32) = ZERO and sum(32) = '0' then
136 2 rhoads
            a_temp := sum(31 downto 0);  --aa=aa-bb;
137
            answer_temp(0) := '1';
138
         else
139
            answer_temp(0) := '0';
140
         end if;
141
         if count_reg /= "011111" then
142
            b_temp(62 downto 0) := reg_b(63 downto 1);
143
         else
144
            b_temp(63 downto 32) := a_temp;
145 23 rhoads
            if do_signed_reg = '0' then
146
               b_temp(31 downto 0) := answer_temp;
147
            else
148
               b_temp(31 downto 0) := bv_negate(answer_temp);
149
            end if;
150 2 rhoads
         end if;
151
      else  -- mult_mode
152
         if reg_b(0) = '1' then
153
            b_temp(63 downto 31) := sum;
154
         else
155
            b_temp(63 downto 31) := '0' & reg_b(63 downto 32);
156
         end if;
157
         b_temp(30 downto 0) := reg_b(31 downto 1);
158
         if count_reg = "010000" and          --early stop
159 7 rhoads
               reg_b(15 downto 0) = ZERO(15 downto 0) then
160 2 rhoads
            count_temp := "111111";
161
            b_temp(31 downto 0) := reg_b(47 downto 16);
162
         end if;
163 7 rhoads
         if count_reg = "001000" and          --early stop
164
               reg_b(23 downto 0) = ZERO(23 downto 0) then
165
            count_temp := "111111";
166
            b_temp(31 downto 0) := reg_b(55 downto 24);
167
         end if;
168 2 rhoads
      end if;
169
   end if;
170
 
171
   if rising_edge(clk) then
172
      do_div_reg <= do_div_temp;
173
      do_signed_reg <= do_signed_temp;
174
      count_reg <= count_temp;
175
      reg_a <= a_temp;
176
      reg_b <= b_temp;
177
      answer_reg <= answer_temp;
178
   end if;
179
 
180
   if count_reg(5) = '0' and mult_func/= mult_nothing and start = '0' then
181
      pause_out <= '1';
182
   else
183
      pause_out <= '0';
184
   end if;
185
   if mult_func = mult_read_lo then
186
      c_mult <= reg_b(31 downto 0);
187
   elsif mult_func = mult_read_hi then
188
      c_mult <= reg_b(63 downto 32);
189
   else
190
      c_mult <= ZERO;
191
   end if;
192
 
193
end process;
194
 
195
end; --architecture logic
196
 
197
 

powered by: WebSVN 2.1.0

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