-
Notifications
You must be signed in to change notification settings - Fork 0
/
fir_tb.v
450 lines (393 loc) · 11.6 KB
/
fir_tb.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 08/20/2023 10:38:55 AM
// Design Name:
// Module Name: fir_tb
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module fir_tb
#( parameter pADDR_WIDTH = 12,
parameter pDATA_WIDTH = 32,
parameter Tape_Num = 11,
parameter Data_Num = 600
)();
wire awready;
wire wready;
reg awvalid;
reg [(pADDR_WIDTH-1): 0] awaddr;
reg wvalid;
reg signed [(pDATA_WIDTH-1) : 0] wdata;
wire arready;
reg rready;
reg arvalid;
reg [(pADDR_WIDTH-1): 0] araddr;
wire rvalid;
wire signed [(pDATA_WIDTH-1): 0] rdata;
reg ss_tvalid;
reg signed [(pDATA_WIDTH-1) : 0] ss_tdata;
reg ss_tlast;
wire ss_tready;
reg sm_tready;
wire sm_tvalid;
wire signed [(pDATA_WIDTH-1) : 0] sm_tdata;
wire sm_tlast;
reg axis_clk;
reg axis_rst_n;
// ram for tap
wire [3:0] tap_WE;
wire tap_EN;
wire [(pDATA_WIDTH-1):0] tap_Di;
wire [(pADDR_WIDTH-1):0] tap_A;
wire [(pDATA_WIDTH-1):0] tap_Do;
// ram for data RAM
wire [3:0] data_WE;
wire data_EN;
wire [(pDATA_WIDTH-1):0] data_Di;
wire [(pADDR_WIDTH-1):0] data_A;
wire [(pDATA_WIDTH-1):0] data_Do;
fir fir_DUT(
.awready(awready),
.wready(wready),
.awvalid(awvalid),
.awaddr(awaddr),
.wvalid(wvalid),
.wdata(wdata),
.arready(arready),
.rready(rready),
.arvalid(arvalid),
.araddr(araddr),
.rvalid(rvalid),
.rdata(rdata),
.ss_tvalid(ss_tvalid),
.ss_tdata(ss_tdata),
.ss_tlast(ss_tlast),
.ss_tready(ss_tready),
.sm_tready(sm_tready),
.sm_tvalid(sm_tvalid),
.sm_tdata(sm_tdata),
.sm_tlast(sm_tlast),
// ram for tap
.tap_WE(tap_WE),
.tap_EN(tap_EN),
.tap_Di(tap_Di),
.tap_A(tap_A),
.tap_Do(tap_Do),
// ram for data
.data_WE(data_WE),
.data_EN(data_EN),
.data_Di(data_Di),
.data_A(data_A),
.data_Do(data_Do),
.axis_clk(axis_clk),
.axis_rst_n(axis_rst_n)
);
// RAM for tap
bram11 tap_RAM (
.CLK(axis_clk),
.WE(tap_WE),
.EN(tap_EN),
.Di(tap_Di),
.A(tap_A),
.Do(tap_Do)
);
// RAM for data: choose bram11 or bram12
bram11 data_RAM(
.CLK(axis_clk),
.WE(data_WE),
.EN(data_EN),
.Di(data_Di),
.A(data_A),
.Do(data_Do)
);
reg signed [(pDATA_WIDTH-1):0] Din_list[0:(Data_Num-1)];
reg signed [(pDATA_WIDTH-1):0] golden_list[0:(Data_Num-1)];
reg [31:0] status;
reg [31:0] data_length;
integer Din, golden, input_data, golden_data, m;
integer i,j;
integer k;
integer num_sent_out, num_result_received;
reg signed [31:0] coef[0:10]; // fill in coef
reg done_received;
reg last_received;
integer latency = 0;
always #5 axis_clk = (~axis_clk);
initial begin
$dumpfile("fir.vcd");
$dumpvars();
data_length = 0;
coef[0] = 32'd0;
coef[1] = -32'd10;
coef[2] = -32'd9;
coef[3] = 32'd23;
coef[4] = 32'd56;
coef[5] = 32'd63;
coef[6] = 32'd56;
coef[7] = 32'd23;
coef[8] = -32'd9;
coef[9] = -32'd10;
coef[10] = 32'd0;
axis_clk = 0;
//reset
axis_rst_n = 0;
arvalid = 0;
araddr = 0;
rready = 0;
awaddr = 0;
awvalid = 0;
wdata = 0;
wvalid = 0;
ss_tvalid = 0;
ss_tlast = 0;
ss_tdata = 0;
sm_tready = 0;
done_received = 0;
num_result_received = 0;
last_received = 0;
num_sent_out = 0;
axis_rst_n = 0;
#20; axis_rst_n = 1;
repeat(10) @(posedge axis_clk);
test_case_gen1;
wait_fir_idle;
setup_fir;
fork
latency_cal;
stream_in;
stream_out;
observe_ap_done;
join
$display("latency = %d", latency);
test_case_gen_auto;
wait_fir_idle;
setup_fir;
fork
latency_cal;
stream_in;
stream_out;
observe_ap_done;
join
$display("latency = %d", latency);
test_case_gen_auto;
wait_fir_idle;
setup_fir;
fork
latency_cal;
stream_in;
stream_out;
observe_ap_done;
join
$display("latency = %d", latency);
repeat(3) @(posedge axis_clk);
$finish;
end
task wait_fir_idle;begin
status = 0;
while(status[2] != 1) begin
araddr <= 12'h00;
arvalid <= 1;
@(posedge axis_clk);
while(!arready) @(posedge axis_clk);
arvalid <= 0;
rready <= 1;
@(posedge axis_clk);
while(!rvalid) @(posedge axis_clk);
status <= rdata;
rready <= 0;
@(posedge axis_clk);
end
done_received = 0;
last_received = 0;
end
endtask
task setup_fir; begin
$display("----Start the coefficient input(AXI-lite)----");
config_write(12'h10, data_length);
for(k=0; k< Tape_Num; k=k+1) begin
config_write(12'h20+4*k, coef[k]);
end
// read-back and check
$display(" Check Coefficient ...");
for(k=0; k < Tape_Num; k=k+1) begin
config_read_check(12'h20+4*k, coef[k], 32'hffffffff);
end
$display(" Tape programming done ...");
$display(" Start FIR");
@(posedge axis_clk) config_write(12'h00, 32'h0000_0001); // ap_start = 1
$display("----End the coefficient input(AXI-lite)----");
end
endtask
task test_case_gen1; begin
data_length = 0;
Din = $fopen("./samples_triangular_wave.dat","r");
golden = $fopen("./out_gold.dat","r");
for(m=0;m<Data_Num;m=m+1) begin
input_data = $fscanf(Din,"%d", Din_list[m]);
golden_data = $fscanf(golden,"%d", golden_list[m]);
data_length = data_length + 1;
end
end
endtask
task test_case_gen_auto; begin
data_length = {$random}%500;
for(i = 0;i<data_length;i=i+1)begin
Din_list[i] = $random%1000;
end
for(i = 0;i<11;i=i+1)begin
coef[i] = $random%1000;
end
for(i = 0;i<data_length+10;i=i+1)begin
golden_list[i] = 0;
for(j=0;j<11;j=j+1)begin
if((i-j)>=0 && (i-j)<data_length) golden_list[i] = golden_list[i] + Din_list[i-j]*coef[j];
end
end
end
endtask
task latency_cal; begin
latency <= 0;
while(!done_received)begin
@(posedge axis_clk)
latency <= latency + 1;
end
end
endtask
task config_write;
input [11:0] addr;
input [31:0] data;
begin
awvalid <= 1; awaddr <= addr;
wvalid <= 1; wdata <= data;
@(posedge axis_clk);
while (!wready) @(posedge axis_clk);
awvalid <= 0; wvalid <= 0;
awaddr <= 0; wdata <= 0;
@(posedge axis_clk);
end
endtask
task config_read_check;
input [11:0] addr;
input signed [31:0] exp_data;
input [31:0] mask;
begin
araddr <= addr;
arvalid <= 1;
@(posedge axis_clk);
while(!arready) @(posedge axis_clk);
arvalid <= 0;
araddr <= 0;
rready <= 1;
@(posedge axis_clk);
while(!rvalid) @(posedge axis_clk);
if( (rdata & mask) != (exp_data & mask)) begin
$display("ERROR: exp = %d, rdata = %d", exp_data, rdata);
repeat(3) @(posedge axis_clk);
$finish;
end else begin
$display("OK: exp = %d, rdata = %d", exp_data, rdata);
end
arvalid <= 0;
//@(posedge axis_clk);
end
endtask
task stream_in;begin
num_sent_out = 0;
$display("----Start the data input(AXI-Stream)----");
for(i=0;i<data_length;i=i+1) begin
if(i == data_length-1) ss_tlast <= 1;
else ss_tlast <= 0;
ss(Din_list[i]);
if(done_received) i=data_length;
num_sent_out <= num_sent_out + 1;
end
ss_tvalid <= 0;
ss_tlast <= 0;
ss_tdata <= 0;
@(posedge axis_clk);
if(num_sent_out != data_length)begin
$display("------done detected before all data sent out------");
repeat(3) @(posedge axis_clk);
$finish;
end
$display("------End the data input(AXI-Stream)------");
end
endtask
task stream_out; begin
k = 0;
while(last_received != 1 && !done_received) begin
sm(golden_list[k],k,last_received);
k = k+1;
end
sm_tready <= 0;
if(k != data_length)begin
$display("------early ending result transmission------");
repeat(3) @(posedge axis_clk);
$finish;
end
end
endtask
task observe_ap_done; begin
status <= 0;
while(status[1] != 1) begin
araddr <= 12'h00;
arvalid <= 1;
@(posedge axis_clk);
while(!arready) @(posedge axis_clk);
arvalid <= 0;
rready <= 1;
@(posedge axis_clk);
while(!rvalid) @(posedge axis_clk);
status <= rdata;
rready <= 0;
@(posedge axis_clk);
end
done_received <= 1;
end
endtask
task ss;
input signed [31:0] in1;
begin
ss_tvalid <= 1;
ss_tdata <= in1;
@(posedge axis_clk);
while (!ss_tready && !done_received) begin
@(posedge axis_clk);
end
end
endtask
task sm;
input signed [31:0] in2; // golden data
input [31:0] pcnt; // pattern count
output last;
begin
sm_tready <= 1;
@(posedge axis_clk);
while(!sm_tvalid && !done_received) @(posedge axis_clk);
if(!done_received)begin
last <= sm_tlast;
if (sm_tdata != in2) begin
$display("[ERROR] [Pattern %d] Golden answer: %d, Your answer: %d", pcnt, in2, sm_tdata);
repeat(3) @(posedge axis_clk);
$finish;
end
else begin
$display("[PASS] [Pattern %d] Golden answer: %d, Your answer: %d", pcnt, in2, sm_tdata);
end
end
@(posedge axis_clk);
end
endtask
endmodule