-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmul_fp25_pipeline.v
299 lines (261 loc) · 7.31 KB
/
mul_fp25_pipeline.v
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
`timescale 1ns/1ns
module mul_fp52 (
clk
,op_a_dat //|< r
,op_b_dat //|< r
,op_c_dat //|< r
,res
);
parameter op_a_exp = 0; // exponent of op_a_dat
parameter op_b_exp = 0; // exponent of op_b_dat
parameter op_c_exp = 0; // exponent of op_c_dat
input clk;
input [5:0] op_a_dat; // 6 bits
input [5:0] op_b_dat; // 6 bits
input [5:0] op_c_dat; // 6 bits
output [17:0] res; //12 + max(3+3,3) = 18 bits
reg [5:0] op_a_dat_reg;
reg [5:0] op_b_dat_reg;
reg [5:0] op_c_dat_reg;
wire [17:0] res_wire;
reg [17:0] res;
always@(posedge clk)
begin
op_a_dat_reg <= op_a_dat;
op_b_dat_reg <= op_b_dat;
op_c_dat_reg <= op_c_dat;
res <= res_wire;
end
mul #( .op_a_exp(op_a_exp),
.op_b_exp(op_b_exp),
.op_c_exp(op_c_exp))
u_mul(
.op_a_dat(op_a_dat_reg)
,.op_b_dat(op_b_dat_reg)
,.op_c_dat(op_c_dat_reg)
,.res(res_wire)
,.clk(clk)
);
endmodule
//==================
// mul unit fp(2,5)
//==================
// for fp(e,m)=fp(2,5), we have
// 6-bit fraction = 1/0 + mantissa
// 2-bit exponent = [0,3] - bias = {-1,0,1,2}
module mul (
op_a_dat //|< r
,op_b_dat //|< r
,op_c_dat //|< r
,res
,clk
);
parameter op_a_exp = 0; // exponent of op_a_dat
parameter op_b_exp = 0; // exponent of op_b_dat
parameter op_c_exp = 0; // exponent of op_c_dat
input clk;
input [5:0] op_a_dat; // 6 bits (1-bit implicit + 5-bit mantissa)
input [5:0] op_b_dat; // 6 bits
input [5:0] op_c_dat; // 6 bits
output [17:0] res; //12+xx bits
wire [6:0] sel_data_0;
wire [6:0] sel_data_1;
wire [6:0] sel_data_2;
wire sel_inv_0;
wire sel_inv_1;
wire sel_inv_2;
reg [6:0] code_lo;
reg [2:0] code_0;
reg [2:0] code_1;
reg [2:0] code_2;
reg [11:0] ppre_0;
reg [11:0] ppre_1;
reg [11:0] ppre_2;
reg [11:0] ppre_3;
// reg [11:0] ppre_4;
reg [17:0] ppre_0_sft;
reg [17:0] ppre_1_sft;
reg [17:0] ppre_2_sft;
reg [17:0] ppre_3_sft;
reg [17:0] ppre_4_sft;
reg [17:0] ppre_5_sft;
reg [53:0] pp_in_l0n0;
reg [53:0] pp_in_l0n1;
wire [17:0] pp_out_l0n0_0;
wire [17:0] pp_out_l0n0_1;
wire [17:0] pp_out_l0n1_0;
wire [17:0] pp_out_l0n1_1;
reg [71:0] pp_in_l1n0;
wire [17:0] pp_out_l1n0_0;
wire [17:0] pp_out_l1n0_1;
reg [17:0] res;
//==========================================================
// Booth recoding and selection, radix-4
//==========================================================
always @(
op_b_dat
) begin
code_lo = {op_b_dat[5:0], 1'b0};
code_0 = code_lo[2:0];
code_1 = code_lo[4:2];
code_2 = code_lo[6:4];
end
NV_NVDLA_CMAC_CORE_MAC_booth u_booth_0 (
.code (code_0[2:0]) //|< r
,.src_data (op_a_dat) //|< r
,.out_data (sel_data_0) //|> w
,.out_inv (sel_inv_0) //|> w
);
NV_NVDLA_CMAC_CORE_MAC_booth u_booth_1 (
.code (code_1[2:0]) //|< r
,.src_data (op_a_dat) //|< r
,.out_data (sel_data_1) //|> w
,.out_inv (sel_inv_1) //|> w
);
NV_NVDLA_CMAC_CORE_MAC_booth u_booth_2 (
.code (code_2[2:0]) //|< r
,.src_data (op_a_dat) //|< r
,.out_data (sel_data_2) //|> w
,.out_inv (sel_inv_2) //|> w
);
//==========================================================
// Partial products generation (Baugh-Wooley)
//==========================================================
always @(
sel_data_0
or sel_data_1
or sel_data_2
or sel_inv_0
or sel_inv_1
or sel_inv_2
) begin
ppre_0 = {5'b0, sel_data_0}; //12 bits
ppre_1 = {3'b0, sel_data_1, 1'b0, sel_inv_0};
ppre_2 = {1'b0, sel_data_2, 1'b0, sel_inv_1, 2'b0};
ppre_3 = {7'b0, sel_inv_2, 4'b0};
end
// sign extension for fractional parts
always @(
ppre_2
or ppre_1
or ppre_0
) begin
ppre_0_sft = ({ppre_0, 6'b0} >>> 6) << (op_a_exp+op_b_exp);
ppre_1_sft = ({ppre_1, 6'b0} >>> 6) << (op_a_exp+op_b_exp);
ppre_2_sft = ({ppre_2, 6'b0} >>> 6) << (op_a_exp+op_b_exp);
end
always @(
op_c_dat
or ppre_3
) begin
ppre_3_sft = ({ppre_3, 6'b0} >>> 6) << (op_a_exp+op_b_exp);
ppre_4_sft = 18'b11_1111_1010_1100_0000 << (op_a_exp+op_b_exp);
ppre_5_sft = ({op_c_dat, 6'b0} >>> 6) << op_c_exp;
end
//==========================================================
// CSA tree (level 1)
//==========================================================
always @(
posedge clk
) begin
pp_in_l0n0 = {ppre_2_sft, ppre_1_sft, ppre_0_sft};
pp_in_l0n1 = {ppre_5_sft, ppre_4_sft, ppre_3_sft};
end
NV_DW02_tree #(3, 18) u_tree_l0n0 (
.INPUT (pp_in_l0n0[53:0]) //|< r
,.OUT0 (pp_out_l0n0_0[17:0]) //|> sum
,.OUT1 (pp_out_l0n0_1[17:0]) //|> carry
);
NV_DW02_tree #(3, 18) u_tree_l0n1 (
.INPUT (pp_in_l0n1[53:0]) //|< r
,.OUT0 (pp_out_l0n1_0[17:0]) //|> sum
,.OUT1 (pp_out_l0n1_1[17:0]) //|> carry
);
//==========================================================
// CSA tree (level 2)
//==========================================================
always @(
posedge clk
) begin
pp_in_l1n0 = {pp_out_l0n1_1, pp_out_l0n1_0, pp_out_l0n0_1, pp_out_l0n0_0};
end
NV_DW02_tree #(4, 18) u_tree_l1n0 (
.INPUT (pp_in_l1n0[71:0]) //|< r
,.OUT0 (pp_out_l1n0_0[17:0]) //|> sum
,.OUT1 (pp_out_l1n0_1[17:0]) //|> carry
);
//=======================
// Shift psum by exponent
//=======================
always @(
pp_out_l1n0_0
or pp_out_l1n0_1
) begin
res = pp_out_l1n0_0 + pp_out_l1n0_1;
end
endmodule // mul
//==========================================================
//
// Sub unit for mul
// Booth's recoder and Booth's selector with inversed sign flag
//
//==========================================================
module NV_NVDLA_CMAC_CORE_MAC_booth (
code
,src_data
,out_data
,out_inv
);
input [2:0] code;
input [5:0] src_data;
output [6:0] out_data;
output out_inv;
reg [6:0] out_data;
reg out_inv;
always @(
code
or src_data
) begin
case(code)
///////// for 4bit /////////
// +/- 0*src_data
3'b000,
3'b111:
begin
out_data = 7'b100_0000;
out_inv = 1'b0;
end
// + 1*src_data
3'b001,
3'b010:
begin
out_data = {~src_data[5], src_data};
out_inv = 1'b0;
end
// - 1*src_data
3'b101,
3'b110:
begin
out_data = {src_data[5], ~src_data};
out_inv = 1'b1;
end
// + 2*src_data
3'b011:
begin
out_data = {~src_data[5], src_data[4:0], 1'b0}; //shift
out_inv = 1'b0;
end
// - 2*src_data
3'b100:
begin
out_data = {src_data[5], ~src_data[4:0], 1'b1}; //shift and add
out_inv = 1'b1;
end
default:
begin
out_data = 7'b100_0000;
out_inv = 1'b0;
end
endcase
end
endmodule // NV_NVDLA_CMAC_CORE_MAC_booth