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

Subversion Repositories dblclockfft

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

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

powered by: WebSVN 2.1.0

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