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

Subversion Repositories mlite

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

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 18 rhoads
--   signal sum_out       : std_logic_vector(32 downto 0);
38 2 rhoads
begin
39
 
40
--multiplication/division unit
41
mult_proc: process(clk, a, b, mult_func,
42
                   do_div_reg, do_signed_reg, count_reg,
43
                   reg_a, reg_b, answer_reg)
44
   variable do_div_temp    : std_logic;
45
   variable do_signed_temp : std_logic;
46
   variable count_temp     : std_logic_vector(5 downto 0);
47
   variable a_temp         : std_logic_vector(31 downto 0);
48
   variable b_temp         : std_logic_vector(63 downto 0);
49
   variable answer_temp    : std_logic_vector(31 downto 0);
50
 
51
   variable aa, bb         : std_logic_vector(32 downto 0);
52
   variable sum            : std_logic_vector(32 downto 0);
53
   variable start          : std_logic;
54
   variable do_write       : std_logic;
55
   variable do_hi          : std_logic;
56
 
57
begin
58
   do_div_temp    := do_div_reg;
59
   do_signed_temp := do_signed_reg;
60
   count_temp     := count_reg;
61
   a_temp         := reg_a;
62
   b_temp         := reg_b;
63
   answer_temp    := answer_reg;
64
 
65
   aa             := '0' & ZERO;
66
   bb             := '0' & ZERO;
67
   sum            := '0' & ZERO;
68
   start          := '0';
69
   do_write       := '0';
70
   do_hi          := '0';
71
 
72
   case mult_func is
73
   when mult_read_lo =>
74
   when mult_read_hi =>
75
      do_hi := '1';
76
   when mult_write_lo =>
77
      do_write := '1';
78
   when mult_write_hi =>
79
      do_write := '1';
80
      do_hi := '1';
81
   when mult_mult =>
82
      start := '1';
83
      do_div_temp := '0';
84
   when mult_divide =>
85
      start := '1';
86
      do_div_temp := '1';
87
      do_signed_temp := '0';
88
   when mult_signed_divide =>
89
      start := '1';
90
      do_div_temp := '1';
91
      do_signed_temp := '1';
92
   when others =>
93
   end case;
94
 
95
   if start = '1' then
96
      count_temp := "000000";
97
      a_temp := a;
98
      answer_temp := ZERO;
99
      if do_div_temp = '1' then
100 18 rhoads
         b_temp(63) := '0';
101 2 rhoads
         if do_signed_temp = '0' or b(31) = '0' then
102
            b_temp(62 downto 31) := b;
103
         else
104
            b_temp(62 downto 31) := bv_negate(b);
105
            a_temp := bv_negate(a);
106
         end if;
107
         b_temp(30 downto 0) := ZERO(30 downto 0);
108 18 rhoads
         if do_signed_temp = '1' and a(31) = b(31) then
109
            do_signed_temp := '0';
110
         end if;
111 2 rhoads
      else --multiply
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 18 rhoads
   aa := do_signed_reg & 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 18 rhoads
         if reg_b(63 downto 32) = ZERO and sum(32) = do_signed_reg 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
            b_temp(31 downto 0) := answer_temp;
146
         end if;
147
      else  -- mult_mode
148
         if reg_b(0) = '1' then
149
            b_temp(63 downto 31) := sum;
150
         else
151
            b_temp(63 downto 31) := '0' & reg_b(63 downto 32);
152
         end if;
153
         b_temp(30 downto 0) := reg_b(31 downto 1);
154
         if count_reg = "010000" and          --early stop
155 7 rhoads
               reg_b(15 downto 0) = ZERO(15 downto 0) then
156 2 rhoads
            count_temp := "111111";
157
            b_temp(31 downto 0) := reg_b(47 downto 16);
158
         end if;
159 7 rhoads
         if count_reg = "001000" and          --early stop
160
               reg_b(23 downto 0) = ZERO(23 downto 0) then
161
            count_temp := "111111";
162
            b_temp(31 downto 0) := reg_b(55 downto 24);
163
         end if;
164 2 rhoads
      end if;
165
   end if;
166
 
167
   if rising_edge(clk) then
168
      do_div_reg <= do_div_temp;
169
      do_signed_reg <= do_signed_temp;
170
      count_reg <= count_temp;
171
      reg_a <= a_temp;
172
      reg_b <= b_temp;
173
      answer_reg <= answer_temp;
174
   end if;
175
 
176
   if count_reg(5) = '0' and mult_func/= mult_nothing and start = '0' then
177
      pause_out <= '1';
178
   else
179
      pause_out <= '0';
180
   end if;
181
   if mult_func = mult_read_lo then
182
      c_mult <= reg_b(31 downto 0);
183
   elsif mult_func = mult_read_hi then
184
      c_mult <= reg_b(63 downto 32);
185
   else
186
      c_mult <= ZERO;
187
   end if;
188
 
189 18 rhoads
--   sum_out <= sum;
190
 
191 2 rhoads
end process;
192
 
193
end; --architecture logic
194
 
195
 

powered by: WebSVN 2.1.0

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