-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcomplex_mul_flat_tb.v
67 lines (61 loc) · 1.56 KB
/
complex_mul_flat_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
`timescale 1ns / 1ns
module complex_mul_tb;
reg clk;
integer cc;
reg fail=0;
initial begin
if ($test$plusargs("vcd")) begin
$dumpfile("complex_mul_flat.vcd");
$dumpvars(5,complex_mul_tb);
end
for (cc=0; cc<50; cc=cc+1) begin
clk=0; #5;
clk=1; #5;
end
if (fail) begin
$display("FAIL");
$stop(0);
end else begin
$display("PASS");
$finish(0);
end
end
reg signed [17:0] x_I=0, x_Q=0, y_I=0, y_Q=0;
wire signed [17:0] z_I, z_Q;
reg signed [35:0] fi=0, fq=0, fi_d=0, fq_d=0, fi_d2=0, fq_d2=0; // reference results
always @(posedge clk) begin
x_I <= $random;
x_Q <= $random;
y_I <= $random;
y_Q <= $random;
fi <= x_I * y_I - x_Q * y_Q;
fq <= x_I * y_Q + y_I * x_Q;
fi_d <= fi;
fq_d <= fq;
fi_d2 <= fi_d;
fq_d2 <= fq_d;
end
wire g_out;
complex_mul_flat dut(.clk(clk), .gate_in(cc%4 == 0),
.x_I(x_I), .x_Q(x_Q),
.y_I(y_I), .y_Q(y_Q),
.z_I(z_I), .z_Q(z_Q), .gate_out(g_out));
reg signed [35:0] fi_test, fq_test, zx_I, zx_Q;
reg fault=0;
always @(negedge clk) if (cc>6 && g_out) begin
zx_I = z_I * 131072;
zx_Q = z_Q * 131072;
fi_test = fi_d2;
fq_test = fq_d2;
if (fi_test > 131072*131071) fi_test = 131072*131071;
if (fi_test < -131072*131072) fi_test = -131072*131072;
if (fq_test > 131072*131071) fq_test = 131072*131071;
if (fq_test < -131072*131072) fq_test = -131072*131072;
fault = (zx_I > fi_test+131072 || zx_I < fi_test-131072 ||
zx_Q > fq_test+131072 || zx_Q < fq_test-131072);
if (fault) fail=1;
$display("%d %d %d %d %d %d %s\n",
zx_I, fi_test, zx_I - fi_test,
zx_Q, fq_test, zx_Q - fq_test, fault ? "FAULT" : " .");
end
endmodule