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

Subversion Repositories mlite

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

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 sign_extension : std_logic;
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
   sign_extension := '0';
66
   aa             := '0' & ZERO;
67
   bb             := '0' & ZERO;
68
   sum            := '0' & ZERO;
69
   start          := '0';
70
   do_write       := '0';
71
   do_hi          := '0';
72
 
73
   case mult_func is
74
   when mult_read_lo =>
75
   when mult_read_hi =>
76
      do_hi := '1';
77
   when mult_write_lo =>
78
      do_write := '1';
79
   when mult_write_hi =>
80
      do_write := '1';
81
      do_hi := '1';
82
   when mult_mult =>
83
      start := '1';
84
      do_div_temp := '0';
85
   when mult_divide =>
86
      start := '1';
87
      do_div_temp := '1';
88
      do_signed_temp := '0';
89
   when mult_signed_divide =>
90
      start := '1';
91
      do_div_temp := '1';
92
      do_signed_temp := '1';
93
   when others =>
94
   end case;
95
 
96
   if start = '1' then
97
      count_temp := "000000";
98
      a_temp := a;
99
      answer_temp := ZERO;
100
      if do_div_temp = '1' then
101
         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
      else --multiply
109 7 rhoads
         b_temp := ZERO & b;
110 2 rhoads
      end if;
111
   elsif do_write = '1' then
112
      if do_hi = '0' then
113
         b_temp(31 downto 0) := a;
114
      else
115
         b_temp(63 downto 32) := a;
116
      end if;
117
   end if;
118
 
119
   if do_div_reg = '1' then
120
      bb := reg_b(32 downto 0);
121
   else
122
      bb := '0' & reg_b(63 downto 32);
123
   end if;
124
   sign_extension := reg_a(31) and do_signed_reg;
125
   aa := sign_extension & reg_a;
126
   sum := bv_adder(aa, bb, do_div_reg);
127
--   sum := bv_adder_lookahead(aa, bb, do_div_reg);
128
 
129
   if count_reg(5) = '0' and start = '0' then
130
      count_temp := bv_inc6(count_reg);
131
      if do_div_reg = '1' then
132
         answer_temp(31 downto 1) := answer_reg(30 downto 0);
133
         if reg_b(63 downto 32) = ZERO and sum(32) = '0' then
134
            a_temp := sum(31 downto 0);  --aa=aa-bb;
135
            answer_temp(0) := '1';
136
         else
137
            answer_temp(0) := '0';
138
         end if;
139
         if count_reg /= "011111" then
140
            b_temp(62 downto 0) := reg_b(63 downto 1);
141
         else
142
            b_temp(63 downto 32) := a_temp;
143
            b_temp(31 downto 0) := answer_temp;
144
         end if;
145
      else  -- mult_mode
146
         if reg_b(0) = '1' then
147
            b_temp(63 downto 31) := sum;
148
         else
149
            b_temp(63 downto 31) := '0' & reg_b(63 downto 32);
150
         end if;
151
         b_temp(30 downto 0) := reg_b(31 downto 1);
152
         if count_reg = "010000" and          --early stop
153 7 rhoads
               reg_b(15 downto 0) = ZERO(15 downto 0) then
154 2 rhoads
            count_temp := "111111";
155
            b_temp(31 downto 0) := reg_b(47 downto 16);
156
         end if;
157 7 rhoads
         if count_reg = "001000" and          --early stop
158
               reg_b(23 downto 0) = ZERO(23 downto 0) then
159
            count_temp := "111111";
160
            b_temp(31 downto 0) := reg_b(55 downto 24);
161
         end if;
162 2 rhoads
      end if;
163
   end if;
164
 
165
   if rising_edge(clk) then
166
      do_div_reg <= do_div_temp;
167
      do_signed_reg <= do_signed_temp;
168
      count_reg <= count_temp;
169
      reg_a <= a_temp;
170
      reg_b <= b_temp;
171
      answer_reg <= answer_temp;
172
   end if;
173
 
174
   if count_reg(5) = '0' and mult_func/= mult_nothing and start = '0' then
175
      pause_out <= '1';
176
   else
177
      pause_out <= '0';
178
   end if;
179
   if mult_func = mult_read_lo then
180
      c_mult <= reg_b(31 downto 0);
181
   elsif mult_func = mult_read_hi then
182
      c_mult <= reg_b(63 downto 32);
183
   else
184
      c_mult <= ZERO;
185
   end if;
186
 
187
end process;
188
 
189
end; --architecture logic
190
 
191
 

powered by: WebSVN 2.1.0

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