-
Notifications
You must be signed in to change notification settings - Fork 1
/
Add_tb.v
101 lines (86 loc) · 2.43 KB
/
Add_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
`timescale 1ns / 10ps
`define CYCLE 10.0
`define PATTERN 100
module Add_tb;
// clk generation
reg clk = 1;
always #(`CYCLE/2) clk = ~clk;
// dump waveform
// vcd
// initial begin
// $dumpfile("add.vcd");
// $dumpvars;
// end
// fsdb
initial begin
$fsdbDumpfile("add.fsdb");
$fsdbDumpvars(0, "+mda");
end
// time out
initial begin
#(100000 * `CYCLE);
$display("\n\033[1;31m=============================================");
$display(" Simulation Time Out! ");
$display("=============================================\033[0m");
$finish;
end
// instantiate DUT
reg [3:0] a, b;
wire [4:0] c;
Add add0(
.a(a),
.b(b),
.c(c)
);
// Initial memory
reg [3:0] INPUT_A_MEM [0:`PATTERN-1];
reg [3:0] INPUT_B_MEM [0:`PATTERN-1];
reg [4:0] GOLDEN_C_MEM [0:`PATTERN-1];
initial begin
$readmemb("pattern/add_pattern/add_in_a.dat", INPUT_A_MEM);
$readmemb("pattern/add_pattern/add_in_b.dat", INPUT_B_MEM);
$readmemb("pattern/add_pattern/add_out_c.dat", GOLDEN_C_MEM);
end
// input pattern & check result
integer i, j;
integer err_num = 0;
initial begin
for (i=0; i<10; i=i+1) begin
@(posedge clk);
end
for (i=0; i<`PATTERN; i=i+1) begin
a = INPUT_A_MEM[i];
b = INPUT_B_MEM[i];
@(posedge clk);
end
a = 'bx;
b = 'bx;
end
// check output
initial begin
for (j=0; j<11; j=j+1) begin
@(negedge clk);
end
for (j=0; j<`PATTERN; j=j+1) begin
if (GOLDEN_C_MEM[j] === c && c !== 4'bx) begin
$display("\033[1;92mPattern %3d passed. / Input A:%4d / Input B:%4d / Output C: %4d / Golden C: %4d\033[0m", j, INPUT_A_MEM[j],INPUT_B_MEM[j],c, GOLDEN_C_MEM[j]);
end
else begin
$display("\033[1;31mPattern %3d failed. / Input A:%4d / Input B:%4d / Output C: %4d / Golden C: %4d\033[0m", j, INPUT_A_MEM[j],INPUT_B_MEM[j],c, GOLDEN_C_MEM[j]);
err_num = err_num + 1;
end
@(negedge clk);
end
if (err_num != 0) begin
$display("\n\033[1;31m=============================================");
$display(" Simulation failed ");
$display("=============================================\033[0m");
end
else begin
$display("\n\033[1;92m=============================================");
$display(" Simulation passed ");
$display("=============================================\033[0m");
end
$finish;
end
endmodule