1 |
15 |
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: SinglePathAdderConversion
|
9 |
|
|
// Project Name:
|
10 |
|
|
// Target Devices:
|
11 |
|
|
// Tool versions:
|
12 |
|
|
// Description: A ± B with mapped conversions
|
13 |
|
|
//
|
14 |
|
|
// Dependencies:
|
15 |
|
|
//
|
16 |
|
|
// Revision:
|
17 |
|
|
// Revision 0.01 - File Created
|
18 |
|
|
// Additional Comments:
|
19 |
|
|
//
|
20 |
|
|
//////////////////////////////////////////////////////////////////////////////////
|
21 |
|
|
module SinglePathAdderConversion #( parameter size_mantissa = 24, //calculate the size containing the hiden bit 1.M
|
22 |
|
|
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 size_integer = 32,
|
30 |
|
|
parameter counter_integer = 6,//log2(size_integer) + 1 = 6)
|
31 |
|
|
parameter [1 : 0] FP_operation = 0, //00
|
32 |
|
|
parameter [1 : 0] FP_to_int = 1, //01
|
33 |
|
|
parameter [1 : 0] int_to_FP = 2, //10
|
34 |
|
|
parameter pipeline = 0,
|
35 |
|
|
parameter pipeline_pos = 0, // 8 bits
|
36 |
|
|
parameter size = size_mantissa + size_exponent + size_exception_field
|
37 |
|
|
)
|
38 |
|
|
( input [1:0] conversion,
|
39 |
|
|
input sub,
|
40 |
|
|
input [size - 1 : 0] a_number_i,
|
41 |
|
|
input [size - 1 : 0] b_number_i,
|
42 |
|
|
output[size - 1 : 0] resulted_number_o);
|
43 |
|
|
|
44 |
|
|
parameter double_size_mantissa = size_mantissa + size_mantissa;
|
45 |
|
|
parameter double_size_counter = size_counter + 1;
|
46 |
|
|
parameter max_size = (size_integer > size_mantissa)? size_integer : size_mantissa;
|
47 |
|
|
parameter max_counter = (counter_integer > size_counter)? counter_integer : size_counter;
|
48 |
|
|
parameter size_diff_i_m = (size_integer > size_mantissa)? (size_integer - size_mantissa) : (size_mantissa - size_integer);
|
49 |
|
|
parameter bias = {1'b0,{(size_exponent-1){1'b1}}};
|
50 |
|
|
parameter exp_biased = bias + size_mantissa;
|
51 |
|
|
parameter exponent = exp_biased - 1'b1;
|
52 |
|
|
parameter subtr = max_size -2'd2;
|
53 |
|
|
|
54 |
|
|
|
55 |
|
|
wire [size_exception_field - 1 : 0] sp_case_a_number, sp_case_b_number;
|
56 |
|
|
wire [size_mantissa - 1 : 0] m_a_number, m_b_number;
|
57 |
|
|
wire [size_exponent - 1 : 0] e_a_number, e_b_number;
|
58 |
|
|
wire s_a_number, s_b_number;
|
59 |
|
|
|
60 |
|
|
wire [size_exponent : 0] a_greater_exponent, b_greater_exponent;
|
61 |
|
|
|
62 |
|
|
wire [size_exponent - 1 : 0] exp_difference;
|
63 |
|
|
wire [size_exponent : 0] exp_inter;
|
64 |
|
|
wire [size_mantissa - 1 : 0] shifted_m_b, convert_neg_mantissa, mantissa_to_shift;
|
65 |
|
|
|
66 |
|
|
wire [size_mantissa - 1 : 0] initial_rounding_bits, inter_rounding_bits;
|
67 |
|
|
wire eff_op;
|
68 |
|
|
|
69 |
|
|
wire [size_mantissa + 1 : 0] adder_mantissa;
|
70 |
|
|
wire [size_mantissa : 0] unnormalized_mantissa;
|
71 |
|
|
|
72 |
|
|
wire [size_exception_field - 1 : 0] sp_case_o, resulted_exception_field;
|
73 |
|
|
wire [size_mantissa - 1 : 0] resulted_mantissa;
|
74 |
|
|
wire [size_exponent - 1 : 0] resulted_exponent;
|
75 |
|
|
wire resulted_sign;
|
76 |
|
|
|
77 |
|
|
wire zero_flag;
|
78 |
|
|
|
79 |
|
|
wire [size_exponent : 0] subtracter;
|
80 |
|
|
|
81 |
|
|
wire [max_size - size_mantissa : 0] dummy_bits;
|
82 |
|
|
wire [size_exponent : 0] shift_value_when_positive_exponent, shift_value_when_negative_exponent;
|
83 |
|
|
wire [size_exponent - 1 : 0] shift_value, shft_val;
|
84 |
|
|
wire lsb_shft_bit;
|
85 |
|
|
|
86 |
|
|
wire [size_exponent - 1 : 0] max_resulted_e_o;
|
87 |
|
|
wire [size_exponent - 1 : 0] max_unadjusted_exponent, max_adjust_exponent;
|
88 |
|
|
wire [size_exponent - 1 : 0] max_exp_selection;
|
89 |
|
|
wire [size_mantissa - 1 : 0] r_mantissa;
|
90 |
|
|
wire [size_mantissa : 0] max_rounded_mantissa;
|
91 |
|
|
wire [max_counter - 1 : 0] max_lzs;
|
92 |
|
|
wire [max_size - 1 : 0] max_entityINT_FP, max_entityFP_INT;
|
93 |
|
|
wire arith_shift;
|
94 |
|
|
wire max_ovf;
|
95 |
|
|
|
96 |
|
|
wire do_conversion;
|
97 |
|
|
|
98 |
|
|
assign do_conversion = |conversion; //let me know if there is a conversion
|
99 |
|
|
|
100 |
|
|
assign e_a_number = a_number_i[size_mantissa + size_exponent - 1 : size_mantissa - 1];
|
101 |
|
|
assign e_b_number = b_number_i[size_mantissa + size_exponent - 1 : size_mantissa - 1];
|
102 |
|
|
assign s_a_number = a_number_i[size - size_exception_field - 1];
|
103 |
|
|
assign s_b_number = b_number_i[size - size_exception_field - 1];
|
104 |
|
|
assign sp_case_a_number = a_number_i[size - 1 : size - size_exception_field];
|
105 |
|
|
assign sp_case_b_number = b_number_i[size - 1 : size - size_exception_field];
|
106 |
|
|
|
107 |
|
|
|
108 |
|
|
//find the greater exponent
|
109 |
|
|
assign a_greater_exponent = e_a_number - e_b_number;
|
110 |
|
|
assign b_greater_exponent = e_b_number - e_a_number;
|
111 |
|
|
|
112 |
|
|
//find the difference between exponents
|
113 |
|
|
assign exp_difference = (a_greater_exponent[size_exponent])? b_greater_exponent[size_exponent - 1 : 0] : a_greater_exponent[size_exponent - 1 : 0];
|
114 |
|
|
assign exp_inter = (b_greater_exponent[size_exponent])? {1'b0, e_a_number} : {1'b0, e_b_number};
|
115 |
|
|
|
116 |
|
|
//set shifter always on m_b_number
|
117 |
|
|
assign {m_a_number, m_b_number} = (b_greater_exponent[size_exponent])?
|
118 |
|
|
{{1'b1, a_number_i[size_mantissa - 2 :0]}, {1'b1, b_number_i[size_mantissa - 2 :0]}} :
|
119 |
|
|
{{1'b1, b_number_i[size_mantissa - 2 :0]}, {1'b1, a_number_i[size_mantissa - 2 :0]}};
|
120 |
|
|
|
121 |
|
|
assign subtracter = e_a_number - bias;
|
122 |
|
|
assign shift_value_when_positive_exponent = subtr - subtracter[size_exponent-1 : 0];
|
123 |
|
|
assign shift_value_when_negative_exponent = max_size + (~subtracter[size_exponent-1 : 0]);
|
124 |
|
|
assign shift_value = (subtracter[size_exponent])? shift_value_when_negative_exponent[size_exponent - 1 : 0] :
|
125 |
|
|
(shift_value_when_positive_exponent[size_exponent])? (~shift_value_when_positive_exponent[size_exponent - 1 : 0]):
|
126 |
|
|
shift_value_when_positive_exponent[size_exponent - 1 : 0];
|
127 |
|
|
assign shft_val = do_conversion? shift_value : exp_difference;
|
128 |
|
|
|
129 |
|
|
assign convert_neg_mantissa = {1'b0, ~a_number_i[size_mantissa-2 : 0]};
|
130 |
|
|
|
131 |
|
|
assign mantissa_to_shift = conversion[0]? (s_a_number? convert_neg_mantissa + 1'b1 : {1'b1, a_number_i[size_mantissa-2 : 0]}) : m_b_number;
|
132 |
|
|
assign arith_shift = conversion[0]? s_a_number : 1'b0;
|
133 |
|
|
|
134 |
|
|
//shift m_b_number
|
135 |
|
|
shifter #( .INPUT_SIZE(size_mantissa),
|
136 |
|
|
.SHIFT_SIZE(size_exponent),
|
137 |
|
|
.OUTPUT_SIZE(double_size_mantissa),
|
138 |
|
|
.DIRECTION(1'b0), //0=right, 1=left
|
139 |
|
|
.PIPELINE(pipeline),
|
140 |
|
|
.POSITION(pipeline_pos))
|
141 |
|
|
m_b_shifter_instance( .a(mantissa_to_shift),//mantissa
|
142 |
|
|
.arith(arith_shift),//logical shift
|
143 |
|
|
.shft(shft_val),
|
144 |
|
|
.shifted_a({shifted_m_b, initial_rounding_bits}));
|
145 |
|
|
|
146 |
|
|
assign max_entityFP_INT = {s_a_number, shifted_m_b[size_mantissa-1 : 0], initial_rounding_bits[size_mantissa-1 : size_mantissa - size_diff_i_m + 1]};
|
147 |
|
|
|
148 |
|
|
//istantiate effective_operation_component
|
149 |
|
|
effective_op effective_op_instance( .a_sign(s_a_number), .b_sign(s_b_number), .sub(sub), .eff_op(eff_op));
|
150 |
|
|
|
151 |
|
|
//compute unnormalized_mantissa
|
152 |
|
|
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});
|
153 |
|
|
|
154 |
|
|
assign {unnormalized_mantissa, inter_rounding_bits} =
|
155 |
|
|
(adder_mantissa[size_mantissa + 1])? ({~adder_mantissa[size_mantissa : 0], ~initial_rounding_bits}) :
|
156 |
|
|
({adder_mantissa[size_mantissa : 0], initial_rounding_bits});
|
157 |
|
|
|
158 |
|
|
assign max_entityINT_FP = do_conversion? (s_a_number? (~a_number_i[max_size-1 : 0]) : a_number_i[max_size-1 : 0]) :
|
159 |
|
|
{{(max_size-size_mantissa-1){1'b0}}, unnormalized_mantissa[size_mantissa : 0]};
|
160 |
|
|
assign lsb_shft_bit = (do_conversion)? s_a_number : max_entityINT_FP[0];
|
161 |
|
|
|
162 |
|
|
assign max_ovf = do_conversion? 1'b0 : unnormalized_mantissa[size_mantissa];
|
163 |
|
|
|
164 |
|
|
//compute leading_zeros over unnormalized mantissa
|
165 |
|
|
leading_zeros #( .SIZE_INT(max_size), .SIZE_COUNTER(max_counter), .PIPELINE(pipeline))
|
166 |
|
|
leading_zeros_instance (.a(max_entityINT_FP),
|
167 |
|
|
.ovf(max_ovf),
|
168 |
|
|
.lz(max_lzs));
|
169 |
|
|
|
170 |
|
|
//compute shifting over unnormalized_mantissa
|
171 |
|
|
shifter #( .INPUT_SIZE(max_size),
|
172 |
|
|
.SHIFT_SIZE(max_counter),
|
173 |
|
|
.OUTPUT_SIZE(max_size + 1),
|
174 |
|
|
.DIRECTION(1'b1), //0=right, 1=left
|
175 |
|
|
.PIPELINE(pipeline),
|
176 |
|
|
.POSITION(pipeline_pos))
|
177 |
|
|
shifter_instance( .a(max_entityINT_FP),//mantissa
|
178 |
|
|
.arith(lsb_shft_bit),//logical shift
|
179 |
|
|
.shft(max_lzs),
|
180 |
|
|
.shifted_a({r_mantissa, dummy_bits}));
|
181 |
|
|
|
182 |
|
|
//instantiate rounding_component
|
183 |
|
|
rounding #( .SIZE_MOST_S_MANTISSA(size_mantissa + 1),
|
184 |
|
|
.SIZE_LEAST_S_MANTISSA(max_size - size_mantissa + 1))
|
185 |
|
|
rounding_instance( .unrounded_mantissa({1'b0,r_mantissa}),
|
186 |
|
|
.dummy_bits(dummy_bits),
|
187 |
|
|
.rounded_mantissa(max_rounded_mantissa));
|
188 |
|
|
|
189 |
|
|
|
190 |
|
|
assign max_exp_selection = do_conversion? exponent : exp_inter;
|
191 |
|
|
assign max_adjust_exponent = max_exp_selection - max_lzs;
|
192 |
|
|
assign max_unadjusted_exponent = max_adjust_exponent + size_diff_i_m;
|
193 |
|
|
assign max_resulted_e_o = (do_conversion & ~(|max_entityINT_FP))? bias : max_unadjusted_exponent + max_rounded_mantissa[size_mantissa];
|
194 |
|
|
|
195 |
|
|
assign resulted_exponent = conversion[0]? max_entityFP_INT[size_mantissa+size_exponent-2 : size_mantissa-1] : max_resulted_e_o;
|
196 |
|
|
assign resulted_mantissa = conversion[0]? max_entityFP_INT[size_mantissa-1 : 0] :
|
197 |
|
|
(max_rounded_mantissa[size_mantissa])? (max_rounded_mantissa[size_mantissa : 1]) :
|
198 |
|
|
(max_rounded_mantissa[size_mantissa-1 : 0]);
|
199 |
|
|
|
200 |
|
|
//compute exception_field
|
201 |
|
|
special_cases #( .size_exception_field(size_exception_field),
|
202 |
|
|
.zero(zero),
|
203 |
|
|
.normal_number(normal_number),
|
204 |
|
|
.infinity(infinity),
|
205 |
|
|
.NaN(NaN))
|
206 |
|
|
special_cases_instance( .sp_case_a_number(sp_case_a_number),
|
207 |
|
|
.sp_case_b_number(sp_case_b_number),
|
208 |
|
|
.sp_case_result_o(sp_case_o));
|
209 |
|
|
|
210 |
|
|
//compute special case
|
211 |
|
|
assign resulted_exception_field = do_conversion? sp_case_a_number : sp_case_o;
|
212 |
|
|
|
213 |
|
|
//set zero_flag in case of equal numbers
|
214 |
|
|
assign zero_flag = ~((|{resulted_mantissa,sp_case_o[1]}) & (|sp_case_o));
|
215 |
|
|
|
216 |
|
|
//compute resulted_sign
|
217 |
|
|
assign resulted_sign = do_conversion? s_a_number : ((eff_op)?
|
218 |
|
|
(!a_greater_exponent[size_exponent]? (!b_greater_exponent[size_exponent]? ~adder_mantissa[size_mantissa+1] : s_a_number) : ~s_b_number) :
|
219 |
|
|
s_a_number);
|
220 |
|
|
|
221 |
|
|
assign resulted_number_o = (zero_flag)? {size{1'b0}} :
|
222 |
|
|
{resulted_exception_field, resulted_sign, resulted_exponent, resulted_mantissa[size_mantissa - 2 : 0]};
|
223 |
|
|
|
224 |
|
|
endmodule
|