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

Subversion Repositories dblclockfft

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

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

powered by: WebSVN 2.1.0

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