1 |
5 |
constantin |
`timescale 1ns / 1ps
|
2 |
|
|
//////////////////////////////////////////////////////////////////////////////////
|
3 |
|
|
// Company:
|
4 |
|
|
// Engineer:
|
5 |
|
|
//
|
6 |
|
|
// Create Date: 17:53:05 10/15/2013
|
7 |
|
|
// Design Name:
|
8 |
|
|
// Module Name: Multiply_Accumulate
|
9 |
|
|
// Project Name:
|
10 |
|
|
// Target Devices:
|
11 |
|
|
// Tool versions:
|
12 |
|
|
// Description: C ± A*B
|
13 |
|
|
//
|
14 |
|
|
// Dependencies:
|
15 |
|
|
//
|
16 |
|
|
// Revision:
|
17 |
|
|
// Revision 0.01 - File Created
|
18 |
|
|
// Additional Comments:
|
19 |
|
|
//
|
20 |
|
|
//////////////////////////////////////////////////////////////////////////////////
|
21 |
9 |
constantin |
module Multiply_Accumulate #( parameter size_mantissa = 24, //mantissa bits(1.M)
|
22 |
|
|
parameter size_exponent = 8, //exponent bits
|
23 |
3 |
constantin |
parameter size_counter = 5, //log2(size_mantissa) + 1 = 5
|
24 |
5 |
constantin |
parameter size_exception_field = 2, // zero/normal numbers/infinity/NaN
|
25 |
3 |
constantin |
parameter zero = 00, //00
|
26 |
|
|
parameter normal_number = 01, //01
|
27 |
|
|
parameter infinity = 10, //10
|
28 |
5 |
constantin |
parameter NaN = 11, //11
|
29 |
|
|
parameter pipeline = 0,
|
30 |
|
|
parameter pipeline_pos = 0, //8 bits
|
31 |
3 |
constantin |
|
32 |
5 |
constantin |
parameter size = size_exponent + size_mantissa + size_exception_field,
|
33 |
|
|
parameter size_mul_mantissa = size_mantissa + size_mantissa,
|
34 |
|
|
parameter size_mul_counter = size_counter + 1)
|
35 |
|
|
( input clk,
|
36 |
|
|
input rst,
|
37 |
|
|
input [size - 1:0] a_number_i,
|
38 |
|
|
input [size - 1:0] b_number_i,
|
39 |
|
|
input [size - 1:0] c_number_i,
|
40 |
|
|
input sub,
|
41 |
|
|
output[size - 1:0] resulting_number_o);
|
42 |
|
|
|
43 |
9 |
constantin |
parameter bias_0_bits = size_exponent - 1;
|
44 |
|
|
parameter shift_mantissa_0_bits = size_mantissa-1'b1;
|
45 |
5 |
constantin |
|
46 |
9 |
constantin |
wire [size_exception_field - 1 : 0] sp_case_a_number, sp_case_b_number, sp_case_c_number;
|
47 |
5 |
constantin |
wire [size_mantissa - 1 : 0] m_a_number, m_b_number, m_c_number;
|
48 |
|
|
wire [size_exponent - 1 : 0] e_a_number, e_b_number, e_c_number;
|
49 |
|
|
wire s_a_number, s_b_number, s_c_number;
|
50 |
|
|
|
51 |
9 |
constantin |
wire [size_exponent : 0] ab_greater_exponent, c_greater_exponent;
|
52 |
5 |
constantin |
|
53 |
9 |
constantin |
wire [size_exponent - 1 : 0] exp_difference;
|
54 |
|
|
wire [size_exponent - 1 : 0] unadjusted_exponent;
|
55 |
|
|
wire [size_exponent : 0] exp_inter;
|
56 |
|
|
|
57 |
|
|
wire [size_mul_mantissa - 1 : 0] m_ab_mantissa, c_mantissa;
|
58 |
|
|
wire [size_exponent : 0] e_ab_number_inter, e_ab_number;
|
59 |
|
|
wire [size_mul_counter - 1 : 0] lz_mul;
|
60 |
|
|
|
61 |
|
|
wire zero_flag;
|
62 |
5 |
constantin |
wire sign_res;
|
63 |
9 |
constantin |
wire eff_op;
|
64 |
|
|
|
65 |
|
|
wire [size_mantissa - 1 : 0] initial_rounding_bits, inter_rounding_bits, final_rounding_bits;
|
66 |
|
|
wire [size_mul_mantissa + 1 : 0] normalized_mantissa, adder_mantissa;
|
67 |
|
|
wire [size_mul_mantissa : 0] unnormalized_mantissa;
|
68 |
|
|
wire [size_mul_mantissa - 1 : 0] shifted_m_ab;
|
69 |
|
|
wire [size_mul_mantissa - 1 : 0] m_c, m_ab;
|
70 |
|
|
|
71 |
5 |
constantin |
wire [size_exception_field - 1 : 0] sp_case_result_o;
|
72 |
9 |
constantin |
wire [size_mantissa - 2 : 0] final_mantissa;
|
73 |
|
|
wire [size_exponent - 1 : 0] final_exponent;
|
74 |
|
|
wire [size_mantissa : 0] rounded_mantissa;
|
75 |
|
|
|
76 |
3 |
constantin |
|
77 |
9 |
constantin |
assign m_a_number = {1'b1, a_number_i[size_mantissa - 2 :0]};
|
78 |
5 |
constantin |
assign m_b_number = {1'b1, b_number_i[size_mantissa - 2 :0]};
|
79 |
|
|
assign m_c_number = {1'b1, c_number_i[size_mantissa - 2 :0]};
|
80 |
|
|
assign e_a_number = a_number_i[size_mantissa + size_exponent - 1 : size_mantissa - 1];
|
81 |
|
|
assign e_b_number = b_number_i[size_mantissa + size_exponent - 1 : size_mantissa - 1];
|
82 |
|
|
assign e_c_number = c_number_i[size_mantissa + size_exponent - 1 : size_mantissa - 1];
|
83 |
|
|
assign s_a_number = a_number_i[size - size_exception_field - 1];
|
84 |
|
|
assign s_b_number = b_number_i[size - size_exception_field - 1];
|
85 |
|
|
assign s_c_number = c_number_i[size - size_exception_field - 1];
|
86 |
|
|
assign sp_case_a_number = a_number_i[size - 1 : size - size_exception_field];
|
87 |
|
|
assign sp_case_b_number = b_number_i[size - 1 : size - size_exception_field];
|
88 |
|
|
assign sp_case_c_number = c_number_i[size - 1 : size - size_exception_field];
|
89 |
3 |
constantin |
|
90 |
5 |
constantin |
|
91 |
|
|
//instantiate multiply component
|
92 |
|
|
multiply #( .size_mantissa(size_mantissa),
|
93 |
|
|
.size_counter(size_counter),
|
94 |
|
|
.size_mul_mantissa(size_mul_mantissa))
|
95 |
|
|
multiply_instance ( .a_mantissa_i(m_a_number),
|
96 |
|
|
.b_mantissa_i(m_b_number),
|
97 |
9 |
constantin |
.mul_mantissa(m_ab_mantissa));
|
98 |
5 |
constantin |
|
99 |
9 |
constantin |
assign c_mantissa = {1'b0,m_c_number, {(shift_mantissa_0_bits){1'b0}}};
|
100 |
|
|
assign e_ab_number_inter = e_a_number + e_b_number;
|
101 |
|
|
assign e_ab_number = e_ab_number_inter - ({1'b1,{(bias_0_bits){1'b0}}} - 1'b1);
|
102 |
5 |
constantin |
|
103 |
9 |
constantin |
//find the greater exponent
|
104 |
|
|
assign ab_greater_exponent = e_ab_number - e_c_number;
|
105 |
|
|
assign c_greater_exponent = e_c_number - e_ab_number;
|
106 |
5 |
constantin |
|
107 |
9 |
constantin |
//find the difference between exponents
|
108 |
|
|
assign exp_difference = (ab_greater_exponent[size_exponent])? c_greater_exponent[size_exponent - 1 : 0] : ab_greater_exponent[size_exponent - 1 : 0];
|
109 |
|
|
assign exp_inter = (c_greater_exponent[size_exponent])? {1'b0, e_ab_number} : {1'b0, e_c_number};
|
110 |
5 |
constantin |
|
111 |
9 |
constantin |
//set shifter always on m_ab_number
|
112 |
|
|
assign {m_c, m_ab} = (ab_greater_exponent[size_exponent])? {c_mantissa, m_ab_mantissa} :
|
113 |
|
|
{m_ab_mantissa, c_mantissa};
|
114 |
5 |
constantin |
|
115 |
9 |
constantin |
//shift m_ab_number
|
116 |
5 |
constantin |
shifter #( .INPUT_SIZE(size_mul_mantissa),
|
117 |
9 |
constantin |
.SHIFT_SIZE(size_exponent),
|
118 |
|
|
.OUTPUT_SIZE(size_mul_mantissa + size_mantissa),
|
119 |
|
|
.DIRECTION(1'b0), //0=right, 1=left
|
120 |
|
|
.PIPELINE(pipeline),
|
121 |
|
|
.POSITION(pipeline_pos))
|
122 |
|
|
m_b_shifter_instance( .a(m_ab),//mantissa
|
123 |
|
|
.arith(1'b0),//logical shift
|
124 |
|
|
.shft(exp_difference),
|
125 |
|
|
.shifted_a({shifted_m_ab, initial_rounding_bits}));
|
126 |
5 |
constantin |
|
127 |
|
|
|
128 |
|
|
//instantiate effective_op component
|
129 |
|
|
effective_op effective_op_instance( .sign_a(s_a_number),
|
130 |
|
|
.sign_b(s_b_number),
|
131 |
|
|
.sign_c(s_c_number),
|
132 |
|
|
.sub(sub),
|
133 |
9 |
constantin |
.eff_sub(eff_op));
|
134 |
|
|
|
135 |
5 |
constantin |
//instantiate accumulate component
|
136 |
9 |
constantin |
accumulate #(.size_mul_mantissa(size_mul_mantissa))
|
137 |
|
|
accumulate_instance ( .m_a(m_c),
|
138 |
|
|
.m_b(shifted_m_ab),
|
139 |
|
|
.eff_op(eff_op),
|
140 |
|
|
.adder_mantissa(adder_mantissa));
|
141 |
|
|
|
142 |
|
|
//compute unnormalized_mantissa
|
143 |
|
|
assign {unnormalized_mantissa, inter_rounding_bits} =
|
144 |
|
|
(adder_mantissa[size_mul_mantissa + 1])? ({~adder_mantissa[size_mul_mantissa : 0], ~initial_rounding_bits}) :
|
145 |
|
|
({adder_mantissa[size_mul_mantissa : 0], initial_rounding_bits});
|
146 |
|
|
|
147 |
5 |
constantin |
//instantiate leading_zeros component
|
148 |
|
|
leading_zeros #( .SIZE_INT(size_mul_mantissa + 1'b1),
|
149 |
|
|
.SIZE_COUNTER(size_mul_counter),
|
150 |
|
|
.PIPELINE(pipeline))
|
151 |
9 |
constantin |
leading_zeros_instance( .a(unnormalized_mantissa[size_mul_mantissa : 0]),
|
152 |
|
|
.ovf(unnormalized_mantissa[size_mul_mantissa]),
|
153 |
5 |
constantin |
.lz(lz_mul));
|
154 |
|
|
|
155 |
|
|
//instantiate shifter component
|
156 |
9 |
constantin |
shifter #( .INPUT_SIZE(size_mul_mantissa + size_mantissa + 1),
|
157 |
5 |
constantin |
.SHIFT_SIZE(size_mul_counter),
|
158 |
9 |
constantin |
.OUTPUT_SIZE(size_mul_mantissa + size_mantissa + 2),
|
159 |
5 |
constantin |
.DIRECTION(1'b1),
|
160 |
|
|
.PIPELINE(pipeline),
|
161 |
|
|
.POSITION(pipeline_pos))
|
162 |
9 |
constantin |
shifter_instance( .a({unnormalized_mantissa, inter_rounding_bits}),
|
163 |
5 |
constantin |
.arith(1'b0),
|
164 |
|
|
.shft(lz_mul),
|
165 |
9 |
constantin |
.shifted_a({normalized_mantissa, final_rounding_bits}));
|
166 |
|
|
|
167 |
|
|
//instantiate rounding_component
|
168 |
|
|
rounding #( .SIZE_MOST_S_MANTISSA(size_mantissa+1),
|
169 |
|
|
.SIZE_LEAST_S_MANTISSA(size_mul_mantissa+2))
|
170 |
|
|
rounding_instance( .unrounded_mantissa({1'b0, normalized_mantissa[size_mul_mantissa+1 : size_mantissa + 2]}),
|
171 |
|
|
.dummy_bits({normalized_mantissa[size_mantissa + 1 : 0],final_rounding_bits}),
|
172 |
|
|
.rounded_mantissa(rounded_mantissa));
|
173 |
|
|
|
174 |
5 |
constantin |
//instantiate special_cases_mul_acc component
|
175 |
|
|
special_cases_mul_acc #( .size_exception_field(size_exception_field),
|
176 |
|
|
.zero(zero),
|
177 |
|
|
.normal_number(normal_number),
|
178 |
|
|
.infinity(infinity),
|
179 |
|
|
.NaN(NaN))
|
180 |
|
|
special_cases_mul_acc_instance ( .sp_case_a_number(sp_case_a_number),
|
181 |
|
|
.sp_case_b_number(sp_case_b_number),
|
182 |
|
|
.sp_case_c_number(sp_case_c_number),
|
183 |
|
|
.sp_case_result_o(sp_case_result_o));
|
184 |
9 |
constantin |
|
185 |
|
|
//set zero_flag in case of equal numbers
|
186 |
|
|
assign zero_flag = ~(|(rounded_mantissa));
|
187 |
5 |
constantin |
|
188 |
|
|
//compute resulted_sign
|
189 |
9 |
constantin |
assign sign_res = (eff_op)? (!c_greater_exponent[size_exponent]?
|
190 |
|
|
(!ab_greater_exponent[size_exponent]? ~adder_mantissa[size_mul_mantissa+1] : s_c_number) : ~(s_b_number^s_a_number)) : s_c_number;
|
191 |
5 |
constantin |
|
192 |
9 |
constantin |
assign final_mantissa = (rounded_mantissa[size_mantissa])?
|
193 |
|
|
(rounded_mantissa[size_mantissa : 1]) :
|
194 |
|
|
(rounded_mantissa[size_mantissa-1: 0]);
|
195 |
|
|
|
196 |
|
|
assign unadjusted_exponent = exp_inter - lz_mul;
|
197 |
|
|
assign final_exponent = unadjusted_exponent + 2'd2;
|
198 |
|
|
assign resulting_number_o = (zero_flag)? {size{1'b0}} :{sp_case_result_o, sign_res, final_exponent, final_mantissa};
|
199 |
5 |
constantin |
endmodule
|