-
Notifications
You must be signed in to change notification settings - Fork 5
/
fpu_exceptions.vhd
522 lines (508 loc) · 15.3 KB
/
fpu_exceptions.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
---------------------------------------------------------------------
---- ----
---- FPU ----
---- Floating Point Unit (Double precision) ----
---- ----
---- Author: David Lundgren ----
---- davidklun@gmail.com ----
---- ----
---------------------------------------------------------------------
---- ----
---- Copyright (C) 2009 David Lundgren ----
---- davidklun@gmail.com ----
---- ----
---- This source file may be used and distributed without ----
---- restriction provided that this copyright statement is not ----
---- removed from the file and that any derivative work contains ----
---- the original copyright notice and the associated disclaimer.----
---- ----
---- THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY ----
---- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED ----
---- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS ----
---- FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR ----
---- OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, ----
---- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ----
---- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ----
---- GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR ----
---- BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ----
---- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ----
---- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT ----
---- OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE ----
---- POSSIBILITY OF SUCH DAMAGE. ----
---- ----
---------------------------------------------------------------------
LIBRARY ieee;
USE ieee.std_logic_1164.all;
USE ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_misc.all;
ENTITY fpu_exceptions IS
PORT(
clk, rst, enable : IN std_logic;
rmode : IN std_logic_vector (1 DOWNTO 0);
opa, opb, in_except : IN std_logic_vector (63 DOWNTO 0);
exponent_in : IN std_logic_vector (11 DOWNTO 0);
mantissa_in : IN std_logic_vector (1 DOWNTO 0);
fpu_op : IN std_logic_vector (2 DOWNTO 0);
out_fp : OUT std_logic_vector (63 DOWNTO 0);
ex_enable, underflow, overflow, inexact : OUT std_logic;
exception, invalid : OUT std_logic
);
END fpu_exceptions;
architecture rtl of fpu_exceptions is
signal in_et_zero : std_logic;
signal opa_et_zero : std_logic;
signal opb_et_zero : std_logic;
signal add : std_logic;
signal subtract : std_logic;
signal multiply : std_logic;
signal divide : std_logic;
signal opa_QNaN : std_logic;
signal opb_QNaN : std_logic;
signal opa_SNaN : std_logic;
signal opb_SNaN : std_logic;
signal opa_pos_inf : std_logic;
signal opb_pos_inf : std_logic;
signal opa_neg_inf : std_logic;
signal opb_neg_inf : std_logic;
signal opa_inf : std_logic;
signal opb_inf : std_logic;
signal NaN_input : std_logic;
signal SNaN_input : std_logic;
signal a_NaN : std_logic;
signal div_by_0 : std_logic;
signal div_0_by_0 : std_logic;
signal div_inf_by_inf : std_logic;
signal div_by_inf : std_logic;
signal mul_0_by_inf : std_logic;
signal mul_inf : std_logic;
signal div_inf : std_logic;
signal add_inf : std_logic;
signal sub_inf : std_logic;
signal addsub_inf_invalid : std_logic;
signal addsub_inf : std_logic;
signal out_inf_trigger : std_logic;
signal out_pos_inf : std_logic;
signal out_neg_inf : std_logic;
signal round_nearest : std_logic;
signal round_to_zero : std_logic;
signal round_to_pos_inf : std_logic;
signal round_to_neg_inf : std_logic;
signal inf_round_down_trigger : std_logic;
signal mul_uf : std_logic;
signal div_uf : std_logic;
signal underflow_trigger : std_logic;
signal invalid_trigger : std_logic;
signal overflow_trigger : std_logic;
signal inexact_trigger : std_logic;
signal except_trigger : std_logic;
signal enable_trigger : std_logic;
signal NaN_out_trigger : std_logic;
signal SNaN_trigger : std_logic;
signal exp_2047 : std_logic_vector(10 downto 0);
signal exp_2046 : std_logic_vector(10 downto 0);
signal NaN_output_0 : std_logic_vector(62 downto 0);
signal NaN_output : std_logic_vector(62 downto 0);
signal mantissa_max : std_logic_vector(51 downto 0);
signal inf_round_down : std_logic_vector(62 downto 0);
signal out_inf : std_logic_vector(62 downto 0);
signal out_0 : std_logic_vector(63 downto 0);
signal out_1 : std_logic_vector(63 downto 0);
signal out_2 : std_logic_vector(63 downto 0);
begin
exp_2047 <= "11111111111";
exp_2046 <= "11111111110";
mantissa_max <= "1111111111111111111111111111111111111111111111111111";
process
begin
wait until clk'event and clk = '1';
if (rst = '1') then
in_et_zero <= '0';
opa_et_zero <= '0';
opb_et_zero <= '0';
add <= '0';
subtract <= '0';
multiply <= '0';
divide <= '0';
opa_QNaN <= '0';
opb_QNaN <= '0';
opa_SNaN <= '0';
opb_SNaN <= '0';
opa_pos_inf <= '0';
opb_pos_inf <= '0';
opa_neg_inf <= '0';
opb_neg_inf <= '0';
opa_inf <= '0';
opb_inf <= '0';
NaN_input <= '0';
SNaN_input <= '0';
a_NaN <= '0';
div_by_0 <= '0';
div_0_by_0 <= '0';
div_inf_by_inf <= '0';
div_by_inf <= '0';
mul_0_by_inf <= '0';
mul_inf <= '0';
div_inf <= '0';
add_inf <= '0';
sub_inf <= '0';
addsub_inf_invalid <= '0';
addsub_inf <= '0';
out_inf_trigger <= '0';
out_pos_inf <= '0';
out_neg_inf <= '0';
round_nearest <= '0';
round_to_zero <= '0';
round_to_pos_inf <= '0';
round_to_neg_inf <= '0';
inf_round_down_trigger <= '0';
mul_uf <= '0';
div_uf <= '0';
underflow_trigger <= '0';
invalid_trigger <= '0';
overflow_trigger <= '0';
inexact_trigger <= '0';
except_trigger <= '0';
enable_trigger <= '0';
NaN_out_trigger <= '0';
SNaN_trigger <= '0';
NaN_output_0 <= (others =>'0');
NaN_output <= (others =>'0');
inf_round_down <= (others =>'0');
out_inf <= (others =>'0');
out_0 <= (others =>'0');
out_1 <= (others =>'0');
out_2 <= (others =>'0');
elsif (enable = '1') then
if or_reduce(in_except(62 downto 0)) = '0' then
in_et_zero <= '1';
else
in_et_zero <= '0';
end if;
if or_reduce(opa(62 downto 0)) = '0' then
opa_et_zero <= '1';
else
opa_et_zero <= '0';
end if;
if or_reduce(opb(62 downto 0)) = '0' then
opb_et_zero <= '1';
else
opb_et_zero <= '0';
end if;
if fpu_op = "000" then
add <= '1';
else
add <= '0';
end if;
if fpu_op = "001" then
subtract <= '1';
else
subtract <= '0';
end if;
if fpu_op = "010" then
multiply <= '1';
else
multiply <= '0';
end if;
if fpu_op = "011" then
divide <= '1';
else
divide <= '0';
end if;
if opa(62 downto 52) = "11111111111" and or_reduce(opa(51 downto 0)) = '1' and
opa(51) = '1' then
opa_QNaN <= '1';
else
opa_QNaN <= '0';
end if;
if opb(62 downto 52) = "11111111111" and or_reduce(opb(51 downto 0)) = '1' and
opb(51) = '1' then
opb_QNaN <= '1';
else
opb_QNaN <= '0';
end if;
if opa(62 downto 52) = "11111111111" and or_reduce(opa(51 downto 0)) = '1' and
opa(51) = '0' then
opa_SNaN <= '1';
else
opa_SNaN <= '0';
end if;
if opb(62 downto 52) = "11111111111" and or_reduce(opb(51 downto 0)) = '1' and
opb(51) = '0' then
opb_SNaN <= '1';
else
opb_SNaN <= '0';
end if;
if opa(62 downto 52) = "11111111111" and or_reduce(opa(51 downto 0)) = '0' and
opa(63) = '0' then
opa_pos_inf <= '1';
else
opa_pos_inf <= '0';
end if;
if opb(62 downto 52) = "11111111111" and or_reduce(opb(51 downto 0)) = '0' and
opb(63) = '0' then
opb_pos_inf <= '1';
else
opb_pos_inf <= '0';
end if;
if opa(62 downto 52) = "11111111111" and or_reduce(opa(51 downto 0)) = '0' and
opa(63) = '1' then
opa_neg_inf <= '1';
else
opa_neg_inf <= '0';
end if;
if opb(62 downto 52) = "11111111111" and or_reduce(opb(51 downto 0)) = '0' and
opb(63) = '1' then
opb_neg_inf <= '1';
else
opb_neg_inf <= '0';
end if;
if opa(62 downto 52) = "11111111111" and or_reduce(opa(51 downto 0)) = '0' then
opa_inf <= '1';
else
opa_inf <= '0';
end if;
if opb(62 downto 52) = "11111111111" and or_reduce(opb(51 downto 0)) = '0' then
opb_inf <= '1';
else
opb_inf <= '0';
end if;
if opa_QNaN = '1' or opb_QNaN = '1' or opa_SNaN = '1' or opb_SNaN = '1' then
NaN_input <= '1';
else
NaN_input <= '0';
end if;
if opa_SNaN = '1' or opb_SNaN = '1' then
SNaN_input <= '1';
else
SNaN_input <= '0';
end if;
if opa_SNaN = '1' or opa_QNaN = '1' then
a_NaN <= '1';
else
a_NaN <= '0';
end if;
if divide = '1' and opb_et_zero = '1' and opa_et_zero = '0' then
div_by_0 <= '1';
else
div_by_0 <= '0';
end if;
if divide = '1' and opb_et_zero = '1' and opa_et_zero = '1' then
div_0_by_0 <= '1';
else
div_0_by_0 <= '0';
end if;
if divide = '1' and opa_inf = '1' and opb_inf = '1' then
div_inf_by_inf <= '1';
else
div_inf_by_inf <= '0';
end if;
if divide = '1' and opa_inf = '0' and opb_inf = '1' then
div_by_inf <= '1';
else
div_by_inf <= '0';
end if;
if multiply = '1' and ((opa_inf = '1' and opb_et_zero = '1') or
(opa_et_zero = '1' and opb_inf = '1')) then
mul_0_by_inf <= '1';
else
mul_0_by_inf <= '0';
end if;
if multiply = '1' and (opa_inf = '1' or opb_inf = '1') and
mul_0_by_inf = '0' then
mul_inf <= '1';
else
mul_inf <= '0';
end if;
if divide = '1' and opa_inf = '1' and opb_inf = '0' then
div_inf <= '1';
else
div_inf <= '0';
end if;
if add = '1' and (opa_inf = '1' or opb_inf = '1') then
add_inf <= '1';
else
add_inf <= '0';
end if;
if subtract = '1' and (opa_inf = '1' or opb_inf = '1') then
sub_inf <= '1';
else
sub_inf <= '0';
end if;
if (add = '1' and opa_pos_inf = '1' and opb_neg_inf = '1') or
(add = '1' and opa_neg_inf = '1' and opb_pos_inf = '1') or
(subtract = '1' and opa_pos_inf = '1' and opb_pos_inf = '1') or
(subtract = '1' and opa_neg_inf = '1' and opb_neg_inf = '1') then
addsub_inf_invalid <= '1';
else
addsub_inf_invalid <= '0';
end if;
if (add_inf = '1' or sub_inf = '1') and addsub_inf_invalid = '0' then
addsub_inf <= '1';
else
addsub_inf <= '0';
end if;
if addsub_inf = '1' or mul_inf = '1' or div_inf = '1' or div_by_0 = '1'
or (exponent_in > "011111111110") then -- 2046
out_inf_trigger <= '1';
else
out_inf_trigger <= '0';
end if;
if out_inf_trigger = '1' and in_except(63) = '0' then
out_pos_inf <= '1';
else
out_pos_inf <= '0';
end if;
if out_inf_trigger = '1' and in_except(63) = '1' then
out_neg_inf <= '1';
else
out_neg_inf <= '0';
end if;
if rmode = "00" then
round_nearest <= '1';
else
round_nearest <= '0';
end if;
if rmode = "01" then
round_to_zero <= '1';
else
round_to_zero <= '0';
end if;
if rmode = "10" then
round_to_pos_inf <= '1';
else
round_to_pos_inf <= '0';
end if;
if rmode = "11" then
round_to_neg_inf <= '1';
else
round_to_neg_inf <= '0';
end if;
if (out_pos_inf = '1' and round_to_neg_inf = '1') or
(out_neg_inf = '1' and round_to_pos_inf = '1') or
(out_inf_trigger = '1' and round_to_zero = '1') then
inf_round_down_trigger <= '1';
else
inf_round_down_trigger <= '0';
end if;
if multiply = '1' and opa_et_zero = '0' and opb_et_zero = '0' and
in_et_zero = '1' then
mul_uf <= '1';
else
mul_uf <= '0';
end if;
if divide = '1' and opa_et_zero = '0' and in_et_zero = '1' then
div_uf <= '1';
else
div_uf <= '0';
end if;
if div_by_inf = '1' or mul_uf = '1' or div_uf = '1' then
underflow_trigger <= '1';
else
underflow_trigger <= '0';
end if;
if SNaN_input = '1' or addsub_inf_invalid = '1' or mul_0_by_inf = '1' or
div_0_by_0 = '1' or div_inf_by_inf = '1' then
invalid_trigger <= '1';
else
invalid_trigger <= '0';
end if;
if div_by_inf = '1' or mul_uf = '1' or div_uf = '1' then
underflow_trigger <= '1';
else
underflow_trigger <= '0';
end if;
if out_inf_trigger = '1' and NaN_input = '0' then
overflow_trigger <= '1';
else
overflow_trigger <= '0';
end if;
if (or_reduce(mantissa_in(1 downto 0)) = '1' or out_inf_trigger = '1' or
underflow_trigger = '1') and NaN_input = '0' then
inexact_trigger <= '1';
else
inexact_trigger <= '0';
end if;
if (invalid_trigger = '1' or overflow_trigger = '1' or
underflow_trigger = '1' or inexact_trigger = '1') then
except_trigger <= '1';
else
except_trigger <= '0';
end if;
if (invalid_trigger = '1' or overflow_trigger = '1' or
underflow_trigger = '1' or inexact_trigger = '1') then
except_trigger <= '1';
else
except_trigger <= '0';
end if;
if (except_trigger = '1' or out_inf_trigger = '1' or
NaN_input = '1') then
enable_trigger <= '1';
else
enable_trigger <= '0';
end if;
if (NaN_input = '1' or invalid_trigger = '1') then
NaN_out_trigger <= '1';
else
NaN_out_trigger <= '0';
end if;
if (invalid_trigger = '1' and SNaN_input = '0') then
SNaN_trigger <= '1';
else
SNaN_trigger <= '0';
end if;
if a_NaN = '1' then
NaN_output_0 <= exp_2047 & '1' & opa(50 downto 0);
else
NaN_output_0 <= exp_2047 & '1' & opb(50 downto 0);
end if;
if SNaN_trigger = '1' then
NaN_output <= exp_2047 & "01" & opa(49 downto 0);
else
NaN_output <= NaN_output_0;
end if;
inf_round_down <= exp_2046 & mantissa_max;
if inf_round_down_trigger = '1' then
out_inf <= inf_round_down;
else
out_inf <= exp_2047 & "0000000000000000000000000000000000000000000000000000";
end if;
if underflow_trigger = '1' then
out_0 <= in_except(63) & "000000000000000000000000000000000000000000000000000000000000000";
else
out_0 <= in_except;
end if;
if out_inf_trigger = '1' then
out_1 <= in_except(63) & out_inf;
else
out_1 <= out_0;
end if;
if NaN_out_trigger = '1' then
out_2 <= in_except(63) & NaN_output;
else
out_2 <= out_1;
end if;
end if;
end process;
process
begin
wait until clk'event and clk = '1';
if (rst = '1') then
ex_enable <= '0';
underflow <= '0';
overflow <= '0';
inexact <= '0';
exception <= '0';
invalid <= '0';
out_fp <= (others =>'0');
elsif (enable = '1') then
ex_enable <= enable_trigger;
underflow <= underflow_trigger;
overflow <= overflow_trigger;
inexact <= inexact_trigger;
exception <= except_trigger;
invalid <= invalid_trigger;
out_fp <= out_2;
end if;
end process;
end rtl;