1 |
118 |
jguarin200 |
------------------------------------------------
|
2 |
119 |
jguarin200 |
--! @file fadd32.vhd
|
3 |
118 |
jguarin200 |
--! @brief RayTrac Floating Point Adder
|
4 |
|
|
--! @author Julián Andrés Guarín Reyes
|
5 |
|
|
--------------------------------------------------
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
-- RAYTRAC (FP BRANCH)
|
9 |
|
|
-- Author Julian Andres Guarin
|
10 |
119 |
jguarin200 |
-- fadd32.vhd
|
11 |
118 |
jguarin200 |
-- This file is part of raytrac.
|
12 |
|
|
--
|
13 |
|
|
-- raytrac is free software: you can redistribute it and/or modify
|
14 |
|
|
-- it under the terms of the GNU General Public License as published by
|
15 |
|
|
-- the Free Software Foundation, either version 3 of the License, or
|
16 |
|
|
-- (at your option) any later version.
|
17 |
|
|
--
|
18 |
|
|
-- raytrac is distributed in the hope that it will be useful,
|
19 |
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20 |
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21 |
|
|
-- GNU General Public License for more details.
|
22 |
|
|
--
|
23 |
|
|
-- You should have received a copy of the GNU General Public License
|
24 |
|
|
-- along with raytrac. If not, see <http://www.gnu.org/licenses/>
|
25 |
|
|
library ieee;
|
26 |
|
|
use ieee.std_logic_1164.all;
|
27 |
|
|
use ieee.std_logic_unsigned.all;
|
28 |
155 |
jguarin200 |
|
29 |
|
|
use work.arithpack.all;
|
30 |
|
|
|
31 |
118 |
jguarin200 |
--! Esta entidad recibe dos números en formato punto flotante IEEE 754, de precision simple y devuelve las mantissas signadas y corridas, y el exponente correspondiente al resultado antes de normalizarlo al formato float.
|
32 |
|
|
--!\nLas 2 mantissas y el exponente entran despues a la entidad add2 que suma las mantissas y entrega el resultado en formato IEEE 754.
|
33 |
139 |
jguarin200 |
entity fadd32 is
|
34 |
150 |
jguarin200 |
|
35 |
118 |
jguarin200 |
port (
|
36 |
150 |
jguarin200 |
clk,dpc : in std_logic;
|
37 |
158 |
jguarin200 |
a32,b32 : in xfloat32;
|
38 |
|
|
c32 : out xfloat32
|
39 |
118 |
jguarin200 |
);
|
40 |
153 |
jguarin200 |
end entity;
|
41 |
119 |
jguarin200 |
architecture fadd32_arch of fadd32 is
|
42 |
118 |
jguarin200 |
|
43 |
190 |
jguarin200 |
--! Altera Compiler Directive, to avoid m9k autoinferring thanks to the guys at http://www.alteraforum.com/forum/archive/index.php/t-30784.html ....
|
44 |
|
|
attribute altera_attribute : string;
|
45 |
|
|
attribute altera_attribute of fadd32_arch : architecture is "-name AUTO_SHIFT_REGISTER_RECOGNITION OFF";
|
46 |
118 |
jguarin200 |
|
47 |
190 |
jguarin200 |
|
48 |
163 |
jguarin200 |
--!TBXSTART:STAGE0
|
49 |
|
|
signal s0delta : std_logic_vector(7 downto 0);
|
50 |
|
|
signal s0a,s0b : std_logic_vector(31 downto 0); -- Float 32 bit
|
51 |
|
|
|
52 |
152 |
jguarin200 |
--!TBXEND
|
53 |
163 |
jguarin200 |
--!TBXSTART:STAGE1
|
54 |
|
|
signal s1zero : std_logic;
|
55 |
|
|
signal s1delta : std_logic_vector(5 downto 0);
|
56 |
|
|
signal s1exp : std_logic_vector(7 downto 0);
|
57 |
|
|
signal s1shifter,s1datab_8x : std_logic_vector(8 downto 0);
|
58 |
|
|
signal s1pl,s1datab : std_logic_vector(17 downto 0);
|
59 |
|
|
signal s1umantshift,s1umantfixed,s1postshift,s1xorslab : std_logic_vector(23 downto 0);
|
60 |
|
|
signal s1ph : std_logic_vector(26 downto 0);
|
61 |
|
|
--!TBXEND
|
62 |
|
|
--!TBXSTART:STAGE2
|
63 |
|
|
signal s2exp : std_logic_vector(7 downto 0);
|
64 |
|
|
signal s2xorslab : std_logic_vector(23 downto 0);
|
65 |
|
|
signal s2umantshift, s2mantfixed : std_logic_vector(24 downto 0);
|
66 |
|
|
--!TBXEND
|
67 |
|
|
--!TBXSTART:STAGE3
|
68 |
|
|
signal s3exp : std_logic_vector(7 downto 0);
|
69 |
|
|
signal s3mantfixed,s3mantshift : std_logic_vector (24 downto 0);
|
70 |
|
|
--!TBXEND
|
71 |
|
|
--!TBXSTART:STAGE4
|
72 |
|
|
signal s4exp : std_logic_vector (7 downto 0);
|
73 |
|
|
signal s4xorslab : std_logic_vector (24 downto 0);
|
74 |
170 |
jguarin200 |
signal s4sresult : std_logic_vector (25 downto 0);
|
75 |
163 |
jguarin200 |
--!TBXEND
|
76 |
|
|
--!TBXSTART:STAGE5
|
77 |
170 |
jguarin200 |
signal s5exp : std_logic_vector (7 downto 0);
|
78 |
|
|
signal s5result : std_logic_vector (25 downto 0);
|
79 |
163 |
jguarin200 |
--!TBXEND
|
80 |
118 |
jguarin200 |
|
81 |
163 |
jguarin200 |
|
82 |
|
|
|
83 |
|
|
|
84 |
|
|
|
85 |
170 |
jguarin200 |
|
86 |
118 |
jguarin200 |
begin
|
87 |
150 |
jguarin200 |
|
88 |
139 |
jguarin200 |
process (clk)
|
89 |
118 |
jguarin200 |
begin
|
90 |
139 |
jguarin200 |
if clk'event and clk='1' then
|
91 |
118 |
jguarin200 |
|
92 |
|
|
--!Registro de entrada
|
93 |
|
|
s0a <= a32;
|
94 |
|
|
s0b(31) <= dpc xor b32(31); --! Importante: Integrar el signo en el operando B
|
95 |
|
|
s0b(30 downto 0) <= b32(30 downto 0);
|
96 |
|
|
|
97 |
|
|
--!Etapa 0,Escoger el mayor exponente que sera el resultado desnormalizado, calcula cuanto debe ser el corrimiento de la mantissa con menor exponente y reorganiza los operandos, si el mayor es b, intercambia las posición si el mayor es a las posiciones la mantiene. Zero check.
|
98 |
|
|
--!signo,exponente,mantissa
|
99 |
|
|
if (s0b(30 downto 23)&s0a(30 downto 23))=x"0000" then
|
100 |
|
|
s1zero <= '0';
|
101 |
|
|
else
|
102 |
|
|
s1zero <= '1';
|
103 |
|
|
end if;
|
104 |
164 |
jguarin200 |
s1delta <= s0delta(7) & (s0delta(7) xor s0delta(4))&(s0delta(7) xor s0delta(3)) & s0delta(2 downto 0);
|
105 |
118 |
jguarin200 |
case s0delta(7) is
|
106 |
|
|
when '1' =>
|
107 |
|
|
s1exp <= s0b(30 downto 23);
|
108 |
|
|
s1umantshift <= s0a(31)&s0a(22 downto 0);
|
109 |
|
|
s1umantfixed <= s0b(31)&s0b(22 downto 0);
|
110 |
|
|
when others =>
|
111 |
|
|
s1exp <= s0a(30 downto 23);
|
112 |
|
|
s1umantshift <= s0b(31)&s0b(22 downto 0);
|
113 |
|
|
s1umantfixed <= s0a(31)&s0a(22 downto 0);
|
114 |
|
|
end case;
|
115 |
|
|
|
116 |
164 |
jguarin200 |
--! Etapa 1: Denormalización de la mantissas.
|
117 |
118 |
jguarin200 |
case s1delta(4 downto 3) is
|
118 |
|
|
when "00" => s2umantshift <= s1umantshift(23)&s1postshift(23 downto 0);
|
119 |
|
|
when "01" => s2umantshift <= s1umantshift(23)&x"00"&s1postshift(23 downto 8);
|
120 |
|
|
when "10" => s2umantshift <= s1umantshift(23)&x"0000"&s1postshift(23 downto 16);
|
121 |
|
|
when others => s2umantshift <= (others => '0');
|
122 |
|
|
end case;
|
123 |
164 |
jguarin200 |
|
124 |
|
|
s2mantfixed <= s1umantfixed(23) & ( ( ('1'&s1umantfixed(22 downto 0)) xor s1xorslab) + ( x"00000"&"000"&s1umantfixed(23) ) );
|
125 |
118 |
jguarin200 |
s2exp <= s1exp;
|
126 |
|
|
|
127 |
|
|
--! Etapa2: Signar la mantissa denormalizada.
|
128 |
|
|
s3mantfixed <= s2mantfixed;
|
129 |
|
|
s3mantshift <= s2umantshift(24)& ( ( s2umantshift(23 downto 0) xor s2xorslab) + ( x"00000"&"000"&s2umantshift(24) ) );
|
130 |
|
|
s3exp <= s2exp;
|
131 |
|
|
|
132 |
119 |
jguarin200 |
--! Etapa 3: Etapa 3 Realizar la suma, entre la mantissa corrida y la fija.
|
133 |
118 |
jguarin200 |
s4sresult <= (s3mantshift(24)&s3mantshift)+(s3mantfixed(24)&s3mantfixed);
|
134 |
|
|
s4exp <= s3exp;
|
135 |
|
|
|
136 |
|
|
--! Etapa 4: Quitar el signo a la mantissa resultante.
|
137 |
|
|
s5result <= s4sresult(25)&((s4sresult(24 downto 0) xor s4xorslab) +(x"000000"&s4sresult(25)));
|
138 |
|
|
s5exp <= s4exp;
|
139 |
|
|
|
140 |
|
|
|
141 |
|
|
|
142 |
119 |
jguarin200 |
|
143 |
137 |
jguarin200 |
|
144 |
170 |
jguarin200 |
|
145 |
118 |
jguarin200 |
end if;
|
146 |
|
|
end process;
|
147 |
170 |
jguarin200 |
--! Etapa 5: Codificar el corrimiento para la normalizacion de la mantissa resultante y entregar el resultado.
|
148 |
|
|
c32(31) <= s5result(25);
|
149 |
|
|
process (s5result(24 downto 0))
|
150 |
|
|
begin
|
151 |
|
|
case s5result(24) is
|
152 |
|
|
when '1' =>
|
153 |
|
|
c32 (22 downto 00) <= s5result(23 downto 1);
|
154 |
|
|
c32 (30 downto 23) <= s5exp+1;
|
155 |
|
|
when others =>
|
156 |
|
|
c32 (22 downto 00) <= s5result(22 downto 0);
|
157 |
|
|
c32 (30 downto 23) <= s5exp;
|
158 |
|
|
end case;
|
159 |
|
|
end process;
|
160 |
|
|
|
161 |
137 |
jguarin200 |
|
162 |
118 |
jguarin200 |
--! Combinatorial gremlin, Etapa 0 el corrimiento de la mantissa con menor exponente y reorganiza los operandos,\n
|
163 |
|
|
--! si el mayor es b, intercambia las posición si el mayor es a las posiciones la mantiene.
|
164 |
|
|
s0delta <= s0a(30 downto 23)-s0b(30 downto 23);
|
165 |
|
|
--! Combinatorial Gremlin, Etapa 1 Codificar el factor de corrimiento de denormalizacion y denormalizar la mantissa no fija. Signar la mantissa que se queda fija.
|
166 |
|
|
decodeshiftfactor:
|
167 |
|
|
process (s1delta(2 downto 0))
|
168 |
|
|
begin
|
169 |
|
|
case s1delta(2 downto 0) is
|
170 |
|
|
when "111" => s1shifter(8 downto 0) <= '0'&s1delta(5)&"00000"¬(s1delta(5))&'0';
|
171 |
|
|
when "110" => s1shifter(8 downto 0) <= "00"&s1delta(5)&"000"¬(s1delta(5))&"00";
|
172 |
|
|
when "101" => s1shifter(8 downto 0) <= "000"&s1delta(5)&'0'¬(s1delta(5))&"000";
|
173 |
|
|
when "100" => s1shifter(8 downto 0) <= '0'&x"10";
|
174 |
|
|
when "011" => s1shifter(8 downto 0) <= "000"¬(s1delta(5))&'0'&s1delta(5)&"000";
|
175 |
|
|
when "010" => s1shifter(8 downto 0) <= "00"¬(s1delta(5))&"000"&s1delta(5)&"00";
|
176 |
|
|
when "001" => s1shifter(8 downto 0) <= '0'¬(s1delta(5))&"00000"&s1delta(5)&'0';
|
177 |
|
|
when others => s1shifter(8 downto 0) <= not(s1delta(5))&"0000000"&s1delta(5);
|
178 |
|
|
end case;
|
179 |
|
|
end process;
|
180 |
157 |
jguarin200 |
s1datab <= s1zero&s1umantshift(22 downto 06);
|
181 |
118 |
jguarin200 |
denormhighshiftermult:lpm_mult
|
182 |
155 |
jguarin200 |
generic map (
|
183 |
|
|
lpm_hint => "DEDICATED_MULTIPLIER_CIRCUITRY=YES,MAXIMIZE_SPEED=9",
|
184 |
|
|
lpm_pipeline => 0,
|
185 |
|
|
lpm_representation => "UNSIGNED",
|
186 |
|
|
lpm_type => "LPM_MULT",
|
187 |
|
|
lpm_widtha => 9,
|
188 |
|
|
lpm_widthb => 18,
|
189 |
|
|
lpm_widthp => 27
|
190 |
|
|
)
|
191 |
|
|
port map (
|
192 |
|
|
dataa => s1shifter,
|
193 |
157 |
jguarin200 |
datab => s1datab,
|
194 |
155 |
jguarin200 |
result => s1ph
|
195 |
|
|
);
|
196 |
157 |
jguarin200 |
s1datab_8x <= s1umantshift(5 downto 0)&"000";
|
197 |
118 |
jguarin200 |
denormlowshiftermult:lpm_mult
|
198 |
155 |
jguarin200 |
generic map (
|
199 |
|
|
lpm_hint => "DEDICATED_MULTIPLIER_CIRCUITRY=YES,MAXIMIZE_SPEED=9",
|
200 |
|
|
lpm_pipeline => 0,
|
201 |
|
|
lpm_representation => "UNSIGNED",
|
202 |
|
|
lpm_type => "LPM_MULT",
|
203 |
|
|
lpm_widtha => 9,
|
204 |
|
|
lpm_widthb => 9,
|
205 |
|
|
lpm_widthp => 18
|
206 |
|
|
)
|
207 |
|
|
port map (
|
208 |
|
|
dataa => s1shifter,
|
209 |
157 |
jguarin200 |
datab(8 downto 0) => s1datab_8x,
|
210 |
155 |
jguarin200 |
result => s1pl
|
211 |
|
|
);
|
212 |
118 |
jguarin200 |
|
213 |
|
|
s1postshift(23 downto 7) <= s1ph(25 downto 9);
|
214 |
|
|
s1postshift(06 downto 0) <= s1ph(08 downto 2) or s1pl(17 downto 11);
|
215 |
|
|
s1xorslab(23 downto 0) <= (others => s1umantfixed(23));
|
216 |
|
|
|
217 |
|
|
--! Combinatorial Gremlin, Etapa 2: Signar la mantissa denormalizada.
|
218 |
|
|
s2xorslab <= (others => s2umantshift(24));
|
219 |
|
|
|
220 |
|
|
--! Combinatorial Gremlin, Etapa 4: Quitar el signo de la mantissa resultante.
|
221 |
|
|
s4xorslab <= (others => s4sresult(25));
|
222 |
|
|
|
223 |
119 |
jguarin200 |
|
224 |
118 |
jguarin200 |
|
225 |
|
|
|
226 |
|
|
|
227 |
|
|
|
228 |
|
|
|
229 |
153 |
jguarin200 |
end architecture;
|
230 |
118 |
jguarin200 |
|
231 |
|
|
|