1 |
36 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
3 |
|
|
// Filename: bldstage.cpp
|
4 |
|
|
//
|
5 |
|
|
// Project: A General Purpose Pipelined FFT Implementation
|
6 |
|
|
//
|
7 |
|
|
// Purpose:
|
8 |
|
|
//
|
9 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
10 |
|
|
// Gisselquist Technology, LLC
|
11 |
|
|
//
|
12 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
13 |
|
|
//
|
14 |
|
|
// Copyright (C) 2015-2018, Gisselquist Technology, LLC
|
15 |
|
|
//
|
16 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
17 |
|
|
// modify it under the terms of the GNU General Public License as published
|
18 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
19 |
|
|
// your option) any later version.
|
20 |
|
|
//
|
21 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
22 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
23 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
24 |
|
|
// for more details.
|
25 |
|
|
//
|
26 |
|
|
// You should have received a copy of the GNU General Public License along
|
27 |
|
|
// with this program. (It's in the $(ROOT)/doc directory, run make with no
|
28 |
|
|
// target there if the PDF file isn't present.) If not, see
|
29 |
|
|
// <http://www.gnu.org/licenses/> for a copy.
|
30 |
|
|
//
|
31 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
32 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
33 |
|
|
//
|
34 |
|
|
//
|
35 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
36 |
|
|
//
|
37 |
|
|
//
|
38 |
|
|
#define _CRT_SECURE_NO_WARNINGS // ms vs 2012 doesn't like fopen
|
39 |
|
|
#include <stdio.h>
|
40 |
|
|
#include <stdlib.h>
|
41 |
|
|
|
42 |
|
|
#ifdef _MSC_VER // added for ms vs compatibility
|
43 |
|
|
|
44 |
|
|
#include <io.h>
|
45 |
|
|
#include <direct.h>
|
46 |
|
|
#define _USE_MATH_DEFINES
|
47 |
|
|
|
48 |
|
|
#else
|
49 |
|
|
// And for G++/Linux environment
|
50 |
|
|
|
51 |
|
|
#include <unistd.h> // Defines the R_OK/W_OK/etc. macros
|
52 |
|
|
#endif
|
53 |
|
|
|
54 |
|
|
#include <string.h>
|
55 |
|
|
#include <string>
|
56 |
|
|
#include <math.h>
|
57 |
|
|
#include <ctype.h>
|
58 |
|
|
#include <assert.h>
|
59 |
|
|
|
60 |
|
|
#include "defaults.h"
|
61 |
|
|
#include "legal.h"
|
62 |
|
|
#include "fftlib.h"
|
63 |
|
|
#include "rounding.h"
|
64 |
|
|
#include "bldstage.h"
|
65 |
|
|
|
66 |
|
|
void build_dblstage(const char *fname, ROUND_T rounding,
|
67 |
|
|
const bool async_reset, const bool dbg) {
|
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 |
|
|
const char *rnd_string;
|
76 |
|
|
if (rounding == RND_TRUNCATE)
|
77 |
|
|
rnd_string = "truncate";
|
78 |
|
|
else if (rounding == RND_FROMZERO)
|
79 |
|
|
rnd_string = "roundfromzero";
|
80 |
|
|
else if (rounding == RND_HALFUP)
|
81 |
|
|
rnd_string = "roundhalfup";
|
82 |
|
|
else
|
83 |
|
|
rnd_string = "convround";
|
84 |
|
|
|
85 |
|
|
std::string resetw("i_reset");
|
86 |
|
|
if (async_reset)
|
87 |
|
|
resetw = std::string("i_areset_n");
|
88 |
|
|
|
89 |
|
|
|
90 |
|
|
fprintf(fp,
|
91 |
|
|
SLASHLINE
|
92 |
|
|
"//\n"
|
93 |
|
|
"// Filename:\tlaststage%s.v\n"
|
94 |
|
|
"//\n"
|
95 |
|
|
"// Project:\t%s\n"
|
96 |
|
|
"//\n"
|
97 |
|
|
"// Purpose:\tThis is part of an FPGA implementation that will process\n"
|
98 |
|
|
"// the final stage of a decimate-in-frequency FFT, running\n"
|
99 |
|
|
"// through the data at two samples per clock. If you notice from the\n"
|
100 |
|
|
"// derivation of an FFT, the only time both even and odd samples are\n"
|
101 |
|
|
"// used at the same time is in this stage. Therefore, other than this\n"
|
102 |
|
|
"// stage and these twiddles, all of the other stages can run two stages\n"
|
103 |
|
|
"// at a time at one sample per clock.\n"
|
104 |
|
|
"//\n"
|
105 |
|
|
"// Operation:\n"
|
106 |
|
|
"// Given a stream of values, operate upon them as though they were\n"
|
107 |
|
|
"// value pairs, x[2n] and x[2n+1]. The stream begins when n=0, and ends\n"
|
108 |
|
|
"// when n=1. When the first x[0] value enters, the synchronization\n"
|
109 |
|
|
"// input, i_sync, must be true as well.\n"
|
110 |
|
|
"//\n"
|
111 |
|
|
"// For this stream, produce outputs\n"
|
112 |
|
|
"// y[2n ] = x[2n] + x[2n+1], and\n"
|
113 |
|
|
"// y[2n+1] = x[2n] - x[2n+1]\n"
|
114 |
|
|
"//\n"
|
115 |
|
|
"// When y[0] is output, a synchronization bit o_sync will be true as\n"
|
116 |
|
|
"// well, otherwise it will be zero.\n"
|
117 |
|
|
"//\n"
|
118 |
|
|
"//\n"
|
119 |
|
|
"// In this implementation, the output is valid one clock after the input\n"
|
120 |
|
|
"// is valid. The output also accumulates one bit above and beyond the\n"
|
121 |
|
|
"// number of bits in the input.\n"
|
122 |
|
|
"//\n"
|
123 |
|
|
"// i_clk A system clock\n", (dbg)?"_dbg":"", prjname);
|
124 |
|
|
if (async_reset)
|
125 |
|
|
fprintf(fp,
|
126 |
|
|
"// i_areset_n An active low asynchronous reset\n");
|
127 |
|
|
else
|
128 |
|
|
fprintf(fp,
|
129 |
|
|
"// i_reset A synchronous reset\n");
|
130 |
|
|
|
131 |
|
|
fprintf(fp,
|
132 |
|
|
"// i_ce Circuit enable--nothing happens unless this line is high\n"
|
133 |
|
|
"// i_sync A synchronization signal, high once per FFT at the start\n"
|
134 |
|
|
"// i_left The first (even) complex sample input. The higher order\n"
|
135 |
|
|
"// bits contain the real portion, low order bits the\n"
|
136 |
|
|
"// imaginary portion, all in two\'s complement.\n"
|
137 |
|
|
"// i_right The next (odd) complex sample input, same format as\n"
|
138 |
|
|
"// i_left.\n"
|
139 |
|
|
"// o_left The first (even) complex output.\n"
|
140 |
|
|
"// o_right The next (odd) complex output.\n"
|
141 |
|
|
"// o_sync Output synchronization signal.\n"
|
142 |
|
|
"//\n%s"
|
143 |
|
|
"//\n", creator);
|
144 |
|
|
|
145 |
|
|
fprintf(fp, "%s", cpyleft);
|
146 |
|
|
fprintf(fp, "//\n//\n`default_nettype\tnone\n//\n");
|
147 |
|
|
fprintf(fp,
|
148 |
|
|
"module\tlaststage%s(i_clk, %s, i_ce, i_sync, i_left, i_right, o_left, o_right, o_sync%s);\n"
|
149 |
|
|
"\tparameter\tIWIDTH=%d,OWIDTH=IWIDTH+1, SHIFT=%d;\n"
|
150 |
|
|
"\tinput\t\ti_clk, %s, i_ce, i_sync;\n"
|
151 |
|
|
"\tinput\t\t[(2*IWIDTH-1):0]\ti_left, i_right;\n"
|
152 |
|
|
"\toutput\treg\t[(2*OWIDTH-1):0]\to_left, o_right;\n"
|
153 |
|
|
"\toutput\treg\t\t\to_sync;\n"
|
154 |
|
|
"\n", (dbg)?"_dbg":"", resetw.c_str(), (dbg)?", o_dbg":"",
|
155 |
|
|
TST_DBLSTAGE_IWIDTH, TST_DBLSTAGE_SHIFT,
|
156 |
|
|
resetw.c_str());
|
157 |
|
|
|
158 |
|
|
if (dbg) { fprintf(fp, "\toutput\twire\t[33:0]\t\t\to_dbg;\n"
|
159 |
|
|
"\tassign\to_dbg = { ((o_sync)&&(i_ce)), i_ce, o_left[(2*OWIDTH-1):(2*OWIDTH-16)],\n"
|
160 |
|
|
"\t\t\t\t\to_left[(OWIDTH-1):(OWIDTH-16)] };\n"
|
161 |
|
|
"\n");
|
162 |
|
|
}
|
163 |
|
|
fprintf(fp,
|
164 |
|
|
"\twire\tsigned\t[(IWIDTH-1):0]\ti_in_0r, i_in_0i, i_in_1r, i_in_1i;\n"
|
165 |
|
|
"\tassign\ti_in_0r = i_left[(2*IWIDTH-1):(IWIDTH)];\n"
|
166 |
|
|
"\tassign\ti_in_0i = i_left[(IWIDTH-1):0];\n"
|
167 |
|
|
"\tassign\ti_in_1r = i_right[(2*IWIDTH-1):(IWIDTH)];\n"
|
168 |
|
|
"\tassign\ti_in_1i = i_right[(IWIDTH-1):0];\n"
|
169 |
|
|
"\twire\t[(OWIDTH-1):0]\t\to_out_0r, o_out_0i,\n"
|
170 |
|
|
"\t\t\t\t\to_out_1r, o_out_1i;\n"
|
171 |
|
|
"\n"
|
172 |
|
|
"\n"
|
173 |
|
|
"\t// Handle a potential rounding situation, when IWIDTH>=OWIDTH.\n"
|
174 |
|
|
"\n"
|
175 |
|
|
"\n");
|
176 |
|
|
fprintf(fp,
|
177 |
|
|
"\n"
|
178 |
|
|
"\t// As with any register connected to the sync pulse, these must\n"
|
179 |
|
|
"\t// have initial values and be reset on the %s signal.\n"
|
180 |
|
|
"\t// Other data values need only restrict their updates to i_ce\n"
|
181 |
|
|
"\t// enabled clocks, but sync\'s must obey resets and initial\n"
|
182 |
|
|
"\t// conditions as well.\n"
|
183 |
|
|
"\treg\trnd_sync, r_sync;\n"
|
184 |
|
|
"\n"
|
185 |
|
|
"\tinitial\trnd_sync = 1\'b0; // Sync into rounding\n"
|
186 |
|
|
"\tinitial\tr_sync = 1\'b0; // Sync coming out\n",
|
187 |
|
|
resetw.c_str());
|
188 |
|
|
if (async_reset)
|
189 |
|
|
fprintf(fp, "\talways @(posedge i_clk, negdge i_areset_n)\n\t\tif (!i_areset_n)\n");
|
190 |
|
|
else
|
191 |
|
|
fprintf(fp, "\talways @(posedge i_clk)\n\t\tif (i_reset)\n");
|
192 |
|
|
fprintf(fp,
|
193 |
|
|
"\t\tbegin\n"
|
194 |
|
|
"\t\t\trnd_sync <= 1\'b0;\n"
|
195 |
|
|
"\t\t\tr_sync <= 1\'b0;\n"
|
196 |
|
|
"\t\tend else if (i_ce)\n"
|
197 |
|
|
"\t\tbegin\n"
|
198 |
|
|
"\t\t\trnd_sync <= i_sync;\n"
|
199 |
|
|
"\t\t\tr_sync <= rnd_sync;\n"
|
200 |
|
|
"\t\tend\n"
|
201 |
|
|
"\n"
|
202 |
|
|
"\t// As with other variables, these are really only updated when in\n"
|
203 |
|
|
"\t// the processing pipeline, after the first i_sync. However, to\n"
|
204 |
|
|
"\t// eliminate as much unnecessary logic as possible, we toggle\n"
|
205 |
|
|
"\t// these any time the i_ce line is enabled, and don\'t reset.\n"
|
206 |
|
|
"\t// them on %s.\n", resetw.c_str());
|
207 |
|
|
fprintf(fp,
|
208 |
|
|
"\t// Don't forget that we accumulate a bit by adding two values\n"
|
209 |
|
|
"\t// together. Therefore our intermediate value must have one more\n"
|
210 |
|
|
"\t// bit than the two originals.\n"
|
211 |
|
|
"\treg\tsigned\t[(IWIDTH):0]\trnd_in_0r, rnd_in_0i;\n"
|
212 |
|
|
"\treg\tsigned\t[(IWIDTH):0]\trnd_in_1r, rnd_in_1i;\n\n"
|
213 |
|
|
"\talways @(posedge i_clk)\n"
|
214 |
|
|
"\t\tif (i_ce)\n"
|
215 |
|
|
"\t\tbegin\n"
|
216 |
|
|
"\t\t\t//\n"
|
217 |
|
|
"\t\t\trnd_in_0r <= i_in_0r + i_in_1r;\n"
|
218 |
|
|
"\t\t\trnd_in_0i <= i_in_0i + i_in_1i;\n"
|
219 |
|
|
"\t\t\t//\n"
|
220 |
|
|
"\t\t\trnd_in_1r <= i_in_0r - i_in_1r;\n"
|
221 |
|
|
"\t\t\trnd_in_1i <= i_in_0i - i_in_1i;\n"
|
222 |
|
|
"\t\t\t//\n"
|
223 |
|
|
"\t\tend\n"
|
224 |
|
|
"\n");
|
225 |
|
|
fprintf(fp,
|
226 |
|
|
"\t%s #(IWIDTH+1,OWIDTH,SHIFT) do_rnd_0r(i_clk, i_ce,\n"
|
227 |
|
|
"\t\t\t\t\t\t\trnd_in_0r, o_out_0r);\n\n", rnd_string);
|
228 |
|
|
fprintf(fp,
|
229 |
|
|
"\t%s #(IWIDTH+1,OWIDTH,SHIFT) do_rnd_0i(i_clk, i_ce,\n"
|
230 |
|
|
"\t\t\t\t\t\t\trnd_in_0i, o_out_0i);\n\n", rnd_string);
|
231 |
|
|
fprintf(fp,
|
232 |
|
|
"\t%s #(IWIDTH+1,OWIDTH,SHIFT) do_rnd_1r(i_clk, i_ce,\n"
|
233 |
|
|
"\t\t\t\t\t\t\trnd_in_1r, o_out_1r);\n\n", rnd_string);
|
234 |
|
|
fprintf(fp,
|
235 |
|
|
"\t%s #(IWIDTH+1,OWIDTH,SHIFT) do_rnd_1i(i_clk, i_ce,\n"
|
236 |
|
|
"\t\t\t\t\t\t\trnd_in_1i, o_out_1i);\n\n", rnd_string);
|
237 |
|
|
|
238 |
|
|
fprintf(fp, "\n"
|
239 |
|
|
"\t// Prior versions of this routine did not include the extra\n"
|
240 |
|
|
"\t// clock and register/flip-flops that this routine requires.\n"
|
241 |
|
|
"\t// These are placed in here to correct a bug in Verilator, that\n"
|
242 |
|
|
"\t// otherwise struggles. (Hopefully this will fix the problem ...)\n"
|
243 |
|
|
"\talways @(posedge i_clk)\n"
|
244 |
|
|
"\t\tif (i_ce)\n"
|
245 |
|
|
"\t\tbegin\n"
|
246 |
|
|
"\t\t\to_left <= { o_out_0r, o_out_0i };\n"
|
247 |
|
|
"\t\t\to_right <= { o_out_1r, o_out_1i };\n"
|
248 |
|
|
"\t\tend\n"
|
249 |
|
|
"\n"
|
250 |
|
|
"\tinitial\to_sync = 1\'b0; // Final sync coming out of module\n");
|
251 |
|
|
if (async_reset)
|
252 |
|
|
fprintf(fp, "\talways @(posedge i_clk, negdge i_areset_n)\n\t\tif (!i_areset_n)\n");
|
253 |
|
|
else
|
254 |
|
|
fprintf(fp, "\talways @(posedge i_clk)\n\t\tif (i_reset)\n");
|
255 |
|
|
fprintf(fp,
|
256 |
|
|
"\t\t\to_sync <= 1\'b0;\n"
|
257 |
|
|
"\t\telse if (i_ce)\n"
|
258 |
|
|
"\t\t\to_sync <= r_sync;\n"
|
259 |
|
|
"\n"
|
260 |
|
|
"endmodule\n");
|
261 |
|
|
fclose(fp);
|
262 |
|
|
}
|
263 |
|
|
|
264 |
|
|
void build_stage(const char *fname,
|
265 |
|
|
int stage, int nwide, int offset,
|
266 |
|
|
int nbits, int xtra, int ckpce,
|
267 |
|
|
const bool async_reset, const bool dbg) {
|
268 |
|
|
FILE *fstage = fopen(fname, "w");
|
269 |
|
|
int cbits = nbits + xtra;
|
270 |
|
|
|
271 |
|
|
std::string resetw("i_reset");
|
272 |
|
|
if (async_reset)
|
273 |
|
|
resetw = std::string("i_areset_n");
|
274 |
|
|
|
275 |
|
|
if (((unsigned)cbits * 2u) >= sizeof(long long)*8) {
|
276 |
|
|
fprintf(stderr, "ERROR: CMEM Coefficient precision requested overflows long long data type.\n");
|
277 |
|
|
exit(-1);
|
278 |
|
|
}
|
279 |
|
|
|
280 |
|
|
if (fstage == NULL) {
|
281 |
|
|
fprintf(stderr, "ERROR: Could not open %s for writing!\n", fname);
|
282 |
|
|
perror("O/S Err was:");
|
283 |
|
|
fprintf(stderr, "Attempting to continue, but this file will be missing.\n");
|
284 |
|
|
return;
|
285 |
|
|
}
|
286 |
|
|
|
287 |
|
|
fprintf(fstage,
|
288 |
|
|
SLASHLINE
|
289 |
|
|
"//\n"
|
290 |
|
|
"// Filename:\tfftstage%s.v\n"
|
291 |
|
|
"//\n"
|
292 |
|
|
"// Project:\t%s\n"
|
293 |
|
|
"//\n"
|
294 |
|
|
"// Purpose:\tThis file is (almost) a Verilog source file. It is meant to\n"
|
295 |
|
|
"// be used by a FFT core compiler to generate FFTs which may be\n"
|
296 |
|
|
"// used as part of an FFT core. Specifically, this file encapsulates\n"
|
297 |
|
|
"// the options of an FFT-stage. For any 2^N length FFT, there shall be\n"
|
298 |
|
|
"// (N-1) of these stages.\n"
|
299 |
|
|
"//\n"
|
300 |
|
|
"//\n"
|
301 |
|
|
"// Operation:\n"
|
302 |
|
|
"// Given a stream of values, operate upon them as though they were\n"
|
303 |
|
|
"// value pairs, x[n] and x[n+N/2]. The stream begins when n=0, and ends\n"
|
304 |
|
|
"// when n=N/2-1 (i.e. there's a full set of N values). When the value\n"
|
305 |
|
|
"// x[0] enters, the synchronization input, i_sync, must be true as well.\n"
|
306 |
|
|
"//\n"
|
307 |
|
|
"// For this stream, produce outputs\n"
|
308 |
|
|
"// y[n ] = x[n] + x[n+N/2], and\n"
|
309 |
|
|
"// y[n+N/2] = (x[n] - x[n+N/2]) * c[n],\n"
|
310 |
|
|
"// where c[n] is a complex coefficient found in the\n"
|
311 |
|
|
"// external memory file COEFFILE.\n"
|
312 |
|
|
"// When y[0] is output, a synchronization bit o_sync will be true as\n"
|
313 |
|
|
"// well, otherwise it will be zero.\n"
|
314 |
|
|
"//\n"
|
315 |
|
|
"// Most of the work to do this is done within the butterfly, whether the\n"
|
316 |
|
|
"// hardware accelerated butterfly (uses a DSP) or not.\n"
|
317 |
|
|
"//\n%s"
|
318 |
|
|
"//\n",
|
319 |
|
|
(dbg)?"_dbg":"", prjname, creator);
|
320 |
|
|
fprintf(fstage, "%s", cpyleft);
|
321 |
|
|
fprintf(fstage, "//\n//\n`default_nettype\tnone\n//\n");
|
322 |
|
|
fprintf(fstage, "module\tfftstage%s(i_clk, %s, i_ce, i_sync, i_data, o_data, o_sync%s);\n",
|
323 |
|
|
(dbg)?"_dbg":"", resetw.c_str(),
|
324 |
|
|
(dbg)?", o_dbg":"");
|
325 |
|
|
// These parameter values are useless at this point--they are to be
|
326 |
|
|
// replaced by the parameter values in the calling program. Only
|
327 |
|
|
// problem is, the CWIDTH needs to match exactly!
|
328 |
|
|
fprintf(fstage, "\tparameter\tIWIDTH=%d,CWIDTH=%d,OWIDTH=%d;\n",
|
329 |
|
|
nbits, 20, nbits+1); // 20, not cbits, since the tb depends upon it
|
330 |
|
|
fprintf(fstage,
|
331 |
|
|
"\t// Parameters specific to the core that should be changed when this\n"
|
332 |
|
|
"\t// core is built ... Note that the minimum LGSPAN (the base two log\n"
|
333 |
|
|
"\t// of the span, or the base two log of the current FFT size) is 3.\n"
|
334 |
|
|
"\t// Smaller spans (i.e. the span of 2) must use the dbl laststage module.\n"
|
335 |
|
|
"\tparameter\tLGWIDTH=%d, LGSPAN=%d, BFLYSHIFT=0;\n"
|
336 |
|
|
"\tparameter\t[0:0] OPT_HWMPY = 1;\n",
|
337 |
|
|
lgval(stage), (nwide <= 1) ? lgval(stage)-1 : lgval(stage)-2);
|
338 |
|
|
fprintf(fstage,
|
339 |
|
|
"\t// Clocks per CE. If your incoming data rate is less than 50%% of your\n"
|
340 |
|
|
"\t// clock speed, you can set CKPCE to 2\'b10, make sure there's at least\n"
|
341 |
|
|
"\t// one clock between cycles when i_ce is high, and then use two\n"
|
342 |
|
|
"\t// multiplies instead of three. Setting CKPCE to 2\'b11, and insisting\n"
|
343 |
|
|
"\t// on at least two clocks with i_ce low between cycles with i_ce high,\n"
|
344 |
|
|
"\t// then the hardware optimized butterfly code will used one multiply\n"
|
345 |
|
|
"\t// instead of two.\n"
|
346 |
|
|
"\tparameter\t CKPCE = %d;\n", ckpce);
|
347 |
|
|
|
348 |
|
|
fprintf(fstage,
|
349 |
|
|
"\t// The COEFFILE parameter contains the name of the file containing the\n"
|
350 |
|
|
"\t// FFT twiddle factors\n");
|
351 |
|
|
if (nwide == 2) {
|
352 |
|
|
fprintf(fstage, "\tparameter\tCOEFFILE=\"cmem_%c%d.hex\";\n",
|
353 |
|
|
(offset)?'o':'e', stage*2);
|
354 |
|
|
} else
|
355 |
|
|
fprintf(fstage, "\tparameter\tCOEFFILE=\"cmem_%d.hex\";\n",
|
356 |
|
|
stage);
|
357 |
|
|
|
358 |
|
|
fprintf(fstage,"\n"
|
359 |
|
|
"`ifdef VERILATOR\n"
|
360 |
|
|
"\tparameter [0:0] ZERO_ON_IDLE = 1'b0;\n"
|
361 |
|
|
"`else\n"
|
362 |
|
|
"\tlocalparam [0:0] ZERO_ON_IDLE = 1'b0;\n"
|
363 |
|
|
"`endif // VERILATOR\n\n");
|
364 |
|
|
|
365 |
|
|
fprintf(fstage,
|
366 |
|
|
"\tinput i_clk, %s, i_ce, i_sync;\n"
|
367 |
|
|
"\tinput [(2*IWIDTH-1):0] i_data;\n"
|
368 |
|
|
"\toutput reg [(2*OWIDTH-1):0] o_data;\n"
|
369 |
|
|
"\toutput reg o_sync;\n"
|
370 |
|
|
"\n", resetw.c_str());
|
371 |
|
|
if (dbg) { fprintf(fstage, "\toutput\twire\t[33:0]\t\t\to_dbg;\n"
|
372 |
|
|
"\tassign\to_dbg = { ((o_sync)&&(i_ce)), i_ce, o_data[(2*OWIDTH-1):(2*OWIDTH-16)],\n"
|
373 |
|
|
"\t\t\t\t\to_data[(OWIDTH-1):(OWIDTH-16)] };\n"
|
374 |
|
|
"\n");
|
375 |
|
|
}
|
376 |
|
|
fprintf(fstage,
|
377 |
|
|
"\treg wait_for_sync;\n"
|
378 |
|
|
"\treg [(2*IWIDTH-1):0] ib_a, ib_b;\n"
|
379 |
|
|
"\treg [(2*CWIDTH-1):0] ib_c;\n"
|
380 |
|
|
"\treg ib_sync;\n"
|
381 |
|
|
"\n"
|
382 |
|
|
"\treg b_started;\n"
|
383 |
|
|
"\twire ob_sync;\n"
|
384 |
|
|
"\twire [(2*OWIDTH-1):0]\tob_a, ob_b;\n");
|
385 |
|
|
fprintf(fstage,
|
386 |
|
|
"\n"
|
387 |
|
|
"\t// cmem is defined as an array of real and complex values,\n"
|
388 |
|
|
"\t// where the top CWIDTH bits are the real value and the bottom\n"
|
389 |
|
|
"\t// CWIDTH bits are the imaginary value.\n"
|
390 |
|
|
"\t//\n"
|
391 |
|
|
"\t// cmem[i] = { (2^(CWIDTH-2)) * cos(2*pi*i/(2^LGWIDTH)),\n"
|
392 |
|
|
"\t// (2^(CWIDTH-2)) * sin(2*pi*i/(2^LGWIDTH)) };\n"
|
393 |
|
|
"\t//\n"
|
394 |
|
|
"\treg [(2*CWIDTH-1):0] cmem [0:((1<<LGSPAN)-1)];\n"
|
395 |
|
|
"\tinitial\t$readmemh(COEFFILE,cmem);\n\n");
|
396 |
|
|
|
397 |
|
|
// gen_coeff_file(coredir, fname, stage, cbits, nwide, offset, inv);
|
398 |
|
|
|
399 |
|
|
fprintf(fstage,
|
400 |
|
|
"\treg [(LGSPAN):0] iaddr;\n"
|
401 |
|
|
"\treg [(2*IWIDTH-1):0] imem [0:((1<<LGSPAN)-1)];\n"
|
402 |
|
|
"\n"
|
403 |
|
|
"\treg [LGSPAN:0] oB;\n"
|
404 |
|
|
"\treg [(2*OWIDTH-1):0] omem [0:((1<<LGSPAN)-1)];\n"
|
405 |
|
|
"\n"
|
406 |
|
|
"\tinitial wait_for_sync = 1\'b1;\n"
|
407 |
|
|
"\tinitial iaddr = 0;\n");
|
408 |
|
|
if (async_reset)
|
409 |
|
|
fprintf(fstage, "\talways @(posedge i_clk, negedge i_areset_n)\n\t\tif (!i_areset_n)\n");
|
410 |
|
|
else
|
411 |
|
|
fprintf(fstage, "\talways @(posedge i_clk)\n\t\tif (i_reset)\n");
|
412 |
|
|
|
413 |
|
|
fprintf(fstage,
|
414 |
|
|
"\tbegin\n"
|
415 |
|
|
"\t\t\twait_for_sync <= 1\'b1;\n"
|
416 |
|
|
"\t\t\tiaddr <= 0;\n"
|
417 |
|
|
"\tend else if ((i_ce)&&((!wait_for_sync)||(i_sync)))\n"
|
418 |
|
|
"\tbegin\n"
|
419 |
|
|
"\t\t//\n"
|
420 |
|
|
"\t\t// First step: Record what we\'re not ready to use yet\n"
|
421 |
|
|
"\t\t//\n"
|
422 |
|
|
"\t\tiaddr <= iaddr + { {(LGSPAN){1\'b0}}, 1\'b1 };\n"
|
423 |
|
|
"\t\twait_for_sync <= 1\'b0;\n"
|
424 |
|
|
"\tend\n"
|
425 |
|
|
"\talways @(posedge i_clk) // Need to make certain here that we don\'t read\n"
|
426 |
|
|
"\tif ((i_ce)&&(!iaddr[LGSPAN])) // and write the same address on\n"
|
427 |
|
|
"\t\timem[iaddr[(LGSPAN-1):0]] <= i_data; // the same clk\n"
|
428 |
|
|
"\n");
|
429 |
|
|
|
430 |
|
|
fprintf(fstage,
|
431 |
|
|
"\t//\n"
|
432 |
|
|
"\t// Now, we have all the inputs, so let\'s feed the butterfly\n"
|
433 |
|
|
"\t//\n"
|
434 |
|
|
"\tinitial ib_sync = 1\'b0;\n");
|
435 |
|
|
if (async_reset)
|
436 |
|
|
fprintf(fstage, "\talways @(posedge i_clk, negedge i_areset_n)\n\tif (!i_areset_n)\n");
|
437 |
|
|
else
|
438 |
|
|
fprintf(fstage, "\talways @(posedge i_clk)\n\tif (i_reset)\n");
|
439 |
|
|
fprintf(fstage,
|
440 |
|
|
"\t\tib_sync <= 1\'b0;\n"
|
441 |
|
|
"\telse if (i_ce)\n"
|
442 |
|
|
"\tbegin\n"
|
443 |
|
|
"\t\t// Set the sync to true on the very first\n"
|
444 |
|
|
"\t\t// valid input in, and hence on the very\n"
|
445 |
|
|
"\t\t// first valid data out per FFT.\n"
|
446 |
|
|
"\t\tib_sync <= (iaddr==(1<<(LGSPAN)));\n"
|
447 |
|
|
"\tend\n\n"
|
448 |
|
|
"\talways\t@(posedge i_clk)\n"
|
449 |
|
|
"\tif (i_ce)\n"
|
450 |
|
|
"\tbegin\n"
|
451 |
|
|
"\t\t// One input from memory, ...\n"
|
452 |
|
|
"\t\tib_a <= imem[iaddr[(LGSPAN-1):0]];\n"
|
453 |
|
|
"\t\t// One input clocked in from the top\n"
|
454 |
|
|
"\t\tib_b <= i_data;\n"
|
455 |
|
|
"\t\t// and the coefficient or twiddle factor\n"
|
456 |
|
|
"\t\tib_c <= cmem[iaddr[(LGSPAN-1):0]];\n"
|
457 |
|
|
"\tend\n\n");
|
458 |
|
|
|
459 |
|
|
fprintf(fstage,
|
460 |
|
|
"\t// The idle register is designed to keep track of when an input\n"
|
461 |
|
|
"\t// to the butterfly is important and going to be used. It's used\n"
|
462 |
|
|
"\t// in a flag following, so that when useful values are placed\n"
|
463 |
|
|
"\t// into the butterfly they'll be non-zero (idle=0), otherwise when\n"
|
464 |
|
|
"\t// the inputs to the butterfly are irrelevant and will be ignored,\n"
|
465 |
|
|
"\t// then (idle=1) those inputs will be set to zero. This\n"
|
466 |
|
|
"\t// functionality is not designed to be used in operation, but only\n"
|
467 |
|
|
"\t// within a Verilator simulation context when chasing a bug.\n"
|
468 |
|
|
"\t// In this limited environment, the non-zero answers will stand\n"
|
469 |
|
|
"\t// in a trace making it easier to highlight a bug.\n"
|
470 |
|
|
"\treg idle;\n"
|
471 |
|
|
"\tgenerate if (ZERO_ON_IDLE)\n"
|
472 |
|
|
"\tbegin\n"
|
473 |
|
|
"\t\tinitial idle = 1;\n"
|
474 |
|
|
"\t\talways @(posedge i_clk)\n"
|
475 |
|
|
"\t\tif (i_reset)\n"
|
476 |
|
|
"\t\t\tidle <= 1\'b1;\n"
|
477 |
|
|
"\t\telse if (i_ce)\n"
|
478 |
|
|
"\t\t\tidle <= (!iaddr[LGSPAN])&&(!wait_for_sync);\n\n"
|
479 |
|
|
"\tend else begin\n\n"
|
480 |
|
|
"\t\talways @(*) idle = 0;\n\n"
|
481 |
|
|
"\tend endgenerate\n\n");
|
482 |
|
|
|
483 |
|
|
fprintf(fstage,
|
484 |
|
|
"\tgenerate if (OPT_HWMPY)\n"
|
485 |
|
|
"\tbegin : HWBFLY\n"
|
486 |
|
|
"\t\thwbfly #(.IWIDTH(IWIDTH),.CWIDTH(CWIDTH),.OWIDTH(OWIDTH),\n"
|
487 |
|
|
"\t\t\t\t.CKPCE(CKPCE), .SHIFT(BFLYSHIFT))\n"
|
488 |
|
|
"\t\t\tbfly(i_clk, %s, i_ce, (idle)?0:ib_c,\n"
|
489 |
|
|
"\t\t\t\t(idle || (!i_ce)) ? 0:ib_a,\n"
|
490 |
|
|
"\t\t\t\t(idle || (!i_ce)) ? 0:ib_b,\n"
|
491 |
|
|
"\t\t\t\t(ib_sync)&&(i_ce),\n"
|
492 |
|
|
"\t\t\t\tob_a, ob_b, ob_sync);\n"
|
493 |
|
|
"\tend else begin : FWBFLY\n"
|
494 |
|
|
"\t\tbutterfly #(.IWIDTH(IWIDTH),.CWIDTH(CWIDTH),.OWIDTH(OWIDTH),\n"
|
495 |
|
|
"\t\t\t\t.CKPCE(CKPCE),.SHIFT(BFLYSHIFT))\n"
|
496 |
|
|
"\t\t\tbfly(i_clk, %s, i_ce,\n"
|
497 |
|
|
"\t\t\t\t\t(idle||(!i_ce))?0:ib_c,\n"
|
498 |
|
|
"\t\t\t\t\t(idle||(!i_ce))?0:ib_a,\n"
|
499 |
|
|
"\t\t\t\t\t(idle||(!i_ce))?0:ib_b,\n"
|
500 |
|
|
"\t\t\t\t\t(ib_sync&&i_ce),\n"
|
501 |
|
|
"\t\t\t\t\tob_a, ob_b, ob_sync);\n"
|
502 |
|
|
"\tend endgenerate\n\n",
|
503 |
|
|
resetw.c_str(), resetw.c_str());
|
504 |
|
|
|
505 |
|
|
fprintf(fstage,
|
506 |
|
|
"\t//\n"
|
507 |
|
|
"\t// Next step: recover the outputs from the butterfly\n"
|
508 |
|
|
"\t//\n"
|
509 |
|
|
"\tinitial oB = 0;\n"
|
510 |
|
|
"\tinitial o_sync = 0;\n"
|
511 |
|
|
"\tinitial b_started = 0;\n");
|
512 |
|
|
if (async_reset)
|
513 |
|
|
fprintf(fstage, "\talways @(posedge i_clk, negedge i_areset_n)\n\t\tif (!i_areset_n)\n");
|
514 |
|
|
else
|
515 |
|
|
fprintf(fstage, "\talways @(posedge i_clk)\n\t\tif (i_reset)\n");
|
516 |
|
|
fprintf(fstage,
|
517 |
|
|
"\tbegin\n"
|
518 |
|
|
"\t\toB <= 0;\n"
|
519 |
|
|
"\t\to_sync <= 0;\n"
|
520 |
|
|
"\t\tb_started <= 0;\n"
|
521 |
|
|
"\tend else if (i_ce)\n"
|
522 |
|
|
"\tbegin\n"
|
523 |
|
|
"\t\to_sync <= (!oB[LGSPAN])?ob_sync : 1\'b0;\n"
|
524 |
|
|
"\t\tif (ob_sync||b_started)\n"
|
525 |
|
|
"\t\t\toB <= oB + { {(LGSPAN){1\'b0}}, 1\'b1 };\n"
|
526 |
|
|
"\t\tif ((ob_sync)&&(!oB[LGSPAN]))\n"
|
527 |
|
|
"\t\t// A butterfly output is available\n"
|
528 |
|
|
"\t\t\tb_started <= 1\'b1;\n"
|
529 |
|
|
"\tend\n\n");
|
530 |
|
|
fprintf(fstage,
|
531 |
|
|
"\treg [(LGSPAN-1):0]\t\tdly_addr;\n"
|
532 |
|
|
"\treg [(2*OWIDTH-1):0]\tdly_value;\n"
|
533 |
|
|
"\talways @(posedge i_clk)\n"
|
534 |
|
|
"\tif (i_ce)\n"
|
535 |
|
|
"\tbegin\n"
|
536 |
|
|
"\t\tdly_addr <= oB[(LGSPAN-1):0];\n"
|
537 |
|
|
"\t\tdly_value <= ob_b;\n"
|
538 |
|
|
"\tend\n"
|
539 |
|
|
"\talways @(posedge i_clk)\n"
|
540 |
|
|
"\tif (i_ce)\n"
|
541 |
|
|
"\t\tomem[dly_addr] <= dly_value;\n"
|
542 |
|
|
"\n");
|
543 |
|
|
fprintf(fstage,
|
544 |
|
|
"\talways @(posedge i_clk)\n"
|
545 |
|
|
"\tif (i_ce)\n"
|
546 |
|
|
"\t\to_data <= (!oB[LGSPAN])?ob_a : omem[oB[(LGSPAN-1):0]];\n"
|
547 |
|
|
"\n");
|
548 |
|
|
fprintf(fstage, "endmodule\n");
|
549 |
|
|
}
|