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

Subversion Repositories dblclockfft

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

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

powered by: WebSVN 2.1.0

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