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

Subversion Repositories dblclockfft

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

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

powered by: WebSVN 2.1.0

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