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 |
|
|
|
26 |
|
|
library ieee;
|
27 |
|
|
use ieee.std_logic_1164.all;
|
28 |
|
|
use ieee.std_logic_unsigned.all;
|
29 |
|
|
use ieee.std_logic_arith.all;
|
30 |
119 |
jguarin200 |
library lpm;
|
31 |
|
|
use lpm.all;
|
32 |
118 |
jguarin200 |
|
33 |
|
|
--! 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.
|
34 |
|
|
--!\nLas 2 mantissas y el exponente entran despues a la entidad add2 que suma las mantissas y entrega el resultado en formato IEEE 754.
|
35 |
119 |
jguarin200 |
entity fadd32 is
|
36 |
118 |
jguarin200 |
port (
|
37 |
|
|
clk,dpc : in std_logic;
|
38 |
|
|
a32,b32 : in std_logic_vector (31 downto 0);
|
39 |
|
|
c32 : out std_logic_vector(31 downto 0)
|
40 |
|
|
);
|
41 |
119 |
jguarin200 |
end fadd32;
|
42 |
118 |
jguarin200 |
|
43 |
119 |
jguarin200 |
architecture fadd32_arch of fadd32 is
|
44 |
118 |
jguarin200 |
|
45 |
|
|
component lpm_mult
|
46 |
|
|
generic (
|
47 |
|
|
lpm_hint : string;
|
48 |
|
|
lpm_representation : string;
|
49 |
|
|
lpm_type : string;
|
50 |
|
|
lpm_widtha : natural;
|
51 |
|
|
lpm_widthb : natural;
|
52 |
|
|
lpm_widthp : natural
|
53 |
|
|
);
|
54 |
|
|
port (
|
55 |
|
|
dataa : in std_logic_vector ( lpm_widtha-1 downto 0 );
|
56 |
|
|
datab : in std_logic_vector ( lpm_widthb-1 downto 0 );
|
57 |
|
|
result : out std_logic_vector( lpm_widthp-1 downto 0 )
|
58 |
|
|
);
|
59 |
|
|
end component;
|
60 |
|
|
|
61 |
119 |
jguarin200 |
signal s1zero : std_logic;
|
62 |
|
|
signal s1delta : std_logic_vector(5 downto 0);
|
63 |
|
|
signal s0delta,s1exp,s2exp,s3exp,s4exp,s5exp,s6exp,s5factor,s6factor,s7exp,s7factor : std_logic_vector(7 downto 0);
|
64 |
|
|
signal s1shifter,s5factorhot9,s6factorhot9 : std_logic_vector(8 downto 0);
|
65 |
|
|
signal s1pl,s6pl : std_logic_vector(17 downto 0);
|
66 |
|
|
signal s6postshift,s7postshift : std_logic_vector(22 downto 0);
|
67 |
|
|
signal s1umantshift,s1umantfixed,s1postshift,s1xorslab,s2xorslab : std_logic_vector(23 downto 0);
|
68 |
|
|
signal s2umantshift,s2mantfixed,s3mantfixed,s3mantshift,s4xorslab : std_logic_vector(24 downto 0);
|
69 |
|
|
signal s5factorhot25 : std_logic_vector(24 downto 0);
|
70 |
|
|
signal s4sresult,s5result,s6result,s7result : std_logic_vector(25 downto 0); -- Signed mantissa result
|
71 |
|
|
signal s1ph,s6ph : std_logic_vector(26 downto 0);
|
72 |
|
|
signal s0a,s0b : std_logic_vector(31 downto 0); -- Float 32 bit
|
73 |
118 |
jguarin200 |
|
74 |
|
|
begin
|
75 |
|
|
|
76 |
|
|
process (clk)
|
77 |
|
|
begin
|
78 |
|
|
if clk'event and clk='1' then
|
79 |
|
|
|
80 |
|
|
--!Registro de entrada
|
81 |
|
|
s0a <= a32;
|
82 |
|
|
s0b(31) <= dpc xor b32(31); --! Importante: Integrar el signo en el operando B
|
83 |
|
|
s0b(30 downto 0) <= b32(30 downto 0);
|
84 |
|
|
|
85 |
|
|
--!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.
|
86 |
|
|
--!signo,exponente,mantissa
|
87 |
|
|
if (s0b(30 downto 23)&s0a(30 downto 23))=x"0000" then
|
88 |
|
|
s1zero <= '0';
|
89 |
|
|
else
|
90 |
|
|
s1zero <= '1';
|
91 |
|
|
end if;
|
92 |
119 |
jguarin200 |
s1delta <= s0delta(7) & (s0delta(7) xor s0delta(4))&(s0delta(7) xor s0delta(3)) & s0delta(2 downto 0);
|
93 |
118 |
jguarin200 |
case s0delta(7) is
|
94 |
|
|
when '1' =>
|
95 |
|
|
s1exp <= s0b(30 downto 23);
|
96 |
|
|
s1umantshift <= s0a(31)&s0a(22 downto 0);
|
97 |
|
|
s1umantfixed <= s0b(31)&s0b(22 downto 0);
|
98 |
|
|
when others =>
|
99 |
|
|
s1exp <= s0a(30 downto 23);
|
100 |
|
|
s1umantshift <= s0b(31)&s0b(22 downto 0);
|
101 |
|
|
s1umantfixed <= s0a(31)&s0a(22 downto 0);
|
102 |
|
|
end case;
|
103 |
|
|
|
104 |
|
|
--! Etapa 1: Denormalización de la mantissas.
|
105 |
|
|
case s1delta(4 downto 3) is
|
106 |
|
|
when "00" => s2umantshift <= s1umantshift(23)&s1postshift(23 downto 0);
|
107 |
|
|
when "01" => s2umantshift <= s1umantshift(23)&x"00"&s1postshift(23 downto 8);
|
108 |
|
|
when "10" => s2umantshift <= s1umantshift(23)&x"0000"&s1postshift(23 downto 16);
|
109 |
|
|
when others => s2umantshift <= (others => '0');
|
110 |
|
|
end case;
|
111 |
|
|
s2mantfixed <= s1umantfixed(23) & ( ( ('1'&s1umantfixed(22 downto 0)) xor s1xorslab) + ( x"00000"&"000"&s1umantfixed(23) ) );
|
112 |
|
|
s2exp <= s1exp;
|
113 |
|
|
|
114 |
|
|
--! Etapa2: Signar la mantissa denormalizada.
|
115 |
|
|
s3mantfixed <= s2mantfixed;
|
116 |
|
|
s3mantshift <= s2umantshift(24)& ( ( s2umantshift(23 downto 0) xor s2xorslab) + ( x"00000"&"000"&s2umantshift(24) ) );
|
117 |
|
|
s3exp <= s2exp;
|
118 |
|
|
|
119 |
119 |
jguarin200 |
--! Etapa 3: Etapa 3 Realizar la suma, entre la mantissa corrida y la fija.
|
120 |
118 |
jguarin200 |
s4sresult <= (s3mantshift(24)&s3mantshift)+(s3mantfixed(24)&s3mantfixed);
|
121 |
|
|
s4exp <= s3exp;
|
122 |
|
|
|
123 |
|
|
--! Etapa 4: Quitar el signo a la mantissa resultante.
|
124 |
|
|
s5result <= s4sresult(25)&((s4sresult(24 downto 0) xor s4xorslab) +(x"000000"&s4sresult(25)));
|
125 |
|
|
s5exp <= s4exp;
|
126 |
|
|
|
127 |
|
|
|
128 |
|
|
--! Etapa 5: Codificar el corrimiento para la normalizacion de la mantissa resultante.
|
129 |
119 |
jguarin200 |
s6result <= s5result;
|
130 |
|
|
s6exp <= s5exp;
|
131 |
118 |
jguarin200 |
s6factor <= s5factor;
|
132 |
119 |
jguarin200 |
s6factorhot9 <= s5factorhot9;
|
133 |
118 |
jguarin200 |
|
134 |
119 |
jguarin200 |
--! Etapa 6: Ejecutar el corrimiento de la mantissa.
|
135 |
|
|
s7result <= s6result;
|
136 |
|
|
s7exp <= s6exp;
|
137 |
|
|
s7factor <= s6factor+x"ff";
|
138 |
|
|
s7postshift <= s6postshift;
|
139 |
|
|
|
140 |
|
|
--! Etapa 7: Entregar el resultado.
|
141 |
|
|
c32(31) <= s7result(25);
|
142 |
|
|
c32(30 downto 23) <= s7exp+s7factor;
|
143 |
|
|
case s7factor(4 downto 3) is
|
144 |
|
|
when "01" => c32(22 downto 0) <= s7postshift(14 downto 00)&x"00";
|
145 |
|
|
when "10" => c32(22 downto 0) <= s7postshift(06 downto 00)&x"0000";
|
146 |
|
|
when others => c32(22 downto 0) <= s7postshift;
|
147 |
118 |
jguarin200 |
end case;
|
148 |
|
|
end if;
|
149 |
|
|
end process;
|
150 |
|
|
--! Combinatorial gremlin, Etapa 0 el corrimiento de la mantissa con menor exponente y reorganiza los operandos,\n
|
151 |
|
|
--! si el mayor es b, intercambia las posición si el mayor es a las posiciones la mantiene.
|
152 |
|
|
s0delta <= s0a(30 downto 23)-s0b(30 downto 23);
|
153 |
|
|
--! Combinatorial Gremlin, Etapa 1 Codificar el factor de corrimiento de denormalizacion y denormalizar la mantissa no fija. Signar la mantissa que se queda fija.
|
154 |
|
|
decodeshiftfactor:
|
155 |
|
|
process (s1delta(2 downto 0))
|
156 |
|
|
begin
|
157 |
|
|
case s1delta(2 downto 0) is
|
158 |
|
|
when "111" => s1shifter(8 downto 0) <= '0'&s1delta(5)&"00000"¬(s1delta(5))&'0';
|
159 |
|
|
when "110" => s1shifter(8 downto 0) <= "00"&s1delta(5)&"000"¬(s1delta(5))&"00";
|
160 |
|
|
when "101" => s1shifter(8 downto 0) <= "000"&s1delta(5)&'0'¬(s1delta(5))&"000";
|
161 |
|
|
when "100" => s1shifter(8 downto 0) <= '0'&x"10";
|
162 |
|
|
when "011" => s1shifter(8 downto 0) <= "000"¬(s1delta(5))&'0'&s1delta(5)&"000";
|
163 |
|
|
when "010" => s1shifter(8 downto 0) <= "00"¬(s1delta(5))&"000"&s1delta(5)&"00";
|
164 |
|
|
when "001" => s1shifter(8 downto 0) <= '0'¬(s1delta(5))&"00000"&s1delta(5)&'0';
|
165 |
|
|
when others => s1shifter(8 downto 0) <= not(s1delta(5))&"0000000"&s1delta(5);
|
166 |
|
|
end case;
|
167 |
|
|
end process;
|
168 |
|
|
denormhighshiftermult:lpm_mult
|
169 |
|
|
generic map ("DEDICATED_MULTIPLIER_CIRCUITRY=YES,MAXIMIZE_SPEED=9","UNSIGNED","LPM_MULT",9,18,27)
|
170 |
|
|
port map (s1shifter,s1zero&s1umantshift(22 downto 06),s1ph);
|
171 |
|
|
denormlowshiftermult:lpm_mult
|
172 |
|
|
generic map ("DEDICATED_MULTIPLIER_CIRCUITRY=YES,MAXIMIZE_SPEED=9","UNSIGNED","LPM_MULT",9,9,18)
|
173 |
|
|
port map (s1shifter,s1umantshift(5 downto 0)&"000",s1pl);
|
174 |
|
|
|
175 |
|
|
s1postshift(23 downto 7) <= s1ph(25 downto 9);
|
176 |
|
|
s1postshift(06 downto 0) <= s1ph(08 downto 2) or s1pl(17 downto 11);
|
177 |
|
|
s1xorslab(23 downto 0) <= (others => s1umantfixed(23));
|
178 |
|
|
|
179 |
|
|
--! Combinatorial Gremlin, Etapa 2: Signar la mantissa denormalizada.
|
180 |
|
|
s2xorslab <= (others => s2umantshift(24));
|
181 |
|
|
|
182 |
|
|
--! Combinatorial Gremlin, Etapa 4: Quitar el signo de la mantissa resultante.
|
183 |
|
|
s4xorslab <= (others => s4sresult(25));
|
184 |
|
|
|
185 |
|
|
--! Combinatorial Gremlin, Etapa 5: Codificar el factor de normalizacion de la mantissa resultante.
|
186 |
|
|
normalizerdecodeshift:
|
187 |
|
|
process (s5result,s5factorhot25)
|
188 |
|
|
begin
|
189 |
119 |
jguarin200 |
s5factor <= x"00";
|
190 |
|
|
s5factorhot25 <= '0'&x"000000";
|
191 |
118 |
jguarin200 |
for i in 24 downto 0 loop
|
192 |
|
|
if s5result(i)='1' then
|
193 |
|
|
s5factor <= conv_std_logic_vector(24-i,8);
|
194 |
|
|
s5factorhot25(24-i) <= '1';
|
195 |
|
|
exit;
|
196 |
|
|
end if;
|
197 |
|
|
end loop;
|
198 |
|
|
s5factorhot9 <= (s5factorhot25(8 downto 1)or s5factorhot25(16 downto 9)or s5factorhot25(24 downto 17)) & s5factorhot25(0);
|
199 |
|
|
end process;
|
200 |
119 |
jguarin200 |
|
201 |
|
|
--! Etapa 6: Ejecutar el corrimiento para normalizar la mantissa.
|
202 |
118 |
jguarin200 |
normhighshiftermult:lpm_mult
|
203 |
|
|
generic map ("DEDICATED_MULTIPLIER_CIRCUITRY=YES,MAXIMIZE_SPEED=9","UNSIGNED","LPM_MULT",9,18,27)
|
204 |
119 |
jguarin200 |
port map (s6factorhot9,s6result(24 downto 7),s6ph);
|
205 |
118 |
jguarin200 |
normlowshiftermult:lpm_mult
|
206 |
|
|
generic map ("DEDICATED_MULTIPLIER_CIRCUITRY=YES,MAXIMIZE_SPEED=9","UNSIGNED","LPM_MULT",9,9,18)
|
207 |
119 |
jguarin200 |
port map (s6factorhot9,s6result(06 downto 0)&"00",s6pl);
|
208 |
|
|
s6postshift(22 downto 15) <= s6ph(16 downto 09);
|
209 |
|
|
s6postshift(14 downto 06) <= s6ph(08 downto 00); --! Activar este pedazo si se requiere extrema precision or s5pl(17 downto 9);
|
210 |
|
|
s6postshift(05 downto 00) <= s6pl(08 downto 03);
|
211 |
118 |
jguarin200 |
|
212 |
|
|
|
213 |
|
|
|
214 |
|
|
|
215 |
|
|
|
216 |
119 |
jguarin200 |
end fadd32_arch;
|
217 |
118 |
jguarin200 |
|
218 |
|
|
|