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

Subversion Repositories dblclockfft

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

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

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

powered by: WebSVN 2.1.0

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