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

Subversion Repositories dblclockfft

[/] [dblclockfft/] [trunk/] [bench/] [cpp/] [hwbfly_tb.cpp] - Blame information for rev 41

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 41 dgisselq
////////////////////////////////////////////////////////////////////////////////
2 22 dgisselq
//
3 36 dgisselq
// Filename:    hwbfly_tb.cpp
4 22 dgisselq
//
5 41 dgisselq
// Project:     A General Purpose Pipelined FFT Implementation
6 22 dgisselq
//
7 36 dgisselq
// Purpose:     A test-bench for the hardware butterfly 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 22 dgisselq
//
12 36 dgisselq
//      This file depends upon verilator to both compile, run, and therefore
13
//      test hwbfly.v
14 22 dgisselq
//
15
// Creator:     Dan Gisselquist, Ph.D.
16 30 dgisselq
//              Gisselquist Technology, LLC
17 22 dgisselq
//
18 41 dgisselq
////////////////////////////////////////////////////////////////////////////////
19 22 dgisselq
//
20 36 dgisselq
// Copyright (C) 2015,2018 Gisselquist Technology, LLC
21 22 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 41 dgisselq
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
34 22 dgisselq
// 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 41 dgisselq
////////////////////////////////////////////////////////////////////////////////
42 22 dgisselq
#include <stdio.h>
43
#include <stdint.h>
44
 
45 36 dgisselq
#include "verilated.h"
46
#include "verilated_vcd_c.h"
47 22 dgisselq
#include "Vhwbfly.h"
48 23 dgisselq
#include "twoc.h"
49 36 dgisselq
#include "fftsize.h"
50 22 dgisselq
 
51 35 dgisselq
#ifdef  NEW_VERILATOR
52
#define VVAR(A) hwbfly__DOT_ ## A
53
#else
54
#define VVAR(A) v__DOT_ ## A
55
#endif
56
 
57 36 dgisselq
#define IWIDTH  TST_BUTTERFLY_IWIDTH
58
#define CWIDTH  TST_BUTTERFLY_CWIDTH
59
#define OWIDTH  TST_BUTTERFLY_OWIDTH
60 35 dgisselq
 
61 36 dgisselq
class   HWBFLY_TB {
62 22 dgisselq
public:
63
        Vhwbfly         *m_bfly;
64 36 dgisselq
        VerilatedVcdC   *m_trace;
65 22 dgisselq
        unsigned long   m_left[64], m_right[64];
66
        bool            m_aux[64];
67
        int             m_addr, m_lastaux, m_offset;
68
        bool            m_syncd;
69 36 dgisselq
        uint64_t        m_tickcount;
70 22 dgisselq
 
71 36 dgisselq
        HWBFLY_TB(void) {
72
                Verilated::traceEverOn(true);
73
                m_trace = NULL;
74 22 dgisselq
                m_bfly = new Vhwbfly;
75
                m_addr = 0;
76
                m_syncd = 0;
77 36 dgisselq
                m_tickcount = 0;
78
                m_bfly->i_reset = 1;
79
                m_bfly->i_clk = 0;
80
                m_bfly->eval();
81
                m_bfly->i_reset = 0;
82 22 dgisselq
        }
83
 
84 36 dgisselq
        void    opentrace(const char *vcdname) {
85
                if (!m_trace) {
86
                        m_trace = new VerilatedVcdC;
87
                        m_bfly->trace(m_trace, 99);
88
                        m_trace->open(vcdname);
89
                }
90
        }
91
 
92
        void    closetrace(void) {
93
                if (m_trace) {
94
                        m_trace->close();
95
                        delete  m_trace;
96
                        m_trace = NULL;
97
                }
98
        }
99
 
100 22 dgisselq
        void    tick(void) {
101 36 dgisselq
                m_tickcount++;
102
 
103 22 dgisselq
                m_lastaux = m_bfly->o_aux;
104
                m_bfly->i_clk = 0;
105
                m_bfly->eval();
106 41 dgisselq
                if (m_trace) m_trace->dump((vluint64_t)(10ul*m_tickcount-2));
107 22 dgisselq
                m_bfly->i_clk = 1;
108
                m_bfly->eval();
109 41 dgisselq
                if (m_trace) m_trace->dump((vluint64_t)(10ul*m_tickcount));
110 36 dgisselq
                m_bfly->i_clk = 0;
111
                m_bfly->eval();
112
                if (m_trace) {
113 41 dgisselq
                        m_trace->dump((vluint64_t)(10ul*m_tickcount+5));
114 36 dgisselq
                        m_trace->flush();
115
                }
116 22 dgisselq
 
117
                if ((!m_syncd)&&(m_bfly->o_aux))
118
                        m_offset = m_addr;
119
                m_syncd = (m_syncd) || (m_bfly->o_aux);
120
        }
121
 
122 36 dgisselq
        void    cetick(void) {
123
                int     ce = m_bfly->i_ce, nkce;
124
 
125
                tick();
126
 
127
                nkce = (rand()&1);
128
#ifdef  FFT_CKPCE
129
                nkce += FFT_CKPCE;
130
#endif
131
 
132
                if ((ce)&&(nkce > 0)) {
133
                        m_bfly->i_ce = 0;
134
                        for(int kce=0; kce<nkce-1; kce++)
135
                                tick();
136
                }
137
 
138
                m_bfly->i_ce = ce;
139
        }
140
 
141 22 dgisselq
        void    reset(void) {
142
                m_bfly->i_ce    = 0;
143 36 dgisselq
                m_bfly->i_reset = 1;
144 22 dgisselq
                m_bfly->i_coef  = 0l;
145
                m_bfly->i_left  = 0;
146
                m_bfly->i_right = 0;
147
                tick();
148 36 dgisselq
                m_bfly->i_reset = 0;
149 22 dgisselq
                m_bfly->i_ce  = 1;
150
                //
151
                // Let's run a RESET test here, forcing the whole butterfly
152
                // to be filled with aux=1.  If the reset works right,
153
                // we'll never get an aux=1 output.
154
                //
155 36 dgisselq
                m_bfly->i_reset = 1;
156
                m_bfly->i_aux = 1;
157 22 dgisselq
                m_bfly->i_ce  = 1;
158
                for(int i=0; i<200; i++)
159 36 dgisselq
                        cetick();
160 22 dgisselq
 
161
                // Now here's the RESET line, so let's see what the test does
162 36 dgisselq
                m_bfly->i_reset = 1;
163 22 dgisselq
                m_bfly->i_ce  = 1;
164
                m_bfly->i_aux = 1;
165 36 dgisselq
                cetick();
166
                m_bfly->i_reset = 0;
167 22 dgisselq
                m_syncd = 0;
168
        }
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 36 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 22 dgisselq
                m_bfly->i_aux   = aux & 1;
177
 
178 26 dgisselq
                m_bfly->i_ce = 1;
179 36 dgisselq
                cetick();
180 22 dgisselq
 
181
                if ((m_bfly->o_aux)&&(!m_lastaux))
182
                        printf("\n");
183
                printf("n,k=%d,%3d: COEF=%010lx, LFT=%08x, RHT=%08x, A=%d, OLFT =%09lx, ORHT=%09lx, AUX=%d",
184
                        n,k,
185
                        m_bfly->i_coef & (~(-1l<<40)),
186
                        m_bfly->i_left,
187
                        m_bfly->i_right,
188
                        m_bfly->i_aux,
189
                        m_bfly->o_left,
190
                        m_bfly->o_right,
191
                        m_bfly->o_aux);
192 36 dgisselq
#if (FFT_CKPCE == 1)
193
                printf(", p1 = 0x%08lx p2 = 0x%08lx, p3 = 0x%08lx",
194
#define rp_one          VVAR(_CKPCE_ONE__DOT__rp_one)
195
#define rp_two          VVAR(_CKPCE_ONE__DOT__rp_two)
196
#define rp_three        VVAR(_CKPCE_ONE__DOT__rp_three)
197
                        m_bfly->rp_one,
198
                        m_bfly->rp_two,
199
                        m_bfly->rp_three);
200
#elif (FFT_CKPCE == 2)
201
#define rp_one          VVAR(_genblk1__DOT__CKPCE_TWO__DOT__rp2_one)
202
#define rp_two          VVAR(_genblk1__DOT__CKPCE_TWO__DOT__rp_two)
203
#define rp_three        VVAR(_genblk1__DOT__CKPCE_TWO__DOT__rp_three)
204
                printf(", p1 = 0x%08lx p2 = 0x%08lx, p3 = 0x%08lx",
205
                        m_bfly->rp_one,
206
                        m_bfly->rp_two,
207
                        m_bfly->rp_three);
208
#else
209
                printf("CKPCE = %d\n", FFT_CKPCE);
210
#endif
211
 
212 22 dgisselq
                printf("\n");
213
 
214
                if ((m_syncd)&&(m_left[(m_addr-m_offset)&(64-1)] != m_bfly->o_left)) {
215 36 dgisselq
                        printf("WRONG O_LEFT! (%lx(exp) != %lx(sut)\n",
216 22 dgisselq
                                m_left[(m_addr-m_offset)&(64-1)],
217
                                m_bfly->o_left);
218 36 dgisselq
                        exit(EXIT_FAILURE);
219 22 dgisselq
                }
220
 
221
                if ((m_syncd)&&(m_right[(m_addr-m_offset)&(64-1)] != m_bfly->o_right)) {
222 36 dgisselq
                        printf("WRONG O_RIGHT! (%lx(exp) != %lx(sut))\n",
223
                                m_right[(m_addr-m_offset)&(64-1)], m_bfly->o_right);
224
                        exit(EXIT_FAILURE);
225 22 dgisselq
                }
226
 
227
                if ((m_syncd)&&(m_aux[(m_addr-m_offset)&(64-1)] != m_bfly->o_aux)) {
228 36 dgisselq
                        printf("FAILED AUX CHANNEL TEST (i.e. the SYNC)\n");
229
                        exit(EXIT_FAILURE);
230 22 dgisselq
                }
231
 
232
                if ((m_addr > 22)&&(!m_syncd)) {
233 36 dgisselq
                        printf("NO SYNC PULSE!\n");
234
                        exit(EXIT_FAILURE);
235 22 dgisselq
                }
236
 
237
                // Now, let's calculate an "expected" result ...
238
                long    rlft, ilft;
239
 
240
                // Extract left and right values ...
241 36 dgisselq
                rlft = sbits(m_bfly->i_left >> IWIDTH, IWIDTH);
242
                ilft = sbits(m_bfly->i_left          , IWIDTH);
243 22 dgisselq
 
244
                // Now repeat for the right hand value ...
245
                long    rrht, irht;
246
                // Extract left and right values ...
247 36 dgisselq
                rrht = sbits(m_bfly->i_right >> IWIDTH, IWIDTH);
248
                irht = sbits(m_bfly->i_right          , IWIDTH);
249 22 dgisselq
 
250
                // and again for the coefficients
251
                long    rcof, icof;
252
                // Extract left and right values ...
253 36 dgisselq
                rcof = sbits(m_bfly->i_coef >> CWIDTH, CWIDTH);
254
                icof = sbits(m_bfly->i_coef          , CWIDTH);
255 22 dgisselq
 
256
                // Now, let's do the butterfly ourselves ...
257
                long sumi, sumr, difi, difr;
258
                sumr = rlft + rrht;
259
                sumi = ilft + irht;
260
                difr = rlft - rrht;
261
                difi = ilft - irht;
262
 
263
        /*
264
                printf("L=%5lx+%5lx,R=%5lx+%5lx,S=%5lx+%5lx,D=%5lx+%5lx, ",
265
                        rlft & 0x02ffffl,
266
                        ilft & 0x02ffffl,
267
                        rrht & 0x02ffffl,
268
                        irht & 0x02ffffl,
269
                        sumr & 0x02ffffl,
270
                        sumi & 0x02ffffl,
271
                        difr & 0x02ffffl,
272
                        difi & 0x02ffffl);
273
        */
274
                long p1, p2, p3, mpyr, mpyi;
275
                p1 = difr * rcof;
276
                p2 = difi * icof;
277
                p3 = (difr + difi) * (rcof + icof);
278
 
279 36 dgisselq
                mpyr = p1-p2;
280
                mpyi = p3-p1-p2;
281 22 dgisselq
 
282 36 dgisselq
                mpyr = rndbits(mpyr, (IWIDTH+2)+(CWIDTH+1), OWIDTH+4);
283
                mpyi = rndbits(mpyi, (IWIDTH+2)+(CWIDTH+1), OWIDTH+4);
284
 
285 22 dgisselq
        /*
286
                printf("RC=%lx, IC=%lx, ", rcof, icof);
287
                printf("P1=%lx,P2=%lx,P3=%lx, ", p1,p2,p3);
288
                printf("MPYr = %lx, ", mpyr);
289
                printf("MPYi = %lx, ", mpyi);
290
        */
291
 
292
                long    o_left_r, o_left_i, o_right_r, o_right_i;
293
                unsigned long   o_left, o_right;
294
 
295 36 dgisselq
                o_left_r = rndbits(sumr<<(CWIDTH-2), CWIDTH+IWIDTH+3, OWIDTH+4);
296
                        o_left_r = ubits(o_left_r, OWIDTH);
297
                o_left_i = rndbits(sumi<<(CWIDTH-2), CWIDTH+IWIDTH+3, OWIDTH+4);
298
                        o_left_i = ubits(o_left_i, OWIDTH);
299
                o_left = (o_left_r << OWIDTH) | (o_left_i);
300 22 dgisselq
 
301 36 dgisselq
                o_right_r = ubits(mpyr, OWIDTH);
302
                o_right_i = ubits(mpyi, OWIDTH);
303
                o_right = (o_right_r << OWIDTH) | (o_right_i);
304 22 dgisselq
        /*
305
                printf("oR_r = %lx, ", o_right_r);
306
                printf("oR_i = %lx\n", o_right_i);
307
        */
308
 
309
                m_left[ m_addr&(64-1)] = o_left;
310
                m_right[m_addr&(64-1)] = o_right;
311
                m_aux[  m_addr&(64-1)] = aux;
312
 
313
                m_addr++;
314
        }
315
};
316
 
317
int     main(int argc, char **argv, char **envp) {
318
        Verilated::commandArgs(argc, argv);
319 36 dgisselq
        HWBFLY_TB       *bfly = new HWBFLY_TB;
320 22 dgisselq
        int16_t         ir0, ii0, lstr, lsti;
321
        int32_t         sumr, sumi, difr, difi;
322
        int32_t         smr, smi, dfr, dfi;
323
        int             rnd = 0;
324
 
325
        const int       TESTSZ = 256;
326
 
327 41 dgisselq
        // bfly->opentrace("hwbfly.vcd");
328 36 dgisselq
 
329 22 dgisselq
        bfly->reset();
330
 
331
        bfly->test(9,0,0x4000000000l,0x7fff0000,0x7fff0000, 1);
332
        bfly->test(9,1,0x4000000000l,0x7fff0000,0x80010000, 0);
333
        bfly->test(9,2,0x4000000000l,0x00007fff,0x00008001, 0);
334
        bfly->test(9,3,0x4000000000l,0x00007fff,0x00007fff, 0);
335
 
336
        bfly->test(8,0,0x4000000000l,0x80010000,0x80010000, 1);
337
        bfly->test(8,1,0x4000000000l,0x00008001,0x00008001, 0);
338
 
339
        bfly->test(9,0,0x4000000000l,0x40000000,0xc0000000, 1);
340
        bfly->test(9,1,0x4000000000l,0x40000000,0x40000000, 0);
341
        bfly->test(9,2,0x4000000000l,0x00004000,0x0000c000, 0);
342
        bfly->test(9,3,0x4000000000l,0x00004000,0x00004000, 0);
343
 
344
        bfly->test(9,0,0x4000000000l,0x20000000,0xe0000000, 1);
345
        bfly->test(9,1,0x4000000000l,0x20000000,0x20000000, 0);
346
        bfly->test(9,2,0x4000000000l,0x00002000,0x0000e000, 0);
347
        bfly->test(9,3,0x4000000000l,0x00002000,0x00002000, 0);
348
 
349
        bfly->test(9,0,0x4000000000l,0x00080000,0xfff80000, 1);
350
        bfly->test(9,1,0x4000000000l,0x00080000,0x00080000, 0);
351
        bfly->test(9,2,0x4000000000l,0x00000008,0x0000fff8, 0);
352
        bfly->test(9,3,0x4000000000l,0x00000008,0x00000008, 0);
353
 
354
        bfly->test(7,0,0x3fffbff9b9l,0xfffe0000,0x00000000, 1);
355
        bfly->test(7,1,0x3ffd4fed28l,0xfffc0000,0x00020000, 0);
356
        bfly->test(7,2,0x3ff85fe098l,0xfff80000,0x00060000, 0);
357
        bfly->test(7,3,0x3ff0efd409l,0xfff00000,0x000e0000, 0);
358
        bfly->test(7,4,0x3fe70fc77cl,0xffe60000,0x00180000, 0);
359
        bfly->test(7,5,0x3fdabfbaf1l,0xffda0000,0x00240000, 0);
360
        bfly->test(7,6,0x3fcbefae69l,0xffca0000,0x00340000, 0);
361
        bfly->test(7,7,0x3fbaafa1e4l,0xffba0000,0x00440000, 0);
362
 
363
        /*
364
        // Special tests
365
        bfly->test(9,0,0x4000000000l,0x00010000,0xffff0000, 1);
366
        bfly->test(9,1,0x4000000000l,0x00010000,0x00010000, 0);
367
        bfly->test(9,2,0x4000000000l,0x00000001,0x0000ffff, 0);
368
        bfly->test(9,3,0x4000000000l,0x00000001,0x00000001, 0);
369
        */
370
 
371
        for(int n=0; n<4; n++) for(int k=0; k<TESTSZ; k++) {
372
                long    iv, rv;
373
                unsigned long   lft, rht, cof;
374
                double  c, s, W;
375
                bool    inv = 1;
376
                int     aux;
377
 
378
                W = ((inv)?-1:1) * 2.0 * M_PI * (2*k) / TESTSZ * 64;
379
                c = cos(W); s = sin(W);
380
                rv = (long)((double)(1l<<(16-2-n))*c+0.5);
381
                iv = (long)((double)(1l<<(16-2-n))*s+0.5);
382
 
383
                rv = (rv << 16) | (iv & (~(-1<<16)));
384
                lft = rv;
385
 
386
                W = ((inv)?-1:1) * 2.0 * M_PI * (2*k+1) / TESTSZ * 64;
387
                c = cos(W); s = sin(W);
388
                rv = (long)((double)(1l<<(16-2-n))*c+0.5);
389
                iv = (long)((double)(1l<<(16-2-n))*s+0.5);
390
 
391
                rv = (rv << 16) | (iv & (~(-1<<16)));
392
                rht = rv;
393
 
394
 
395
                // Switch the sign of W
396
                W = ((inv)?1:-1) * 2.0 * M_PI * (2*k) / TESTSZ;
397
                c = cos(W); s = sin(W);
398
                rv = (long)((double)(1l<<(20-2))*c+0.5); // Keep 20-2 bits for
399
                iv = (long)((double)(1l<<(20-2))*s+0.5); // coefficients
400
 
401
                rv = (rv << 20) | (iv & (~(-1<<20)));
402
                cof = rv;
403
 
404
                aux = ((k&(TESTSZ-1))==0);
405
 
406
                bfly->test(n,k, cof, lft, rht, aux);
407
        }
408
 
409
        delete  bfly;
410
 
411
        printf("SUCCESS!\n");
412
        exit(0);
413
}

powered by: WebSVN 2.1.0

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