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

Subversion Repositories dblclockfft

[/] [dblclockfft/] [trunk/] [sw/] [fftgen.cpp] - Blame information for rev 33

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

Line No. Rev Author Line
1 29 dgisselq
////////////////////////////////////////////////////////////////////////////////
2 16 dgisselq
//
3 24 dgisselq
// Filename:    fftgen.cpp
4 16 dgisselq
//
5
// Project:     A Doubletime Pipelined FFT
6
//
7
// Purpose:     This is the core generator for the project.  Every part
8
//              and piece of this project begins and ends in this program.
9 33 dgisselq
//      Once built, this program will build an FFT (or IFFT) core of arbitrary
10
//      width, precision, etc., that will run at two samples per clock.
11
//      (Incidentally, I didn't pick two samples per clock because it was
12
//      easier, but rather because there weren't any two-sample per clock
13
//      FFT's posted on opencores.com.  Further, FFT's running at one sample
14
//      per aren't that hard to find.)
15 16 dgisselq
//
16 33 dgisselq
//      You can find the documentation for this program in two places.  One is
17
//      in the usage() function below.  The second is in the 'doc'uments
18
//      directory that comes with this package, specifically in the spec.pdf
19
//      file.  If it's not there, type make in the documents directory to
20
//      build it.
21 16 dgisselq
//
22 31 dgisselq
//      20160123 - Thanks to Lesha Birukov, adjusted for MS Visual Studio 2012.
23
//              (Adjustments are at the top of the file ...)
24
//
25 16 dgisselq
// Creator:     Dan Gisselquist, Ph.D.
26 30 dgisselq
//              Gisselquist Technology, LLC
27 16 dgisselq
//
28 29 dgisselq
////////////////////////////////////////////////////////////////////////////////
29 16 dgisselq
//
30 33 dgisselq
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
31 16 dgisselq
//
32
// This program is free software (firmware): you can redistribute it and/or
33
// modify it under the terms of  the GNU General Public License as published
34
// by the Free Software Foundation, either version 3 of the License, or (at
35
// your option) any later version.
36
//
37
// This program is distributed in the hope that it will be useful, but WITHOUT
38
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
39
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
40
// for more details.
41
//
42
// You should have received a copy of the GNU General Public License along
43
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
44
// target there if the PDF file isn't present.)  If not, see
45
// <http://www.gnu.org/licenses/> for a copy.
46
//
47
// License:     GPL, v3, as defined and found on www.gnu.org,
48
//              http://www.gnu.org/licenses/gpl.html
49
//
50
//
51 29 dgisselq
////////////////////////////////////////////////////////////////////////////////
52 16 dgisselq
//
53
//
54 31 dgisselq
#define _CRT_SECURE_NO_WARNINGS   //  ms vs 2012 doesn't like fopen
55 2 dgisselq
#include <stdio.h>
56
#include <stdlib.h>
57 31 dgisselq
 
58
#ifdef _MSC_VER //  added for ms vs compatibility
59
 
60
#include <io.h>
61
#include <direct.h>
62
#define _USE_MATH_DEFINES
63
#define R_OK    4       /* Test for read permission.  */
64
#define W_OK    2       /* Test for write permission.  */
65
#define X_OK    0       /* !!!!!! execute permission - unsupported in windows*/
66
#define F_OK    0       /* Test for existence.  */
67
 
68
#if _MSC_VER <= 1700
69
 
70
long long llround(double d) {
71
        if (d<0) return -(long long)(-d+0.5);
72
        else    return (long long)(d+0.5); }
73
int lstat(const char *filename, struct stat *buf) { return 1; };
74
#define S_ISDIR(A)      0
75
 
76
#else
77
 
78
#define lstat   _stat
79
#define S_ISDIR _S_IFDIR
80
 
81
#endif
82
 
83
#define mkdir(A,B)      _mkdir(A)
84
 
85
#define access _access
86
 
87
#else
88
// And for G++/Linux environment
89
 
90
#include <unistd.h>     // Defines the R_OK/W_OK/etc. macros
91 2 dgisselq
#include <sys/stat.h>
92 31 dgisselq
#endif
93
 
94 2 dgisselq
#include <string.h>
95 14 dgisselq
#include <string>
96 2 dgisselq
#include <math.h>
97
#include <ctype.h>
98
#include <assert.h>
99
 
100 26 dgisselq
#define DEF_NBITSIN     16
101
#define DEF_COREDIR     "fft-core"
102
#define DEF_XTRACBITS   4
103
#define DEF_NMPY        0
104
#define DEF_XTRAPBITS   0
105 29 dgisselq
#define USE_OLD_MULTIPLY        false
106 2 dgisselq
 
107 29 dgisselq
// To coordinate testing, it helps to have some defines in our header file that
108
// are common with the default parameters found within the various subroutines.
109
// We'll define those common parameters here.  These values, however, have no
110
// effect on anything other than bench testing.  They do, though, allow us to
111
// bench test exact copies of what is going on within the FFT when necessary
112
// in order to find problems.
113
// First, parameters for the new multiply based upon the bi-multiply structure
114
// (2-bits/2-tableau rows at a time).
115
#define TST_LONGBIMPY_AW        16
116
#define TST_LONGBIMPY_BW        20      // Leave undefined to match AW
117
 
118
//  We also include parameters for the shift add multiply
119
#define TST_SHIFTADDMPY_AW      16
120
#define TST_SHIFTADDMPY_BW      20      // Leave undefined to match AW
121
 
122
// Now for parameters matching the butterfly
123
#define TST_BUTTERFLY_IWIDTH    16
124
#define TST_BUTTERFLY_CWIDTH    20
125
#define TST_BUTTERFLY_OWIDTH    17
126
 
127
// Now for parameters matching the qtrstage
128
#define TST_QTRSTAGE_IWIDTH     16
129
#define TST_QTRSTAGE_LGWIDTH    8
130
 
131
// Parameters for the dblstage
132
#define TST_DBLSTAGE_IWIDTH     16
133
#define TST_DBLSTAGE_SHIFT      0
134
 
135
// Now for parameters matching the dblreverse stage
136
#define TST_DBLREVERSE_LGSIZE   5
137
 
138 23 dgisselq
typedef enum {
139
        RND_TRUNCATE, RND_FROMZERO, RND_HALFUP, RND_CONVERGENT
140
} ROUND_T;
141
 
142 33 dgisselq
const char      cpyleft[] =
143 29 dgisselq
"////////////////////////////////////////////////////////////////////////////////\n"
144 2 dgisselq
"//\n"
145 33 dgisselq
"// Copyright (C) 2015-2017, Gisselquist Technology, LLC\n"
146 2 dgisselq
"//\n"
147
"// This program is free software (firmware): you can redistribute it and/or\n"
148
"// modify it under the terms of  the GNU General Public License as published\n"
149
"// by the Free Software Foundation, either version 3 of the License, or (at\n"
150
"// your option) any later version.\n"
151
"//\n"
152
"// This program is distributed in the hope that it will be useful, but WITHOUT\n"
153
"// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or\n"
154
"// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\n"
155
"// for more details.\n"
156
"//\n"
157
"// You should have received a copy of the GNU General Public License along\n"
158 5 dgisselq
"// with this program.  (It's in the $(ROOT)/doc directory, run make with no\n"
159
"// target there if the PDF file isn\'t present.)  If not, see\n"
160
"// <http://www.gnu.org/licenses/> for a copy.\n"
161
"//\n"
162 2 dgisselq
"// License:    GPL, v3, as defined and found on www.gnu.org,\n"
163
"//             http://www.gnu.org/licenses/gpl.html\n"
164
"//\n"
165
"//\n"
166 29 dgisselq
"////////////////////////////////////////////////////////////////////////////////\n";
167 14 dgisselq
const char      prjname[] = "A Doubletime Pipelined FFT";
168 2 dgisselq
const char      creator[] =     "// Creator:    Dan Gisselquist, Ph.D.\n"
169 30 dgisselq
                                "//             Gisselquist Technology, LLC\n";
170 2 dgisselq
 
171
int     lgval(int vl) {
172
        int     lg;
173
 
174
        for(lg=1; (1<<lg) < vl; lg++)
175
                ;
176
        return lg;
177
}
178
 
179
int     nextlg(int vl) {
180
        int     r;
181
 
182
        for(r=1; r<vl; r<<=1)
183
                ;
184
        return r;
185
}
186
 
187 14 dgisselq
int     bflydelay(int nbits, int xtra) {
188 2 dgisselq
        int     cbits = nbits + xtra;
189 14 dgisselq
        int     delay;
190 29 dgisselq
 
191
        if (USE_OLD_MULTIPLY) {
192
                if (nbits+1<cbits)
193
                        delay = nbits+4;
194
                else
195
                        delay = cbits+3;
196
        } else {
197
                int     na=nbits+2, nb=cbits+1;
198
                if (nb<na) {
199
                        int tmp = nb;
200
                        nb = na; na = tmp;
201
                } delay = ((na)/2+(na&1)+2);
202
        }
203 14 dgisselq
        return delay;
204 2 dgisselq
}
205
 
206 14 dgisselq
int     lgdelay(int nbits, int xtra) {
207
        // The butterfly code needs to compare a valid address, of this
208
        // many bits, with an address two greater.  This guarantees we
209
        // have enough bits for that comparison.  We'll also end up with
210 33 dgisselq
        // more storage space to look for these values, but without a
211 14 dgisselq
        // redesign that's just what we'll deal with.
212
        return lgval(bflydelay(nbits, xtra)+3);
213
}
214
 
215 23 dgisselq
void    build_truncator(const char *fname) {
216
        printf("TRUNCATING!\n");
217 2 dgisselq
        FILE    *fp = fopen(fname, "w");
218
        if (NULL == fp) {
219
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
220
                perror("O/S Err was:");
221
                return;
222
        }
223
 
224
        fprintf(fp,
225
"///////////////////////////////////////////////////////////////////////////\n"
226
"//\n"
227 23 dgisselq
"// Filename:   truncate.v\n"
228
"//             \n"
229
"// Project:    %s\n"
230
"//\n"
231
"// Purpose:    Truncation is one of several options that can be used\n"
232
"//             internal to the various FFT stages to drop bits from one \n"
233
"//             stage to the next.  In general, it is the simplest method\n"
234
"//             of dropping bits, since it requires only a bit selection.\n"
235
"//\n"
236
"//             This form of rounding isn\'t really that great for FFT\'s,\n"
237
"//             since it tends to produce a DC bias in the result.  (Other\n"
238
"//             less pronounced biases may also exist.)\n"
239
"//\n"
240
"//             This particular version also registers the output with the\n"
241
"//             clock, so there will be a delay of one going through this\n"
242
"//             module.  This will keep it in line with the other forms of\n"
243
"//             rounding that can be used.\n"
244
"//\n"
245
"//\n%s"
246
"//\n",
247
                prjname, creator);
248
 
249
        fprintf(fp, "%s", cpyleft);
250
        fprintf(fp,
251
"module truncate(i_clk, i_ce, i_val, o_val);\n"
252
        "\tparameter\tIWID=16, OWID=8, SHIFT=0;\n"
253
        "\tinput\t\t\t\t\ti_clk, i_ce;\n"
254
        "\tinput\t\tsigned\t[(IWID-1):0]\ti_val;\n"
255
        "\toutput\treg\tsigned\t[(OWID-1):0]\to_val;\n"
256
"\n"
257
        "\talways @(posedge i_clk)\n"
258
                "\t\tif (i_ce)\n"
259
                "\t\t\to_val <= i_val[(IWID-1-SHIFT):(IWID-SHIFT-OWID)];\n"
260
"\n"
261
"endmodule\n");
262
}
263
 
264
 
265
void    build_roundhalfup(const char *fname) {
266
        FILE    *fp = fopen(fname, "w");
267
        if (NULL == fp) {
268
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
269
                perror("O/S Err was:");
270
                return;
271
        }
272
 
273
        fprintf(fp,
274
"///////////////////////////////////////////////////////////////////////////\n"
275
"//\n"
276
"// Filename:   roundhalfup.v\n"
277
"//             \n"
278
"// Project:    %s\n"
279
"//\n"
280
"// Purpose:    Rounding half up is the way I was always taught to round in\n"
281
"//             school.  A one half value is added to the result, and then\n"
282
"//             the result is truncated.  When used in an FFT, this produces\n"
283
"//             less bias than the truncation method, although a bias still\n"
284
"//             tends to remain.\n"
285
"//\n"
286
"//\n%s"
287
"//\n",
288
                prjname, creator);
289
 
290
        fprintf(fp, "%s", cpyleft);
291
        fprintf(fp,
292
"module roundhalfup(i_clk, i_ce, i_val, o_val);\n"
293
        "\tparameter\tIWID=16, OWID=8, SHIFT=0;\n"
294
        "\tinput\t\t\t\t\ti_clk, i_ce;\n"
295
        "\tinput\t\tsigned\t[(IWID-1):0]\ti_val;\n"
296
        "\toutput\treg\tsigned\t[(OWID-1):0]\to_val;\n"
297
"\n"
298
        "\t// Let's deal with two cases to be as general as we can be here\n"
299
        "\t//\n"
300
        "\t//   1. The desired output would lose no bits at all\n"
301
        "\t//   2. One or more bits would be dropped, so the rounding is simply\n"
302
        "\t//\t\ta matter of adding one to the bit about to be dropped,\n"
303
        "\t//\t\tmoving all halfway and above numbers up to the next\n"
304
        "\t//\t\tvalue.\n"
305
        "\tgenerate\n"
306
        "\tif (IWID-SHIFT == OWID)\n"
307
        "\tbegin // No truncation or rounding, output drops no bits\n"
308
"\n"
309
                "\t\talways @(posedge i_clk)\n"
310
                        "\t\t\tif (i_ce)\to_val <= i_val[(IWID-SHIFT-1):0];\n"
311
"\n"
312
        "\tend else // if (IWID-SHIFT-1 >= OWID)\n"
313
        "\tbegin // Output drops one bit, can only add one or ... not.\n"
314
                "\t\twire\t[(OWID-1):0] truncated_value, rounded_up;\n"
315
                "\t\twire\t\t\tlast_valid_bit, first_lost_bit;\n"
316
                "\t\tassign\ttruncated_value=i_val[(IWID-1-SHIFT):(IWID-SHIFT-OWID)];\n"
317 26 dgisselq
                "\t\tassign\trounded_up=truncated_value + {{(OWID-1){1\'b0}}, 1\'b1 };\n"
318 23 dgisselq
                "\t\tassign\tfirst_lost_bit = i_val[(IWID-SHIFT-OWID-1)];\n"
319
"\n"
320
                "\t\talways @(posedge i_clk)\n"
321
                "\t\t\tif (i_ce)\n"
322
                "\t\t\tbegin\n"
323
                        "\t\t\t\tif (~first_lost_bit) // Round down / truncate\n"
324
                        "\t\t\t\t\to_val <= truncated_value;\n"
325
                        "\t\t\t\telse\n"
326
                        "\t\t\t\t\to_val <= rounded_up; // even value\n"
327
                "\t\t\tend\n"
328
"\n"
329
        "\tend\n"
330
        "\tendgenerate\n"
331
"\n"
332
"endmodule\n");
333
}
334
 
335
void    build_roundfromzero(const char *fname) {
336
        FILE    *fp = fopen(fname, "w");
337
        if (NULL == fp) {
338
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
339
                perror("O/S Err was:");
340
                return;
341
        }
342
 
343
        fprintf(fp,
344
"///////////////////////////////////////////////////////////////////////////\n"
345
"//\n"
346
"// Filename:   roundfromzero.v\n"
347
"//             \n"
348
"// Project:    %s\n"
349
"//\n"
350
"// Purpose:    Truncation is one of several options that can be used\n"
351
"//             internal to the various FFT stages to drop bits from one \n"
352
"//             stage to the next.  In general, it is the simplest method\n"
353
"//             of dropping bits, since it requires only a bit selection.\n"
354
"//\n"
355
"//             This form of rounding isn\'t really that great for FFT\'s,\n"
356
"//             since it tends to produce a DC bias in the result.  (Other\n"
357
"//             less pronounced biases may also exist.)\n"
358
"//\n"
359
"//             This particular version also registers the output with the\n"
360
"//             clock, so there will be a delay of one going through this\n"
361
"//             module.  This will keep it in line with the other forms of\n"
362
"//             rounding that can be used.\n"
363
"//\n"
364
"//\n%s"
365
"//\n",
366
                prjname, creator);
367
 
368
        fprintf(fp, "%s", cpyleft);
369
        fprintf(fp,
370 33 dgisselq
"module roundfromzero(i_clk, i_ce, i_val, o_val);\n"
371 23 dgisselq
        "\tparameter\tIWID=16, OWID=8, SHIFT=0;\n"
372
        "\tinput\t\t\t\t\ti_clk, i_ce;\n"
373
        "\tinput\t\tsigned\t[(IWID-1):0]\ti_val;\n"
374
        "\toutput\treg\tsigned\t[(OWID-1):0]\to_val;\n"
375
"\n"
376
        "\t// Let's deal with three cases to be as general as we can be here\n"
377
        "\t//\n"
378
        "\t//\t1. The desired output would lose no bits at all\n"
379
        "\t//\t2. One bit would be dropped, so the rounding is simply\n"
380
        "\t//\t\tadjusting the value to be the closer to zero in\n"
381
        "\t//\t\tcases of being halfway between two.  If identically\n"
382
        "\t//\t\tequal to a number, we just leave it as is.\n"
383
        "\t//\t3. Two or more bits would be dropped.  In this case, we round\n"
384
        "\t//\t\tnormally unless we are rounding a value of exactly\n"
385
        "\t//\t\thalfway between the two.  In the halfway case, we\n"
386
        "\t//\t\tround away from zero.\n"
387
        "\tgenerate\n"
388 28 dgisselq
        "\tif (IWID == OWID) // In this case, the shift is irrelevant and\n"
389
        "\tbegin // cannot be applied.  No truncation or rounding takes\n"
390
        "\t// effect here.\n"
391
"\n"
392
                "\t\talways @(posedge i_clk)\n"
393
                        "\t\t\tif (i_ce)\to_val <= i_val[(IWID-1):0];\n"
394
"\n"
395
        "\tend else if (IWID-SHIFT == OWID)\n"
396 23 dgisselq
        "\tbegin // No truncation or rounding, output drops no bits\n"
397
"\n"
398
                "\t\talways @(posedge i_clk)\n"
399
                        "\t\t\tif (i_ce)\to_val <= i_val[(IWID-SHIFT-1):0];\n"
400
"\n"
401
        "\tend else if (IWID-SHIFT-1 == OWID)\n"
402
        "\tbegin // Output drops one bit, can only add one or ... not.\n"
403
        "\t\twire\t[(OWID-1):0]\ttruncated_value, rounded_up;\n"
404
        "\t\twire\t\t\tsign_bit, first_lost_bit;\n"
405
        "\t\tassign\ttruncated_value=i_val[(IWID-1-SHIFT):(IWID-SHIFT-OWID)];\n"
406 26 dgisselq
        "\t\tassign\trounded_up=truncated_value + {{(OWID-1){1\'b0}}, 1\'b1 };\n"
407 23 dgisselq
        "\t\tassign\tfirst_lost_bit = i_val[0];\n"
408
        "\t\tassign\tsign_bit = i_val[(IWID-1)];\n"
409
"\n"
410
        "\t\talways @(posedge i_clk)\n"
411
                "\t\t\tif (i_ce)\n"
412
                "\t\t\tbegin\n"
413
                        "\t\t\t\tif (~first_lost_bit) // Round down / truncate\n"
414
                                "\t\t\t\t\to_val <= truncated_value;\n"
415
                        "\t\t\t\telse if (sign_bit)\n"
416
                                "\t\t\t\t\to_val <= truncated_value;\n"
417
                        "\t\t\t\telse\n"
418
                                "\t\t\t\t\to_val <= rounded_up;\n"
419
                "\t\t\tend\n"
420
"\n"
421
        "\tend else // If there's more than one bit we are dropping\n"
422
        "\tbegin\n"
423
                "\t\twire\t[(OWID-1):0]\ttruncated_value, rounded_up;\n"
424
                "\t\twire\t\t\tsign_bit, first_lost_bit;\n"
425
                "\t\tassign\ttruncated_value=i_val[(IWID-1-SHIFT):(IWID-SHIFT-OWID)];\n"
426 26 dgisselq
                "\t\tassign\trounded_up=truncated_value + {{(OWID-1){1\'b0}}, 1\'b1 };\n"
427 23 dgisselq
                "\t\tassign\tfirst_lost_bit = i_val[(IWID-SHIFT-OWID-1)];\n"
428
                "\t\tassign\tsign_bit = i_val[(IWID-1)];\n"
429
"\n"
430
                "\t\twire\t[(IWID-SHIFT-OWID-2):0]\tother_lost_bits;\n"
431
                "\t\tassign\tother_lost_bits = i_val[(IWID-SHIFT-OWID-2):0];\n"
432
"\n"
433
                "\t\talways @(posedge i_clk)\n"
434
                        "\t\t\tif (i_ce)\n"
435
                        "\t\t\tbegin\n"
436
                        "\t\t\t\tif (~first_lost_bit) // Round down / truncate\n"
437
                                "\t\t\t\t\to_val <= truncated_value;\n"
438
                        "\t\t\t\telse if (|other_lost_bits) // Round up to\n"
439
                                "\t\t\t\t\to_val <= rounded_up; // closest value\n"
440
                        "\t\t\t\telse if (sign_bit)\n"
441
                                "\t\t\t\t\to_val <= truncated_value;\n"
442
                        "\t\t\t\telse\n"
443
                                "\t\t\t\t\to_val <= rounded_up;\n"
444
                        "\t\t\tend\n"
445
        "\tend\n"
446
        "\tendgenerate\n"
447
"\n"
448
"endmodule\n");
449
}
450
 
451
void    build_convround(const char *fname) {
452
        FILE    *fp = fopen(fname, "w");
453
        if (NULL == fp) {
454
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
455
                perror("O/S Err was:");
456
                return;
457
        }
458
 
459
        fprintf(fp,
460
"///////////////////////////////////////////////////////////////////////////\n"
461
"//\n"
462
"// Filename:   convround.v\n"
463
"//             \n"
464
"// Project:    %s\n"
465
"//\n"
466
"// Purpose:    A convergent rounding routine, also known as banker\'s\n"
467
"//             rounding, Dutch rounding, Gaussian rounding, unbiased\n"
468 33 dgisselq
"//     rounding, or ... more, at least according to Wikipedia.\n"
469 23 dgisselq
"//\n"
470 33 dgisselq
"//     This form of rounding works by rounding, when the direction is in\n"
471
"//     question, towards the nearest even value.\n"
472 23 dgisselq
"//\n"
473
"//\n%s"
474
"//\n",
475
                prjname, creator);
476
 
477
        fprintf(fp, "%s", cpyleft);
478
        fprintf(fp,
479
"module convround(i_clk, i_ce, i_val, o_val);\n"
480
"\tparameter\tIWID=16, OWID=8, SHIFT=0;\n"
481
"\tinput\t\t\t\t\ti_clk, i_ce;\n"
482
"\tinput\t\tsigned\t[(IWID-1):0]\ti_val;\n"
483
"\toutput\treg\tsigned\t[(OWID-1):0]\to_val;\n"
484
"\n"
485
"\t// Let's deal with three cases to be as general as we can be here\n"
486
"\t//\n"
487
"\t//\t1. The desired output would lose no bits at all\n"
488
"\t//\t2. One bit would be dropped, so the rounding is simply\n"
489
"\t//\t\tadjusting the value to be the nearest even number in\n"
490
"\t//\t\tcases of being halfway between two.  If identically\n"
491
"\t//\t\tequal to a number, we just leave it as is.\n"
492
"\t//\t3. Two or more bits would be dropped.  In this case, we round\n"
493
"\t//\t\tnormally unless we are rounding a value of exactly\n"
494
"\t//\t\thalfway between the two.  In the halfway case we round\n"
495
"\t//\t\tto the nearest even number.\n"
496
"\tgenerate\n"
497 33 dgisselq
// What if IWID < OWID?  We should expand here ... somehow
498 28 dgisselq
        "\tif (IWID == OWID) // In this case, the shift is irrelevant and\n"
499
        "\tbegin // cannot be applied.  No truncation or rounding takes\n"
500
        "\t// effect here.\n"
501
"\n"
502
                "\t\talways @(posedge i_clk)\n"
503
                        "\t\t\tif (i_ce)\to_val <= i_val[(IWID-1):0];\n"
504
"\n"
505 33 dgisselq
// What if IWID-SHIFT < OWID?  Shouldn't we also shift here as well?
506 28 dgisselq
"\tend else if (IWID-SHIFT == OWID)\n"
507 23 dgisselq
"\tbegin // No truncation or rounding, output drops no bits\n"
508
"\n"
509
"\t\talways @(posedge i_clk)\n"
510
"\t\t\tif (i_ce)\to_val <= i_val[(IWID-SHIFT-1):0];\n"
511
"\n"
512
"\tend else if (IWID-SHIFT-1 == OWID)\n"
513 33 dgisselq
// Is there any way to limit the number of bits that are examined here, for the
514
// purpose of simplifying/reducing logic?  I mean, if we go from 32 to 16 bits,
515
// must we check all 15 bits for equality to zero?
516 23 dgisselq
"\tbegin // Output drops one bit, can only add one or ... not.\n"
517
"\t\twire\t[(OWID-1):0] truncated_value, rounded_up;\n"
518
"\t\twire\t\t\tlast_valid_bit, first_lost_bit;\n"
519
"\t\tassign\ttruncated_value=i_val[(IWID-1-SHIFT):(IWID-SHIFT-OWID)];\n"
520 26 dgisselq
"\t\tassign\trounded_up=truncated_value + {{(OWID-1){1\'b0}}, 1\'b1 };\n"
521 23 dgisselq
"\t\tassign\tlast_valid_bit = truncated_value[0];\n"
522
"\t\tassign\tfirst_lost_bit = i_val[0];\n"
523
"\n"
524
"\t\talways @(posedge i_clk)\n"
525
"\t\t\tif (i_ce)\n"
526
"\t\t\tbegin\n"
527
"\t\t\t\tif (~first_lost_bit) // Round down / truncate\n"
528
"\t\t\t\t\to_val <= truncated_value;\n"
529
"\t\t\t\telse if (last_valid_bit)// Round up to nearest\n"
530
"\t\t\t\t\to_val <= rounded_up; // even value\n"
531
"\t\t\t\telse // else round down to the nearest\n"
532
"\t\t\t\t\to_val <= truncated_value; // even value\n"
533
"\t\t\tend\n"
534
"\n"
535
"\tend else // If there's more than one bit we are dropping\n"
536
"\tbegin\n"
537
"\t\twire\t[(OWID-1):0] truncated_value, rounded_up;\n"
538
"\t\twire\t\t\tlast_valid_bit, first_lost_bit;\n"
539
"\t\tassign\ttruncated_value=i_val[(IWID-1-SHIFT):(IWID-SHIFT-OWID)];\n"
540 26 dgisselq
"\t\tassign\trounded_up=truncated_value + {{(OWID-1){1\'b0}}, 1\'b1 };\n"
541 23 dgisselq
"\t\tassign\tlast_valid_bit = truncated_value[0];\n"
542
"\t\tassign\tfirst_lost_bit = i_val[(IWID-SHIFT-OWID-1)];\n"
543
"\n"
544
"\t\twire\t[(IWID-SHIFT-OWID-2):0]\tother_lost_bits;\n"
545
"\t\tassign\tother_lost_bits = i_val[(IWID-SHIFT-OWID-2):0];\n"
546
"\n"
547
"\t\talways @(posedge i_clk)\n"
548
"\t\t\tif (i_ce)\n"
549
"\t\t\tbegin\n"
550
"\t\t\t\tif (~first_lost_bit) // Round down / truncate\n"
551
"\t\t\t\t\to_val <= truncated_value;\n"
552
"\t\t\t\telse if (|other_lost_bits) // Round up to\n"
553
"\t\t\t\t\to_val <= rounded_up; // closest value\n"
554
"\t\t\t\telse if (last_valid_bit) // Round up to\n"
555
"\t\t\t\t\to_val <= rounded_up; // nearest even\n"
556
"\t\t\t\telse   // else round down to nearest even\n"
557
"\t\t\t\t\to_val <= truncated_value;\n"
558
"\t\t\tend\n"
559
"\tend\n"
560
"\tendgenerate\n"
561
"\n"
562
"endmodule\n");
563
}
564
 
565 26 dgisselq
void    build_quarters(const char *fname, ROUND_T rounding, bool dbg=false) {
566 23 dgisselq
        FILE    *fp = fopen(fname, "w");
567
        if (NULL == fp) {
568
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
569
                perror("O/S Err was:");
570
                return;
571
        }
572
        const   char    *rnd_string;
573
        if (rounding == RND_TRUNCATE)
574
                rnd_string = "truncate";
575
        else if (rounding == RND_FROMZERO)
576
                rnd_string = "roundfromzero";
577
        else if (rounding == RND_HALFUP)
578
                rnd_string = "roundhalfup";
579
        else
580
                rnd_string = "convround";
581
 
582
 
583
        fprintf(fp,
584
"///////////////////////////////////////////////////////////////////////////\n"
585
"//\n"
586 26 dgisselq
"// Filename:   qtrstage%s.v\n"
587 2 dgisselq
"//             \n"
588
"// Project:    %s\n"
589
"//\n"
590 5 dgisselq
"// Purpose:    This file encapsulates the 4 point stage of a decimation in\n"
591
"//             frequency FFT.  This particular implementation is optimized\n"
592
"//             so that all of the multiplies are accomplished by additions\n"
593
"//             and multiplexers only.\n"
594
"//\n"
595 2 dgisselq
"//\n%s"
596
"//\n",
597 26 dgisselq
                (dbg)?"_dbg":"", prjname, creator);
598 2 dgisselq
        fprintf(fp, "%s", cpyleft);
599
 
600
        fprintf(fp,
601 26 dgisselq
"module\tqtrstage%s(i_clk, i_rst, i_ce, i_sync, i_data, o_data, o_sync%s);\n"
602 29 dgisselq
        "\tparameter    IWIDTH=%d, OWIDTH=IWIDTH+1;\n"
603 5 dgisselq
        "\t// Parameters specific to the core that should be changed when this\n"
604
        "\t// core is built ... Note that the minimum LGSPAN is 2.  Smaller \n"
605
        "\t// spans must use the fftdoubles stage.\n"
606 29 dgisselq
        "\tparameter\tLGWIDTH=%d, ODD=0, INVERSE=0,SHIFT=0;\n"
607 5 dgisselq
        "\tinput\t                              i_clk, i_rst, i_ce, i_sync;\n"
608
        "\tinput\t      [(2*IWIDTH-1):0]        i_data;\n"
609
        "\toutput\treg  [(2*OWIDTH-1):0]        o_data;\n"
610
        "\toutput\treg                          o_sync;\n"
611 29 dgisselq
        "\t\n", (dbg)?"_dbg":"", (dbg)?", o_dbg":"", TST_QTRSTAGE_IWIDTH,
612
        TST_QTRSTAGE_LGWIDTH);
613 26 dgisselq
        if (dbg) { fprintf(fp, "\toutput\twire\t[33:0]\t\t\to_dbg;\n"
614
                "\tassign\to_dbg = { ((o_sync)&&(i_ce)), i_ce, o_data[(2*OWIDTH-1):(2*OWIDTH-16)],\n"
615
                        "\t\t\t\t\to_data[(OWIDTH-1):(OWIDTH-16)] };\n"
616
"\n");
617
        }
618 14 dgisselq
        fprintf(fp,
619 5 dgisselq
        "\treg\t        wait_for_sync;\n"
620 23 dgisselq
        "\treg\t[3:0]   pipeline;\n"
621 2 dgisselq
"\n"
622 5 dgisselq
        "\treg\t[(IWIDTH):0]    sum_r, sum_i, diff_r, diff_i;\n"
623 2 dgisselq
"\n"
624 23 dgisselq
        "\treg\t[(2*OWIDTH-1):0]\tob_a;\n"
625
        "\twire\t[(2*OWIDTH-1):0]\tob_b;\n"
626
        "\treg\t[(OWIDTH-1):0]\t\tob_b_r, ob_b_i;\n"
627
        "\tassign\tob_b = { ob_b_r, ob_b_i };\n"
628 2 dgisselq
"\n"
629 23 dgisselq
        "\treg\t[(LGWIDTH-1):0]\t\tiaddr;\n"
630
        "\treg\t[(2*IWIDTH-1):0]\timem;\n"
631 2 dgisselq
"\n"
632 5 dgisselq
        "\twire\tsigned\t[(IWIDTH-1):0]\timem_r, imem_i;\n"
633
        "\tassign\timem_r = imem[(2*IWIDTH-1):(IWIDTH)];\n"
634
        "\tassign\timem_i = imem[(IWIDTH-1):0];\n"
635 2 dgisselq
"\n"
636 5 dgisselq
        "\twire\tsigned\t[(IWIDTH-1):0]\ti_data_r, i_data_i;\n"
637
        "\tassign\ti_data_r = i_data[(2*IWIDTH-1):(IWIDTH)];\n"
638
        "\tassign\ti_data_i = i_data[(IWIDTH-1):0];\n"
639 2 dgisselq
"\n"
640 5 dgisselq
        "\treg  [(2*OWIDTH-1):0]        omem;\n"
641 14 dgisselq
"\n");
642
        fprintf(fp,
643 23 dgisselq
        "\twire\tsigned\t[(OWIDTH-1):0]\trnd_sum_r, rnd_sum_i, rnd_diff_r, rnd_diff_i,\n");
644
        fprintf(fp,
645
        "\t\t\t\t\tn_rnd_diff_r, n_rnd_diff_i;\n");
646
        fprintf(fp,
647 26 dgisselq
        "\t%s #(IWIDTH+1,OWIDTH,SHIFT)\tdo_rnd_sum_r(i_clk, i_ce,\n"
648 23 dgisselq
        "\t\t\t\tsum_r, rnd_sum_r);\n\n", rnd_string);
649
        fprintf(fp,
650 26 dgisselq
        "\t%s #(IWIDTH+1,OWIDTH,SHIFT)\tdo_rnd_sum_i(i_clk, i_ce,\n"
651 23 dgisselq
        "\t\t\t\tsum_i, rnd_sum_i);\n\n", rnd_string);
652
        fprintf(fp,
653 26 dgisselq
        "\t%s #(IWIDTH+1,OWIDTH,SHIFT)\tdo_rnd_diff_r(i_clk, i_ce,\n"
654 23 dgisselq
        "\t\t\t\tdiff_r, rnd_diff_r);\n\n", rnd_string);
655
        fprintf(fp,
656 26 dgisselq
        "\t%s #(IWIDTH+1,OWIDTH,SHIFT)\tdo_rnd_diff_i(i_clk, i_ce,\n"
657 23 dgisselq
        "\t\t\t\tdiff_i, rnd_diff_i);\n\n", rnd_string);
658
        fprintf(fp, "\tassign n_rnd_diff_r = - rnd_diff_r;\n"
659
                "\tassign n_rnd_diff_i = - rnd_diff_i;\n");
660
/*
661
        fprintf(fp,
662 5 dgisselq
        "\twire [(IWIDTH-1):0]  rnd;\n"
663 9 dgisselq
        "\tgenerate\n"
664
        "\tif ((ROUND)&&((IWIDTH+1-OWIDTH-SHIFT)>0))\n"
665 26 dgisselq
                "\t\tassign rnd = { {(IWIDTH-1){1\'b0}}, 1\'b1 };\n"
666 9 dgisselq
        "\telse\n"
667 26 dgisselq
                "\t\tassign rnd = { {(IWIDTH){1\'b0}}};\n"
668 9 dgisselq
        "\tendgenerate\n"
669 2 dgisselq
"\n"
670 23 dgisselq
*/
671
        fprintf(fp,
672 25 dgisselq
        "\tinitial wait_for_sync = 1\'b1;\n"
673
        "\tinitial iaddr = 0;\n"
674 5 dgisselq
        "\talways @(posedge i_clk)\n"
675
                "\t\tif (i_rst)\n"
676
                "\t\tbegin\n"
677 26 dgisselq
                        "\t\t\twait_for_sync <= 1\'b1;\n"
678 5 dgisselq
                        "\t\t\tiaddr <= 0;\n"
679 23 dgisselq
                "\t\tend else if ((i_ce)&&((~wait_for_sync)||(i_sync)))\n"
680 5 dgisselq
                "\t\tbegin\n"
681 26 dgisselq
                        "\t\t\tiaddr <= iaddr + { {(LGWIDTH-1){1\'b0}}, 1\'b1 };\n"
682
                        "\t\t\twait_for_sync <= 1\'b0;\n"
683
                "\t\tend\n"
684
        "\talways @(posedge i_clk)\n"
685
                "\t\tif (i_ce)\n"
686 5 dgisselq
                        "\t\t\timem <= i_data;\n"
687 26 dgisselq
                "\n\n");
688 23 dgisselq
        fprintf(fp,
689
        "\t// Note that we don\'t check on wait_for_sync or i_sync here.\n"
690
        "\t// Why not?  Because iaddr will always be zero until after the\n"
691
        "\t// first i_ce, so we are safe.\n"
692 25 dgisselq
        "\tinitial pipeline = 4\'h0;\n"
693 23 dgisselq
        "\talways\t@(posedge i_clk)\n"
694
                "\t\tif (i_rst)\n"
695 26 dgisselq
                        "\t\t\tpipeline <= 4\'h0;\n"
696 23 dgisselq
                "\t\telse if (i_ce) // is our pipeline process full?  Which stages?\n"
697
                        "\t\t\tpipeline <= { pipeline[2:0], iaddr[0] };\n\n");
698
        fprintf(fp,
699
        "\t// This is the pipeline[-1] stage, pipeline[0] will be set next.\n"
700
        "\talways\t@(posedge i_clk)\n"
701
                "\t\tif ((i_ce)&&(iaddr[0]))\n"
702
                "\t\tbegin\n"
703
                        "\t\t\tsum_r  <= imem_r + i_data_r;\n"
704
                        "\t\t\tsum_i  <= imem_i + i_data_i;\n"
705
                        "\t\t\tdiff_r <= imem_r - i_data_r;\n"
706
                        "\t\t\tdiff_i <= imem_i - i_data_i;\n"
707
                "\t\tend\n\n");
708
        fprintf(fp,
709
        "\t// pipeline[1] takes sum_x and diff_x and produces rnd_x\n\n");
710
        fprintf(fp,
711 26 dgisselq
        "\t// Now for pipeline[2].  We can actually do this at all i_ce\n"
712
        "\t// clock times, since nothing will listen unless pipeline[3]\n"
713
        "\t// on the next clock.  Thus, we simplify this logic and do\n"
714
        "\t// it independent of pipeline[2].\n"
715 23 dgisselq
        "\talways\t@(posedge i_clk)\n"
716 26 dgisselq
                "\t\tif (i_ce)\n"
717 23 dgisselq
                "\t\tbegin\n"
718
                        "\t\t\tob_a <= { rnd_sum_r, rnd_sum_i };\n"
719
                        "\t\t\t// on Even, W = e^{-j2pi 1/4 0} = 1\n"
720
                        "\t\t\tif (ODD == 0)\n"
721 5 dgisselq
                        "\t\t\tbegin\n"
722 23 dgisselq
                        "\t\t\t\tob_b_r <= rnd_diff_r;\n"
723
                        "\t\t\t\tob_b_i <= rnd_diff_i;\n"
724
                        "\t\t\tend else if (INVERSE==0) begin\n"
725
                        "\t\t\t\t// on Odd, W = e^{-j2pi 1/4} = -j\n"
726
                        "\t\t\t\tob_b_r <=   rnd_diff_i;\n"
727
                        "\t\t\t\tob_b_i <= n_rnd_diff_r;\n"
728
                        "\t\t\tend else begin\n"
729
                        "\t\t\t\t// on Odd, W = e^{j2pi 1/4} = j\n"
730
                        "\t\t\t\tob_b_r <= n_rnd_diff_i;\n"
731
                        "\t\t\t\tob_b_i <=   rnd_diff_r;\n"
732 5 dgisselq
                        "\t\t\tend\n"
733 23 dgisselq
                "\t\tend\n\n");
734
        fprintf(fp,
735
        "\talways\t@(posedge i_clk)\n"
736
                "\t\tif (i_ce)\n"
737
                "\t\tbegin // In sequence, clock = 3\n"
738
                        "\t\t\tif (pipeline[3])\n"
739 5 dgisselq
                        "\t\t\tbegin\n"
740
                                "\t\t\t\tomem <= ob_b;\n"
741
                                "\t\t\t\to_data <= ob_a;\n"
742
                        "\t\t\tend else\n"
743
                                "\t\t\t\to_data <= omem;\n"
744 23 dgisselq
                "\t\tend\n\n");
745
 
746
        fprintf(fp,
747
        "\t// Don\'t forget in the sync check that we are running\n"
748
        "\t// at two clocks per sample.  Thus we need to\n"
749
        "\t// produce a sync every 2^(LGWIDTH-1) clocks.\n"
750 26 dgisselq
        "\tinitial\to_sync = 1\'b0;\n"
751 23 dgisselq
        "\talways\t@(posedge i_clk)\n"
752 26 dgisselq
                "\t\tif (i_rst)\n"
753
                "\t\t\to_sync <= 1\'b0;\n"
754
                "\t\telse if (i_ce)\n"
755 23 dgisselq
                        "\t\t\to_sync <= &(~iaddr[(LGWIDTH-2):3]) && (iaddr[2:0] == 3'b101);\n");
756
        fprintf(fp, "endmodule\n");
757 2 dgisselq
}
758
 
759 26 dgisselq
void    build_dblstage(const char *fname, ROUND_T rounding, const bool dbg = false) {
760 2 dgisselq
        FILE    *fp = fopen(fname, "w");
761
        if (NULL == fp) {
762
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
763
                perror("O/S Err was:");
764
                return;
765
        }
766
 
767 23 dgisselq
        const   char    *rnd_string;
768
        if (rounding == RND_TRUNCATE)
769
                rnd_string = "truncate";
770
        else if (rounding == RND_FROMZERO)
771
                rnd_string = "roundfromzero";
772
        else if (rounding == RND_HALFUP)
773
                rnd_string = "roundhalfup";
774
        else
775
                rnd_string = "convround";
776
 
777
 
778 2 dgisselq
        fprintf(fp,
779
"///////////////////////////////////////////////////////////////////////////\n"
780
"//\n"
781 26 dgisselq
"// Filename:   dblstage%s.v\n"
782 2 dgisselq
"//\n"
783
"// Project:    %s\n"
784
"//\n"
785
"// Purpose:    This is part of an FPGA implementation that will process\n"
786 5 dgisselq
"//             the final stage of a decimate-in-frequency FFT, running\n"
787
"//             through the data at two samples per clock.  If you notice\n"
788
"//             from the derivation of an FFT, the only time both even and\n"
789
"//             odd samples are used at the same time is in this stage.\n"
790
"//             Therefore, other than this stage and these twiddles, all of\n"
791
"//             the other stages can run two stages at a time at one sample\n"
792
"//             per clock.\n"
793 2 dgisselq
"//\n"
794
"//             In this implementation, the output is valid one clock after\n"
795
"//             the input is valid.  The output also accumulates one bit\n"
796
"//             above and beyond the number of bits in the input.\n"
797
"//             \n"
798
"//             i_clk   A system clock\n"
799 6 dgisselq
"//             i_rst   A synchronous reset\n"
800 2 dgisselq
"//             i_ce    Circuit enable--nothing happens unless this line is high\n"
801 6 dgisselq
"//             i_sync  A synchronization signal, high once per FFT at the start\n"
802 2 dgisselq
"//             i_left  The first (even) complex sample input.  The higher order\n"
803
"//                     bits contain the real portion, low order bits the\n"
804
"//                     imaginary portion, all in two\'s complement.\n"
805
"//             i_right The next (odd) complex sample input, same format as\n"
806
"//                     i_left.\n"
807
"//             o_left  The first (even) complex output.\n"
808
"//             o_right The next (odd) complex output.\n"
809 6 dgisselq
"//             o_sync  Output synchronization signal.\n"
810 2 dgisselq
"//\n%s"
811 26 dgisselq
"//\n", (dbg)?"_dbg":"", prjname, creator);
812 2 dgisselq
 
813
        fprintf(fp, "%s", cpyleft);
814 33 dgisselq
        fprintf(fp,
815 26 dgisselq
"module\tdblstage%s(i_clk, i_rst, i_ce, i_sync, i_left, i_right, o_left, o_right, o_sync%s);\n"
816 29 dgisselq
        "\tparameter\tIWIDTH=%d,OWIDTH=IWIDTH+1, SHIFT=%d;\n"
817 6 dgisselq
        "\tinput\t\ti_clk, i_rst, i_ce, i_sync;\n"
818 5 dgisselq
        "\tinput\t\t[(2*IWIDTH-1):0]\ti_left, i_right;\n"
819 28 dgisselq
        "\toutput\treg\t[(2*OWIDTH-1):0]\to_left, o_right;\n"
820 6 dgisselq
        "\toutput\treg\t\t\to_sync;\n"
821 29 dgisselq
        "\n", (dbg)?"_dbg":"", (dbg)?", o_dbg":"",
822
        TST_DBLSTAGE_IWIDTH, TST_DBLSTAGE_SHIFT);
823 26 dgisselq
 
824
        if (dbg) { fprintf(fp, "\toutput\twire\t[33:0]\t\t\to_dbg;\n"
825
                "\tassign\to_dbg = { ((o_sync)&&(i_ce)), i_ce, o_left[(2*OWIDTH-1):(2*OWIDTH-16)],\n"
826
                        "\t\t\t\t\to_left[(OWIDTH-1):(OWIDTH-16)] };\n"
827
"\n");
828
        }
829 33 dgisselq
        fprintf(fp,
830 5 dgisselq
        "\twire\tsigned\t[(IWIDTH-1):0]\ti_in_0r, i_in_0i, i_in_1r, i_in_1i;\n"
831
        "\tassign\ti_in_0r = i_left[(2*IWIDTH-1):(IWIDTH)]; \n"
832
        "\tassign\ti_in_0i = i_left[(IWIDTH-1):0]; \n"
833
        "\tassign\ti_in_1r = i_right[(2*IWIDTH-1):(IWIDTH)]; \n"
834
        "\tassign\ti_in_1i = i_right[(IWIDTH-1):0]; \n"
835
        "\twire\t[(OWIDTH-1):0]\t\to_out_0r, o_out_0i,\n"
836
                                "\t\t\t\t\to_out_1r, o_out_1i;\n"
837 2 dgisselq
"\n"
838 15 dgisselq
"\n"
839 19 dgisselq
        "\t// Handle a potential rounding situation, when IWIDTH>=OWIDTH.\n"
840 15 dgisselq
"\n"
841 23 dgisselq
"\n");
842
        fprintf(fp,
843 26 dgisselq
        "\n"
844
        "\t// As with any register connected to the sync pulse, these must\n"
845
        "\t// have initial values and be reset on the i_rst signal.\n"
846
        "\t// Other data values need only restrict their updates to i_ce\n"
847
        "\t// enabled clocks, but sync\'s must obey resets and initial\n"
848
        "\t// conditions as well.\n"
849 28 dgisselq
        "\treg\trnd_sync, r_sync;\n"
850 2 dgisselq
"\n"
851 28 dgisselq
        "\tinitial\trnd_sync      = 1\'b0; // Sync into rounding\n"
852
        "\tinitial\tr_sync        = 1\'b0; // Sync coming out\n"
853 5 dgisselq
        "\talways @(posedge i_clk)\n"
854 6 dgisselq
                "\t\tif (i_rst)\n"
855 23 dgisselq
                "\t\tbegin\n"
856 26 dgisselq
                        "\t\t\trnd_sync <= 1\'b0;\n"
857 28 dgisselq
                        "\t\t\tr_sync <= 1\'b0;\n"
858
                "\t\tend else if (i_ce)\n"
859 5 dgisselq
                "\t\tbegin\n"
860 26 dgisselq
                        "\t\t\trnd_sync <= i_sync;\n"
861 28 dgisselq
                        "\t\t\tr_sync <= rnd_sync;\n"
862 26 dgisselq
                "\t\tend\n"
863
"\n"
864
        "\t// As with other variables, these are really only updated when in\n"
865
        "\t// the processing pipeline, after the first i_sync.  However, to\n"
866
        "\t// eliminate as much unnecessary logic as possible, we toggle\n"
867 28 dgisselq
        "\t// these any time the i_ce line is enabled, and don\'t reset.\n"
868
        "\t// them on i_rst.\n");
869
        fprintf(fp,
870
        "\t// Don't forget that we accumulate a bit by adding two values\n"
871
        "\t// together. Therefore our intermediate value must have one more\n"
872
        "\t// bit than the two originals.\n"
873
        "\treg\tsigned\t[(IWIDTH):0]\trnd_in_0r, rnd_in_0i;\n"
874
        "\treg\tsigned\t[(IWIDTH):0]\trnd_in_1r, rnd_in_1i;\n\n"
875 26 dgisselq
        "\talways @(posedge i_clk)\n"
876
                "\t\tif (i_ce)\n"
877
                "\t\tbegin\n"
878
                        "\t\t\t//\n"
879 23 dgisselq
                        "\t\t\trnd_in_0r <= i_in_0r + i_in_1r;\n"
880
                        "\t\t\trnd_in_0i <= i_in_0i + i_in_1i;\n"
881 5 dgisselq
                        "\t\t\t//\n"
882 23 dgisselq
                        "\t\t\trnd_in_1r <= i_in_0r - i_in_1r;\n"
883
                        "\t\t\trnd_in_1i <= i_in_0i - i_in_1i;\n"
884 6 dgisselq
                        "\t\t\t//\n"
885 5 dgisselq
                "\t\tend\n"
886 28 dgisselq
"\n");
887
        fprintf(fp,
888
        "\t%s #(IWIDTH+1,OWIDTH,SHIFT) do_rnd_0r(i_clk, i_ce,\n"
889
        "\t\t\t\t\t\t\trnd_in_0r, o_out_0r);\n\n", rnd_string);
890
        fprintf(fp,
891
        "\t%s #(IWIDTH+1,OWIDTH,SHIFT) do_rnd_0i(i_clk, i_ce,\n"
892
        "\t\t\t\t\t\t\trnd_in_0i, o_out_0i);\n\n", rnd_string);
893
        fprintf(fp,
894
        "\t%s #(IWIDTH+1,OWIDTH,SHIFT) do_rnd_1r(i_clk, i_ce,\n"
895
        "\t\t\t\t\t\t\trnd_in_1r, o_out_1r);\n\n", rnd_string);
896
        fprintf(fp,
897
        "\t%s #(IWIDTH+1,OWIDTH,SHIFT) do_rnd_1i(i_clk, i_ce,\n"
898
        "\t\t\t\t\t\t\trnd_in_1i, o_out_1i);\n\n", rnd_string);
899
 
900
        fprintf(fp, "\n"
901
        "\t// Prior versions of this routine did not include the extra\n"
902
        "\t// clock and register/flip-flops that this routine requires.\n"
903
        "\t// These are placed in here to correct a bug in Verilator, that\n"
904
        "\t// otherwise struggles.  (Hopefully this will fix the problem ...)\n"
905
        "\talways @(posedge i_clk)\n"
906
                "\t\tif (i_ce)\n"
907
                "\t\tbegin\n"
908
                        "\t\t\to_left  <= { o_out_0r, o_out_0i };\n"
909
                        "\t\t\to_right <= { o_out_1r, o_out_1i };\n"
910
                "\t\tend\n"
911 2 dgisselq
"\n"
912 28 dgisselq
        "\tinitial\to_sync = 1'b0; // Final sync coming out of module\n"
913
        "\talways @(posedge i_clk)\n"
914
                "\t\tif (i_rst)\n"
915
                "\t\t\to_sync <= 1'b0;\n"
916
                "\t\telse if (i_ce)\n"
917
                "\t\t\to_sync <= r_sync;\n"
918 2 dgisselq
"\n"
919
"endmodule\n");
920
        fclose(fp);
921
}
922
 
923
void    build_multiply(const char *fname) {
924
        FILE    *fp = fopen(fname, "w");
925
        if (NULL == fp) {
926
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
927
                perror("O/S Err was:");
928
                return;
929
        }
930
 
931
        fprintf(fp,
932
"///////////////////////////////////////////////////////////////////////////\n"
933
"//\n"
934
"// Filename:   shiftaddmpy.v\n"
935
"//\n"
936
"// Project:    %s\n"
937
"//\n"
938
"// Purpose:    A portable shift and add multiply.\n"
939
"//\n"
940
"//             While both Xilinx and Altera will offer single clock \n"
941
"//             multiplies, this simple approach will multiply two numbers\n"
942
"//             on any architecture.  The result maintains the full width\n"
943
"//             of the multiply, there are no extra stuff bits, no rounding,\n"
944
"//             no shifted bits, etc.\n"
945
"//\n"
946
"//             Further, for those applications that can support it, this\n"
947
"//             multiply is pipelined and will produce one answer per clock.\n"
948
"//\n"
949
"//             For minimal processing delay, make the first parameter\n"
950
"//             the one with the least bits, so that AWIDTH <= BWIDTH.\n"
951
"//\n"
952
"//             The processing delay in this multiply is (AWIDTH+1) cycles.\n"
953
"//             That is, if the data is present on the input at clock t=0,\n"
954
"//             the result will be present on the output at time t=AWIDTH+1;\n"
955
"//\n"
956
"//\n%s"
957
"//\n", prjname, creator);
958
 
959
        fprintf(fp, "%s", cpyleft);
960 33 dgisselq
        fprintf(fp,
961 2 dgisselq
"module shiftaddmpy(i_clk, i_ce, i_a, i_b, o_r);\n"
962 29 dgisselq
        "\tparameter\tAWIDTH=%d,BWIDTH=", TST_SHIFTADDMPY_AW);
963
#ifdef  TST_SHIFTADDMPY_BW
964
        fprintf(fp, "%d;\n", TST_SHIFTADDMPY_BW);
965
#else
966
        fprintf(fp, "AWIDTH;\n");
967
#endif
968
        fprintf(fp,
969 2 dgisselq
        "\tinput\t\t\t\t\ti_clk, i_ce;\n"
970
        "\tinput\t\t[(AWIDTH-1):0]\t\ti_a;\n"
971
        "\tinput\t\t[(BWIDTH-1):0]\t\ti_b;\n"
972
        "\toutput\treg\t[(AWIDTH+BWIDTH-1):0]\to_r;\n"
973
"\n"
974
        "\treg\t[(AWIDTH-1):0]\tu_a;\n"
975
        "\treg\t[(BWIDTH-1):0]\tu_b;\n"
976
        "\treg\t\t\tsgn;\n"
977
"\n"
978
        "\treg\t[(AWIDTH-2):0]\t\tr_a[0:(AWIDTH-1)];\n"
979
        "\treg\t[(AWIDTH+BWIDTH-2):0]\tr_b[0:(AWIDTH-1)];\n"
980
        "\treg\t\t\t\tr_s[0:(AWIDTH-1)];\n"
981
        "\treg\t[(AWIDTH+BWIDTH-1):0]\tacc[0:(AWIDTH-1)];\n"
982
        "\tgenvar k;\n"
983
"\n"
984 5 dgisselq
        "\t// If we were forced to stay within two\'s complement arithmetic,\n"
985
        "\t// taking the absolute value here would require an additional bit.\n"
986
        "\t// However, because our results are now unsigned, we can stay\n"
987
        "\t// within the number of bits given (for now).\n"
988 2 dgisselq
        "\talways @(posedge i_clk)\n"
989
                "\t\tif (i_ce)\n"
990
                "\t\tbegin\n"
991
                        "\t\t\tu_a <= (i_a[AWIDTH-1])?(-i_a):(i_a);\n"
992
                        "\t\t\tu_b <= (i_b[BWIDTH-1])?(-i_b):(i_b);\n"
993
                        "\t\t\tsgn <= i_a[AWIDTH-1] ^ i_b[BWIDTH-1];\n"
994
                "\t\tend\n"
995
"\n"
996
        "\talways @(posedge i_clk)\n"
997
                "\t\tif (i_ce)\n"
998
                "\t\tbegin\n"
999 26 dgisselq
                        "\t\t\tacc[0] <= (u_a[0]) ? { {(AWIDTH){1\'b0}}, u_b }\n"
1000
                        "\t\t\t\t\t: {(AWIDTH+BWIDTH){1\'b0}};\n"
1001 2 dgisselq
                        "\t\t\tr_a[0] <= { u_a[(AWIDTH-1):1] };\n"
1002 26 dgisselq
                        "\t\t\tr_b[0] <= { {(AWIDTH-1){1\'b0}}, u_b };\n"
1003 2 dgisselq
                        "\t\t\tr_s[0] <= sgn; // The final sign, needs to be preserved\n"
1004
                "\t\tend\n"
1005
"\n"
1006
        "\tgenerate\n"
1007 21 dgisselq
        "\tfor(k=0; k<AWIDTH-1; k=k+1)\n"
1008 25 dgisselq
        "\tbegin : genstages\n"
1009 21 dgisselq
                "\t\talways @(posedge i_clk)\n"
1010
                "\t\tif (i_ce)\n"
1011 2 dgisselq
                "\t\tbegin\n"
1012 26 dgisselq
                        "\t\t\tacc[k+1] <= acc[k] + ((r_a[k][0]) ? {r_b[k],1\'b0}:0);\n"
1013
                        "\t\t\tr_a[k+1] <= { 1\'b0, r_a[k][(AWIDTH-2):1] };\n"
1014
                        "\t\t\tr_b[k+1] <= { r_b[k][(AWIDTH+BWIDTH-3):0], 1\'b0};\n"
1015 2 dgisselq
                        "\t\t\tr_s[k+1] <= r_s[k];\n"
1016
                "\t\tend\n"
1017
        "\tend\n"
1018
        "\tendgenerate\n"
1019
"\n"
1020
        "\talways @(posedge i_clk)\n"
1021
                "\t\tif (i_ce)\n"
1022
                        "\t\t\to_r <= (r_s[AWIDTH-1]) ? (-acc[AWIDTH-1]) : acc[AWIDTH-1];\n"
1023
"\n"
1024
"endmodule\n");
1025
 
1026
        fclose(fp);
1027
}
1028
 
1029 29 dgisselq
void    build_bimpy(const char *fname) {
1030
        FILE    *fp = fopen(fname, "w");
1031
        if (NULL == fp) {
1032
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
1033
                perror("O/S Err was:");
1034
                return;
1035
        }
1036
 
1037
        fprintf(fp,
1038
"////////////////////////////////////////////////////////////////////////////////\n"
1039
"//\n"
1040
"// Filename:   %s\n"
1041
"//\n"
1042
"// Project:    %s\n"
1043
"//\n"
1044
"// Purpose:    A simple 2-bit multiply based upon the fact that LUT's allow\n"
1045
"//             6-bits of input.  In other words, I could build a 3-bit\n"
1046
"//             multiply from 6 LUTs (5 actually, since the first could have\n"
1047
"//             two outputs).  This would allow multiplication of three bit\n"
1048
"//             digits, save only for the fact that you would need two bits\n"
1049
"//             of carry.  The bimpy approach throttles back a bit and does\n"
1050
"//             a 2x2 bit multiply in a LUT, guaranteeing that it will never\n"
1051
"//             carry more than one bit.  While this multiply is hardware\n"
1052
"//             independent (and can still run under Verilator therefore),\n"
1053
"//             it is really motivated by trying to optimize for a specific\n"
1054
"//             piece of hardware (Xilinx-7 series ...) that has at least\n"
1055
"//             4-input LUT's with carry chains.\n"
1056
"//\n"
1057
"//\n"
1058
"//\n%s"
1059
"//\n", fname, prjname, creator);
1060
 
1061
        fprintf(fp, "%s", cpyleft);
1062 33 dgisselq
        fprintf(fp,
1063 29 dgisselq
"module bimpy(i_clk, i_ce, i_a, i_b, o_r);\n"
1064
"\tparameter\tBW=18, // Number of bits in i_b\n"
1065
"\t\t\tLUTB=2; // Number of bits in i_a for our LUT multiply\n"
1066
"\tinput\t\t\t\ti_clk, i_ce;\n"
1067
"\tinput\t\t[(LUTB-1):0]\ti_a;\n"
1068
"\tinput\t\t[(BW-1):0]\ti_b;\n"
1069
"\toutput\treg\t[(BW+LUTB-1):0] o_r;\n"
1070
"\n"
1071
"\twire [(BW+LUTB-2):0] w_r;\n"
1072
"\twire [(BW+LUTB-3):1] c;\n"
1073
"\n"
1074
"\tassign\tw_r =  { ((i_a[1])?i_b:{(BW){1'b0}}), 1'b0 }\n"
1075
"\t\t\t\t^ { 1'b0, ((i_a[0])?i_b:{(BW){1'b0}}) };\n"
1076
"\tassign\tc = { ((i_a[1])?i_b[(BW-2):0]:{(BW-1){1'b0}}) }\n"
1077
"\t\t\t& ((i_a[0])?i_b[(BW-1):1]:{(BW-1){1'b0}});\n"
1078
"\n"
1079
"\talways @(posedge i_clk)\n"
1080
"\t\tif (i_ce)\n"
1081
"\t\t\to_r <= w_r + { c, 2'b0 };\n"
1082
"\n"
1083
"endmodule\n");
1084
 
1085
        fclose(fp);
1086
}
1087
 
1088
void    build_longbimpy(const char *fname) {
1089
        FILE    *fp = fopen(fname, "w");
1090
        if (NULL == fp) {
1091
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
1092
                perror("O/S Err was:");
1093
                return;
1094
        }
1095
 
1096
        fprintf(fp,
1097
"////////////////////////////////////////////////////////////////////////////////\n"
1098
"//\n"
1099
"// Filename:   %s\n"
1100
"//\n"
1101
"// Project:    %s\n"
1102
"//\n"
1103
"// Purpose:    A portable shift and add multiply, built with the knowledge\n"
1104
"//             of the existence of a six bit LUT and carry chain.  That\n"
1105
"//             knowledge allows us to multiply two bits from one value\n"
1106
"//             at a time against all of the bits of the other value.  This\n"
1107
"//             sub multiply is called the bimpy.\n"
1108
"//\n"
1109
"//             For minimal processing delay, make the first parameter\n"
1110
"//             the one with the least bits, so that AWIDTH <= BWIDTH.\n"
1111
"//\n"
1112
"//\n"
1113
"//\n%s"
1114
"//\n", fname, prjname, creator);
1115
 
1116
        fprintf(fp, "%s", cpyleft);
1117 33 dgisselq
        fprintf(fp,
1118 29 dgisselq
"module longbimpy(i_clk, i_ce, i_a, i_b, o_r);\n"
1119
        "\tparameter    AW=%d,  // The width of i_a, min width is 5\n"
1120
                        "\t\t\tBW=", TST_LONGBIMPY_AW);
1121
#ifdef  TST_LONGBIMPY_BW
1122
        fprintf(fp, "%d", TST_LONGBIMPY_BW);
1123
#else
1124
        fprintf(fp, "AW");
1125
#endif
1126
 
1127
        fprintf(fp, ",  // The width of i_b, can be anything\n"
1128
                        "\t\t\t// The following three parameters should not be changed\n"
1129
                        "\t\t\t// by any implementation, but are based upon hardware\n"
1130
                        "\t\t\t// and the above values:\n"
1131
                        "\t\t\tOW=AW+BW,        // The output width\n"
1132
                        "\t\t\tIW=(AW+1)&(-2),  // Internal width of A\n"
1133
                        "\t\t\tLUTB=2,  // How many bits we can multiply by at once\n"
1134
                        "\t\t\tTLEN=(AW+(LUTB-1))/LUTB; // Nmbr of rows in our tableau\n"
1135
        "\tinput\t\t\t\ti_clk, i_ce;\n"
1136
        "\tinput\t\t[(AW-1):0]\ti_a;\n"
1137
        "\tinput\t\t[(BW-1):0]\ti_b;\n"
1138
        "\toutput\treg\t[(AW+BW-1):0]\to_r;\n"
1139
"\n"
1140
        "\treg\t[(IW-1):0]\tu_a;\n"
1141
        "\treg\t[(BW-1):0]\tu_b;\n"
1142
        "\treg\t\t\tsgn;\n"
1143
"\n"
1144
        "\treg\t[(IW-1-2*(LUTB)):0]\tr_a[0:(TLEN-3)];\n"
1145
        "\treg\t[(BW-1):0]\t\tr_b[0:(TLEN-3)];\n"
1146
        "\treg\t[(TLEN-1):0]\t\tr_s;\n"
1147
        "\treg\t[(IW+BW-1):0]\t\tacc[0:(TLEN-2)];\n"
1148
        "\tgenvar k;\n"
1149
"\n"
1150
        "\t// First step:\n"
1151
        "\t// Switch to unsigned arithmetic for our multiply, keeping track\n"
1152
        "\t// of the along the way.  We'll then add the sign again later at\n"
1153
        "\t// the end.\n"
1154
        "\t//\n"
1155
        "\t// If we were forced to stay within two's complement arithmetic,\n"
1156
        "\t// taking the absolute value here would require an additional bit.\n"
1157
        "\t// However, because our results are now unsigned, we can stay\n"
1158
        "\t// within the number of bits given (for now).\n"
1159
        "\tgenerate if (IW > AW)\n"
1160
        "\tbegin\n"
1161
                "\t\talways @(posedge i_clk)\n"
1162
                        "\t\t\tif (i_ce)\n"
1163
                        "\t\t\t\tu_a <= { 1'b0, (i_a[AW-1])?(-i_a):(i_a) };\n"
1164
        "\tend else begin\n"
1165
                "\t\talways @(posedge i_clk)\n"
1166
                        "\t\t\tif (i_ce)\n"
1167
                        "\t\t\t\tu_a <= (i_a[AW-1])?(-i_a):(i_a);\n"
1168
        "\tend endgenerate\n"
1169
"\n"
1170
        "\talways @(posedge i_clk)\n"
1171
                "\t\tif (i_ce)\n"
1172
                "\t\tbegin\n"
1173
                        "\t\t\tu_b <= (i_b[BW-1])?(-i_b):(i_b);\n"
1174
                        "\t\t\tsgn <= i_a[AW-1] ^ i_b[BW-1];\n"
1175
                "\t\tend\n"
1176
"\n"
1177
        "\twire [(BW+LUTB-1):0] pr_a, pr_b;\n"
1178
"\n"
1179
        "\t//\n"
1180
        "\t// Second step: First two 2xN products.\n"
1181
        "\t//\n"
1182
        "\t// Since we have no tableau of additions (yet), we can do both\n"
1183
        "\t// of the first two rows at the same time and add them together.\n"
1184
        "\t// For the next round, we'll then have a previous sum to accumulate\n"
1185
        "\t// with new and subsequent product, and so only do one product at\n"
1186
        "\t// a time can follow this--but the first clock can do two at a time.\n"
1187
        "\tbimpy\t#(BW) lmpy_0(i_clk,i_ce,u_a[(  LUTB-1):   0], u_b, pr_a);\n"
1188
        "\tbimpy\t#(BW) lmpy_1(i_clk,i_ce,u_a[(2*LUTB-1):LUTB], u_b, pr_b);\n"
1189
        "\talways @(posedge i_clk)\n"
1190
                "\t\tif (i_ce) r_a[0] <= u_a[(IW-1):(2*LUTB)];\n"
1191
        "\talways @(posedge i_clk)\n"
1192
                "\t\tif (i_ce) r_b[0] <= u_b;\n"
1193
        "\talways @(posedge i_clk)\n"
1194
                "\t\tif (i_ce) r_s <= { r_s[(TLEN-2):0], sgn };\n"
1195
        "\talways @(posedge i_clk) // One clk after p[0],p[1] become valid\n"
1196
                "\t\tif (i_ce) acc[0] <= { {(IW-LUTB){1'b0}}, pr_a}\n"
1197
                        "\t\t\t  +{ {(IW-(2*LUTB)){1'b0}}, pr_b, {(LUTB){1'b0}} };\n"
1198
"\n"
1199
        "\tgenerate // Keep track of intermediate values, before multiplying them\n"
1200
        "\tif (TLEN > 3) for(k=0; k<TLEN-3; k=k+1)\n"
1201
        "\tbegin : gencopies\n"
1202
                "\t\talways @(posedge i_clk)\n"
1203
                "\t\tif (i_ce)\n"
1204
                "\t\tbegin\n"
1205
                        "\t\t\tr_a[k+1] <= { {(LUTB){1'b0}},\n"
1206
                                "\t\t\t\tr_a[k][(IW-1-(2*LUTB)):LUTB] };\n"
1207
                        "\t\t\tr_b[k+1] <= r_b[k];\n"
1208
                        "\t\tend\n"
1209
        "\tend endgenerate\n"
1210
"\n"
1211
        "\tgenerate // The actual multiply and accumulate stage\n"
1212
        "\tif (TLEN > 2) for(k=0; k<TLEN-2; k=k+1)\n"
1213
        "\tbegin : genstages\n"
1214
                "\t\t// First, the multiply: 2-bits times BW bits\n"
1215
                "\t\twire\t[(BW+LUTB-1):0] genp;\n"
1216
                "\t\tbimpy #(BW) genmpy(i_clk,i_ce,r_a[k][(LUTB-1):0],r_b[k], genp);\n"
1217
"\n"
1218
                "\t\t// Then the accumulate step -- on the next clock\n"
1219
                "\t\talways @(posedge i_clk)\n"
1220
                        "\t\t\tif (i_ce)\n"
1221
                                "\t\t\t\tacc[k+1] <= acc[k] + {{(IW-LUTB*(k+3)){1'b0}},\n"
1222
                                        "\t\t\t\t\tgenp, {{(LUTB*(k+2))}{1'b0}} };\n"
1223
        "\tend endgenerate\n"
1224
"\n"
1225
        "\twire [(IW+BW-1):0]   w_r;\n"
1226
        "\tassign\tw_r = (r_s[TLEN-1]) ? (-acc[TLEN-2]) : acc[TLEN-2];\n"
1227
        "\talways @(posedge i_clk)\n"
1228
                "\t\tif (i_ce)\n"
1229
                        "\t\t\to_r <= w_r[(AW+BW-1):0];\n"
1230
"\n"
1231
"endmodule\n");
1232
 
1233
        fclose(fp);
1234
}
1235
 
1236 2 dgisselq
void    build_dblreverse(const char *fname) {
1237
        FILE    *fp = fopen(fname, "w");
1238
        if (NULL == fp) {
1239
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
1240
                perror("O/S Err was:");
1241
                return;
1242
        }
1243
 
1244
        fprintf(fp,
1245
"///////////////////////////////////////////////////////////////////////////\n"
1246
"//\n"
1247
"// Filename:   dblreverse.v\n"
1248
"//\n"
1249
"// Project:    %s\n"
1250
"//\n"
1251
"// Purpose:    This module bitreverses a pipelined FFT input.  Operation is\n"
1252
"//             expected as follows:\n"
1253
"//\n"
1254
"//             i_clk   A running clock at whatever system speed is offered.\n"
1255
"//             i_rst   A synchronous reset signal, that resets all internals\n"
1256
"//             i_ce    If this is one, one input is consumed and an output\n"
1257
"//                     is produced.\n"
1258
"//             i_in_0, i_in_1\n"
1259
"//                     Two inputs to be consumed, each of width WIDTH.\n"
1260
"//             o_out_0, o_out_1\n"
1261
"//                     Two of the bitreversed outputs, also of the same\n"
1262
"//                     width, WIDTH.  Of course, there is a delay from the\n"
1263
"//                     first input to the first output.  For this purpose,\n"
1264
"//                     o_sync is present.\n"
1265 26 dgisselq
"//             o_sync  This will be a 1\'b1 for the first value in any block.\n"
1266
"//                     Following a reset, this will only become 1\'b1 once\n"
1267 2 dgisselq
"//                     the data has been loaded and is now valid.  After that,\n"
1268
"//                     all outputs will be valid.\n"
1269 26 dgisselq
"//\n"
1270
"//     20150602 -- This module has undergone massive rework in order to\n"
1271
"//             ensure that it uses resources efficiently.  As a result, \n"
1272
"//             it now optimizes nicely into block RAMs.  As an unfortunately\n"
1273
"//             side effect, it now passes it\'s bench test (dblrev_tb) but\n"
1274
"//             fails the integration bench test (fft_tb).\n"
1275
"//\n"
1276 2 dgisselq
"//\n%s"
1277
"//\n", prjname, creator);
1278
        fprintf(fp, "%s", cpyleft);
1279
        fprintf(fp,
1280
"\n\n"
1281
"//\n"
1282
"// How do we do bit reversing at two smples per clock?  Can we separate out\n"
1283
"// our work into eight memory banks, writing two banks at once and reading\n"
1284
"// another two banks in the same clock?\n"
1285
"//\n"
1286
"//     mem[00xxx0] = s_0[n]\n"
1287
"//     mem[00xxx1] = s_1[n]\n"
1288
"//     o_0[n] = mem[10xxx0]\n"
1289
"//     o_1[n] = mem[11xxx0]\n"
1290
"//     ...\n"
1291
"//     mem[01xxx0] = s_0[m]\n"
1292
"//     mem[01xxx1] = s_1[m]\n"
1293
"//     o_0[m] = mem[10xxx1]\n"
1294
"//     o_1[m] = mem[11xxx1]\n"
1295
"//     ...\n"
1296
"//     mem[10xxx0] = s_0[n]\n"
1297
"//     mem[10xxx1] = s_1[n]\n"
1298
"//     o_0[n] = mem[00xxx0]\n"
1299
"//     o_1[n] = mem[01xxx0]\n"
1300
"//     ...\n"
1301
"//     mem[11xxx0] = s_0[m]\n"
1302
"//     mem[11xxx1] = s_1[m]\n"
1303
"//     o_0[m] = mem[00xxx1]\n"
1304
"//     o_1[m] = mem[01xxx1]\n"
1305
"//     ...\n"
1306
"//\n"
1307 5 dgisselq
"//     The answer is that, yes we can but: we need to use four memory banks\n"
1308
"//     to do it properly.  These four banks are defined by the two bits\n"
1309
"//     that determine the top and bottom of the correct address.  Larger\n"
1310
"//     FFT\'s would require more memories.\n"
1311
"//\n"
1312 2 dgisselq
"//\n");
1313 33 dgisselq
        fprintf(fp,
1314 2 dgisselq
"module dblreverse(i_clk, i_rst, i_ce, i_in_0, i_in_1,\n"
1315 5 dgisselq
        "\t\to_out_0, o_out_1, o_sync);\n"
1316 29 dgisselq
        "\tparameter\t\t\tLGSIZE=%d, WIDTH=24;\n"
1317 5 dgisselq
        "\tinput\t\t\t\ti_clk, i_rst, i_ce;\n"
1318
        "\tinput\t\t[(2*WIDTH-1):0]\ti_in_0, i_in_1;\n"
1319 26 dgisselq
        "\toutput\twire\t[(2*WIDTH-1):0]\to_out_0, o_out_1;\n"
1320 29 dgisselq
        "\toutput\treg\t\t\to_sync;\n", TST_DBLREVERSE_LGSIZE);
1321
 
1322 33 dgisselq
        fprintf(fp,
1323 2 dgisselq
"\n"
1324 26 dgisselq
        "\treg\t\t\tin_reset;\n"
1325
        "\treg\t[(LGSIZE-1):0]\tiaddr;\n"
1326
        "\twire\t[(LGSIZE-3):0]\tbraddr;\n"
1327 2 dgisselq
"\n"
1328 5 dgisselq
        "\tgenvar\tk;\n"
1329 26 dgisselq
        "\tgenerate for(k=0; k<LGSIZE-2; k=k+1)\n"
1330 25 dgisselq
        "\tbegin : gen_a_bit_reversed_value\n"
1331 26 dgisselq
                "\t\tassign braddr[k] = iaddr[LGSIZE-3-k];\n"
1332 25 dgisselq
        "\tend endgenerate\n"
1333 2 dgisselq
"\n"
1334 25 dgisselq
        "\tinitial iaddr = 0;\n"
1335
        "\tinitial in_reset = 1\'b1;\n"
1336 26 dgisselq
        "\tinitial o_sync = 1\'b0;\n"
1337 5 dgisselq
        "\talways @(posedge i_clk)\n"
1338
                "\t\tif (i_rst)\n"
1339
                "\t\tbegin\n"
1340
                        "\t\t\tiaddr <= 0;\n"
1341 26 dgisselq
                        "\t\t\tin_reset <= 1\'b1;\n"
1342
                        "\t\t\to_sync <= 1\'b0;\n"
1343 5 dgisselq
                "\t\tend else if (i_ce)\n"
1344
                "\t\tbegin\n"
1345 26 dgisselq
                        "\t\t\tiaddr <= iaddr + { {(LGSIZE-1){1\'b0}}, 1\'b1 };\n"
1346
                        "\t\t\tif (&iaddr[(LGSIZE-2):0])\n"
1347
                                "\t\t\t\tin_reset <= 1\'b0;\n"
1348 5 dgisselq
                        "\t\t\tif (in_reset)\n"
1349 26 dgisselq
                                "\t\t\t\to_sync <= 1\'b0;\n"
1350
                        "\t\t\telse\n"
1351
                                "\t\t\t\to_sync <= ~(|iaddr[(LGSIZE-2):0]);\n"
1352 5 dgisselq
                "\t\tend\n"
1353 2 dgisselq
"\n"
1354 26 dgisselq
        "\treg\t[(2*WIDTH-1):0]\tmem_e [0:((1<<(LGSIZE))-1)];\n"
1355
        "\treg\t[(2*WIDTH-1):0]\tmem_o [0:((1<<(LGSIZE))-1)];\n"
1356
"\n"
1357
        "\talways @(posedge i_clk)\n"
1358
                "\t\tif (i_ce)\tmem_e[iaddr] <= i_in_0;\n"
1359
        "\talways @(posedge i_clk)\n"
1360
                "\t\tif (i_ce)\tmem_o[iaddr] <= i_in_1;\n"
1361
"\n"
1362
"\n"
1363
        "\treg [(2*WIDTH-1):0] evn_out_0, evn_out_1, odd_out_0, odd_out_1;\n"
1364
"\n"
1365
        "\talways @(posedge i_clk)\n"
1366
                "\t\tif (i_ce)\n\t\t\tevn_out_0 <= mem_e[{~iaddr[LGSIZE-1],1\'b0,braddr}];\n"
1367
        "\talways @(posedge i_clk)\n"
1368
                "\t\tif (i_ce)\n\t\t\tevn_out_1 <= mem_e[{~iaddr[LGSIZE-1],1\'b1,braddr}];\n"
1369
        "\talways @(posedge i_clk)\n"
1370
                "\t\tif (i_ce)\n\t\t\todd_out_0 <= mem_o[{~iaddr[LGSIZE-1],1\'b0,braddr}];\n"
1371
        "\talways @(posedge i_clk)\n"
1372
                "\t\tif (i_ce)\n\t\t\todd_out_1 <= mem_o[{~iaddr[LGSIZE-1],1\'b1,braddr}];\n"
1373
"\n"
1374
        "\treg\tadrz;\n"
1375
        "\talways @(posedge i_clk)\n"
1376 28 dgisselq
                "\t\tif (i_ce) adrz <= iaddr[LGSIZE-2];\n"
1377 26 dgisselq
"\n"
1378
        "\tassign\to_out_0 = (adrz)?odd_out_0:evn_out_0;\n"
1379
        "\tassign\to_out_1 = (adrz)?odd_out_1:evn_out_1;\n"
1380
"\n"
1381 21 dgisselq
"endmodule\n");
1382 2 dgisselq
 
1383
        fclose(fp);
1384
}
1385
 
1386 23 dgisselq
void    build_butterfly(const char *fname, int xtracbits, ROUND_T rounding) {
1387 2 dgisselq
        FILE    *fp = fopen(fname, "w");
1388
        if (NULL == fp) {
1389
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
1390
                perror("O/S Err was:");
1391
                return;
1392
        }
1393 23 dgisselq
        const   char    *rnd_string;
1394
        if (rounding == RND_TRUNCATE)
1395
                rnd_string = "truncate";
1396
        else if (rounding == RND_FROMZERO)
1397
                rnd_string = "roundfromzero";
1398
        else if (rounding == RND_HALFUP)
1399
                rnd_string = "roundhalfup";
1400
        else
1401
                rnd_string = "convround";
1402 2 dgisselq
 
1403
        fprintf(fp,
1404
"///////////////////////////////////////////////////////////////////////////\n"
1405
"//\n"
1406
"// Filename:   butterfly.v\n"
1407
"//\n"
1408
"// Project:    %s\n"
1409
"//\n"
1410
"// Purpose:    This routine caculates a butterfly for a decimation\n"
1411
"//             in frequency version of an FFT.  Specifically, given\n"
1412
"//             complex Left and Right values together with a \n"
1413
"//             coefficient, the output of this routine is given\n"
1414
"//             by:\n"
1415
"//\n"
1416
"//             L' = L + R\n"
1417
"//             R' = (L - R)*C\n"
1418
"//\n"
1419
"//             The rest of the junk below handles timing (mostly),\n"
1420
"//             to make certain that L' and R' reach the output at\n"
1421
"//             the same clock.  Further, just to make certain\n"
1422
"//             that is the case, an 'aux' input exists.  This\n"
1423
"//             aux value will come out of this routine synchronized\n"
1424
"//             to the values it came in with.  (i.e., both L', R',\n"
1425
"//             and aux all have the same delay.)  Hence, a caller\n"
1426
"//             of this routine may set aux on the first input with\n"
1427
"//             valid data, and then wait to see aux set on the output\n"
1428
"//             to know when to find the first output with valid data.\n"
1429
"//\n"
1430
"//             All bits are preserved until the very last clock,\n"
1431
"//             where any more bits than OWIDTH will be quietly\n"
1432
"//             discarded.\n"
1433
"//\n"
1434
"//             This design features no overflow checking.\n"
1435
"// \n"
1436
"// Notes:\n"
1437
"//             CORDIC:\n"
1438
"//             Much as we would like, we can't use a cordic here.\n"
1439
"//             The goal is to accomplish an FFT, as defined, and a\n"
1440
"//             CORDIC places a scale factor onto the data.  Removing\n"
1441
"//             the scale factor would cost a two multiplies, which\n"
1442
"//             is precisely what we are trying to avoid.\n"
1443
"//\n"
1444
"//\n"
1445
"//             3-MULTIPLIES:\n"
1446
"//             It should also be possible to do this with three \n"
1447
"//             multiplies and an extra two addition cycles.  \n"
1448
"//\n"
1449
"//             We want\n"
1450
"//                     R+I = (a + jb) * (c + jd)\n"
1451
"//                     R+I = (ac-bd) + j(ad+bc)\n"
1452
"//             We multiply\n"
1453
"//                     P1 = ac\n"
1454
"//                     P2 = bd\n"
1455
"//                     P3 = (a+b)(c+d)\n"
1456
"//             Then \n"
1457
"//                     R+I=(P1-P2)+j(P3-P2-P1)\n"
1458
"//\n"
1459
"//             WIDTHS:\n"
1460
"//             On multiplying an X width number by an\n"
1461
"//             Y width number, X>Y, the result should be (X+Y)\n"
1462
"//             bits, right?\n"
1463
"//             -2^(X-1) <= a <= 2^(X-1) - 1\n"
1464
"//             -2^(Y-1) <= b <= 2^(Y-1) - 1\n"
1465
"//             (2^(Y-1)-1)*(-2^(X-1)) <= ab <= 2^(X-1)2^(Y-1)\n"
1466
"//             -2^(X+Y-2)+2^(X-1) <= ab <= 2^(X+Y-2) <= 2^(X+Y-1) - 1\n"
1467
"//             -2^(X+Y-1) <= ab <= 2^(X+Y-1)-1\n"
1468
"//             YUP!  But just barely.  Do this and you'll really want\n"
1469
"//             to drop a bit, although you will risk overflow in so\n"
1470
"//             doing.\n"
1471 26 dgisselq
"//\n"
1472
"//     20150602 -- The sync logic lines have been completely redone.  The\n"
1473
"//             synchronization lines no longer go through the FIFO with the\n"
1474
"//             left hand sum, but are kept out of memory.  This allows the\n"
1475
"//             butterfly to use more optimal memory resources, while also\n"
1476
"//             guaranteeing that the sync lines can be properly reset upon\n"
1477
"//             any reset signal.\n"
1478
"//\n"
1479 2 dgisselq
"//\n%s"
1480
"//\n", prjname, creator);
1481
        fprintf(fp, "%s", cpyleft);
1482
 
1483
        fprintf(fp,
1484 6 dgisselq
"module\tbutterfly(i_clk, i_rst, i_ce, i_coef, i_left, i_right, i_aux,\n"
1485 5 dgisselq
                "\t\to_left, o_right, o_aux);\n"
1486
        "\t// Public changeable parameters ...\n"
1487 29 dgisselq
        "\tparameter IWIDTH=%d,", TST_BUTTERFLY_IWIDTH);
1488
#ifdef  TST_BUTTERFLY_CWIDTH
1489
        fprintf(fp, "CWIDTH=%d,", TST_BUTTERFLY_CWIDTH);
1490
#else
1491
        fprintf(fp, "CWIDTH=IWIDTH+%d,", xtracbits);
1492
#endif
1493
#ifdef  TST_BUTTERFLY_OWIDTH
1494
        fprintf(fp, "OWIDTH=%d;\n", TST_BUTTERFLY_OWIDTH);
1495
#else
1496
        fprintf(fp, "OWIDTH=IWIDTH+1;\n");
1497
#endif
1498
        fprintf(fp,
1499 5 dgisselq
        "\t// Parameters specific to the core that should not be changed.\n"
1500 29 dgisselq
        "\tparameter    MPYDELAY=%d'd%d,\n"
1501 28 dgisselq
                        "\t\t\tSHIFT=0, AUXLEN=(MPYDELAY+3);\n"
1502 5 dgisselq
        "\t// The LGDELAY should be the base two log of the MPYDELAY.  If\n"
1503
        "\t// this value is fractional, then round up to the nearest\n"
1504
        "\t// integer: LGDELAY=ceil(log(MPYDELAY)/log(2));\n"
1505 14 dgisselq
        "\tparameter\tLGDELAY=%d;\n"
1506 6 dgisselq
        "\tinput\t\ti_clk, i_rst, i_ce;\n"
1507 5 dgisselq
        "\tinput\t\t[(2*CWIDTH-1):0] i_coef;\n"
1508
        "\tinput\t\t[(2*IWIDTH-1):0] i_left, i_right;\n"
1509
        "\tinput\t\ti_aux;\n"
1510
        "\toutput\twire [(2*OWIDTH-1):0] o_left, o_right;\n"
1511 26 dgisselq
        "\toutput\treg\to_aux;\n"
1512 29 dgisselq
        "\n", lgdelay(16,xtracbits), bflydelay(16, xtracbits),
1513
                lgdelay(16,xtracbits));
1514 14 dgisselq
        fprintf(fp,
1515 5 dgisselq
        "\twire\t[(OWIDTH-1):0] o_left_r, o_left_i, o_right_r, o_right_i;\n"
1516 2 dgisselq
"\n"
1517 5 dgisselq
        "\treg\t[(2*IWIDTH-1):0]\tr_left, r_right;\n"
1518
        "\treg\t\t\t\tr_aux, r_aux_2;\n"
1519
        "\treg\t[(2*CWIDTH-1):0]\tr_coef, r_coef_2;\n"
1520
        "\twire\tsigned\t[(IWIDTH-1):0]\tr_left_r, r_left_i, r_right_r, r_right_i;\n"
1521
        "\tassign\tr_left_r  = r_left[ (2*IWIDTH-1):(IWIDTH)];\n"
1522
        "\tassign\tr_left_i  = r_left[ (IWIDTH-1):0];\n"
1523
        "\tassign\tr_right_r = r_right[(2*IWIDTH-1):(IWIDTH)];\n"
1524
        "\tassign\tr_right_i = r_right[(IWIDTH-1):0];\n"
1525 2 dgisselq
"\n"
1526 5 dgisselq
        "\treg\tsigned\t[(IWIDTH):0]\tr_sum_r, r_sum_i, r_dif_r, r_dif_i;\n"
1527 2 dgisselq
"\n"
1528 5 dgisselq
        "\treg  [(LGDELAY-1):0] fifo_addr;\n"
1529
        "\twire [(LGDELAY-1):0] fifo_read_addr;\n"
1530 6 dgisselq
        "\tassign\tfifo_read_addr = fifo_addr - MPYDELAY;\n"
1531 26 dgisselq
        "\treg  [(2*IWIDTH+1):0]        fifo_left [ 0:((1<<LGDELAY)-1)];\n"
1532 5 dgisselq
"\n");
1533
        fprintf(fp,
1534
        "\t// Set up the input to the multiply\n"
1535 2 dgisselq
        "\talways @(posedge i_clk)\n"
1536
                "\t\tif (i_ce)\n"
1537
                "\t\tbegin\n"
1538
                        "\t\t\t// One clock just latches the inputs\n"
1539
                        "\t\t\tr_left <= i_left;        // No change in # of bits\n"
1540
                        "\t\t\tr_right <= i_right;\n"
1541
                        "\t\t\tr_coef  <= i_coef;\n"
1542
                        "\t\t\t// Next clock adds/subtracts\n"
1543
                        "\t\t\tr_sum_r <= r_left_r + r_right_r; // Now IWIDTH+1 bits\n"
1544
                        "\t\t\tr_sum_i <= r_left_i + r_right_i;\n"
1545
                        "\t\t\tr_dif_r <= r_left_r - r_right_r;\n"
1546
                        "\t\t\tr_dif_i <= r_left_i - r_right_i;\n"
1547
                        "\t\t\t// Other inputs are simply delayed on second clock\n"
1548
                        "\t\t\tr_coef_2<= r_coef;\n"
1549
        "\t\tend\n"
1550 5 dgisselq
"\n");
1551
        fprintf(fp,
1552
        "\t// Don\'t forget to record the even side, since it doesn\'t need\n"
1553
        "\t// to be multiplied, but yet we still need the results in sync\n"
1554
        "\t// with the answer when it is ready.\n"
1555 25 dgisselq
        "\tinitial fifo_addr = 0;\n"
1556 2 dgisselq
        "\talways @(posedge i_clk)\n"
1557 6 dgisselq
                "\t\tif (i_rst)\n"
1558
                        "\t\t\tfifo_addr <= 0;\n"
1559 26 dgisselq
                "\t\telse if (i_ce)\n"
1560 2 dgisselq
                        "\t\t\t// Need to delay the sum side--nothing else happens\n"
1561
                        "\t\t\t// to it, but it needs to stay synchronized with the\n"
1562
                        "\t\t\t// right side.\n"
1563
                        "\t\t\tfifo_addr <= fifo_addr + 1;\n"
1564 14 dgisselq
"\n"
1565 26 dgisselq
        "\talways @(posedge i_clk)\n"
1566
                "\t\tif (i_ce)\n"
1567
                        "\t\t\tfifo_left[fifo_addr] <= { r_sum_r, r_sum_i };\n"
1568 2 dgisselq
"\n"
1569 5 dgisselq
        "\twire\tsigned\t[(CWIDTH-1):0] ir_coef_r, ir_coef_i;\n"
1570
        "\tassign\tir_coef_r = r_coef_2[(2*CWIDTH-1):CWIDTH];\n"
1571
        "\tassign\tir_coef_i = r_coef_2[(CWIDTH-1):0];\n"
1572
        "\twire\tsigned\t[((IWIDTH+2)+(CWIDTH+1)-1):0]\tp_one, p_two, p_three;\n"
1573 2 dgisselq
"\n"
1574 5 dgisselq
"\n");
1575
        fprintf(fp,
1576
        "\t// Multiply output is always a width of the sum of the widths of\n"
1577
        "\t// the two inputs.  ALWAYS.  This is independent of the number of\n"
1578
        "\t// bits in p_one, p_two, or p_three.  These values needed to \n"
1579
        "\t// accumulate a bit (or two) each.  However, this approach to a\n"
1580
        "\t// three multiply complex multiply cannot increase the total\n"
1581
        "\t// number of bits in our final output.  We\'ll take care of\n"
1582
        "\t// dropping back down to the proper width, OWIDTH, in our routine\n"
1583
        "\t// below.\n"
1584 2 dgisselq
"\n"
1585 5 dgisselq
"\n");
1586
        fprintf(fp,
1587
        "\t// We accomplish here \"Karatsuba\" multiplication.  That is,\n"
1588
        "\t// by doing three multiplies we accomplish the work of four.\n"
1589
        "\t// Let\'s prove to ourselves that this works ... We wish to\n"
1590
        "\t// multiply: (a+jb) * (c+jd), where a+jb is given by\n"
1591
        "\t//\ta + jb = r_dif_r + j r_dif_i, and\n"
1592
        "\t//\tc + jd = ir_coef_r + j ir_coef_i.\n"
1593
        "\t// We do this by calculating the intermediate products P1, P2,\n"
1594
        "\t// and P3 as\n"
1595
        "\t//\tP1 = ac\n"
1596
        "\t//\tP2 = bd\n"
1597
        "\t//\tP3 = (a + b) * (c + d)\n"
1598
        "\t// and then complete our final answer with\n"
1599
        "\t//\tac - bd = P1 - P2 (this checks)\n"
1600
        "\t//\tad + bc = P3 - P2 - P1\n"
1601
        "\t//\t        = (ac + bc + ad + bd) - bd - ac\n"
1602
        "\t//\t        = bc + ad (this checks)\n"
1603 2 dgisselq
"\n"
1604 5 dgisselq
"\n");
1605
        fprintf(fp,
1606
        "\t// This should really be based upon an IF, such as in\n"
1607
        "\t// if (IWIDTH < CWIDTH) then ...\n"
1608
        "\t// However, this is the only (other) way I know to do it.\n"
1609 29 dgisselq
        "\tgenerate if (CWIDTH < IWIDTH+1)\n"
1610 2 dgisselq
        "\tbegin\n"
1611 22 dgisselq
                "\t\twire\t[(CWIDTH):0]\tp3c_in;\n"
1612
                "\t\twire\t[(IWIDTH+1):0]\tp3d_in;\n"
1613
                "\t\tassign\tp3c_in = ir_coef_i + ir_coef_r;\n"
1614
                "\t\tassign\tp3d_in = r_dif_r + r_dif_i;\n"
1615
                "\n"
1616 2 dgisselq
                "\t\t// We need to pad these first two multiplies by an extra\n"
1617 5 dgisselq
                "\t\t// bit just to keep them aligned with the third,\n"
1618
                "\t\t// simpler, multiply.\n"
1619 29 dgisselq
                "\t\t%s #(CWIDTH+1,IWIDTH+2) p1(i_clk, i_ce,\n"
1620 2 dgisselq
                                "\t\t\t\t{ir_coef_r[CWIDTH-1],ir_coef_r},\n"
1621
                                "\t\t\t\t{r_dif_r[IWIDTH],r_dif_r}, p_one);\n"
1622 29 dgisselq
                "\t\t%s #(CWIDTH+1,IWIDTH+2) p2(i_clk, i_ce,\n"
1623 5 dgisselq
                                "\t\t\t\t{ir_coef_i[CWIDTH-1],ir_coef_i},\n"
1624 2 dgisselq
                                "\t\t\t\t{r_dif_i[IWIDTH],r_dif_i}, p_two);\n"
1625 29 dgisselq
                "\t\t%s #(CWIDTH+1,IWIDTH+2) p3(i_clk, i_ce,\n"
1626 22 dgisselq
                        "\t\t\t\tp3c_in, p3d_in, p_three);\n"
1627 2 dgisselq
        "\tend else begin\n"
1628 22 dgisselq
                "\t\twire\t[(CWIDTH):0]\tp3c_in;\n"
1629
                "\t\twire\t[(IWIDTH+1):0]\tp3d_in;\n"
1630
                "\t\tassign\tp3c_in = ir_coef_i + ir_coef_r;\n"
1631
                "\t\tassign\tp3d_in = r_dif_r + r_dif_i;\n"
1632
                "\n"
1633 29 dgisselq
                "\t\t%s #(IWIDTH+2,CWIDTH+1) p1a(i_clk, i_ce,\n"
1634 2 dgisselq
                                "\t\t\t\t{r_dif_r[IWIDTH],r_dif_r},\n"
1635
                                "\t\t\t\t{ir_coef_r[CWIDTH-1],ir_coef_r}, p_one);\n"
1636 29 dgisselq
                "\t\t%s #(IWIDTH+2,CWIDTH+1) p2a(i_clk, i_ce,\n"
1637 2 dgisselq
                                "\t\t\t\t{r_dif_i[IWIDTH], r_dif_i},\n"
1638 5 dgisselq
                                "\t\t\t\t{ir_coef_i[CWIDTH-1],ir_coef_i}, p_two);\n"
1639 29 dgisselq
                "\t\t%s #(IWIDTH+2,CWIDTH+1) p3a(i_clk, i_ce,\n"
1640 22 dgisselq
                                "\t\t\t\tp3d_in, p3c_in, p_three);\n"
1641 2 dgisselq
        "\tend\n"
1642
        "\tendgenerate\n"
1643 29 dgisselq
"\n",
1644
                (USE_OLD_MULTIPLY)?"shiftaddmpy":"longbimpy",
1645
                (USE_OLD_MULTIPLY)?"shiftaddmpy":"longbimpy",
1646
                (USE_OLD_MULTIPLY)?"shiftaddmpy":"longbimpy",
1647
                (USE_OLD_MULTIPLY)?"shiftaddmpy":"longbimpy",
1648
                (USE_OLD_MULTIPLY)?"shiftaddmpy":"longbimpy",
1649
                (USE_OLD_MULTIPLY)?"shiftaddmpy":"longbimpy");
1650 5 dgisselq
        fprintf(fp,
1651
        "\t// These values are held in memory and delayed during the\n"
1652
        "\t// multiply.  Here, we recover them.  During the multiply,\n"
1653
        "\t// values were multiplied by 2^(CWIDTH-2)*exp{-j*2*pi*...},\n"
1654
        "\t// therefore, the left_x values need to be right shifted by\n"
1655
        "\t// CWIDTH-2 as well.  The additional bits come from a sign\n"
1656
        "\t// extension.\n"
1657
        "\twire\tsigned\t[(IWIDTH+CWIDTH):0]    fifo_i, fifo_r;\n"
1658 26 dgisselq
        "\treg\t\t[(2*IWIDTH+1):0]      fifo_read;\n"
1659
        "\tassign\tfifo_r = { {2{fifo_read[2*(IWIDTH+1)-1]}}, fifo_read[(2*(IWIDTH+1)-1):(IWIDTH+1)], {(CWIDTH-2){1\'b0}} };\n"
1660
        "\tassign\tfifo_i = { {2{fifo_read[(IWIDTH+1)-1]}}, fifo_read[((IWIDTH+1)-1):0], {(CWIDTH-2){1\'b0}} };\n"
1661 2 dgisselq
"\n"
1662
"\n"
1663 23 dgisselq
        "\treg\tsigned\t[(OWIDTH-1):0]  b_left_r, b_left_i,\n"
1664 5 dgisselq
                        "\t\t\t\t\t\tb_right_r, b_right_i;\n"
1665
        "\treg\tsigned\t[(CWIDTH+IWIDTH+3-1):0] mpy_r, mpy_i;\n"
1666
"\n");
1667
        fprintf(fp,
1668 23 dgisselq
        "\t// Let's do some rounding and remove unnecessary bits.\n"
1669 5 dgisselq
        "\t// We have (IWIDTH+CWIDTH+3) bits here, we need to drop down to\n"
1670
        "\t// OWIDTH, and SHIFT by SHIFT bits in the process.  The trick is\n"
1671
        "\t// that we don\'t need (IWIDTH+CWIDTH+3) bits.  We\'ve accumulated\n"
1672
        "\t// them, but the actual values will never fill all these bits.\n"
1673
        "\t// In particular, we only need:\n"
1674
        "\t//\t IWIDTH bits for the input\n"
1675
        "\t//\t     +1 bit for the add/subtract\n"
1676
        "\t//\t+CWIDTH bits for the coefficient multiply\n"
1677
        "\t//\t     +1 bit for the add/subtract in the complex multiply\n"
1678
        "\t//\t ------\n"
1679
        "\t//\t (IWIDTH+CWIDTH+2) bits at full precision.\n"
1680
        "\t//\n"
1681
        "\t// However, the coefficient multiply multiplied by a maximum value\n"
1682
        "\t// of 2^(CWIDTH-2).  Thus, we only have\n"
1683
        "\t//\t   IWIDTH bits for the input\n"
1684
        "\t//\t       +1 bit for the add/subtract\n"
1685
        "\t//\t+CWIDTH-2 bits for the coefficient multiply\n"
1686
        "\t//\t       +1 (optional) bit for the add/subtract in the cpx mpy.\n"
1687
        "\t//\t -------- ... multiply.  (This last bit may be shifted out.)\n"
1688
        "\t//\t (IWIDTH+CWIDTH) valid output bits. \n"
1689
        "\t// Now, if the user wants to keep any extras of these (via OWIDTH),\n"
1690
        "\t// or if he wishes to arbitrarily shift some of these off (via\n"
1691
        "\t// SHIFT) we accomplish that here.\n"
1692 23 dgisselq
"\n");
1693
        fprintf(fp,
1694
        "\twire\tsigned\t[(OWIDTH-1):0]\trnd_left_r, rnd_left_i, rnd_right_r, rnd_right_i;\n\n");
1695
 
1696
        fprintf(fp,
1697 26 dgisselq
        "\t%s #(CWIDTH+IWIDTH+3,OWIDTH,SHIFT+4) do_rnd_left_r(i_clk, i_ce,\n"
1698 23 dgisselq
        "\t\t\t\t{ {2{fifo_r[(IWIDTH+CWIDTH)]}}, fifo_r }, rnd_left_r);\n\n",
1699
                rnd_string);
1700
        fprintf(fp,
1701 26 dgisselq
        "\t%s #(CWIDTH+IWIDTH+3,OWIDTH,SHIFT+4) do_rnd_left_i(i_clk, i_ce,\n"
1702 23 dgisselq
        "\t\t\t\t{ {2{fifo_i[(IWIDTH+CWIDTH)]}}, fifo_i }, rnd_left_i);\n\n",
1703
                rnd_string);
1704
        fprintf(fp,
1705 26 dgisselq
        "\t%s #(CWIDTH+IWIDTH+3,OWIDTH,SHIFT+4) do_rnd_right_r(i_clk, i_ce,\n"
1706 23 dgisselq
        "\t\t\t\tmpy_r, rnd_right_r);\n\n", rnd_string);
1707
        fprintf(fp,
1708 26 dgisselq
        "\t%s #(CWIDTH+IWIDTH+3,OWIDTH,SHIFT+4) do_rnd_right_i(i_clk, i_ce,\n"
1709 23 dgisselq
        "\t\t\t\tmpy_i, rnd_right_i);\n\n", rnd_string);
1710
        fprintf(fp,
1711
        "\talways @(posedge i_clk)\n"
1712
                "\t\tif (i_ce)\n"
1713
                "\t\tbegin\n"
1714
                        "\t\t\t// First clock, recover all values\n"
1715
                        "\t\t\tfifo_read <= fifo_left[fifo_read_addr];\n"
1716
                        "\t\t\t// These values are IWIDTH+CWIDTH+3 bits wide\n"
1717
                        "\t\t\t// although they only need to be (IWIDTH+1)\n"
1718
                        "\t\t\t// + (CWIDTH) bits wide.  (We\'ve got two\n"
1719
                        "\t\t\t// extra bits we need to get rid of.)\n"
1720
                        "\t\t\tmpy_r <= p_one - p_two;\n"
1721
                        "\t\t\tmpy_i <= p_three - p_one - p_two;\n"
1722 2 dgisselq
"\n"
1723 23 dgisselq
                        "\t\t\t// Second clock, round and latch for final clock\n"
1724
                        "\t\t\tb_right_r <= rnd_right_r;\n"
1725
                        "\t\t\tb_right_i <= rnd_right_i;\n"
1726
                        "\t\t\tb_left_r <= rnd_left_r;\n"
1727
                        "\t\t\tb_left_i <= rnd_left_i;\n"
1728 24 dgisselq
                "\t\tend\n"
1729
"\n");
1730 26 dgisselq
 
1731 24 dgisselq
        fprintf(fp,
1732 26 dgisselq
        "\treg\t[(AUXLEN-1):0]\taux_pipeline;\n"
1733
        "\tinitial\taux_pipeline = 0;\n"
1734
        "\talways @(posedge i_clk)\n"
1735
        "\t\tif (i_rst)\n"
1736
        "\t\t\taux_pipeline <= 0;\n"
1737
        "\t\telse if (i_ce)\n"
1738
        "\t\t\taux_pipeline <= { aux_pipeline[(AUXLEN-2):0], i_aux };\n"
1739
"\n");
1740
        fprintf(fp,
1741 25 dgisselq
        "\tinitial o_aux = 1\'b0;\n"
1742 24 dgisselq
        "\talways @(posedge i_clk)\n"
1743
                "\t\tif (i_rst)\n"
1744
                "\t\t\to_aux <= 1\'b0;\n"
1745
                "\t\telse if (i_ce)\n"
1746
                "\t\tbegin\n"
1747
                        "\t\t\t// Second clock, latch for final clock\n"
1748 26 dgisselq
                        "\t\t\to_aux <= aux_pipeline[AUXLEN-1];\n"
1749 23 dgisselq
                "\t\tend\n"
1750
"\n");
1751 24 dgisselq
 
1752 23 dgisselq
        fprintf(fp,
1753 5 dgisselq
        "\t// As a final step, we pack our outputs into two packed two\'s\n"
1754
        "\t// complement numbers per output word, so that each output word\n"
1755
        "\t// has (2*OWIDTH) bits in it, with the top half being the real\n"
1756
        "\t// portion and the bottom half being the imaginary portion.\n"
1757 23 dgisselq
        "\tassign       o_left = { rnd_left_r, rnd_left_i };\n"
1758
        "\tassign       o_right= { rnd_right_r,rnd_right_i};\n"
1759 2 dgisselq
"\n"
1760
"endmodule\n");
1761
        fclose(fp);
1762
}
1763
 
1764 23 dgisselq
void    build_hwbfly(const char *fname, int xtracbits, ROUND_T rounding) {
1765 22 dgisselq
        FILE    *fp = fopen(fname, "w");
1766
        if (NULL == fp) {
1767
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
1768
                perror("O/S Err was:");
1769
                return;
1770
        }
1771
 
1772 23 dgisselq
        const   char    *rnd_string;
1773
        if (rounding == RND_TRUNCATE)
1774
                rnd_string = "truncate";
1775
        else if (rounding == RND_FROMZERO)
1776
                rnd_string = "roundfromzero";
1777
        else if (rounding == RND_HALFUP)
1778
                rnd_string = "roundhalfup";
1779
        else
1780
                rnd_string = "convround";
1781
 
1782
 
1783 22 dgisselq
        fprintf(fp,
1784
"///////////////////////////////////////////////////////////////////////////\n"
1785
"//\n"
1786
"// Filename:   hwbfly.v\n"
1787
"//\n"
1788
"// Project:    %s\n"
1789
"//\n"
1790
"// Purpose:    This routine is identical to the butterfly.v routine found\n"
1791
"//             in 'butterfly.v', save only that it uses the verilog \n"
1792
"//             operator '*' in hopes that the synthesizer would be able\n"
1793
"//             to optimize it with hardware resources.\n"
1794
"//\n"
1795
"//             It is understood that a hardware multiply can complete its\n"
1796
"//             operation in a single clock.\n"
1797
"//\n"
1798
"//\n%s"
1799
"//\n", prjname, creator);
1800
        fprintf(fp, "%s", cpyleft);
1801
        fprintf(fp,
1802
"module hwbfly(i_clk, i_rst, i_ce, i_coef, i_left, i_right, i_aux,\n"
1803
                "\t\to_left, o_right, o_aux);\n"
1804
        "\t// Public changeable parameters ...\n"
1805
        "\tparameter IWIDTH=16,CWIDTH=IWIDTH+%d,OWIDTH=IWIDTH+1;\n"
1806
        "\t// Parameters specific to the core that should not be changed.\n"
1807 23 dgisselq
        "\tparameter\tSHIFT=0;\n"
1808 22 dgisselq
        "\tinput\t\ti_clk, i_rst, i_ce;\n"
1809
        "\tinput\t\t[(2*CWIDTH-1):0]\ti_coef;\n"
1810
        "\tinput\t\t[(2*IWIDTH-1):0]\ti_left, i_right;\n"
1811
        "\tinput\t\ti_aux;\n"
1812
        "\toutput\twire\t[(2*OWIDTH-1):0]\to_left, o_right;\n"
1813
        "\toutput\treg\to_aux;\n"
1814
"\n", xtracbits);
1815
        fprintf(fp,
1816
        "\twire\t[(OWIDTH-1):0] o_left_r, o_left_i, o_right_r, o_right_i;\n"
1817
"\n"
1818
        "\treg\t[(2*IWIDTH-1):0]        r_left, r_right;\n"
1819
        "\treg\t                        r_aux, r_aux_2;\n"
1820
        "\treg\t[(2*CWIDTH-1):0]        r_coef, r_coef_2;\n"
1821
        "\twire signed  [(IWIDTH-1):0]  r_left_r, r_left_i, r_right_r, r_right_i;\n"
1822
        "\tassign\tr_left_r  = r_left[ (2*IWIDTH-1):(IWIDTH)];\n"
1823
        "\tassign\tr_left_i  = r_left[ (IWIDTH-1):0];\n"
1824
        "\tassign\tr_right_r = r_right[(2*IWIDTH-1):(IWIDTH)];\n"
1825
        "\tassign\tr_right_i = r_right[(IWIDTH-1):0];\n"
1826 26 dgisselq
        "\treg  signed  [(CWIDTH-1):0]  ir_coef_r, ir_coef_i;\n"
1827 22 dgisselq
"\n"
1828
        "\treg  signed  [(IWIDTH):0]    r_sum_r, r_sum_i, r_dif_r, r_dif_i;\n"
1829
"\n"
1830
        "\treg  [(2*IWIDTH+2):0]        leftv, leftvv;\n"
1831
"\n"
1832
        "\t// Set up the input to the multiply\n"
1833 25 dgisselq
        "\tinitial r_aux   = 1\'b0;\n"
1834
        "\tinitial r_aux_2 = 1\'b0;\n"
1835 22 dgisselq
        "\talways @(posedge i_clk)\n"
1836 25 dgisselq
                "\t\tif (i_rst)\n"
1837
                "\t\tbegin\n"
1838 26 dgisselq
                        "\t\t\tr_aux <= 1\'b0;\n"
1839
                        "\t\t\tr_aux_2 <= 1\'b0;\n"
1840 25 dgisselq
                "\t\tend else if (i_ce)\n"
1841
                "\t\tbegin\n"
1842
                        "\t\t\t// One clock just latches the inputs\n"
1843 26 dgisselq
                        "\t\t\tr_aux <= i_aux;\n"
1844
                        "\t\t\t// Next clock adds/subtracts\n"
1845
                        "\t\t\t// Other inputs are simply delayed on second clock\n"
1846
                        "\t\t\tr_aux_2 <= r_aux;\n"
1847
                "\t\tend\n"
1848
        "\talways @(posedge i_clk)\n"
1849
                "\t\tif (i_ce)\n"
1850
                "\t\tbegin\n"
1851
                        "\t\t\t// One clock just latches the inputs\n"
1852 25 dgisselq
                        "\t\t\tr_left <= i_left;        // No change in # of bits\n"
1853
                        "\t\t\tr_right <= i_right;\n"
1854
                        "\t\t\tr_coef  <= i_coef;\n"
1855
                        "\t\t\t// Next clock adds/subtracts\n"
1856
                        "\t\t\tr_sum_r <= r_left_r + r_right_r; // Now IWIDTH+1 bits\n"
1857
                        "\t\t\tr_sum_i <= r_left_i + r_right_i;\n"
1858
                        "\t\t\tr_dif_r <= r_left_r - r_right_r;\n"
1859
                        "\t\t\tr_dif_i <= r_left_i - r_right_i;\n"
1860
                        "\t\t\t// Other inputs are simply delayed on second clock\n"
1861 26 dgisselq
                        "\t\t\tir_coef_r <= r_coef[(2*CWIDTH-1):CWIDTH];\n"
1862
                        "\t\t\tir_coef_i <= r_coef[(CWIDTH-1):0];\n"
1863 25 dgisselq
                "\t\tend\n"
1864 22 dgisselq
        "\n\n");
1865
        fprintf(fp,
1866
"\t// See comments in the butterfly.v source file for a discussion of\n"
1867
"\t// these operations and the appropriate bit widths.\n\n");
1868 33 dgisselq
        fprintf(fp,
1869 26 dgisselq
        "\treg\tsigned  [((IWIDTH+1)+(CWIDTH)-1):0]     p_one, p_two;\n"
1870
        "\treg\tsigned  [((IWIDTH+2)+(CWIDTH+1)-1):0]   p_three;\n"
1871 22 dgisselq
"\n"
1872 26 dgisselq
        "\treg\tsigned  [(CWIDTH-1):0]  p1c_in, p2c_in; // Coefficient multiply inputs\n"
1873
        "\treg\tsigned  [(IWIDTH):0]    p1d_in, p2d_in; // Data multiply inputs\n"
1874
        "\treg\tsigned  [(CWIDTH):0]    p3c_in; // Product 3, coefficient input\n"
1875
        "\treg\tsigned  [(IWIDTH+1):0]  p3d_in; // Product 3, data input\n"
1876 22 dgisselq
"\n"
1877 25 dgisselq
        "\tinitial leftv    = 0;\n"
1878
        "\tinitial leftvv   = 0;\n"
1879 22 dgisselq
        "\talways @(posedge i_clk)\n"
1880
        "\tbegin\n"
1881
                "\t\tif (i_rst)\n"
1882
                "\t\tbegin\n"
1883
                        "\t\t\tleftv <= 0;\n"
1884
                        "\t\t\tleftvv <= 0;\n"
1885 26 dgisselq
                "\t\tend else if (i_ce)\n"
1886 22 dgisselq
                "\t\tbegin\n"
1887
                        "\t\t\t// Second clock, pipeline = 1\n"
1888 26 dgisselq
                        "\t\t\tleftv <= { r_aux_2, r_sum_r, r_sum_i };\n"
1889
"\n"
1890
                        "\t\t\t// Third clock, pipeline = 3\n"
1891
                        "\t\t\t//   As desired, each of these lines infers a DSP48\n"
1892
                        "\t\t\tleftvv <= leftv;\n"
1893
                "\t\tend\n"
1894
        "\tend\n"
1895
"\n"
1896
        "\talways @(posedge i_clk)\n"
1897
                "\t\tif (i_ce)\n"
1898
                "\t\tbegin\n"
1899
                        "\t\t\t// Second clock, pipeline = 1\n"
1900
                        "\t\t\tp1c_in <= ir_coef_r;\n"
1901
                        "\t\t\tp2c_in <= ir_coef_i;\n"
1902
                        "\t\t\tp1d_in <= r_dif_r;\n"
1903
                        "\t\t\tp2d_in <= r_dif_i;\n"
1904 22 dgisselq
                        "\t\t\tp3c_in <= ir_coef_i + ir_coef_r;\n"
1905
                        "\t\t\tp3d_in <= r_dif_r + r_dif_i;\n"
1906 23 dgisselq
"\n"
1907
"\n"
1908 22 dgisselq
                        "\t\t\t// Third clock, pipeline = 3\n"
1909 26 dgisselq
                        "\t\t\t//   As desired, each of these lines infers a DSP48\n"
1910 22 dgisselq
                        "\t\t\tp_one   <= p1c_in * p1d_in;\n"
1911
                        "\t\t\tp_two   <= p2c_in * p2d_in;\n"
1912
                        "\t\t\tp_three <= p3c_in * p3d_in;\n"
1913 26 dgisselq
                "\t\tend\n"
1914 22 dgisselq
"\n"
1915 26 dgisselq
        "\twire\tsigned [((IWIDTH+2)+(CWIDTH+1)-1):0]   w_one, w_two;\n"
1916
        "\tassign\tw_one = { {(2){p_one[((IWIDTH+1)+(CWIDTH)-1)]}}, p_one };\n"
1917
        "\tassign\tw_two = { {(2){p_two[((IWIDTH+1)+(CWIDTH)-1)]}}, p_two };\n"
1918 22 dgisselq
"\n");
1919
 
1920 33 dgisselq
        fprintf(fp,
1921 22 dgisselq
        "\t// These values are held in memory and delayed during the\n"
1922
        "\t// multiply.  Here, we recover them.  During the multiply,\n"
1923
        "\t// values were multiplied by 2^(CWIDTH-2)*exp{-j*2*pi*...},\n"
1924
        "\t// therefore, the left_x values need to be right shifted by\n"
1925
        "\t// CWIDTH-2 as well.  The additional bits come from a sign\n"
1926
        "\t// extension.\n"
1927 24 dgisselq
        "\twire\taux_s;\n"
1928 22 dgisselq
        "\twire\tsigned\t[(IWIDTH+CWIDTH):0]    left_si, left_sr;\n"
1929
        "\treg\t\t[(2*IWIDTH+2):0]      left_saved;\n"
1930 26 dgisselq
        "\tassign\tleft_sr = { {2{left_saved[2*(IWIDTH+1)-1]}}, left_saved[(2*(IWIDTH+1)-1):(IWIDTH+1)], {(CWIDTH-2){1\'b0}} };\n"
1931
        "\tassign\tleft_si = { {2{left_saved[(IWIDTH+1)-1]}}, left_saved[((IWIDTH+1)-1):0], {(CWIDTH-2){1\'b0}} };\n"
1932 22 dgisselq
        "\tassign\taux_s = left_saved[2*IWIDTH+2];\n"
1933
"\n"
1934
"\n"
1935 26 dgisselq
        "\t(* use_dsp48=\"no\" *)\n"
1936 23 dgisselq
        "\treg  signed  [(CWIDTH+IWIDTH+3-1):0] mpy_r, mpy_i;\n");
1937
        fprintf(fp,
1938
        "\twire\tsigned\t[(OWIDTH-1):0]\trnd_left_r, rnd_left_i, rnd_right_r, rnd_right_i;\n\n");
1939 22 dgisselq
 
1940
        fprintf(fp,
1941 26 dgisselq
        "\t%s #(CWIDTH+IWIDTH+1,OWIDTH,SHIFT+2) do_rnd_left_r(i_clk, i_ce,\n"
1942
        "\t\t\t\tleft_sr, rnd_left_r);\n\n",
1943 23 dgisselq
                rnd_string);
1944
        fprintf(fp,
1945 26 dgisselq
        "\t%s #(CWIDTH+IWIDTH+1,OWIDTH,SHIFT+2) do_rnd_left_i(i_clk, i_ce,\n"
1946
        "\t\t\t\tleft_si, rnd_left_i);\n\n",
1947 23 dgisselq
                rnd_string);
1948
        fprintf(fp,
1949 26 dgisselq
        "\t%s #(CWIDTH+IWIDTH+3,OWIDTH,SHIFT+4) do_rnd_right_r(i_clk, i_ce,\n"
1950 23 dgisselq
        "\t\t\t\tmpy_r, rnd_right_r);\n\n", rnd_string);
1951
        fprintf(fp,
1952 26 dgisselq
        "\t%s #(CWIDTH+IWIDTH+3,OWIDTH,SHIFT+4) do_rnd_right_i(i_clk, i_ce,\n"
1953 23 dgisselq
        "\t\t\t\tmpy_i, rnd_right_i);\n\n", rnd_string);
1954
 
1955
        fprintf(fp,
1956 25 dgisselq
        "\tinitial left_saved = 0;\n"
1957
        "\tinitial o_aux      = 1\'b0;\n"
1958 22 dgisselq
        "\talways @(posedge i_clk)\n"
1959
        "\t\tif (i_rst)\n"
1960
        "\t\tbegin\n"
1961
                "\t\t\tleft_saved <= 0;\n"
1962 26 dgisselq
                "\t\t\to_aux <= 1\'b0;\n"
1963 22 dgisselq
        "\t\tend else if (i_ce)\n"
1964
        "\t\tbegin\n"
1965
                "\t\t\t// First clock, recover all values\n"
1966
                "\t\t\tleft_saved <= leftvv;\n"
1967 26 dgisselq
"\n"
1968
                "\t\t\t// Second clock, round and latch for final clock\n"
1969
                "\t\t\to_aux <= aux_s;\n"
1970
        "\t\tend\n"
1971
        "\talways @(posedge i_clk)\n"
1972
        "\t\tif (i_ce)\n"
1973
        "\t\tbegin\n"
1974 22 dgisselq
                "\t\t\t// These values are IWIDTH+CWIDTH+3 bits wide\n"
1975
                "\t\t\t// although they only need to be (IWIDTH+1)\n"
1976
                "\t\t\t// + (CWIDTH) bits wide.  (We've got two\n"
1977
                "\t\t\t// extra bits we need to get rid of.)\n"
1978 26 dgisselq
                "\n"
1979
                "\t\t\t// These two lines also infer DSP48\'s.\n"
1980
                "\t\t\t// To keep from using extra DSP48 resources,\n"
1981
                "\t\t\t// they are prevented from using DSP48\'s\n"
1982
                "\t\t\t// by the (* use_dsp48 ... *) comment above.\n"
1983
                "\t\t\tmpy_r <= w_one - w_two;\n"
1984
                "\t\t\tmpy_i <= p_three - w_one - w_two;\n"
1985 22 dgisselq
        "\t\tend\n"
1986
        "\n");
1987
 
1988
        fprintf(fp,
1989
        "\t// As a final step, we pack our outputs into two packed two's\n"
1990
        "\t// complement numbers per output word, so that each output word\n"
1991
        "\t// has (2*OWIDTH) bits in it, with the top half being the real\n"
1992
        "\t// portion and the bottom half being the imaginary portion.\n"
1993 23 dgisselq
        "\tassign\to_left = { rnd_left_r, rnd_left_i };\n"
1994
        "\tassign\to_right= { rnd_right_r,rnd_right_i};\n"
1995 22 dgisselq
"\n"
1996
"endmodule\n");
1997
 
1998
}
1999
 
2000 26 dgisselq
void    build_stage(const char *fname, const char *coredir, int stage, bool odd, int nbits, bool inv, int xtra, bool hwmpy=false, bool dbg=false) {
2001 2 dgisselq
        FILE    *fstage = fopen(fname, "w");
2002
        int     cbits = nbits + xtra;
2003
 
2004
        if ((cbits * 2) >= sizeof(long long)*8) {
2005
                fprintf(stderr, "ERROR: CMEM Coefficient precision requested overflows long long data type.\n");
2006
                exit(-1);
2007
        }
2008
 
2009
        if (fstage == NULL) {
2010
                fprintf(stderr, "ERROR: Could not open %s for writing!\n", fname);
2011
                perror("O/S Err was:");
2012
                fprintf(stderr, "Attempting to continue, but this file will be missing.\n");
2013
                return;
2014
        }
2015
 
2016
        fprintf(fstage,
2017
"////////////////////////////////////////////////////////////////////////////\n"
2018
"//\n"
2019 26 dgisselq
"// Filename:   %sfftstage_%c%d%s.v\n"
2020 2 dgisselq
"//\n"
2021
"// Project:    %s\n"
2022
"//\n"
2023
"// Purpose:    This file is (almost) a Verilog source file.  It is meant to\n"
2024
"//             be used by a FFT core compiler to generate FFTs which may be\n"
2025
"//             used as part of an FFT core.  Specifically, this file \n"
2026
"//             encapsulates the options of an FFT-stage.  For any 2^N length\n"
2027
"//             FFT, there shall be (N-1) of these stages.  \n"
2028
"//\n%s"
2029
"//\n",
2030 26 dgisselq
                (inv)?"i":"", (odd)?'o':'e', stage*2, (dbg)?"_dbg":"", prjname, creator);
2031 2 dgisselq
        fprintf(fstage, "%s", cpyleft);
2032 26 dgisselq
        fprintf(fstage, "module\t%sfftstage_%c%d%s(i_clk, i_rst, i_ce, i_sync, i_data, o_data, o_sync%s);\n",
2033
                (inv)?"i":"", (odd)?'o':'e', stage*2, (dbg)?"_dbg":"",
2034
                (dbg)?", o_dbg":"");
2035 2 dgisselq
        // These parameter values are useless at this point--they are to be
2036
        // replaced by the parameter values in the calling program.  Only
2037
        // problem is, the CWIDTH needs to match exactly!
2038
        fprintf(fstage, "\tparameter\tIWIDTH=%d,CWIDTH=%d,OWIDTH=%d;\n",
2039
                nbits, cbits, nbits+1);
2040
        fprintf(fstage,
2041
"\t// Parameters specific to the core that should be changed when this\n"
2042
"\t// core is built ... Note that the minimum LGSPAN (the base two log\n"
2043
"\t// of the span, or the base two log of the current FFT size) is 3.\n"
2044
"\t// Smaller spans (i.e. the span of 2) must use the dblstage module.\n"
2045 6 dgisselq
"\tparameter\tLGWIDTH=11, LGSPAN=9, LGBDLY=5, BFLYSHIFT=0;\n");
2046 33 dgisselq
        fprintf(fstage,
2047 2 dgisselq
"\tinput                                        i_clk, i_rst, i_ce, i_sync;\n"
2048
"\tinput                [(2*IWIDTH-1):0]        i_data;\n"
2049
"\toutput       reg     [(2*OWIDTH-1):0]        o_data;\n"
2050
"\toutput       reg                             o_sync;\n"
2051 26 dgisselq
"\n");
2052
        if (dbg) { fprintf(fstage, "\toutput\twire\t[33:0]\t\t\to_dbg;\n"
2053
                "\tassign\to_dbg = { ((o_sync)&&(i_ce)), i_ce, o_data[(2*OWIDTH-1):(2*OWIDTH-16)],\n"
2054
                        "\t\t\t\t\to_data[(OWIDTH-1):(OWIDTH-16)] };\n"
2055
"\n");
2056
        }
2057 33 dgisselq
        fprintf(fstage,
2058 2 dgisselq
"\treg  wait_for_sync;\n"
2059
"\treg  [(2*IWIDTH-1):0]        ib_a, ib_b;\n"
2060
"\treg  [(2*CWIDTH-1):0]        ib_c;\n"
2061 8 dgisselq
"\treg  ib_sync;\n"
2062 2 dgisselq
"\n"
2063
"\treg  b_started;\n"
2064
"\twire ob_sync;\n"
2065 23 dgisselq
"\twire [(2*OWIDTH-1):0]\tob_a, ob_b;\n");
2066 33 dgisselq
        fprintf(fstage,
2067 2 dgisselq
"\n"
2068
"\t// %scmem is defined as an array of real and complex values,\n"
2069
"\t// where the top CWIDTH bits are the real value and the bottom\n"
2070
"\t// CWIDTH bits are the imaginary value.\n"
2071
"\t//\n"
2072 24 dgisselq
"\t// %scmem[i] = { (2^(CWIDTH-2)) * cos(2*pi*i/(2^LGWIDTH)),\n"
2073 2 dgisselq
"\t//           (2^(CWIDTH-2)) * sin(2*pi*i/(2^LGWIDTH)) };\n"
2074
"\t//\n"
2075
"\treg  [(2*CWIDTH-1):0]        %scmem [0:((1<<LGSPAN)-1)];\n"
2076
"\tinitial\t$readmemh(\"%scmem_%c%d.hex\",%scmem);\n\n",
2077 24 dgisselq
                (inv)?"i":"", (inv)?"i":"", (inv)?"i":"",
2078
                (inv)?"i":"", (odd)?'o':'e',stage<<1, (inv)?"i":"");
2079 2 dgisselq
        {
2080
                FILE    *cmem;
2081
 
2082 14 dgisselq
                {
2083
                        char    *memfile, *ptr;
2084
 
2085
                        memfile = new char[strlen(fname)+128];
2086
                        strcpy(memfile, fname);
2087
                        if ((NULL != (ptr = strrchr(memfile, '/')))&&(ptr>memfile)) {
2088
                                ptr++;
2089
                                sprintf(ptr, "%scmem_%c%d.hex", (inv)?"i":"", (odd)?'o':'e', stage*2);
2090
                        } else {
2091
                                sprintf(memfile, "%s/%scmem_%c%d.hex",
2092 26 dgisselq
                                        coredir, (inv)?"i":"",
2093 14 dgisselq
                                        (odd)?'o':'e', stage*2);
2094
                        }
2095
                        // strcpy(&memfile[strlen(memfile)-2], ".hex");
2096
                        cmem = fopen(memfile, "w");
2097
                        if (NULL == cmem) {
2098
                                fprintf(stderr, "Could not open/write \'%s\' with FFT coefficients.\n", memfile);
2099
                                perror("Err from O/S:");
2100
                                exit(-2);
2101
                        }
2102
 
2103
                        delete[] memfile;
2104 2 dgisselq
                }
2105
                // fprintf(cmem, "// CBITS = %d, inv = %s\n", cbits, (inv)?"true":"false");
2106
                for(int i=0; i<stage/2; i++) {
2107
                        int k = 2*i+odd;
2108 9 dgisselq
                        double  W = ((inv)?1:-1)*2.0*M_PI*k/(double)(2*stage);
2109 2 dgisselq
                        double  c, s;
2110
                        long long ic, is, vl;
2111
 
2112
                        c = cos(W); s = sin(W);
2113 31 dgisselq
                        ic = (long long)llround((1ll<<(cbits-2)) * c);
2114
                        is = (long long)llround((1ll<<(cbits-2)) * s);
2115 2 dgisselq
                        vl = (ic & (~(-1ll << (cbits))));
2116
                        vl <<= (cbits);
2117
                        vl |= (is & (~(-1ll << (cbits))));
2118
                        fprintf(cmem, "%0*llx\n", ((cbits*2+3)/4), vl);
2119
                        /*
2120
                        fprintf(cmem, "%0*llx\t\t// %f+j%f -> %llx +j%llx\n",
2121
                                ((cbits*2+3)/4), vl, c, s,
2122
                                ic & (~(-1ll<<(((cbits+3)/4)*4))),
2123
                                is & (~(-1ll<<(((cbits+3)/4)*4))));
2124
                        */
2125
                } fclose(cmem);
2126
        }
2127
 
2128
        fprintf(fstage,
2129 6 dgisselq
"\treg  [(LGWIDTH-2):0]         iaddr;\n"
2130 2 dgisselq
"\treg  [(2*IWIDTH-1):0]        imem    [0:((1<<LGSPAN)-1)];\n"
2131
"\n"
2132 8 dgisselq
"\treg  [LGSPAN:0]              oB;\n"
2133 2 dgisselq
"\treg  [(2*OWIDTH-1):0]        omem    [0:((1<<LGSPAN)-1)];\n"
2134
"\n"
2135 25 dgisselq
"\tinitial wait_for_sync = 1\'b1;\n"
2136
"\tinitial iaddr = 0;\n"
2137 2 dgisselq
"\talways @(posedge i_clk)\n"
2138
        "\t\tif (i_rst)\n"
2139
        "\t\tbegin\n"
2140 26 dgisselq
                "\t\t\twait_for_sync <= 1\'b1;\n"
2141 2 dgisselq
                "\t\t\tiaddr <= 0;\n"
2142
        "\t\tend\n"
2143
        "\t\telse if ((i_ce)&&((~wait_for_sync)||(i_sync)))\n"
2144
        "\t\tbegin\n"
2145
                "\t\t\t//\n"
2146
                "\t\t\t// First step: Record what we\'re not ready to use yet\n"
2147
                "\t\t\t//\n"
2148 25 dgisselq
                "\t\t\tiaddr <= iaddr + { {(LGWIDTH-2){1\'b0}}, 1\'b1 };\n"
2149 26 dgisselq
                "\t\t\twait_for_sync <= 1\'b0;\n"
2150
        "\t\tend\n"
2151
"\talways @(posedge i_clk) // Need to make certain here that we don\'t read\n"
2152
        "\t\tif ((i_ce)&&(~iaddr[LGSPAN])) // and write the same address on\n"
2153
                "\t\t\timem[iaddr[(LGSPAN-1):0]] <= i_data; // the same clk\n"
2154
        "\n");
2155 23 dgisselq
 
2156
        fprintf(fstage,
2157
        "\t//\n"
2158
        "\t// Now, we have all the inputs, so let\'s feed the butterfly\n"
2159
        "\t//\n"
2160 25 dgisselq
        "\tinitial ib_sync = 1\'b0;\n"
2161 23 dgisselq
        "\talways\t@(posedge i_clk)\n"
2162 26 dgisselq
                "\t\tif (i_rst)\n"
2163
                        "\t\t\tib_sync <= 1\'b0;\n"
2164
                "\t\telse if ((i_ce)&&(iaddr[LGSPAN]))\n"
2165
                        "\t\t\tbegin\n"
2166
                                "\t\t\t\t// Set the sync to true on the very first\n"
2167
                                "\t\t\t\t// valid input in, and hence on the very\n"
2168
                                "\t\t\t\t// first valid data out per FFT.\n"
2169
                                "\t\t\t\tib_sync <= (iaddr==(1<<(LGSPAN)));\n"
2170
                        "\t\t\tend\n"
2171 24 dgisselq
        "\talways\t@(posedge i_clk)\n"
2172 26 dgisselq
                "\t\tif ((i_ce)&&(iaddr[LGSPAN]))\n"
2173
                "\t\t\tbegin\n"
2174
                        "\t\t\t\t// One input from memory, ...\n"
2175
                        "\t\t\t\tib_a <= imem[iaddr[(LGSPAN-1):0]];\n"
2176
                        "\t\t\t\t// One input clocked in from the top\n"
2177
                        "\t\t\t\tib_b <= i_data;\n"
2178
                        "\t\t\t\t// and the coefficient or twiddle factor\n"
2179
                        "\t\t\t\tib_c <= %scmem[iaddr[(LGSPAN-1):0]];\n"
2180
                "\t\t\tend\n\n", (inv)?"i":"");
2181 23 dgisselq
 
2182
        if (hwmpy) {
2183
                fprintf(fstage,
2184
        "\thwbfly #(.IWIDTH(IWIDTH),.CWIDTH(CWIDTH),.OWIDTH(OWIDTH),\n"
2185
                        "\t\t\t.SHIFT(BFLYSHIFT))\n"
2186
                "\t\tbfly(i_clk, i_rst, i_ce, ib_c,\n"
2187
                        "\t\t\tib_a, ib_b, ib_sync, ob_a, ob_b, ob_sync);\n");
2188
        } else {
2189
        fprintf(fstage,
2190
        "\tbutterfly #(.IWIDTH(IWIDTH),.CWIDTH(CWIDTH),.OWIDTH(OWIDTH),\n"
2191
                "\t\t\t.MPYDELAY(%d\'d%d),.LGDELAY(LGBDLY),.SHIFT(BFLYSHIFT))\n"
2192
        "\t\tbfly(i_clk, i_rst, i_ce, ib_c,\n"
2193
                "\t\t\tib_a, ib_b, ib_sync, ob_a, ob_b, ob_sync);\n",
2194
                        lgdelay(nbits, xtra), bflydelay(nbits, xtra));
2195
        }
2196
 
2197
        fprintf(fstage,
2198
        "\t//\n"
2199
        "\t// Next step: recover the outputs from the butterfly\n"
2200
        "\t//\n"
2201 25 dgisselq
        "\tinitial oB        = 0;\n"
2202
        "\tinitial o_sync    = 0;\n"
2203
        "\tinitial b_started = 0;\n"
2204 23 dgisselq
        "\talways\t@(posedge i_clk)\n"
2205
        "\t\tif (i_rst)\n"
2206
        "\t\tbegin\n"
2207
                "\t\t\toB <= 0;\n"
2208
                "\t\t\to_sync <= 0;\n"
2209
                "\t\t\tb_started <= 0;\n"
2210
        "\t\tend else if (i_ce)\n"
2211
        "\t\tbegin\n"
2212 26 dgisselq
        "\t\t\to_sync <= (~oB[LGSPAN])?ob_sync : 1\'b0;\n"
2213
        "\t\t\tif (ob_sync||b_started)\n"
2214
                "\t\t\t\toB <= oB + { {(LGSPAN){1\'b0}}, 1\'b1 };\n"
2215
        "\t\t\tif ((ob_sync)&&(~oB[LGSPAN]))\n"
2216
                "\t\t\t// A butterfly output is available\n"
2217
                        "\t\t\t\tb_started <= 1\'b1;\n"
2218 23 dgisselq
        "\t\tend\n\n");
2219 26 dgisselq
        fprintf(fstage,
2220
        "\treg  [(LGSPAN-1):0]\t\tdly_addr;\n"
2221
        "\treg  [(2*OWIDTH-1):0]\tdly_value;\n"
2222
        "\talways @(posedge i_clk)\n"
2223
        "\t\tif (i_ce)\n"
2224
        "\t\tbegin\n"
2225
        "\t\t\tdly_addr <= oB[(LGSPAN-1):0];\n"
2226
        "\t\t\tdly_value <= ob_b;\n"
2227
        "\t\tend\n"
2228
        "\talways @(posedge i_clk)\n"
2229
        "\t\tif (i_ce)\n"
2230
                "\t\t\tomem[dly_addr] <= dly_value;\n"
2231
"\n");
2232
        fprintf(fstage,
2233
        "\talways @(posedge i_clk)\n"
2234
        "\t\tif (i_ce)\n"
2235
        "\t\t\to_data <= (~oB[LGSPAN])?ob_a : omem[oB[(LGSPAN-1):0]];\n"
2236
"\n");
2237 22 dgisselq
        fprintf(fstage, "endmodule\n");
2238 2 dgisselq
}
2239
 
2240
void    usage(void) {
2241
        fprintf(stderr,
2242 26 dgisselq
"USAGE:\tfftgen [-f <size>] [-d dir] [-c cbits] [-n nbits] [-m mxbits] [-s]\n"
2243 2 dgisselq
// "\tfftgen -i\n"
2244 26 dgisselq
"\t-1\tBuild a normal FFT, running at one clock per complex sample, or (for\n"
2245
"\t\ta real FFT) at one clock per two real input samples.\n"
2246 2 dgisselq
"\t-c <cbits>\tCauses all internal complex coefficients to be\n"
2247
"\t\tlonger than the corresponding data bits, to help avoid\n"
2248 32 dgisselq
"\t\tcoefficient truncation errors.  The default is %d bits longer\n"
2249 26 dgisselq
"\t\tthan the data bits.\n"
2250 2 dgisselq
"\t-d <dir>\tPlaces all of the generated verilog files into <dir>.\n"
2251 26 dgisselq
"\t\tThe default is a subdirectory of the current directory named %s.\n"
2252 2 dgisselq
"\t-f <size>\tSets the size of the FFT as the number of complex\n"
2253 26 dgisselq
"\t\tsamples input to the transform.  (No default value, this is\n"
2254
"\t\ta required parameter.)\n"
2255
"\t-i\tAn inverse FFT, meaning that the coefficients are\n"
2256
"\t\tgiven by e^{ j 2 pi k/N n }.  The default is a forward FFT, with\n"
2257
"\t\tcoefficients given by e^{ -j 2 pi k/N n }.\n"
2258 2 dgisselq
"\t-m <mxbits>\tSets the maximum bit width that the FFT should ever\n"
2259
"\t\tproduce.  Internal values greater than this value will be\n"
2260 26 dgisselq
"\t\ttruncated to this value.  (The default value grows the input\n"
2261
"\t\tsize by one bit for every two FFT stages.)\n"
2262 22 dgisselq
"\t-n <nbits>\tSets the bitwidth for values coming into the (i)FFT.\n"
2263 26 dgisselq
"\t\tThe default is %d bits input for each component of the two\n"
2264
"\t\tcomplex values into the FFT.\n"
2265 22 dgisselq
"\t-p <nmpy>\tSets the number of stages that will use any hardware \n"
2266
"\t\tmultiplication facility, instead of shift-add emulation.\n"
2267 26 dgisselq
"\t\tThree multiplies per butterfly, or six multiplies per stage will\n"
2268
"\t\tbe accelerated in this fashion.  The default is not to use any\n"
2269
"\t\thardware multipliers.\n"
2270
"\t-r\tBuild a real-FFT at four input points per sample, rather than a\n"
2271
"\t\tcomplex FFT.  (Default is a Complex FFT.)\n"
2272 2 dgisselq
"\t-s\tSkip the final bit reversal stage.  This is useful in\n"
2273
"\t\talgorithms that need to apply a filter without needing to do\n"
2274
"\t\tbin shifting, as these algorithms can, with this option, just\n"
2275
"\t\tmultiply by a bit reversed correlation sequence and then\n"
2276 22 dgisselq
"\t\tinverse FFT the (still bit reversed) result.  (You would need\n"
2277
"\t\ta decimation in time inverse to do this, which this program does\n"
2278
"\t\tnot yet provide.)\n"
2279 2 dgisselq
"\t-S\tInclude the final bit reversal stage (default).\n"
2280 22 dgisselq
"\t-x <xtrabits>\tUse this many extra bits internally, before any final\n"
2281
"\t\trounding or truncation of the answer to the final number of bits.\n"
2282 26 dgisselq
"\t\tThe default is to use %d extra bits internally.\n",
2283
/*
2284 2 dgisselq
"\t-0\tA forward FFT (default), meaning that the coefficients are\n"
2285
"\t\tgiven by e^{-j 2 pi k/N n }.\n"
2286
"\t-1\tAn inverse FFT, meaning that the coefficients are\n"
2287 26 dgisselq
"\t\tgiven by e^{ j 2 pi k/N n }.\n",
2288
*/
2289
        DEF_XTRACBITS, DEF_COREDIR, DEF_NBITSIN, DEF_XTRAPBITS);
2290 2 dgisselq
}
2291
 
2292
// Features still needed:
2293
//      Interactivity.
2294
int main(int argc, char **argv) {
2295
        int     fftsize = -1, lgsize = -1;
2296 26 dgisselq
        int     nbitsin = DEF_NBITSIN, xtracbits = DEF_XTRACBITS,
2297
                        nummpy=DEF_NMPY, nonmpy=2;
2298
        int     nbitsout, maxbitsout = -1, xtrapbits=DEF_XTRAPBITS;
2299
        bool    bitreverse = true, inverse=false,
2300
                verbose_flag = false, single_clock = false,
2301
                real_fft = false;
2302 2 dgisselq
        FILE    *vmain;
2303 28 dgisselq
        std::string     coredir = DEF_COREDIR, cmdline = "", hdrname = "";
2304 23 dgisselq
        ROUND_T rounding = RND_CONVERGENT;
2305
        // ROUND_T      rounding = RND_HALFUP;
2306 2 dgisselq
 
2307 26 dgisselq
        bool    dbg = false;
2308
        int     dbgstage = 128;
2309
 
2310 2 dgisselq
        if (argc <= 1)
2311
                usage();
2312
 
2313 14 dgisselq
        cmdline = argv[0];
2314 2 dgisselq
        for(int argn=1; argn<argc; argn++) {
2315 14 dgisselq
                cmdline += " ";
2316
                cmdline += argv[argn];
2317
        }
2318
 
2319
        for(int argn=1; argn<argc; argn++) {
2320 2 dgisselq
                if ('-' == argv[argn][0]) {
2321
                        for(int j=1; (argv[argn][j])&&(j<100); j++) {
2322
                                switch(argv[argn][j]) {
2323 26 dgisselq
                                        /*
2324 2 dgisselq
                                        case '0':
2325
                                                inverse = false;
2326
                                                break;
2327 26 dgisselq
                                        */
2328 2 dgisselq
                                        case '1':
2329 26 dgisselq
                                                single_clock = true;
2330 2 dgisselq
                                                break;
2331 28 dgisselq
                                        case 'a':
2332
                                                if (argn+1 >= argc) {
2333
                                                        printf("ERR: No header filename given\n\n");
2334
                                                        usage(); exit(-1);
2335
                                                }
2336
                                                hdrname = argv[++argn];
2337
                                                j+= 200;
2338
                                                break;
2339 2 dgisselq
                                        case 'c':
2340
                                                if (argn+1 >= argc) {
2341 19 dgisselq
                                                        printf("ERR: No extra number of coefficient bits given!\n\n");
2342 2 dgisselq
                                                        usage(); exit(-1);
2343
                                                }
2344
                                                xtracbits = atoi(argv[++argn]);
2345
                                                j+= 200;
2346
                                                break;
2347
                                        case 'd':
2348
                                                if (argn+1 >= argc) {
2349 19 dgisselq
                                                        printf("ERR: No directory given into which to place the core!\n\n");
2350 2 dgisselq
                                                        usage(); exit(-1);
2351
                                                }
2352 14 dgisselq
                                                coredir = argv[++argn];
2353 2 dgisselq
                                                j += 200;
2354
                                                break;
2355 26 dgisselq
                                        case 'D':
2356
                                                dbg = true;
2357
                                                if (argn+1 >= argc) {
2358
                                                        printf("ERR: No debug stage number given!\n\n");
2359
                                                        usage(); exit(-1);
2360
                                                }
2361
                                                dbgstage = atoi(argv[++argn]);
2362
                                                j+= 200;
2363
                                                break;
2364 2 dgisselq
                                        case 'f':
2365
                                                if (argn+1 >= argc) {
2366 19 dgisselq
                                                        printf("ERR: No FFT Size given!\n\n");
2367 2 dgisselq
                                                        usage(); exit(-1);
2368
                                                }
2369
                                                fftsize = atoi(argv[++argn]);
2370
                                                { int sln = strlen(argv[argn]);
2371
                                                if (!isdigit(argv[argn][sln-1])){
2372
                                                        switch(argv[argn][sln-1]) {
2373
                                                        case 'k': case 'K':
2374
                                                                fftsize <<= 10;
2375
                                                                break;
2376
                                                        case 'm': case 'M':
2377
                                                                fftsize <<= 20;
2378
                                                                break;
2379
                                                        case 'g': case 'G':
2380
                                                                fftsize <<= 30;
2381
                                                                break;
2382
                                                        default:
2383 19 dgisselq
                                                                printf("ERR: Unknown FFT size, %s!\n", argv[argn]);
2384 2 dgisselq
                                                                exit(-1);
2385
                                                        }
2386
                                                }}
2387
                                                j += 200;
2388
                                                break;
2389
                                        case 'h':
2390
                                                usage();
2391
                                                exit(0);
2392
                                                break;
2393
                                        case 'i':
2394 26 dgisselq
                                                inverse = true;
2395 2 dgisselq
                                                break;
2396
                                        case 'm':
2397
                                                if (argn+1 >= argc) {
2398 19 dgisselq
                                                        printf("ERR: No maximum output bit value given!\n\n");
2399 2 dgisselq
                                                        exit(-1);
2400
                                                }
2401
                                                maxbitsout = atoi(argv[++argn]);
2402
                                                j += 200;
2403
                                                break;
2404
                                        case 'n':
2405
                                                if (argn+1 >= argc) {
2406 19 dgisselq
                                                        printf("ERR: No input bit size given!\n\n");
2407 2 dgisselq
                                                        exit(-1);
2408
                                                }
2409
                                                nbitsin = atoi(argv[++argn]);
2410
                                                j += 200;
2411
                                                break;
2412 22 dgisselq
                                        case 'p':
2413
                                                if (argn+1 >= argc) {
2414
                                                        printf("ERR: No number given for number of hardware multiply stages!\n\n");
2415
                                                        exit(-1);
2416
                                                }
2417
                                                nummpy = atoi(argv[++argn]);
2418
                                                j += 200;
2419
                                                break;
2420 26 dgisselq
                                        case 'r':
2421
                                                real_fft = true;
2422
                                                break;
2423 2 dgisselq
                                        case 'S':
2424
                                                bitreverse = true;
2425
                                                break;
2426
                                        case 's':
2427
                                                bitreverse = false;
2428
                                                break;
2429 19 dgisselq
                                        case 'x':
2430
                                                if (argn+1 >= argc) {
2431
                                                        printf("ERR: No extra number of bits given!\n\n");
2432
                                                        usage(); exit(-1);
2433
                                                } j+= 200;
2434
                                                xtrapbits = atoi(argv[++argn]);
2435
                                                break;
2436 2 dgisselq
                                        case 'v':
2437
                                                verbose_flag = true;
2438
                                                break;
2439 33 dgisselq
                                        default:
2440 2 dgisselq
                                                printf("Unknown argument, -%c\n", argv[argn][j]);
2441
                                                usage();
2442
                                                exit(-1);
2443
                                }
2444
                        }
2445
                } else {
2446
                        printf("Unrecognized argument, %s\n", argv[argn]);
2447
                        usage();
2448
                        exit(-1);
2449
                }
2450
        }
2451
 
2452 26 dgisselq
        if (real_fft) {
2453
                printf("The real FFT option is not implemented yet, but still on\nmy to do list.  Please try again later.\n");
2454
                exit(0);
2455
        } if (single_clock) {
2456
                printf("The single clock FFT option is not implemented yet, but still on\nmy to do list.  Please try again later.\n");
2457
                exit(0);
2458
        } if (!bitreverse) {
2459
                printf("WARNING: While I can skip the bit reverse stage, the code to do\n");
2460
                printf("an inverse FFT on a bit--reversed input has not yet been\n");
2461
                printf("built.\n");
2462
        }
2463
 
2464 2 dgisselq
        if ((lgsize < 0)&&(fftsize > 1)) {
2465
                for(lgsize=1; (1<<lgsize) < fftsize; lgsize++)
2466
                        ;
2467
        }
2468
 
2469
        if ((fftsize <= 0)||(nbitsin < 1)||(nbitsin>48)) {
2470
                printf("INVALID PARAMETERS!!!!\n");
2471
                exit(-1);
2472
        }
2473
 
2474
 
2475
        if (nextlg(fftsize) != fftsize) {
2476
                fprintf(stderr, "ERR: FFTSize (%d) *must* be a power of two\n",
2477
                                fftsize);
2478
                exit(-1);
2479
        } else if (fftsize < 2) {
2480
                fprintf(stderr, "ERR: Minimum FFTSize is 2, not %d\n",
2481
                                fftsize);
2482
                if (fftsize == 1) {
2483
                        fprintf(stderr, "You do realize that a 1 point FFT makes very little sense\n");
2484
                        fprintf(stderr, "in an FFT operation that handles two samples per clock?\n");
2485
                        fprintf(stderr, "If you really need to do an FFT of this size, the output\n");
2486
                        fprintf(stderr, "can be connected straight to the input.\n");
2487
                } else {
2488
                        fprintf(stderr, "Indeed, a size of %d doesn\'t make much sense to me at all.\n", fftsize);
2489
                        fprintf(stderr, "Is such an operation even defined?\n");
2490
                }
2491
                exit(-1);
2492
        }
2493
 
2494
        // Calculate how many output bits we'll have, and what the log
2495
        // based two size of our FFT is.
2496
        {
2497
                int     tmp_size = fftsize;
2498
 
2499
                // The first stage always accumulates one bit, regardless
2500
                // of whether you need to or not.
2501
                nbitsout = nbitsin + 1;
2502
                tmp_size >>= 1;
2503
 
2504
                while(tmp_size > 4) {
2505
                        nbitsout += 1;
2506
                        tmp_size >>= 2;
2507
                }
2508
 
2509
                if (tmp_size > 1)
2510
                        nbitsout ++;
2511
 
2512
                if (fftsize <= 2)
2513
                        bitreverse = false;
2514
        } if ((maxbitsout > 0)&&(nbitsout > maxbitsout))
2515
                nbitsout = maxbitsout;
2516
 
2517 22 dgisselq
        // Figure out how many multiply stages to use, and how many to skip
2518
        {
2519
                int     lgv = lgval(fftsize);
2520 2 dgisselq
 
2521 22 dgisselq
                nonmpy = lgv - nummpy;
2522
                if (nonmpy < 2) nonmpy = 2;
2523
                nummpy = lgv - nonmpy;
2524
        }
2525
 
2526 2 dgisselq
        {
2527
                struct stat     sbuf;
2528 14 dgisselq
                if (lstat(coredir.c_str(), &sbuf)==0) {
2529 2 dgisselq
                        if (!S_ISDIR(sbuf.st_mode)) {
2530 14 dgisselq
                                fprintf(stderr, "\'%s\' already exists, and is not a directory!\n", coredir.c_str());
2531 2 dgisselq
                                fprintf(stderr, "I will stop now, lest I overwrite something you care about.\n");
2532
                                fprintf(stderr, "To try again, please remove this file.\n");
2533
                                exit(-1);
2534
                        }
2535 33 dgisselq
                } else
2536 14 dgisselq
                        mkdir(coredir.c_str(), 0755);
2537
                if (access(coredir.c_str(), X_OK|W_OK) != 0) {
2538
                        fprintf(stderr, "I have no access to the directory \'%s\'.\n", coredir.c_str());
2539 2 dgisselq
                        exit(-1);
2540
                }
2541
        }
2542
 
2543 28 dgisselq
        if (hdrname.length() > 0) {
2544
                FILE    *hdr = fopen(hdrname.c_str(), "w");
2545
                if (hdr == NULL) {
2546
                        fprintf(stderr, "ERROR: Cannot open %s to create header file\n", hdrname.c_str());
2547
                        perror("O/S Err:");
2548
                        exit(-2);
2549
                }
2550
 
2551
                fprintf(hdr, "/////////////////////////////////////////////////////////////////////////////\n");
2552
                fprintf(hdr, "//\n");
2553
                fprintf(hdr, "// Filename:      %s\n", hdrname.c_str());
2554
                fprintf(hdr, "//\n");
2555
                fprintf(hdr, "// Project:       %s\n", prjname);
2556
                fprintf(hdr, "//\n");
2557
                fprintf(hdr, "// Purpose:       This simple header file captures the internal constants\n");
2558
                fprintf(hdr, "//                within the FFT that were used to build it, for the purpose\n");
2559
                fprintf(hdr, "//                of making C++ integration (and test bench testing) simpler.  That\n");
2560
                fprintf(hdr, "//                is, should the FFT change size, this will note that size change\n");
2561
                fprintf(hdr, "//                and thus any test bench or other C++ program dependent upon\n");
2562
                fprintf(hdr, "//                either the size of the FFT, the number of bits in or out of\n");
2563
                fprintf(hdr, "//                it, etc., can pick up the changes in the defines found within\n");
2564
                fprintf(hdr, "//                this file.\n");
2565
                fprintf(hdr, "//\n");
2566
                fprintf(hdr, "%s", creator);
2567
                fprintf(hdr, "//\n");
2568
                fprintf(hdr, "%s", cpyleft);
2569
                fprintf(hdr, "//\n"
2570
                "//\n"
2571
                "#ifndef %sFFTHDR_H\n"
2572
                "#define %sFFTHDR_H\n"
2573
                "\n"
2574
                "#define\t%sFFT_IWIDTH\t%d\n"
2575
                "#define\t%sFFT_OWIDTH\t%d\n"
2576
                "#define\t%sFFT_LGWIDTH\t%d\n"
2577
                "#define\t%sFFT_SIZE\t(1<<%sFFT_LGWIDTH)\n\n",
2578
                        (inverse)?"I":"", (inverse)?"I":"",
2579
                        (inverse)?"I":"", nbitsin,
2580
                        (inverse)?"I":"", nbitsout,
2581
                        (inverse)?"I":"", lgsize,
2582
                        (inverse)?"I":"", (inverse)?"I":"");
2583
                if (!bitreverse)
2584
                        fprintf(hdr, "#define\t%sFFT_SKIPS_BIT_REVERSE\n",
2585
                                (inverse)?"I":"");
2586
                if (real_fft)
2587
                        fprintf(hdr, "#define\tRL%sFFT\n\n", (inverse)?"I":"");
2588
                if (!single_clock)
2589
                        fprintf(hdr, "#define\tDBLCLK%sFFT\n\n", (inverse)?"I":"");
2590 29 dgisselq
                if (USE_OLD_MULTIPLY)
2591
                        fprintf(hdr, "#define\tUSE_OLD_MULTIPLY\n\n");
2592 33 dgisselq
 
2593 29 dgisselq
                fprintf(hdr, "// Parameters for testing the longbimpy\n");
2594
                fprintf(hdr, "#define\tTST_LONGBIMPY_AW\t%d\n", TST_LONGBIMPY_AW);
2595
#ifdef  TST_LONGBIMPY_BW
2596
                fprintf(hdr, "#define\tTST_LONGBIMPY_BW\t%d\n\n", TST_LONGBIMPY_BW);
2597
#else
2598
                fprintf(hdr, "#define\tTST_LONGBIMPY_BW\tTST_LONGBIMPY_AW\n\n");
2599
#endif
2600
 
2601
                fprintf(hdr, "// Parameters for testing the shift add multiply\n");
2602
                fprintf(hdr, "#define\tTST_SHIFTADDMPY_AW\t%d\n", TST_SHIFTADDMPY_AW);
2603
#ifdef  TST_SHIFTADDMPY_BW
2604
                fprintf(hdr, "#define\tTST_SHIFTADDMPY_BW\t%d\n\n", TST_SHIFTADDMPY_BW);
2605
#else
2606
                fprintf(hdr, "#define\tTST_SHIFTADDMPY_BW\tTST_SHIFTADDMPY_AW\n\n");
2607
#endif
2608
 
2609
#define TST_SHIFTADDMPY_AW      16
2610
#define TST_SHIFTADDMPY_BW      20      // Leave undefined to match AW
2611
                fprintf(hdr, "// Parameters for testing the butterfly\n");
2612
                fprintf(hdr, "#define\tTST_BUTTERFLY_IWIDTH\t%d\n", TST_BUTTERFLY_IWIDTH);
2613
                fprintf(hdr, "#define\tTST_BUTTERFLY_CWIDTH\t%d\n", TST_BUTTERFLY_CWIDTH);
2614
                fprintf(hdr, "#define\tTST_BUTTERFLY_OWIDTH\t%d\n", TST_BUTTERFLY_OWIDTH);
2615
                fprintf(hdr, "#define\tTST_BUTTERFLY_MPYDELAY\t%d\n\n",
2616
                                bflydelay(TST_BUTTERFLY_IWIDTH,
2617
                                        TST_BUTTERFLY_CWIDTH-TST_BUTTERFLY_IWIDTH));
2618
 
2619
                fprintf(hdr, "// Parameters for testing the quarter stage\n");
2620
                fprintf(hdr, "#define\tTST_QTRSTAGE_IWIDTH\t%d\n", TST_QTRSTAGE_IWIDTH);
2621
                fprintf(hdr, "#define\tTST_QTRSTAGE_LGWIDTH\t%d\n\n", TST_QTRSTAGE_LGWIDTH);
2622
 
2623
                fprintf(hdr, "// Parameters for testing the double stage\n");
2624
                fprintf(hdr, "#define\tTST_DBLSTAGE_IWIDTH\t%d\n", TST_DBLSTAGE_IWIDTH);
2625
                fprintf(hdr, "#define\tTST_DBLSTAGE_SHIFT\t%d\n\n", TST_DBLSTAGE_SHIFT);
2626
 
2627
                fprintf(hdr, "// Parameters for testing the bit reversal stage\n");
2628
                fprintf(hdr, "#define\tTST_DBLREVERSE_LGSIZE\t%d\n\n", TST_DBLREVERSE_LGSIZE);
2629 28 dgisselq
                fprintf(hdr, "\n" "#endif\n\n");
2630
                fclose(hdr);
2631
        }
2632
 
2633 14 dgisselq
        {
2634
                std::string     fname_string;
2635
 
2636
                fname_string = coredir;
2637
                fname_string += "/";
2638
                if (inverse) fname_string += "i";
2639
                fname_string += "fftmain.v";
2640
 
2641
                vmain = fopen(fname_string.c_str(), "w");
2642
                if (NULL == vmain) {
2643
                        fprintf(stderr, "Could not open \'%s\' for writing\n", fname_string.c_str());
2644
                        perror("Err from O/S:");
2645
                        exit(-1);
2646
                }
2647 2 dgisselq
        }
2648
 
2649
        fprintf(vmain, "/////////////////////////////////////////////////////////////////////////////\n");
2650
        fprintf(vmain, "//\n");
2651
        fprintf(vmain, "// Filename:    %sfftmain.v\n", (inverse)?"i":"");
2652
        fprintf(vmain, "//\n");
2653
        fprintf(vmain, "// Project:     %s\n", prjname);
2654
        fprintf(vmain, "//\n");
2655
        fprintf(vmain, "// Purpose:     This is the main module in the Doubletime FPGA FFT project.\n");
2656
        fprintf(vmain, "//              As such, all other modules are subordinate to this one.\n");
2657
        fprintf(vmain, "//              (I have been reading too much legalese this week ...)\n");
2658
        fprintf(vmain, "//              This module accomplish a fixed size Complex FFT on %d data\n", fftsize);
2659
        fprintf(vmain, "//              points.  The FFT is fully pipelined, and accepts as inputs\n");
2660
        fprintf(vmain, "//              two complex two\'s complement samples per clock.\n");
2661
        fprintf(vmain, "//\n");
2662
        fprintf(vmain, "// Parameters:\n");
2663
        fprintf(vmain, "//      i_clk\tThe clock.  All operations are synchronous with this clock.\n");
2664
        fprintf(vmain, "//\ti_rst\tSynchronous reset, active high.  Setting this line will\n");
2665
        fprintf(vmain, "//\t\t\tforce the reset of all of the internals to this routine.\n");
2666
        fprintf(vmain, "//\t\t\tFurther, following a reset, the o_sync line will go\n");
2667
        fprintf(vmain, "//\t\t\thigh the same time the first output sample is valid.\n");
2668 32 dgisselq
        fprintf(vmain, "//\ti_ce\tA clock enable line.  If this line is set, this module\n");
2669 2 dgisselq
        fprintf(vmain, "//\t\t\twill accept two complex values as inputs, and produce\n");
2670
        fprintf(vmain, "//\t\t\ttwo (possibly empty) complex values as outputs.\n");
2671 32 dgisselq
        fprintf(vmain, "//\ti_left\tThe first of two complex input samples.  This value is split\n");
2672
        fprintf(vmain, "//\t\t\tinto two two\'s complement numbers, %d bits each, with\n", nbitsin);
2673
        fprintf(vmain, "//\t\t\tthe real portion in the high order bits, and the\n");
2674
        fprintf(vmain, "//\t\t\timaginary portion taking the bottom %d bits.\n", nbitsin);
2675
        fprintf(vmain, "//\ti_right\tThis is the same thing as i_left, only this is the second of\n");
2676
        fprintf(vmain, "//\t\t\ttwo such samples.  Hence, i_left would contain input\n");
2677
        fprintf(vmain, "//\t\t\tsample zero, i_right would contain sample one.  On the\n");
2678
        fprintf(vmain, "//\t\t\tnext clock i_left would contain input sample two,\n");
2679
        fprintf(vmain, "//\t\t\ti_right number three and so forth.\n");
2680
        fprintf(vmain, "//\to_left\tThe first of two output samples, of the same format as i_left,\n");
2681
        fprintf(vmain, "//\t\t\tonly having %d bits for each of the real and imaginary\n", nbitsout);
2682
        fprintf(vmain, "//\t\t\tcomponents, leading to %d bits total.\n", nbitsout*2);
2683
        fprintf(vmain, "//\to_right\tThe second of two output samples produced each clock.  This has\n");
2684
        fprintf(vmain, "//\t\t\tthe same format as o_left.\n");
2685
        fprintf(vmain, "//\to_sync\tA one bit output indicating the first valid sample produced by\n");
2686
        fprintf(vmain, "//\t\t\tthis FFT following a reset.  Ever after, this will\n");
2687
        fprintf(vmain, "//\t\t\tindicate the first sample of an FFT frame.\n");
2688 2 dgisselq
        fprintf(vmain, "//\n");
2689 14 dgisselq
        fprintf(vmain, "// Arguments:\tThis file was computer generated using the\n");
2690
        fprintf(vmain, "//\t\tfollowing command line:\n");
2691
        fprintf(vmain, "//\n");
2692
        fprintf(vmain, "//\t\t%% %s\n", cmdline.c_str());
2693
        fprintf(vmain, "//\n");
2694 2 dgisselq
        fprintf(vmain, "%s", creator);
2695
        fprintf(vmain, "//\n");
2696
        fprintf(vmain, "%s", cpyleft);
2697
 
2698
 
2699
        fprintf(vmain, "//\n");
2700
        fprintf(vmain, "//\n");
2701
        fprintf(vmain, "module %sfftmain(i_clk, i_rst, i_ce,\n", (inverse)?"i":"");
2702
        fprintf(vmain, "\t\ti_left, i_right,\n");
2703 26 dgisselq
        fprintf(vmain, "\t\to_left, o_right, o_sync%s);\n",
2704
                        (dbg)?", o_dbg":"");
2705 2 dgisselq
        fprintf(vmain, "\tparameter\tIWIDTH=%d, OWIDTH=%d, LGWIDTH=%d;\n", nbitsin, nbitsout, lgsize);
2706
        assert(lgsize > 0);
2707
        fprintf(vmain, "\tinput\t\ti_clk, i_rst, i_ce;\n");
2708
        fprintf(vmain, "\tinput\t\t[(2*IWIDTH-1):0]\ti_left, i_right;\n");
2709
        fprintf(vmain, "\toutput\treg\t[(2*OWIDTH-1):0]\to_left, o_right;\n");
2710
        fprintf(vmain, "\toutput\treg\t\t\to_sync;\n");
2711 26 dgisselq
        if (dbg)
2712
                fprintf(vmain, "\toutput\twire\t[33:0]\t\to_dbg;\n");
2713 2 dgisselq
        fprintf(vmain, "\n\n");
2714
 
2715
        fprintf(vmain, "\t// Outputs of the FFT, ready for bit reversal.\n");
2716 33 dgisselq
        fprintf(vmain, "\twire\t[(2*OWIDTH-1):0]\tbr_left, br_right;\n");
2717 2 dgisselq
        fprintf(vmain, "\n\n");
2718
 
2719
        int     tmp_size = fftsize, lgtmp = lgsize;
2720
        if (fftsize == 2) {
2721
                if (bitreverse) {
2722
                        fprintf(vmain, "\treg\tbr_start;\n");
2723 25 dgisselq
                        fprintf(vmain, "\tinitial br_start = 1\'b0;\n");
2724 2 dgisselq
                        fprintf(vmain, "\talways @(posedge i_clk)\n");
2725
                        fprintf(vmain, "\t\tif (i_rst)\n");
2726 26 dgisselq
                        fprintf(vmain, "\t\t\tbr_start <= 1\'b0;\n");
2727 2 dgisselq
                        fprintf(vmain, "\t\telse if (i_ce)\n");
2728 26 dgisselq
                        fprintf(vmain, "\t\t\tbr_start <= 1\'b1;\n");
2729 2 dgisselq
                }
2730
                fprintf(vmain, "\n\n");
2731 6 dgisselq
                fprintf(vmain, "\tdblstage\t#(IWIDTH)\tstage_2(i_clk, i_rst, i_ce,\n");
2732
                fprintf(vmain, "\t\t\t(~i_rst), i_left, i_right, br_left, br_right);\n");
2733 2 dgisselq
                fprintf(vmain, "\n\n");
2734
        } else {
2735
                int     nbits = nbitsin, dropbit=0;
2736 26 dgisselq
                int     obits = nbits+1+xtrapbits;
2737
 
2738
                if ((maxbitsout > 0)&&(obits > maxbitsout))
2739
                        obits = maxbitsout;
2740
 
2741 2 dgisselq
                // Always do a first stage
2742 14 dgisselq
                {
2743 22 dgisselq
                        bool    mpystage;
2744 2 dgisselq
 
2745 22 dgisselq
                        // Last two stages are always non-multiply stages
2746
                        // since the multiplies can be done by adds
2747
                        mpystage = ((lgtmp-2) <= nummpy);
2748
 
2749 28 dgisselq
                        if (mpystage)
2750
                                fprintf(vmain, "\t// A hardware optimized FFT stage\n");
2751
                        fprintf(vmain, "\n\n");
2752
                        fprintf(vmain, "\twire\t\tw_s%d, w_os%d;\n", fftsize, fftsize);
2753
                        fprintf(vmain, "\twire\t[%d:0]\tw_e%d, w_o%d;\n", 2*(obits+xtrapbits)-1, fftsize, fftsize);
2754
                        fprintf(vmain, "\t%sfftstage_e%d%s\t#(IWIDTH,IWIDTH+%d,%d,%d,%d,%d,0)\tstage_e%d(i_clk, i_rst, i_ce,\n",
2755
                                (inverse)?"i":"", fftsize,
2756
                                        ((dbg)&&(dbgstage == fftsize))?"_dbg":"",
2757
                                xtracbits, obits+xtrapbits,
2758
                                lgsize, lgtmp-2, lgdelay(nbits,xtracbits),
2759
                                fftsize);
2760
                        fprintf(vmain, "\t\t\t(~i_rst), i_left, w_e%d, w_s%d%s);\n", fftsize, fftsize, ((dbg)&&(dbgstage == fftsize))?", o_dbg":"");
2761
                        fprintf(vmain, "\t%sfftstage_o%d\t#(IWIDTH,IWIDTH+%d,%d,%d,%d,%d,0)\tstage_o%d(i_clk, i_rst, i_ce,\n",
2762
                                (inverse)?"i":"", fftsize,
2763
                                xtracbits, obits+xtrapbits,
2764
                                lgsize, lgtmp-2, lgdelay(nbits,xtracbits),
2765
                                fftsize);
2766
                        fprintf(vmain, "\t\t\t(~i_rst), i_right, w_o%d, w_os%d);\n", fftsize, fftsize);
2767
                        fprintf(vmain, "\n\n");
2768
 
2769
 
2770
                        std::string     fname;
2771
                        char    numstr[12];
2772
 
2773 14 dgisselq
                        fname = coredir + "/";
2774
                        if (inverse) fname += "i";
2775
                        fname += "fftstage_e";
2776
                        sprintf(numstr, "%d", fftsize);
2777
                        fname += numstr;
2778 26 dgisselq
                        if ((dbg)&&(dbgstage == fftsize))
2779
                                fname += "_dbg";
2780 14 dgisselq
                        fname += ".v";
2781 26 dgisselq
                        build_stage(fname.c_str(), coredir.c_str(), fftsize/2, 0, nbits, inverse, xtracbits, mpystage, (dbg)&&(dbgstage == fftsize));    // Even stage
2782 14 dgisselq
 
2783
                        fname = coredir + "/";
2784
                        if (inverse) fname += "i";
2785
                        fname += "fftstage_o";
2786
                        sprintf(numstr, "%d", fftsize);
2787
                        fname += numstr;
2788
                        fname += ".v";
2789 26 dgisselq
                        build_stage(fname.c_str(), coredir.c_str(), fftsize/2, 1, nbits, inverse, xtracbits, mpystage, false);  // Odd  stage
2790 14 dgisselq
                }
2791
 
2792 26 dgisselq
                nbits = obits;  // New number of input bits
2793 2 dgisselq
                tmp_size >>= 1; lgtmp--;
2794
                dropbit = 0;
2795
                fprintf(vmain, "\n\n");
2796
                while(tmp_size >= 8) {
2797 26 dgisselq
                        obits = nbits+((dropbit)?0:1);
2798 2 dgisselq
 
2799
                        if ((maxbitsout > 0)&&(obits > maxbitsout))
2800
                                obits = maxbitsout;
2801
 
2802 14 dgisselq
                        {
2803 22 dgisselq
                                bool            mpystage;
2804 2 dgisselq
 
2805 22 dgisselq
                                mpystage = ((lgtmp-2) <= nummpy);
2806
 
2807 28 dgisselq
                                if (mpystage)
2808
                                        fprintf(vmain, "\t// A hardware optimized FFT stage\n");
2809
                                fprintf(vmain, "\twire\t\tw_s%d, w_os%d;\n",
2810
                                        tmp_size, tmp_size);
2811
                                fprintf(vmain,"\twire\t[%d:0]\tw_e%d, w_o%d;\n",
2812
                                        2*(obits+xtrapbits)-1,
2813
                                        tmp_size, tmp_size);
2814
                                fprintf(vmain, "\t%sfftstage_e%d%s\t#(%d,%d,%d,%d,%d,%d,%d)\tstage_e%d(i_clk, i_rst, i_ce,\n",
2815
                                        (inverse)?"i":"", tmp_size,
2816
                                        ((dbg)&&(dbgstage==tmp_size))?"_dbg":"",
2817
                                        nbits+xtrapbits,
2818
                                        nbits+xtracbits+xtrapbits,
2819
                                        obits+xtrapbits,
2820
                                        lgsize, lgtmp-2,
2821
                                        lgdelay(nbits+xtrapbits,xtracbits),
2822
                                        (dropbit)?0:0, tmp_size);
2823
                                fprintf(vmain, "\t\t\t\t\t\tw_s%d, w_e%d, w_e%d, w_s%d%s);\n",
2824
                                        tmp_size<<1, tmp_size<<1,
2825
                                        tmp_size, tmp_size,
2826
                                        ((dbg)&&(dbgstage == tmp_size))
2827
                                                ?", o_dbg":"");
2828
                                fprintf(vmain, "\t%sfftstage_o%d\t#(%d,%d,%d,%d,%d,%d,%d)\tstage_o%d(i_clk, i_rst, i_ce,\n",
2829
                                        (inverse)?"i":"", tmp_size,
2830
                                        nbits+xtrapbits,
2831
                                        nbits+xtracbits+xtrapbits,
2832
                                        obits+xtrapbits,
2833
                                        lgsize, lgtmp-2,
2834
                                        lgdelay(nbits+xtrapbits,xtracbits),
2835
                                        (dropbit)?0:0, tmp_size);
2836
                                fprintf(vmain, "\t\t\t\t\t\tw_s%d, w_o%d, w_o%d, w_os%d);\n",
2837
                                        tmp_size<<1, tmp_size<<1,
2838
                                        tmp_size, tmp_size);
2839
                                fprintf(vmain, "\n\n");
2840
 
2841
                                std::string     fname;
2842
                                char            numstr[12];
2843
 
2844 14 dgisselq
                                fname = coredir + "/";
2845
                                if (inverse) fname += "i";
2846
                                fname += "fftstage_e";
2847
                                sprintf(numstr, "%d", tmp_size);
2848
                                fname += numstr;
2849 26 dgisselq
                                if ((dbg)&&(dbgstage == tmp_size))
2850
                                        fname += "_dbg";
2851 14 dgisselq
                                fname += ".v";
2852 26 dgisselq
                                build_stage(fname.c_str(), coredir.c_str(), tmp_size/2, 0,
2853 22 dgisselq
                                        nbits+xtrapbits, inverse, xtracbits,
2854 26 dgisselq
                                        mpystage, ((dbg)&&(dbgstage == tmp_size)));     // Even stage
2855 2 dgisselq
 
2856 14 dgisselq
                                fname = coredir + "/";
2857
                                if (inverse) fname += "i";
2858
                                fname += "fftstage_o";
2859
                                sprintf(numstr, "%d", tmp_size);
2860
                                fname += numstr;
2861
                                fname += ".v";
2862 26 dgisselq
                                build_stage(fname.c_str(), coredir.c_str(), tmp_size/2, 1,
2863 22 dgisselq
                                        nbits+xtrapbits, inverse, xtracbits,
2864 26 dgisselq
                                        mpystage, false);       // Odd  stage
2865 14 dgisselq
                        }
2866
 
2867
 
2868 2 dgisselq
                        dropbit ^= 1;
2869
                        nbits = obits;
2870
                        tmp_size >>= 1; lgtmp--;
2871
                }
2872
 
2873
                if (tmp_size == 4) {
2874 26 dgisselq
                        obits = nbits+((dropbit)?0:1);
2875 2 dgisselq
 
2876
                        if ((maxbitsout > 0)&&(obits > maxbitsout))
2877
                                obits = maxbitsout;
2878
 
2879
                        fprintf(vmain, "\twire\t\tw_s4, w_os4;\n");
2880 19 dgisselq
                        fprintf(vmain, "\twire\t[%d:0]\tw_e4, w_o4;\n", 2*(obits+xtrapbits)-1);
2881 26 dgisselq
                        fprintf(vmain, "\tqtrstage%s\t#(%d,%d,%d,0,%d,%d)\tstage_e4(i_clk, i_rst, i_ce,\n",
2882
                                ((dbg)&&(dbgstage==4))?"_dbg":"",
2883
                                nbits+xtrapbits, obits+xtrapbits, lgsize,
2884
                                (inverse)?1:0, (dropbit)?0:0);
2885
                        fprintf(vmain, "\t\t\t\t\t\tw_s8, w_e8, w_e4, w_s4%s);\n",
2886
                                ((dbg)&&(dbgstage==4))?", o_dbg":"");
2887 2 dgisselq
                        fprintf(vmain, "\tqtrstage\t#(%d,%d,%d,1,%d,%d)\tstage_o4(i_clk, i_rst, i_ce,\n",
2888 19 dgisselq
                                nbits+xtrapbits, obits+xtrapbits, lgsize, (inverse)?1:0, (dropbit)?0:0);
2889 6 dgisselq
                        fprintf(vmain, "\t\t\t\t\t\tw_s8, w_o8, w_o4, w_os4);\n");
2890 2 dgisselq
                        dropbit ^= 1;
2891
                        nbits = obits;
2892
                        tmp_size >>= 1; lgtmp--;
2893
                }
2894
 
2895
                {
2896 26 dgisselq
                        obits = nbits+((dropbit)?0:1);
2897 2 dgisselq
                        if (obits > nbitsout)
2898
                                obits = nbitsout;
2899
                        if ((maxbitsout>0)&&(obits > maxbitsout))
2900
                                obits = maxbitsout;
2901
                        fprintf(vmain, "\twire\t\tw_s2;\n");
2902
                        fprintf(vmain, "\twire\t[%d:0]\tw_e2, w_o2;\n", 2*obits-1);
2903 28 dgisselq
                        if ((nbits+xtrapbits+1 == obits)&&(!dropbit))
2904
                                printf("WARNING: SCALING OFF BY A FACTOR OF TWO--should\'ve dropped a bit in the last stage.\n");
2905 19 dgisselq
                        fprintf(vmain, "\tdblstage\t#(%d,%d,%d)\tstage_2(i_clk, i_rst, i_ce,\n", nbits+xtrapbits, obits,(dropbit)?0:1);
2906 6 dgisselq
                        fprintf(vmain, "\t\t\t\t\tw_s4, w_e4, w_o4, w_e2, w_o2, w_s2);\n");
2907 2 dgisselq
 
2908
                        fprintf(vmain, "\n\n");
2909
                        nbits = obits;
2910
                }
2911
 
2912
                fprintf(vmain, "\t// Prepare for a (potential) bit-reverse stage.\n");
2913
                fprintf(vmain, "\tassign\tbr_left  = w_e2;\n");
2914
                fprintf(vmain, "\tassign\tbr_right = w_o2;\n");
2915
                fprintf(vmain, "\n");
2916
                if (bitreverse) {
2917
                        fprintf(vmain, "\twire\tbr_start;\n");
2918
                        fprintf(vmain, "\treg\tr_br_started;\n");
2919 25 dgisselq
                        fprintf(vmain, "\tinitial\tr_br_started = 1\'b0;\n");
2920 2 dgisselq
                        fprintf(vmain, "\talways @(posedge i_clk)\n");
2921
                        fprintf(vmain, "\t\tif (i_rst)\n");
2922 26 dgisselq
                        fprintf(vmain, "\t\t\tr_br_started <= 1\'b0;\n");
2923
                        fprintf(vmain, "\t\telse if (i_ce)\n");
2924 23 dgisselq
                        fprintf(vmain, "\t\t\tr_br_started <= r_br_started || w_s2;\n");
2925
                        fprintf(vmain, "\tassign\tbr_start = r_br_started || w_s2;\n");
2926 2 dgisselq
                }
2927
        }
2928
 
2929
        fprintf(vmain, "\n");
2930
        fprintf(vmain, "\t// Now for the bit-reversal stage.\n");
2931
        fprintf(vmain, "\twire\tbr_sync;\n");
2932
        fprintf(vmain, "\twire\t[(2*OWIDTH-1):0]\tbr_o_left, br_o_right;\n");
2933
        if (bitreverse) {
2934
                fprintf(vmain, "\tdblreverse\t#(%d,%d)\trevstage(i_clk, i_rst,\n", lgsize, nbitsout);
2935
                fprintf(vmain, "\t\t\t(i_ce & br_start), br_left, br_right,\n");
2936
                fprintf(vmain, "\t\t\tbr_o_left, br_o_right, br_sync);\n");
2937
        } else {
2938
                fprintf(vmain, "\tassign\tbr_o_left  = br_left;\n");
2939
                fprintf(vmain, "\tassign\tbr_o_right = br_right;\n");
2940
                fprintf(vmain, "\tassign\tbr_sync    = w_s2;\n");
2941
        }
2942
 
2943
        fprintf(vmain, "\n\n");
2944
        fprintf(vmain, "\t// Last clock: Register our outputs, we\'re done.\n");
2945 26 dgisselq
        fprintf(vmain, "\tinitial\to_sync  = 1\'b0;\n");
2946 2 dgisselq
        fprintf(vmain, "\talways @(posedge i_clk)\n");
2947 26 dgisselq
        fprintf(vmain, "\t\tif (i_rst)\n");
2948
        fprintf(vmain, "\t\t\to_sync  <= 1\'b0;\n");
2949
        fprintf(vmain, "\t\telse if (i_ce)\n");
2950
        fprintf(vmain, "\t\t\to_sync  <= br_sync;\n");
2951
        fprintf(vmain, "\n");
2952
        fprintf(vmain, "\talways @(posedge i_clk)\n");
2953
        fprintf(vmain, "\t\tif (i_ce)\n");
2954 2 dgisselq
        fprintf(vmain, "\t\tbegin\n");
2955
        fprintf(vmain, "\t\t\to_left  <= br_o_left;\n");
2956
        fprintf(vmain, "\t\t\to_right <= br_o_right;\n");
2957
        fprintf(vmain, "\t\tend\n");
2958
        fprintf(vmain, "\n\n");
2959
        fprintf(vmain, "endmodule\n");
2960
        fclose(vmain);
2961
 
2962 14 dgisselq
        {
2963
                std::string     fname;
2964 2 dgisselq
 
2965 14 dgisselq
                fname = coredir + "/butterfly.v";
2966 23 dgisselq
                build_butterfly(fname.c_str(), xtracbits, rounding);
2967 2 dgisselq
 
2968 22 dgisselq
                if (nummpy > 0) {
2969
                        fname = coredir + "/hwbfly.v";
2970 23 dgisselq
                        build_hwbfly(fname.c_str(), xtracbits, rounding);
2971 22 dgisselq
                }
2972
 
2973 29 dgisselq
                {
2974
                        // To make debugging easier, we build both of these
2975
                        fname = coredir + "/shiftaddmpy.v";
2976
                        build_multiply(fname.c_str());
2977 2 dgisselq
 
2978 29 dgisselq
                        fname = coredir + "/longbimpy.v";
2979
                        build_longbimpy(fname.c_str());
2980
                        fname = coredir + "/bimpy.v";
2981
                        build_bimpy(fname.c_str());
2982
                }
2983
 
2984 26 dgisselq
                if ((dbg)&&(dbgstage == 4)) {
2985
                        fname = coredir + "/qtrstage_dbg.v";
2986
                        build_quarters(fname.c_str(), rounding, true);
2987
                }
2988 14 dgisselq
                fname = coredir + "/qtrstage.v";
2989 26 dgisselq
                build_quarters(fname.c_str(), rounding, false);
2990 2 dgisselq
 
2991 26 dgisselq
                if ((dbg)&&(dbgstage == 2))
2992
                        fname = coredir + "/dblstage_dbg.v";
2993
                else
2994
                        fname = coredir + "/dblstage.v";
2995
                build_dblstage(fname.c_str(), rounding, (dbg)&&(dbgstage==2));
2996 14 dgisselq
 
2997
                if (bitreverse) {
2998
                        fname = coredir + "/dblreverse.v";
2999
                        build_dblreverse(fname.c_str());
3000
                }
3001 23 dgisselq
 
3002
                const   char    *rnd_string = "";
3003
                switch(rounding) {
3004
                        case RND_TRUNCATE:      rnd_string = "/truncate.v"; break;
3005
                        case RND_FROMZERO:      rnd_string = "/roundfromzero.v"; break;
3006
                        case RND_HALFUP:        rnd_string = "/roundhalfup.v"; break;
3007
                        default:
3008
                                rnd_string = "/convround.v"; break;
3009
                } fname = coredir + rnd_string;
3010
                switch(rounding) {
3011
                        case RND_TRUNCATE: build_truncator(fname.c_str()); break;
3012
                        case RND_FROMZERO: build_roundfromzero(fname.c_str()); break;
3013
                        case RND_HALFUP: build_roundhalfup(fname.c_str()); break;
3014
                        default:
3015
                                build_convround(fname.c_str()); break;
3016
                }
3017
 
3018 2 dgisselq
        }
3019
}

powered by: WebSVN 2.1.0

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