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

Subversion Repositories dblclockfft

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

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
// Purpose:     A test-bench for the butterfly.v subfile of the double
8
//              clocked FFT.  This file may be run autonomously.  If so,
9
//              the last line output will either read "SUCCESS" on success,
10
//              or some other failure message otherwise.
11
//
12
//              This file depends upon verilator to both compile, run, and
13
//              therefore test butterfly.v
14
//
15
// Creator:     Dan Gisselquist, Ph.D.
16 30 dgisselq
//              Gisselquist Technology, LLC
17 6 dgisselq
//
18
///////////////////////////////////////////////////////////////////////////
19
//
20
// Copyright (C) 2015, Gisselquist Technology, LLC
21
//
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 29 dgisselq
#include "fftsize.h"
46 3 dgisselq
#include "Vbutterfly.h"
47
#include "verilated.h"
48 23 dgisselq
#include "twoc.h"
49 3 dgisselq
 
50 29 dgisselq
#define IWIDTH  TST_BUTTERFLY_IWIDTH
51
#define CWIDTH  TST_BUTTERFLY_CWIDTH
52
#define OWIDTH  TST_BUTTERFLY_OWIDTH
53
#define BFLYDELAY       TST_BUTTERFLY_MPYDELAY
54 15 dgisselq
 
55 5 dgisselq
class   BFLY_TB {
56
public:
57
        Vbutterfly      *m_bfly;
58
        unsigned long   m_left[64], m_right[64];
59 6 dgisselq
        bool            m_aux[64];
60 13 dgisselq
        int             m_addr, m_lastaux, m_offset;
61 23 dgisselq
        bool            m_syncd, m_waiting_for_sync_input;
62 3 dgisselq
 
63 5 dgisselq
        BFLY_TB(void) {
64
                m_bfly = new Vbutterfly;
65
                m_addr = 0;
66 6 dgisselq
                m_syncd = 0;
67 23 dgisselq
                m_waiting_for_sync_input = true;
68 5 dgisselq
        }
69 3 dgisselq
 
70 5 dgisselq
        void    tick(void) {
71
                m_lastaux = m_bfly->o_aux;
72
                m_bfly->i_clk = 0;
73
                m_bfly->eval();
74
                m_bfly->i_clk = 1;
75
                m_bfly->eval();
76 6 dgisselq
 
77 13 dgisselq
                if ((!m_syncd)&&(m_bfly->o_aux))
78
                        m_offset = m_addr;
79 6 dgisselq
                m_syncd = (m_syncd) || (m_bfly->o_aux);
80 5 dgisselq
        }
81
 
82
        void    reset(void) {
83
                m_bfly->i_ce    = 0;
84 6 dgisselq
                m_bfly->i_rst   = 1;
85 5 dgisselq
                m_bfly->i_coef  = 0l;
86
                m_bfly->i_left  = 0;
87
                m_bfly->i_right = 0;
88
                tick();
89 6 dgisselq
                m_bfly->i_rst = 0;
90 5 dgisselq
                m_bfly->i_ce  = 1;
91 6 dgisselq
                //
92
                // Let's run a RESET test here, forcing the whole butterfly
93
                // to be filled with aux=1.  If the reset works right,
94
                // we'll never get an aux=1 output.
95
                //
96
                m_bfly->i_rst = 1;
97 5 dgisselq
                m_bfly->i_aux = 1;
98 26 dgisselq
                for(int i=0; i<200; i++) {
99
                        m_bfly->i_ce = 1;
100 6 dgisselq
                        tick();
101 26 dgisselq
                }
102 5 dgisselq
 
103 6 dgisselq
                // Now here's the RESET line, so let's see what the test does
104
                m_bfly->i_rst = 1;
105
                m_bfly->i_ce  = 1;
106
                m_bfly->i_aux = 1;
107 5 dgisselq
                tick();
108 6 dgisselq
                m_bfly->i_rst = 0;
109
                m_syncd = 0;
110 23 dgisselq
 
111
                m_waiting_for_sync_input = true;
112 5 dgisselq
        }
113
 
114
        void    test(const int n, const int k, const unsigned long cof,
115
                        const unsigned lft, const unsigned rht, const int aux) {
116
 
117 29 dgisselq
                m_bfly->i_coef  = ubits(cof, 2*TST_BUTTERFLY_CWIDTH);
118
                m_bfly->i_left  = ubits(lft, 2*TST_BUTTERFLY_IWIDTH);
119
                m_bfly->i_right = ubits(rht, 2*TST_BUTTERFLY_IWIDTH);
120 5 dgisselq
                m_bfly->i_aux   = aux & 1;
121 23 dgisselq
                if ((m_waiting_for_sync_input)&&(aux&1)) {
122
                        m_waiting_for_sync_input = false;
123
                        m_addr = 0;
124
                }
125 5 dgisselq
 
126 26 dgisselq
                m_bfly->i_ce = 1;
127 5 dgisselq
                tick();
128
 
129
                if ((m_bfly->o_aux)&&(!m_lastaux))
130
                        printf("\n");
131
                printf("n,k=%d,%3d: COEF=%010lx, LFT=%08x, RHT=%08x, A=%d, OLFT =%09lx, ORHT=%09lx, AUX=%d\n",
132
                        n,k,
133
                        m_bfly->i_coef & (~(-1l<<40)),
134
                        m_bfly->i_left,
135
                        m_bfly->i_right,
136
                        m_bfly->i_aux,
137
                        m_bfly->o_left,
138
                        m_bfly->o_right,
139
                        m_bfly->o_aux);
140
 
141 13 dgisselq
                if ((m_syncd)&&(m_left[(m_addr-m_offset)&(64-1)] != m_bfly->o_left)) {
142 29 dgisselq
                        printf("WRONG O_LEFT! (%lx(exp) != %lx(sut))\n",
143 15 dgisselq
                                m_left[(m_addr-m_offset)&(64-1)],
144
                                m_bfly->o_left);
145 5 dgisselq
                        exit(-1);
146
                }
147
 
148 13 dgisselq
                if ((m_syncd)&&(m_right[(m_addr-m_offset)&(64-1)] != m_bfly->o_right)) {
149 29 dgisselq
                        printf("WRONG O_RIGHT (%10lx(exp) != (%10lx(sut))!\n",
150 23 dgisselq
                                m_right[(m_addr-m_offset)&(64-1)], m_bfly->o_right);
151 5 dgisselq
                        exit(-1);
152
                }
153
 
154 13 dgisselq
                if ((m_syncd)&&(m_aux[(m_addr-m_offset)&(64-1)] != m_bfly->o_aux)) {
155 29 dgisselq
                        printf("FAILED AUX CHANNEL TEST (i.e. the SYNC)\n");
156 6 dgisselq
                        exit(-1);
157
                }
158
 
159 29 dgisselq
                if ((m_addr > TST_BUTTERFLY_MPYDELAY+6)&&(!m_syncd)) {
160
                        printf("NO SYNC PULSE!\n");
161
                        // exit(-1);
162 6 dgisselq
                }
163
 
164 5 dgisselq
                // Now, let's calculate an "expected" result ...
165
                long    rlft, ilft;
166
 
167
                // Extract left and right values ...
168 29 dgisselq
                rlft = sbits(m_bfly->i_left >> IWIDTH, IWIDTH);
169
                ilft = sbits(m_bfly->i_left          , IWIDTH);
170 5 dgisselq
 
171
                // Now repeat for the right hand value ...
172
                long    rrht, irht;
173
                // Extract left and right values ...
174 29 dgisselq
                rrht = sbits(m_bfly->i_right >> IWIDTH, IWIDTH);
175
                irht = sbits(m_bfly->i_right          , IWIDTH);
176 5 dgisselq
 
177
                // and again for the coefficients
178
                long    rcof, icof;
179
                // Extract left and right values ...
180 29 dgisselq
                rcof = sbits(m_bfly->i_coef >> CWIDTH, CWIDTH);
181
                icof = sbits(m_bfly->i_coef          , CWIDTH);
182 5 dgisselq
 
183
                // Now, let's do the butterfly ourselves ...
184
                long sumi, sumr, difi, difr;
185
                sumr = rlft + rrht;
186
                sumi = ilft + irht;
187
                difr = rlft - rrht;
188
                difi = ilft - irht;
189
 
190
        /*
191
                printf("L=%5lx+%5lx,R=%5lx+%5lx,S=%5lx+%5lx,D=%5lx+%5lx, ",
192
                        rlft & 0x02ffffl,
193
                        ilft & 0x02ffffl,
194
                        rrht & 0x02ffffl,
195
                        irht & 0x02ffffl,
196
                        sumr & 0x02ffffl,
197
                        sumi & 0x02ffffl,
198
                        difr & 0x02ffffl,
199
                        difi & 0x02ffffl);
200
        */
201
                long p1, p2, p3, mpyr, mpyi;
202
                p1 = difr * rcof;
203
                p2 = difi * icof;
204
                p3 = (difr + difi) * (rcof + icof);
205
 
206 29 dgisselq
                mpyr = p1-p2;
207
                mpyi = p3-p1-p2;
208 5 dgisselq
 
209 29 dgisselq
                mpyr = rndbits(mpyr, (IWIDTH+2)+(CWIDTH+1), OWIDTH+4);
210
                mpyi = rndbits(mpyi, (IWIDTH+2)+(CWIDTH+1), OWIDTH+4);
211
 
212 5 dgisselq
        /*
213
                printf("RC=%lx, IC=%lx, ", rcof, icof);
214
                printf("P1=%lx,P2=%lx,P3=%lx, ", p1,p2,p3);
215
                printf("MPYr = %lx, ", mpyr);
216
                printf("MPYi = %lx, ", mpyi);
217
        */
218
 
219
                long    o_left_r, o_left_i, o_right_r, o_right_i;
220
                unsigned long   o_left, o_right;
221
 
222 29 dgisselq
                o_left_r = rndbits(sumr<<(CWIDTH-2), CWIDTH+IWIDTH+3, OWIDTH+4);
223
                        o_left_r = ubits(o_left_r, OWIDTH);
224
                o_left_i = rndbits(sumi<<(CWIDTH-2), CWIDTH+IWIDTH+3, OWIDTH+4);
225
                        o_left_i = ubits(o_left_i, OWIDTH);
226
                o_left = (o_left_r << OWIDTH) | (o_left_i);
227 5 dgisselq
 
228 29 dgisselq
                o_right_r = ubits(mpyr, OWIDTH);
229
                o_right_i = ubits(mpyi, OWIDTH);
230
                o_right = (o_right_r << OWIDTH) | (o_right_i);
231 5 dgisselq
        /*
232
                printf("oR_r = %lx, ", o_right_r);
233
                printf("oR_i = %lx\n", o_right_i);
234
        */
235
 
236
                m_left[ m_addr&(64-1)] = o_left;
237
                m_right[m_addr&(64-1)] = o_right;
238 6 dgisselq
                m_aux[  m_addr&(64-1)] = aux;
239 5 dgisselq
 
240
                m_addr++;
241
        }
242
};
243
 
244 3 dgisselq
int     main(int argc, char **argv, char **envp) {
245
        Verilated::commandArgs(argc, argv);
246 5 dgisselq
        BFLY_TB *bfly = new BFLY_TB;
247 3 dgisselq
        int16_t         ir0, ii0, lstr, lsti;
248
        int32_t         sumr, sumi, difr, difi;
249
        int32_t         smr, smi, dfr, dfi;
250
        int             rnd = 0;
251
 
252 5 dgisselq
        const int       TESTSZ = 256;
253 3 dgisselq
 
254 5 dgisselq
        bfly->reset();
255 3 dgisselq
 
256 23 dgisselq
        // Test whether or not the aux channel starts clear, like its supposed to
257
        bfly->test(9,0,0x4000000000l,0x000f0000,0x000f0000, 0);
258
        bfly->test(9,1,0x4000000000l,0x000f0000,0xfff10000, 0);
259
        bfly->test(9,2,0x4000000000l,0x0000000f,0x0000fff1, 0);
260
        bfly->test(9,3,0x4000000000l,0x0000000f,0x0000000f, 0);
261
 
262 5 dgisselq
        bfly->test(9,0,0x4000000000l,0x7fff0000,0x7fff0000, 1);
263
        bfly->test(9,1,0x4000000000l,0x7fff0000,0x80010000, 0);
264
        bfly->test(9,2,0x4000000000l,0x00007fff,0x00008001, 0);
265
        bfly->test(9,3,0x4000000000l,0x00007fff,0x00007fff, 0);
266 3 dgisselq
 
267 5 dgisselq
        bfly->test(8,0,0x4000000000l,0x80010000,0x80010000, 1);
268
        bfly->test(8,1,0x4000000000l,0x00008001,0x00008001, 0);
269 3 dgisselq
 
270 5 dgisselq
        bfly->test(9,0,0x4000000000l,0x40000000,0xc0000000, 1);
271
        bfly->test(9,1,0x4000000000l,0x40000000,0x40000000, 0);
272
        bfly->test(9,2,0x4000000000l,0x00004000,0x0000c000, 0);
273
        bfly->test(9,3,0x4000000000l,0x00004000,0x00004000, 0);
274 3 dgisselq
 
275 5 dgisselq
        bfly->test(9,0,0x4000000000l,0x20000000,0xe0000000, 1);
276
        bfly->test(9,1,0x4000000000l,0x20000000,0x20000000, 0);
277
        bfly->test(9,2,0x4000000000l,0x00002000,0x0000e000, 0);
278
        bfly->test(9,3,0x4000000000l,0x00002000,0x00002000, 0);
279 3 dgisselq
 
280 5 dgisselq
        bfly->test(9,0,0x4000000000l,0x00080000,0xfff80000, 1);
281
        bfly->test(9,1,0x4000000000l,0x00080000,0x00080000, 0);
282
        bfly->test(9,2,0x4000000000l,0x00000008,0x0000fff8, 0);
283
        bfly->test(9,3,0x4000000000l,0x00000008,0x00000008, 0);
284 3 dgisselq
 
285 8 dgisselq
        bfly->test(7,0,0x3fffbff9b9l,0xfffe0000,0x00000000, 1);
286
        bfly->test(7,1,0x3ffd4fed28l,0xfffc0000,0x00020000, 0);
287
        bfly->test(7,2,0x3ff85fe098l,0xfff80000,0x00060000, 0);
288
        bfly->test(7,3,0x3ff0efd409l,0xfff00000,0x000e0000, 0);
289
        bfly->test(7,4,0x3fe70fc77cl,0xffe60000,0x00180000, 0);
290
        bfly->test(7,5,0x3fdabfbaf1l,0xffda0000,0x00240000, 0);
291
        bfly->test(7,6,0x3fcbefae69l,0xffca0000,0x00340000, 0);
292
        bfly->test(7,7,0x3fbaafa1e4l,0xffba0000,0x00440000, 0);
293
 
294
        /*
295
        // Special tests
296 5 dgisselq
        bfly->test(9,0,0x4000000000l,0x00010000,0xffff0000, 1);
297
        bfly->test(9,1,0x4000000000l,0x00010000,0x00010000, 0);
298
        bfly->test(9,2,0x4000000000l,0x00000001,0x0000ffff, 0);
299
        bfly->test(9,3,0x4000000000l,0x00000001,0x00000001, 0);
300 8 dgisselq
        */
301 3 dgisselq
 
302 5 dgisselq
        for(int n=0; n<4; n++) for(int k=0; k<TESTSZ; k++) {
303
                long    iv, rv;
304
                unsigned long   lft, rht, cof;
305
                double  c, s, W;
306
                bool    inv = 1;
307
                int     aux;
308 3 dgisselq
 
309 5 dgisselq
                W = ((inv)?-1:1) * 2.0 * M_PI * (2*k) / TESTSZ * 64;
310
                c = cos(W); s = sin(W);
311
                rv = (long)((double)(1l<<(16-2-n))*c+0.5);
312
                iv = (long)((double)(1l<<(16-2-n))*s+0.5);
313 3 dgisselq
 
314 5 dgisselq
                rv = (rv << 16) | (iv & (~(-1<<16)));
315
                lft = rv;
316 3 dgisselq
 
317 5 dgisselq
                W = ((inv)?-1:1) * 2.0 * M_PI * (2*k+1) / TESTSZ * 64;
318
                c = cos(W); s = sin(W);
319
                rv = (long)((double)(1l<<(16-2-n))*c+0.5);
320
                iv = (long)((double)(1l<<(16-2-n))*s+0.5);
321
 
322
                rv = (rv << 16) | (iv & (~(-1<<16)));
323
                rht = rv;
324
 
325
 
326
                // Switch the sign of W
327
                W = ((inv)?1:-1) * 2.0 * M_PI * (2*k) / TESTSZ;
328
                c = cos(W); s = sin(W);
329
                rv = (long)((double)(1l<<(20-2))*c+0.5); // Keep 20-2 bits for
330
                iv = (long)((double)(1l<<(20-2))*s+0.5); // coefficients
331
 
332
                rv = (rv << 20) | (iv & (~(-1<<20)));
333
                cof = rv;
334
 
335
                aux = ((k&(TESTSZ-1))==0);
336
 
337
                bfly->test(n,k, cof, lft, rht, aux);
338 3 dgisselq
        }
339
 
340
        delete  bfly;
341
 
342 4 dgisselq
        printf("SUCCESS!\n");
343 3 dgisselq
        exit(0);
344
}

powered by: WebSVN 2.1.0

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