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

Subversion Repositories fpu_double

[/] [fpu_double/] [trunk/] [fpu_sub.vhd] - Blame information for rev 5

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 davidklun
---------------------------------------------------------------------
2
----                                                             ----
3
----  FPU                                                        ----
4
----  Floating Point Unit (Double precision)                     ----
5
----                                                             ----
6
----  Author: David Lundgren                                     ----
7
----          davidklun@gmail.com                                ----
8
----                                                             ----
9
---------------------------------------------------------------------
10
----                                                             ----
11
---- Copyright (C) 2009 David Lundgren                           ----
12
----                  davidklun@gmail.com                        ----
13
----                                                             ----
14
---- This source file may be used and distributed without        ----
15
---- restriction provided that this copyright statement is not   ----
16
---- removed from the file and that any derivative work contains ----
17
---- the original copyright notice and the associated disclaimer.----
18
----                                                             ----
19
----     THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY     ----
20
---- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED   ----
21
---- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS   ----
22
---- FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR      ----
23
---- OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,         ----
24
---- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    ----
25
---- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE   ----
26
---- GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR        ----
27
---- BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF  ----
28
---- LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT  ----
29
---- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT  ----
30
---- OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE         ----
31
---- POSSIBILITY OF SUCH DAMAGE.                                 ----
32
----                                                             ----
33
---------------------------------------------------------------------
34
 
35
        LIBRARY ieee;
36
        USE ieee.std_logic_1164.all;
37
        USE ieee.std_logic_arith.all;
38
        use ieee.std_logic_unsigned.all;
39
        use ieee.std_logic_misc.all;
40
        library work;
41
        use work.fpupack.all;
42
 
43
        ENTITY fpu_sub IS
44
 
45
   PORT(
46
      clk : IN     std_logic;
47
      rst : IN     std_logic;
48
      enable  : IN     std_logic;
49
      opa : IN     std_logic_vector (63 DOWNTO 0);
50
      opb : IN     std_logic_vector (63 DOWNTO 0);
51
      fpu_op : IN     std_logic_vector (2 DOWNTO 0);
52
      sign : OUT    std_logic;
53
      diff_2 : OUT    std_logic_vector (55 DOWNTO 0);
54
      exponent_2 : OUT    std_logic_vector (10 DOWNTO 0)
55
   );
56
 
57
        -- Declarations
58
 
59
        END fpu_sub;
60
 
61
 
62
        architecture rtl of fpu_sub is
63
 
64
        signal   fpu_op_add : std_logic;
65
        signal   diff_shift : std_logic_vector(5 downto 0);
66
        signal   diff_shift_2 : std_logic_vector(5 downto 0);
67
        signal   exponent_a : std_logic_vector(10 downto 0);
68
        signal   exponent_b : std_logic_vector(10 downto 0);
69
        signal   mantissa_a : std_logic_vector(51 downto 0);
70
        signal   mantissa_b : std_logic_vector(51 downto 0);
71
        signal   expa_gt_expb : std_logic;
72
        signal   expa_et_expb : std_logic;
73
        signal   mana_gtet_manb : std_logic;
74
        signal   a_gtet_b : std_logic;
75
        signal   exponent_small : std_logic_vector(10 downto 0);
76
        signal   exponent_large : std_logic_vector(10 downto 0);
77
        signal   mantissa_small : std_logic_vector(51 downto 0);
78
        signal   mantissa_large : std_logic_vector(51 downto 0);
79
        signal   small_is_denorm : std_logic;
80
        signal   large_is_denorm : std_logic;
81
        signal   large_norm_small_denorm : std_logic;
82
        signal   small_is_nonzero : std_logic;
83
        signal   exponent_diff : std_logic_vector(10 downto 0);
84
        signal   minuend : std_logic_vector(54 downto 0);
85
        signal   subtrahend : std_logic_vector(54 downto 0);
86
        signal   subtra_shift : std_logic_vector(54 downto 0);
87
        signal   subtra_shift_nonzero : std_logic;
88
        signal   subtra_fraction_enable : std_logic;
89
        signal   subtra_shift_2 : std_logic_vector(54 downto 0);
90
        signal   subtra_shift_3 : std_logic_vector(54 downto 0);
91
        signal   diff : std_logic_vector(54 downto 0);
92
        signal   diffshift_gt_exponent : std_logic;
93
        signal   diffshift_et_55 : std_logic; -- when the difference = 0
94
        signal   diff_1 : std_logic_vector(54 downto 0);
95
        signal   exponent : std_logic_vector(10 downto 0);
96
        signal   in_norm_out_denorm : std_logic;
97
 
98
        begin
99
 
100
        subtra_shift_nonzero <= or_reduce(subtra_shift);
101
        subtra_fraction_enable <= small_is_nonzero and not subtra_shift_nonzero;
102
        subtra_shift_2 <= "0000000000000000000000000000000000000000000000000000001";
103
        in_norm_out_denorm <= or_reduce(exponent_large) and not or_reduce(exponent);
104
        fpu_op_add <= '1' when fpu_op = "000" else '0';
105
 
106
process
107
        begin
108
        wait until clk'event and clk = '1';
109
                if (rst = '1') then
110
                        exponent_a <= (others =>'0');
111
                        exponent_b <= (others =>'0');
112
                        mantissa_a <= (others =>'0');
113
                        mantissa_b <= (others =>'0');
114
                        expa_gt_expb <= '0';
115
                        expa_et_expb <= '0';
116
                        mana_gtet_manb <= '0';
117
                        a_gtet_b <= '0';
118
                        exponent_small  <= (others =>'0');
119
                        exponent_large  <= (others =>'0');
120
                        mantissa_small  <= (others =>'0');
121
                        mantissa_large  <= (others =>'0');
122
                        sign <= '0';
123
                        small_is_denorm <= '0';
124
                        large_is_denorm <= '0';
125
                        large_norm_small_denorm <= '0';
126
                        small_is_nonzero <= '0';
127
                        exponent_diff <= (others =>'0');
128
                        minuend <= (others =>'0');
129
                        subtrahend <= (others =>'0');
130
                        subtra_shift <= (others =>'0');
131
                        subtra_shift_3 <= (others =>'0');
132
                        diff_shift_2 <= (others =>'0');
133
                        diff <= (others =>'0');
134
                        diffshift_gt_exponent <= '0';
135
                        diffshift_et_55 <= '0';
136
                        diff_1 <= (others =>'0');
137
                        exponent <= (others =>'0');
138
                        exponent_2 <= (others =>'0');
139
                        diff_2 <= (others =>'0');
140
                elsif (enable = '1') then
141
                        exponent_a <= opa(62 downto 52);
142
                        exponent_b <= opb(62 downto 52);
143
                        mantissa_a <= opa(51 downto 0);
144
                        mantissa_b <= opb(51 downto 0);
145
                        if (exponent_a > exponent_b) then
146
                                expa_gt_expb <= '1';
147
                        else
148
                                expa_gt_expb <= '0';
149
                        end if;
150
                        if (exponent_a = exponent_b) then
151
                                expa_et_expb <= '1';
152
                        else
153
                                expa_et_expb <= '0';
154
                        end if;
155
                        if (mantissa_a >= mantissa_b) then
156
                                mana_gtet_manb <= '1';
157
                        else
158
                                mana_gtet_manb <= '0';
159
                        end if;
160
                        a_gtet_b <= expa_gt_expb or (expa_et_expb and mana_gtet_manb);
161
                        if (a_gtet_b = '1') then
162
                                exponent_small <= exponent_b;
163
                                exponent_large <= exponent_a;
164
                                mantissa_small <= mantissa_b;
165
                                mantissa_large <= mantissa_a;
166
                                sign <= opa(63);
167
                        else
168
                                exponent_small <= exponent_a;
169
                                exponent_large <= exponent_b;
170
                                mantissa_small <= mantissa_a;
171
                                mantissa_large <= mantissa_b;
172
                                sign <= (not opb(63)) xor fpu_op_add;
173
                        end if;
174
                        if (exponent_small > 0) then
175
                                small_is_denorm <= '0';
176
                        else
177
                                small_is_denorm <= '1';
178
                        end if;
179
                        if (exponent_large > 0) then
180
                                large_is_denorm <= '0';
181
                        else
182
                                large_is_denorm <= '1';
183
                        end if;
184
                        if (small_is_denorm = '1' and large_is_denorm = '0') then
185
                                large_norm_small_denorm <= '1';
186
                        else
187
                                large_norm_small_denorm <= '0';
188
                        end if;
189
                        small_is_nonzero <= (not small_is_denorm) or or_reduce(mantissa_small);
190
                        exponent_diff <= exponent_large - exponent_small - large_norm_small_denorm;
191
                        minuend <= not large_is_denorm & mantissa_large & "00";
192
                        subtrahend <= not small_is_denorm & mantissa_small & "00";
193
                        subtra_shift <= shr(subtrahend,  exponent_diff);
194
                        if (subtra_fraction_enable = '1') then
195
                                subtra_shift_3 <= subtra_shift_2;
196
                        else
197
                                subtra_shift_3 <= subtra_shift;
198
                        end if;
199
                        diff <= minuend - subtra_shift_3;
200
                        diff_shift <= count_l_zeros(diff(54 downto 0));
201
                        diff_shift_2 <= diff_shift;
202
                        if (diff_shift_2 > exponent_large) then
203
                                diffshift_gt_exponent <= '1';
204
                        else
205
                                diffshift_gt_exponent <= '0';
206
                        end if;
207
                        if (diff_shift_2 = "0110111") then -- 55
208
                                diffshift_et_55 <= '1';
209
                        else
210
                                diffshift_et_55 <= '0';
211
                        end if;
212
                        if (diffshift_gt_exponent = '1') then
213
                                diff_1 <= shl(diff, exponent_large);
214
                                exponent <= "00000000000";
215
                        else
216
                                diff_1 <= shl(diff, diff_shift_2);
217
                                exponent <= exponent_large - diff_shift_2;
218
                        end if;
219
                        if (diffshift_et_55 = '1') then
220
                                exponent_2 <= "00000000000";
221
                        else
222
                                exponent_2 <=  exponent;
223
                        end if;
224
                        if (in_norm_out_denorm = '1') then
225
                                diff_2 <= '0' & shr(diff_1,conv_std_logic_vector('1', 55));
226
                        else
227
                                diff_2 <= '0' & diff_1;
228
                        end if;
229
                end if;
230
        end process;
231
 
232
        end rtl;
233
 
234
 

powered by: WebSVN 2.1.0

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