1 |
90 |
jguarin200 |
------------------------------------------------
|
2 |
102 |
jguarin200 |
--! @file ema32x3.vhd
|
3 |
90 |
jguarin200 |
--! @brief RayTrac Exponent Managment Adder
|
4 |
|
|
--! @author Julián Andrés Guarín Reyes
|
5 |
|
|
--------------------------------------------------
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
-- RAYTRAC (FP BRANCH)
|
9 |
|
|
-- Author Julian Andres Guarin
|
10 |
102 |
jguarin200 |
-- ema32x3.vhd
|
11 |
90 |
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 |
84 |
jguarin200 |
library ieee;
|
27 |
|
|
use ieee.std_logic_1164.all;
|
28 |
|
|
use ieee.std_logic_unsigned.all;
|
29 |
|
|
use ieee.std_logic_arith.all;
|
30 |
|
|
|
31 |
|
|
|
32 |
91 |
jguarin200 |
|
33 |
102 |
jguarin200 |
entity ema32x3 is
|
34 |
84 |
jguarin200 |
port (
|
35 |
100 |
jguarin200 |
clk,dpc : in std_logic;
|
36 |
84 |
jguarin200 |
a32,b32,c32 : in std_logic_vector (31 downto 0);
|
37 |
100 |
jguarin200 |
res32 : out std_logic_vector (31 downto 0)
|
38 |
84 |
jguarin200 |
|
39 |
|
|
|
40 |
|
|
|
41 |
|
|
);
|
42 |
102 |
jguarin200 |
end ema32x3;
|
43 |
84 |
jguarin200 |
|
44 |
102 |
jguarin200 |
architecture ema32x3_arch of ema32x3 is
|
45 |
91 |
jguarin200 |
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 |
100 |
jguarin200 |
signal s2slrb,s2slrc : std_logic_vector(1 downto 0);
|
62 |
|
|
signal s3lshift,s4lshift : std_logic_vector(4 downto 0);
|
63 |
|
|
signal s2exp,s3exp,s4exp : std_logic_vector(7 downto 0);
|
64 |
|
|
signal s4slab : std_logic_vector(15 downto 0);
|
65 |
|
|
signal s2slabb,s2slabc : std_logic_vector(16 downto 0);
|
66 |
|
|
signal b1s,c1s,s4nrmP : std_logic_vector(22 downto 0); -- Inversor de la mantissa
|
67 |
|
|
signal s0a,s0b,s0c,s1a,s1b,s1c : std_logic_vector(31 downto 0); -- Float 32 bit
|
68 |
|
|
signal s1sma,s2sma,s2smb,s2smc,s3sma,s3smb,s3smc : std_logic_vector(24 downto 0); -- Signed mantissas
|
69 |
|
|
signal s3res : std_logic_vector(26 downto 0); -- Signed mantissa result
|
70 |
|
|
signal s3ures,s4ures : std_logic_vector(25 downto 0);
|
71 |
|
|
signal s1pSb,s1pHb,s1pLb,s1pSc,s1pHc,s1pLc,s4nrmL,s4nrmH,s4nrmS : std_logic_vector(17 downto 0); -- Shifert Product
|
72 |
|
|
signal s0zeroa,s0zerob,s0zeroc,s1zb,s1zc,s4sgr : std_logic;
|
73 |
84 |
jguarin200 |
begin
|
74 |
|
|
|
75 |
|
|
process (clk)
|
76 |
|
|
begin
|
77 |
|
|
if clk'event and clk='1' then
|
78 |
|
|
|
79 |
|
|
--!Registro de entrada
|
80 |
100 |
jguarin200 |
s0a <= a32;
|
81 |
|
|
s0b(31) <= dpc xor b32(31); --! Importante: Integrar el signo en el operando B
|
82 |
|
|
s0b(30 downto 0) <= b32(30 downto 0);
|
83 |
|
|
s0c <= c32;
|
84 |
84 |
jguarin200 |
|
85 |
100 |
jguarin200 |
--!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 |
|
|
if s0a(30 downto 23) >= s0b (30 downto 23) and s0a(30 downto 23) >=s0c(30 downto 23) then
|
87 |
91 |
jguarin200 |
--!signo,exponente,mantissa de b yc
|
88 |
100 |
jguarin200 |
s1b(31) <= s0b(31);
|
89 |
|
|
s1b(30 downto 23) <= s0a(30 downto 23) - s0b(30 downto 23);
|
90 |
|
|
s1b(22 downto 0) <= s0b(22 downto 0);
|
91 |
|
|
s1zb <= s0zerob;
|
92 |
102 |
jguarin200 |
s1c(31) <= s0c(31);
|
93 |
100 |
jguarin200 |
s1c(30 downto 23) <= s0a(30 downto 23) - s0c(30 downto 23);
|
94 |
|
|
s1c(22 downto 0) <= s0c(22 downto 0);
|
95 |
|
|
s1zc <= s0zeroc;
|
96 |
84 |
jguarin200 |
--!clasifica a
|
97 |
100 |
jguarin200 |
s1a <= s0a;
|
98 |
|
|
elsif s0b(30 downto 23) >= s0c (30 downto 23) then
|
99 |
84 |
jguarin200 |
--!signo,exponente,mantissa
|
100 |
100 |
jguarin200 |
s1b(31) <= s0a(31);
|
101 |
|
|
s1b(30 downto 23) <= s0b(30 downto 23)-s0a(30 downto 23);
|
102 |
|
|
s1b(22 downto 0) <= s0a(22 downto 0);
|
103 |
|
|
s1zb <= s0zeroa;
|
104 |
|
|
s1c(31) <= s0c(31);
|
105 |
|
|
s1c(30 downto 23) <= s0b(30 downto 23) - s0c(30 downto 23);
|
106 |
|
|
s1c(22 downto 0) <= s0c(22 downto 0);
|
107 |
|
|
s1zc <= s0zeroc;
|
108 |
84 |
jguarin200 |
--!clasifica b
|
109 |
100 |
jguarin200 |
s1a <= s0b;
|
110 |
91 |
jguarin200 |
else
|
111 |
84 |
jguarin200 |
--!signo,exponente,mantissa
|
112 |
100 |
jguarin200 |
s1b(31) <= s0b(31);
|
113 |
|
|
s1b(30 downto 23) <= s0c(30 downto 23)-s0b(30 downto 23);
|
114 |
|
|
s1b(22 downto 0) <= s0b(22 downto 0);
|
115 |
|
|
s1zb <= s0zerob;
|
116 |
|
|
s1c(31) <= s0a(31);
|
117 |
|
|
s1c(30 downto 23) <= s0c(30 downto 23) - s0a(30 downto 23);
|
118 |
|
|
s1c(22 downto 0) <= s0a(22 downto 0);
|
119 |
|
|
s1zc <= s0zeroa;
|
120 |
84 |
jguarin200 |
--!clasifica c
|
121 |
100 |
jguarin200 |
s1a <= s0c;
|
122 |
84 |
jguarin200 |
end if;
|
123 |
|
|
|
124 |
|
|
|
125 |
91 |
jguarin200 |
|
126 |
100 |
jguarin200 |
--! Etapa 1: Denormalización de las mantissas.
|
127 |
|
|
|
128 |
|
|
--! A
|
129 |
|
|
s2exp <= s1a(30 downto 23);
|
130 |
|
|
s2sma <= s1sma;
|
131 |
91 |
jguarin200 |
|
132 |
100 |
jguarin200 |
--! B & C
|
133 |
|
|
for i in 23 downto 15 loop
|
134 |
|
|
s2smb(i) <= s1pLb(23-i) xor s1b(31);
|
135 |
|
|
s2smc(i) <= s1pLc(23-i) xor s1c(31);
|
136 |
91 |
jguarin200 |
end loop;
|
137 |
100 |
jguarin200 |
for i in 14 downto 6 loop
|
138 |
|
|
s2smb(i) <= (s1pHc(14-i) or s1pLb(14-i+9)) xor s1b(31);
|
139 |
|
|
s2smc(i) <= (s1pHc(14-i) or s1pLb(14-i+9)) xor s1c(31);
|
140 |
|
|
end loop;
|
141 |
|
|
for i in 5 downto 0 loop
|
142 |
|
|
s2smb(i) <= (s1pSb(5-i) or s1pHb(5-i+9)) xor s1b(31);
|
143 |
|
|
s2smc(i) <= (s1pSc(5-i) or s1pHc(5-i+9)) xor s1c(31);
|
144 |
91 |
jguarin200 |
end loop;
|
145 |
100 |
jguarin200 |
|
146 |
|
|
if s1b(30 downto 28)>"000" then
|
147 |
|
|
s2slrb <= "11";
|
148 |
|
|
else
|
149 |
|
|
s2slrb <= s1b(27 downto 26);
|
150 |
|
|
end if;
|
151 |
|
|
if s1c(30 downto 28)>"000" then
|
152 |
|
|
s2slrc <= "11";
|
153 |
|
|
else
|
154 |
|
|
s2slrc <= s1c(27 downto 26);
|
155 |
|
|
end if;
|
156 |
91 |
jguarin200 |
|
157 |
100 |
jguarin200 |
s2smb(24) <= s1b(31);
|
158 |
|
|
s2smc(24) <= s1c(31);
|
159 |
|
|
|
160 |
|
|
--! Etapa2: Finalizar la denormalización de b y c.
|
161 |
|
|
--! A
|
162 |
|
|
s3sma <= s2sma;
|
163 |
|
|
s3exp <= s2exp;
|
164 |
|
|
|
165 |
|
|
--! B
|
166 |
|
|
case (s2slrb) is
|
167 |
|
|
when "00" =>
|
168 |
|
|
s3smb <= s2smb(24 downto 0)+s2smb(24);
|
169 |
|
|
when "01" =>
|
170 |
|
|
s3smb <= ( s2slabb(8 downto 0) & s2smb(23 downto 8) ) + s2smb(24);
|
171 |
|
|
when "10" =>
|
172 |
|
|
s3smb <= ( s2slabb(16 downto 0) & s2smb(23 downto 16)) + s2smb(24);
|
173 |
|
|
when others =>
|
174 |
|
|
s3smb <= (others => '0');
|
175 |
|
|
end case;
|
176 |
|
|
|
177 |
|
|
--! C
|
178 |
|
|
case (s2slrc) is
|
179 |
|
|
when "00" =>
|
180 |
|
|
s3smc <= s2smc(24 downto 0)+s2smc(24);
|
181 |
|
|
when "01" =>
|
182 |
|
|
s3smc <= ( s2slabc(8 downto 0) & s2smc(23 downto 8) ) + s2smc(24);
|
183 |
|
|
when "10" =>
|
184 |
|
|
s3smc <= ( s2slabc(16 downto 0) & s2smc(23 downto 16)) + s2smc(24);
|
185 |
|
|
when others =>
|
186 |
|
|
s3smc <= (others => '0');
|
187 |
|
|
end case;
|
188 |
|
|
|
189 |
|
|
--! Etapa 3: Etapa 3 Realizar la suma, quitar el signo de la mantissa y codificar el corrimiento hacia la izquierda.
|
190 |
|
|
s4ures <= s3ures+s3res(25); --Resultado no signado
|
191 |
|
|
s4sgr <= s3res(25); --Signo
|
192 |
|
|
s4exp <= s3exp; --Exponente
|
193 |
|
|
s4lshift <= s3lshift; --Corrimiento hacia la izquierda.
|
194 |
|
|
|
195 |
|
|
--! Etapa 4: Corrimiento y normalización de la mantissa resultado.
|
196 |
|
|
res32(31) <= s4sgr;
|
197 |
|
|
if s4ures(25)='1' then
|
198 |
|
|
res32(22 downto 0) <= s4ures(24 downto 2);
|
199 |
|
|
res32(30 downto 23) <= s4exp+2;
|
200 |
|
|
elsif s4ures(24)='1' then
|
201 |
|
|
res32(22 downto 0) <= s4ures(23 downto 1);
|
202 |
|
|
res32(30 downto 23) <= s4exp+1;
|
203 |
|
|
else
|
204 |
|
|
case s4lshift(4 downto 3) is
|
205 |
|
|
when "00" =>
|
206 |
|
|
res32(22 downto 0) <= s4nrmP(22 downto 0);
|
207 |
|
|
res32(30 downto 23) <= s4exp - s4lshift;
|
208 |
|
|
when "01" =>
|
209 |
|
|
res32(22 downto 0) <= s4nrmP(14 downto 0) & s4slab(7 downto 0);
|
210 |
|
|
res32(30 downto 23) <= s4exp - s4lshift;
|
211 |
|
|
when "10" =>
|
212 |
|
|
res32(22 downto 0) <= s4nrmP(6 downto 0) & s4slab(15 downto 0);
|
213 |
|
|
res32(30 downto 23) <= s4exp - s4lshift;
|
214 |
|
|
when others =>
|
215 |
|
|
res32(30 downto 0) <= (others => '0');
|
216 |
|
|
end case;
|
217 |
|
|
|
218 |
|
|
end if;
|
219 |
|
|
|
220 |
84 |
jguarin200 |
end if;
|
221 |
|
|
end process;
|
222 |
91 |
jguarin200 |
|
223 |
100 |
jguarin200 |
--! Combinatorial gremlin, Etapa 0, Escoger el mayor exponente que sera el resultado desnormalizado,\n
|
224 |
|
|
--! calcula cuanto debe ser el corrimiento de la mantissa con menor exponente y reorganiza los operandos,\n
|
225 |
|
|
--! si el mayor es b, intercambia las posición si el mayor es a las posiciones la mantiene. Zero check.\n
|
226 |
102 |
jguarin200 |
process (s0b(30 downto 23),s0a(30 downto 23),s0c(30 downto 23))
|
227 |
91 |
jguarin200 |
begin
|
228 |
100 |
jguarin200 |
s0zerob <='0';
|
229 |
|
|
s0zeroa <='0';
|
230 |
|
|
s0zeroc <='0';
|
231 |
91 |
jguarin200 |
|
232 |
|
|
for i in 30 downto 23 loop
|
233 |
100 |
jguarin200 |
if s0a(i)='1' then
|
234 |
|
|
s0zeroa <= '1';
|
235 |
91 |
jguarin200 |
end if;
|
236 |
100 |
jguarin200 |
if s0b(i)='1' then
|
237 |
|
|
s0zerob <='1';
|
238 |
91 |
jguarin200 |
end if;
|
239 |
100 |
jguarin200 |
if s0c(i)='1' then
|
240 |
|
|
s0zeroc <='1';
|
241 |
91 |
jguarin200 |
end if;
|
242 |
100 |
jguarin200 |
|
243 |
91 |
jguarin200 |
end loop;
|
244 |
100 |
jguarin200 |
end process;
|
245 |
|
|
|
246 |
|
|
--! Combinatorial Gremlin, Etapa 1 Denormalización de las mantissas.
|
247 |
|
|
denormsupershiftermultb:lpm_mult
|
248 |
|
|
generic map ("DEDICATED_MULTIPLIER_CIRCUITRY=YES,MAXIMIZE_SPEED=9","UNSIGNED","LPM_MULT",9,9,18)
|
249 |
|
|
port map ("00"&shl(conv_std_logic_vector(1,7),s1b(25 downto 23)),conv_std_logic_vector(0,3)&b1s(22 downto 17),s1pSb);
|
250 |
|
|
denormhighshiftermultb:lpm_mult
|
251 |
|
|
generic map ("DEDICATED_MULTIPLIER_CIRCUITRY=YES,MAXIMIZE_SPEED=9","UNSIGNED","LPM_MULT",9,9,18)
|
252 |
|
|
port map ("00"&shl(conv_std_logic_vector(1,7),s1b(25 downto 23)),b1s(16 downto 8),s1pHb);
|
253 |
|
|
denormlowshiftermultb:lpm_mult
|
254 |
|
|
generic map ("DEDICATED_MULTIPLIER_CIRCUITRY=YES,MAXIMIZE_SPEED=9","UNSIGNED","LPM_MULT",9,9,18)
|
255 |
|
|
port map ("00"&shl(conv_std_logic_vector(1,7),s1b(25 downto 23)),b1s(7 downto 0)&s1zb,s1pLb);
|
256 |
|
|
denormsupershiftermultc:lpm_mult
|
257 |
|
|
generic map ("DEDICATED_MULTIPLIER_CIRCUITRY=YES,MAXIMIZE_SPEED=9","UNSIGNED","LPM_MULT",9,9,18)
|
258 |
|
|
port map ("00"&shl(conv_std_logic_vector(1,7),s1c(25 downto 23)),conv_std_logic_vector(0,3)&c1s(22 downto 17),s1pSc);
|
259 |
|
|
denormhighshiftermultc:lpm_mult
|
260 |
|
|
generic map ("DEDICATED_MULTIPLIER_CIRCUITRY=YES,MAXIMIZE_SPEED=9","UNSIGNED","LPM_MULT",9,9,18)
|
261 |
|
|
port map ("00"&shl(conv_std_logic_vector(1,7),s1c(25 downto 23)),c1s(16 downto 8),s1pHc);
|
262 |
|
|
denormlowshiftermultc:lpm_mult
|
263 |
|
|
generic map ("DEDICATED_MULTIPLIER_CIRCUITRY=YES,MAXIMIZE_SPEED=9","UNSIGNED","LPM_MULT",9,9,18)
|
264 |
|
|
port map ("00"&shl(conv_std_logic_vector(1,7),s1c(25 downto 23)),c1s(7 downto 0)&s1zc,s1pLc);
|
265 |
|
|
s1b2b1s:
|
266 |
91 |
jguarin200 |
for i in 22 downto 0 generate
|
267 |
100 |
jguarin200 |
b1s(i) <= s1b(22-i);
|
268 |
|
|
end generate s1b2b1s;
|
269 |
|
|
s1c2c1s:
|
270 |
|
|
for i in 22 downto 0 generate
|
271 |
|
|
c1s(i) <= s1c(22-i);
|
272 |
|
|
end generate s1c2c1s;
|
273 |
|
|
signa:
|
274 |
|
|
for i in 22 downto 0 generate
|
275 |
|
|
s1sma(i) <= s1a(31) xor s1a(i);
|
276 |
|
|
end generate;
|
277 |
|
|
s1sma(23) <= not(s1a(31));
|
278 |
|
|
s1sma(24) <= s1a(31);
|
279 |
84 |
jguarin200 |
|
280 |
100 |
jguarin200 |
--! Combinatorial Gremlin, Etapa2: Finalizar la denormalización de b.
|
281 |
|
|
s2signslab:
|
282 |
|
|
for i in 16 downto 0 generate
|
283 |
|
|
s2slabb(i) <= s2smb(24);
|
284 |
|
|
s2slabc(i) <= s2smc(24);
|
285 |
|
|
end generate s2signslab;
|
286 |
|
|
|
287 |
|
|
--! Combinatorial Gremlin, Etapa 3 Realizar la suma, quitar el signo de la mantissa y codificar el corrimiento hacia la izquierda.
|
288 |
|
|
--adder:sadd2
|
289 |
|
|
--port map (s3sma(24)&s3sma,s3smb(24)&s3smb,dpc,s3res);
|
290 |
|
|
process (s3sma,s3smb,s3smc)
|
291 |
|
|
begin
|
292 |
|
|
--! Magia: La suma ocurre aqui
|
293 |
104 |
jguarin200 |
s3res <= (s3sma(24)&s3sma(24)&s3sma)+(s3smb(24)&s3smb(24)&s3smb);
|
294 |
|
|
--! +(s3smc(24)&s3smc(24)&s3smc);
|
295 |
100 |
jguarin200 |
end process;
|
296 |
|
|
|
297 |
|
|
process(s3res)
|
298 |
|
|
variable lshift : integer range 24 downto 0;
|
299 |
|
|
begin
|
300 |
|
|
lshift:=24;
|
301 |
|
|
|
302 |
|
|
for i in 0 to 23 loop
|
303 |
|
|
s3ures(i) <= s3res(26) xor s3res(i);
|
304 |
|
|
if (s3res(26) xor s3res(i))='1' then
|
305 |
|
|
lshift:=23-i;
|
306 |
|
|
end if;
|
307 |
|
|
end loop;
|
308 |
|
|
s3ures(24) <= s3res(24) xor s3res(26);
|
309 |
|
|
s3ures(25) <= s3res(25) xor s3res(26);
|
310 |
|
|
s3lshift <= conv_std_logic_vector(lshift,5);
|
311 |
|
|
end process;
|
312 |
|
|
|
313 |
|
|
--! Combinatorial Gremlin, Etapa 4 corrimientos y normalización de la mantissa resultado.
|
314 |
|
|
normsupershiftermult:lpm_mult
|
315 |
|
|
generic map ("DEDICATED_MULTIPLIER_CIRCUITRY=YES,MAXIMIZE_SPEED=9","UNSIGNED","LPM_MULT",9,9,18)
|
316 |
|
|
port map (shl(conv_std_logic_vector(1,9),s4lshift(2 downto 0)),s4ures(22 downto 14),s4nrmS);
|
317 |
|
|
normhighshiftermult:lpm_mult
|
318 |
|
|
generic map ("DEDICATED_MULTIPLIER_CIRCUITRY=YES,MAXIMIZE_SPEED=9","UNSIGNED","LPM_MULT",9,9,18)
|
319 |
|
|
port map (shl(conv_std_logic_vector(1,9),s4lshift(2 downto 0)),s4ures(13 downto 5),s4nrmH);
|
320 |
|
|
normlowshiftermult:lpm_mult
|
321 |
|
|
generic map ("DEDICATED_MULTIPLIER_CIRCUITRY=YES,MAXIMIZE_SPEED=9","UNSIGNED","LPM_MULT",9,9,18)
|
322 |
|
|
port map (shl(conv_std_logic_vector(1,9),s4lshift(2 downto 0)),s4ures(4 downto 0)&conv_std_logic_vector(0,4),s4nrmL);
|
323 |
|
|
process (s4nrmS,s4nrmH,s4nrmL)
|
324 |
|
|
begin
|
325 |
|
|
s4nrmP(22 downto 14) <= s4nrmS(8 downto 0) or s4nrmH(17 downto 9);
|
326 |
|
|
s4nrmP(13 downto 5) <= s4nrmH(8 downto 0) or s4nrmL(17 downto 9);
|
327 |
|
|
s4nrmP(4 downto 0) <= s4nrmL(8 downto 4);
|
328 |
|
|
end process;
|
329 |
|
|
s4signslab:
|
330 |
|
|
for i in 15 downto 0 generate
|
331 |
|
|
s4slab(i) <= '0';
|
332 |
|
|
end generate s4signslab;
|
333 |
102 |
jguarin200 |
end ema32x3_arch;
|
334 |
84 |
jguarin200 |
|
335 |
100 |
jguarin200 |
|
336 |
|
|
|
337 |
|
|
|
338 |
84 |
jguarin200 |
|