OpenCores
URL https://opencores.org/ocsvn/dblclockfft/dblclockfft/trunk

Subversion Repositories dblclockfft

[/] [dblclockfft/] [trunk/] [bench/] [cpp/] [ifft_tb.cpp] - Blame information for rev 30

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 dgisselq
//
2
// Filename:    ifft_tb.cpp
3
//
4
// Project:     A Doubletime Pipelined FFT
5
//
6
// Purpose:     A test-bench for the combined work of both fftmain.v and
7
//              ifftmain.v.  If they work together, in concert like they should,
8
//              then the operation of both in series should yield an identity.
9
//              This program attempts to check that identity with various
10
//              inputs given to it.
11
//
12
//              This file has a variety of dependencies, not the least of which
13
//              are verilator, ifftmain.v and fftmain.v (both produced by
14
//              fftgen), but also on the ifft_tb.v verilog test bench found
15
//              within this directory.
16
//
17
// Creator:     Dan Gisselquist, Ph.D.
18 30 dgisselq
//              Gisselquist Technology, LLC
19 14 dgisselq
//
20
///////////////////////////////////////////////////////////////////////////
21
//
22
// Copyright (C) 2015, Gisselquist Technology, LLC
23
//
24
// This program is free software (firmware): you can redistribute it and/or
25
// modify it under the terms of  the GNU General Public License as published
26
// by the Free Software Foundation, either version 3 of the License, or (at
27
// your option) any later version.
28
//
29
// This program is distributed in the hope that it will be useful, but WITHOUT
30
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
31
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
32
// for more details.
33
//
34
// You should have received a copy of the GNU General Public License along
35
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
36
// target there if the PDF file isn't present.)  If not, see
37
// <http://www.gnu.org/licenses/> for a copy.
38
//
39
// License:     GPL, v3, as defined and found on www.gnu.org,
40
//              http://www.gnu.org/licenses/gpl.html
41
//
42
//
43
///////////////////////////////////////////////////////////////////////////
44
#include <stdio.h>
45
#include <math.h>
46
#include <assert.h>
47
 
48
#include "verilated.h"
49
#include "Vifft_tb.h"
50 23 dgisselq
#include "twoc.h"
51 14 dgisselq
 
52
#define LGWIDTH 11
53
#define IWIDTH  16
54
#define MWIDTH  22
55
#define OWIDTH  28
56
 
57
#define FFTLEN  (1<<LGWIDTH)
58
 
59
class   IFFT_TB {
60
public:
61
        Vifft_tb        *m_tb;
62
        unsigned int    m_log[8*FFTLEN];
63
        long            m_data[2*FFTLEN];
64
        int             m_iaddr, m_oaddr, m_offset;
65
        FILE            *m_dumpfp;
66
        // double               *m_tb_buf;
67
        // int                  m_ntest;
68
        bool            m_syncd;
69
 
70
        IFFT_TB(void) {
71
                m_tb = new Vifft_tb;
72
                m_iaddr = m_oaddr = 0;
73
                m_dumpfp = NULL;
74
 
75
                m_syncd = false;
76
                // m_ntest = 0;
77
        }
78
 
79
        void    tick(void) {
80
                m_tb->i_clk = 0;
81
                m_tb->eval();
82
                m_tb->i_clk = 1;
83
                m_tb->eval();
84
        }
85
 
86
        void    reset(void) {
87
                m_tb->i_ce  = 0;
88
                m_tb->i_rst = 1;
89
                tick();
90
                m_tb->i_rst = 0;
91
                tick();
92
 
93
                m_iaddr = m_oaddr = 0;
94
                m_syncd = false;
95
        }
96
 
97
        long    twos_complement(const long val, const int bits) {
98 23 dgisselq
                return sbits(val, bits);
99 14 dgisselq
        }
100
 
101
        void    checkresults(void) {
102
        /*
103
                double  *dp, *sp; // Complex array
104
                double  vout[FFTLEN*2];
105
                double  isq=0.0, osq = 0.0;
106
                long    *lp;
107
 
108
                // Fill up our test array from the log array
109
                printf("%3d : CHECK: %8d %5x\n", m_ntest, m_iaddr, m_iaddr);
110
                dp = m_tb_buf; lp = &m_log[(m_iaddr-FFTLEN*3)&((4*FFTLEN-1)&(-FFTLEN))];
111
                for(int i=0; i<FFTLEN; i++) {
112
                        long    tv = *lp++;
113
 
114
                        dp[0] = twos_complement(tv >> IWIDTH, IWIDTH);
115
                        dp[1] = twos_complement(tv, IWIDTH);
116
 
117
                        printf("IN[%4d = %4x] = %9.1f %9.1f\n",
118
                                i+((m_iaddr-FFTLEN*3)&((4*FFTLEN-1)&(-FFTLEN))),
119
                                i+((m_iaddr-FFTLEN*3)&((4*FFTLEN-1)&(-FFTLEN))),
120
                                dp[0], dp[1]);
121
                        dp += 2;
122
                }
123
 
124
                // Let's measure ... are we the zero vector?  If not, how close?
125
                dp = m_tb_buf;
126
                for(int i=0; i<FFTLEN; i++)
127
                        isq += (*dp) * (*dp);
128
 
129
                fftw_execute(m_plan);
130
 
131
                // Let's load up the output we received into vout
132
                dp = vout;
133
                for(int i=0; i<FFTLEN; i++) {
134
                        long    tv = m_data[i];
135
 
136
                        printf("OUT[%4d = %4x] = ", i, i);
137
                        printf("%16lx = ", tv);
138
                        *dp = twos_complement(tv >> OWIDTH, OWIDTH);
139
                        printf("%12.1f + ", *dp);
140
                        osq += (*dp) * (*dp); dp++;
141
                        *dp = twos_complement(tv, OWIDTH);
142
                        printf("%12.1f j", *dp);
143
                        osq += (*dp) * (*dp); dp++;
144
                        printf(" <-> %12.1f %12.1f\n", m_tb_buf[2*i], m_fft_buf[2*i+1]);
145
                }
146
 
147
 
148
                // Let's figure out if there's a scale factor difference ...
149
                double  scale = 0.0, wt = 0.0;
150
                sp = m_tb_buf;  dp = vout;
151
                for(int i=0; i<FFTLEN*2; i++) {
152
                        scale += (*sp) * (*dp++);
153
                        wt += (*sp) * (*sp); sp++;
154
                } scale = scale / wt;
155
 
156
                if (wt == 0.0) scale = 1.0;
157
 
158
                double xisq = 0.0;
159
                sp = m_tb_buf;  dp = vout;
160
                for(int i=0; i<FFTLEN*2; i++) {
161
                        double vl = (*sp++) * scale - (*dp++);
162
                        xisq += vl * vl;
163
                }
164
 
165
                printf("%3d : SCALE = %12.6f, WT = %18.1f, ISQ = %15.1f, ",
166
                        m_ntest, scale, wt, isq);
167
                printf("OSQ = %18.1f, ", osq);
168
                printf("XISQ = %18.1f\n", xisq);
169
                m_ntest++;
170
                */
171
        }
172
 
173
        bool    test(int lft, int rht) {
174
                m_tb->i_ce    = 1;
175
                m_tb->i_rst   = 0;
176
                m_tb->i_left  = lft;
177
                m_tb->i_right = rht;
178
 
179
                m_log[(m_iaddr++)&(8*FFTLEN-1)] = lft;
180
                m_log[(m_iaddr++)&(8*FFTLEN-1)] = rht;
181
 
182
                tick();
183
 
184
                if ((m_tb->o_sync)&&(!m_syncd)) {
185
                        m_offset = m_iaddr;
186
                        m_oaddr = 0;
187
                        m_syncd = true;
188
                }
189
 
190
                m_data[(m_oaddr++)&(FFTLEN-1)] = m_tb->o_left;
191
                m_data[(m_oaddr++)&(FFTLEN-1)] = m_tb->o_right;
192
 
193
                if ((m_syncd)&&((m_oaddr&(FFTLEN-1)) == 0)) {
194
                        dumpwrite();
195
                        // checkresults();
196
                }
197
 
198
                return (m_tb->o_sync);
199
        }
200
 
201
        bool    test(double lft_r, double lft_i, double rht_r, double rht_i) {
202
                int     ilft, irht, ilft_r, ilft_i, irht_r, irht_i;
203
 
204
                assert(2*IWIDTH <= 32);
205
                ilft_r = (int)(lft_r) & ((1<<IWIDTH)-1);
206
                ilft_i = (int)(lft_i) & ((1<<IWIDTH)-1);
207
                irht_r = (int)(rht_r) & ((1<<IWIDTH)-1);
208
                irht_i = (int)(rht_i) & ((1<<IWIDTH)-1);
209
 
210
                ilft = (ilft_r << IWIDTH) | ilft_i;
211
                irht = (irht_r << IWIDTH) | irht_i;
212
 
213
                return test(ilft, irht);
214
        }
215
 
216
        double  rdata(int addr) {
217
                long    ivl = m_data[addr & (FFTLEN-1)];
218
 
219
                ivl = twos_complement(ivl >> OWIDTH, OWIDTH);
220
                return (double)ivl;
221
        }
222
 
223
        double  idata(int addr) {
224
                long    ivl = m_data[addr & (FFTLEN-1)];
225
 
226
                ivl = twos_complement(ivl, OWIDTH);
227
                return (double)ivl;
228
        }
229
 
230
        void    dump(FILE *fp) {
231
                m_dumpfp = fp;
232
        }
233
 
234
        void    dumpwrite(void) {
235
                if (!m_dumpfp)
236
                        return;
237
 
238
                double  *buf;
239
 
240
                buf = new double[FFTLEN * 2];
241
                for(int i=0; i<FFTLEN; i++) {
242
                        buf[i*2] = rdata(i);
243
                        buf[i*2+1] = idata(i);
244
                }
245
 
246
                fwrite(buf, sizeof(double), FFTLEN*2, m_dumpfp);
247
                delete[] buf;
248
        }
249
};
250
 
251
 
252
int     main(int argc, char **argv, char **envp) {
253
        Verilated::commandArgs(argc, argv);
254
        IFFT_TB *tb = new IFFT_TB;
255
        FILE    *fpout;
256
 
257
        fpout = fopen("ifft_tb.dbl", "w");
258
        if (NULL == fpout) {
259
                fprintf(stderr, "Cannot write output file, fft_tb.dbl\n");
260
                exit(-1);
261
        }
262
 
263
        tb->reset();
264
        tb->dump(fpout);
265
 
266
        //     1 -> 0x0001 
267
        //     2 -> 0x0002 
268
        //     4 -> 0x0004 
269
        //     8 -> 0x0008 
270
        //    16 -> 0x0010 
271
        //    32 -> 0x0020 
272
        //    64 -> 0x0040 
273
        //   128 -> 0x0080 
274
        //   256 -> 0x0100 
275
        //   512 -> 0x0200 
276
        //  1024 -> 0x0400 
277
        //  2048 -> 0x0800
278
        //  4096 -> 0x1000
279
        //  8192 -> 0x2000
280
        // 16384 -> 0x4000
281
        for(int v=1; v<32768; v<<=1) for(int k=0; k<FFTLEN/2; k++)
282
                tb->test((double)v,0.0,(double)v,0.0);
283
        //     1 -> 0xffff      
284
        //     2 -> 0xfffe
285
        //     4 -> 0xfffc
286
        //     8 -> 0xfff8
287
        //    16 -> 0xfff0
288
        //    32 -> 0xffe0
289
        //    64 -> 0xffc0
290
        //   128 -> 0xff80
291
        //   256 -> 0xff00
292
        //   512 -> 0xfe00
293
        //  1024 -> 0xfc00
294
        //  2048 -> 0xf800
295
        //  4096 -> 0xf000
296
        //  8192 -> 0xe000
297
        // 16384 -> 0xc000
298
        // 32768 -> 0x8000
299
        for(int v=1; v<=32768; v<<=1) for(int k=0; k<FFTLEN/2; k++)
300
                tb->test(-(double)v,0.0,-(double)v,0.0);
301
        //     1 -> 0x000040    CORRECT!!
302
        //     2 -> 0x000080 
303
        //     4 -> 0x000100 
304
        //     8 -> 0x000200
305
        //    16 -> 0x000400
306
        //    32 -> 0x000800
307
        //    64 -> 0x001000
308
        //   128 -> 0x002000
309
        //   256 -> 0x004000
310
        //   512 -> 0x008000
311
        //  1024 -> 0x010000
312
        //  2048 -> 0x020000
313
        //  4096 -> 0x040000
314
        //  8192 -> 0x080000
315
        // 16384 -> 0x100000
316
        for(int v=1; v<32768; v<<=1) for(int k=0; k<FFTLEN/2; k++)
317
                tb->test(0.0,(double)v,0.0,(double)v);
318
        //     1 -> 0x3fffc0
319
        //     2 -> 0x3fff80
320
        //     4 -> 0x3fff00
321
        //     8 -> 0x3ffe00
322
        //    16 -> 0x3ffc00
323
        //    32 -> 0x3ff800
324
        //    64 -> 0x3ff000
325
        //   128 -> 0x3fe000
326
        //   256 -> 0x3fc000
327
        //   512 -> 0x3f8000
328
        //  1024 -> 0x3f0000
329
        //  2048 -> 0x3e0000
330
        //  4096 -> 0x3c0000
331
        //  8192 -> 0x380000
332
        // 16384 -> 0x300000
333
        for(int v=1; v<32768; v<<=1) for(int k=0; k<FFTLEN/2; k++)
334
                tb->test(0.0,-(double)v,0.0,-(double)v);
335
 
336
        // 61. Now, how about the smallest alternating real signal
337
        for(int k=0; k<FFTLEN/2; k++)
338
                tb->test(2.0,0.0,0.0,0.0); // Don't forget to expect a bias!
339
        // 62. Now, how about the smallest alternating imaginary signal
340
        for(int k=0; k<FFTLEN/2; k++)
341
                tb->test(0.0,2.0,0.0,0.0); // Don't forget to expect a bias!
342
        // 63. Now, how about the smallest alternating real signal,2nd phase
343
        for(int k=0; k<FFTLEN/2; k++)
344
                tb->test(0.0,0.0,2.0,0.0); // Don't forget to expect a bias!
345
        // 64.Now, how about the smallest alternating imaginary signal,2nd phase
346
        for(int k=0; k<FFTLEN/2; k++)
347
                tb->test(0.0,0.0,0.0,2.0); // Don't forget to expect a bias!
348
 
349
        // 65.
350
        for(int k=0; k<FFTLEN/2; k++)
351
                tb->test(32767.0,0.0,-32767.0,0.0);
352
        // 66.
353
        for(int k=0; k<FFTLEN/2; k++)
354
                tb->test(0.0,-32767.0,0.0,32767.0);
355
        // 67.
356
        for(int k=0; k<FFTLEN/2; k++)
357
                tb->test(-32768.0,-32768.0,-32768.0,-32768.0);
358
        // 68.
359
        for(int k=0; k<FFTLEN/2; k++)
360
                tb->test(0.0,-32767.0,0.0,32767.0);
361
        // 69.
362
        for(int k=0; k<FFTLEN/2; k++)
363
                tb->test(0.0,32767.0,0.0,-32767.0);
364
        // 70. 
365
        for(int k=0; k<FFTLEN/2; k++)
366
                tb->test(-32768.0,-32768.0,-32768.0,-32768.0);
367
 
368
        // 71. Now let's go for an impulse (SUCCESS)
369
        tb->test(16384.0, 0.0, 0.0, 0.0);
370
        for(int k=0; k<FFTLEN/2-1; k++)
371
                tb->test(0.0,0.0,0.0,0.0);
372
 
373
        // 72. And another one on the next clock (FAILS, ugly)
374
        //      Lot's of roundoff error, or some error in small bits
375
        tb->test(0.0, 0.0, 16384.0, 0.0);
376
        for(int k=0; k<FFTLEN/2-1; k++)
377
                tb->test(0.0,0.0,0.0,0.0);
378
 
379
        // 73. And an imaginary one on the second clock
380
        //      Much roundoff error, as in last test
381
        tb->test(0.0, 0.0, 0.0, 16384.0);
382
        for(int k=0; k<FFTLEN/2-1; k++)
383
                tb->test(0.0,0.0,0.0,0.0);
384
 
385
        // 74. Likewise the next clock
386
        //      Much roundoff error, as in last test
387
        tb->test(0.0,0.0,0.0,0.0);
388
        tb->test(16384.0, 0.0, 0.0, 0.0);
389
        for(int k=0; k<FFTLEN/2-2; k++)
390
                tb->test(0.0,0.0,0.0,0.0);
391
 
392
        // 75. And it's imaginary counterpart
393
        //      Much roundoff error, as in last test
394
        tb->test(0.0,0.0,0.0,0.0);
395
        tb->test(0.0, 16384.0, 0.0, 0.0);
396
        for(int k=0; k<FFTLEN/2-2; k++)
397
                tb->test(0.0,0.0,0.0,0.0);
398
 
399
        // 76. Likewise the next clock
400
        //      Much roundoff error, as in last test
401
        tb->test(0.0,0.0,0.0,0.0);
402
        tb->test(0.0, 0.0, 16384.0, 0.0);
403
        for(int k=0; k<FFTLEN/2-2; k++)
404
                tb->test(0.0,0.0,0.0,0.0);
405
 
406
        // 77. And it's imaginary counterpart
407
        //      Much roundoff error, as in last test
408
        tb->test(0.0,0.0,0.0,0.0);
409
        tb->test(0.0, 0.0, 0.0, 16384.0);
410
        for(int k=0; k<FFTLEN/2-2; k++)
411
                tb->test(0.0,0.0,0.0,0.0);
412
 
413
 
414
        // 78. Now let's try some exponentials
415
        for(int k=0; k<FFTLEN/2; k++) {
416
                double cl, cr, sl, sr, W;
417
                W = - 2.0 * M_PI / FFTLEN;
418
                cl = cos(W * (2*k  )) * 16383.0;
419
                sl = sin(W * (2*k  )) * 16383.0;
420
                cr = cos(W * (2*k+1)) * 16383.0;
421
                sr = sin(W * (2*k+1)) * 16383.0;
422
                tb->test(cl, sl, cr, sr);
423
        }
424
 
425
        // 79.
426
        for(int k=0; k<FFTLEN/2; k++) {
427
                double cl, cr, sl, sr, W;
428
                W = - 2.0 * M_PI / FFTLEN * 5;
429
                cl = cos(W * (2*k  )) * 16383.0;
430
                sl = sin(W * (2*k  )) * 16383.0;
431
                cr = cos(W * (2*k+1)) * 16383.0;
432
                sr = sin(W * (2*k+1)) * 16383.0;
433
                tb->test(cl, sl, cr, sr);
434
        }
435
 
436
        // 80.
437
        for(int k=0; k<FFTLEN/2; k++) {
438
                double cl, cr, sl, sr, W;
439
                W = - 2.0 * M_PI / FFTLEN * 8;
440
                cl = cos(W * (2*k  )) * 8190.0;
441
                sl = sin(W * (2*k  )) * 8190.0;
442
                cr = cos(W * (2*k+1)) * 8190.0;
443
                sr = sin(W * (2*k+1)) * 8190.0;
444
                tb->test(cl, sl, cr, sr);
445
        }
446
 
447
        // 81.
448
        for(int k=0; k<FFTLEN/2; k++) {
449
                double cl, cr, sl, sr, W;
450
                W = - 2.0 * M_PI / FFTLEN * 25;
451
                cl = cos(W * (2*k  )) * 4.0;
452
                sl = sin(W * (2*k  )) * 4.0;
453
                cr = cos(W * (2*k+1)) * 4.0;
454
                sr = sin(W * (2*k+1)) * 4.0;
455
                tb->test(cl, sl, cr, sr);
456
        }
457
 
458
        // 19.--24. And finally, let's clear out our results / buffer
459
        for(int k=0; k<(FFTLEN/2) * 5; k++)
460
                tb->test(0.0,0.0,0.0,0.0);
461
 
462
        fclose(fpout);
463
}
464
 
465
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.