1 |
8 |
constantin |
`timescale 1ns / 1ps
|
2 |
|
|
//////////////////////////////////////////////////////////////////////////////////
|
3 |
|
|
// Company:
|
4 |
|
|
// Engineer:
|
5 |
|
|
//
|
6 |
|
|
// Create Date: 16:09:49 11/04/2013
|
7 |
|
|
// Design Name:
|
8 |
|
|
// Module Name: SinglePathFPAdder
|
9 |
|
|
// Project Name:
|
10 |
|
|
// Target Devices:
|
11 |
|
|
// Tool versions:
|
12 |
|
|
// Description: A ± B
|
13 |
|
|
//
|
14 |
|
|
// Dependencies:
|
15 |
|
|
//
|
16 |
|
|
// Revision:
|
17 |
|
|
// Revision 0.01 - File Created
|
18 |
|
|
// Additional Comments:
|
19 |
|
|
//
|
20 |
|
|
//////////////////////////////////////////////////////////////////////////////////
|
21 |
9 |
constantin |
module SinglePathFPAdder #( parameter size_mantissa = 24, //calculate the size containing the hiden bit 1.M
|
22 |
8 |
constantin |
parameter size_exponent = 8,
|
23 |
|
|
parameter size_exception_field = 2,
|
24 |
|
|
parameter size_counter = 5, //log2(size_mantissa) + 1 = 5)
|
25 |
|
|
parameter [size_exception_field - 1 : 0] zero = 0, //00
|
26 |
|
|
parameter [size_exception_field - 1 : 0] normal_number= 1, //01
|
27 |
|
|
parameter [size_exception_field - 1 : 0] infinity = 2, //10
|
28 |
|
|
parameter [size_exception_field - 1 : 0] NaN = 3, //11
|
29 |
|
|
parameter pipeline = 0,
|
30 |
|
|
parameter pipeline_pos = 0, // 8 bits
|
31 |
|
|
parameter double_size_mantissa = size_mantissa + size_mantissa,
|
32 |
|
|
parameter double_size_counter = size_counter + 1,
|
33 |
|
|
parameter size = size_mantissa + size_exponent + size_exception_field)
|
34 |
|
|
|
35 |
9 |
constantin |
( input sub,
|
36 |
|
|
input [size - 1 : 0] a_number_i,
|
37 |
|
|
input [size - 1 : 0] b_number_i,
|
38 |
|
|
output[size - 1 : 0] resulted_number_o);
|
39 |
8 |
constantin |
|
40 |
9 |
constantin |
|
41 |
|
|
wire [size_exception_field - 1 : 0] sp_case_a_number, sp_case_b_number;
|
42 |
8 |
constantin |
wire [size_mantissa - 1 : 0] m_a_number, m_b_number;
|
43 |
|
|
wire [size_exponent - 1 : 0] e_a_number, e_b_number;
|
44 |
|
|
wire s_a_number, s_b_number;
|
45 |
|
|
|
46 |
9 |
constantin |
wire [size_exponent : 0] a_greater_exponent, b_greater_exponent;
|
47 |
|
|
wire [size_exponent - 1 : 0] unadjusted_exponent;
|
48 |
|
|
|
49 |
8 |
constantin |
wire [size_exponent - 1 : 0] exp_difference;
|
50 |
9 |
constantin |
wire [size_exponent : 0] exp_inter;
|
51 |
|
|
wire [size_mantissa - 1 : 0] shifted_m_b;
|
52 |
|
|
wire [size_mantissa - 1 : 0] initial_rounding_bits, inter_rounding_bits, final_rounding_bits;
|
53 |
8 |
constantin |
wire eff_op;
|
54 |
|
|
|
55 |
9 |
constantin |
wire [size_counter - 1 : 0] lzs;
|
56 |
|
|
wire [size_mantissa + 1 : 0] adder_mantissa;
|
57 |
|
|
wire [size_mantissa + 1 : 0] rounded_mantissa;
|
58 |
|
|
wire [size_mantissa : 0] unnormalized_mantissa, unrounded_mantissa;
|
59 |
8 |
constantin |
|
60 |
9 |
constantin |
wire [size_exception_field - 1 : 0] resulted_exception_field;
|
61 |
|
|
wire [size_mantissa - 1 : 0] resulted_mantissa;
|
62 |
|
|
wire [size_exponent - 1 : 0] resulted_exponent;
|
63 |
8 |
constantin |
wire resulted_sign;
|
64 |
|
|
|
65 |
9 |
constantin |
wire dummy_bit;
|
66 |
|
|
wire zero_flag;
|
67 |
8 |
constantin |
|
68 |
9 |
constantin |
|
69 |
8 |
constantin |
assign e_a_number = a_number_i[size_mantissa + size_exponent - 1 : size_mantissa - 1];
|
70 |
|
|
assign e_b_number = b_number_i[size_mantissa + size_exponent - 1 : size_mantissa - 1];
|
71 |
|
|
assign s_a_number = a_number_i[size - size_exception_field - 1];
|
72 |
|
|
assign s_b_number = b_number_i[size - size_exception_field - 1];
|
73 |
|
|
assign sp_case_a_number = a_number_i[size - 1 : size - size_exception_field];
|
74 |
|
|
assign sp_case_b_number = b_number_i[size - 1 : size - size_exception_field];
|
75 |
9 |
constantin |
|
76 |
|
|
|
77 |
|
|
//find the greater exponent
|
78 |
|
|
assign a_greater_exponent = e_a_number - e_b_number;
|
79 |
|
|
assign b_greater_exponent = e_b_number - e_a_number;
|
80 |
|
|
|
81 |
8 |
constantin |
//find the difference between exponents
|
82 |
9 |
constantin |
assign exp_difference = (a_greater_exponent[size_exponent])? b_greater_exponent[size_exponent - 1 : 0] : a_greater_exponent[size_exponent - 1 : 0];
|
83 |
|
|
assign exp_inter = (b_greater_exponent[size_exponent])? {1'b0, e_a_number} : {1'b0, e_b_number};
|
84 |
|
|
|
85 |
|
|
//set shifter always on m_b_number
|
86 |
|
|
assign {m_a_number, m_b_number} = (b_greater_exponent[size_exponent])?
|
87 |
|
|
{{1'b1, a_number_i[size_mantissa - 2 :0]}, {1'b1, b_number_i[size_mantissa - 2 :0]}} :
|
88 |
|
|
{{1'b1, b_number_i[size_mantissa - 2 :0]}, {1'b1, a_number_i[size_mantissa - 2 :0]}};
|
89 |
|
|
|
90 |
|
|
//shift m_b_number
|
91 |
8 |
constantin |
shifter #( .INPUT_SIZE(size_mantissa),
|
92 |
9 |
constantin |
.SHIFT_SIZE(size_exponent),
|
93 |
|
|
.OUTPUT_SIZE(double_size_mantissa),
|
94 |
|
|
.DIRECTION(1'b0), //0=right, 1=left
|
95 |
|
|
.PIPELINE(pipeline),
|
96 |
|
|
.POSITION(pipeline_pos))
|
97 |
8 |
constantin |
m_b_shifter_instance( .a(m_b_number),//mantissa
|
98 |
9 |
constantin |
.arith(1'b0),//logical shift
|
99 |
|
|
.shft(exp_difference),
|
100 |
|
|
.shifted_a({shifted_m_b, initial_rounding_bits}));
|
101 |
8 |
constantin |
|
102 |
|
|
//istantiate effective_operation_component
|
103 |
|
|
effective_op effective_op_instance( .a_sign(s_a_number), .b_sign(s_b_number), .sub(sub), .eff_op(eff_op));
|
104 |
9 |
constantin |
|
105 |
|
|
//compute unnormalized_mantissa
|
106 |
|
|
assign adder_mantissa = (eff_op)? ({1'b0, m_a_number} - {1'b0, shifted_m_b}) : ({1'b0, m_a_number} + {1'b0, shifted_m_b});
|
107 |
8 |
constantin |
|
108 |
9 |
constantin |
assign {unnormalized_mantissa, inter_rounding_bits} =
|
109 |
|
|
(adder_mantissa[size_mantissa + 1])? ({~adder_mantissa[size_mantissa : 0], ~initial_rounding_bits}) :
|
110 |
|
|
({adder_mantissa[size_mantissa : 0], initial_rounding_bits});
|
111 |
|
|
|
112 |
8 |
constantin |
//compute leading_zeros over unnormalized mantissa
|
113 |
9 |
constantin |
leading_zeros #( .SIZE_INT(size_mantissa + 1), .SIZE_COUNTER(size_counter), .PIPELINE(pipeline))
|
114 |
|
|
leading_zeros_instance (.a(unnormalized_mantissa[size_mantissa : 0]),
|
115 |
|
|
.ovf(unnormalized_mantissa[size_mantissa]),
|
116 |
8 |
constantin |
.lz(lzs));
|
117 |
9 |
constantin |
|
118 |
8 |
constantin |
//compute shifting over unnormalized_mantissa
|
119 |
9 |
constantin |
shifter #( .INPUT_SIZE(double_size_mantissa + 1),
|
120 |
|
|
.SHIFT_SIZE(size_counter),
|
121 |
|
|
.OUTPUT_SIZE(double_size_mantissa + 2),
|
122 |
8 |
constantin |
.DIRECTION(1'b1), //0=right, 1=left
|
123 |
|
|
.PIPELINE(pipeline),
|
124 |
|
|
.POSITION(pipeline_pos))
|
125 |
9 |
constantin |
shifter_instance( .a({unnormalized_mantissa, inter_rounding_bits}),//mantissa
|
126 |
8 |
constantin |
.arith(1'b0),//logical shift
|
127 |
|
|
.shft(lzs),
|
128 |
9 |
constantin |
.shifted_a({unrounded_mantissa, final_rounding_bits, dummy_bit}));
|
129 |
|
|
|
130 |
|
|
//instantiate rounding_component
|
131 |
|
|
rounding #( .SIZE_MOST_S_MANTISSA(size_mantissa + 2),
|
132 |
|
|
.SIZE_LEAST_S_MANTISSA(size_mantissa))
|
133 |
|
|
rounding_instance( .unrounded_mantissa({1'b0, unrounded_mantissa}),
|
134 |
|
|
.dummy_bits(final_rounding_bits),
|
135 |
|
|
.rounded_mantissa(rounded_mantissa));
|
136 |
8 |
constantin |
|
137 |
9 |
constantin |
//adjust exponent in case of overflow
|
138 |
|
|
assign adjust_exponent = (rounded_mantissa[size_mantissa + 1])? 2'd2 : 2'd1;
|
139 |
8 |
constantin |
|
140 |
|
|
//compute resulted_exponent
|
141 |
9 |
constantin |
assign unadjusted_exponent = exp_inter - lzs;
|
142 |
|
|
assign resulted_exponent = unadjusted_exponent + adjust_exponent;
|
143 |
8 |
constantin |
|
144 |
9 |
constantin |
assign resulted_mantissa = (rounded_mantissa[size_mantissa + 1])? (rounded_mantissa[size_mantissa + 1 : 2]) : (rounded_mantissa[size_mantissa : 1]);
|
145 |
8 |
constantin |
|
146 |
|
|
//compute exception_field
|
147 |
|
|
special_cases #( .size_exception_field(size_exception_field),
|
148 |
|
|
.zero(zero),
|
149 |
|
|
.normal_number(normal_number),
|
150 |
|
|
.infinity(infinity),
|
151 |
|
|
.NaN(NaN))
|
152 |
|
|
special_cases_instance( .sp_case_a_number(sp_case_a_number),
|
153 |
|
|
.sp_case_b_number(sp_case_b_number),
|
154 |
|
|
.sp_case_result_o(resulted_exception_field));
|
155 |
|
|
|
156 |
9 |
constantin |
//set zero_flag in case of equal numbers
|
157 |
|
|
assign zero_flag = ~(|(resulted_mantissa));
|
158 |
|
|
|
159 |
|
|
//compute resulted_sign
|
160 |
|
|
assign resulted_sign = (eff_op)?
|
161 |
|
|
(!a_greater_exponent[size_exponent]? (!b_greater_exponent[size_exponent]? ~adder_mantissa[size_mantissa+1] : s_a_number) : ~s_b_number) :
|
162 |
|
|
s_a_number;
|
163 |
|
|
|
164 |
|
|
assign resulted_number_o = (zero_flag)? {size{1'b0}} :
|
165 |
|
|
{resulted_exception_field, resulted_sign, resulted_exponent, resulted_mantissa[size_mantissa - 2 : 0]};
|
166 |
|
|
|
167 |
8 |
constantin |
endmodule
|