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

Subversion Repositories dblclockfft

[/] [dblclockfft/] [trunk/] [bench/] [cpp/] [butterfly_tb.cpp] - Blame information for rev 36

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

Line No. Rev Author Line
1 6 dgisselq
////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    butterfly_tb.cpp
4
//
5
// Project:     A Doubletime Pipelined FFT
6
//
7 36 dgisselq
// Purpose:     A test-bench for the butterfly.v subfile of the generic
8
//              pipelined FFT.  This file may be run autonomously.  If so,
9
//      the last line output will either read "SUCCESS" on success, or some
10
//      other failure message otherwise.
11 6 dgisselq
//
12 36 dgisselq
//      This file depends upon verilator to both compile, run, and therefore
13
//      test butterfly.v
14 6 dgisselq
//
15
// Creator:     Dan Gisselquist, Ph.D.
16 30 dgisselq
//              Gisselquist Technology, LLC
17 6 dgisselq
//
18
///////////////////////////////////////////////////////////////////////////
19
//
20 36 dgisselq
// Copyright (C) 2015,2018 Gisselquist Technology, LLC
21 6 dgisselq
//
22
// This program is free software (firmware): you can redistribute it and/or
23
// modify it under the terms of  the GNU General Public License as published
24
// by the Free Software Foundation, either version 3 of the License, or (at
25
// your option) any later version.
26
//
27
// This program is distributed in the hope that it will be useful, but WITHOUT
28
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
29
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
30
// for more details.
31
//
32
// You should have received a copy of the GNU General Public License along
33
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
34
// target there if the PDF file isn't present.)  If not, see
35
// <http://www.gnu.org/licenses/> for a copy.
36
//
37
// License:     GPL, v3, as defined and found on www.gnu.org,
38
//              http://www.gnu.org/licenses/gpl.html
39
//
40
//
41
///////////////////////////////////////////////////////////////////////////
42 3 dgisselq
#include <stdio.h>
43
#include <stdint.h>
44
 
45 36 dgisselq
#include "verilated.h"
46
#include "verilated_vcd_c.h"
47 3 dgisselq
#include "Vbutterfly.h"
48 23 dgisselq
#include "twoc.h"
49 36 dgisselq
#include "fftsize.h"
50 3 dgisselq
 
51 36 dgisselq
#ifdef  NEW_VERILATOR
52
#define VVAR(A) butterfly__DOT__ ## A
53
#else
54
#define VVAR(A) v__DOT_ ## A
55
#endif
56
 
57 29 dgisselq
#define IWIDTH  TST_BUTTERFLY_IWIDTH
58
#define CWIDTH  TST_BUTTERFLY_CWIDTH
59
#define OWIDTH  TST_BUTTERFLY_OWIDTH
60
#define BFLYDELAY       TST_BUTTERFLY_MPYDELAY
61 15 dgisselq
 
62 5 dgisselq
class   BFLY_TB {
63
public:
64
        Vbutterfly      *m_bfly;
65 36 dgisselq
        VerilatedVcdC   *m_trace;
66 5 dgisselq
        unsigned long   m_left[64], m_right[64];
67 6 dgisselq
        bool            m_aux[64];
68 13 dgisselq
        int             m_addr, m_lastaux, m_offset;
69 23 dgisselq
        bool            m_syncd, m_waiting_for_sync_input;
70 36 dgisselq
        uint64_t        m_tickcount;
71 3 dgisselq
 
72 5 dgisselq
        BFLY_TB(void) {
73 36 dgisselq
                Verilated::traceEverOn(true);
74
                m_trace = NULL;
75 5 dgisselq
                m_bfly = new Vbutterfly;
76
                m_addr = 0;
77 6 dgisselq
                m_syncd = 0;
78 36 dgisselq
                m_tickcount = 0;
79 23 dgisselq
                m_waiting_for_sync_input = true;
80 5 dgisselq
        }
81 3 dgisselq
 
82 36 dgisselq
        void    opentrace(const char *vcdname) {
83
                if (!m_trace) {
84
                        m_trace = new VerilatedVcdC;
85
                        m_bfly->trace(m_trace, 99);
86
                        m_trace->open(vcdname);
87
                }
88
        }
89
 
90
        void    closetrace(void) {
91
                if (m_trace) {
92
                        m_trace->close();
93
                        delete  m_trace;
94
                        m_trace = NULL;
95
                }
96
        }
97
 
98 5 dgisselq
        void    tick(void) {
99 36 dgisselq
                m_tickcount++;
100
 
101 5 dgisselq
                m_lastaux = m_bfly->o_aux;
102
                m_bfly->i_clk = 0;
103
                m_bfly->eval();
104 36 dgisselq
                if (m_trace) m_trace->dump((uint64_t)(10ul*m_tickcount-2));
105 5 dgisselq
                m_bfly->i_clk = 1;
106
                m_bfly->eval();
107 36 dgisselq
                if (m_trace) m_trace->dump((uint64_t)(10ul*m_tickcount));
108
                m_bfly->i_clk = 0;
109
                m_bfly->eval();
110
                if (m_trace) {
111
                        m_trace->dump((uint64_t)(10ul*m_tickcount+5));
112
                        m_trace->flush();
113
                }
114 6 dgisselq
 
115 13 dgisselq
                if ((!m_syncd)&&(m_bfly->o_aux))
116
                        m_offset = m_addr;
117 6 dgisselq
                m_syncd = (m_syncd) || (m_bfly->o_aux);
118 5 dgisselq
        }
119
 
120 36 dgisselq
        void    cetick(void) {
121
                int     ce = m_bfly->i_ce, nkce;
122
 
123
                tick();
124
 
125
                nkce = (rand()&1);
126
#ifdef  FFT_CKPCE
127
                nkce += FFT_CKPCE;
128
#endif
129
 
130
                if ((ce)&&(nkce > 0)) {
131
                        m_bfly->i_ce = 0;
132
                        for(int kce=0; kce<nkce-1; kce++)
133
                                tick();
134
                }
135
 
136
                m_bfly->i_ce = ce;
137
        }
138
 
139 5 dgisselq
        void    reset(void) {
140
                m_bfly->i_ce    = 0;
141 36 dgisselq
                m_bfly->i_reset = 1;
142 5 dgisselq
                m_bfly->i_coef  = 0l;
143
                m_bfly->i_left  = 0;
144
                m_bfly->i_right = 0;
145
                tick();
146 36 dgisselq
                m_bfly->i_reset = 0;
147 5 dgisselq
                m_bfly->i_ce  = 1;
148 6 dgisselq
                //
149
                // Let's run a RESET test here, forcing the whole butterfly
150
                // to be filled with aux=1.  If the reset works right,
151
                // we'll never get an aux=1 output.
152
                //
153 36 dgisselq
                m_bfly->i_reset = 1;
154 5 dgisselq
                m_bfly->i_aux = 1;
155 36 dgisselq
                m_bfly->i_ce  = 1;
156
                for(int i=0; i<200; i++)
157
                        cetick();
158 5 dgisselq
 
159 6 dgisselq
                // Now here's the RESET line, so let's see what the test does
160 36 dgisselq
                m_bfly->i_reset = 1;
161 6 dgisselq
                m_bfly->i_ce  = 1;
162
                m_bfly->i_aux = 1;
163 36 dgisselq
                cetick();
164
                m_bfly->i_reset = 0;
165 6 dgisselq
                m_syncd = 0;
166 23 dgisselq
 
167
                m_waiting_for_sync_input = true;
168 5 dgisselq
        }
169
 
170
        void    test(const int n, const int k, const unsigned long cof,
171
                        const unsigned lft, const unsigned rht, const int aux) {
172
 
173 29 dgisselq
                m_bfly->i_coef  = ubits(cof, 2*TST_BUTTERFLY_CWIDTH);
174
                m_bfly->i_left  = ubits(lft, 2*TST_BUTTERFLY_IWIDTH);
175
                m_bfly->i_right = ubits(rht, 2*TST_BUTTERFLY_IWIDTH);
176 5 dgisselq
                m_bfly->i_aux   = aux & 1;
177 23 dgisselq
                if ((m_waiting_for_sync_input)&&(aux&1)) {
178
                        m_waiting_for_sync_input = false;
179
                        m_addr = 0;
180
                }
181 5 dgisselq
 
182 26 dgisselq
                m_bfly->i_ce = 1;
183 36 dgisselq
                cetick();
184 5 dgisselq
 
185
                if ((m_bfly->o_aux)&&(!m_lastaux))
186
                        printf("\n");
187 36 dgisselq
                printf("n,k=%d,%3d: COEF=%0*lx, LFT=%0*x, RHT=%0*x, A=%d, OLFT =%0*lx, ORHT=%0*lx, AUX=%d\n",
188 5 dgisselq
                        n,k,
189 36 dgisselq
                        (2*CWIDTH+3)/4, ubits(m_bfly->i_coef, 2*CWIDTH),
190
                        (2*IWIDTH+3)/4, m_bfly->i_left,
191
                        (2*IWIDTH+3)/4, m_bfly->i_right,
192 5 dgisselq
                        m_bfly->i_aux,
193 36 dgisselq
                        (2*OWIDTH+3)/4, (long)m_bfly->o_left,
194
                        (2*OWIDTH+3)/4, (long)m_bfly->o_right,
195 5 dgisselq
                        m_bfly->o_aux);
196
 
197 13 dgisselq
                if ((m_syncd)&&(m_left[(m_addr-m_offset)&(64-1)] != m_bfly->o_left)) {
198 36 dgisselq
                        printf("WRONG O_LEFT! (%lx(exp) != %lx(sut)\n",
199 15 dgisselq
                                m_left[(m_addr-m_offset)&(64-1)],
200 36 dgisselq
                                (long)m_bfly->o_left);
201
                        exit(EXIT_FAILURE);
202 5 dgisselq
                }
203
 
204 13 dgisselq
                if ((m_syncd)&&(m_right[(m_addr-m_offset)&(64-1)] != m_bfly->o_right)) {
205 36 dgisselq
                        printf("WRONG O_RIGHT! (%lx(exp) != %lx(sut))\n",
206
                                m_right[(m_addr-m_offset)&(64-1)],
207
                                (long)m_bfly->o_right);
208
                        exit(EXIT_FAILURE);
209 5 dgisselq
                }
210
 
211 13 dgisselq
                if ((m_syncd)&&(m_aux[(m_addr-m_offset)&(64-1)] != m_bfly->o_aux)) {
212 29 dgisselq
                        printf("FAILED AUX CHANNEL TEST (i.e. the SYNC)\n");
213 36 dgisselq
                        exit(EXIT_FAILURE);
214 6 dgisselq
                }
215
 
216 29 dgisselq
                if ((m_addr > TST_BUTTERFLY_MPYDELAY+6)&&(!m_syncd)) {
217
                        printf("NO SYNC PULSE!\n");
218 36 dgisselq
                        exit(EXIT_FAILURE);
219 6 dgisselq
                }
220
 
221 5 dgisselq
                // Now, let's calculate an "expected" result ...
222
                long    rlft, ilft;
223
 
224
                // Extract left and right values ...
225 29 dgisselq
                rlft = sbits(m_bfly->i_left >> IWIDTH, IWIDTH);
226
                ilft = sbits(m_bfly->i_left          , IWIDTH);
227 5 dgisselq
 
228
                // Now repeat for the right hand value ...
229
                long    rrht, irht;
230
                // Extract left and right values ...
231 29 dgisselq
                rrht = sbits(m_bfly->i_right >> IWIDTH, IWIDTH);
232
                irht = sbits(m_bfly->i_right          , IWIDTH);
233 5 dgisselq
 
234
                // and again for the coefficients
235
                long    rcof, icof;
236
                // Extract left and right values ...
237 29 dgisselq
                rcof = sbits(m_bfly->i_coef >> CWIDTH, CWIDTH);
238
                icof = sbits(m_bfly->i_coef          , CWIDTH);
239 5 dgisselq
 
240
                // Now, let's do the butterfly ourselves ...
241
                long sumi, sumr, difi, difr;
242
                sumr = rlft + rrht;
243
                sumi = ilft + irht;
244
                difr = rlft - rrht;
245
                difi = ilft - irht;
246
 
247
        /*
248
                printf("L=%5lx+%5lx,R=%5lx+%5lx,S=%5lx+%5lx,D=%5lx+%5lx, ",
249
                        rlft & 0x02ffffl,
250
                        ilft & 0x02ffffl,
251
                        rrht & 0x02ffffl,
252
                        irht & 0x02ffffl,
253
                        sumr & 0x02ffffl,
254
                        sumi & 0x02ffffl,
255
                        difr & 0x02ffffl,
256
                        difi & 0x02ffffl);
257
        */
258
                long p1, p2, p3, mpyr, mpyi;
259
                p1 = difr * rcof;
260
                p2 = difi * icof;
261
                p3 = (difr + difi) * (rcof + icof);
262
 
263 29 dgisselq
                mpyr = p1-p2;
264
                mpyi = p3-p1-p2;
265 5 dgisselq
 
266 29 dgisselq
                mpyr = rndbits(mpyr, (IWIDTH+2)+(CWIDTH+1), OWIDTH+4);
267
                mpyi = rndbits(mpyi, (IWIDTH+2)+(CWIDTH+1), OWIDTH+4);
268
 
269 5 dgisselq
        /*
270
                printf("RC=%lx, IC=%lx, ", rcof, icof);
271
                printf("P1=%lx,P2=%lx,P3=%lx, ", p1,p2,p3);
272
                printf("MPYr = %lx, ", mpyr);
273
                printf("MPYi = %lx, ", mpyi);
274
        */
275
 
276
                long    o_left_r, o_left_i, o_right_r, o_right_i;
277
                unsigned long   o_left, o_right;
278
 
279 29 dgisselq
                o_left_r = rndbits(sumr<<(CWIDTH-2), CWIDTH+IWIDTH+3, OWIDTH+4);
280
                        o_left_r = ubits(o_left_r, OWIDTH);
281
                o_left_i = rndbits(sumi<<(CWIDTH-2), CWIDTH+IWIDTH+3, OWIDTH+4);
282
                        o_left_i = ubits(o_left_i, OWIDTH);
283
                o_left = (o_left_r << OWIDTH) | (o_left_i);
284 5 dgisselq
 
285 29 dgisselq
                o_right_r = ubits(mpyr, OWIDTH);
286
                o_right_i = ubits(mpyi, OWIDTH);
287
                o_right = (o_right_r << OWIDTH) | (o_right_i);
288 5 dgisselq
        /*
289
                printf("oR_r = %lx, ", o_right_r);
290
                printf("oR_i = %lx\n", o_right_i);
291
        */
292
 
293
                m_left[ m_addr&(64-1)] = o_left;
294
                m_right[m_addr&(64-1)] = o_right;
295 6 dgisselq
                m_aux[  m_addr&(64-1)] = aux;
296 5 dgisselq
 
297
                m_addr++;
298
        }
299
};
300
 
301 36 dgisselq
long gentestword(int w, int al, int ar) {
302
        unsigned long   lo, hi, r;
303
        hi  = ((unsigned long)(al&0x0c))<<(w-4);
304
        hi += (al&3)-2ul;
305
 
306
        lo  = ((unsigned long)(ar&0x0c))<<(w-4);
307
        lo += (ar&3)-2ul;
308
 
309
        r = (ubits(hi, w) << w) | (ubits(lo, w));
310
        return r;
311
}
312
 
313 3 dgisselq
int     main(int argc, char **argv, char **envp) {
314
        Verilated::commandArgs(argc, argv);
315 5 dgisselq
        BFLY_TB *bfly = new BFLY_TB;
316 3 dgisselq
        int16_t         ir0, ii0, lstr, lsti;
317
        int32_t         sumr, sumi, difr, difi;
318
        int32_t         smr, smi, dfr, dfi;
319
        int             rnd = 0;
320
 
321 5 dgisselq
        const int       TESTSZ = 256;
322 3 dgisselq
 
323 36 dgisselq
        bfly->opentrace("butterfly.vcd");
324
 
325 5 dgisselq
        bfly->reset();
326 3 dgisselq
 
327 36 dgisselq
// #define      ZEROTEST
328
#define ZEROTEST bfly->test(9,0,0x0000000000l,0x00000000,0x00000000, 0)
329 23 dgisselq
        // Test whether or not the aux channel starts clear, like its supposed to
330 36 dgisselq
 
331
        bfly->test(9,0,0x4000000000l,0x000f0000,0x00000000, 1);
332
        ZEROTEST;
333
        ZEROTEST;
334
        bfly->test(9,0,0x4000000000l,0x00000000,0x000f0000, 0);
335
        ZEROTEST;
336
        ZEROTEST;
337 23 dgisselq
        bfly->test(9,0,0x4000000000l,0x000f0000,0x000f0000, 0);
338 36 dgisselq
        ZEROTEST;
339
        ZEROTEST;
340 23 dgisselq
        bfly->test(9,1,0x4000000000l,0x000f0000,0xfff10000, 0);
341 36 dgisselq
        ZEROTEST;
342
        ZEROTEST;
343 23 dgisselq
        bfly->test(9,2,0x4000000000l,0x0000000f,0x0000fff1, 0);
344 36 dgisselq
        ZEROTEST;
345
        ZEROTEST;
346 23 dgisselq
        bfly->test(9,3,0x4000000000l,0x0000000f,0x0000000f, 0);
347 36 dgisselq
        ZEROTEST;
348
        ZEROTEST;
349 23 dgisselq
 
350 5 dgisselq
        bfly->test(9,0,0x4000000000l,0x7fff0000,0x7fff0000, 1);
351
        bfly->test(9,1,0x4000000000l,0x7fff0000,0x80010000, 0);
352
        bfly->test(9,2,0x4000000000l,0x00007fff,0x00008001, 0);
353
        bfly->test(9,3,0x4000000000l,0x00007fff,0x00007fff, 0);
354 3 dgisselq
 
355 5 dgisselq
        bfly->test(8,0,0x4000000000l,0x80010000,0x80010000, 1);
356
        bfly->test(8,1,0x4000000000l,0x00008001,0x00008001, 0);
357 3 dgisselq
 
358 5 dgisselq
        bfly->test(9,0,0x4000000000l,0x40000000,0xc0000000, 1);
359
        bfly->test(9,1,0x4000000000l,0x40000000,0x40000000, 0);
360
        bfly->test(9,2,0x4000000000l,0x00004000,0x0000c000, 0);
361
        bfly->test(9,3,0x4000000000l,0x00004000,0x00004000, 0);
362 3 dgisselq
 
363 5 dgisselq
        bfly->test(9,0,0x4000000000l,0x20000000,0xe0000000, 1);
364
        bfly->test(9,1,0x4000000000l,0x20000000,0x20000000, 0);
365
        bfly->test(9,2,0x4000000000l,0x00002000,0x0000e000, 0);
366
        bfly->test(9,3,0x4000000000l,0x00002000,0x00002000, 0);
367 3 dgisselq
 
368 5 dgisselq
        bfly->test(9,0,0x4000000000l,0x00080000,0xfff80000, 1);
369
        bfly->test(9,1,0x4000000000l,0x00080000,0x00080000, 0);
370
        bfly->test(9,2,0x4000000000l,0x00000008,0x0000fff8, 0);
371
        bfly->test(9,3,0x4000000000l,0x00000008,0x00000008, 0);
372 3 dgisselq
 
373 8 dgisselq
        bfly->test(7,0,0x3fffbff9b9l,0xfffe0000,0x00000000, 1);
374
        bfly->test(7,1,0x3ffd4fed28l,0xfffc0000,0x00020000, 0);
375
        bfly->test(7,2,0x3ff85fe098l,0xfff80000,0x00060000, 0);
376
        bfly->test(7,3,0x3ff0efd409l,0xfff00000,0x000e0000, 0);
377
        bfly->test(7,4,0x3fe70fc77cl,0xffe60000,0x00180000, 0);
378
        bfly->test(7,5,0x3fdabfbaf1l,0xffda0000,0x00240000, 0);
379
        bfly->test(7,6,0x3fcbefae69l,0xffca0000,0x00340000, 0);
380
        bfly->test(7,7,0x3fbaafa1e4l,0xffba0000,0x00440000, 0);
381
 
382
        /*
383
        // Special tests
384 5 dgisselq
        bfly->test(9,0,0x4000000000l,0x00010000,0xffff0000, 1);
385
        bfly->test(9,1,0x4000000000l,0x00010000,0x00010000, 0);
386
        bfly->test(9,2,0x4000000000l,0x00000001,0x0000ffff, 0);
387
        bfly->test(9,3,0x4000000000l,0x00000001,0x00000001, 0);
388 8 dgisselq
        */
389 3 dgisselq
 
390 5 dgisselq
        for(int n=0; n<4; n++) for(int k=0; k<TESTSZ; k++) {
391
                long    iv, rv;
392
                unsigned long   lft, rht, cof;
393
                double  c, s, W;
394
                bool    inv = 1;
395
                int     aux;
396 3 dgisselq
 
397 5 dgisselq
                W = ((inv)?-1:1) * 2.0 * M_PI * (2*k) / TESTSZ * 64;
398
                c = cos(W); s = sin(W);
399
                rv = (long)((double)(1l<<(16-2-n))*c+0.5);
400
                iv = (long)((double)(1l<<(16-2-n))*s+0.5);
401 3 dgisselq
 
402 5 dgisselq
                rv = (rv << 16) | (iv & (~(-1<<16)));
403
                lft = rv;
404 3 dgisselq
 
405 5 dgisselq
                W = ((inv)?-1:1) * 2.0 * M_PI * (2*k+1) / TESTSZ * 64;
406
                c = cos(W); s = sin(W);
407
                rv = (long)((double)(1l<<(16-2-n))*c+0.5);
408
                iv = (long)((double)(1l<<(16-2-n))*s+0.5);
409
 
410
                rv = (rv << 16) | (iv & (~(-1<<16)));
411
                rht = rv;
412
 
413
 
414
                // Switch the sign of W
415
                W = ((inv)?1:-1) * 2.0 * M_PI * (2*k) / TESTSZ;
416
                c = cos(W); s = sin(W);
417
                rv = (long)((double)(1l<<(20-2))*c+0.5); // Keep 20-2 bits for
418
                iv = (long)((double)(1l<<(20-2))*s+0.5); // coefficients
419
 
420
                rv = (rv << 20) | (iv & (~(-1<<20)));
421
                cof = rv;
422
 
423
                aux = ((k&(TESTSZ-1))==0);
424
 
425
                bfly->test(n,k, cof, lft, rht, aux);
426 3 dgisselq
        }
427
 
428 36 dgisselq
        int     k = TESTSZ;
429
        // Exhaustively test
430
#if (4*IWIDTH+2*CWIDTH <= 24)
431
                for(int a=0; a<(1<<(2*IWIDTH)); a++)
432
                for(int b=0; b<(1<<(2*IWIDTH)); b++)
433
                for(int c=0; c<(1<<(2*CWIDTH)); c++)
434
                        bfly->test(0, k++, c, a, b, 0);
435
 
436
                printf("Exhaust complete\n");
437
#else
438
                for(int al=0; al<16; al++)
439
                for(int ar=0; ar<16; ar++)
440
                for(int bl=0; bl<16; bl++)
441
                for(int br=0; br<16; br++)
442
                for(int cl=0; cl<16; cl++)
443
                for(int cr=0; cr<16; cr++) {
444
                        long a = gentestword(IWIDTH, al, ar);
445
                        long b = gentestword(IWIDTH, bl, br);
446
                        long c = gentestword(CWIDTH, cl, cr);
447
                        bfly->test(0, k++, c, a, b, 0);
448
                }
449
                printf("Partial exhaust complete\n");
450
#endif
451
 
452 3 dgisselq
        delete  bfly;
453
 
454 4 dgisselq
        printf("SUCCESS!\n");
455 3 dgisselq
        exit(0);
456
}

powered by: WebSVN 2.1.0

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