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

Subversion Repositories dblclockfft

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

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

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

powered by: WebSVN 2.1.0

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