1 |
2 |
jidan |
-------------------------------------------------------------------------------
|
2 |
|
|
--
|
3 |
|
|
-- Project: <Floating Point Unit Core>
|
4 |
|
|
--
|
5 |
|
|
-- Description: post-normalization entity for the division unit
|
6 |
|
|
-------------------------------------------------------------------------------
|
7 |
|
|
--
|
8 |
|
|
-- 100101011010011100100
|
9 |
|
|
-- 110000111011100100000
|
10 |
|
|
-- 100000111011000101101
|
11 |
|
|
-- 100010111100101111001
|
12 |
|
|
-- 110000111011101101001
|
13 |
|
|
-- 010000001011101001010
|
14 |
|
|
-- 110100111001001100001
|
15 |
|
|
-- 110111010000001100111
|
16 |
|
|
-- 110110111110001011101
|
17 |
|
|
-- 101110110010111101000
|
18 |
|
|
-- 100000010111000000000
|
19 |
|
|
--
|
20 |
|
|
-- Author: Jidan Al-eryani
|
21 |
|
|
-- E-mail: jidan@gmx.net
|
22 |
|
|
--
|
23 |
|
|
-- Copyright (C) 2006
|
24 |
|
|
--
|
25 |
|
|
-- This source file may be used and distributed without
|
26 |
|
|
-- restriction provided that this copyright statement is not
|
27 |
|
|
-- removed from the file and that any derivative work contains
|
28 |
|
|
-- the original copyright notice and the associated disclaimer.
|
29 |
|
|
--
|
30 |
|
|
-- THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY
|
31 |
|
|
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
32 |
|
|
-- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
33 |
|
|
-- FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR
|
34 |
|
|
-- OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
35 |
|
|
-- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
36 |
|
|
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
37 |
|
|
-- GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
38 |
|
|
-- BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
39 |
|
|
-- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
40 |
|
|
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
41 |
|
|
-- OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
42 |
|
|
-- POSSIBILITY OF SUCH DAMAGE.
|
43 |
|
|
--
|
44 |
|
|
|
45 |
|
|
library ieee ;
|
46 |
|
|
use ieee.std_logic_1164.all;
|
47 |
|
|
use ieee.std_logic_unsigned.all;
|
48 |
|
|
use ieee.std_logic_misc.all;
|
49 |
|
|
|
50 |
|
|
library work;
|
51 |
|
|
use work.fpupack.all;
|
52 |
|
|
|
53 |
|
|
|
54 |
|
|
entity post_norm_div is
|
55 |
|
|
port(
|
56 |
|
|
clk_i : in std_logic;
|
57 |
|
|
opa_i : in std_logic_vector(FP_WIDTH-1 downto 0);
|
58 |
|
|
opb_i : in std_logic_vector(FP_WIDTH-1 downto 0);
|
59 |
|
|
qutnt_i : in std_logic_vector(FRAC_WIDTH+3 downto 0);
|
60 |
|
|
rmndr_i : in std_logic_vector(FRAC_WIDTH+3 downto 0);
|
61 |
|
|
exp_10_i : in std_logic_vector(EXP_WIDTH+1 downto 0);
|
62 |
|
|
sign_i : in std_logic;
|
63 |
|
|
rmode_i : in std_logic_vector(1 downto 0);
|
64 |
|
|
output_o : out std_logic_vector(FP_WIDTH-1 downto 0);
|
65 |
|
|
ine_o : out std_logic
|
66 |
|
|
);
|
67 |
|
|
end post_norm_div;
|
68 |
|
|
|
69 |
|
|
architecture rtl of post_norm_div is
|
70 |
|
|
|
71 |
|
|
|
72 |
|
|
-- input&output register signals
|
73 |
|
|
signal s_opa_i, s_opb_i : std_logic_vector(FP_WIDTH-1 downto 0);
|
74 |
|
|
signal s_expa, s_expb : std_logic_vector(EXP_WIDTH-1 downto 0);
|
75 |
|
|
signal s_qutnt_i, s_rmndr_i : std_logic_vector(FRAC_WIDTH+3 downto 0);
|
76 |
|
|
signal s_r_zeros : std_logic_vector(5 downto 0);
|
77 |
|
|
signal s_exp_10_i : std_logic_vector(EXP_WIDTH+1 downto 0);
|
78 |
|
|
signal s_sign_i : std_logic;
|
79 |
|
|
signal s_rmode_i : std_logic_vector(1 downto 0);
|
80 |
|
|
signal s_output_o : std_logic_vector(FP_WIDTH-1 downto 0);
|
81 |
|
|
signal s_ine_o, s_overflow : std_logic;
|
82 |
|
|
|
83 |
|
|
signal s_opa_dn, s_opb_dn : std_logic;
|
84 |
|
|
signal s_qutdn : std_logic;
|
85 |
|
|
|
86 |
|
|
signal s_shr1, s_shl1 : std_logic_vector(5 downto 0);
|
87 |
|
|
signal s_shr2 : std_logic;
|
88 |
|
|
signal s_expo1, s_expo2, s_expo3 : std_logic_vector(8 downto 0);
|
89 |
|
|
signal s_fraco1 : std_logic_vector(26 downto 0);
|
90 |
|
|
signal s_frac_rnd, s_fraco2 : std_logic_vector(24 downto 0);
|
91 |
|
|
signal s_guard, s_round, s_sticky, s_roundup : std_logic;
|
92 |
|
|
signal s_lost : std_logic;
|
93 |
|
|
|
94 |
|
|
signal s_op_0, s_opab_0 : std_logic;
|
95 |
|
|
signal s_infa, s_infb : std_logic;
|
96 |
|
|
signal s_nan_in, s_nan_op, s_nan_a, s_nan_b : std_logic;
|
97 |
|
|
signal s_exp_10b : std_logic_vector(9 downto 0);
|
98 |
|
|
|
99 |
|
|
begin
|
100 |
|
|
|
101 |
|
|
-- Input Register
|
102 |
|
|
process(clk_i)
|
103 |
|
|
begin
|
104 |
|
|
if rising_edge(clk_i) then
|
105 |
|
|
s_opa_i <= opa_i;
|
106 |
|
|
s_opb_i <= opb_i;
|
107 |
|
|
s_expa <= opa_i(30 downto 23);
|
108 |
|
|
s_expb <= opb_i(30 downto 23);
|
109 |
|
|
s_qutnt_i <= qutnt_i;
|
110 |
|
|
s_rmndr_i <= rmndr_i;
|
111 |
|
|
s_exp_10_i <= exp_10_i;
|
112 |
|
|
s_sign_i <= sign_i;
|
113 |
|
|
s_rmode_i <= rmode_i;
|
114 |
|
|
end if;
|
115 |
|
|
end process;
|
116 |
|
|
|
117 |
|
|
-- Output Register
|
118 |
|
|
process(clk_i)
|
119 |
|
|
begin
|
120 |
|
|
if rising_edge(clk_i) then
|
121 |
|
|
output_o <= s_output_o;
|
122 |
|
|
ine_o <= s_ine_o;
|
123 |
|
|
end if;
|
124 |
|
|
end process;
|
125 |
|
|
|
126 |
|
|
-- qutnt_i
|
127 |
|
|
-- 26 25 3
|
128 |
|
|
-- | | |
|
129 |
|
|
-- h fffffffffffffffffffffff grs
|
130 |
|
|
|
131 |
|
|
--*** Stage 1 ****
|
132 |
|
|
-- figure out the exponent and howmuch the fraction has to be shiftd right/left
|
133 |
|
|
|
134 |
|
|
s_opa_dn <= '1' when or_reduce(s_expa)='0' and or_reduce(opa_i(22 downto 0))='1' else '0';
|
135 |
|
|
s_opb_dn <= '1' when or_reduce(s_expb)='0' and or_reduce(opb_i(22 downto 0))='1' else '0';
|
136 |
|
|
|
137 |
|
|
s_qutdn <= not s_qutnt_i(26);
|
138 |
|
|
|
139 |
|
|
|
140 |
|
|
s_exp_10b <= s_exp_10_i - ("000000000"&s_qutdn);
|
141 |
|
|
|
142 |
|
|
|
143 |
|
|
|
144 |
|
|
process(clk_i)
|
145 |
|
|
variable v_shr, v_shl : std_logic_vector(9 downto 0);
|
146 |
|
|
begin
|
147 |
|
|
if rising_edge(clk_i) then
|
148 |
|
|
if s_exp_10b(9)='1' or s_exp_10b="0000000000" then
|
149 |
|
|
v_shr := ("0000000001" - s_exp_10b) - s_qutdn;
|
150 |
|
|
v_shl := (others =>'0');
|
151 |
|
|
s_expo1 <= "000000001";
|
152 |
|
|
elsif s_exp_10b(8)='1' then
|
153 |
|
|
v_shr := (others =>'0');
|
154 |
|
|
v_shl := (others =>'0');
|
155 |
|
|
s_expo1 <= "011111111";
|
156 |
|
|
else
|
157 |
|
|
v_shr := (others =>'0');
|
158 |
|
|
v_shl := "000000000"& s_qutdn;
|
159 |
|
|
s_expo1 <= s_exp_10b(8 downto 0);
|
160 |
|
|
end if;
|
161 |
|
|
if v_shr(6)='1' then
|
162 |
|
|
s_shr1 <= "111111";
|
163 |
|
|
else
|
164 |
|
|
s_shr1 <= v_shr(5 downto 0);
|
165 |
|
|
end if;
|
166 |
|
|
s_shl1 <= v_shl(5 downto 0);
|
167 |
|
|
end if;
|
168 |
|
|
end process;
|
169 |
|
|
|
170 |
|
|
|
171 |
|
|
-- *** Stage 2 ***
|
172 |
|
|
-- Shifting the fraction and rounding
|
173 |
|
|
|
174 |
|
|
|
175 |
|
|
-- shift the fraction
|
176 |
|
|
process(clk_i)
|
177 |
|
|
begin
|
178 |
|
|
if rising_edge(clk_i) then
|
179 |
|
|
if s_shr1 /= "000000" then
|
180 |
|
|
s_fraco1 <= shr(s_qutnt_i, s_shr1);
|
181 |
|
|
else
|
182 |
|
|
s_fraco1 <= shl(s_qutnt_i, s_shl1);
|
183 |
|
|
end if;
|
184 |
|
|
end if;
|
185 |
|
|
end process;
|
186 |
|
|
|
187 |
|
|
s_expo2 <= s_expo1 - "000000001" when s_fraco1(26)='0' else s_expo1;
|
188 |
|
|
|
189 |
|
|
|
190 |
|
|
s_r_zeros <= count_r_zeros(s_qutnt_i);
|
191 |
|
|
|
192 |
|
|
|
193 |
|
|
s_lost <= '1' when (s_shr1+("00000"&s_shr2)) > s_r_zeros else '0';
|
194 |
|
|
|
195 |
|
|
-- ***Stage 3***
|
196 |
|
|
-- Rounding
|
197 |
|
|
|
198 |
|
|
s_guard <= s_fraco1(2);
|
199 |
|
|
s_round <= s_fraco1(1);
|
200 |
|
|
s_sticky <= s_fraco1(0) or or_reduce(s_rmndr_i);
|
201 |
|
|
|
202 |
|
|
s_roundup <= s_guard and ((s_round or s_sticky)or s_fraco1(3)) when s_rmode_i="00" else -- round to nearset even
|
203 |
|
|
( s_guard or s_round or s_sticky) and (not s_sign_i) when s_rmode_i="10" else -- round up
|
204 |
|
|
( s_guard or s_round or s_sticky) and (s_sign_i) when s_rmode_i="11" else -- round down
|
205 |
|
|
'0'; -- round to zero(truncate = no rounding)
|
206 |
|
|
|
207 |
|
|
|
208 |
|
|
s_frac_rnd <= ("0"&s_fraco1(26 downto 3)) + '1' when s_roundup='1' else "0"&s_fraco1(26 downto 3);
|
209 |
|
|
s_shr2 <= s_frac_rnd(24);
|
210 |
|
|
|
211 |
|
|
process(clk_i)
|
212 |
|
|
begin
|
213 |
|
|
if rising_edge(clk_i) then
|
214 |
|
|
if s_shr2='1' and s_expo2 /= "011111111" then
|
215 |
|
|
s_expo3 <= s_expo2 + "1";
|
216 |
|
|
s_fraco2 <= "0"&s_frac_rnd(24 downto 1);
|
217 |
|
|
else
|
218 |
|
|
s_expo3 <= s_expo2;
|
219 |
|
|
s_fraco2 <= s_frac_rnd;
|
220 |
|
|
end if;
|
221 |
|
|
end if;
|
222 |
|
|
end process;
|
223 |
|
|
|
224 |
|
|
|
225 |
|
|
---
|
226 |
|
|
|
227 |
|
|
---***Stage 4****
|
228 |
|
|
-- Output
|
229 |
|
|
|
230 |
|
|
s_op_0 <= not ( or_reduce(s_opa_i(30 downto 0)) and or_reduce(s_opb_i(30 downto 0)) );
|
231 |
|
|
s_opab_0 <= not ( or_reduce(s_opa_i(30 downto 0)) or or_reduce(s_opb_i(30 downto 0)) );
|
232 |
|
|
|
233 |
|
|
s_infa <= '1' when s_expa="11111111" else '0';
|
234 |
|
|
s_infb <= '1' when s_expb="11111111" else '0';
|
235 |
|
|
|
236 |
|
|
s_nan_a <= '1' when (s_infa='1' and or_reduce (s_opa_i(22 downto 0))='1') else '0';
|
237 |
|
|
s_nan_b <= '1' when (s_infb='1' and or_reduce (s_opb_i(22 downto 0))='1') else '0';
|
238 |
|
|
s_nan_in <= '1' when s_nan_a='1' or s_nan_b='1' else '0';
|
239 |
|
|
s_nan_op <= '1' when (s_infa and s_infb)='1' or s_opab_0='1' else '0';-- 0 / 0, inf / inf
|
240 |
|
|
|
241 |
|
|
|
242 |
|
|
s_overflow <= '1' when s_expo3 = "011111111" and (s_infa or s_infb)='0' and or_reduce(s_opb_i(30 downto 0))='1' else '0';
|
243 |
|
|
|
244 |
|
|
s_ine_o <= '1' when s_op_0='0' and (s_lost or or_reduce(s_fraco1(2 downto 0)) or s_overflow or or_reduce(s_rmndr_i))='1' else '0';
|
245 |
|
|
|
246 |
|
|
process(s_sign_i, s_expo3, s_fraco2, s_nan_in, s_nan_op, s_infa, s_infb, s_overflow)
|
247 |
|
|
begin
|
248 |
|
|
if (s_nan_in or s_nan_op)='1' then
|
249 |
|
|
s_output_o <= s_sign_i & QNAN;
|
250 |
|
|
elsif (s_infa or s_infb)='1' or s_overflow='1' then
|
251 |
|
|
s_output_o <= s_sign_i & INF;
|
252 |
|
|
else
|
253 |
|
|
s_output_o <= s_sign_i & s_expo3(7 downto 0) & s_fraco2(22 downto 0);
|
254 |
|
|
end if;
|
255 |
|
|
end process;
|
256 |
|
|
|
257 |
|
|
end rtl;
|