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

Subversion Repositories dblclockfft

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

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

Line No. Rev Author Line
1 16 dgisselq
/////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    fftgen.v
4
//
5
// Project:     A Doubletime Pipelined FFT
6
//
7
// Purpose:     This is the core generator for the project.  Every part
8
//              and piece of this project begins and ends in this program.
9
//              Once built, this program will build an FFT (or IFFT) core
10
//              of arbitrary width, precision, etc., that will run at
11
//              two samples per clock.  (Incidentally, I didn't pick two
12
//              samples per clock because it was easier, but rather because
13
//              there weren't any two-sample per clock FFT's posted on 
14
//              opencores.com.  Further, FFT's running at one sample per
15
//              clock aren't that hard to find.)
16
//
17
//              You can find the documentation for this program in two places.
18
//              One is in the usage() function below.  The second is in the
19
//              'doc'uments directory that comes with this package, 
20
//              specifically in the spec.pdf file.  If it's not there, type
21
//              make in the documents directory to build it.
22
//
23
// Creator:     Dan Gisselquist, Ph.D.
24
//              Gisselquist Tecnology, LLC
25
//
26
///////////////////////////////////////////////////////////////////////////
27
//
28
// Copyright (C) 2015, Gisselquist Technology, LLC
29
//
30
// This program is free software (firmware): you can redistribute it and/or
31
// modify it under the terms of  the GNU General Public License as published
32
// by the Free Software Foundation, either version 3 of the License, or (at
33
// your option) any later version.
34
//
35
// This program is distributed in the hope that it will be useful, but WITHOUT
36
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
37
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
38
// for more details.
39
//
40
// You should have received a copy of the GNU General Public License along
41
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
42
// target there if the PDF file isn't present.)  If not, see
43
// <http://www.gnu.org/licenses/> for a copy.
44
//
45
// License:     GPL, v3, as defined and found on www.gnu.org,
46
//              http://www.gnu.org/licenses/gpl.html
47
//
48
//
49
///////////////////////////////////////////////////////////////////////////
50
//
51
//
52 2 dgisselq
#include <stdio.h>
53
#include <stdlib.h>
54
#include <unistd.h>
55
#include <sys/stat.h>
56
#include <string.h>
57 14 dgisselq
#include <string>
58 2 dgisselq
#include <math.h>
59
#include <ctype.h>
60
#include <assert.h>
61
 
62
#define COREDIR "fft-core"
63
 
64
const char      cpyleft[] =
65
"///////////////////////////////////////////////////////////////////////////\n"
66
"//\n"
67
"// Copyright (C) 2015, Gisselquist Technology, LLC\n"
68
"//\n"
69
"// This program is free software (firmware): you can redistribute it and/or\n"
70
"// modify it under the terms of  the GNU General Public License as published\n"
71
"// by the Free Software Foundation, either version 3 of the License, or (at\n"
72
"// your option) any later version.\n"
73
"//\n"
74
"// This program is distributed in the hope that it will be useful, but WITHOUT\n"
75
"// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or\n"
76
"// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\n"
77
"// for more details.\n"
78
"//\n"
79
"// You should have received a copy of the GNU General Public License along\n"
80 5 dgisselq
"// with this program.  (It's in the $(ROOT)/doc directory, run make with no\n"
81
"// target there if the PDF file isn\'t present.)  If not, see\n"
82
"// <http://www.gnu.org/licenses/> for a copy.\n"
83
"//\n"
84 2 dgisselq
"// License:    GPL, v3, as defined and found on www.gnu.org,\n"
85
"//             http://www.gnu.org/licenses/gpl.html\n"
86
"//\n"
87
"//\n"
88
"///////////////////////////////////////////////////////////////////////////\n";
89 14 dgisselq
const char      prjname[] = "A Doubletime Pipelined FFT";
90 2 dgisselq
const char      creator[] =     "// Creator:    Dan Gisselquist, Ph.D.\n"
91
                                "//             Gisselquist Tecnology, LLC\n";
92
 
93
int     lgval(int vl) {
94
        int     lg;
95
 
96
        for(lg=1; (1<<lg) < vl; lg++)
97
                ;
98
        return lg;
99
}
100
 
101
int     nextlg(int vl) {
102
        int     r;
103
 
104
        for(r=1; r<vl; r<<=1)
105
                ;
106
        return r;
107
}
108
 
109 14 dgisselq
int     bflydelay(int nbits, int xtra) {
110 2 dgisselq
        int     cbits = nbits + xtra;
111 14 dgisselq
        int     delay;
112 2 dgisselq
        if (nbits+1<cbits)
113 5 dgisselq
                delay = nbits+4;
114 2 dgisselq
        else
115 5 dgisselq
                delay = cbits+3;
116 14 dgisselq
        return delay;
117 2 dgisselq
}
118
 
119 14 dgisselq
int     lgdelay(int nbits, int xtra) {
120
        // The butterfly code needs to compare a valid address, of this
121
        // many bits, with an address two greater.  This guarantees we
122
        // have enough bits for that comparison.  We'll also end up with
123
        // more storage space to look for these values, but without a 
124
        // redesign that's just what we'll deal with.
125
        return lgval(bflydelay(nbits, xtra)+3);
126
}
127
 
128 2 dgisselq
void    build_quarters(const char *fname) {
129
        FILE    *fp = fopen(fname, "w");
130
        if (NULL == fp) {
131
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
132
                perror("O/S Err was:");
133
                return;
134
        }
135
 
136
        fprintf(fp,
137
"///////////////////////////////////////////////////////////////////////////\n"
138
"//\n"
139
"// Filename:   qtrstage.v\n"
140
"//             \n"
141
"// Project:    %s\n"
142
"//\n"
143 5 dgisselq
"// Purpose:    This file encapsulates the 4 point stage of a decimation in\n"
144
"//             frequency FFT.  This particular implementation is optimized\n"
145
"//             so that all of the multiplies are accomplished by additions\n"
146
"//             and multiplexers only.\n"
147
"//\n"
148 2 dgisselq
"//\n%s"
149
"//\n",
150
                prjname, creator);
151
        fprintf(fp, "%s", cpyleft);
152
 
153
        fprintf(fp,
154
"module\tqtrstage(i_clk, i_rst, i_ce, i_sync, i_data, o_data, o_sync);\n"
155 5 dgisselq
        "\tparameter    IWIDTH=16, OWIDTH=IWIDTH+1;\n"
156
        "\t// Parameters specific to the core that should be changed when this\n"
157
        "\t// core is built ... Note that the minimum LGSPAN is 2.  Smaller \n"
158
        "\t// spans must use the fftdoubles stage.\n"
159 15 dgisselq
        "\tparameter\tLGWIDTH=8, ODD=0, INVERSE=0,SHIFT=0,ROUND=1;\n"
160 5 dgisselq
        "\tinput\t                              i_clk, i_rst, i_ce, i_sync;\n"
161
        "\tinput\t      [(2*IWIDTH-1):0]        i_data;\n"
162
        "\toutput\treg  [(2*OWIDTH-1):0]        o_data;\n"
163
        "\toutput\treg                          o_sync;\n"
164 14 dgisselq
        "\t\n");
165
        fprintf(fp,
166 5 dgisselq
        "\treg\t        wait_for_sync;\n"
167
        "\treg\t[2:0]   pipeline;\n"
168 2 dgisselq
"\n"
169 5 dgisselq
        "\treg\t[(IWIDTH):0]    sum_r, sum_i, diff_r, diff_i;\n"
170 14 dgisselq
        "\twire\t[(IWIDTH):0]   n_diff_r, n_diff_i;\n"
171
        "\tassign n_diff_r = -diff_r;\n"
172 5 dgisselq
        "\tassign n_diff_i = -diff_i;\n"
173 2 dgisselq
"\n"
174 5 dgisselq
        "\treg\t[(2*OWIDTH-1):0]        ob_a;\n"
175
        "\twire\t[(2*OWIDTH-1):0]       ob_b;\n"
176
        "\treg\t[(OWIDTH-1):0]          ob_b_r, ob_b_i;\n"
177
        "\tassign       ob_b = { ob_b_r, ob_b_i };\n"
178 2 dgisselq
"\n"
179 5 dgisselq
        "\treg\t[(LGWIDTH-1):0]         iaddr;\n"
180
        "\treg\t[(2*IWIDTH-1):0]        imem;\n"
181 2 dgisselq
"\n"
182 5 dgisselq
        "\twire\tsigned\t[(IWIDTH-1):0]\timem_r, imem_i;\n"
183
        "\tassign\timem_r = imem[(2*IWIDTH-1):(IWIDTH)];\n"
184
        "\tassign\timem_i = imem[(IWIDTH-1):0];\n"
185 2 dgisselq
"\n"
186 5 dgisselq
        "\twire\tsigned\t[(IWIDTH-1):0]\ti_data_r, i_data_i;\n"
187
        "\tassign\ti_data_r = i_data[(2*IWIDTH-1):(IWIDTH)];\n"
188
        "\tassign\ti_data_i = i_data[(IWIDTH-1):0];\n"
189 2 dgisselq
"\n"
190 5 dgisselq
        "\treg  [(2*OWIDTH-1):0]        omem;\n"
191 14 dgisselq
"\n");
192
        fprintf(fp,
193 5 dgisselq
        "\twire [(IWIDTH-1):0]  rnd;\n"
194 9 dgisselq
        "\tgenerate\n"
195
        "\tif ((ROUND)&&((IWIDTH+1-OWIDTH-SHIFT)>0))\n"
196
                "\t\tassign rnd = { {(IWIDTH-1){1'b0}}, 1'b1 };\n"
197
        "\telse\n"
198
                "\t\tassign rnd = { {(IWIDTH){1'b0}}};\n"
199
        "\tendgenerate\n"
200 2 dgisselq
"\n"
201 5 dgisselq
        "\talways @(posedge i_clk)\n"
202
                "\t\tif (i_rst)\n"
203
                "\t\tbegin\n"
204
                        "\t\t\twait_for_sync <= 1'b1;\n"
205
                        "\t\t\tiaddr <= 0;\n"
206
                        "\t\t\tpipeline <= 3'b000;\n"
207
                "\t\tend\n"
208
                "\t\telse if ((i_ce)&&((~wait_for_sync)||(i_sync)))\n"
209
                "\t\tbegin\n"
210
                        "\t\t\t// Always\n"
211
                        "\t\t\timem <= i_data;\n"
212
                        "\t\t\tiaddr <= iaddr + 1;\n"
213
                        "\t\t\twait_for_sync <= 1'b0;\n"
214 2 dgisselq
"\n"
215 5 dgisselq
                        "\t\t\t// In sequence, clock = 0\n"
216
                        "\t\t\tif (iaddr[0])\n"
217
                        "\t\t\tbegin\n"
218
                                "\t\t\t\tsum_r  <= imem_r + i_data_r + rnd;\n"
219
                                "\t\t\t\tsum_i  <= imem_i + i_data_i + rnd;\n"
220
                                "\t\t\t\tdiff_r <= imem_r - i_data_r + rnd;\n"
221
                                "\t\t\t\tdiff_i <= imem_i - i_data_i + rnd;\n"
222 2 dgisselq
"\n"
223 5 dgisselq
                        "\t\t\t\tpipeline[2:0] <= { pipeline[1:0], 1'b1 };\n"
224
                        "\t\t\tend else\n"
225
                        "\t\t\t\tpipeline[2:0] <= { pipeline[1:0], 1'b0 };\n"
226 2 dgisselq
"\n"
227 5 dgisselq
                        "\t\t\t// In sequence, clock = 1\n"
228
                        "\t\t\tif (pipeline[1])\n"
229
                        "\t\t\tbegin\n"
230 9 dgisselq
"\t\t\t\tob_a <= { sum_r[(IWIDTH-SHIFT):(IWIDTH+1-OWIDTH-SHIFT)],\n"
231
        "\t\t\t\t\t\tsum_i[(IWIDTH-SHIFT):(IWIDTH+1-OWIDTH-SHIFT)] };\n"
232 5 dgisselq
                                "\t\t\t\t// on Even, W = e^{-j2pi 1/4 0} = 1\n"
233 9 dgisselq
                                "\t\t\t\tif (ODD == 0)\n"
234 5 dgisselq
                                "\t\t\t\tbegin\n"
235 2 dgisselq
"\t\t\t\t\tob_b_r <= diff_r[(IWIDTH-SHIFT):(IWIDTH+1-OWIDTH-SHIFT)];\n"
236
"\t\t\t\t\tob_b_i <= diff_i[(IWIDTH-SHIFT):(IWIDTH+1-OWIDTH-SHIFT)];\n"
237 9 dgisselq
// "\t\t\t\t\tob_b_r <=   { (OWIDTH) {1'b0} };\n"
238
// "\t\t\t\t\tob_b_i <=   { (OWIDTH) {1'b0} };\n"
239 14 dgisselq
                                "\t\t\t\tend else if (INVERSE==0) begin\n"
240 2 dgisselq
"\t\t\t\t\t// on Odd, W = e^{-j2pi 1/4} = -j\n"
241
"\t\t\t\t\tob_b_r <=   diff_i[(IWIDTH-SHIFT):(IWIDTH+1-OWIDTH-SHIFT)];\n"
242 14 dgisselq
"\t\t\t\t\tob_b_i <= n_diff_r[(IWIDTH-SHIFT):(IWIDTH+1-OWIDTH-SHIFT)];\n"
243 9 dgisselq
// "\t\t\t\t\tob_b_r <=   { (OWIDTH) {1'b0} };\n"
244
// "\t\t\t\t\tob_b_i <=   { (OWIDTH) {1'b0} };\n"
245 5 dgisselq
                                "\t\t\t\tend else begin\n"
246 2 dgisselq
"\t\t\t\t\t// on Odd, W = e^{j2pi 1/4} = j\n"
247
"\t\t\t\t\tob_b_r <= n_diff_i[(IWIDTH-SHIFT):(IWIDTH+1-OWIDTH-SHIFT)];\n"
248
"\t\t\t\t\tob_b_i <=   diff_r[(IWIDTH-SHIFT):(IWIDTH+1-OWIDTH-SHIFT)];\n"
249 9 dgisselq
// "\t\t\t\t\tob_b_r <=   { (OWIDTH) {1'b0} };\n"
250
// "\t\t\t\t\tob_b_i <=   { (OWIDTH) {1'b0} };\n"
251
 
252 5 dgisselq
                                "\t\t\t\tend\n"
253
                                "\t\t\t\t// (wire) ob_b <= { ob_b_r, ob_b_i };\n"
254
                        "\t\t\tend\n"
255
                        "\t\t\t// In sequence, clock = 2\n"
256
                        "\t\t\tif (pipeline[2])\n"
257
                        "\t\t\tbegin\n"
258
                                "\t\t\t\tomem <= ob_b;\n"
259
                                "\t\t\t\to_data <= ob_a;\n"
260
                        "\t\t\tend else\n"
261
                                "\t\t\t\to_data <= omem;\n"
262 6 dgisselq
                        "\t\t\t// Don\'t forget in the sync check that we are running\n"
263
                        "\t\t\t// at two clocks per sample.  Thus we need to\n"
264
                        "\t\t\t// produce a sync every 2^(LGWIDTH-1) clocks.\n"
265
                        "\t\t\to_sync <= &(~iaddr[(LGWIDTH-2):3]) && (iaddr[2:0] == 3'b100);\n"
266 5 dgisselq
                "\t\tend\n"
267 2 dgisselq
"endmodule\n");
268
}
269
 
270
void    build_dblstage(const char *fname) {
271
        FILE    *fp = fopen(fname, "w");
272
        if (NULL == fp) {
273
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
274
                perror("O/S Err was:");
275
                return;
276
        }
277
 
278
        fprintf(fp,
279
"///////////////////////////////////////////////////////////////////////////\n"
280
"//\n"
281
"// Filename:   dblstage.v\n"
282
"//\n"
283
"// Project:    %s\n"
284
"//\n"
285
"// Purpose:    This is part of an FPGA implementation that will process\n"
286 5 dgisselq
"//             the final stage of a decimate-in-frequency FFT, running\n"
287
"//             through the data at two samples per clock.  If you notice\n"
288
"//             from the derivation of an FFT, the only time both even and\n"
289
"//             odd samples are used at the same time is in this stage.\n"
290
"//             Therefore, other than this stage and these twiddles, all of\n"
291
"//             the other stages can run two stages at a time at one sample\n"
292
"//             per clock.\n"
293 2 dgisselq
"//\n"
294
"//             In this implementation, the output is valid one clock after\n"
295
"//             the input is valid.  The output also accumulates one bit\n"
296
"//             above and beyond the number of bits in the input.\n"
297
"//             \n"
298
"//             i_clk   A system clock\n"
299 6 dgisselq
"//             i_rst   A synchronous reset\n"
300 2 dgisselq
"//             i_ce    Circuit enable--nothing happens unless this line is high\n"
301 6 dgisselq
"//             i_sync  A synchronization signal, high once per FFT at the start\n"
302 2 dgisselq
"//             i_left  The first (even) complex sample input.  The higher order\n"
303
"//                     bits contain the real portion, low order bits the\n"
304
"//                     imaginary portion, all in two\'s complement.\n"
305
"//             i_right The next (odd) complex sample input, same format as\n"
306
"//                     i_left.\n"
307
"//             o_left  The first (even) complex output.\n"
308
"//             o_right The next (odd) complex output.\n"
309 6 dgisselq
"//             o_sync  Output synchronization signal.\n"
310 2 dgisselq
"//\n%s"
311
"//\n", prjname, creator);
312
 
313
        fprintf(fp, "%s", cpyleft);
314
        fprintf(fp,
315 9 dgisselq
"module\tdblstage(i_clk, i_rst, i_ce, i_sync, i_left, i_right, o_left, o_right, o_sync);\n"
316 15 dgisselq
        "\tparameter\tIWIDTH=16,OWIDTH=IWIDTH+1, SHIFT=0;\n" //  ROUND=1
317 6 dgisselq
        "\tinput\t\ti_clk, i_rst, i_ce, i_sync;\n"
318 5 dgisselq
        "\tinput\t\t[(2*IWIDTH-1):0]\ti_left, i_right;\n"
319 6 dgisselq
        "\toutput\twire\t[(2*OWIDTH-1):0]\to_left, o_right;\n"
320
        "\toutput\treg\t\t\to_sync;\n"
321 2 dgisselq
"\n"
322 5 dgisselq
        "\twire\tsigned\t[(IWIDTH-1):0]\ti_in_0r, i_in_0i, i_in_1r, i_in_1i;\n"
323
        "\tassign\ti_in_0r = i_left[(2*IWIDTH-1):(IWIDTH)]; \n"
324
        "\tassign\ti_in_0i = i_left[(IWIDTH-1):0]; \n"
325
        "\tassign\ti_in_1r = i_right[(2*IWIDTH-1):(IWIDTH)]; \n"
326
        "\tassign\ti_in_1i = i_right[(IWIDTH-1):0]; \n"
327
        "\twire\t[(OWIDTH-1):0]\t\to_out_0r, o_out_0i,\n"
328
                                "\t\t\t\t\to_out_1r, o_out_1i;\n"
329 2 dgisselq
"\n"
330 15 dgisselq
"\n"
331
//      "\t// Handle a potential rounding situation, when IWIDTH=OWIDTH.\n"
332
//"\n"
333
//      "\twire\tsigned\t[(IWIDTH-1):0]\trnd;\n"
334
//"\n"
335
//      "\tgenerate\n"
336
//      "\tif ((ROUND==1)&&(IWIDTH==OWIDTH))\n"
337
//              "\t\tassign rnd = { {(IWIDTH-1){1'b0}}, 1'b1 };\n"
338
//      "\telse\n"
339
//              "\t\tassign rnd = { {(IWIDTH){1'b0}} };\n"
340
//      "\tendgenerate\n"
341
"\n"
342 5 dgisselq
        "\t// Don't forget that we accumulate a bit by adding two values\n"
343
        "\t// together. Therefore our intermediate value must have one more\n"
344
        "\t// bit than the two originals.\n"
345
        "\treg\t[IWIDTH:0]\tout_0r, out_0i, out_1r, out_1i;\n"
346 2 dgisselq
"\n"
347 6 dgisselq
        "\treg\twait_for_sync;\n"
348
"\n"
349 5 dgisselq
        "\talways @(posedge i_clk)\n"
350 6 dgisselq
                "\t\tif (i_rst)\n"
351
                        "\t\t\twait_for_sync <= 1'b1;\n"
352
                "\t\telse if ((i_ce)&&((~wait_for_sync)||(i_sync)))\n"
353 5 dgisselq
                "\t\tbegin\n"
354 6 dgisselq
                        "\t\t\twait_for_sync <= 1'b0;\n"
355
                        "\t\t\t//\n"
356 15 dgisselq
                        "\t\t\tout_0r <= i_in_0r + i_in_1r;\n"  // + rnd
357
                        "\t\t\tout_0i <= i_in_0i + i_in_1i;\n"  // + rnd
358 5 dgisselq
                        "\t\t\t//\n"
359 15 dgisselq
                        "\t\t\tout_1r <= i_in_0r - i_in_1r;\n"  // + rnd
360
                        "\t\t\tout_1i <= i_in_0i - i_in_1i;\n"  // + rnd
361 6 dgisselq
                        "\t\t\t//\n"
362
                        "\t\t\to_sync <= i_sync;\n"
363 5 dgisselq
                "\t\tend\n"
364 2 dgisselq
"\n"
365 5 dgisselq
        "\t// Now, if the master control program doesn't want to keep all of\n"
366
        "\t// our bits, we can shift down to OWIDTH bits here.\n"
367
        "\tassign\to_out_0r = out_0r[(IWIDTH-SHIFT):(IWIDTH+1-OWIDTH-SHIFT)];\n"
368
        "\tassign\to_out_0i = out_0i[(IWIDTH-SHIFT):(IWIDTH+1-OWIDTH-SHIFT)];\n"
369
        "\tassign\to_out_1r = out_1r[(IWIDTH-SHIFT):(IWIDTH+1-OWIDTH-SHIFT)];\n"
370
        "\tassign\to_out_1i = out_1i[(IWIDTH-SHIFT):(IWIDTH+1-OWIDTH-SHIFT)];\n"
371 2 dgisselq
"\n"
372 5 dgisselq
        "\tassign\to_left  = { o_out_0r, o_out_0i };\n"
373
        "\tassign\to_right = { o_out_1r, o_out_1i };\n"
374 2 dgisselq
"\n"
375
"endmodule\n");
376
        fclose(fp);
377
}
378
 
379
void    build_multiply(const char *fname) {
380
        FILE    *fp = fopen(fname, "w");
381
        if (NULL == fp) {
382
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
383
                perror("O/S Err was:");
384
                return;
385
        }
386
 
387
        fprintf(fp,
388
"///////////////////////////////////////////////////////////////////////////\n"
389
"//\n"
390
"// Filename:   shiftaddmpy.v\n"
391
"//\n"
392
"// Project:    %s\n"
393
"//\n"
394
"// Purpose:    A portable shift and add multiply.\n"
395
"//\n"
396
"//             While both Xilinx and Altera will offer single clock \n"
397
"//             multiplies, this simple approach will multiply two numbers\n"
398
"//             on any architecture.  The result maintains the full width\n"
399
"//             of the multiply, there are no extra stuff bits, no rounding,\n"
400
"//             no shifted bits, etc.\n"
401
"//\n"
402
"//             Further, for those applications that can support it, this\n"
403
"//             multiply is pipelined and will produce one answer per clock.\n"
404
"//\n"
405
"//             For minimal processing delay, make the first parameter\n"
406
"//             the one with the least bits, so that AWIDTH <= BWIDTH.\n"
407
"//\n"
408
"//             The processing delay in this multiply is (AWIDTH+1) cycles.\n"
409
"//             That is, if the data is present on the input at clock t=0,\n"
410
"//             the result will be present on the output at time t=AWIDTH+1;\n"
411
"//\n"
412
"//\n%s"
413
"//\n", prjname, creator);
414
 
415
        fprintf(fp, "%s", cpyleft);
416
        fprintf(fp,
417
"module shiftaddmpy(i_clk, i_ce, i_a, i_b, o_r);\n"
418
        "\tparameter\tAWIDTH=16,BWIDTH=AWIDTH;\n"
419
        "\tinput\t\t\t\t\ti_clk, i_ce;\n"
420
        "\tinput\t\t[(AWIDTH-1):0]\t\ti_a;\n"
421
        "\tinput\t\t[(BWIDTH-1):0]\t\ti_b;\n"
422
        "\toutput\treg\t[(AWIDTH+BWIDTH-1):0]\to_r;\n"
423
"\n"
424
        "\treg\t[(AWIDTH-1):0]\tu_a;\n"
425
        "\treg\t[(BWIDTH-1):0]\tu_b;\n"
426
        "\treg\t\t\tsgn;\n"
427
"\n"
428
        "\treg\t[(AWIDTH-2):0]\t\tr_a[0:(AWIDTH-1)];\n"
429
        "\treg\t[(AWIDTH+BWIDTH-2):0]\tr_b[0:(AWIDTH-1)];\n"
430
        "\treg\t\t\t\tr_s[0:(AWIDTH-1)];\n"
431
        "\treg\t[(AWIDTH+BWIDTH-1):0]\tacc[0:(AWIDTH-1)];\n"
432
        "\tgenvar k;\n"
433
"\n"
434 5 dgisselq
        "\t// If we were forced to stay within two\'s complement arithmetic,\n"
435
        "\t// taking the absolute value here would require an additional bit.\n"
436
        "\t// However, because our results are now unsigned, we can stay\n"
437
        "\t// within the number of bits given (for now).\n"
438 2 dgisselq
        "\talways @(posedge i_clk)\n"
439
                "\t\tif (i_ce)\n"
440
                "\t\tbegin\n"
441
                        "\t\t\tu_a <= (i_a[AWIDTH-1])?(-i_a):(i_a);\n"
442
                        "\t\t\tu_b <= (i_b[BWIDTH-1])?(-i_b):(i_b);\n"
443
                        "\t\t\tsgn <= i_a[AWIDTH-1] ^ i_b[BWIDTH-1];\n"
444
                "\t\tend\n"
445
"\n"
446
        "\talways @(posedge i_clk)\n"
447
                "\t\tif (i_ce)\n"
448
                "\t\tbegin\n"
449
                        "\t\t\tacc[0] <= (u_a[0]) ? { {(AWIDTH){1'b0}}, u_b }\n"
450
                        "\t\t\t\t\t: {(AWIDTH+BWIDTH){1'b0}};\n"
451
                        "\t\t\tr_a[0] <= { u_a[(AWIDTH-1):1] };\n"
452
                        "\t\t\tr_b[0] <= { {(AWIDTH-1){1'b0}}, u_b };\n"
453
                        "\t\t\tr_s[0] <= sgn; // The final sign, needs to be preserved\n"
454
                "\t\tend\n"
455
"\n"
456
        "\tgenerate\n"
457
        "\talways @(posedge i_clk)\n"
458
        "\tif (i_ce)\n"
459
        "\tbegin\n"
460
                "\t\tfor(k=0; k<AWIDTH-1; k++)\n"
461
                "\t\tbegin\n"
462
                        "\t\t\tacc[k+1] <= acc[k] + ((r_a[k][0]) ? {r_b[k],1'b0}:0);\n"
463
                        "\t\t\tr_a[k+1] <= { 1'b0, r_a[k][(AWIDTH-2):1] };\n"
464
                        "\t\t\tr_b[k+1] <= { r_b[k][(AWIDTH+BWIDTH-3):0], 1'b0};\n"
465
                        "\t\t\tr_s[k+1] <= r_s[k];\n"
466
                "\t\tend\n"
467
        "\tend\n"
468
        "\tendgenerate\n"
469
"\n"
470
        "\talways @(posedge i_clk)\n"
471
                "\t\tif (i_ce)\n"
472
                        "\t\t\to_r <= (r_s[AWIDTH-1]) ? (-acc[AWIDTH-1]) : acc[AWIDTH-1];\n"
473
"\n"
474
"endmodule\n");
475
 
476
        fclose(fp);
477
}
478
 
479
void    build_dblreverse(const char *fname) {
480
        FILE    *fp = fopen(fname, "w");
481
        if (NULL == fp) {
482
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
483
                perror("O/S Err was:");
484
                return;
485
        }
486
 
487
        fprintf(fp,
488
"///////////////////////////////////////////////////////////////////////////\n"
489
"//\n"
490
"// Filename:   dblreverse.v\n"
491
"//\n"
492
"// Project:    %s\n"
493
"//\n"
494
"// Purpose:    This module bitreverses a pipelined FFT input.  Operation is\n"
495
"//             expected as follows:\n"
496
"//\n"
497
"//             i_clk   A running clock at whatever system speed is offered.\n"
498
"//             i_rst   A synchronous reset signal, that resets all internals\n"
499
"//             i_ce    If this is one, one input is consumed and an output\n"
500
"//                     is produced.\n"
501
"//             i_in_0, i_in_1\n"
502
"//                     Two inputs to be consumed, each of width WIDTH.\n"
503
"//             o_out_0, o_out_1\n"
504
"//                     Two of the bitreversed outputs, also of the same\n"
505
"//                     width, WIDTH.  Of course, there is a delay from the\n"
506
"//                     first input to the first output.  For this purpose,\n"
507
"//                     o_sync is present.\n"
508
"//             o_sync  This will be a 1'b1 for the first value in any block.\n"
509
"//                     Following a reset, this will only become 1'b1 once\n"
510
"//                     the data has been loaded and is now valid.  After that,\n"
511
"//                     all outputs will be valid.\n"
512
"//\n%s"
513
"//\n", prjname, creator);
514
        fprintf(fp, "%s", cpyleft);
515
        fprintf(fp,
516
"\n\n"
517
"//\n"
518
"// How do we do bit reversing at two smples per clock?  Can we separate out\n"
519
"// our work into eight memory banks, writing two banks at once and reading\n"
520
"// another two banks in the same clock?\n"
521
"//\n"
522
"//     mem[00xxx0] = s_0[n]\n"
523
"//     mem[00xxx1] = s_1[n]\n"
524
"//     o_0[n] = mem[10xxx0]\n"
525
"//     o_1[n] = mem[11xxx0]\n"
526
"//     ...\n"
527
"//     mem[01xxx0] = s_0[m]\n"
528
"//     mem[01xxx1] = s_1[m]\n"
529
"//     o_0[m] = mem[10xxx1]\n"
530
"//     o_1[m] = mem[11xxx1]\n"
531
"//     ...\n"
532
"//     mem[10xxx0] = s_0[n]\n"
533
"//     mem[10xxx1] = s_1[n]\n"
534
"//     o_0[n] = mem[00xxx0]\n"
535
"//     o_1[n] = mem[01xxx0]\n"
536
"//     ...\n"
537
"//     mem[11xxx0] = s_0[m]\n"
538
"//     mem[11xxx1] = s_1[m]\n"
539
"//     o_0[m] = mem[00xxx1]\n"
540
"//     o_1[m] = mem[01xxx1]\n"
541
"//     ...\n"
542
"//\n"
543 5 dgisselq
"//     The answer is that, yes we can but: we need to use four memory banks\n"
544
"//     to do it properly.  These four banks are defined by the two bits\n"
545
"//     that determine the top and bottom of the correct address.  Larger\n"
546
"//     FFT\'s would require more memories.\n"
547
"//\n"
548 2 dgisselq
"//\n");
549
        fprintf(fp,
550
"module dblreverse(i_clk, i_rst, i_ce, i_in_0, i_in_1,\n"
551 5 dgisselq
        "\t\to_out_0, o_out_1, o_sync);\n"
552
        "\tparameter\t\t\tLGSIZE=4, WIDTH=24;\n"
553
        "\tinput\t\t\t\ti_clk, i_rst, i_ce;\n"
554
        "\tinput\t\t[(2*WIDTH-1):0]\ti_in_0, i_in_1;\n"
555
        "\toutput\treg\t[(2*WIDTH-1):0]\to_out_0, o_out_1;\n"
556
        "\toutput\treg\t\t\to_sync;\n"
557 2 dgisselq
"\n"
558 5 dgisselq
        "\treg\tin_reset;\n"
559
        "\treg\t[(LGSIZE):0]\tiaddr;\n"
560
        "\treg\t[(2*WIDTH-1):0]\tmem_0e [0:((1<<(LGSIZE-1))-1)];\n"
561
        "\treg\t[(2*WIDTH-1):0]\tmem_0o [0:((1<<(LGSIZE-1))-1)];\n"
562
        "\treg\t[(2*WIDTH-1):0]\tmem_1e [0:((1<<(LGSIZE-1))-1)];\n"
563
        "\treg\t[(2*WIDTH-1):0]\tmem_1o [0:((1<<(LGSIZE-1))-1)];\n"
564 2 dgisselq
"\n"
565 5 dgisselq
        "\twire\t[(2*LGSIZE-1):0]       braddr;\n"
566
        "\tgenvar\tk;\n"
567
        "\tgenerate for(k=0; k<LGSIZE; k++)\n"
568
                "\t\tassign braddr[k] = iaddr[LGSIZE-1-k];\n"
569
        "\tendgenerate\n"
570 2 dgisselq
"\n"
571 5 dgisselq
        "\talways @(posedge i_clk)\n"
572
                "\t\tif (i_rst)\n"
573
                "\t\tbegin\n"
574
                        "\t\t\tiaddr <= 0;\n"
575
                        "\t\t\tin_reset <= 1'b1;\n"
576
                "\t\tend else if (i_ce)\n"
577
                "\t\tbegin\n"
578
                        "\t\t\tif (iaddr[(LGSIZE-1)])\n"
579
                        "\t\t\tbegin\n"
580
                                "\t\t\t\tmem_1e[{iaddr[LGSIZE],iaddr[(LGSIZE-2):1]}] <= i_in_0;\n"
581
                                "\t\t\t\tmem_1o[{iaddr[LGSIZE],iaddr[(LGSIZE-2):1]}] <= i_in_1;\n"
582
                        "\t\t\tend else begin\n"
583
                                "\t\t\t\tmem_0e[{iaddr[LGSIZE],iaddr[(LGSIZE-2):1]}] <= i_in_0;\n"
584
                                "\t\t\t\tmem_0o[{iaddr[LGSIZE],iaddr[(LGSIZE-2):1]}] <= i_in_1;\n"
585
                        "\t\t\tend\n"
586
                        "\t\t\tiaddr <= iaddr + 2;\n"
587
                        "\t\t\tif (&iaddr[(LGSIZE-1):1])\n"
588
                                "\t\t\t\tin_reset <= 1'b0;\n"
589
                        "\t\t\tif (in_reset)\n"
590
                        "\t\t\tbegin\n"
591
                                "\t\t\t\to_out_0 <= {(2*WIDTH){1'b0}};\n"
592
                                "\t\t\t\to_out_1 <= {(2*WIDTH){1'b0}};\n"
593
                                "\t\t\t\to_sync <= 1'b0;\n"
594
                        "\t\t\tend else\n"
595
                        "\t\t\tbegin\n"
596
                                "\t\t\t\tif (braddr[0])\n"
597
                                "\t\t\t\tbegin\n"
598 2 dgisselq
"\t\t\t\t\to_out_0 <= mem_0o[{~iaddr[LGSIZE],braddr[(LGSIZE-2):1]}];\n"
599
"\t\t\t\t\to_out_1 <= mem_1o[{~iaddr[LGSIZE],braddr[(LGSIZE-2):1]}];\n"
600 5 dgisselq
                                "\t\t\t\tend else begin\n"
601 2 dgisselq
"\t\t\t\t\to_out_0 <= mem_0e[{~iaddr[LGSIZE],braddr[(LGSIZE-2):1]}];\n"
602
"\t\t\t\t\to_out_1 <= mem_1e[{~iaddr[LGSIZE],braddr[(LGSIZE-2):1]}];\n"
603 5 dgisselq
                                "\t\t\t\tend\n"
604
                                "\t\t\t\to_sync <= ~(|iaddr[(LGSIZE-1):0]);\n"
605
                        "\t\t\tend\n"
606
                "\t\tend\n"
607 2 dgisselq
"\n"
608
"endmodule;\n");
609
 
610
        fclose(fp);
611
}
612
 
613 14 dgisselq
void    build_butterfly(const char *fname, int xtracbits) {
614 2 dgisselq
        FILE    *fp = fopen(fname, "w");
615
        if (NULL == fp) {
616
                fprintf(stderr, "Could not open \'%s\' for writing\n", fname);
617
                perror("O/S Err was:");
618
                return;
619
        }
620
 
621
        fprintf(fp,
622
"///////////////////////////////////////////////////////////////////////////\n"
623
"//\n"
624
"// Filename:   butterfly.v\n"
625
"//\n"
626
"// Project:    %s\n"
627
"//\n"
628
"// Purpose:    This routine caculates a butterfly for a decimation\n"
629
"//             in frequency version of an FFT.  Specifically, given\n"
630
"//             complex Left and Right values together with a \n"
631
"//             coefficient, the output of this routine is given\n"
632
"//             by:\n"
633
"//\n"
634
"//             L' = L + R\n"
635
"//             R' = (L - R)*C\n"
636
"//\n"
637
"//             The rest of the junk below handles timing (mostly),\n"
638
"//             to make certain that L' and R' reach the output at\n"
639
"//             the same clock.  Further, just to make certain\n"
640
"//             that is the case, an 'aux' input exists.  This\n"
641
"//             aux value will come out of this routine synchronized\n"
642
"//             to the values it came in with.  (i.e., both L', R',\n"
643
"//             and aux all have the same delay.)  Hence, a caller\n"
644
"//             of this routine may set aux on the first input with\n"
645
"//             valid data, and then wait to see aux set on the output\n"
646
"//             to know when to find the first output with valid data.\n"
647
"//\n"
648
"//             All bits are preserved until the very last clock,\n"
649
"//             where any more bits than OWIDTH will be quietly\n"
650
"//             discarded.\n"
651
"//\n"
652
"//             This design features no overflow checking.\n"
653
"// \n"
654
"// Notes:\n"
655
"//             CORDIC:\n"
656
"//             Much as we would like, we can't use a cordic here.\n"
657
"//             The goal is to accomplish an FFT, as defined, and a\n"
658
"//             CORDIC places a scale factor onto the data.  Removing\n"
659
"//             the scale factor would cost a two multiplies, which\n"
660
"//             is precisely what we are trying to avoid.\n"
661
"//\n"
662
"//\n"
663
"//             3-MULTIPLIES:\n"
664
"//             It should also be possible to do this with three \n"
665
"//             multiplies and an extra two addition cycles.  \n"
666
"//\n"
667
"//             We want\n"
668
"//                     R+I = (a + jb) * (c + jd)\n"
669
"//                     R+I = (ac-bd) + j(ad+bc)\n"
670
"//             We multiply\n"
671
"//                     P1 = ac\n"
672
"//                     P2 = bd\n"
673
"//                     P3 = (a+b)(c+d)\n"
674
"//             Then \n"
675
"//                     R+I=(P1-P2)+j(P3-P2-P1)\n"
676
"//\n"
677
"//             WIDTHS:\n"
678
"//             On multiplying an X width number by an\n"
679
"//             Y width number, X>Y, the result should be (X+Y)\n"
680
"//             bits, right?\n"
681
"//             -2^(X-1) <= a <= 2^(X-1) - 1\n"
682
"//             -2^(Y-1) <= b <= 2^(Y-1) - 1\n"
683
"//             (2^(Y-1)-1)*(-2^(X-1)) <= ab <= 2^(X-1)2^(Y-1)\n"
684
"//             -2^(X+Y-2)+2^(X-1) <= ab <= 2^(X+Y-2) <= 2^(X+Y-1) - 1\n"
685
"//             -2^(X+Y-1) <= ab <= 2^(X+Y-1)-1\n"
686
"//             YUP!  But just barely.  Do this and you'll really want\n"
687
"//             to drop a bit, although you will risk overflow in so\n"
688
"//             doing.\n"
689
"//\n%s"
690
"//\n", prjname, creator);
691
        fprintf(fp, "%s", cpyleft);
692
 
693
        fprintf(fp,
694 6 dgisselq
"module\tbutterfly(i_clk, i_rst, i_ce, i_coef, i_left, i_right, i_aux,\n"
695 5 dgisselq
                "\t\to_left, o_right, o_aux);\n"
696
        "\t// Public changeable parameters ...\n"
697 14 dgisselq
        "\tparameter IWIDTH=%d,CWIDTH=IWIDTH+%d,OWIDTH=IWIDTH+1;\n"
698 5 dgisselq
        "\t// Parameters specific to the core that should not be changed.\n"
699 14 dgisselq
        "\tparameter    MPYDELAY=%d'd%d, // (IWIDTH+1 < CWIDTH)?(IWIDTH+4):(CWIDTH+3),\n"
700 15 dgisselq
                        "\t\t\tSHIFT=0, ROUND=1;\n"
701 5 dgisselq
        "\t// The LGDELAY should be the base two log of the MPYDELAY.  If\n"
702
        "\t// this value is fractional, then round up to the nearest\n"
703
        "\t// integer: LGDELAY=ceil(log(MPYDELAY)/log(2));\n"
704 14 dgisselq
        "\tparameter\tLGDELAY=%d;\n"
705 6 dgisselq
        "\tinput\t\ti_clk, i_rst, i_ce;\n"
706 5 dgisselq
        "\tinput\t\t[(2*CWIDTH-1):0] i_coef;\n"
707
        "\tinput\t\t[(2*IWIDTH-1):0] i_left, i_right;\n"
708
        "\tinput\t\ti_aux;\n"
709
        "\toutput\twire [(2*OWIDTH-1):0] o_left, o_right;\n"
710
        "\toutput\twire o_aux;\n"
711 14 dgisselq
        "\n", 16, xtracbits, lgdelay(16,xtracbits),
712
        bflydelay(16, xtracbits), lgdelay(16,xtracbits));
713
        fprintf(fp,
714 5 dgisselq
        "\twire\t[(OWIDTH-1):0] o_left_r, o_left_i, o_right_r, o_right_i;\n"
715 2 dgisselq
"\n"
716 5 dgisselq
        "\treg\t[(2*IWIDTH-1):0]\tr_left, r_right;\n"
717
        "\treg\t\t\t\tr_aux, r_aux_2;\n"
718
        "\treg\t[(2*CWIDTH-1):0]\tr_coef, r_coef_2;\n"
719
        "\twire\tsigned\t[(CWIDTH-1):0]\tr_coef_r, r_coef_i;\n"
720
        "\tassign\tr_coef_r  = r_coef_2[ (2*CWIDTH-1):(CWIDTH)];\n"
721
        "\tassign\tr_coef_i  = r_coef_2[ (  CWIDTH-1):0];\n"
722
        "\twire\tsigned\t[(IWIDTH-1):0]\tr_left_r, r_left_i, r_right_r, r_right_i;\n"
723
        "\tassign\tr_left_r  = r_left[ (2*IWIDTH-1):(IWIDTH)];\n"
724
        "\tassign\tr_left_i  = r_left[ (IWIDTH-1):0];\n"
725
        "\tassign\tr_right_r = r_right[(2*IWIDTH-1):(IWIDTH)];\n"
726
        "\tassign\tr_right_i = r_right[(IWIDTH-1):0];\n"
727 2 dgisselq
"\n"
728 5 dgisselq
        "\treg\tsigned\t[(IWIDTH):0]\tr_sum_r, r_sum_i, r_dif_r, r_dif_i;\n"
729 2 dgisselq
"\n"
730 5 dgisselq
        "\treg  [(LGDELAY-1):0] fifo_addr;\n"
731
        "\twire [(LGDELAY-1):0] fifo_read_addr;\n"
732 6 dgisselq
        "\tassign\tfifo_read_addr = fifo_addr - MPYDELAY;\n"
733 5 dgisselq
        "\treg  [(2*IWIDTH+2):0]        fifo_left [ 0:((1<<LGDELAY)-1)];\n"
734 6 dgisselq
        "\treg\t\t\t\tovalid;\n"
735 5 dgisselq
"\n");
736
        fprintf(fp,
737
        "\t// Set up the input to the multiply\n"
738 2 dgisselq
        "\talways @(posedge i_clk)\n"
739
                "\t\tif (i_ce)\n"
740
                "\t\tbegin\n"
741
                        "\t\t\t// One clock just latches the inputs\n"
742
                        "\t\t\tr_left <= i_left;        // No change in # of bits\n"
743
                        "\t\t\tr_right <= i_right;\n"
744
                        "\t\t\tr_aux <= i_aux;\n"
745
                        "\t\t\tr_coef  <= i_coef;\n"
746
                        "\t\t\t// Next clock adds/subtracts\n"
747
                        "\t\t\tr_sum_r <= r_left_r + r_right_r; // Now IWIDTH+1 bits\n"
748
                        "\t\t\tr_sum_i <= r_left_i + r_right_i;\n"
749
                        "\t\t\tr_dif_r <= r_left_r - r_right_r;\n"
750
                        "\t\t\tr_dif_i <= r_left_i - r_right_i;\n"
751
                        "\t\t\t// Other inputs are simply delayed on second clock\n"
752
                        "\t\t\tr_aux_2 <= r_aux;\n"
753
                        "\t\t\tr_coef_2<= r_coef;\n"
754
        "\t\tend\n"
755 5 dgisselq
"\n");
756
        fprintf(fp,
757
        "\t// Don\'t forget to record the even side, since it doesn\'t need\n"
758
        "\t// to be multiplied, but yet we still need the results in sync\n"
759
        "\t// with the answer when it is ready.\n"
760 2 dgisselq
        "\talways @(posedge i_clk)\n"
761 6 dgisselq
                "\t\tif (i_rst)\n"
762 2 dgisselq
                "\t\tbegin\n"
763 6 dgisselq
                        "\t\t\tfifo_addr <= 0;\n"
764
                        "\t\t\tovalid <= 1'b0;\n"
765
                "\t\tend else if (i_ce)\n"
766
                "\t\tbegin\n"
767 2 dgisselq
                        "\t\t\t// Need to delay the sum side--nothing else happens\n"
768
                        "\t\t\t// to it, but it needs to stay synchronized with the\n"
769
                        "\t\t\t// right side.\n"
770
                        "\t\t\tfifo_left[fifo_addr] <= { r_aux_2, r_sum_r, r_sum_i };\n"
771
                        "\t\t\tfifo_addr <= fifo_addr + 1;\n"
772 14 dgisselq
"\n"
773
                        "\t\t\tovalid <= (ovalid) || (fifo_addr > (MPYDELAY+1));\n"
774 2 dgisselq
                "\t\tend\n"
775
"\n"
776 5 dgisselq
        "\twire\tsigned\t[(CWIDTH-1):0] ir_coef_r, ir_coef_i;\n"
777
        "\tassign\tir_coef_r = r_coef_2[(2*CWIDTH-1):CWIDTH];\n"
778
        "\tassign\tir_coef_i = r_coef_2[(CWIDTH-1):0];\n"
779
        "\twire\tsigned\t[((IWIDTH+2)+(CWIDTH+1)-1):0]\tp_one, p_two, p_three;\n"
780 2 dgisselq
"\n"
781 5 dgisselq
"\n");
782
        fprintf(fp,
783
        "\t// Multiply output is always a width of the sum of the widths of\n"
784
        "\t// the two inputs.  ALWAYS.  This is independent of the number of\n"
785
        "\t// bits in p_one, p_two, or p_three.  These values needed to \n"
786
        "\t// accumulate a bit (or two) each.  However, this approach to a\n"
787
        "\t// three multiply complex multiply cannot increase the total\n"
788
        "\t// number of bits in our final output.  We\'ll take care of\n"
789
        "\t// dropping back down to the proper width, OWIDTH, in our routine\n"
790
        "\t// below.\n"
791 2 dgisselq
"\n"
792 5 dgisselq
"\n");
793
        fprintf(fp,
794
        "\t// We accomplish here \"Karatsuba\" multiplication.  That is,\n"
795
        "\t// by doing three multiplies we accomplish the work of four.\n"
796
        "\t// Let\'s prove to ourselves that this works ... We wish to\n"
797
        "\t// multiply: (a+jb) * (c+jd), where a+jb is given by\n"
798
        "\t//\ta + jb = r_dif_r + j r_dif_i, and\n"
799
        "\t//\tc + jd = ir_coef_r + j ir_coef_i.\n"
800
        "\t// We do this by calculating the intermediate products P1, P2,\n"
801
        "\t// and P3 as\n"
802
        "\t//\tP1 = ac\n"
803
        "\t//\tP2 = bd\n"
804
        "\t//\tP3 = (a + b) * (c + d)\n"
805
        "\t// and then complete our final answer with\n"
806
        "\t//\tac - bd = P1 - P2 (this checks)\n"
807
        "\t//\tad + bc = P3 - P2 - P1\n"
808
        "\t//\t        = (ac + bc + ad + bd) - bd - ac\n"
809
        "\t//\t        = bc + ad (this checks)\n"
810 2 dgisselq
"\n"
811 5 dgisselq
"\n");
812
        fprintf(fp,
813
        "\t// This should really be based upon an IF, such as in\n"
814
        "\t// if (IWIDTH < CWIDTH) then ...\n"
815
        "\t// However, this is the only (other) way I know to do it.\n"
816 2 dgisselq
        "\tgenerate\n"
817
        "\tif (CWIDTH < IWIDTH+1)\n"
818
        "\tbegin\n"
819
                "\t\t// We need to pad these first two multiplies by an extra\n"
820 5 dgisselq
                "\t\t// bit just to keep them aligned with the third,\n"
821
                "\t\t// simpler, multiply.\n"
822 2 dgisselq
                "\t\tshiftaddmpy #(CWIDTH+1,IWIDTH+2) p1(i_clk, i_ce,\n"
823
                                "\t\t\t\t{ir_coef_r[CWIDTH-1],ir_coef_r},\n"
824
                                "\t\t\t\t{r_dif_r[IWIDTH],r_dif_r}, p_one);\n"
825
                "\t\tshiftaddmpy #(CWIDTH+1,IWIDTH+2) p2(i_clk, i_ce,\n"
826 5 dgisselq
                                "\t\t\t\t{ir_coef_i[CWIDTH-1],ir_coef_i},\n"
827 2 dgisselq
                                "\t\t\t\t{r_dif_i[IWIDTH],r_dif_i}, p_two);\n"
828
                "\t\tshiftaddmpy #(CWIDTH+1,IWIDTH+2) p3(i_clk, i_ce,\n"
829 5 dgisselq
                        "\t\t\t\tir_coef_i+ir_coef_r,\n"
830
                        "\t\t\t\tr_dif_r + r_dif_i,\n"
831
                        "\t\t\t\tp_three);\n"
832 2 dgisselq
        "\tend else begin\n"
833
                "\t\tshiftaddmpy #(IWIDTH+2,CWIDTH+1) p1a(i_clk, i_ce,\n"
834
                                "\t\t\t\t{r_dif_r[IWIDTH],r_dif_r},\n"
835
                                "\t\t\t\t{ir_coef_r[CWIDTH-1],ir_coef_r}, p_one);\n"
836
                "\t\tshiftaddmpy #(IWIDTH+2,CWIDTH+1) p2a(i_clk, i_ce,\n"
837
                                "\t\t\t\t{r_dif_i[IWIDTH], r_dif_i},\n"
838 5 dgisselq
                                "\t\t\t\t{ir_coef_i[CWIDTH-1],ir_coef_i}, p_two);\n"
839 2 dgisselq
                "\t\tshiftaddmpy #(IWIDTH+2,CWIDTH+1) p3a(i_clk, i_ce,\n"
840
                                "\t\t\t\tr_dif_r+r_dif_i,\n"
841
                                "\t\t\t\tir_coef_i+ir_coef_r,\n"
842
                                "\t\t\t\tp_three);\n"
843
        "\tend\n"
844
        "\tendgenerate\n"
845 5 dgisselq
"\n");
846
        fprintf(fp,
847
        "\t// These values are held in memory and delayed during the\n"
848
        "\t// multiply.  Here, we recover them.  During the multiply,\n"
849
        "\t// values were multiplied by 2^(CWIDTH-2)*exp{-j*2*pi*...},\n"
850
        "\t// therefore, the left_x values need to be right shifted by\n"
851
        "\t// CWIDTH-2 as well.  The additional bits come from a sign\n"
852
        "\t// extension.\n"
853 2 dgisselq
        "\twire aux;\n"
854 5 dgisselq
        "\twire\tsigned\t[(IWIDTH+CWIDTH):0]    fifo_i, fifo_r;\n"
855
        "\treg\t\t[(2*IWIDTH+2):0]      fifo_read;\n"
856
        "\tassign\tfifo_r = { {2{fifo_read[2*(IWIDTH+1)-1]}}, fifo_read[(2*(IWIDTH+1)-1):(IWIDTH+1)], {(CWIDTH-2){1'b0}} };\n"
857
        "\tassign\tfifo_i = { {2{fifo_read[(IWIDTH+1)-1]}}, fifo_read[((IWIDTH+1)-1):0], {(CWIDTH-2){1'b0}} };\n"
858
        "\tassign\taux = fifo_read[2*IWIDTH+2];\n"
859 2 dgisselq
"\n"
860
"\n"
861 5 dgisselq
        "\treg\tsigned\t[(CWIDTH+IWIDTH+3-1):0] b_left_r, b_left_i,\n"
862
                        "\t\t\t\t\t\tb_right_r, b_right_i;\n"
863
        "\treg\tsigned\t[(CWIDTH+IWIDTH+3-1):0] mpy_r, mpy_i;\n"
864
        "\treg\tsigned\t[(CWIDTH+IWIDTH+3-1):0] rnd;\n"
865
        "\tgenerate\n"
866 16 dgisselq
        "\tif ((ROUND==0)||(CWIDTH+IWIDTH-OWIDTH-SHIFT<2))\n"
867 5 dgisselq
                "\t\tassign rnd = ({(CWIDTH+IWIDTH+3){1'b0}});\n"
868 16 dgisselq
        "\telse if ((IWIDTH+CWIDTH)-(OWIDTH+SHIFT) == 2)\n"
869
                "\t\tassign rnd = ({ {(OWIDTH+4+SHIFT){1'b0}},1'b1 });\n"
870 5 dgisselq
        "\telse\n"
871 15 dgisselq
                "\t\tassign rnd = ({ {(OWIDTH+4+SHIFT){1'b0}},1'b1,\n"
872
                "\t\t\t\t{((IWIDTH+CWIDTH+3)-(OWIDTH+SHIFT+5)){1'b0}} });\n"
873 5 dgisselq
        "\tendgenerate\n"
874
"\n");
875
        fprintf(fp,
876 2 dgisselq
        "\talways @(posedge i_clk)\n"
877
                "\t\tif (i_ce)\n"
878
                "\t\tbegin\n"
879
                        "\t\t\t// First clock, recover all values\n"
880
                        "\t\t\tfifo_read <= fifo_left[fifo_read_addr];\n"
881
                        "\t\t\t// These values are IWIDTH+CWIDTH+3 bits wide\n"
882 5 dgisselq
                        "\t\t\t// although they only need to be (IWIDTH+1)\n"
883
                        "\t\t\t// + (CWIDTH) bits wide.  (We\'ve got two\n"
884
                        "\t\t\t// extra bits we need to get rid of.)\n"
885 2 dgisselq
                        "\t\t\tmpy_r <= p_one - p_two;\n"
886
                        "\t\t\tmpy_i <= p_three - p_one - p_two;\n"
887
"\n"
888
                        "\t\t\t// Second clock, round and latch for final clock\n"
889
                        "\t\t\tb_right_r <= mpy_r + rnd;\n"
890
                        "\t\t\tb_right_i <= mpy_i + rnd;\n"
891 5 dgisselq
                        "\t\t\tb_left_r <= { {2{fifo_r[(IWIDTH+CWIDTH)]}},fifo_r } + rnd;\n"
892
                        "\t\t\tb_left_i <= { {2{fifo_i[(IWIDTH+CWIDTH)]}},fifo_i } + rnd;\n"
893 6 dgisselq
                        "\t\t\to_aux <= aux & ovalid;\n"
894 2 dgisselq
                "\t\tend\n"
895 5 dgisselq
"\n");
896
        fprintf(fp,
897 2 dgisselq
        "\t// Final clock--clock and remove unnecessary bits.\n"
898 5 dgisselq
        "\t// We have (IWIDTH+CWIDTH+3) bits here, we need to drop down to\n"
899
        "\t// OWIDTH, and SHIFT by SHIFT bits in the process.  The trick is\n"
900
        "\t// that we don\'t need (IWIDTH+CWIDTH+3) bits.  We\'ve accumulated\n"
901
        "\t// them, but the actual values will never fill all these bits.\n"
902
        "\t// In particular, we only need:\n"
903
        "\t//\t IWIDTH bits for the input\n"
904
        "\t//\t     +1 bit for the add/subtract\n"
905
        "\t//\t+CWIDTH bits for the coefficient multiply\n"
906
        "\t//\t     +1 bit for the add/subtract in the complex multiply\n"
907
        "\t//\t ------\n"
908
        "\t//\t (IWIDTH+CWIDTH+2) bits at full precision.\n"
909
        "\t//\n"
910
        "\t// However, the coefficient multiply multiplied by a maximum value\n"
911
        "\t// of 2^(CWIDTH-2).  Thus, we only have\n"
912
        "\t//\t   IWIDTH bits for the input\n"
913
        "\t//\t       +1 bit for the add/subtract\n"
914
        "\t//\t+CWIDTH-2 bits for the coefficient multiply\n"
915
        "\t//\t       +1 (optional) bit for the add/subtract in the cpx mpy.\n"
916
        "\t//\t -------- ... multiply.  (This last bit may be shifted out.)\n"
917
        "\t//\t (IWIDTH+CWIDTH) valid output bits. \n"
918
        "\t// Now, if the user wants to keep any extras of these (via OWIDTH),\n"
919
        "\t// or if he wishes to arbitrarily shift some of these off (via\n"
920
        "\t// SHIFT) we accomplish that here.\n"
921
        "\tassign o_left_r  = b_left_r[ (CWIDTH+IWIDTH-1-SHIFT-1):(CWIDTH+IWIDTH-OWIDTH-SHIFT-1)];\n"
922
        "\tassign o_left_i  = b_left_i[ (CWIDTH+IWIDTH-1-SHIFT-1):(CWIDTH+IWIDTH-OWIDTH-SHIFT-1)];\n"
923
        "\tassign o_right_r = b_right_r[(CWIDTH+IWIDTH-1-SHIFT-1):(CWIDTH+IWIDTH-OWIDTH-SHIFT-1)];\n"
924
        "\tassign o_right_i = b_right_i[(CWIDTH+IWIDTH-1-SHIFT-1):(CWIDTH+IWIDTH-OWIDTH-SHIFT-1)];\n"
925 2 dgisselq
"\n"
926 5 dgisselq
        "\t// As a final step, we pack our outputs into two packed two\'s\n"
927
        "\t// complement numbers per output word, so that each output word\n"
928
        "\t// has (2*OWIDTH) bits in it, with the top half being the real\n"
929
        "\t// portion and the bottom half being the imaginary portion.\n"
930 2 dgisselq
        "\tassign       o_left = { o_left_r, o_left_i };\n"
931
        "\tassign       o_right= { o_right_r,o_right_i};\n"
932
"\n"
933
"endmodule\n");
934
        fclose(fp);
935
}
936
 
937
void    build_stage(const char *fname, int stage, bool odd, int nbits, bool inv, int xtra) {
938
        FILE    *fstage = fopen(fname, "w");
939
        int     cbits = nbits + xtra;
940
 
941
        if ((cbits * 2) >= sizeof(long long)*8) {
942
                fprintf(stderr, "ERROR: CMEM Coefficient precision requested overflows long long data type.\n");
943
                exit(-1);
944
        }
945
 
946
        if (fstage == NULL) {
947
                fprintf(stderr, "ERROR: Could not open %s for writing!\n", fname);
948
                perror("O/S Err was:");
949
                fprintf(stderr, "Attempting to continue, but this file will be missing.\n");
950
                return;
951
        }
952
 
953
        fprintf(fstage,
954
"////////////////////////////////////////////////////////////////////////////\n"
955
"//\n"
956
"// Filename:   %sfftstage_%c%d.v\n"
957
"//\n"
958
"// Project:    %s\n"
959
"//\n"
960
"// Purpose:    This file is (almost) a Verilog source file.  It is meant to\n"
961
"//             be used by a FFT core compiler to generate FFTs which may be\n"
962
"//             used as part of an FFT core.  Specifically, this file \n"
963
"//             encapsulates the options of an FFT-stage.  For any 2^N length\n"
964
"//             FFT, there shall be (N-1) of these stages.  \n"
965
"//\n%s"
966
"//\n",
967
                (inv)?"i":"", (odd)?'o':'e', stage*2, prjname, creator);
968
        fprintf(fstage, "%s", cpyleft);
969
        fprintf(fstage, "module\t%sfftstage_%c%d(i_clk, i_rst, i_ce, i_sync, i_data, o_data, o_sync);\n",
970
                (inv)?"i":"", (odd)?'o':'e', stage*2);
971
        // These parameter values are useless at this point--they are to be
972
        // replaced by the parameter values in the calling program.  Only
973
        // problem is, the CWIDTH needs to match exactly!
974
        fprintf(fstage, "\tparameter\tIWIDTH=%d,CWIDTH=%d,OWIDTH=%d;\n",
975
                nbits, cbits, nbits+1);
976
        fprintf(fstage,
977
"\t// Parameters specific to the core that should be changed when this\n"
978
"\t// core is built ... Note that the minimum LGSPAN (the base two log\n"
979
"\t// of the span, or the base two log of the current FFT size) is 3.\n"
980
"\t// Smaller spans (i.e. the span of 2) must use the dblstage module.\n"
981 6 dgisselq
"\tparameter\tLGWIDTH=11, LGSPAN=9, LGBDLY=5, BFLYSHIFT=0;\n");
982 2 dgisselq
        fprintf(fstage,
983
"\tinput                                        i_clk, i_rst, i_ce, i_sync;\n"
984
"\tinput                [(2*IWIDTH-1):0]        i_data;\n"
985
"\toutput       reg     [(2*OWIDTH-1):0]        o_data;\n"
986
"\toutput       reg                             o_sync;\n"
987
"\n"
988
"\treg  wait_for_sync;\n"
989
"\treg  [(2*IWIDTH-1):0]        ib_a, ib_b;\n"
990
"\treg  [(2*CWIDTH-1):0]        ib_c;\n"
991 8 dgisselq
"\treg  ib_sync;\n"
992 2 dgisselq
"\n"
993
"\treg  b_started;\n"
994
"\twire ob_sync;\n"
995
"\twire [(2*OWIDTH-1):0]        ob_a, ob_b;\n");
996
        fprintf(fstage,
997
"\n"
998
"\t// %scmem is defined as an array of real and complex values,\n"
999
"\t// where the top CWIDTH bits are the real value and the bottom\n"
1000
"\t// CWIDTH bits are the imaginary value.\n"
1001
"\t//\n"
1002
"\t// cmem[i] = { (2^(CWIDTH-2)) * cos(2*pi*i/(2^LGWIDTH)),\n"
1003
"\t//           (2^(CWIDTH-2)) * sin(2*pi*i/(2^LGWIDTH)) };\n"
1004
"\t//\n"
1005
"\treg  [(2*CWIDTH-1):0]        %scmem [0:((1<<LGSPAN)-1)];\n"
1006
"\tinitial\t$readmemh(\"%scmem_%c%d.hex\",%scmem);\n\n",
1007
                (inv)?"i":"", (inv)?"i":"",
1008
                (inv)?"i":"", (odd)?'o':'e',stage<<1,
1009
                (inv)?"i":"");
1010
        {
1011
                FILE    *cmem;
1012
 
1013 14 dgisselq
                {
1014
                        char    *memfile, *ptr;
1015
 
1016
                        memfile = new char[strlen(fname)+128];
1017
                        strcpy(memfile, fname);
1018
                        if ((NULL != (ptr = strrchr(memfile, '/')))&&(ptr>memfile)) {
1019
                                ptr++;
1020
                                sprintf(ptr, "%scmem_%c%d.hex", (inv)?"i":"", (odd)?'o':'e', stage*2);
1021
                        } else {
1022
                                sprintf(memfile, "%s/%scmem_%c%d.hex",
1023
                                        COREDIR, (inv)?"i":"",
1024
                                        (odd)?'o':'e', stage*2);
1025
                        }
1026
                        // strcpy(&memfile[strlen(memfile)-2], ".hex");
1027
                        cmem = fopen(memfile, "w");
1028
                        if (NULL == cmem) {
1029
                                fprintf(stderr, "Could not open/write \'%s\' with FFT coefficients.\n", memfile);
1030
                                perror("Err from O/S:");
1031
                                exit(-2);
1032
                        }
1033
 
1034
                        delete[] memfile;
1035 2 dgisselq
                }
1036
                // fprintf(cmem, "// CBITS = %d, inv = %s\n", cbits, (inv)?"true":"false");
1037
                for(int i=0; i<stage/2; i++) {
1038
                        int k = 2*i+odd;
1039 9 dgisselq
                        double  W = ((inv)?1:-1)*2.0*M_PI*k/(double)(2*stage);
1040 2 dgisselq
                        double  c, s;
1041
                        long long ic, is, vl;
1042
 
1043
                        c = cos(W); s = sin(W);
1044
                        ic = (long long)((double)((1ll<<(cbits-2)) * c + 0.5));
1045
                        is = (long long)((double)((1ll<<(cbits-2)) * s + 0.5));
1046
                        vl = (ic & (~(-1ll << (cbits))));
1047
                        vl <<= (cbits);
1048
                        vl |= (is & (~(-1ll << (cbits))));
1049
                        fprintf(cmem, "%0*llx\n", ((cbits*2+3)/4), vl);
1050
                        /*
1051
                        fprintf(cmem, "%0*llx\t\t// %f+j%f -> %llx +j%llx\n",
1052
                                ((cbits*2+3)/4), vl, c, s,
1053
                                ic & (~(-1ll<<(((cbits+3)/4)*4))),
1054
                                is & (~(-1ll<<(((cbits+3)/4)*4))));
1055
                        */
1056
                } fclose(cmem);
1057
        }
1058
 
1059
        fprintf(fstage,
1060 6 dgisselq
"\treg  [(LGWIDTH-2):0]         iaddr;\n"
1061 2 dgisselq
"\treg  [(2*IWIDTH-1):0]        imem    [0:((1<<LGSPAN)-1)];\n"
1062
"\n"
1063 8 dgisselq
"\treg  [LGSPAN:0]              oB;\n"
1064 2 dgisselq
"\treg  [(2*OWIDTH-1):0]        omem    [0:((1<<LGSPAN)-1)];\n"
1065
"\n"
1066
"\talways @(posedge i_clk)\n"
1067
        "\t\tif (i_rst)\n"
1068
        "\t\tbegin\n"
1069
                "\t\t\twait_for_sync <= 1'b1;\n"
1070
                "\t\t\tiaddr <= 0;\n"
1071
                "\t\t\toB <= 0;\n"
1072 8 dgisselq
                "\t\t\tib_sync   <= 1'b0;\n"
1073
                "\t\t\to_sync    <= 1'b0;\n"
1074
                "\t\t\tb_started <= 1'b0;\n"
1075 2 dgisselq
        "\t\tend\n"
1076
        "\t\telse if ((i_ce)&&((~wait_for_sync)||(i_sync)))\n"
1077
        "\t\tbegin\n"
1078
                "\t\t\t//\n"
1079
                "\t\t\t// First step: Record what we\'re not ready to use yet\n"
1080
                "\t\t\t//\n"
1081
                "\t\t\timem[iaddr[(LGSPAN-1):0]] <= i_data;\n"
1082
                "\t\t\tiaddr <= iaddr + 1;\n"
1083
                "\t\t\twait_for_sync <= 1'b0;\n"
1084
"\n"
1085
                "\t\t\t//\n"
1086
                "\t\t\t// Now, we have all the inputs, so let\'s feed the\n"
1087
                "\t\t\t// butterfly\n"
1088
                "\t\t\t//\n"
1089 6 dgisselq
                "\t\t\tif (iaddr[LGSPAN])\n"
1090 2 dgisselq
                "\t\t\tbegin\n"
1091
                        "\t\t\t\t// One input from memory, ...\n"
1092
                        "\t\t\t\tib_a <= imem[iaddr[(LGSPAN-1):0]];\n"
1093
                        "\t\t\t\t// One input clocked in from the top\n"
1094
                        "\t\t\t\tib_b <= i_data;\n"
1095
                        "\t\t\t\t// Set the sync to true on the very first\n"
1096
                        "\t\t\t\t// valid input in, and hence on the very\n"
1097
                        "\t\t\t\t// first valid data out per FFT.\n"
1098 6 dgisselq
                        "\t\t\t\tib_sync <= (iaddr==(1<<(LGSPAN)));\n"
1099 2 dgisselq
                        "\t\t\t\tib_c <= %scmem[iaddr[(LGSPAN-1):0]];\n"
1100 8 dgisselq
                "\t\t\tend else begin\n"
1101
                        "\t\t\t\t// Just to make debugging easier, let\'s\n"
1102
                        "\t\t\t\t// clear these registers.  That\'ll make\n"
1103
                        "\t\t\t\t// the transition easier to watch.\n"
1104
                        "\t\t\t\tib_a <= {(2*IWIDTH){1'b0}};\n"
1105
                        "\t\t\t\tib_b <= {(2*IWIDTH){1'b0}};\n"
1106
                        "\t\t\t\tib_sync <= 1'b0;\n"
1107
                "\t\t\tend\n"
1108 2 dgisselq
"\n"
1109
                "\t\t\t//\n"
1110
                "\t\t\t// Next step: recover the outputs from the butterfly\n"
1111
                "\t\t\t//\n"
1112 8 dgisselq
                "\t\t\tif ((ob_sync||b_started)&&(~oB[LGSPAN]))\n"
1113 2 dgisselq
                "\t\t\tbegin // A butterfly output is available\n"
1114
                        "\t\t\t\tb_started <= 1'b1;\n"
1115 8 dgisselq
                        "\t\t\t\tomem[oB[(LGSPAN-1):0]] <= ob_b;\n"
1116 2 dgisselq
                        "\t\t\t\toB <= oB+1;\n"
1117
"\n"
1118 6 dgisselq
                        "\t\t\t\to_sync <= (ob_sync);\n"
1119 2 dgisselq
                        "\t\t\t\to_data <= ob_a;\n"
1120
                "\t\t\tend else if (b_started)\n"
1121
                "\t\t\tbegin // and keep outputting once you start--at a rate\n"
1122
                "\t\t\t// of one guaranteed output per clock that has i_ce set.\n"
1123 8 dgisselq
                        "\t\t\t\to_data <= omem[oB[(LGSPAN-1):0]];\n"
1124 2 dgisselq
                        "\t\t\t\toB <= oB + 1;\n"
1125
                        "\t\t\t\to_sync <= 1'b0;\n"
1126
                "\t\t\tend else\n"
1127
                        "\t\t\t\to_sync <= 1'b0;\n"
1128
        "\t\tend\n"
1129 5 dgisselq
"\n", (inv)?"i":"");
1130
        fprintf(fstage,
1131 2 dgisselq
"\tbutterfly #(.IWIDTH(IWIDTH),.CWIDTH(CWIDTH),.OWIDTH(OWIDTH),\n"
1132 5 dgisselq
"\t\t\t.MPYDELAY(%d\'d%d),.LGDELAY(LGBDLY),.SHIFT(BFLYSHIFT))\n"
1133 8 dgisselq
"\t\tbfly(i_clk, i_rst, i_ce, ib_c,\n"
1134 2 dgisselq
"\t\t\tib_a, ib_b, ib_sync, ob_a, ob_b, ob_sync);\n"
1135
"endmodule;\n",
1136 14 dgisselq
        lgdelay(nbits, xtra), bflydelay(nbits, xtra));
1137 2 dgisselq
}
1138
 
1139
void    usage(void) {
1140
        fprintf(stderr,
1141
"USAGE:\tfftgen [-f <size>] [-d dir] [-c cbits] [-n nbits] [-m mxbits] [-s01]\n"
1142
// "\tfftgen -i\n"
1143
"\t-c <cbits>\tCauses all internal complex coefficients to be\n"
1144
"\t\tlonger than the corresponding data bits, to help avoid\n"
1145
"\t\tcoefficient truncation errors.\n"
1146
"\t-d <dir>\tPlaces all of the generated verilog files into <dir>.\n"
1147
"\t-f <size>\tSets the size of the FFT as the number of complex\n"
1148
"\t\tsamples input to the transform.\n"
1149
"\t-n <nbits>\tSets the number of bits in the twos complement input\n"
1150
"\t\tto the FFT routine.\n"
1151
"\t-m <mxbits>\tSets the maximum bit width that the FFT should ever\n"
1152
"\t\tproduce.  Internal values greater than this value will be\n"
1153
"\t\ttruncated to this value.\n"
1154
"\t-s\tSkip the final bit reversal stage.  This is useful in\n"
1155
"\t\talgorithms that need to apply a filter without needing to do\n"
1156
"\t\tbin shifting, as these algorithms can, with this option, just\n"
1157
"\t\tmultiply by a bit reversed correlation sequence and then\n"
1158
"\t\tinverse FFT the (still bit reversed) result.\n"
1159
"\t-S\tInclude the final bit reversal stage (default).\n"
1160
"\t-0\tA forward FFT (default), meaning that the coefficients are\n"
1161
"\t\tgiven by e^{-j 2 pi k/N n }.\n"
1162
"\t-1\tAn inverse FFT, meaning that the coefficients are\n"
1163
"\t\tgiven by e^{ j 2 pi k/N n }.\n");
1164
}
1165
 
1166
// Features still needed:
1167
//      Interactivity.
1168
//      Some number of maximum bits, beyond which we won't accumulate any more.
1169
//      Obviously, the build_stage above.
1170
//      Copying the files of interest into the fft-core directory, from
1171
//              whatever directory this file is run out of.
1172
int main(int argc, char **argv) {
1173
        int     fftsize = -1, lgsize = -1;
1174
        int     nbitsin = 16, xtracbits = 4;
1175
        int     nbitsout, maxbitsout = -1;
1176
        bool    bitreverse = true, inverse=false, interactive = false,
1177
                verbose_flag = false;
1178
        FILE    *vmain;
1179 14 dgisselq
        std::string     coredir = "fft-core", cmdline = "";
1180 2 dgisselq
 
1181
        if (argc <= 1)
1182
                usage();
1183
 
1184 14 dgisselq
        cmdline = argv[0];
1185 2 dgisselq
        for(int argn=1; argn<argc; argn++) {
1186 14 dgisselq
                cmdline += " ";
1187
                cmdline += argv[argn];
1188
        }
1189
 
1190
        for(int argn=1; argn<argc; argn++) {
1191 2 dgisselq
                if ('-' == argv[argn][0]) {
1192
                        for(int j=1; (argv[argn][j])&&(j<100); j++) {
1193
                                switch(argv[argn][j]) {
1194
                                        case '0':
1195
                                                inverse = false;
1196
                                                break;
1197
                                        case '1':
1198
                                                inverse = true;
1199
                                                break;
1200
                                        case 'c':
1201
                                                if (argn+1 >= argc) {
1202
                                                        printf("No extra number of coefficient bits given\n");
1203
                                                        usage(); exit(-1);
1204
                                                }
1205
                                                xtracbits = atoi(argv[++argn]);
1206
                                                j+= 200;
1207
                                                break;
1208
                                        case 'd':
1209
                                                if (argn+1 >= argc) {
1210
                                                        printf("No extra number of coefficient bits given\n");
1211
                                                        usage(); exit(-1);
1212
                                                }
1213 14 dgisselq
                                                coredir = argv[++argn];
1214 2 dgisselq
                                                j += 200;
1215
                                                break;
1216
                                        case 'f':
1217
                                                if (argn+1 >= argc) {
1218
                                                        printf("No FFT Size given\n");
1219
                                                        usage(); exit(-1);
1220
                                                }
1221
                                                fftsize = atoi(argv[++argn]);
1222
                                                { int sln = strlen(argv[argn]);
1223
                                                if (!isdigit(argv[argn][sln-1])){
1224
                                                        switch(argv[argn][sln-1]) {
1225
                                                        case 'k': case 'K':
1226
                                                                fftsize <<= 10;
1227
                                                                break;
1228
                                                        case 'm': case 'M':
1229
                                                                fftsize <<= 20;
1230
                                                                break;
1231
                                                        case 'g': case 'G':
1232
                                                                fftsize <<= 30;
1233
                                                                break;
1234
                                                        default:
1235
                                                                printf("Unknown FFT size, %s\n", argv[argn]);
1236
                                                                exit(-1);
1237
                                                        }
1238
                                                }}
1239
                                                j += 200;
1240
                                                break;
1241
                                        case 'h':
1242
                                                usage();
1243
                                                exit(0);
1244
                                                break;
1245
                                        case 'i':
1246
                                                interactive = true;
1247
                                                break;
1248
                                        case 'm':
1249
                                                if (argn+1 >= argc) {
1250
                                                        printf("No maximum output bit value given\n");
1251
                                                        exit(-1);
1252
                                                }
1253
                                                maxbitsout = atoi(argv[++argn]);
1254
                                                j += 200;
1255
                                                break;
1256
                                        case 'n':
1257
                                                if (argn+1 >= argc) {
1258
                                                        printf("No input bit size given\n");
1259
                                                        exit(-1);
1260
                                                }
1261
                                                nbitsin = atoi(argv[++argn]);
1262
                                                j += 200;
1263
                                                break;
1264
                                        case 'S':
1265
                                                bitreverse = true;
1266
                                                break;
1267
                                        case 's':
1268
                                                bitreverse = false;
1269
                                                break;
1270
                                        case 'v':
1271
                                                verbose_flag = true;
1272
                                                break;
1273
                                        default:
1274
                                                printf("Unknown argument, -%c\n", argv[argn][j]);
1275
                                                usage();
1276
                                                exit(-1);
1277
                                }
1278
                        }
1279
                } else {
1280
                        printf("Unrecognized argument, %s\n", argv[argn]);
1281
                        usage();
1282
                        exit(-1);
1283
                }
1284
        }
1285
 
1286
        if ((lgsize < 0)&&(fftsize > 1)) {
1287
                for(lgsize=1; (1<<lgsize) < fftsize; lgsize++)
1288
                        ;
1289
        }
1290
 
1291
        if ((fftsize <= 0)||(nbitsin < 1)||(nbitsin>48)) {
1292
                printf("INVALID PARAMETERS!!!!\n");
1293
                exit(-1);
1294
        }
1295
 
1296
 
1297
        if (nextlg(fftsize) != fftsize) {
1298
                fprintf(stderr, "ERR: FFTSize (%d) *must* be a power of two\n",
1299
                                fftsize);
1300
                exit(-1);
1301
        } else if (fftsize < 2) {
1302
                fprintf(stderr, "ERR: Minimum FFTSize is 2, not %d\n",
1303
                                fftsize);
1304
                if (fftsize == 1) {
1305
                        fprintf(stderr, "You do realize that a 1 point FFT makes very little sense\n");
1306
                        fprintf(stderr, "in an FFT operation that handles two samples per clock?\n");
1307
                        fprintf(stderr, "If you really need to do an FFT of this size, the output\n");
1308
                        fprintf(stderr, "can be connected straight to the input.\n");
1309
                } else {
1310
                        fprintf(stderr, "Indeed, a size of %d doesn\'t make much sense to me at all.\n", fftsize);
1311
                        fprintf(stderr, "Is such an operation even defined?\n");
1312
                }
1313
                exit(-1);
1314
        }
1315
 
1316
        // Calculate how many output bits we'll have, and what the log
1317
        // based two size of our FFT is.
1318
        {
1319
                int     tmp_size = fftsize;
1320
 
1321
                // The first stage always accumulates one bit, regardless
1322
                // of whether you need to or not.
1323
                nbitsout = nbitsin + 1;
1324
                tmp_size >>= 1;
1325
 
1326
                while(tmp_size > 4) {
1327
                        nbitsout += 1;
1328
                        tmp_size >>= 2;
1329
                }
1330
 
1331
                if (tmp_size > 1)
1332
                        nbitsout ++;
1333
 
1334
                if (fftsize <= 2)
1335
                        bitreverse = false;
1336
        } if ((maxbitsout > 0)&&(nbitsout > maxbitsout))
1337
                nbitsout = maxbitsout;
1338
 
1339
 
1340
        {
1341
                struct stat     sbuf;
1342 14 dgisselq
                if (lstat(coredir.c_str(), &sbuf)==0) {
1343 2 dgisselq
                        if (!S_ISDIR(sbuf.st_mode)) {
1344 14 dgisselq
                                fprintf(stderr, "\'%s\' already exists, and is not a directory!\n", coredir.c_str());
1345 2 dgisselq
                                fprintf(stderr, "I will stop now, lest I overwrite something you care about.\n");
1346
                                fprintf(stderr, "To try again, please remove this file.\n");
1347
                                exit(-1);
1348
                        }
1349
                } else
1350 14 dgisselq
                        mkdir(coredir.c_str(), 0755);
1351
                if (access(coredir.c_str(), X_OK|W_OK) != 0) {
1352
                        fprintf(stderr, "I have no access to the directory \'%s\'.\n", coredir.c_str());
1353 2 dgisselq
                        exit(-1);
1354
                }
1355
        }
1356
 
1357 14 dgisselq
        {
1358
                std::string     fname_string;
1359
 
1360
                fname_string = coredir;
1361
                fname_string += "/";
1362
                if (inverse) fname_string += "i";
1363
                fname_string += "fftmain.v";
1364
 
1365
                vmain = fopen(fname_string.c_str(), "w");
1366
                if (NULL == vmain) {
1367
                        fprintf(stderr, "Could not open \'%s\' for writing\n", fname_string.c_str());
1368
                        perror("Err from O/S:");
1369
                        exit(-1);
1370
                }
1371 2 dgisselq
        }
1372
 
1373
        fprintf(vmain, "/////////////////////////////////////////////////////////////////////////////\n");
1374
        fprintf(vmain, "//\n");
1375
        fprintf(vmain, "// Filename:    %sfftmain.v\n", (inverse)?"i":"");
1376
        fprintf(vmain, "//\n");
1377
        fprintf(vmain, "// Project:     %s\n", prjname);
1378
        fprintf(vmain, "//\n");
1379
        fprintf(vmain, "// Purpose:     This is the main module in the Doubletime FPGA FFT project.\n");
1380
        fprintf(vmain, "//              As such, all other modules are subordinate to this one.\n");
1381
        fprintf(vmain, "//              (I have been reading too much legalese this week ...)\n");
1382
        fprintf(vmain, "//              This module accomplish a fixed size Complex FFT on %d data\n", fftsize);
1383
        fprintf(vmain, "//              points.  The FFT is fully pipelined, and accepts as inputs\n");
1384
        fprintf(vmain, "//              two complex two\'s complement samples per clock.\n");
1385
        fprintf(vmain, "//\n");
1386
        fprintf(vmain, "// Parameters:\n");
1387
        fprintf(vmain, "//      i_clk\tThe clock.  All operations are synchronous with this clock.\n");
1388
        fprintf(vmain, "//\ti_rst\tSynchronous reset, active high.  Setting this line will\n");
1389
        fprintf(vmain, "//\t\t\tforce the reset of all of the internals to this routine.\n");
1390
        fprintf(vmain, "//\t\t\tFurther, following a reset, the o_sync line will go\n");
1391
        fprintf(vmain, "//\t\t\thigh the same time the first output sample is valid.\n");
1392
        fprintf(vmain, "//      i_ce\tA clock enable line.  If this line is set, this module\n");
1393
        fprintf(vmain, "//\t\t\twill accept two complex values as inputs, and produce\n");
1394
        fprintf(vmain, "//\t\t\ttwo (possibly empty) complex values as outputs.\n");
1395
        fprintf(vmain, "//\t\ti_left\tThe first of two complex input samples.  This value\n");
1396
        fprintf(vmain, "//\t\t\tis split into two two\'s complement numbers, of \n");
1397
        fprintf(vmain, "//\t\t\t%d bits each, with the real portion in the high\n", nbitsin);
1398
        fprintf(vmain, "//\t\t\torder bits, and the imaginary portion taking the\n");
1399
        fprintf(vmain, "//\t\t\tbottom %d bits.\n", nbitsin);
1400
        fprintf(vmain, "//\t\ti_right\tThis is the same thing as i_left, only this is the\n");
1401
        fprintf(vmain, "//\t\t\tsecond of two such samples.  Hence, i_left would\n");
1402
        fprintf(vmain, "//\t\t\tcontain input sample zero, i_right would contain\n");
1403
        fprintf(vmain, "//\t\t\tsample one.  On the next clock i_left would contain\n");
1404
        fprintf(vmain, "//\t\t\tinput sample two, i_right number three and so forth.\n");
1405
        fprintf(vmain, "//\t\to_left\tThe first of two output samples, of the same\n");
1406
        fprintf(vmain, "//\t\t\tformat as i_left, only having %d bits for each of\n", nbitsout);
1407
        fprintf(vmain, "//\t\t\tthe real and imaginary components, leading to %d\n", nbitsout*2);
1408
        fprintf(vmain, "//\t\t\tbits total.\n");
1409
        fprintf(vmain, "//\t\to_right\tThe second of two output samples produced each clock.\n");
1410
        fprintf(vmain, "//\t\t\tThis has the same format as o_left.\n");
1411
        fprintf(vmain, "//\t\to_sync\tA one bit output indicating the first valid sample\n");
1412
        fprintf(vmain, "//\t\t\tproduced by this FFT following a reset.  Ever after,\n");
1413
        fprintf(vmain, "//\t\t\tthis will indicate the first sample of an FFT frame.\n");
1414
        fprintf(vmain, "//\n");
1415 14 dgisselq
        fprintf(vmain, "// Arguments:\tThis file was computer generated using the\n");
1416
        fprintf(vmain, "//\t\tfollowing command line:\n");
1417
        fprintf(vmain, "//\n");
1418
        fprintf(vmain, "//\t\t%% %s\n", cmdline.c_str());
1419
        fprintf(vmain, "//\n");
1420 2 dgisselq
        fprintf(vmain, "%s", creator);
1421
        fprintf(vmain, "//\n");
1422
        fprintf(vmain, "%s", cpyleft);
1423
 
1424
 
1425
        fprintf(vmain, "//\n");
1426
        fprintf(vmain, "//\n");
1427
        fprintf(vmain, "module %sfftmain(i_clk, i_rst, i_ce,\n", (inverse)?"i":"");
1428
        fprintf(vmain, "\t\ti_left, i_right,\n");
1429
        fprintf(vmain, "\t\to_left, o_right, o_sync);\n");
1430
        fprintf(vmain, "\tparameter\tIWIDTH=%d, OWIDTH=%d, LGWIDTH=%d;\n", nbitsin, nbitsout, lgsize);
1431
        assert(lgsize > 0);
1432
        fprintf(vmain, "\tinput\t\ti_clk, i_rst, i_ce;\n");
1433
        fprintf(vmain, "\tinput\t\t[(2*IWIDTH-1):0]\ti_left, i_right;\n");
1434
        fprintf(vmain, "\toutput\treg\t[(2*OWIDTH-1):0]\to_left, o_right;\n");
1435
        fprintf(vmain, "\toutput\treg\t\t\to_sync;\n");
1436
        fprintf(vmain, "\n\n");
1437
 
1438
        fprintf(vmain, "\t// Outputs of the FFT, ready for bit reversal.\n");
1439
        fprintf(vmain, "\twire\t[(2*OWIDTH-1):0]\tbr_left, br_right;\n");
1440
        fprintf(vmain, "\n\n");
1441
 
1442
        int     tmp_size = fftsize, lgtmp = lgsize;
1443
        if (fftsize == 2) {
1444
                if (bitreverse) {
1445
                        fprintf(vmain, "\treg\tbr_start;\n");
1446
                        fprintf(vmain, "\talways @(posedge i_clk)\n");
1447
                        fprintf(vmain, "\t\tif (i_rst)\n");
1448
                        fprintf(vmain, "\t\t\tbr_start <= 1'b0;\n");
1449
                        fprintf(vmain, "\t\telse if (i_ce)\n");
1450
                        fprintf(vmain, "\t\t\tbr_start <= 1'b1;\n");
1451
                }
1452
                fprintf(vmain, "\n\n");
1453 6 dgisselq
                fprintf(vmain, "\tdblstage\t#(IWIDTH)\tstage_2(i_clk, i_rst, i_ce,\n");
1454
                fprintf(vmain, "\t\t\t(~i_rst), i_left, i_right, br_left, br_right);\n");
1455 2 dgisselq
                fprintf(vmain, "\n\n");
1456
        } else {
1457
                int     nbits = nbitsin, dropbit=0;
1458
                // Always do a first stage
1459
                fprintf(vmain, "\n\n");
1460
                fprintf(vmain, "\twire\t\tw_s%d, w_os%d;\n", fftsize, fftsize);
1461
                fprintf(vmain, "\twire\t[(2*IWIDTH+1):0]\tw_e%d, w_o%d;\n", fftsize, fftsize);
1462
                fprintf(vmain, "\t%sfftstage_e%d\t#(IWIDTH,IWIDTH+%d,IWIDTH+1,%d,%d,%d,0)\tstage_e%d(i_clk, i_rst, i_ce,\n",
1463
                        (inverse)?"i":"", fftsize,
1464
                        xtracbits,
1465
                        lgsize, lgtmp-2, lgdelay(nbits,xtracbits),
1466
                        fftsize);
1467
                fprintf(vmain, "\t\t\t(~i_rst), i_left, w_e%d, w_s%d);\n", fftsize, fftsize);
1468
                fprintf(vmain, "\t%sfftstage_o%d\t#(IWIDTH,IWIDTH+%d,IWIDTH+1,%d,%d,%d,0)\tstage_o%d(i_clk, i_rst, i_ce,\n",
1469
                        (inverse)?"i":"", fftsize,
1470
                        xtracbits,
1471
                        lgsize, lgtmp-2, lgdelay(nbits,xtracbits),
1472
                        fftsize);
1473 9 dgisselq
                fprintf(vmain, "\t\t\t(~i_rst), i_right, w_o%d, w_os%d);\n", fftsize, fftsize);
1474 2 dgisselq
                fprintf(vmain, "\n\n");
1475
 
1476 14 dgisselq
                {
1477
                        std::string     fname;
1478
                        char    numstr[12];
1479 2 dgisselq
 
1480 14 dgisselq
                        fname = coredir + "/";
1481
                        if (inverse) fname += "i";
1482
                        fname += "fftstage_e";
1483
                        sprintf(numstr, "%d", fftsize);
1484
                        fname += numstr;
1485
                        fname += ".v";
1486
                        build_stage(fname.c_str(), fftsize/2, 0, nbits, inverse, xtracbits);     // Even stage
1487
 
1488
                        fname = coredir + "/";
1489
                        if (inverse) fname += "i";
1490
                        fname += "fftstage_o";
1491
                        sprintf(numstr, "%d", fftsize);
1492
                        fname += numstr;
1493
                        fname += ".v";
1494
                        build_stage(fname.c_str(), fftsize/2, 1, nbits, inverse, xtracbits);    // Odd  stage
1495
                }
1496
 
1497 2 dgisselq
                nbits += 1;     // New number of input bits
1498
                tmp_size >>= 1; lgtmp--;
1499
                dropbit = 0;
1500
                fprintf(vmain, "\n\n");
1501
                while(tmp_size >= 8) {
1502
                        int     obits = nbits+((dropbit)?0:1);
1503
 
1504
                        if ((maxbitsout > 0)&&(obits > maxbitsout))
1505
                                obits = maxbitsout;
1506
 
1507
                        fprintf(vmain, "\twire\t\tw_s%d, w_os%d;\n", tmp_size, tmp_size);
1508
                        fprintf(vmain, "\twire\t[%d:0]\tw_e%d, w_o%d;\n", 2*obits-1, tmp_size, tmp_size);
1509
                        fprintf(vmain, "\t%sfftstage_e%d\t#(%d,%d,%d,%d,%d,%d,%d)\tstage_e%d(i_clk, i_rst, i_ce,\n",
1510
                                (inverse)?"i":"", tmp_size,
1511
                                nbits, nbits+xtracbits, obits,
1512 9 dgisselq
                                lgsize, lgtmp-2, lgdelay(nbits,xtracbits), (dropbit)?0:0,
1513 2 dgisselq
                                tmp_size);
1514
                        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);
1515
                        fprintf(vmain, "\t%sfftstage_o%d\t#(%d,%d,%d,%d,%d,%d,%d)\tstage_o%d(i_clk, i_rst, i_ce,\n",
1516
                                (inverse)?"i":"", tmp_size,
1517
                                nbits, nbits+xtracbits, obits,
1518 9 dgisselq
                                lgsize, lgtmp-2, lgdelay(nbits,xtracbits), (dropbit)?0:0,
1519 2 dgisselq
                                tmp_size);
1520
                        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);
1521
                        fprintf(vmain, "\n\n");
1522
 
1523 14 dgisselq
                        {
1524
                                std::string     fname;
1525
                                char            numstr[12];
1526 2 dgisselq
 
1527 14 dgisselq
                                fname = coredir + "/";
1528
                                if (inverse) fname += "i";
1529
                                fname += "fftstage_e";
1530
                                sprintf(numstr, "%d", tmp_size);
1531
                                fname += numstr;
1532
                                fname += ".v";
1533
                                build_stage(fname.c_str(), tmp_size/2, 0, nbits, inverse, xtracbits);    // Even stage
1534 2 dgisselq
 
1535 14 dgisselq
                                fname = coredir + "/";
1536
                                if (inverse) fname += "i";
1537
                                fname += "fftstage_o";
1538
                                sprintf(numstr, "%d", tmp_size);
1539
                                fname += numstr;
1540
                                fname += ".v";
1541
                                build_stage(fname.c_str(), tmp_size/2, 1, nbits, inverse, xtracbits);   // Odd  stage
1542
                        }
1543
 
1544
 
1545 2 dgisselq
                        dropbit ^= 1;
1546
                        nbits = obits;
1547
                        tmp_size >>= 1; lgtmp--;
1548
                }
1549
 
1550
                if (tmp_size == 4) {
1551
                        int     obits = nbits+((dropbit)?0:1);
1552
 
1553
                        if ((maxbitsout > 0)&&(obits > maxbitsout))
1554
                                obits = maxbitsout;
1555
 
1556
                        fprintf(vmain, "\twire\t\tw_s4, w_os4;\n");
1557
                        fprintf(vmain, "\twire\t[%d:0]\tw_e4, w_o4;\n", 2*obits-1);
1558
                        fprintf(vmain, "\tqtrstage\t#(%d,%d,%d,0,%d,%d)\tstage_e4(i_clk, i_rst, i_ce,\n",
1559 9 dgisselq
                                nbits, obits, lgsize, (inverse)?1:0, (dropbit)?0:0);
1560 6 dgisselq
                        fprintf(vmain, "\t\t\t\t\t\tw_s8, w_e8, w_e4, w_s4);\n");
1561 2 dgisselq
                        fprintf(vmain, "\tqtrstage\t#(%d,%d,%d,1,%d,%d)\tstage_o4(i_clk, i_rst, i_ce,\n",
1562 9 dgisselq
                                nbits, obits, lgsize, (inverse)?1:0, (dropbit)?0:0);
1563 6 dgisselq
                        fprintf(vmain, "\t\t\t\t\t\tw_s8, w_o8, w_o4, w_os4);\n");
1564 2 dgisselq
                        dropbit ^= 1;
1565
                        nbits = obits;
1566
                        tmp_size >>= 1; lgtmp--;
1567
                }
1568
 
1569
                {
1570
                        int obits = nbits+((dropbit)?0:1);
1571
                        if (obits > nbitsout)
1572
                                obits = nbitsout;
1573
                        if ((maxbitsout>0)&&(obits > maxbitsout))
1574
                                obits = maxbitsout;
1575
                        fprintf(vmain, "\twire\t\tw_s2;\n");
1576
                        fprintf(vmain, "\twire\t[%d:0]\tw_e2, w_o2;\n", 2*obits-1);
1577 9 dgisselq
                        fprintf(vmain, "\tdblstage\t#(%d,%d,%d)\tstage_2(i_clk, i_rst, i_ce,\n", nbits, obits,(dropbit)?0:1);
1578 6 dgisselq
                        fprintf(vmain, "\t\t\t\t\tw_s4, w_e4, w_o4, w_e2, w_o2, w_s2);\n");
1579 2 dgisselq
 
1580
                        fprintf(vmain, "\n\n");
1581
                        nbits = obits;
1582
                }
1583
 
1584
                fprintf(vmain, "\t// Prepare for a (potential) bit-reverse stage.\n");
1585
                fprintf(vmain, "\tassign\tbr_left  = w_e2;\n");
1586
                fprintf(vmain, "\tassign\tbr_right = w_o2;\n");
1587
                fprintf(vmain, "\n");
1588
                if (bitreverse) {
1589
                        fprintf(vmain, "\twire\tbr_start;\n");
1590
                        fprintf(vmain, "\treg\tr_br_started;\n");
1591
                        fprintf(vmain, "\t// A delay of one clock here is perfect, as it matches the delay in\n");
1592
                        fprintf(vmain, "\t// our dblstage.\n");
1593
                        fprintf(vmain, "\talways @(posedge i_clk)\n");
1594
                        fprintf(vmain, "\t\tif (i_rst)\n");
1595
                        fprintf(vmain, "\t\t\tr_br_started <= 1'b0;\n");
1596
                        fprintf(vmain, "\t\telse\n");
1597
                        fprintf(vmain, "\t\t\tr_br_started <= r_br_started || w_s4;\n");
1598
                        fprintf(vmain, "\tassign\tbr_start = r_br_started;\n");
1599
                }
1600
        }
1601
 
1602
        fprintf(vmain, "\n");
1603
        fprintf(vmain, "\t// Now for the bit-reversal stage.\n");
1604
        fprintf(vmain, "\twire\tbr_sync;\n");
1605
        fprintf(vmain, "\twire\t[(2*OWIDTH-1):0]\tbr_o_left, br_o_right;\n");
1606
        if (bitreverse) {
1607
                fprintf(vmain, "\tdblreverse\t#(%d,%d)\trevstage(i_clk, i_rst,\n", lgsize, nbitsout);
1608
                fprintf(vmain, "\t\t\t(i_ce & br_start), br_left, br_right,\n");
1609
                fprintf(vmain, "\t\t\tbr_o_left, br_o_right, br_sync);\n");
1610
        } else {
1611
                fprintf(vmain, "\tassign\tbr_o_left  = br_left;\n");
1612
                fprintf(vmain, "\tassign\tbr_o_right = br_right;\n");
1613
                fprintf(vmain, "\tassign\tbr_sync    = w_s2;\n");
1614
        }
1615
 
1616
        fprintf(vmain, "\n\n");
1617
        fprintf(vmain, "\t// Last clock: Register our outputs, we\'re done.\n");
1618
        fprintf(vmain, "\talways @(posedge i_clk)\n");
1619
        fprintf(vmain, "\t\tbegin\n");
1620
        fprintf(vmain, "\t\t\to_left  <= br_o_left;\n");
1621
        fprintf(vmain, "\t\t\to_right <= br_o_right;\n");
1622
        fprintf(vmain, "\t\t\to_sync  <= br_sync;\n");
1623
        fprintf(vmain, "\t\tend\n");
1624
        fprintf(vmain, "\n\n");
1625
        fprintf(vmain, "endmodule\n");
1626
        fclose(vmain);
1627
 
1628 14 dgisselq
        {
1629
                std::string     fname;
1630 2 dgisselq
 
1631 14 dgisselq
                fname = coredir + "/butterfly.v";
1632
                build_butterfly(fname.c_str(), xtracbits);
1633 2 dgisselq
 
1634 14 dgisselq
                fname = coredir + "/shiftaddmpy.v";
1635
                build_multiply(fname.c_str());
1636 2 dgisselq
 
1637 14 dgisselq
                fname = coredir + "/qtrstage.v";
1638
                build_quarters(fname.c_str());
1639 2 dgisselq
 
1640 14 dgisselq
                fname = coredir + "/dblstage.v";
1641
                build_dblstage(fname.c_str());
1642
 
1643
                if (bitreverse) {
1644
                        fname = coredir + "/dblreverse.v";
1645
                        build_dblreverse(fname.c_str());
1646
                }
1647 2 dgisselq
        }
1648
}
1649
 
1650 16 dgisselq
 

powered by: WebSVN 2.1.0

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