File size: 8,154 Bytes
7df9186 | 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 | `timescale 1ns / 1ns
`include "constants.vams"
module cic_multichannel_tb;
// Configurable parameters
parameter n_chan = 12;
// Testbench stimulus
localparam ADC_DWI = 16;
localparam MULT_GUARD_BITS = 3; // Guard bits to keep in output of mixer
localparam OSC_WIDTH = 18; // Width of local oscillator
localparam CLK_PERIOD = 10;
localparam CLK_SLOW_RATIO = 2; // TODO: Make this programmable
localparam CLK_PER_SLOW = CLK_PERIOD*CLK_SLOW_RATIO;
integer den, logden;
reg [2:0] shift;
real fden, fnum, ampi, ampo_expect, phsi, phs_marg;
reg overload=0;
integer num_tx=0;
initial begin
// Test not designed to work with phsi near pi
// 4 <= den <= 128, no factor of fnum
if (!$value$plusargs("amp=%f", ampi)) ampi=10000.0;
if (!$value$plusargs("phs=%f", phsi)) phsi=0.0;
if (!$value$plusargs("den=%d", den )) den=16;
ampo_expect=ampi;
phs_marg=0.00002;
phs_marg=0.95/ampi;
if (ampi > 32765.0) begin
overload=1;
ampo_expect=32764.0;
phs_marg=0.2;
end
$display("ampi=%.2f ampo_expect=%.2f phsi=%.5f phs_marg=%.5f",
ampi, ampo_expect, phsi, phs_marg);
fden=den;
fnum=3.0;
logden=$clog2(den);
shift=logden-2;
$display("den=%d logden=%d shift=%d", den, logden, shift);
end
reg clk, slow_clk;
integer cc, errors;
initial begin
if ($test$plusargs("vcd")) begin
$dumpfile("cic_multichannel.vcd");
$dumpvars(5,cic_multichannel_tb);
end
errors=0;
$display(" X1 Y1 X2 Y2 R1 OK Phi1 OK");
for (cc=0; cc<64*den; cc=cc+1) begin
clk=0; #(CLK_PERIOD/2);
clk=1; #(CLK_PERIOD/2);
end
if (num_tx == 0) begin
$display("ERROR: No transactions, nothing was tested.");
errors = errors + 1;
end
if (errors == 0) begin
$display("PASS");
$finish(0);
end else begin
$display("FAIL");
$stop(0);
end
end
integer cc_s;
initial begin
for (cc_s=0; cc_s <64*den*CLK_SLOW_RATIO; cc_s = cc_s + 1) begin
slow_clk=0; #(CLK_PER_SLOW/2);
slow_clk=1; #(CLK_PER_SLOW/2);
end
end
// ---------------------
// Generate stimulus
// ---------------------
reg signed [ADC_DWI-1:0] adc=0;
integer noise;
integer nseed=1234;
integer ccmod;
real th0, tha, thb;
reg signed [OSC_WIDTH-1:0] cosa=0, sina=0, cosb=0, sinb=0;
reg signed [OSC_WIDTH-1:0] xcosa, xsina, xcosb, xsinb;
integer ax; // can be huge in the face of clipping. Don't be stupid and
// set amplitude larger than 2^31 in ADC sine wave below.
// 100 X overdrive is plenty for this purpose.
reg sample=0;
reg stb_in=0;
// Generate stimulus at half rate to test strobe capability
// Stimulus code based on mon_12_tb.v
// TODO: Make this configurable
always @(posedge clk) begin
stb_in <= ~stb_in;
end
always @(posedge slow_clk) begin
noise = $dist_normal(nseed,0,1024);
th0 = (cc_s)*`M_TWO_PI*fnum/fden - phsi;
ax = $floor(ampi*$cos(th0)+0.5+noise/1024.0);
if (ax > 32767) ax = 32767;
if (ax < -32678) ax = -32768;
adc <= ax;
// $display("%d adc", adc);
ccmod = cc_s%den;
tha = ccmod*`M_TWO_PI*fnum/fden;
// Scaling of LO is non-obvious. Set such that a square-wave input
// can't overflow CIC. Conceptually that's pi/2, so set to pi/4 of full
// scale and absorb a factor of two later.
// 2^17 = 131072 - a little bit to cover rounding
xcosa = $floor(131070.0*$cos(tha)+0.5); cosa <= xcosa;
xsina = $floor(131070.0*$sin(tha)+0.5); sina <= xsina;
thb = ccmod*`M_TWO_PI*7.0/fden;
xcosb = $floor(13107.00*$cos(tha)+0.5); cosb <= xcosb;
xsinb = $floor(13107.00*$sin(tha)+0.5); sinb <= xsinb;
sample <= ccmod==0;
end
// ---------------------
// Instantiate Mixers
// ---------------------
wire signed [ADC_DWI+MULT_GUARD_BITS-1:0] adc_a_cos, adc_a_sin, adc_b_cos, adc_b_sin;
mixer #(
.dwi(ADC_DWI),
.davr(MULT_GUARD_BITS),
.dwlo(OSC_WIDTH))
i_mixer_a_cos (
.clk(slow_clk),
.adcf(adc),
.mult(cosa),
.mixout(adc_a_cos)
);
mixer #(
.dwi(ADC_DWI),
.davr(MULT_GUARD_BITS),
.dwlo(OSC_WIDTH))
i_mixer_a_sin (
.clk(slow_clk),
.adcf(adc),
.mult(sina),
.mixout(adc_a_sin)
);
// ---------------------
// Instantiate Sampler
// ---------------------
wire cic_sample, cc_sample;
multi_sampler #(
.sample_period_wi(8),
.dsample0_en(1),
.dsample0_wi(8),
.dsample1_en(0),
.dsample1_wi(8),
.dsample2_en(0),
.dsample2_wi(8))
i_multi_sampler (
.clk(clk),
.reset(1'b0),
.ext_trig(1'b1),
.sample_period({den[6:0],1'b0}),
.dsample0_period(8'h1),
.dsample1_period(8'h1),
.dsample2_period(8'h1),
.sample_out(cic_sample),
.dsample0_stb(cc_sample),
.dsample1_stb(), // Unused output
.dsample2_stb() // Unused output
);
// ---------------------
// Instantiate DUT
// ---------------------
wire sr_val;
wire [19:0] sr_out;
wire [n_chan*(ADC_DWI+MULT_GUARD_BITS)-1:0] d_in_flat;
wire di_stb_out;
wire [31:0] di_sr_out;
assign d_in_flat = {{(n_chan-4)*(ADC_DWI+MULT_GUARD_BITS){1'b0}},
adc_a_sin,
adc_a_cos,
adc_a_sin,
adc_a_cos};
cic_multichannel #(
.n_chan (n_chan),
// DI parameters
.di_dwi (ADC_DWI+MULT_GUARD_BITS), // data width
.di_rwi (32), // result width
// Difference between above two widths should be N*log2 of the maximum number
// of samples per CIC sample, where N=2 is the order of the CIC filter.
.di_noise_bits (1), // NOTE: Setting to 1 to compensate for removed /2 from double_inte
.cc_outw (20), // CCFilt output width; Must be 20 if using half-band filter
.cc_halfband (1),
.cc_use_delay (0), // Match pipeline length of filt_halfband=1
.cc_shift_base (0)) // Bits to discard from previous acc step
dut
(
.clk (clk),
.reset (1'b0),
.stb_in (stb_in),
.d_in (d_in_flat), // Flattened array of unprocessed data streams. CH0 in LSBs
.cic_sample (cic_sample),
.cc_sample (cc_sample),
.cc_shift ({shift,1'b1}), // controls scaling of filter result
.di_stb_out (di_stb_out), // TODO: Test Double Integrator tap
.di_sr_out (di_sr_out),
.cc_stb_out (sr_val),
.cc_sr_out (sr_out)
);
reg strobe1=0;
integer col=0;
reg signed [19:0] out_set[0:n_chan-1];
always @(posedge clk) begin
strobe1 <= sr_val;
if (sr_val) begin
col <= (col==n_chan-1) ? 0 : col+1;
out_set[col] <= sr_out;
// $display("%d: out[%d] <= %d", cc, col, result);
end
end
real xr, xi, ampo, phso;
reg amp_pass, phs_pass, fault, use_row;
always @(negedge clk) if (cc_s/den > 18) begin
//if (strobe & ~strobe1) $display("#");
//if (strobe) $display("%d", result);
xr=out_set[0];
xi=out_set[1];
ampo=$sqrt(xr*xr+xi*xi)/(fden*fden)*(1<<(2*shift));
ampo=ampo*131072.0/131070.0;
phso=$atan2(xi,xr);
amp_pass = overload ? ampo > ampo_expect :
((ampo > ampo_expect*0.99995-0.7) & (ampo < ampo_expect*1.00005+0.7));
phs_pass = (phso>phsi-phs_marg) & (phso<phsi+phs_marg);
if (sr_val && (col==0)) begin
num_tx <= num_tx + 1;
$display("%d %d %d %d %8.2f %b %8.5f %b %s.",
out_set[0], out_set[1], out_set[2], out_set[3],
ampo, amp_pass, phso, phs_pass, fault ? "FAULT" : "");
fault = (~amp_pass | ~phs_pass);
if (fault) errors=errors+1;
end
end
endmodule
|