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 |
3 |
constantin |
module Multiply_Accumulate #( parameter size_exponent = 8, //exponent bits
|
22 |
|
|
parameter size_mantissa = 24, //mantissa bits
|
23 |
|
|
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 |
|
|
|
44 |
|
|
wire [size_mantissa - 1 : 0] m_a_number, m_b_number, m_c_number;
|
45 |
|
|
wire [size_exponent - 1 : 0] e_a_number, e_b_number, e_c_number;
|
46 |
|
|
wire s_a_number, s_b_number, s_c_number;
|
47 |
|
|
wire [size_exception_field - 1 : 0] sp_case_a_number, sp_case_b_number, sp_case_c_number;
|
48 |
|
|
//---------------------------------------------------------------------------------------
|
49 |
|
|
|
50 |
|
|
|
51 |
|
|
wire [size_mul_mantissa-1:0] mul_mantissa, c_mantissa;
|
52 |
|
|
wire [size_mul_mantissa :0] acc_resulting_number;
|
53 |
|
|
wire [size_mul_mantissa :0] ab_shifted_mul_mantissa, c_shifted_mantissa;
|
54 |
|
|
wire [size_exponent : 0] exp_ab;
|
55 |
|
|
wire [size_exponent-1:0] modify_exp_ab, modify_exp_c;
|
56 |
|
|
wire [size_mul_counter-1: 0] lz_mul;
|
57 |
|
|
wire sign_res;
|
58 |
|
|
wire eff_sub;
|
59 |
|
|
wire ovf;
|
60 |
|
|
wire comp_exp;
|
61 |
|
|
wire [size_mul_mantissa+1:0] normalized_mantissa;
|
62 |
|
|
wire [size_mantissa - 1 : 0] rounded_mantissa;
|
63 |
|
|
wire [size_exponent :0] unnormalized_exp;
|
64 |
|
|
wire [size_mantissa-2:0] final_mantissa;
|
65 |
|
|
wire [size_exponent-1:0] final_exponent;
|
66 |
|
|
wire [size_exception_field - 1 : 0] sp_case_result_o;
|
67 |
3 |
constantin |
|
68 |
5 |
constantin |
assign m_a_number = {1'b1, a_number_i[size_mantissa - 2 :0]};
|
69 |
|
|
assign m_b_number = {1'b1, b_number_i[size_mantissa - 2 :0]};
|
70 |
|
|
assign m_c_number = {1'b1, c_number_i[size_mantissa - 2 :0]};
|
71 |
|
|
assign e_a_number = a_number_i[size_mantissa + size_exponent - 1 : size_mantissa - 1];
|
72 |
|
|
assign e_b_number = b_number_i[size_mantissa + size_exponent - 1 : size_mantissa - 1];
|
73 |
|
|
assign e_c_number = c_number_i[size_mantissa + size_exponent - 1 : size_mantissa - 1];
|
74 |
|
|
assign s_a_number = a_number_i[size - size_exception_field - 1];
|
75 |
|
|
assign s_b_number = b_number_i[size - size_exception_field - 1];
|
76 |
|
|
assign s_c_number = c_number_i[size - size_exception_field - 1];
|
77 |
|
|
assign sp_case_a_number = a_number_i[size - 1 : size - size_exception_field];
|
78 |
|
|
assign sp_case_b_number = b_number_i[size - 1 : size - size_exception_field];
|
79 |
|
|
assign sp_case_c_number = c_number_i[size - 1 : size - size_exception_field];
|
80 |
3 |
constantin |
|
81 |
5 |
constantin |
|
82 |
|
|
//instantiate multiply component
|
83 |
|
|
multiply #( .size_mantissa(size_mantissa),
|
84 |
|
|
.size_counter(size_counter),
|
85 |
|
|
.size_mul_mantissa(size_mul_mantissa))
|
86 |
|
|
multiply_instance ( .a_mantissa_i(m_a_number),
|
87 |
|
|
.b_mantissa_i(m_b_number),
|
88 |
|
|
.mul_mantissa(mul_mantissa));
|
89 |
|
|
|
90 |
|
|
|
91 |
|
|
assign c_mantissa = {1'b0,m_c_number, {(size_mantissa-1'b1){1'b0}}};
|
92 |
|
|
assign exp_ab = e_a_number + e_b_number - ({1'b1,{(size_exponent-1'b1){1'b0}}} - 1'b1);
|
93 |
|
|
assign {modify_exp_ab, modify_exp_c, unnormalized_exp} = (exp_ab >= e_c_number)? {8'd0,(exp_ab - e_c_number), exp_ab} : {(e_c_number - exp_ab), 8'd0, {1'b0,e_c_number}};
|
94 |
|
|
|
95 |
|
|
|
96 |
|
|
//instantiate shifter component for mul_mantissa shift, mul_mantissa <=> ab_mantissa
|
97 |
|
|
shifter #( .INPUT_SIZE(size_mul_mantissa),
|
98 |
|
|
.SHIFT_SIZE(size_exponent),
|
99 |
|
|
.OUTPUT_SIZE(size_mul_mantissa + 1'b1),
|
100 |
|
|
.DIRECTION(1'b0),
|
101 |
|
|
.PIPELINE(pipeline),
|
102 |
|
|
.POSITION(pipeline_pos))
|
103 |
|
|
shifter_ab_instance( .a(mul_mantissa),
|
104 |
|
|
.arith(1'b0),
|
105 |
|
|
.shft(modify_exp_ab),
|
106 |
|
|
.shifted_a(ab_shifted_mul_mantissa));
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
//instantiate shifter component for c_mantissa shift
|
110 |
|
|
shifter #( .INPUT_SIZE(size_mul_mantissa),
|
111 |
|
|
.SHIFT_SIZE(size_exponent),
|
112 |
|
|
.OUTPUT_SIZE(size_mul_mantissa + 1'b1),
|
113 |
|
|
.DIRECTION(1'b0),
|
114 |
|
|
.PIPELINE(pipeline),
|
115 |
|
|
.POSITION(pipeline_pos))
|
116 |
|
|
shifter_c_instance( .a(c_mantissa),
|
117 |
|
|
.arith(1'b0),
|
118 |
|
|
.shft(modify_exp_c),
|
119 |
|
|
.shifted_a(c_shifted_mantissa));
|
120 |
|
|
|
121 |
|
|
|
122 |
|
|
//instantiate effective_op component
|
123 |
|
|
effective_op effective_op_instance( .sign_a(s_a_number),
|
124 |
|
|
.sign_b(s_b_number),
|
125 |
|
|
.sign_c(s_c_number),
|
126 |
|
|
.sub(sub),
|
127 |
|
|
.eff_sub(eff_sub));
|
128 |
|
|
|
129 |
|
|
|
130 |
|
|
//instantiate accumulate component
|
131 |
|
|
accumulate #( .size_mantissa(size_mantissa),
|
132 |
|
|
.size_counter(size_counter),
|
133 |
|
|
.size_mul_mantissa(size_mul_mantissa))
|
134 |
|
|
accumulate_instance ( .ab_number_i(ab_shifted_mul_mantissa[size_mul_mantissa:1]),
|
135 |
|
|
.c_number_i(c_shifted_mantissa[size_mul_mantissa:1]),
|
136 |
|
|
.sub(eff_sub),
|
137 |
|
|
.ovf(ovf),
|
138 |
|
|
.acc_resulting_number_o(acc_resulting_number));
|
139 |
|
|
|
140 |
|
|
|
141 |
|
|
//instantiate leading_zeros component
|
142 |
|
|
leading_zeros #( .SIZE_INT(size_mul_mantissa + 1'b1),
|
143 |
|
|
.SIZE_COUNTER(size_mul_counter),
|
144 |
|
|
.PIPELINE(pipeline))
|
145 |
|
|
leading_zeros_instance( .a(acc_resulting_number),
|
146 |
|
|
.ovf(ovf),
|
147 |
|
|
.lz(lz_mul));
|
148 |
|
|
|
149 |
|
|
|
150 |
|
|
//instantiate shifter component
|
151 |
|
|
shifter #( .INPUT_SIZE(size_mul_mantissa + 1'b1),
|
152 |
|
|
.SHIFT_SIZE(size_mul_counter),
|
153 |
|
|
.OUTPUT_SIZE(size_mul_mantissa + 2'd2),
|
154 |
|
|
.DIRECTION(1'b1),
|
155 |
|
|
.PIPELINE(pipeline),
|
156 |
|
|
.POSITION(pipeline_pos))
|
157 |
|
|
shifter_instance( .a(acc_resulting_number),
|
158 |
|
|
.arith(1'b0),
|
159 |
|
|
.shft(lz_mul),
|
160 |
|
|
.shifted_a(normalized_mantissa));
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
//instantiate rounding component
|
164 |
|
|
rounding #( .SIZE_MOST_S_MANTISSA(size_mantissa),
|
165 |
|
|
.SIZE_LEAST_S_MANTISSA(size_mul_mantissa-size_mantissa+2))
|
166 |
|
|
rounding_instance ( .unrounded_mantissa(normalized_mantissa[size_mul_mantissa+1 : size_mul_mantissa+2-size_mantissa]),
|
167 |
|
|
.dummy_bits(normalized_mantissa[size_mul_mantissa+1-size_mantissa : 0]),
|
168 |
|
|
.rounded_mantissa(rounded_mantissa));
|
169 |
|
|
|
170 |
|
|
|
171 |
|
|
//instantiate special_cases_mul_acc component
|
172 |
|
|
special_cases_mul_acc #( .size_exception_field(size_exception_field),
|
173 |
|
|
.zero(zero),
|
174 |
|
|
.normal_number(normal_number),
|
175 |
|
|
.infinity(infinity),
|
176 |
|
|
.NaN(NaN))
|
177 |
|
|
special_cases_mul_acc_instance ( .sp_case_a_number(sp_case_a_number),
|
178 |
|
|
.sp_case_b_number(sp_case_b_number),
|
179 |
|
|
.sp_case_c_number(sp_case_c_number),
|
180 |
|
|
.sp_case_result_o(sp_case_result_o));
|
181 |
|
|
|
182 |
|
|
|
183 |
|
|
//compute resulted_sign
|
184 |
|
|
assign sign_res = (eff_sub)? ((c_shifted_mantissa > ab_shifted_mul_mantissa)? s_c_number : ~s_c_number) : s_c_number;
|
185 |
|
|
|
186 |
|
|
|
187 |
|
|
assign final_exponent = unnormalized_exp - lz_mul + 2'd2;
|
188 |
|
|
assign final_mantissa = rounded_mantissa[size_mantissa-2 : 0];
|
189 |
|
|
assign resulting_number_o = {sp_case_result_o, sign_res, final_exponent, final_mantissa};
|
190 |
|
|
endmodule
|