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

Subversion Repositories dblclockfft

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

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

Line No. Rev Author Line
1 16 dgisselq
/////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    fftgen.v
4
//
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
//              Once built, this program will build an FFT (or IFFT) core
10
//              of arbitrary width, precision, etc., that will run at
11
//              two samples per clock.  (Incidentally, I didn't pick two
12
//              samples per clock because it was easier, but rather because
13
//              there weren't any two-sample per clock FFT's posted on 
14
//              opencores.com.  Further, FFT's running at one sample per
15
//              clock aren't that hard to find.)
16
//
17
//              You can find the documentation for this program in two places.
18
//              One is in the usage() function below.  The second is in the
19
//              'doc'uments directory that comes with this package, 
20
//              specifically in the spec.pdf file.  If it's not there, type
21
//              make in the documents directory to build it.
22
//
23
// Creator:     Dan Gisselquist, Ph.D.
24
//              Gisselquist Tecnology, LLC
25
//
26
///////////////////////////////////////////////////////////////////////////
27
//
28
// Copyright (C) 2015, Gisselquist Technology, LLC
29
//
30
// This program is free software (firmware): you can redistribute it and/or
31
// modify it under the terms of  the GNU General Public License as published
32
// by the Free Software Foundation, either version 3 of the License, or (at
33
// your option) any later version.
34
//
35
// This program is distributed in the hope that it will be useful, but WITHOUT
36
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
37
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
38
// for more details.
39
//
40
// You should have received a copy of the GNU General Public License along
41
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
42
// target there if the PDF file isn't present.)  If not, see
43
// <http://www.gnu.org/licenses/> for a copy.
44
//
45
// License:     GPL, v3, as defined and found on www.gnu.org,
46
//              http://www.gnu.org/licenses/gpl.html
47
//
48
//
49
///////////////////////////////////////////////////////////////////////////
50
//
51
//
52 2 dgisselq
#include <stdio.h>
53
#include <stdlib.h>
54
#include <unistd.h>
55
#include <sys/stat.h>
56
#include <string.h>
57 14 dgisselq
#include <string>
58 2 dgisselq
#include <math.h>
59
#include <ctype.h>
60
#include <assert.h>
61
 
62
#define COREDIR "fft-core"
63
 
64 23 dgisselq
typedef enum {
65
        RND_TRUNCATE, RND_FROMZERO, RND_HALFUP, RND_CONVERGENT
66
} ROUND_T;
67
 
68 2 dgisselq
const char      cpyleft[] =
69
"///////////////////////////////////////////////////////////////////////////\n"
70
"//\n"
71
"// Copyright (C) 2015, Gisselquist Technology, LLC\n"
72
"//\n"
73
"// This program is free software (firmware): you can redistribute it and/or\n"
74
"// modify it under the terms of  the GNU General Public License as published\n"
75
"// by the Free Software Foundation, either version 3 of the License, or (at\n"
76
"// your option) any later version.\n"
77
"//\n"
78
"// This program is distributed in the hope that it will be useful, but WITHOUT\n"
79
"// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or\n"
80
"// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\n"
81
"// for more details.\n"
82
"//\n"
83
"// You should have received a copy of the GNU General Public License along\n"
84 5 dgisselq
"// with this program.  (It's in the $(ROOT)/doc directory, run make with no\n"
85
"// target there if the PDF file isn\'t present.)  If not, see\n"
86
"// <http://www.gnu.org/licenses/> for a copy.\n"
87
"//\n"
88 2 dgisselq
"// License:    GPL, v3, as defined and found on www.gnu.org,\n"
89
"//             http://www.gnu.org/licenses/gpl.html\n"
90
"//\n"
91
"//\n"
92
"///////////////////////////////////////////////////////////////////////////\n";
93 14 dgisselq
const char      prjname[] = "A Doubletime Pipelined FFT";
94 2 dgisselq
const char      creator[] =     "// Creator:    Dan Gisselquist, Ph.D.\n"
95
                                "//             Gisselquist Tecnology, LLC\n";
96
 
97
int     lgval(int vl) {
98
        int     lg;
99
 
100
        for(lg=1; (1<<lg) < vl; lg++)
101
                ;
102
        return lg;
103
}
104
 
105
int     nextlg(int vl) {
106
        int     r;
107
 
108
        for(r=1; r<vl; r<<=1)
109
                ;
110
        return r;
111
}
112
 
113 14 dgisselq
int     bflydelay(int nbits, int xtra) {
114 2 dgisselq
        int     cbits = nbits + xtra;
115 14 dgisselq
        int     delay;
116 2 dgisselq
        if (nbits+1<cbits)
117 5 dgisselq
                delay = nbits+4;
118 2 dgisselq
        else
119 5 dgisselq
                delay = cbits+3;
120 14 dgisselq
        return delay;
121 2 dgisselq
}
122
 
123 14 dgisselq
int     lgdelay(int nbits, int xtra) {
124
        // The butterfly code needs to compare a valid address, of this
125
        // many bits, with an address two greater.  This guarantees we
126
        // have enough bits for that comparison.  We'll also end up with
127
        // more storage space to look for these values, but without a 
128
        // redesign that's just what we'll deal with.
129
        return lgval(bflydelay(nbits, xtra)+3);
130
}
131
 
132 23 dgisselq
void    build_truncator(const char *fname) {
133
        printf("TRUNCATING!\n");
134 2 dgisselq
        FILE    *fp = fopen(fname, "w");
135
        if (NULL == fp) {
136
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
137
                perror("O/S Err was:");
138
                return;
139
        }
140
 
141
        fprintf(fp,
142
"///////////////////////////////////////////////////////////////////////////\n"
143
"//\n"
144 23 dgisselq
"// Filename:   truncate.v\n"
145
"//             \n"
146
"// Project:    %s\n"
147
"//\n"
148
"// Purpose:    Truncation is one of several options that can be used\n"
149
"//             internal to the various FFT stages to drop bits from one \n"
150
"//             stage to the next.  In general, it is the simplest method\n"
151
"//             of dropping bits, since it requires only a bit selection.\n"
152
"//\n"
153
"//             This form of rounding isn\'t really that great for FFT\'s,\n"
154
"//             since it tends to produce a DC bias in the result.  (Other\n"
155
"//             less pronounced biases may also exist.)\n"
156
"//\n"
157
"//             This particular version also registers the output with the\n"
158
"//             clock, so there will be a delay of one going through this\n"
159
"//             module.  This will keep it in line with the other forms of\n"
160
"//             rounding that can be used.\n"
161
"//\n"
162
"//\n%s"
163
"//\n",
164
                prjname, creator);
165
 
166
        fprintf(fp, "%s", cpyleft);
167
        fprintf(fp,
168
"module truncate(i_clk, i_ce, i_val, o_val);\n"
169
        "\tparameter\tIWID=16, OWID=8, SHIFT=0;\n"
170
        "\tinput\t\t\t\t\ti_clk, i_ce;\n"
171
        "\tinput\t\tsigned\t[(IWID-1):0]\ti_val;\n"
172
        "\toutput\treg\tsigned\t[(OWID-1):0]\to_val;\n"
173
"\n"
174
        "\talways @(posedge i_clk)\n"
175
                "\t\tif (i_ce)\n"
176
                "\t\t\to_val <= i_val[(IWID-1-SHIFT):(IWID-SHIFT-OWID)];\n"
177
"\n"
178
"endmodule\n");
179
}
180
 
181
 
182
void    build_roundhalfup(const char *fname) {
183
        FILE    *fp = fopen(fname, "w");
184
        if (NULL == fp) {
185
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
186
                perror("O/S Err was:");
187
                return;
188
        }
189
 
190
        fprintf(fp,
191
"///////////////////////////////////////////////////////////////////////////\n"
192
"//\n"
193
"// Filename:   roundhalfup.v\n"
194
"//             \n"
195
"// Project:    %s\n"
196
"//\n"
197
"// Purpose:    Rounding half up is the way I was always taught to round in\n"
198
"//             school.  A one half value is added to the result, and then\n"
199
"//             the result is truncated.  When used in an FFT, this produces\n"
200
"//             less bias than the truncation method, although a bias still\n"
201
"//             tends to remain.\n"
202
"//\n"
203
"//\n%s"
204
"//\n",
205
                prjname, creator);
206
 
207
        fprintf(fp, "%s", cpyleft);
208
        fprintf(fp,
209
"module roundhalfup(i_clk, i_ce, i_val, o_val);\n"
210
        "\tparameter\tIWID=16, OWID=8, SHIFT=0;\n"
211
        "\tinput\t\t\t\t\ti_clk, i_ce;\n"
212
        "\tinput\t\tsigned\t[(IWID-1):0]\ti_val;\n"
213
        "\toutput\treg\tsigned\t[(OWID-1):0]\to_val;\n"
214
"\n"
215
        "\t// Let's deal with two cases to be as general as we can be here\n"
216
        "\t//\n"
217
        "\t//   1. The desired output would lose no bits at all\n"
218
        "\t//   2. One or more bits would be dropped, so the rounding is simply\n"
219
        "\t//\t\ta matter of adding one to the bit about to be dropped,\n"
220
        "\t//\t\tmoving all halfway and above numbers up to the next\n"
221
        "\t//\t\tvalue.\n"
222
        "\tgenerate\n"
223
        "\tif (IWID-SHIFT == OWID)\n"
224
        "\tbegin // No truncation or rounding, output drops no bits\n"
225
"\n"
226
                "\t\talways @(posedge i_clk)\n"
227
                        "\t\t\tif (i_ce)\to_val <= i_val[(IWID-SHIFT-1):0];\n"
228
"\n"
229
        "\tend else // if (IWID-SHIFT-1 >= OWID)\n"
230
        "\tbegin // Output drops one bit, can only add one or ... not.\n"
231
                "\t\twire\t[(OWID-1):0] truncated_value, rounded_up;\n"
232
                "\t\twire\t\t\tlast_valid_bit, first_lost_bit;\n"
233
                "\t\tassign\ttruncated_value=i_val[(IWID-1-SHIFT):(IWID-SHIFT-OWID)];\n"
234
                "\t\tassign\trounded_up=truncated_value + {{(OWID-1){1'b0}}, 1'b1 };\n"
235
                "\t\tassign\tfirst_lost_bit = i_val[(IWID-SHIFT-OWID-1)];\n"
236
"\n"
237
                "\t\talways @(posedge i_clk)\n"
238
                "\t\t\tif (i_ce)\n"
239
                "\t\t\tbegin\n"
240
                        "\t\t\t\tif (~first_lost_bit) // Round down / truncate\n"
241
                        "\t\t\t\t\to_val <= truncated_value;\n"
242
                        "\t\t\t\telse\n"
243
                        "\t\t\t\t\to_val <= rounded_up; // even value\n"
244
                "\t\t\tend\n"
245
"\n"
246
        "\tend\n"
247
        "\tendgenerate\n"
248
"\n"
249
"endmodule\n");
250
}
251
 
252
void    build_roundfromzero(const char *fname) {
253
        FILE    *fp = fopen(fname, "w");
254
        if (NULL == fp) {
255
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
256
                perror("O/S Err was:");
257
                return;
258
        }
259
 
260
        fprintf(fp,
261
"///////////////////////////////////////////////////////////////////////////\n"
262
"//\n"
263
"// Filename:   roundfromzero.v\n"
264
"//             \n"
265
"// Project:    %s\n"
266
"//\n"
267
"// Purpose:    Truncation is one of several options that can be used\n"
268
"//             internal to the various FFT stages to drop bits from one \n"
269
"//             stage to the next.  In general, it is the simplest method\n"
270
"//             of dropping bits, since it requires only a bit selection.\n"
271
"//\n"
272
"//             This form of rounding isn\'t really that great for FFT\'s,\n"
273
"//             since it tends to produce a DC bias in the result.  (Other\n"
274
"//             less pronounced biases may also exist.)\n"
275
"//\n"
276
"//             This particular version also registers the output with the\n"
277
"//             clock, so there will be a delay of one going through this\n"
278
"//             module.  This will keep it in line with the other forms of\n"
279
"//             rounding that can be used.\n"
280
"//\n"
281
"//\n%s"
282
"//\n",
283
                prjname, creator);
284
 
285
        fprintf(fp, "%s", cpyleft);
286
        fprintf(fp,
287
"module convround(i_clk, i_ce, i_val, o_val);\n"
288
        "\tparameter\tIWID=16, OWID=8, SHIFT=0;\n"
289
        "\tinput\t\t\t\t\ti_clk, i_ce;\n"
290
        "\tinput\t\tsigned\t[(IWID-1):0]\ti_val;\n"
291
        "\toutput\treg\tsigned\t[(OWID-1):0]\to_val;\n"
292
"\n"
293
        "\t// Let's deal with three cases to be as general as we can be here\n"
294
        "\t//\n"
295
        "\t//\t1. The desired output would lose no bits at all\n"
296
        "\t//\t2. One bit would be dropped, so the rounding is simply\n"
297
        "\t//\t\tadjusting the value to be the closer to zero in\n"
298
        "\t//\t\tcases of being halfway between two.  If identically\n"
299
        "\t//\t\tequal to a number, we just leave it as is.\n"
300
        "\t//\t3. Two or more bits would be dropped.  In this case, we round\n"
301
        "\t//\t\tnormally unless we are rounding a value of exactly\n"
302
        "\t//\t\thalfway between the two.  In the halfway case, we\n"
303
        "\t//\t\tround away from zero.\n"
304
        "\tgenerate\n"
305
        "\tif (IWID-SHIFT == OWID)\n"
306
        "\tbegin // No truncation or rounding, output drops no bits\n"
307
"\n"
308
                "\t\talways @(posedge i_clk)\n"
309
                        "\t\t\tif (i_ce)\to_val <= i_val[(IWID-SHIFT-1):0];\n"
310
"\n"
311
        "\tend else if (IWID-SHIFT-1 == OWID)\n"
312
        "\tbegin // Output drops one bit, can only add one or ... not.\n"
313
        "\t\twire\t[(OWID-1):0]\ttruncated_value, rounded_up;\n"
314
        "\t\twire\t\t\tsign_bit, first_lost_bit;\n"
315
        "\t\tassign\ttruncated_value=i_val[(IWID-1-SHIFT):(IWID-SHIFT-OWID)];\n"
316
        "\t\tassign\trounded_up=truncated_value + {{(OWID-1){1'b0}}, 1'b1 };\n"
317
        "\t\tassign\tfirst_lost_bit = i_val[0];\n"
318
        "\t\tassign\tsign_bit = i_val[(IWID-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 if (sign_bit)\n"
326
                                "\t\t\t\t\to_val <= truncated_value;\n"
327
                        "\t\t\t\telse\n"
328
                                "\t\t\t\t\to_val <= rounded_up;\n"
329
                "\t\t\tend\n"
330
"\n"
331
        "\tend else // If there's more than one bit we are dropping\n"
332
        "\tbegin\n"
333
                "\t\twire\t[(OWID-1):0]\ttruncated_value, rounded_up;\n"
334
                "\t\twire\t\t\tsign_bit, first_lost_bit;\n"
335
                "\t\tassign\ttruncated_value=i_val[(IWID-1-SHIFT):(IWID-SHIFT-OWID)];\n"
336
                "\t\tassign\trounded_up=truncated_value + {{(OWID-1){1'b0}}, 1'b1 };\n"
337
                "\t\tassign\tfirst_lost_bit = i_val[(IWID-SHIFT-OWID-1)];\n"
338
                "\t\tassign\tsign_bit = i_val[(IWID-1)];\n"
339
"\n"
340
                "\t\twire\t[(IWID-SHIFT-OWID-2):0]\tother_lost_bits;\n"
341
                "\t\tassign\tother_lost_bits = i_val[(IWID-SHIFT-OWID-2):0];\n"
342
"\n"
343
                "\t\talways @(posedge i_clk)\n"
344
                        "\t\t\tif (i_ce)\n"
345
                        "\t\t\tbegin\n"
346
                        "\t\t\t\tif (~first_lost_bit) // Round down / truncate\n"
347
                                "\t\t\t\t\to_val <= truncated_value;\n"
348
                        "\t\t\t\telse if (|other_lost_bits) // Round up to\n"
349
                                "\t\t\t\t\to_val <= rounded_up; // closest value\n"
350
                        "\t\t\t\telse if (sign_bit)\n"
351
                                "\t\t\t\t\to_val <= truncated_value;\n"
352
                        "\t\t\t\telse\n"
353
                                "\t\t\t\t\to_val <= rounded_up;\n"
354
                        "\t\t\tend\n"
355
        "\tend\n"
356
        "\tendgenerate\n"
357
"\n"
358
"endmodule\n");
359
}
360
 
361
void    build_convround(const char *fname) {
362
        printf("CONVERGENT--ROUNDING!\n");
363
        FILE    *fp = fopen(fname, "w");
364
        if (NULL == fp) {
365
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
366
                perror("O/S Err was:");
367
                return;
368
        }
369
 
370
        fprintf(fp,
371
"///////////////////////////////////////////////////////////////////////////\n"
372
"//\n"
373
"// Filename:   convround.v\n"
374
"//             \n"
375
"// Project:    %s\n"
376
"//\n"
377
"// Purpose:    A convergent rounding routine, also known as banker\'s\n"
378
"//             rounding, Dutch rounding, Gaussian rounding, unbiased\n"
379
"//             rounding, or ... more, at least according to Wikipedia.\n"
380
"//\n"
381
"//             This form of rounding works by rounding, when the direction\n"
382
"//             is in question, towards the nearest even value.\n"
383
"//\n"
384
"//\n%s"
385
"//\n",
386
                prjname, creator);
387
 
388
        fprintf(fp, "%s", cpyleft);
389
        fprintf(fp,
390
"module convround(i_clk, i_ce, i_val, o_val);\n"
391
"\tparameter\tIWID=16, OWID=8, SHIFT=0;\n"
392
"\tinput\t\t\t\t\ti_clk, i_ce;\n"
393
"\tinput\t\tsigned\t[(IWID-1):0]\ti_val;\n"
394
"\toutput\treg\tsigned\t[(OWID-1):0]\to_val;\n"
395
"\n"
396
"\t// Let's deal with three cases to be as general as we can be here\n"
397
"\t//\n"
398
"\t//\t1. The desired output would lose no bits at all\n"
399
"\t//\t2. One bit would be dropped, so the rounding is simply\n"
400
"\t//\t\tadjusting the value to be the nearest even number in\n"
401
"\t//\t\tcases of being halfway between two.  If identically\n"
402
"\t//\t\tequal to a number, we just leave it as is.\n"
403
"\t//\t3. Two or more bits would be dropped.  In this case, we round\n"
404
"\t//\t\tnormally unless we are rounding a value of exactly\n"
405
"\t//\t\thalfway between the two.  In the halfway case we round\n"
406
"\t//\t\tto the nearest even number.\n"
407
"\tgenerate\n"
408
"\tif (IWID-SHIFT == OWID)\n"
409
"\tbegin // No truncation or rounding, output drops no bits\n"
410
"\n"
411
"\t\talways @(posedge i_clk)\n"
412
"\t\t\tif (i_ce)\to_val <= i_val[(IWID-SHIFT-1):0];\n"
413
"\n"
414
"\tend else if (IWID-SHIFT-1 == OWID)\n"
415
"\tbegin // Output drops one bit, can only add one or ... not.\n"
416
"\t\twire\t[(OWID-1):0] truncated_value, rounded_up;\n"
417
"\t\twire\t\t\tlast_valid_bit, first_lost_bit;\n"
418
"\t\tassign\ttruncated_value=i_val[(IWID-1-SHIFT):(IWID-SHIFT-OWID)];\n"
419
"\t\tassign\trounded_up=truncated_value + {{(OWID-1){1'b0}}, 1'b1 };\n"
420
"\t\tassign\tlast_valid_bit = truncated_value[0];\n"
421
"\t\tassign\tfirst_lost_bit = i_val[0];\n"
422
"\n"
423
"\t\talways @(posedge i_clk)\n"
424
"\t\t\tif (i_ce)\n"
425
"\t\t\tbegin\n"
426
"\t\t\t\tif (~first_lost_bit) // Round down / truncate\n"
427
"\t\t\t\t\to_val <= truncated_value;\n"
428
"\t\t\t\telse if (last_valid_bit)// Round up to nearest\n"
429
"\t\t\t\t\to_val <= rounded_up; // even value\n"
430
"\t\t\t\telse // else round down to the nearest\n"
431
"\t\t\t\t\to_val <= truncated_value; // even value\n"
432
"\t\t\tend\n"
433
"\n"
434
"\tend else // If there's more than one bit we are dropping\n"
435
"\tbegin\n"
436
"\t\twire\t[(OWID-1):0] truncated_value, rounded_up;\n"
437
"\t\twire\t\t\tlast_valid_bit, first_lost_bit;\n"
438
"\t\tassign\ttruncated_value=i_val[(IWID-1-SHIFT):(IWID-SHIFT-OWID)];\n"
439
"\t\tassign\trounded_up=truncated_value + {{(OWID-1){1'b0}}, 1'b1 };\n"
440
"\t\tassign\tlast_valid_bit = truncated_value[0];\n"
441
"\t\tassign\tfirst_lost_bit = i_val[(IWID-SHIFT-OWID-1)];\n"
442
"\n"
443
"\t\twire\t[(IWID-SHIFT-OWID-2):0]\tother_lost_bits;\n"
444
"\t\tassign\tother_lost_bits = i_val[(IWID-SHIFT-OWID-2):0];\n"
445
"\n"
446
"\t\talways @(posedge i_clk)\n"
447
"\t\t\tif (i_ce)\n"
448
"\t\t\tbegin\n"
449
"\t\t\t\tif (~first_lost_bit) // Round down / truncate\n"
450
"\t\t\t\t\to_val <= truncated_value;\n"
451
"\t\t\t\telse if (|other_lost_bits) // Round up to\n"
452
"\t\t\t\t\to_val <= rounded_up; // closest value\n"
453
"\t\t\t\telse if (last_valid_bit) // Round up to\n"
454
"\t\t\t\t\to_val <= rounded_up; // nearest even\n"
455
"\t\t\t\telse   // else round down to nearest even\n"
456
"\t\t\t\t\to_val <= truncated_value;\n"
457
"\t\t\tend\n"
458
"\tend\n"
459
"\tendgenerate\n"
460
"\n"
461
"endmodule\n");
462
}
463
 
464
void    build_quarters(const char *fname, ROUND_T rounding) {
465
        FILE    *fp = fopen(fname, "w");
466
        if (NULL == fp) {
467
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
468
                perror("O/S Err was:");
469
                return;
470
        }
471
        const   char    *rnd_string;
472
        if (rounding == RND_TRUNCATE)
473
                rnd_string = "truncate";
474
        else if (rounding == RND_FROMZERO)
475
                rnd_string = "roundfromzero";
476
        else if (rounding == RND_HALFUP)
477
                rnd_string = "roundhalfup";
478
        else
479
                rnd_string = "convround";
480
 
481
 
482
        fprintf(fp,
483
"///////////////////////////////////////////////////////////////////////////\n"
484
"//\n"
485 2 dgisselq
"// Filename:   qtrstage.v\n"
486
"//             \n"
487
"// Project:    %s\n"
488
"//\n"
489 5 dgisselq
"// Purpose:    This file encapsulates the 4 point stage of a decimation in\n"
490
"//             frequency FFT.  This particular implementation is optimized\n"
491
"//             so that all of the multiplies are accomplished by additions\n"
492
"//             and multiplexers only.\n"
493
"//\n"
494 2 dgisselq
"//\n%s"
495
"//\n",
496
                prjname, creator);
497
        fprintf(fp, "%s", cpyleft);
498
 
499
        fprintf(fp,
500
"module\tqtrstage(i_clk, i_rst, i_ce, i_sync, i_data, o_data, o_sync);\n"
501 5 dgisselq
        "\tparameter    IWIDTH=16, OWIDTH=IWIDTH+1;\n"
502
        "\t// Parameters specific to the core that should be changed when this\n"
503
        "\t// core is built ... Note that the minimum LGSPAN is 2.  Smaller \n"
504
        "\t// spans must use the fftdoubles stage.\n"
505 23 dgisselq
        "\tparameter\tLGWIDTH=8, ODD=0, INVERSE=0,SHIFT=0;\n"
506 5 dgisselq
        "\tinput\t                              i_clk, i_rst, i_ce, i_sync;\n"
507
        "\tinput\t      [(2*IWIDTH-1):0]        i_data;\n"
508
        "\toutput\treg  [(2*OWIDTH-1):0]        o_data;\n"
509
        "\toutput\treg                          o_sync;\n"
510 14 dgisselq
        "\t\n");
511
        fprintf(fp,
512 5 dgisselq
        "\treg\t        wait_for_sync;\n"
513 23 dgisselq
        "\treg\t[3:0]   pipeline;\n"
514 2 dgisselq
"\n"
515 5 dgisselq
        "\treg\t[(IWIDTH):0]    sum_r, sum_i, diff_r, diff_i;\n"
516 14 dgisselq
        "\twire\t[(IWIDTH):0]   n_diff_r, n_diff_i;\n"
517
        "\tassign n_diff_r = -diff_r;\n"
518 5 dgisselq
        "\tassign n_diff_i = -diff_i;\n"
519 2 dgisselq
"\n"
520 23 dgisselq
        "\treg\t[(2*OWIDTH-1):0]\tob_a;\n"
521
        "\twire\t[(2*OWIDTH-1):0]\tob_b;\n"
522
        "\treg\t[(OWIDTH-1):0]\t\tob_b_r, ob_b_i;\n"
523
        "\tassign\tob_b = { ob_b_r, ob_b_i };\n"
524 2 dgisselq
"\n"
525 23 dgisselq
        "\treg\t[(LGWIDTH-1):0]\t\tiaddr;\n"
526
        "\treg\t[(2*IWIDTH-1):0]\timem;\n"
527 2 dgisselq
"\n"
528 5 dgisselq
        "\twire\tsigned\t[(IWIDTH-1):0]\timem_r, imem_i;\n"
529
        "\tassign\timem_r = imem[(2*IWIDTH-1):(IWIDTH)];\n"
530
        "\tassign\timem_i = imem[(IWIDTH-1):0];\n"
531 2 dgisselq
"\n"
532 5 dgisselq
        "\twire\tsigned\t[(IWIDTH-1):0]\ti_data_r, i_data_i;\n"
533
        "\tassign\ti_data_r = i_data[(2*IWIDTH-1):(IWIDTH)];\n"
534
        "\tassign\ti_data_i = i_data[(IWIDTH-1):0];\n"
535 2 dgisselq
"\n"
536 5 dgisselq
        "\treg  [(2*OWIDTH-1):0]        omem;\n"
537 14 dgisselq
"\n");
538
        fprintf(fp,
539 23 dgisselq
        "\twire\tsigned\t[(OWIDTH-1):0]\trnd_sum_r, rnd_sum_i, rnd_diff_r, rnd_diff_i,\n");
540
        fprintf(fp,
541
        "\t\t\t\t\tn_rnd_diff_r, n_rnd_diff_i;\n");
542
        fprintf(fp,
543
        "\t%s\t#(IWIDTH+1,OWIDTH,SHIFT)\tdo_rnd_sum_r(i_clk, i_ce,\n"
544
        "\t\t\t\tsum_r, rnd_sum_r);\n\n", rnd_string);
545
        fprintf(fp,
546
        "\t%s\t#(IWIDTH+1,OWIDTH,SHIFT)\tdo_rnd_sum_i(i_clk, i_ce,\n"
547
        "\t\t\t\tsum_i, rnd_sum_i);\n\n", rnd_string);
548
        fprintf(fp,
549
        "\t%s\t#(IWIDTH+1,OWIDTH,SHIFT)\tdo_rnd_diff_r(i_clk, i_ce,\n"
550
        "\t\t\t\tdiff_r, rnd_diff_r);\n\n", rnd_string);
551
        fprintf(fp,
552
        "\t%s\t#(IWIDTH+1,OWIDTH,SHIFT)\tdo_rnd_diff_i(i_clk, i_ce,\n"
553
        "\t\t\t\tdiff_i, rnd_diff_i);\n\n", rnd_string);
554
        fprintf(fp, "\tassign n_rnd_diff_r = - rnd_diff_r;\n"
555
                "\tassign n_rnd_diff_i = - rnd_diff_i;\n");
556
/*
557
        fprintf(fp,
558 5 dgisselq
        "\twire [(IWIDTH-1):0]  rnd;\n"
559 9 dgisselq
        "\tgenerate\n"
560
        "\tif ((ROUND)&&((IWIDTH+1-OWIDTH-SHIFT)>0))\n"
561
                "\t\tassign rnd = { {(IWIDTH-1){1'b0}}, 1'b1 };\n"
562
        "\telse\n"
563
                "\t\tassign rnd = { {(IWIDTH){1'b0}}};\n"
564
        "\tendgenerate\n"
565 2 dgisselq
"\n"
566 23 dgisselq
*/
567
        fprintf(fp,
568 5 dgisselq
        "\talways @(posedge i_clk)\n"
569
                "\t\tif (i_rst)\n"
570
                "\t\tbegin\n"
571
                        "\t\t\twait_for_sync <= 1'b1;\n"
572
                        "\t\t\tiaddr <= 0;\n"
573 23 dgisselq
                "\t\tend else if ((i_ce)&&((~wait_for_sync)||(i_sync)))\n"
574 5 dgisselq
                "\t\tbegin\n"
575
                        "\t\t\timem <= i_data;\n"
576
                        "\t\t\tiaddr <= iaddr + 1;\n"
577
                        "\t\t\twait_for_sync <= 1'b0;\n"
578 23 dgisselq
                "\t\tend\n\n");
579
        fprintf(fp,
580
        "\t// Note that we don\'t check on wait_for_sync or i_sync here.\n"
581
        "\t// Why not?  Because iaddr will always be zero until after the\n"
582
        "\t// first i_ce, so we are safe.\n"
583
        "\talways\t@(posedge i_clk)\n"
584
                "\t\tif (i_rst)\n"
585
                        "\t\t\tpipeline <= 4'h0;\n"
586
                "\t\telse if (i_ce) // is our pipeline process full?  Which stages?\n"
587
                        "\t\t\tpipeline <= { pipeline[2:0], iaddr[0] };\n\n");
588
        fprintf(fp,
589
        "\t// This is the pipeline[-1] stage, pipeline[0] will be set next.\n"
590
        "\talways\t@(posedge i_clk)\n"
591
                "\t\tif ((i_ce)&&(iaddr[0]))\n"
592
                "\t\tbegin\n"
593
                        "\t\t\tsum_r  <= imem_r + i_data_r;\n"
594
                        "\t\t\tsum_i  <= imem_i + i_data_i;\n"
595
                        "\t\t\tdiff_r <= imem_r - i_data_r;\n"
596
                        "\t\t\tdiff_i <= imem_i - i_data_i;\n"
597
                "\t\tend\n\n");
598
        fprintf(fp,
599
        "\t// pipeline[1] takes sum_x and diff_x and produces rnd_x\n\n");
600
        fprintf(fp,
601
        "\t// Now for pipeline[2]\n"
602
        "\talways\t@(posedge i_clk)\n"
603
                "\t\tif ((i_ce)&&(pipeline[2]))\n"
604
                "\t\tbegin\n"
605
                        "\t\t\tob_a <= { rnd_sum_r, rnd_sum_i };\n"
606
                        "\t\t\t// on Even, W = e^{-j2pi 1/4 0} = 1\n"
607
                        "\t\t\tif (ODD == 0)\n"
608 5 dgisselq
                        "\t\t\tbegin\n"
609 23 dgisselq
                        "\t\t\t\tob_b_r <= rnd_diff_r;\n"
610
                        "\t\t\t\tob_b_i <= rnd_diff_i;\n"
611
                        "\t\t\tend else if (INVERSE==0) begin\n"
612
                        "\t\t\t\t// on Odd, W = e^{-j2pi 1/4} = -j\n"
613
                        "\t\t\t\tob_b_r <=   rnd_diff_i;\n"
614
                        "\t\t\t\tob_b_i <= n_rnd_diff_r;\n"
615
                        "\t\t\tend else begin\n"
616
                        "\t\t\t\t// on Odd, W = e^{j2pi 1/4} = j\n"
617
                        "\t\t\t\tob_b_r <= n_rnd_diff_i;\n"
618
                        "\t\t\t\tob_b_i <=   rnd_diff_r;\n"
619 5 dgisselq
                        "\t\t\tend\n"
620 23 dgisselq
                "\t\tend\n\n");
621
        fprintf(fp,
622
        "\talways\t@(posedge i_clk)\n"
623
                "\t\tif (i_ce)\n"
624
                "\t\tbegin // In sequence, clock = 3\n"
625
                        "\t\t\tif (pipeline[3])\n"
626 5 dgisselq
                        "\t\t\tbegin\n"
627
                                "\t\t\t\tomem <= ob_b;\n"
628
                                "\t\t\t\to_data <= ob_a;\n"
629
                        "\t\t\tend else\n"
630
                                "\t\t\t\to_data <= omem;\n"
631 23 dgisselq
                "\t\tend\n\n");
632
 
633
        fprintf(fp,
634
        "\t// Don\'t forget in the sync check that we are running\n"
635
        "\t// at two clocks per sample.  Thus we need to\n"
636
        "\t// produce a sync every 2^(LGWIDTH-1) clocks.\n"
637
        "\talways\t@(posedge i_clk)\n"
638
                "\t\tif (i_ce)\n"
639
                        "\t\t\to_sync <= &(~iaddr[(LGWIDTH-2):3]) && (iaddr[2:0] == 3'b101);\n");
640
        fprintf(fp, "endmodule\n");
641 2 dgisselq
}
642
 
643 23 dgisselq
void    build_dblstage(const char *fname, ROUND_T rounding) {
644 2 dgisselq
        FILE    *fp = fopen(fname, "w");
645
        if (NULL == fp) {
646
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
647
                perror("O/S Err was:");
648
                return;
649
        }
650
 
651 23 dgisselq
        const   char    *rnd_string;
652
        if (rounding == RND_TRUNCATE)
653
                rnd_string = "truncate";
654
        else if (rounding == RND_FROMZERO)
655
                rnd_string = "roundfromzero";
656
        else if (rounding == RND_HALFUP)
657
                rnd_string = "roundhalfup";
658
        else
659
                rnd_string = "convround";
660
 
661
 
662 2 dgisselq
        fprintf(fp,
663
"///////////////////////////////////////////////////////////////////////////\n"
664
"//\n"
665
"// Filename:   dblstage.v\n"
666
"//\n"
667
"// Project:    %s\n"
668
"//\n"
669
"// Purpose:    This is part of an FPGA implementation that will process\n"
670 5 dgisselq
"//             the final stage of a decimate-in-frequency FFT, running\n"
671
"//             through the data at two samples per clock.  If you notice\n"
672
"//             from the derivation of an FFT, the only time both even and\n"
673
"//             odd samples are used at the same time is in this stage.\n"
674
"//             Therefore, other than this stage and these twiddles, all of\n"
675
"//             the other stages can run two stages at a time at one sample\n"
676
"//             per clock.\n"
677 2 dgisselq
"//\n"
678
"//             In this implementation, the output is valid one clock after\n"
679
"//             the input is valid.  The output also accumulates one bit\n"
680
"//             above and beyond the number of bits in the input.\n"
681
"//             \n"
682
"//             i_clk   A system clock\n"
683 6 dgisselq
"//             i_rst   A synchronous reset\n"
684 2 dgisselq
"//             i_ce    Circuit enable--nothing happens unless this line is high\n"
685 6 dgisselq
"//             i_sync  A synchronization signal, high once per FFT at the start\n"
686 2 dgisselq
"//             i_left  The first (even) complex sample input.  The higher order\n"
687
"//                     bits contain the real portion, low order bits the\n"
688
"//                     imaginary portion, all in two\'s complement.\n"
689
"//             i_right The next (odd) complex sample input, same format as\n"
690
"//                     i_left.\n"
691
"//             o_left  The first (even) complex output.\n"
692
"//             o_right The next (odd) complex output.\n"
693 6 dgisselq
"//             o_sync  Output synchronization signal.\n"
694 2 dgisselq
"//\n%s"
695
"//\n", prjname, creator);
696
 
697
        fprintf(fp, "%s", cpyleft);
698
        fprintf(fp,
699 9 dgisselq
"module\tdblstage(i_clk, i_rst, i_ce, i_sync, i_left, i_right, o_left, o_right, o_sync);\n"
700 23 dgisselq
        "\tparameter\tIWIDTH=16,OWIDTH=IWIDTH+1, SHIFT=0;\n"
701 6 dgisselq
        "\tinput\t\ti_clk, i_rst, i_ce, i_sync;\n"
702 5 dgisselq
        "\tinput\t\t[(2*IWIDTH-1):0]\ti_left, i_right;\n"
703 6 dgisselq
        "\toutput\twire\t[(2*OWIDTH-1):0]\to_left, o_right;\n"
704
        "\toutput\treg\t\t\to_sync;\n"
705 19 dgisselq
        "\n");
706
        fprintf(fp,
707 5 dgisselq
        "\twire\tsigned\t[(IWIDTH-1):0]\ti_in_0r, i_in_0i, i_in_1r, i_in_1i;\n"
708
        "\tassign\ti_in_0r = i_left[(2*IWIDTH-1):(IWIDTH)]; \n"
709
        "\tassign\ti_in_0i = i_left[(IWIDTH-1):0]; \n"
710
        "\tassign\ti_in_1r = i_right[(2*IWIDTH-1):(IWIDTH)]; \n"
711
        "\tassign\ti_in_1i = i_right[(IWIDTH-1):0]; \n"
712
        "\twire\t[(OWIDTH-1):0]\t\to_out_0r, o_out_0i,\n"
713
                                "\t\t\t\t\to_out_1r, o_out_1i;\n"
714 2 dgisselq
"\n"
715 15 dgisselq
"\n"
716 19 dgisselq
        "\t// Handle a potential rounding situation, when IWIDTH>=OWIDTH.\n"
717 15 dgisselq
"\n"
718 23 dgisselq
"\n");
719
        fprintf(fp,
720 5 dgisselq
        "\t// Don't forget that we accumulate a bit by adding two values\n"
721
        "\t// together. Therefore our intermediate value must have one more\n"
722
        "\t// bit than the two originals.\n"
723 23 dgisselq
        "\twire\tsigned\t[(IWIDTH):0]\trnd_in_0r, rnd_in_0i, rnd_in_1r, rnd_in_1i;\n\n");
724
        fprintf(fp,
725
        "\t%s\t#(IWIDTH+1,OWIDTH,SHIFT)\tdo_rnd_0r(i_clk, i_ce,\n"
726
        "\t\t\t\t\t\t\t\trnd_in_0r, o_out_0r);\n\n", rnd_string);
727
        fprintf(fp,
728
        "\t%s\t#(IWIDTH+1,OWIDTH,SHIFT)\tdo_rnd_0i(i_clk, i_ce,\n"
729
        "\t\t\t\t\t\t\t\trnd_in_0i, o_out_0i);\n\n", rnd_string);
730
        fprintf(fp,
731
        "\t%s\t#(IWIDTH+1,OWIDTH,SHIFT)\tdo_rnd_1r(i_clk, i_ce,\n"
732
        "\t\t\t\t\t\t\t\trnd_in_1r, o_out_1r);\n\n", rnd_string);
733
        fprintf(fp,
734
        "\t%s\t#(IWIDTH+1,OWIDTH,SHIFT)\tdo_rnd_1i(i_clk, i_ce,\n"
735
        "\t\t\t\t\t\t\t\trnd_in_1i, o_out_1i);\n\n", rnd_string);
736
 
737
        fprintf(fp,
738
        "\treg\twait_for_sync, rnd_sync;\n"
739 2 dgisselq
"\n"
740 5 dgisselq
        "\talways @(posedge i_clk)\n"
741 6 dgisselq
                "\t\tif (i_rst)\n"
742 23 dgisselq
                "\t\tbegin\n"
743
                        "\t\t\trnd_sync <= 1'b0;\n"
744
                        "\t\t\to_sync <= 1'b0;\n"
745 6 dgisselq
                        "\t\t\twait_for_sync <= 1'b1;\n"
746 23 dgisselq
                "\t\tend else if ((i_ce)&&((~wait_for_sync)||(i_sync)))\n"
747 5 dgisselq
                "\t\tbegin\n"
748 6 dgisselq
                        "\t\t\twait_for_sync <= 1'b0;\n"
749
                        "\t\t\t//\n"
750 23 dgisselq
                        "\t\t\trnd_in_0r <= i_in_0r + i_in_1r;\n"
751
                        "\t\t\trnd_in_0i <= i_in_0i + i_in_1i;\n"
752 5 dgisselq
                        "\t\t\t//\n"
753 23 dgisselq
                        "\t\t\trnd_in_1r <= i_in_0r - i_in_1r;\n"
754
                        "\t\t\trnd_in_1i <= i_in_0i - i_in_1i;\n"
755 6 dgisselq
                        "\t\t\t//\n"
756 23 dgisselq
                        "\t\t\trnd_sync <= i_sync;\n"
757
                        "\t\t\to_sync <= rnd_sync;\n"
758 5 dgisselq
                "\t\tend\n"
759 2 dgisselq
"\n"
760 5 dgisselq
        "\tassign\to_left  = { o_out_0r, o_out_0i };\n"
761
        "\tassign\to_right = { o_out_1r, o_out_1i };\n"
762 2 dgisselq
"\n"
763
"endmodule\n");
764
        fclose(fp);
765
}
766
 
767
void    build_multiply(const char *fname) {
768
        FILE    *fp = fopen(fname, "w");
769
        if (NULL == fp) {
770
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
771
                perror("O/S Err was:");
772
                return;
773
        }
774
 
775
        fprintf(fp,
776
"///////////////////////////////////////////////////////////////////////////\n"
777
"//\n"
778
"// Filename:   shiftaddmpy.v\n"
779
"//\n"
780
"// Project:    %s\n"
781
"//\n"
782
"// Purpose:    A portable shift and add multiply.\n"
783
"//\n"
784
"//             While both Xilinx and Altera will offer single clock \n"
785
"//             multiplies, this simple approach will multiply two numbers\n"
786
"//             on any architecture.  The result maintains the full width\n"
787
"//             of the multiply, there are no extra stuff bits, no rounding,\n"
788
"//             no shifted bits, etc.\n"
789
"//\n"
790
"//             Further, for those applications that can support it, this\n"
791
"//             multiply is pipelined and will produce one answer per clock.\n"
792
"//\n"
793
"//             For minimal processing delay, make the first parameter\n"
794
"//             the one with the least bits, so that AWIDTH <= BWIDTH.\n"
795
"//\n"
796
"//             The processing delay in this multiply is (AWIDTH+1) cycles.\n"
797
"//             That is, if the data is present on the input at clock t=0,\n"
798
"//             the result will be present on the output at time t=AWIDTH+1;\n"
799
"//\n"
800
"//\n%s"
801
"//\n", prjname, creator);
802
 
803
        fprintf(fp, "%s", cpyleft);
804
        fprintf(fp,
805
"module shiftaddmpy(i_clk, i_ce, i_a, i_b, o_r);\n"
806
        "\tparameter\tAWIDTH=16,BWIDTH=AWIDTH;\n"
807
        "\tinput\t\t\t\t\ti_clk, i_ce;\n"
808
        "\tinput\t\t[(AWIDTH-1):0]\t\ti_a;\n"
809
        "\tinput\t\t[(BWIDTH-1):0]\t\ti_b;\n"
810
        "\toutput\treg\t[(AWIDTH+BWIDTH-1):0]\to_r;\n"
811
"\n"
812
        "\treg\t[(AWIDTH-1):0]\tu_a;\n"
813
        "\treg\t[(BWIDTH-1):0]\tu_b;\n"
814
        "\treg\t\t\tsgn;\n"
815
"\n"
816
        "\treg\t[(AWIDTH-2):0]\t\tr_a[0:(AWIDTH-1)];\n"
817
        "\treg\t[(AWIDTH+BWIDTH-2):0]\tr_b[0:(AWIDTH-1)];\n"
818
        "\treg\t\t\t\tr_s[0:(AWIDTH-1)];\n"
819
        "\treg\t[(AWIDTH+BWIDTH-1):0]\tacc[0:(AWIDTH-1)];\n"
820
        "\tgenvar k;\n"
821
"\n"
822 5 dgisselq
        "\t// If we were forced to stay within two\'s complement arithmetic,\n"
823
        "\t// taking the absolute value here would require an additional bit.\n"
824
        "\t// However, because our results are now unsigned, we can stay\n"
825
        "\t// within the number of bits given (for now).\n"
826 2 dgisselq
        "\talways @(posedge i_clk)\n"
827
                "\t\tif (i_ce)\n"
828
                "\t\tbegin\n"
829
                        "\t\t\tu_a <= (i_a[AWIDTH-1])?(-i_a):(i_a);\n"
830
                        "\t\t\tu_b <= (i_b[BWIDTH-1])?(-i_b):(i_b);\n"
831
                        "\t\t\tsgn <= i_a[AWIDTH-1] ^ i_b[BWIDTH-1];\n"
832
                "\t\tend\n"
833
"\n"
834
        "\talways @(posedge i_clk)\n"
835
                "\t\tif (i_ce)\n"
836
                "\t\tbegin\n"
837
                        "\t\t\tacc[0] <= (u_a[0]) ? { {(AWIDTH){1'b0}}, u_b }\n"
838
                        "\t\t\t\t\t: {(AWIDTH+BWIDTH){1'b0}};\n"
839
                        "\t\t\tr_a[0] <= { u_a[(AWIDTH-1):1] };\n"
840
                        "\t\t\tr_b[0] <= { {(AWIDTH-1){1'b0}}, u_b };\n"
841
                        "\t\t\tr_s[0] <= sgn; // The final sign, needs to be preserved\n"
842
                "\t\tend\n"
843
"\n"
844
        "\tgenerate\n"
845 21 dgisselq
        "\tfor(k=0; k<AWIDTH-1; k=k+1)\n"
846 2 dgisselq
        "\tbegin\n"
847 21 dgisselq
                "\t\talways @(posedge i_clk)\n"
848
                "\t\tif (i_ce)\n"
849 2 dgisselq
                "\t\tbegin\n"
850
                        "\t\t\tacc[k+1] <= acc[k] + ((r_a[k][0]) ? {r_b[k],1'b0}:0);\n"
851
                        "\t\t\tr_a[k+1] <= { 1'b0, r_a[k][(AWIDTH-2):1] };\n"
852
                        "\t\t\tr_b[k+1] <= { r_b[k][(AWIDTH+BWIDTH-3):0], 1'b0};\n"
853
                        "\t\t\tr_s[k+1] <= r_s[k];\n"
854
                "\t\tend\n"
855
        "\tend\n"
856
        "\tendgenerate\n"
857
"\n"
858
        "\talways @(posedge i_clk)\n"
859
                "\t\tif (i_ce)\n"
860
                        "\t\t\to_r <= (r_s[AWIDTH-1]) ? (-acc[AWIDTH-1]) : acc[AWIDTH-1];\n"
861
"\n"
862
"endmodule\n");
863
 
864
        fclose(fp);
865
}
866
 
867
void    build_dblreverse(const char *fname) {
868
        FILE    *fp = fopen(fname, "w");
869
        if (NULL == fp) {
870
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
871
                perror("O/S Err was:");
872
                return;
873
        }
874
 
875
        fprintf(fp,
876
"///////////////////////////////////////////////////////////////////////////\n"
877
"//\n"
878
"// Filename:   dblreverse.v\n"
879
"//\n"
880
"// Project:    %s\n"
881
"//\n"
882
"// Purpose:    This module bitreverses a pipelined FFT input.  Operation is\n"
883
"//             expected as follows:\n"
884
"//\n"
885
"//             i_clk   A running clock at whatever system speed is offered.\n"
886
"//             i_rst   A synchronous reset signal, that resets all internals\n"
887
"//             i_ce    If this is one, one input is consumed and an output\n"
888
"//                     is produced.\n"
889
"//             i_in_0, i_in_1\n"
890
"//                     Two inputs to be consumed, each of width WIDTH.\n"
891
"//             o_out_0, o_out_1\n"
892
"//                     Two of the bitreversed outputs, also of the same\n"
893
"//                     width, WIDTH.  Of course, there is a delay from the\n"
894
"//                     first input to the first output.  For this purpose,\n"
895
"//                     o_sync is present.\n"
896
"//             o_sync  This will be a 1'b1 for the first value in any block.\n"
897
"//                     Following a reset, this will only become 1'b1 once\n"
898
"//                     the data has been loaded and is now valid.  After that,\n"
899
"//                     all outputs will be valid.\n"
900
"//\n%s"
901
"//\n", prjname, creator);
902
        fprintf(fp, "%s", cpyleft);
903
        fprintf(fp,
904
"\n\n"
905
"//\n"
906
"// How do we do bit reversing at two smples per clock?  Can we separate out\n"
907
"// our work into eight memory banks, writing two banks at once and reading\n"
908
"// another two banks in the same clock?\n"
909
"//\n"
910
"//     mem[00xxx0] = s_0[n]\n"
911
"//     mem[00xxx1] = s_1[n]\n"
912
"//     o_0[n] = mem[10xxx0]\n"
913
"//     o_1[n] = mem[11xxx0]\n"
914
"//     ...\n"
915
"//     mem[01xxx0] = s_0[m]\n"
916
"//     mem[01xxx1] = s_1[m]\n"
917
"//     o_0[m] = mem[10xxx1]\n"
918
"//     o_1[m] = mem[11xxx1]\n"
919
"//     ...\n"
920
"//     mem[10xxx0] = s_0[n]\n"
921
"//     mem[10xxx1] = s_1[n]\n"
922
"//     o_0[n] = mem[00xxx0]\n"
923
"//     o_1[n] = mem[01xxx0]\n"
924
"//     ...\n"
925
"//     mem[11xxx0] = s_0[m]\n"
926
"//     mem[11xxx1] = s_1[m]\n"
927
"//     o_0[m] = mem[00xxx1]\n"
928
"//     o_1[m] = mem[01xxx1]\n"
929
"//     ...\n"
930
"//\n"
931 5 dgisselq
"//     The answer is that, yes we can but: we need to use four memory banks\n"
932
"//     to do it properly.  These four banks are defined by the two bits\n"
933
"//     that determine the top and bottom of the correct address.  Larger\n"
934
"//     FFT\'s would require more memories.\n"
935
"//\n"
936 2 dgisselq
"//\n");
937
        fprintf(fp,
938
"module dblreverse(i_clk, i_rst, i_ce, i_in_0, i_in_1,\n"
939 5 dgisselq
        "\t\to_out_0, o_out_1, o_sync);\n"
940
        "\tparameter\t\t\tLGSIZE=4, WIDTH=24;\n"
941
        "\tinput\t\t\t\ti_clk, i_rst, i_ce;\n"
942
        "\tinput\t\t[(2*WIDTH-1):0]\ti_in_0, i_in_1;\n"
943
        "\toutput\treg\t[(2*WIDTH-1):0]\to_out_0, o_out_1;\n"
944
        "\toutput\treg\t\t\to_sync;\n"
945 2 dgisselq
"\n"
946 5 dgisselq
        "\treg\tin_reset;\n"
947
        "\treg\t[(LGSIZE):0]\tiaddr;\n"
948
        "\treg\t[(2*WIDTH-1):0]\tmem_0e [0:((1<<(LGSIZE-1))-1)];\n"
949
        "\treg\t[(2*WIDTH-1):0]\tmem_0o [0:((1<<(LGSIZE-1))-1)];\n"
950
        "\treg\t[(2*WIDTH-1):0]\tmem_1e [0:((1<<(LGSIZE-1))-1)];\n"
951
        "\treg\t[(2*WIDTH-1):0]\tmem_1o [0:((1<<(LGSIZE-1))-1)];\n"
952 2 dgisselq
"\n"
953 5 dgisselq
        "\twire\t[(2*LGSIZE-1):0]       braddr;\n"
954
        "\tgenvar\tk;\n"
955 21 dgisselq
        "\tgenerate for(k=0; k<LGSIZE; k=k+1)\n"
956 5 dgisselq
                "\t\tassign braddr[k] = iaddr[LGSIZE-1-k];\n"
957
        "\tendgenerate\n"
958 2 dgisselq
"\n"
959 5 dgisselq
        "\talways @(posedge i_clk)\n"
960
                "\t\tif (i_rst)\n"
961
                "\t\tbegin\n"
962
                        "\t\t\tiaddr <= 0;\n"
963
                        "\t\t\tin_reset <= 1'b1;\n"
964
                "\t\tend else if (i_ce)\n"
965
                "\t\tbegin\n"
966
                        "\t\t\tif (iaddr[(LGSIZE-1)])\n"
967
                        "\t\t\tbegin\n"
968
                                "\t\t\t\tmem_1e[{iaddr[LGSIZE],iaddr[(LGSIZE-2):1]}] <= i_in_0;\n"
969
                                "\t\t\t\tmem_1o[{iaddr[LGSIZE],iaddr[(LGSIZE-2):1]}] <= i_in_1;\n"
970
                        "\t\t\tend else begin\n"
971
                                "\t\t\t\tmem_0e[{iaddr[LGSIZE],iaddr[(LGSIZE-2):1]}] <= i_in_0;\n"
972
                                "\t\t\t\tmem_0o[{iaddr[LGSIZE],iaddr[(LGSIZE-2):1]}] <= i_in_1;\n"
973
                        "\t\t\tend\n"
974
                        "\t\t\tiaddr <= iaddr + 2;\n"
975
                        "\t\t\tif (&iaddr[(LGSIZE-1):1])\n"
976
                                "\t\t\t\tin_reset <= 1'b0;\n"
977
                        "\t\t\tif (in_reset)\n"
978
                        "\t\t\tbegin\n"
979
                                "\t\t\t\to_out_0 <= {(2*WIDTH){1'b0}};\n"
980
                                "\t\t\t\to_out_1 <= {(2*WIDTH){1'b0}};\n"
981
                                "\t\t\t\to_sync <= 1'b0;\n"
982
                        "\t\t\tend else\n"
983
                        "\t\t\tbegin\n"
984
                                "\t\t\t\tif (braddr[0])\n"
985
                                "\t\t\t\tbegin\n"
986 2 dgisselq
"\t\t\t\t\to_out_0 <= mem_0o[{~iaddr[LGSIZE],braddr[(LGSIZE-2):1]}];\n"
987
"\t\t\t\t\to_out_1 <= mem_1o[{~iaddr[LGSIZE],braddr[(LGSIZE-2):1]}];\n"
988 5 dgisselq
                                "\t\t\t\tend else begin\n"
989 2 dgisselq
"\t\t\t\t\to_out_0 <= mem_0e[{~iaddr[LGSIZE],braddr[(LGSIZE-2):1]}];\n"
990
"\t\t\t\t\to_out_1 <= mem_1e[{~iaddr[LGSIZE],braddr[(LGSIZE-2):1]}];\n"
991 5 dgisselq
                                "\t\t\t\tend\n"
992
                                "\t\t\t\to_sync <= ~(|iaddr[(LGSIZE-1):0]);\n"
993
                        "\t\t\tend\n"
994
                "\t\tend\n"
995 2 dgisselq
"\n"
996 21 dgisselq
"endmodule\n");
997 2 dgisselq
 
998
        fclose(fp);
999
}
1000
 
1001 23 dgisselq
void    build_butterfly(const char *fname, int xtracbits, ROUND_T rounding) {
1002 2 dgisselq
        FILE    *fp = fopen(fname, "w");
1003
        if (NULL == fp) {
1004
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
1005
                perror("O/S Err was:");
1006
                return;
1007
        }
1008 23 dgisselq
        const   char    *rnd_string;
1009
        if (rounding == RND_TRUNCATE)
1010
                rnd_string = "truncate";
1011
        else if (rounding == RND_FROMZERO)
1012
                rnd_string = "roundfromzero";
1013
        else if (rounding == RND_HALFUP)
1014
                rnd_string = "roundhalfup";
1015
        else
1016
                rnd_string = "convround";
1017 2 dgisselq
 
1018
        fprintf(fp,
1019
"///////////////////////////////////////////////////////////////////////////\n"
1020
"//\n"
1021
"// Filename:   butterfly.v\n"
1022
"//\n"
1023
"// Project:    %s\n"
1024
"//\n"
1025
"// Purpose:    This routine caculates a butterfly for a decimation\n"
1026
"//             in frequency version of an FFT.  Specifically, given\n"
1027
"//             complex Left and Right values together with a \n"
1028
"//             coefficient, the output of this routine is given\n"
1029
"//             by:\n"
1030
"//\n"
1031
"//             L' = L + R\n"
1032
"//             R' = (L - R)*C\n"
1033
"//\n"
1034
"//             The rest of the junk below handles timing (mostly),\n"
1035
"//             to make certain that L' and R' reach the output at\n"
1036
"//             the same clock.  Further, just to make certain\n"
1037
"//             that is the case, an 'aux' input exists.  This\n"
1038
"//             aux value will come out of this routine synchronized\n"
1039
"//             to the values it came in with.  (i.e., both L', R',\n"
1040
"//             and aux all have the same delay.)  Hence, a caller\n"
1041
"//             of this routine may set aux on the first input with\n"
1042
"//             valid data, and then wait to see aux set on the output\n"
1043
"//             to know when to find the first output with valid data.\n"
1044
"//\n"
1045
"//             All bits are preserved until the very last clock,\n"
1046
"//             where any more bits than OWIDTH will be quietly\n"
1047
"//             discarded.\n"
1048
"//\n"
1049
"//             This design features no overflow checking.\n"
1050
"// \n"
1051
"// Notes:\n"
1052
"//             CORDIC:\n"
1053
"//             Much as we would like, we can't use a cordic here.\n"
1054
"//             The goal is to accomplish an FFT, as defined, and a\n"
1055
"//             CORDIC places a scale factor onto the data.  Removing\n"
1056
"//             the scale factor would cost a two multiplies, which\n"
1057
"//             is precisely what we are trying to avoid.\n"
1058
"//\n"
1059
"//\n"
1060
"//             3-MULTIPLIES:\n"
1061
"//             It should also be possible to do this with three \n"
1062
"//             multiplies and an extra two addition cycles.  \n"
1063
"//\n"
1064
"//             We want\n"
1065
"//                     R+I = (a + jb) * (c + jd)\n"
1066
"//                     R+I = (ac-bd) + j(ad+bc)\n"
1067
"//             We multiply\n"
1068
"//                     P1 = ac\n"
1069
"//                     P2 = bd\n"
1070
"//                     P3 = (a+b)(c+d)\n"
1071
"//             Then \n"
1072
"//                     R+I=(P1-P2)+j(P3-P2-P1)\n"
1073
"//\n"
1074
"//             WIDTHS:\n"
1075
"//             On multiplying an X width number by an\n"
1076
"//             Y width number, X>Y, the result should be (X+Y)\n"
1077
"//             bits, right?\n"
1078
"//             -2^(X-1) <= a <= 2^(X-1) - 1\n"
1079
"//             -2^(Y-1) <= b <= 2^(Y-1) - 1\n"
1080
"//             (2^(Y-1)-1)*(-2^(X-1)) <= ab <= 2^(X-1)2^(Y-1)\n"
1081
"//             -2^(X+Y-2)+2^(X-1) <= ab <= 2^(X+Y-2) <= 2^(X+Y-1) - 1\n"
1082
"//             -2^(X+Y-1) <= ab <= 2^(X+Y-1)-1\n"
1083
"//             YUP!  But just barely.  Do this and you'll really want\n"
1084
"//             to drop a bit, although you will risk overflow in so\n"
1085
"//             doing.\n"
1086
"//\n%s"
1087
"//\n", prjname, creator);
1088
        fprintf(fp, "%s", cpyleft);
1089
 
1090
        fprintf(fp,
1091 6 dgisselq
"module\tbutterfly(i_clk, i_rst, i_ce, i_coef, i_left, i_right, i_aux,\n"
1092 5 dgisselq
                "\t\to_left, o_right, o_aux);\n"
1093
        "\t// Public changeable parameters ...\n"
1094 14 dgisselq
        "\tparameter IWIDTH=%d,CWIDTH=IWIDTH+%d,OWIDTH=IWIDTH+1;\n"
1095 5 dgisselq
        "\t// Parameters specific to the core that should not be changed.\n"
1096 14 dgisselq
        "\tparameter    MPYDELAY=%d'd%d, // (IWIDTH+1 < CWIDTH)?(IWIDTH+4):(CWIDTH+3),\n"
1097 23 dgisselq
                        "\t\t\tSHIFT=0;\n"
1098 5 dgisselq
        "\t// The LGDELAY should be the base two log of the MPYDELAY.  If\n"
1099
        "\t// this value is fractional, then round up to the nearest\n"
1100
        "\t// integer: LGDELAY=ceil(log(MPYDELAY)/log(2));\n"
1101 14 dgisselq
        "\tparameter\tLGDELAY=%d;\n"
1102 6 dgisselq
        "\tinput\t\ti_clk, i_rst, i_ce;\n"
1103 5 dgisselq
        "\tinput\t\t[(2*CWIDTH-1):0] i_coef;\n"
1104
        "\tinput\t\t[(2*IWIDTH-1):0] i_left, i_right;\n"
1105
        "\tinput\t\ti_aux;\n"
1106
        "\toutput\twire [(2*OWIDTH-1):0] o_left, o_right;\n"
1107 21 dgisselq
        "\toutput\treg  o_aux;\n"
1108 14 dgisselq
        "\n", 16, xtracbits, lgdelay(16,xtracbits),
1109
        bflydelay(16, xtracbits), lgdelay(16,xtracbits));
1110
        fprintf(fp,
1111 5 dgisselq
        "\twire\t[(OWIDTH-1):0] o_left_r, o_left_i, o_right_r, o_right_i;\n"
1112 2 dgisselq
"\n"
1113 5 dgisselq
        "\treg\t[(2*IWIDTH-1):0]\tr_left, r_right;\n"
1114
        "\treg\t\t\t\tr_aux, r_aux_2;\n"
1115
        "\treg\t[(2*CWIDTH-1):0]\tr_coef, r_coef_2;\n"
1116
        "\twire\tsigned\t[(CWIDTH-1):0]\tr_coef_r, r_coef_i;\n"
1117
        "\tassign\tr_coef_r  = r_coef_2[ (2*CWIDTH-1):(CWIDTH)];\n"
1118
        "\tassign\tr_coef_i  = r_coef_2[ (  CWIDTH-1):0];\n"
1119
        "\twire\tsigned\t[(IWIDTH-1):0]\tr_left_r, r_left_i, r_right_r, r_right_i;\n"
1120
        "\tassign\tr_left_r  = r_left[ (2*IWIDTH-1):(IWIDTH)];\n"
1121
        "\tassign\tr_left_i  = r_left[ (IWIDTH-1):0];\n"
1122
        "\tassign\tr_right_r = r_right[(2*IWIDTH-1):(IWIDTH)];\n"
1123
        "\tassign\tr_right_i = r_right[(IWIDTH-1):0];\n"
1124 2 dgisselq
"\n"
1125 5 dgisselq
        "\treg\tsigned\t[(IWIDTH):0]\tr_sum_r, r_sum_i, r_dif_r, r_dif_i;\n"
1126 2 dgisselq
"\n"
1127 5 dgisselq
        "\treg  [(LGDELAY-1):0] fifo_addr;\n"
1128
        "\twire [(LGDELAY-1):0] fifo_read_addr;\n"
1129 6 dgisselq
        "\tassign\tfifo_read_addr = fifo_addr - MPYDELAY;\n"
1130 5 dgisselq
        "\treg  [(2*IWIDTH+2):0]        fifo_left [ 0:((1<<LGDELAY)-1)];\n"
1131 6 dgisselq
        "\treg\t\t\t\tovalid;\n"
1132 5 dgisselq
"\n");
1133
        fprintf(fp,
1134
        "\t// Set up the input to the multiply\n"
1135 2 dgisselq
        "\talways @(posedge i_clk)\n"
1136
                "\t\tif (i_ce)\n"
1137
                "\t\tbegin\n"
1138
                        "\t\t\t// One clock just latches the inputs\n"
1139
                        "\t\t\tr_left <= i_left;        // No change in # of bits\n"
1140
                        "\t\t\tr_right <= i_right;\n"
1141
                        "\t\t\tr_aux <= i_aux;\n"
1142
                        "\t\t\tr_coef  <= i_coef;\n"
1143
                        "\t\t\t// Next clock adds/subtracts\n"
1144
                        "\t\t\tr_sum_r <= r_left_r + r_right_r; // Now IWIDTH+1 bits\n"
1145
                        "\t\t\tr_sum_i <= r_left_i + r_right_i;\n"
1146
                        "\t\t\tr_dif_r <= r_left_r - r_right_r;\n"
1147
                        "\t\t\tr_dif_i <= r_left_i - r_right_i;\n"
1148
                        "\t\t\t// Other inputs are simply delayed on second clock\n"
1149
                        "\t\t\tr_aux_2 <= r_aux;\n"
1150
                        "\t\t\tr_coef_2<= r_coef;\n"
1151
        "\t\tend\n"
1152 5 dgisselq
"\n");
1153
        fprintf(fp,
1154
        "\t// Don\'t forget to record the even side, since it doesn\'t need\n"
1155
        "\t// to be multiplied, but yet we still need the results in sync\n"
1156
        "\t// with the answer when it is ready.\n"
1157 2 dgisselq
        "\talways @(posedge i_clk)\n"
1158 6 dgisselq
                "\t\tif (i_rst)\n"
1159 2 dgisselq
                "\t\tbegin\n"
1160 6 dgisselq
                        "\t\t\tfifo_addr <= 0;\n"
1161
                        "\t\t\tovalid <= 1'b0;\n"
1162
                "\t\tend else if (i_ce)\n"
1163
                "\t\tbegin\n"
1164 2 dgisselq
                        "\t\t\t// Need to delay the sum side--nothing else happens\n"
1165
                        "\t\t\t// to it, but it needs to stay synchronized with the\n"
1166
                        "\t\t\t// right side.\n"
1167
                        "\t\t\tfifo_left[fifo_addr] <= { r_aux_2, r_sum_r, r_sum_i };\n"
1168
                        "\t\t\tfifo_addr <= fifo_addr + 1;\n"
1169 14 dgisselq
"\n"
1170
                        "\t\t\tovalid <= (ovalid) || (fifo_addr > (MPYDELAY+1));\n"
1171 2 dgisselq
                "\t\tend\n"
1172
"\n"
1173 5 dgisselq
        "\twire\tsigned\t[(CWIDTH-1):0] ir_coef_r, ir_coef_i;\n"
1174
        "\tassign\tir_coef_r = r_coef_2[(2*CWIDTH-1):CWIDTH];\n"
1175
        "\tassign\tir_coef_i = r_coef_2[(CWIDTH-1):0];\n"
1176
        "\twire\tsigned\t[((IWIDTH+2)+(CWIDTH+1)-1):0]\tp_one, p_two, p_three;\n"
1177 2 dgisselq
"\n"
1178 5 dgisselq
"\n");
1179
        fprintf(fp,
1180
        "\t// Multiply output is always a width of the sum of the widths of\n"
1181
        "\t// the two inputs.  ALWAYS.  This is independent of the number of\n"
1182
        "\t// bits in p_one, p_two, or p_three.  These values needed to \n"
1183
        "\t// accumulate a bit (or two) each.  However, this approach to a\n"
1184
        "\t// three multiply complex multiply cannot increase the total\n"
1185
        "\t// number of bits in our final output.  We\'ll take care of\n"
1186
        "\t// dropping back down to the proper width, OWIDTH, in our routine\n"
1187
        "\t// below.\n"
1188 2 dgisselq
"\n"
1189 5 dgisselq
"\n");
1190
        fprintf(fp,
1191
        "\t// We accomplish here \"Karatsuba\" multiplication.  That is,\n"
1192
        "\t// by doing three multiplies we accomplish the work of four.\n"
1193
        "\t// Let\'s prove to ourselves that this works ... We wish to\n"
1194
        "\t// multiply: (a+jb) * (c+jd), where a+jb is given by\n"
1195
        "\t//\ta + jb = r_dif_r + j r_dif_i, and\n"
1196
        "\t//\tc + jd = ir_coef_r + j ir_coef_i.\n"
1197
        "\t// We do this by calculating the intermediate products P1, P2,\n"
1198
        "\t// and P3 as\n"
1199
        "\t//\tP1 = ac\n"
1200
        "\t//\tP2 = bd\n"
1201
        "\t//\tP3 = (a + b) * (c + d)\n"
1202
        "\t// and then complete our final answer with\n"
1203
        "\t//\tac - bd = P1 - P2 (this checks)\n"
1204
        "\t//\tad + bc = P3 - P2 - P1\n"
1205
        "\t//\t        = (ac + bc + ad + bd) - bd - ac\n"
1206
        "\t//\t        = bc + ad (this checks)\n"
1207 2 dgisselq
"\n"
1208 5 dgisselq
"\n");
1209
        fprintf(fp,
1210
        "\t// This should really be based upon an IF, such as in\n"
1211
        "\t// if (IWIDTH < CWIDTH) then ...\n"
1212
        "\t// However, this is the only (other) way I know to do it.\n"
1213 2 dgisselq
        "\tgenerate\n"
1214
        "\tif (CWIDTH < IWIDTH+1)\n"
1215
        "\tbegin\n"
1216 22 dgisselq
                "\t\twire\t[(CWIDTH):0]\tp3c_in;\n"
1217
                "\t\twire\t[(IWIDTH+1):0]\tp3d_in;\n"
1218
                "\t\tassign\tp3c_in = ir_coef_i + ir_coef_r;\n"
1219
                "\t\tassign\tp3d_in = r_dif_r + r_dif_i;\n"
1220
                "\n"
1221 2 dgisselq
                "\t\t// We need to pad these first two multiplies by an extra\n"
1222 5 dgisselq
                "\t\t// bit just to keep them aligned with the third,\n"
1223
                "\t\t// simpler, multiply.\n"
1224 2 dgisselq
                "\t\tshiftaddmpy #(CWIDTH+1,IWIDTH+2) p1(i_clk, i_ce,\n"
1225
                                "\t\t\t\t{ir_coef_r[CWIDTH-1],ir_coef_r},\n"
1226
                                "\t\t\t\t{r_dif_r[IWIDTH],r_dif_r}, p_one);\n"
1227
                "\t\tshiftaddmpy #(CWIDTH+1,IWIDTH+2) p2(i_clk, i_ce,\n"
1228 5 dgisselq
                                "\t\t\t\t{ir_coef_i[CWIDTH-1],ir_coef_i},\n"
1229 2 dgisselq
                                "\t\t\t\t{r_dif_i[IWIDTH],r_dif_i}, p_two);\n"
1230
                "\t\tshiftaddmpy #(CWIDTH+1,IWIDTH+2) p3(i_clk, i_ce,\n"
1231 22 dgisselq
                        "\t\t\t\tp3c_in, p3d_in, p_three);\n"
1232 2 dgisselq
        "\tend else begin\n"
1233 22 dgisselq
                "\t\twire\t[(CWIDTH):0]\tp3c_in;\n"
1234
                "\t\twire\t[(IWIDTH+1):0]\tp3d_in;\n"
1235
                "\t\tassign\tp3c_in = ir_coef_i + ir_coef_r;\n"
1236
                "\t\tassign\tp3d_in = r_dif_r + r_dif_i;\n"
1237
                "\n"
1238 2 dgisselq
                "\t\tshiftaddmpy #(IWIDTH+2,CWIDTH+1) p1a(i_clk, i_ce,\n"
1239
                                "\t\t\t\t{r_dif_r[IWIDTH],r_dif_r},\n"
1240
                                "\t\t\t\t{ir_coef_r[CWIDTH-1],ir_coef_r}, p_one);\n"
1241
                "\t\tshiftaddmpy #(IWIDTH+2,CWIDTH+1) p2a(i_clk, i_ce,\n"
1242
                                "\t\t\t\t{r_dif_i[IWIDTH], r_dif_i},\n"
1243 5 dgisselq
                                "\t\t\t\t{ir_coef_i[CWIDTH-1],ir_coef_i}, p_two);\n"
1244 2 dgisselq
                "\t\tshiftaddmpy #(IWIDTH+2,CWIDTH+1) p3a(i_clk, i_ce,\n"
1245 22 dgisselq
                                "\t\t\t\tp3d_in, p3c_in, p_three);\n"
1246 2 dgisselq
        "\tend\n"
1247
        "\tendgenerate\n"
1248 5 dgisselq
"\n");
1249
        fprintf(fp,
1250
        "\t// These values are held in memory and delayed during the\n"
1251
        "\t// multiply.  Here, we recover them.  During the multiply,\n"
1252
        "\t// values were multiplied by 2^(CWIDTH-2)*exp{-j*2*pi*...},\n"
1253
        "\t// therefore, the left_x values need to be right shifted by\n"
1254
        "\t// CWIDTH-2 as well.  The additional bits come from a sign\n"
1255
        "\t// extension.\n"
1256 2 dgisselq
        "\twire aux;\n"
1257 5 dgisselq
        "\twire\tsigned\t[(IWIDTH+CWIDTH):0]    fifo_i, fifo_r;\n"
1258
        "\treg\t\t[(2*IWIDTH+2):0]      fifo_read;\n"
1259
        "\tassign\tfifo_r = { {2{fifo_read[2*(IWIDTH+1)-1]}}, fifo_read[(2*(IWIDTH+1)-1):(IWIDTH+1)], {(CWIDTH-2){1'b0}} };\n"
1260
        "\tassign\tfifo_i = { {2{fifo_read[(IWIDTH+1)-1]}}, fifo_read[((IWIDTH+1)-1):0], {(CWIDTH-2){1'b0}} };\n"
1261
        "\tassign\taux = fifo_read[2*IWIDTH+2];\n"
1262 2 dgisselq
"\n"
1263
"\n"
1264 23 dgisselq
        "\treg\tsigned\t[(OWIDTH-1):0]  b_left_r, b_left_i,\n"
1265 5 dgisselq
                        "\t\t\t\t\t\tb_right_r, b_right_i;\n"
1266
        "\treg\tsigned\t[(CWIDTH+IWIDTH+3-1):0] mpy_r, mpy_i;\n"
1267
"\n");
1268
        fprintf(fp,
1269 23 dgisselq
        "\t// Let's do some rounding and remove unnecessary bits.\n"
1270 5 dgisselq
        "\t// We have (IWIDTH+CWIDTH+3) bits here, we need to drop down to\n"
1271
        "\t// OWIDTH, and SHIFT by SHIFT bits in the process.  The trick is\n"
1272
        "\t// that we don\'t need (IWIDTH+CWIDTH+3) bits.  We\'ve accumulated\n"
1273
        "\t// them, but the actual values will never fill all these bits.\n"
1274
        "\t// In particular, we only need:\n"
1275
        "\t//\t IWIDTH bits for the input\n"
1276
        "\t//\t     +1 bit for the add/subtract\n"
1277
        "\t//\t+CWIDTH bits for the coefficient multiply\n"
1278
        "\t//\t     +1 bit for the add/subtract in the complex multiply\n"
1279
        "\t//\t ------\n"
1280
        "\t//\t (IWIDTH+CWIDTH+2) bits at full precision.\n"
1281
        "\t//\n"
1282
        "\t// However, the coefficient multiply multiplied by a maximum value\n"
1283
        "\t// of 2^(CWIDTH-2).  Thus, we only have\n"
1284
        "\t//\t   IWIDTH bits for the input\n"
1285
        "\t//\t       +1 bit for the add/subtract\n"
1286
        "\t//\t+CWIDTH-2 bits for the coefficient multiply\n"
1287
        "\t//\t       +1 (optional) bit for the add/subtract in the cpx mpy.\n"
1288
        "\t//\t -------- ... multiply.  (This last bit may be shifted out.)\n"
1289
        "\t//\t (IWIDTH+CWIDTH) valid output bits. \n"
1290
        "\t// Now, if the user wants to keep any extras of these (via OWIDTH),\n"
1291
        "\t// or if he wishes to arbitrarily shift some of these off (via\n"
1292
        "\t// SHIFT) we accomplish that here.\n"
1293 23 dgisselq
"\n");
1294
        fprintf(fp,
1295
        "\twire\tsigned\t[(OWIDTH-1):0]\trnd_left_r, rnd_left_i, rnd_right_r, rnd_right_i;\n\n");
1296
 
1297
        fprintf(fp,
1298
        "\t%s\t#(CWIDTH+IWIDTH+3,OWIDTH,SHIFT+4)\tdo_rnd_left_r(i_clk, i_ce,\n"
1299
        "\t\t\t\t{ {2{fifo_r[(IWIDTH+CWIDTH)]}}, fifo_r }, rnd_left_r);\n\n",
1300
                rnd_string);
1301
        fprintf(fp,
1302
        "\t%s\t#(CWIDTH+IWIDTH+3,OWIDTH,SHIFT+4)\tdo_rnd_left_i(i_clk, i_ce,\n"
1303
        "\t\t\t\t{ {2{fifo_i[(IWIDTH+CWIDTH)]}}, fifo_i }, rnd_left_i);\n\n",
1304
                rnd_string);
1305
        fprintf(fp,
1306
        "\t%s\t#(CWIDTH+IWIDTH+3,OWIDTH,SHIFT+4)\tdo_rnd_right_r(i_clk, i_ce,\n"
1307
        "\t\t\t\tmpy_r, rnd_right_r);\n\n", rnd_string);
1308
        fprintf(fp,
1309
        "\t%s\t#(CWIDTH+IWIDTH+3,OWIDTH,SHIFT+4)\tdo_rnd_right_i(i_clk, i_ce,\n"
1310
        "\t\t\t\tmpy_i, rnd_right_i);\n\n", rnd_string);
1311
        fprintf(fp,
1312
        "\talways @(posedge i_clk)\n"
1313
                "\t\tif (i_ce)\n"
1314
                "\t\tbegin\n"
1315
                        "\t\t\t// First clock, recover all values\n"
1316
                        "\t\t\tfifo_read <= fifo_left[fifo_read_addr];\n"
1317
                        "\t\t\t// These values are IWIDTH+CWIDTH+3 bits wide\n"
1318
                        "\t\t\t// although they only need to be (IWIDTH+1)\n"
1319
                        "\t\t\t// + (CWIDTH) bits wide.  (We\'ve got two\n"
1320
                        "\t\t\t// extra bits we need to get rid of.)\n"
1321
                        "\t\t\tmpy_r <= p_one - p_two;\n"
1322
                        "\t\t\tmpy_i <= p_three - p_one - p_two;\n"
1323 2 dgisselq
"\n"
1324 23 dgisselq
                        "\t\t\t// Second clock, round and latch for final clock\n"
1325
                        "\t\t\tb_right_r <= rnd_right_r;\n"
1326
                        "\t\t\tb_right_i <= rnd_right_i;\n"
1327
                        "\t\t\tb_left_r <= rnd_left_r;\n"
1328
                        "\t\t\tb_left_i <= rnd_left_i;\n"
1329
                        "\t\t\to_aux <= aux & ovalid;\n"
1330
                "\t\tend\n"
1331
"\n");
1332
        fprintf(fp,
1333 5 dgisselq
        "\t// As a final step, we pack our outputs into two packed two\'s\n"
1334
        "\t// complement numbers per output word, so that each output word\n"
1335
        "\t// has (2*OWIDTH) bits in it, with the top half being the real\n"
1336
        "\t// portion and the bottom half being the imaginary portion.\n"
1337 23 dgisselq
        "\tassign       o_left = { rnd_left_r, rnd_left_i };\n"
1338
        "\tassign       o_right= { rnd_right_r,rnd_right_i};\n"
1339 2 dgisselq
"\n"
1340
"endmodule\n");
1341
        fclose(fp);
1342
}
1343
 
1344 23 dgisselq
void    build_hwbfly(const char *fname, int xtracbits, ROUND_T rounding) {
1345 22 dgisselq
        FILE    *fp = fopen(fname, "w");
1346
        if (NULL == fp) {
1347
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
1348
                perror("O/S Err was:");
1349
                return;
1350
        }
1351
 
1352 23 dgisselq
        const   char    *rnd_string;
1353
        if (rounding == RND_TRUNCATE)
1354
                rnd_string = "truncate";
1355
        else if (rounding == RND_FROMZERO)
1356
                rnd_string = "roundfromzero";
1357
        else if (rounding == RND_HALFUP)
1358
                rnd_string = "roundhalfup";
1359
        else
1360
                rnd_string = "convround";
1361
 
1362
 
1363 22 dgisselq
        fprintf(fp,
1364
"///////////////////////////////////////////////////////////////////////////\n"
1365
"//\n"
1366
"// Filename:   hwbfly.v\n"
1367
"//\n"
1368
"// Project:    %s\n"
1369
"//\n"
1370
"// Purpose:    This routine is identical to the butterfly.v routine found\n"
1371
"//             in 'butterfly.v', save only that it uses the verilog \n"
1372
"//             operator '*' in hopes that the synthesizer would be able\n"
1373
"//             to optimize it with hardware resources.\n"
1374
"//\n"
1375
"//             It is understood that a hardware multiply can complete its\n"
1376
"//             operation in a single clock.\n"
1377
"//\n"
1378
"//\n%s"
1379
"//\n", prjname, creator);
1380
        fprintf(fp, "%s", cpyleft);
1381
        fprintf(fp,
1382
"module hwbfly(i_clk, i_rst, i_ce, i_coef, i_left, i_right, i_aux,\n"
1383
                "\t\to_left, o_right, o_aux);\n"
1384
        "\t// Public changeable parameters ...\n"
1385
        "\tparameter IWIDTH=16,CWIDTH=IWIDTH+%d,OWIDTH=IWIDTH+1;\n"
1386
        "\t// Parameters specific to the core that should not be changed.\n"
1387 23 dgisselq
        "\tparameter\tSHIFT=0;\n"
1388 22 dgisselq
        "\tinput\t\ti_clk, i_rst, i_ce;\n"
1389
        "\tinput\t\t[(2*CWIDTH-1):0]\ti_coef;\n"
1390
        "\tinput\t\t[(2*IWIDTH-1):0]\ti_left, i_right;\n"
1391
        "\tinput\t\ti_aux;\n"
1392
        "\toutput\twire\t[(2*OWIDTH-1):0]\to_left, o_right;\n"
1393
        "\toutput\treg\to_aux;\n"
1394
"\n", xtracbits);
1395
        fprintf(fp,
1396
        "\twire\t[(OWIDTH-1):0] o_left_r, o_left_i, o_right_r, o_right_i;\n"
1397
"\n"
1398
        "\treg\t[(2*IWIDTH-1):0]        r_left, r_right;\n"
1399
        "\treg\t                        r_aux, r_aux_2;\n"
1400
        "\treg\t[(2*CWIDTH-1):0]        r_coef, r_coef_2;\n"
1401
        "\twire\tsigned [(CWIDTH-1):0]  r_coef_r, r_coef_i;\n"
1402
        "\tassign\tr_coef_r  = r_coef_2[ (2*CWIDTH-1):(CWIDTH)];\n"
1403
        "\tassign\tr_coef_i  = r_coef_2[ (  CWIDTH-1):0];\n"
1404
        "\twire signed  [(IWIDTH-1):0]  r_left_r, r_left_i, r_right_r, r_right_i;\n"
1405
        "\tassign\tr_left_r  = r_left[ (2*IWIDTH-1):(IWIDTH)];\n"
1406
        "\tassign\tr_left_i  = r_left[ (IWIDTH-1):0];\n"
1407
        "\tassign\tr_right_r = r_right[(2*IWIDTH-1):(IWIDTH)];\n"
1408
        "\tassign\tr_right_i = r_right[(IWIDTH-1):0];\n"
1409
"\n"
1410
        "\treg  signed  [(IWIDTH):0]    r_sum_r, r_sum_i, r_dif_r, r_dif_i;\n"
1411
"\n"
1412
        "\treg  [(2*IWIDTH+2):0]        leftv, leftvv;\n"
1413
"\n"
1414
        "\t// Set up the input to the multiply\n"
1415
        "\talways @(posedge i_clk)\n"
1416
        "\t\tif (i_rst)\n"
1417
        "\t\tbegin\n"
1418
        "\t\t\tr_aux <= 1'b0;\n"
1419
        "\t\t\tr_aux_2 <= 1'b0;\n"
1420
        "\t\tend else if (i_ce)\n"
1421
        "\t\tbegin\n"
1422
        "\t\t\t// One clock just latches the inputs\n"
1423
        "\t\t\tr_left <= i_left;        // No change in # of bits\n"
1424
        "\t\t\tr_right <= i_right;\n"
1425
        "\t\t\tr_aux <= i_aux;\n"
1426
        "\t\t\tr_coef  <= i_coef;\n"
1427
        "\t\t\t// Next clock adds/subtracts\n"
1428
        "\t\t\tr_sum_r <= r_left_r + r_right_r; // Now IWIDTH+1 bits\n"
1429
        "\t\t\tr_sum_i <= r_left_i + r_right_i;\n"
1430
        "\t\t\tr_dif_r <= r_left_r - r_right_r;\n"
1431
        "\t\t\tr_dif_i <= r_left_i - r_right_i;\n"
1432
        "\t\t\t// Other inputs are simply delayed on second clock\n"
1433
        "\t\t\tr_aux_2 <= r_aux;\n"
1434
        "\t\t\tr_coef_2<= r_coef;\n"
1435
        "\t\tend\n"
1436
        "\n\n");
1437
        fprintf(fp,
1438
"\t// See comments in the butterfly.v source file for a discussion of\n"
1439
"\t// these operations and the appropriate bit widths.\n\n");
1440
        fprintf(fp,
1441
        "\twire signed  [(CWIDTH-1):0]  ir_coef_r, ir_coef_i;\n"
1442
        "\tassign       ir_coef_r = r_coef_2[(2*CWIDTH-1):CWIDTH];\n"
1443
        "\tassign       ir_coef_i = r_coef_2[(CWIDTH-1):0];\n"
1444
        "\treg\tsigned  [((IWIDTH+2)+(CWIDTH+1)-1):0]   p_one, p_two, p_three;\n"
1445
"\n"
1446
        "\treg\tsigned  [(CWIDTH):0]    p3c_in, p1c_in, p2c_in;\n"
1447
        "\treg\tsigned  [(IWIDTH+1):0]  p3d_in, p1d_in, p2d_in;\n"
1448
        "\treg\t[3:0]           pipeline;\n"
1449
"\n"
1450
        "\talways @(posedge i_clk)\n"
1451
        "\tbegin\n"
1452
                "\t\tif (i_rst)\n"
1453
                "\t\tbegin\n"
1454
                        "\t\t\tpipeline <= 4'h0;\n"
1455
                        "\t\t\tleftv <= 0;\n"
1456
                        "\t\t\tleftvv <= 0;\n"
1457
                "\t\tend else if (i_clk)\n"
1458
                "\t\tbegin\n"
1459
                        "\t\t\t// Second clock, pipeline = 1\n"
1460
                        "\t\t\tp1c_in <= { ir_coef_r[(CWIDTH-1)], ir_coef_r };\n"
1461
                        "\t\t\tp2c_in <= { ir_coef_i[(CWIDTH-1)], ir_coef_i };\n"
1462
                        "\t\t\tp1d_in <= { r_dif_r[(IWIDTH)], r_dif_r };\n"
1463
                        "\t\t\tp2d_in <= { r_dif_i[(IWIDTH)], r_dif_i };\n"
1464
                        "\t\t\tp3c_in <= ir_coef_i + ir_coef_r;\n"
1465
                        "\t\t\tp3d_in <= r_dif_r + r_dif_i;\n"
1466 23 dgisselq
"\n"
1467 22 dgisselq
                        "\t\t\tleftv <= { r_aux_2, r_sum_r, r_sum_i };\n"
1468 23 dgisselq
"\n"
1469 22 dgisselq
                        "\t\t\t// Third clock, pipeline = 3\n"
1470
                        "\t\t\tp_one   <= p1c_in * p1d_in;\n"
1471
                        "\t\t\tp_two   <= p2c_in * p2d_in;\n"
1472
                        "\t\t\tp_three <= p3c_in * p3d_in;\n"
1473
                        "\t\t\tleftvv <= leftv;\n"
1474
"\n"
1475
                        "\t\t\tpipeline <= { pipeline[2:0], 1'b1 };\n"
1476
                "\t\tend\n"
1477
        "\tend\n"
1478
"\n");
1479
 
1480
        fprintf(fp,
1481
        "\t// These values are held in memory and delayed during the\n"
1482
        "\t// multiply.  Here, we recover them.  During the multiply,\n"
1483
        "\t// values were multiplied by 2^(CWIDTH-2)*exp{-j*2*pi*...},\n"
1484
        "\t// therefore, the left_x values need to be right shifted by\n"
1485
        "\t// CWIDTH-2 as well.  The additional bits come from a sign\n"
1486
        "\t// extension.\n"
1487 23 dgisselq
        "\twire\taux_s, aux_ss;\n"
1488 22 dgisselq
        "\twire\tsigned\t[(IWIDTH+CWIDTH):0]    left_si, left_sr;\n"
1489
        "\treg\t\t[(2*IWIDTH+2):0]      left_saved;\n"
1490
        "\tassign\tleft_sr = { {2{left_saved[2*(IWIDTH+1)-1]}}, left_saved[(2*(IWIDTH+1)-1):(IWIDTH+1)], {(CWIDTH-2){1'b0}} };\n"
1491
        "\tassign\tleft_si = { {2{left_saved[(IWIDTH+1)-1]}}, left_saved[((IWIDTH+1)-1):0], {(CWIDTH-2){1'b0}} };\n"
1492
        "\tassign\taux_s = left_saved[2*IWIDTH+2];\n"
1493
"\n"
1494
"\n"
1495 23 dgisselq
        "\treg  signed  [(CWIDTH+IWIDTH+3-1):0] mpy_r, mpy_i;\n");
1496
        fprintf(fp,
1497
        "\twire\tsigned\t[(OWIDTH-1):0]\trnd_left_r, rnd_left_i, rnd_right_r, rnd_right_i;\n\n");
1498 22 dgisselq
 
1499
        fprintf(fp,
1500 23 dgisselq
        "\t%s\t#(CWIDTH+IWIDTH+3,OWIDTH,SHIFT+4)\tdo_rnd_left_r(i_clk, i_ce,\n"
1501
        "\t\t\t\t{ {2{left_sr[(IWIDTH+CWIDTH)]}}, left_sr }, rnd_left_r);\n\n",
1502
                rnd_string);
1503
        fprintf(fp,
1504
        "\t%s\t#(CWIDTH+IWIDTH+3,OWIDTH,SHIFT+4)\tdo_rnd_left_i(i_clk, i_ce,\n"
1505
        "\t\t\t\t{ {2{left_si[(IWIDTH+CWIDTH)]}}, left_si }, rnd_left_i);\n\n",
1506
                rnd_string);
1507
        fprintf(fp,
1508
        "\t%s\t#(CWIDTH+IWIDTH+3,OWIDTH,SHIFT+4)\tdo_rnd_right_r(i_clk, i_ce,\n"
1509
        "\t\t\t\tmpy_r, rnd_right_r);\n\n", rnd_string);
1510
        fprintf(fp,
1511
        "\t%s\t#(CWIDTH+IWIDTH+3,OWIDTH,SHIFT+4)\tdo_rnd_right_i(i_clk, i_ce,\n"
1512
        "\t\t\t\tmpy_i, rnd_right_i);\n\n", rnd_string);
1513
 
1514
        fprintf(fp,
1515 22 dgisselq
        "\talways @(posedge i_clk)\n"
1516
        "\t\tif (i_rst)\n"
1517
        "\t\tbegin\n"
1518
                "\t\t\tleft_saved <= 0;\n"
1519
                "\t\t\to_aux <= 1'b0;\n"
1520
        "\t\tend else if (i_ce)\n"
1521
        "\t\tbegin\n"
1522
                "\t\t\t// First clock, recover all values\n"
1523
                "\t\t\tleft_saved <= leftvv;\n"
1524
                "\t\t\t// These values are IWIDTH+CWIDTH+3 bits wide\n"
1525
                "\t\t\t// although they only need to be (IWIDTH+1)\n"
1526
                "\t\t\t// + (CWIDTH) bits wide.  (We've got two\n"
1527
                "\t\t\t// extra bits we need to get rid of.)\n"
1528
                "\t\t\tmpy_r <= p_one - p_two;\n"
1529
                "\t\t\tmpy_i <= p_three - p_one - p_two;\n"
1530
"\n"
1531
                "\t\t\t// Second clock, round and latch for final clock\n"
1532
"\n"
1533
                "\t\t\to_aux <= aux_s;\n"
1534
        "\t\tend\n"
1535
        "\n");
1536
 
1537
        fprintf(fp,
1538
        "\t// As a final step, we pack our outputs into two packed two's\n"
1539
        "\t// complement numbers per output word, so that each output word\n"
1540
        "\t// has (2*OWIDTH) bits in it, with the top half being the real\n"
1541
        "\t// portion and the bottom half being the imaginary portion.\n"
1542 23 dgisselq
        "\tassign\to_left = { rnd_left_r, rnd_left_i };\n"
1543
        "\tassign\to_right= { rnd_right_r,rnd_right_i};\n"
1544 22 dgisselq
"\n"
1545
"endmodule\n");
1546
 
1547
}
1548
 
1549
void    build_stage(const char *fname, int stage, bool odd, int nbits, bool inv, int xtra, bool hwmpy=false) {
1550 2 dgisselq
        FILE    *fstage = fopen(fname, "w");
1551
        int     cbits = nbits + xtra;
1552
 
1553
        if ((cbits * 2) >= sizeof(long long)*8) {
1554
                fprintf(stderr, "ERROR: CMEM Coefficient precision requested overflows long long data type.\n");
1555
                exit(-1);
1556
        }
1557
 
1558
        if (fstage == NULL) {
1559
                fprintf(stderr, "ERROR: Could not open %s for writing!\n", fname);
1560
                perror("O/S Err was:");
1561
                fprintf(stderr, "Attempting to continue, but this file will be missing.\n");
1562
                return;
1563
        }
1564
 
1565
        fprintf(fstage,
1566
"////////////////////////////////////////////////////////////////////////////\n"
1567
"//\n"
1568
"// Filename:   %sfftstage_%c%d.v\n"
1569
"//\n"
1570
"// Project:    %s\n"
1571
"//\n"
1572
"// Purpose:    This file is (almost) a Verilog source file.  It is meant to\n"
1573
"//             be used by a FFT core compiler to generate FFTs which may be\n"
1574
"//             used as part of an FFT core.  Specifically, this file \n"
1575
"//             encapsulates the options of an FFT-stage.  For any 2^N length\n"
1576
"//             FFT, there shall be (N-1) of these stages.  \n"
1577
"//\n%s"
1578
"//\n",
1579
                (inv)?"i":"", (odd)?'o':'e', stage*2, prjname, creator);
1580
        fprintf(fstage, "%s", cpyleft);
1581
        fprintf(fstage, "module\t%sfftstage_%c%d(i_clk, i_rst, i_ce, i_sync, i_data, o_data, o_sync);\n",
1582
                (inv)?"i":"", (odd)?'o':'e', stage*2);
1583
        // These parameter values are useless at this point--they are to be
1584
        // replaced by the parameter values in the calling program.  Only
1585
        // problem is, the CWIDTH needs to match exactly!
1586
        fprintf(fstage, "\tparameter\tIWIDTH=%d,CWIDTH=%d,OWIDTH=%d;\n",
1587
                nbits, cbits, nbits+1);
1588
        fprintf(fstage,
1589
"\t// Parameters specific to the core that should be changed when this\n"
1590
"\t// core is built ... Note that the minimum LGSPAN (the base two log\n"
1591
"\t// of the span, or the base two log of the current FFT size) is 3.\n"
1592
"\t// Smaller spans (i.e. the span of 2) must use the dblstage module.\n"
1593 6 dgisselq
"\tparameter\tLGWIDTH=11, LGSPAN=9, LGBDLY=5, BFLYSHIFT=0;\n");
1594 2 dgisselq
        fprintf(fstage,
1595
"\tinput                                        i_clk, i_rst, i_ce, i_sync;\n"
1596
"\tinput                [(2*IWIDTH-1):0]        i_data;\n"
1597
"\toutput       reg     [(2*OWIDTH-1):0]        o_data;\n"
1598
"\toutput       reg                             o_sync;\n"
1599
"\n"
1600
"\treg  wait_for_sync;\n"
1601
"\treg  [(2*IWIDTH-1):0]        ib_a, ib_b;\n"
1602
"\treg  [(2*CWIDTH-1):0]        ib_c;\n"
1603 8 dgisselq
"\treg  ib_sync;\n"
1604 2 dgisselq
"\n"
1605
"\treg  b_started;\n"
1606
"\twire ob_sync;\n"
1607 23 dgisselq
"\twire [(2*OWIDTH-1):0]\tob_a, ob_b;\n");
1608 2 dgisselq
        fprintf(fstage,
1609
"\n"
1610
"\t// %scmem is defined as an array of real and complex values,\n"
1611
"\t// where the top CWIDTH bits are the real value and the bottom\n"
1612
"\t// CWIDTH bits are the imaginary value.\n"
1613
"\t//\n"
1614
"\t// cmem[i] = { (2^(CWIDTH-2)) * cos(2*pi*i/(2^LGWIDTH)),\n"
1615
"\t//           (2^(CWIDTH-2)) * sin(2*pi*i/(2^LGWIDTH)) };\n"
1616
"\t//\n"
1617
"\treg  [(2*CWIDTH-1):0]        %scmem [0:((1<<LGSPAN)-1)];\n"
1618
"\tinitial\t$readmemh(\"%scmem_%c%d.hex\",%scmem);\n\n",
1619
                (inv)?"i":"", (inv)?"i":"",
1620
                (inv)?"i":"", (odd)?'o':'e',stage<<1,
1621
                (inv)?"i":"");
1622
        {
1623
                FILE    *cmem;
1624
 
1625 14 dgisselq
                {
1626
                        char    *memfile, *ptr;
1627
 
1628
                        memfile = new char[strlen(fname)+128];
1629
                        strcpy(memfile, fname);
1630
                        if ((NULL != (ptr = strrchr(memfile, '/')))&&(ptr>memfile)) {
1631
                                ptr++;
1632
                                sprintf(ptr, "%scmem_%c%d.hex", (inv)?"i":"", (odd)?'o':'e', stage*2);
1633
                        } else {
1634
                                sprintf(memfile, "%s/%scmem_%c%d.hex",
1635
                                        COREDIR, (inv)?"i":"",
1636
                                        (odd)?'o':'e', stage*2);
1637
                        }
1638
                        // strcpy(&memfile[strlen(memfile)-2], ".hex");
1639
                        cmem = fopen(memfile, "w");
1640
                        if (NULL == cmem) {
1641
                                fprintf(stderr, "Could not open/write \'%s\' with FFT coefficients.\n", memfile);
1642
                                perror("Err from O/S:");
1643
                                exit(-2);
1644
                        }
1645
 
1646
                        delete[] memfile;
1647 2 dgisselq
                }
1648
                // fprintf(cmem, "// CBITS = %d, inv = %s\n", cbits, (inv)?"true":"false");
1649
                for(int i=0; i<stage/2; i++) {
1650
                        int k = 2*i+odd;
1651 9 dgisselq
                        double  W = ((inv)?1:-1)*2.0*M_PI*k/(double)(2*stage);
1652 2 dgisselq
                        double  c, s;
1653
                        long long ic, is, vl;
1654
 
1655
                        c = cos(W); s = sin(W);
1656 20 dgisselq
                        ic = (long long)round((1ll<<(cbits-2)) * c);
1657
                        is = (long long)round((1ll<<(cbits-2)) * s);
1658 2 dgisselq
                        vl = (ic & (~(-1ll << (cbits))));
1659
                        vl <<= (cbits);
1660
                        vl |= (is & (~(-1ll << (cbits))));
1661
                        fprintf(cmem, "%0*llx\n", ((cbits*2+3)/4), vl);
1662
                        /*
1663
                        fprintf(cmem, "%0*llx\t\t// %f+j%f -> %llx +j%llx\n",
1664
                                ((cbits*2+3)/4), vl, c, s,
1665
                                ic & (~(-1ll<<(((cbits+3)/4)*4))),
1666
                                is & (~(-1ll<<(((cbits+3)/4)*4))));
1667
                        */
1668
                } fclose(cmem);
1669
        }
1670
 
1671
        fprintf(fstage,
1672 6 dgisselq
"\treg  [(LGWIDTH-2):0]         iaddr;\n"
1673 2 dgisselq
"\treg  [(2*IWIDTH-1):0]        imem    [0:((1<<LGSPAN)-1)];\n"
1674
"\n"
1675 8 dgisselq
"\treg  [LGSPAN:0]              oB;\n"
1676 2 dgisselq
"\treg  [(2*OWIDTH-1):0]        omem    [0:((1<<LGSPAN)-1)];\n"
1677
"\n"
1678
"\talways @(posedge i_clk)\n"
1679
        "\t\tif (i_rst)\n"
1680
        "\t\tbegin\n"
1681
                "\t\t\twait_for_sync <= 1'b1;\n"
1682
                "\t\t\tiaddr <= 0;\n"
1683 8 dgisselq
                "\t\t\tib_sync   <= 1'b0;\n"
1684 2 dgisselq
        "\t\tend\n"
1685
        "\t\telse if ((i_ce)&&((~wait_for_sync)||(i_sync)))\n"
1686
        "\t\tbegin\n"
1687
                "\t\t\t//\n"
1688
                "\t\t\t// First step: Record what we\'re not ready to use yet\n"
1689
                "\t\t\t//\n"
1690
                "\t\t\timem[iaddr[(LGSPAN-1):0]] <= i_data;\n"
1691
                "\t\t\tiaddr <= iaddr + 1;\n"
1692
                "\t\t\twait_for_sync <= 1'b0;\n"
1693 23 dgisselq
        "\t\tend\n\n");
1694
 
1695
        fprintf(fstage,
1696
        "\t//\n"
1697
        "\t// Now, we have all the inputs, so let\'s feed the butterfly\n"
1698
        "\t//\n"
1699
        "\talways\t@(posedge i_clk)\n"
1700
        "\tif ((i_ce)&&(iaddr[LGSPAN]))\n"
1701
                "\t\tbegin\n"
1702
                        "\t\t\t// One input from memory, ...\n"
1703
                        "\t\t\tib_a <= imem[iaddr[(LGSPAN-1):0]];\n"
1704
                        "\t\t\t// One input clocked in from the top\n"
1705
                        "\t\t\tib_b <= i_data;\n"
1706
                        "\t\t\t// Set the sync to true on the very first\n"
1707
                        "\t\t\t// valid input in, and hence on the very\n"
1708
                        "\t\t\t// first valid data out per FFT.\n"
1709
                        "\t\t\tib_sync <= (iaddr==(1<<(LGSPAN)));\n"
1710
                        "\t\t\tib_c <= %scmem[iaddr[(LGSPAN-1):0]];\n"
1711
                "\t\tend\n\n", (inv)?"i":"");
1712
 
1713
        if (hwmpy) {
1714
                fprintf(fstage,
1715
        "\thwbfly #(.IWIDTH(IWIDTH),.CWIDTH(CWIDTH),.OWIDTH(OWIDTH),\n"
1716
                        "\t\t\t.SHIFT(BFLYSHIFT))\n"
1717
                "\t\tbfly(i_clk, i_rst, i_ce, ib_c,\n"
1718
                        "\t\t\tib_a, ib_b, ib_sync, ob_a, ob_b, ob_sync);\n");
1719
        } else {
1720
        fprintf(fstage,
1721
        "\tbutterfly #(.IWIDTH(IWIDTH),.CWIDTH(CWIDTH),.OWIDTH(OWIDTH),\n"
1722
                "\t\t\t.MPYDELAY(%d\'d%d),.LGDELAY(LGBDLY),.SHIFT(BFLYSHIFT))\n"
1723
        "\t\tbfly(i_clk, i_rst, i_ce, ib_c,\n"
1724
                "\t\t\tib_a, ib_b, ib_sync, ob_a, ob_b, ob_sync);\n",
1725
                        lgdelay(nbits, xtra), bflydelay(nbits, xtra));
1726
        }
1727
 
1728
        fprintf(fstage,
1729
        "\t//\n"
1730
        "\t// Next step: recover the outputs from the butterfly\n"
1731
        "\t//\n"
1732
        "\talways\t@(posedge i_clk)\n"
1733
        "\t\tif (i_rst)\n"
1734
        "\t\tbegin\n"
1735
                "\t\t\toB <= 0;\n"
1736
                "\t\t\to_sync <= 0;\n"
1737
                "\t\t\tb_started <= 0;\n"
1738
        "\t\tend else if (i_ce)\n"
1739
        "\t\tbegin\n"
1740
        "\t\t\tif ((ob_sync||b_started)&&(~oB[LGSPAN]))\n"
1741 2 dgisselq
                "\t\t\tbegin // A butterfly output is available\n"
1742
                        "\t\t\t\tb_started <= 1'b1;\n"
1743 8 dgisselq
                        "\t\t\t\tomem[oB[(LGSPAN-1):0]] <= ob_b;\n"
1744 2 dgisselq
                        "\t\t\t\toB <= oB+1;\n"
1745
"\n"
1746 6 dgisselq
                        "\t\t\t\to_sync <= (ob_sync);\n"
1747 2 dgisselq
                        "\t\t\t\to_data <= ob_a;\n"
1748
                "\t\t\tend else if (b_started)\n"
1749
                "\t\t\tbegin // and keep outputting once you start--at a rate\n"
1750
                "\t\t\t// of one guaranteed output per clock that has i_ce set.\n"
1751 8 dgisselq
                        "\t\t\t\to_data <= omem[oB[(LGSPAN-1):0]];\n"
1752 2 dgisselq
                        "\t\t\t\toB <= oB + 1;\n"
1753
                        "\t\t\t\to_sync <= 1'b0;\n"
1754
                "\t\t\tend else\n"
1755
                        "\t\t\t\to_sync <= 1'b0;\n"
1756 23 dgisselq
        "\t\tend\n\n");
1757 22 dgisselq
        fprintf(fstage, "endmodule\n");
1758 2 dgisselq
}
1759
 
1760
void    usage(void) {
1761
        fprintf(stderr,
1762
"USAGE:\tfftgen [-f <size>] [-d dir] [-c cbits] [-n nbits] [-m mxbits] [-s01]\n"
1763
// "\tfftgen -i\n"
1764
"\t-c <cbits>\tCauses all internal complex coefficients to be\n"
1765
"\t\tlonger than the corresponding data bits, to help avoid\n"
1766
"\t\tcoefficient truncation errors.\n"
1767
"\t-d <dir>\tPlaces all of the generated verilog files into <dir>.\n"
1768
"\t-f <size>\tSets the size of the FFT as the number of complex\n"
1769
"\t\tsamples input to the transform.\n"
1770
"\t-n <nbits>\tSets the number of bits in the twos complement input\n"
1771
"\t\tto the FFT routine.\n"
1772
"\t-m <mxbits>\tSets the maximum bit width that the FFT should ever\n"
1773
"\t\tproduce.  Internal values greater than this value will be\n"
1774
"\t\ttruncated to this value.\n"
1775 22 dgisselq
"\t-n <nbits>\tSets the bitwidth for values coming into the (i)FFT.\n"
1776
"\t-p <nmpy>\tSets the number of stages that will use any hardware \n"
1777
"\t\tmultiplication facility, instead of shift-add emulation.\n"
1778 2 dgisselq
"\t-s\tSkip the final bit reversal stage.  This is useful in\n"
1779
"\t\talgorithms that need to apply a filter without needing to do\n"
1780
"\t\tbin shifting, as these algorithms can, with this option, just\n"
1781
"\t\tmultiply by a bit reversed correlation sequence and then\n"
1782 22 dgisselq
"\t\tinverse FFT the (still bit reversed) result.  (You would need\n"
1783
"\t\ta decimation in time inverse to do this, which this program does\n"
1784
"\t\tnot yet provide.)\n"
1785 2 dgisselq
"\t-S\tInclude the final bit reversal stage (default).\n"
1786 22 dgisselq
"\t-x <xtrabits>\tUse this many extra bits internally, before any final\n"
1787
"\t\trounding or truncation of the answer to the final number of bits.\n"
1788 2 dgisselq
"\t-0\tA forward FFT (default), meaning that the coefficients are\n"
1789
"\t\tgiven by e^{-j 2 pi k/N n }.\n"
1790
"\t-1\tAn inverse FFT, meaning that the coefficients are\n"
1791
"\t\tgiven by e^{ j 2 pi k/N n }.\n");
1792
}
1793
 
1794
// Features still needed:
1795
//      Interactivity.
1796
//      Some number of maximum bits, beyond which we won't accumulate any more.
1797
//      Obviously, the build_stage above.
1798
//      Copying the files of interest into the fft-core directory, from
1799
//              whatever directory this file is run out of.
1800
int main(int argc, char **argv) {
1801
        int     fftsize = -1, lgsize = -1;
1802 22 dgisselq
        int     nbitsin = 16, xtracbits = 4, nummpy=0, nonmpy=2;
1803 19 dgisselq
        int     nbitsout, maxbitsout = -1, xtrapbits=0;
1804 2 dgisselq
        bool    bitreverse = true, inverse=false, interactive = false,
1805
                verbose_flag = false;
1806
        FILE    *vmain;
1807 14 dgisselq
        std::string     coredir = "fft-core", cmdline = "";
1808 23 dgisselq
        ROUND_T rounding = RND_CONVERGENT;
1809
        // ROUND_T      rounding = RND_HALFUP;
1810 2 dgisselq
 
1811
        if (argc <= 1)
1812
                usage();
1813
 
1814 14 dgisselq
        cmdline = argv[0];
1815 2 dgisselq
        for(int argn=1; argn<argc; argn++) {
1816 14 dgisselq
                cmdline += " ";
1817
                cmdline += argv[argn];
1818
        }
1819
 
1820
        for(int argn=1; argn<argc; argn++) {
1821 2 dgisselq
                if ('-' == argv[argn][0]) {
1822
                        for(int j=1; (argv[argn][j])&&(j<100); j++) {
1823
                                switch(argv[argn][j]) {
1824
                                        case '0':
1825
                                                inverse = false;
1826
                                                break;
1827
                                        case '1':
1828
                                                inverse = true;
1829
                                                break;
1830
                                        case 'c':
1831
                                                if (argn+1 >= argc) {
1832 19 dgisselq
                                                        printf("ERR: No extra number of coefficient bits given!\n\n");
1833 2 dgisselq
                                                        usage(); exit(-1);
1834
                                                }
1835
                                                xtracbits = atoi(argv[++argn]);
1836
                                                j+= 200;
1837
                                                break;
1838
                                        case 'd':
1839
                                                if (argn+1 >= argc) {
1840 19 dgisselq
                                                        printf("ERR: No directory given into which to place the core!\n\n");
1841 2 dgisselq
                                                        usage(); exit(-1);
1842
                                                }
1843 14 dgisselq
                                                coredir = argv[++argn];
1844 2 dgisselq
                                                j += 200;
1845
                                                break;
1846
                                        case 'f':
1847
                                                if (argn+1 >= argc) {
1848 19 dgisselq
                                                        printf("ERR: No FFT Size given!\n\n");
1849 2 dgisselq
                                                        usage(); exit(-1);
1850
                                                }
1851
                                                fftsize = atoi(argv[++argn]);
1852
                                                { int sln = strlen(argv[argn]);
1853
                                                if (!isdigit(argv[argn][sln-1])){
1854
                                                        switch(argv[argn][sln-1]) {
1855
                                                        case 'k': case 'K':
1856
                                                                fftsize <<= 10;
1857
                                                                break;
1858
                                                        case 'm': case 'M':
1859
                                                                fftsize <<= 20;
1860
                                                                break;
1861
                                                        case 'g': case 'G':
1862
                                                                fftsize <<= 30;
1863
                                                                break;
1864
                                                        default:
1865 19 dgisselq
                                                                printf("ERR: Unknown FFT size, %s!\n", argv[argn]);
1866 2 dgisselq
                                                                exit(-1);
1867
                                                        }
1868
                                                }}
1869
                                                j += 200;
1870
                                                break;
1871
                                        case 'h':
1872
                                                usage();
1873
                                                exit(0);
1874
                                                break;
1875
                                        case 'i':
1876
                                                interactive = true;
1877
                                                break;
1878
                                        case 'm':
1879
                                                if (argn+1 >= argc) {
1880 19 dgisselq
                                                        printf("ERR: No maximum output bit value given!\n\n");
1881 2 dgisselq
                                                        exit(-1);
1882
                                                }
1883
                                                maxbitsout = atoi(argv[++argn]);
1884
                                                j += 200;
1885
                                                break;
1886
                                        case 'n':
1887
                                                if (argn+1 >= argc) {
1888 19 dgisselq
                                                        printf("ERR: No input bit size given!\n\n");
1889 2 dgisselq
                                                        exit(-1);
1890
                                                }
1891
                                                nbitsin = atoi(argv[++argn]);
1892
                                                j += 200;
1893
                                                break;
1894 22 dgisselq
                                        case 'p':
1895
                                                if (argn+1 >= argc) {
1896
                                                        printf("ERR: No number given for number of hardware multiply stages!\n\n");
1897
                                                        exit(-1);
1898
                                                }
1899
                                                nummpy = atoi(argv[++argn]);
1900
                                                j += 200;
1901
                                                break;
1902 2 dgisselq
                                        case 'S':
1903
                                                bitreverse = true;
1904
                                                break;
1905
                                        case 's':
1906
                                                bitreverse = false;
1907
                                                break;
1908 19 dgisselq
                                        case 'x':
1909
                                                if (argn+1 >= argc) {
1910
                                                        printf("ERR: No extra number of bits given!\n\n");
1911
                                                        usage(); exit(-1);
1912
                                                } j+= 200;
1913
                                                xtrapbits = atoi(argv[++argn]);
1914
                                                break;
1915 2 dgisselq
                                        case 'v':
1916
                                                verbose_flag = true;
1917
                                                break;
1918
                                        default:
1919
                                                printf("Unknown argument, -%c\n", argv[argn][j]);
1920
                                                usage();
1921
                                                exit(-1);
1922
                                }
1923
                        }
1924
                } else {
1925
                        printf("Unrecognized argument, %s\n", argv[argn]);
1926
                        usage();
1927
                        exit(-1);
1928
                }
1929
        }
1930
 
1931
        if ((lgsize < 0)&&(fftsize > 1)) {
1932
                for(lgsize=1; (1<<lgsize) < fftsize; lgsize++)
1933
                        ;
1934
        }
1935
 
1936
        if ((fftsize <= 0)||(nbitsin < 1)||(nbitsin>48)) {
1937
                printf("INVALID PARAMETERS!!!!\n");
1938
                exit(-1);
1939
        }
1940
 
1941
 
1942
        if (nextlg(fftsize) != fftsize) {
1943
                fprintf(stderr, "ERR: FFTSize (%d) *must* be a power of two\n",
1944
                                fftsize);
1945
                exit(-1);
1946
        } else if (fftsize < 2) {
1947
                fprintf(stderr, "ERR: Minimum FFTSize is 2, not %d\n",
1948
                                fftsize);
1949
                if (fftsize == 1) {
1950
                        fprintf(stderr, "You do realize that a 1 point FFT makes very little sense\n");
1951
                        fprintf(stderr, "in an FFT operation that handles two samples per clock?\n");
1952
                        fprintf(stderr, "If you really need to do an FFT of this size, the output\n");
1953
                        fprintf(stderr, "can be connected straight to the input.\n");
1954
                } else {
1955
                        fprintf(stderr, "Indeed, a size of %d doesn\'t make much sense to me at all.\n", fftsize);
1956
                        fprintf(stderr, "Is such an operation even defined?\n");
1957
                }
1958
                exit(-1);
1959
        }
1960
 
1961
        // Calculate how many output bits we'll have, and what the log
1962
        // based two size of our FFT is.
1963
        {
1964
                int     tmp_size = fftsize;
1965
 
1966
                // The first stage always accumulates one bit, regardless
1967
                // of whether you need to or not.
1968
                nbitsout = nbitsin + 1;
1969
                tmp_size >>= 1;
1970
 
1971
                while(tmp_size > 4) {
1972
                        nbitsout += 1;
1973
                        tmp_size >>= 2;
1974
                }
1975
 
1976
                if (tmp_size > 1)
1977
                        nbitsout ++;
1978
 
1979
                if (fftsize <= 2)
1980
                        bitreverse = false;
1981
        } if ((maxbitsout > 0)&&(nbitsout > maxbitsout))
1982
                nbitsout = maxbitsout;
1983
 
1984 22 dgisselq
        // Figure out how many multiply stages to use, and how many to skip
1985
        {
1986
                int     lgv = lgval(fftsize);
1987 2 dgisselq
 
1988 22 dgisselq
                nonmpy = lgv - nummpy;
1989
                if (nonmpy < 2) nonmpy = 2;
1990
                nummpy = lgv - nonmpy;
1991
        }
1992
 
1993 2 dgisselq
        {
1994
                struct stat     sbuf;
1995 14 dgisselq
                if (lstat(coredir.c_str(), &sbuf)==0) {
1996 2 dgisselq
                        if (!S_ISDIR(sbuf.st_mode)) {
1997 14 dgisselq
                                fprintf(stderr, "\'%s\' already exists, and is not a directory!\n", coredir.c_str());
1998 2 dgisselq
                                fprintf(stderr, "I will stop now, lest I overwrite something you care about.\n");
1999
                                fprintf(stderr, "To try again, please remove this file.\n");
2000
                                exit(-1);
2001
                        }
2002
                } else
2003 14 dgisselq
                        mkdir(coredir.c_str(), 0755);
2004
                if (access(coredir.c_str(), X_OK|W_OK) != 0) {
2005
                        fprintf(stderr, "I have no access to the directory \'%s\'.\n", coredir.c_str());
2006 2 dgisselq
                        exit(-1);
2007
                }
2008
        }
2009
 
2010 14 dgisselq
        {
2011
                std::string     fname_string;
2012
 
2013
                fname_string = coredir;
2014
                fname_string += "/";
2015
                if (inverse) fname_string += "i";
2016
                fname_string += "fftmain.v";
2017
 
2018
                vmain = fopen(fname_string.c_str(), "w");
2019
                if (NULL == vmain) {
2020
                        fprintf(stderr, "Could not open \'%s\' for writing\n", fname_string.c_str());
2021
                        perror("Err from O/S:");
2022
                        exit(-1);
2023
                }
2024 2 dgisselq
        }
2025
 
2026
        fprintf(vmain, "/////////////////////////////////////////////////////////////////////////////\n");
2027
        fprintf(vmain, "//\n");
2028
        fprintf(vmain, "// Filename:    %sfftmain.v\n", (inverse)?"i":"");
2029
        fprintf(vmain, "//\n");
2030
        fprintf(vmain, "// Project:     %s\n", prjname);
2031
        fprintf(vmain, "//\n");
2032
        fprintf(vmain, "// Purpose:     This is the main module in the Doubletime FPGA FFT project.\n");
2033
        fprintf(vmain, "//              As such, all other modules are subordinate to this one.\n");
2034
        fprintf(vmain, "//              (I have been reading too much legalese this week ...)\n");
2035
        fprintf(vmain, "//              This module accomplish a fixed size Complex FFT on %d data\n", fftsize);
2036
        fprintf(vmain, "//              points.  The FFT is fully pipelined, and accepts as inputs\n");
2037
        fprintf(vmain, "//              two complex two\'s complement samples per clock.\n");
2038
        fprintf(vmain, "//\n");
2039
        fprintf(vmain, "// Parameters:\n");
2040
        fprintf(vmain, "//      i_clk\tThe clock.  All operations are synchronous with this clock.\n");
2041
        fprintf(vmain, "//\ti_rst\tSynchronous reset, active high.  Setting this line will\n");
2042
        fprintf(vmain, "//\t\t\tforce the reset of all of the internals to this routine.\n");
2043
        fprintf(vmain, "//\t\t\tFurther, following a reset, the o_sync line will go\n");
2044
        fprintf(vmain, "//\t\t\thigh the same time the first output sample is valid.\n");
2045
        fprintf(vmain, "//      i_ce\tA clock enable line.  If this line is set, this module\n");
2046
        fprintf(vmain, "//\t\t\twill accept two complex values as inputs, and produce\n");
2047
        fprintf(vmain, "//\t\t\ttwo (possibly empty) complex values as outputs.\n");
2048
        fprintf(vmain, "//\t\ti_left\tThe first of two complex input samples.  This value\n");
2049
        fprintf(vmain, "//\t\t\tis split into two two\'s complement numbers, of \n");
2050
        fprintf(vmain, "//\t\t\t%d bits each, with the real portion in the high\n", nbitsin);
2051
        fprintf(vmain, "//\t\t\torder bits, and the imaginary portion taking the\n");
2052
        fprintf(vmain, "//\t\t\tbottom %d bits.\n", nbitsin);
2053
        fprintf(vmain, "//\t\ti_right\tThis is the same thing as i_left, only this is the\n");
2054
        fprintf(vmain, "//\t\t\tsecond of two such samples.  Hence, i_left would\n");
2055
        fprintf(vmain, "//\t\t\tcontain input sample zero, i_right would contain\n");
2056
        fprintf(vmain, "//\t\t\tsample one.  On the next clock i_left would contain\n");
2057
        fprintf(vmain, "//\t\t\tinput sample two, i_right number three and so forth.\n");
2058
        fprintf(vmain, "//\t\to_left\tThe first of two output samples, of the same\n");
2059
        fprintf(vmain, "//\t\t\tformat as i_left, only having %d bits for each of\n", nbitsout);
2060
        fprintf(vmain, "//\t\t\tthe real and imaginary components, leading to %d\n", nbitsout*2);
2061
        fprintf(vmain, "//\t\t\tbits total.\n");
2062
        fprintf(vmain, "//\t\to_right\tThe second of two output samples produced each clock.\n");
2063
        fprintf(vmain, "//\t\t\tThis has the same format as o_left.\n");
2064
        fprintf(vmain, "//\t\to_sync\tA one bit output indicating the first valid sample\n");
2065
        fprintf(vmain, "//\t\t\tproduced by this FFT following a reset.  Ever after,\n");
2066
        fprintf(vmain, "//\t\t\tthis will indicate the first sample of an FFT frame.\n");
2067
        fprintf(vmain, "//\n");
2068 14 dgisselq
        fprintf(vmain, "// Arguments:\tThis file was computer generated using the\n");
2069
        fprintf(vmain, "//\t\tfollowing command line:\n");
2070
        fprintf(vmain, "//\n");
2071
        fprintf(vmain, "//\t\t%% %s\n", cmdline.c_str());
2072
        fprintf(vmain, "//\n");
2073 2 dgisselq
        fprintf(vmain, "%s", creator);
2074
        fprintf(vmain, "//\n");
2075
        fprintf(vmain, "%s", cpyleft);
2076
 
2077
 
2078
        fprintf(vmain, "//\n");
2079
        fprintf(vmain, "//\n");
2080
        fprintf(vmain, "module %sfftmain(i_clk, i_rst, i_ce,\n", (inverse)?"i":"");
2081
        fprintf(vmain, "\t\ti_left, i_right,\n");
2082
        fprintf(vmain, "\t\to_left, o_right, o_sync);\n");
2083
        fprintf(vmain, "\tparameter\tIWIDTH=%d, OWIDTH=%d, LGWIDTH=%d;\n", nbitsin, nbitsout, lgsize);
2084
        assert(lgsize > 0);
2085
        fprintf(vmain, "\tinput\t\ti_clk, i_rst, i_ce;\n");
2086
        fprintf(vmain, "\tinput\t\t[(2*IWIDTH-1):0]\ti_left, i_right;\n");
2087
        fprintf(vmain, "\toutput\treg\t[(2*OWIDTH-1):0]\to_left, o_right;\n");
2088
        fprintf(vmain, "\toutput\treg\t\t\to_sync;\n");
2089
        fprintf(vmain, "\n\n");
2090
 
2091
        fprintf(vmain, "\t// Outputs of the FFT, ready for bit reversal.\n");
2092
        fprintf(vmain, "\twire\t[(2*OWIDTH-1):0]\tbr_left, br_right;\n");
2093
        fprintf(vmain, "\n\n");
2094
 
2095
        int     tmp_size = fftsize, lgtmp = lgsize;
2096
        if (fftsize == 2) {
2097
                if (bitreverse) {
2098
                        fprintf(vmain, "\treg\tbr_start;\n");
2099
                        fprintf(vmain, "\talways @(posedge i_clk)\n");
2100
                        fprintf(vmain, "\t\tif (i_rst)\n");
2101
                        fprintf(vmain, "\t\t\tbr_start <= 1'b0;\n");
2102
                        fprintf(vmain, "\t\telse if (i_ce)\n");
2103
                        fprintf(vmain, "\t\t\tbr_start <= 1'b1;\n");
2104
                }
2105
                fprintf(vmain, "\n\n");
2106 6 dgisselq
                fprintf(vmain, "\tdblstage\t#(IWIDTH)\tstage_2(i_clk, i_rst, i_ce,\n");
2107
                fprintf(vmain, "\t\t\t(~i_rst), i_left, i_right, br_left, br_right);\n");
2108 2 dgisselq
                fprintf(vmain, "\n\n");
2109
        } else {
2110
                int     nbits = nbitsin, dropbit=0;
2111
                // Always do a first stage
2112
                fprintf(vmain, "\n\n");
2113
                fprintf(vmain, "\twire\t\tw_s%d, w_os%d;\n", fftsize, fftsize);
2114 19 dgisselq
                fprintf(vmain, "\twire\t[%d:0]\tw_e%d, w_o%d;\n", 2*(nbits+1+xtrapbits)-1, fftsize, fftsize);
2115
                fprintf(vmain, "\t%sfftstage_e%d\t#(IWIDTH,IWIDTH+%d,%d,%d,%d,%d,0)\tstage_e%d(i_clk, i_rst, i_ce,\n",
2116 2 dgisselq
                        (inverse)?"i":"", fftsize,
2117 19 dgisselq
                        xtracbits, nbits+1+xtrapbits,
2118 2 dgisselq
                        lgsize, lgtmp-2, lgdelay(nbits,xtracbits),
2119
                        fftsize);
2120
                fprintf(vmain, "\t\t\t(~i_rst), i_left, w_e%d, w_s%d);\n", fftsize, fftsize);
2121 19 dgisselq
                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",
2122 2 dgisselq
                        (inverse)?"i":"", fftsize,
2123 19 dgisselq
                        xtracbits, nbits+1+xtrapbits,
2124 2 dgisselq
                        lgsize, lgtmp-2, lgdelay(nbits,xtracbits),
2125
                        fftsize);
2126 9 dgisselq
                fprintf(vmain, "\t\t\t(~i_rst), i_right, w_o%d, w_os%d);\n", fftsize, fftsize);
2127 2 dgisselq
                fprintf(vmain, "\n\n");
2128
 
2129 14 dgisselq
                {
2130
                        std::string     fname;
2131
                        char    numstr[12];
2132 22 dgisselq
                        bool    mpystage;
2133 2 dgisselq
 
2134 22 dgisselq
                        // Last two stages are always non-multiply stages
2135
                        // since the multiplies can be done by adds
2136
                        mpystage = ((lgtmp-2) <= nummpy);
2137
 
2138 14 dgisselq
                        fname = coredir + "/";
2139
                        if (inverse) fname += "i";
2140
                        fname += "fftstage_e";
2141
                        sprintf(numstr, "%d", fftsize);
2142
                        fname += numstr;
2143
                        fname += ".v";
2144 22 dgisselq
                        build_stage(fname.c_str(), fftsize/2, 0, nbits, inverse, xtracbits, mpystage);   // Even stage
2145 14 dgisselq
 
2146
                        fname = coredir + "/";
2147
                        if (inverse) fname += "i";
2148
                        fname += "fftstage_o";
2149
                        sprintf(numstr, "%d", fftsize);
2150
                        fname += numstr;
2151
                        fname += ".v";
2152 22 dgisselq
                        build_stage(fname.c_str(), fftsize/2, 1, nbits, inverse, xtracbits, mpystage);  // Odd  stage
2153 14 dgisselq
                }
2154
 
2155 2 dgisselq
                nbits += 1;     // New number of input bits
2156
                tmp_size >>= 1; lgtmp--;
2157
                dropbit = 0;
2158
                fprintf(vmain, "\n\n");
2159
                while(tmp_size >= 8) {
2160
                        int     obits = nbits+((dropbit)?0:1);
2161
 
2162
                        if ((maxbitsout > 0)&&(obits > maxbitsout))
2163
                                obits = maxbitsout;
2164
 
2165
                        fprintf(vmain, "\twire\t\tw_s%d, w_os%d;\n", tmp_size, tmp_size);
2166 19 dgisselq
                        fprintf(vmain, "\twire\t[%d:0]\tw_e%d, w_o%d;\n", 2*(obits+xtrapbits)-1, tmp_size, tmp_size);
2167 2 dgisselq
                        fprintf(vmain, "\t%sfftstage_e%d\t#(%d,%d,%d,%d,%d,%d,%d)\tstage_e%d(i_clk, i_rst, i_ce,\n",
2168
                                (inverse)?"i":"", tmp_size,
2169 19 dgisselq
                                nbits+xtrapbits, nbits+xtracbits+xtrapbits, obits+xtrapbits,
2170
                                lgsize, lgtmp-2, lgdelay(nbits+xtrapbits,xtracbits), (dropbit)?0:0,
2171 2 dgisselq
                                tmp_size);
2172
                        fprintf(vmain, "\t\t\t\t\t\tw_s%d, w_e%d, w_e%d, w_s%d);\n", tmp_size<<1, tmp_size<<1, tmp_size, tmp_size);
2173
                        fprintf(vmain, "\t%sfftstage_o%d\t#(%d,%d,%d,%d,%d,%d,%d)\tstage_o%d(i_clk, i_rst, i_ce,\n",
2174
                                (inverse)?"i":"", tmp_size,
2175 19 dgisselq
                                nbits+xtrapbits, nbits+xtracbits+xtrapbits, obits+xtrapbits,
2176
                                lgsize, lgtmp-2, lgdelay(nbits+xtrapbits,xtracbits), (dropbit)?0:0,
2177 2 dgisselq
                                tmp_size);
2178
                        fprintf(vmain, "\t\t\t\t\t\tw_s%d, w_o%d, w_o%d, w_os%d);\n", tmp_size<<1, tmp_size<<1, tmp_size, tmp_size);
2179
                        fprintf(vmain, "\n\n");
2180
 
2181 14 dgisselq
                        {
2182
                                std::string     fname;
2183
                                char            numstr[12];
2184 22 dgisselq
                                bool            mpystage;
2185 2 dgisselq
 
2186 22 dgisselq
                                mpystage = ((lgtmp-2) <= nummpy);
2187
 
2188 14 dgisselq
                                fname = coredir + "/";
2189
                                if (inverse) fname += "i";
2190
                                fname += "fftstage_e";
2191
                                sprintf(numstr, "%d", tmp_size);
2192
                                fname += numstr;
2193
                                fname += ".v";
2194 22 dgisselq
                                build_stage(fname.c_str(), tmp_size/2, 0,
2195
                                        nbits+xtrapbits, inverse, xtracbits,
2196
                                        mpystage);      // Even stage
2197 2 dgisselq
 
2198 14 dgisselq
                                fname = coredir + "/";
2199
                                if (inverse) fname += "i";
2200
                                fname += "fftstage_o";
2201
                                sprintf(numstr, "%d", tmp_size);
2202
                                fname += numstr;
2203
                                fname += ".v";
2204 22 dgisselq
                                build_stage(fname.c_str(), tmp_size/2, 1,
2205
                                        nbits+xtrapbits, inverse, xtracbits,
2206
                                        mpystage);      // Odd  stage
2207 14 dgisselq
                        }
2208
 
2209
 
2210 2 dgisselq
                        dropbit ^= 1;
2211
                        nbits = obits;
2212
                        tmp_size >>= 1; lgtmp--;
2213
                }
2214
 
2215
                if (tmp_size == 4) {
2216
                        int     obits = nbits+((dropbit)?0:1);
2217
 
2218
                        if ((maxbitsout > 0)&&(obits > maxbitsout))
2219
                                obits = maxbitsout;
2220
 
2221
                        fprintf(vmain, "\twire\t\tw_s4, w_os4;\n");
2222 19 dgisselq
                        fprintf(vmain, "\twire\t[%d:0]\tw_e4, w_o4;\n", 2*(obits+xtrapbits)-1);
2223 2 dgisselq
                        fprintf(vmain, "\tqtrstage\t#(%d,%d,%d,0,%d,%d)\tstage_e4(i_clk, i_rst, i_ce,\n",
2224 19 dgisselq
                                nbits+xtrapbits, obits+xtrapbits, lgsize, (inverse)?1:0, (dropbit)?0:0);
2225 6 dgisselq
                        fprintf(vmain, "\t\t\t\t\t\tw_s8, w_e8, w_e4, w_s4);\n");
2226 2 dgisselq
                        fprintf(vmain, "\tqtrstage\t#(%d,%d,%d,1,%d,%d)\tstage_o4(i_clk, i_rst, i_ce,\n",
2227 19 dgisselq
                                nbits+xtrapbits, obits+xtrapbits, lgsize, (inverse)?1:0, (dropbit)?0:0);
2228 6 dgisselq
                        fprintf(vmain, "\t\t\t\t\t\tw_s8, w_o8, w_o4, w_os4);\n");
2229 2 dgisselq
                        dropbit ^= 1;
2230
                        nbits = obits;
2231
                        tmp_size >>= 1; lgtmp--;
2232
                }
2233
 
2234
                {
2235
                        int obits = nbits+((dropbit)?0:1);
2236
                        if (obits > nbitsout)
2237
                                obits = nbitsout;
2238
                        if ((maxbitsout>0)&&(obits > maxbitsout))
2239
                                obits = maxbitsout;
2240
                        fprintf(vmain, "\twire\t\tw_s2;\n");
2241
                        fprintf(vmain, "\twire\t[%d:0]\tw_e2, w_o2;\n", 2*obits-1);
2242 19 dgisselq
                        fprintf(vmain, "\tdblstage\t#(%d,%d,%d)\tstage_2(i_clk, i_rst, i_ce,\n", nbits+xtrapbits, obits,(dropbit)?0:1);
2243 6 dgisselq
                        fprintf(vmain, "\t\t\t\t\tw_s4, w_e4, w_o4, w_e2, w_o2, w_s2);\n");
2244 2 dgisselq
 
2245
                        fprintf(vmain, "\n\n");
2246
                        nbits = obits;
2247
                }
2248
 
2249
                fprintf(vmain, "\t// Prepare for a (potential) bit-reverse stage.\n");
2250
                fprintf(vmain, "\tassign\tbr_left  = w_e2;\n");
2251
                fprintf(vmain, "\tassign\tbr_right = w_o2;\n");
2252
                fprintf(vmain, "\n");
2253
                if (bitreverse) {
2254
                        fprintf(vmain, "\twire\tbr_start;\n");
2255
                        fprintf(vmain, "\treg\tr_br_started;\n");
2256
                        fprintf(vmain, "\talways @(posedge i_clk)\n");
2257
                        fprintf(vmain, "\t\tif (i_rst)\n");
2258
                        fprintf(vmain, "\t\t\tr_br_started <= 1'b0;\n");
2259
                        fprintf(vmain, "\t\telse\n");
2260 23 dgisselq
                        fprintf(vmain, "\t\t\tr_br_started <= r_br_started || w_s2;\n");
2261
                        fprintf(vmain, "\tassign\tbr_start = r_br_started || w_s2;\n");
2262 2 dgisselq
                }
2263
        }
2264
 
2265
        fprintf(vmain, "\n");
2266
        fprintf(vmain, "\t// Now for the bit-reversal stage.\n");
2267
        fprintf(vmain, "\twire\tbr_sync;\n");
2268
        fprintf(vmain, "\twire\t[(2*OWIDTH-1):0]\tbr_o_left, br_o_right;\n");
2269
        if (bitreverse) {
2270
                fprintf(vmain, "\tdblreverse\t#(%d,%d)\trevstage(i_clk, i_rst,\n", lgsize, nbitsout);
2271
                fprintf(vmain, "\t\t\t(i_ce & br_start), br_left, br_right,\n");
2272
                fprintf(vmain, "\t\t\tbr_o_left, br_o_right, br_sync);\n");
2273
        } else {
2274
                fprintf(vmain, "\tassign\tbr_o_left  = br_left;\n");
2275
                fprintf(vmain, "\tassign\tbr_o_right = br_right;\n");
2276
                fprintf(vmain, "\tassign\tbr_sync    = w_s2;\n");
2277
        }
2278
 
2279
        fprintf(vmain, "\n\n");
2280
        fprintf(vmain, "\t// Last clock: Register our outputs, we\'re done.\n");
2281
        fprintf(vmain, "\talways @(posedge i_clk)\n");
2282
        fprintf(vmain, "\t\tbegin\n");
2283
        fprintf(vmain, "\t\t\to_left  <= br_o_left;\n");
2284
        fprintf(vmain, "\t\t\to_right <= br_o_right;\n");
2285
        fprintf(vmain, "\t\t\to_sync  <= br_sync;\n");
2286
        fprintf(vmain, "\t\tend\n");
2287
        fprintf(vmain, "\n\n");
2288
        fprintf(vmain, "endmodule\n");
2289
        fclose(vmain);
2290
 
2291 14 dgisselq
        {
2292
                std::string     fname;
2293 2 dgisselq
 
2294 14 dgisselq
                fname = coredir + "/butterfly.v";
2295 23 dgisselq
                build_butterfly(fname.c_str(), xtracbits, rounding);
2296 2 dgisselq
 
2297 22 dgisselq
                if (nummpy > 0) {
2298
                        fname = coredir + "/hwbfly.v";
2299 23 dgisselq
                        build_hwbfly(fname.c_str(), xtracbits, rounding);
2300 22 dgisselq
                }
2301
 
2302 14 dgisselq
                fname = coredir + "/shiftaddmpy.v";
2303
                build_multiply(fname.c_str());
2304 2 dgisselq
 
2305 14 dgisselq
                fname = coredir + "/qtrstage.v";
2306 23 dgisselq
                build_quarters(fname.c_str(), rounding);
2307 2 dgisselq
 
2308 14 dgisselq
                fname = coredir + "/dblstage.v";
2309 23 dgisselq
                build_dblstage(fname.c_str(), rounding);
2310 14 dgisselq
 
2311
                if (bitreverse) {
2312
                        fname = coredir + "/dblreverse.v";
2313
                        build_dblreverse(fname.c_str());
2314
                }
2315 23 dgisselq
 
2316
                const   char    *rnd_string = "";
2317
                switch(rounding) {
2318
                        case RND_TRUNCATE:      rnd_string = "/truncate.v"; break;
2319
                        case RND_FROMZERO:      rnd_string = "/roundfromzero.v"; break;
2320
                        case RND_HALFUP:        rnd_string = "/roundhalfup.v"; break;
2321
                        default:
2322
                                rnd_string = "/convround.v"; break;
2323
                } fname = coredir + rnd_string;
2324
                switch(rounding) {
2325
                        case RND_TRUNCATE: build_truncator(fname.c_str()); break;
2326
                        case RND_FROMZERO: build_roundfromzero(fname.c_str()); break;
2327
                        case RND_HALFUP: build_roundhalfup(fname.c_str()); break;
2328
                        default:
2329
                                build_convround(fname.c_str()); break;
2330
                }
2331
 
2332 2 dgisselq
        }
2333
}
2334
 
2335 16 dgisselq
 

powered by: WebSVN 2.1.0

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