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

Subversion Repositories dblclockfft

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

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

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

powered by: WebSVN 2.1.0

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