forked from BerkeleyLab/Bedrock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbiquad_tb.v
217 lines (197 loc) · 5.75 KB
/
biquad_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
`timescale 1 ns / 1ns
module biquad_tb;
parameter DATA_WIDTH = 28;
parameter DATA_COUNT = 4;
parameter COEFFICIENT_WIDTH = 25;
reg sysClk = 1, dataClk = 1;
reg sysCoefficientStrobe = 0;
reg [2:0] sysCoefficientAddress = {3{1'bx}};
reg [COEFFICIENT_WIDTH-1:0] sysCoefficientValue = {COEFFICIENT_WIDTH{1'bx}};
reg S_TVALID = 0;
reg [(DATA_COUNT*DATA_WIDTH)-1:0] S_TDATA = {DATA_COUNT*DATA_WIDTH{1'bx}};
wire S_TREADY;
wire M_TVALID;
wire [(DATA_COUNT*DATA_WIDTH)-1:0] M_TDATA;
biquad #(.DATA_WIDTH(DATA_WIDTH),
.DATA_COUNT(DATA_COUNT),
.COEFFICIENT_WIDTH(COEFFICIENT_WIDTH))
biquad (
.sysClk(sysClk),
.sysCoefficientStrobe(sysCoefficientStrobe),
.sysCoefficientAddress(sysCoefficientAddress),
.sysCoefficientValue(sysCoefficientValue),
.dataClk(dataClk),
.S_TDATA(S_TDATA),
.S_TVALID(S_TVALID),
.S_TREADY(S_TREADY),
.M_TDATA(M_TDATA),
.M_TVALID(M_TVALID),
.M_TREADY(1'b1));
always begin
#5 sysClk = !sysClk;
end
always begin
#4 dataClk = !dataClk;
end
integer pass = 1;
integer xCheck, x, xOld;
integer i;
initial begin
if ($test$plusargs("vcd")) begin
$dumpfile("biquad.vcd");
$dumpvars(4,biquad_tb);
end
#40;
$display("Unity gain");
setCoefficients(1, 0, 0, 0, 0);
check(-101, -101);
check(101, 101);
check({1'b1, {DATA_WIDTH-2{1'b0}}, 1'b1},
{1'b1, {DATA_WIDTH-2{1'b0}}, 1'b1});
$display("Unity gain, one sample delay");
setCoefficients(0, 1, 0, 0, 0);
check(-101, 0);
check(101, -101);
check({1'b1, {DATA_WIDTH-2{1'b0}}, 1'b1}, 101);
check(0, {1'b1, {DATA_WIDTH-2{1'b0}}, 1'b1});
$display("Unity gain, two sample delay");
setCoefficients(0, 0, 1, 0, 0);
check(-101, 0);
check(101, 0);
check({1'b1, {DATA_WIDTH-2{1'b0}}, 1'b1}, -101);
check(0, 101);
check(0, {1'b1, {DATA_WIDTH-2{1'b0}}, 1'b1});
$display("Boxcar average");
setCoefficients(1, 1, 1, 0, 0);
check(101, 101);
check(101, 202);
check(101, 303);
check(101, 303);
check(-101, 101);
check(-101, -101);
check(101, -101);
check(101, 101);
// Check overflow
check({1'b0, {DATA_WIDTH-1{1'b1}}}, {1'b0, {DATA_WIDTH-1{1'b1}}});
check({1'b0, {DATA_WIDTH-1{1'b1}}}, {1'b0, {DATA_WIDTH-1{1'b1}}});
check(0, {1'b0, {DATA_WIDTH-1{1'b1}}});
// Check exit from overflow
check(-1, {1'b0, {DATA_WIDTH-1{1'b1}}} - 1);
check(0, -1);
$display("Weighted average (FIR)");
setCoefficients(0.1, -0.25, 0.65, 0, 0);
check(100000000, 10000000);
check( 90000000,-16000000);
check( 80000000, 50500000);
check( 0, 38500000);
$display("First order low pass");
setCoefficients(0.25, 0, 0, -0.75, 0);
x = 16777216;
check(16777216 * 4, x);
for (i = 0 ; i < 20 ; i += 1) begin
x = x * 3 / 4;
check(0, x);
end
setCoefficients(0.125, 0, 0, -0.875, 0);
x = -67108864 / 8;
for (i = 0 ; i < 20 ; i += 1) begin
check(-67108864, x);
x = (-67108864 + x * 7) / 8;
end
$display("Second order low pass");
setCoefficients(0.5, 0, 0, -1, 0.5);
x = 0;
xOld = 0;
for (i = 0 ; i < 20 ; i += 1) begin
xCheck = 5000000 + x - (xOld / 2);
check(10000000, xCheck);
xOld = x;
x = xCheck;
end
#100;
$display("%s", pass ? "PASS" : "FAIL");
$finish;
end
task setCoefficients;
input real b0, b1, b2, a1, a2;
reg [COEFFICIENT_WIDTH-1:0] ib0, ib1, ib2, ia1, ia2;
begin
ib0 = (1 << (COEFFICIENT_WIDTH-2)) * b0;
ib1 = (1 << (COEFFICIENT_WIDTH-2)) * b1;
ib2 = (1 << (COEFFICIENT_WIDTH-2)) * b2;
ia1 = (1 << (COEFFICIENT_WIDTH-2)) * a1;
ia2 = (1 << (COEFFICIENT_WIDTH-2)) * a2;
@(posedge sysClk) begin
sysCoefficientAddress <= 0;
sysCoefficientValue <= ib0;
sysCoefficientStrobe <= 1;
end
@(posedge sysClk) begin
sysCoefficientAddress <= 1;
sysCoefficientValue <= ib1;
end
@(posedge sysClk) begin
sysCoefficientAddress <= 2;
sysCoefficientValue <= ib2;
end
@(posedge sysClk) begin
sysCoefficientAddress <= 3;
sysCoefficientValue <= -ia2;
end
@(posedge sysClk) begin
sysCoefficientAddress <= 4;
sysCoefficientValue <= -ia1;
end
@(posedge sysClk) begin
sysCoefficientAddress <= 7;
sysCoefficientValue <= {COEFFICIENT_WIDTH{1'bx}};
end
@(posedge sysClk) begin
sysCoefficientAddress <= {3{1'bx}};
sysCoefficientStrobe <= 0;
end
end
endtask
task check;
input signed [DATA_WIDTH-1:0] u, yGood;
reg [(DATA_COUNT*DATA_WIDTH)-1:0] dIn, dOutGood;
reg signed [DATA_WIDTH-1:0] yActual, diff;
integer i, match, near;
begin
for (i = 0 ; i < DATA_WIDTH ; i = i + 1) begin
dIn[i*DATA_WIDTH+:DATA_WIDTH] = u * $signed(i + 1);
dOutGood[i*DATA_WIDTH+:DATA_WIDTH] = yGood * $signed(i + 1);
end
@(posedge dataClk) begin
S_TDATA <= dIn;
S_TVALID <= 1;
end
while (!S_TREADY) #1;
@(posedge dataClk) begin
S_TDATA <= {DATA_COUNT*DATA_WIDTH{1'bx}};
S_TVALID <= 0;
end
while (!M_TVALID) @(posedge dataClk);
yActual = $signed(M_TDATA[DATA_WIDTH-1:0]);
if (M_TDATA == dOutGood) begin
match = 1;
near = 1;
end
else begin
match = 0;
diff = yGood - yActual;
if ($abs(diff) <= 4) begin
near = 1;
end
else begin
near = 0;
pass = 0;
end
end
$display("u:%d expect:%d got:%d dIn:%x dOutGood:%x dOut:%x %s", u, yGood,
yActual, dIn, dOutGood, M_TDATA,
match ? "PASS" : near ? "NEAR" : "FAIL");
@(posedge dataClk) ;
end
endtask
endmodule