1 |
36 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
3 |
|
|
// Filename: butterfly.v
|
4 |
|
|
//
|
5 |
|
|
// Project: A General Purpose Pipelined FFT Implementation
|
6 |
|
|
//
|
7 |
|
|
// Purpose: This routine caculates a butterfly for a decimation
|
8 |
|
|
// in frequency version of an FFT. Specifically, given
|
9 |
|
|
// complex Left and Right values together with a coefficient, the output
|
10 |
|
|
// of this routine is given by:
|
11 |
|
|
//
|
12 |
|
|
// L' = L + R
|
13 |
|
|
// R' = (L - R)*C
|
14 |
|
|
//
|
15 |
|
|
// The rest of the junk below handles timing (mostly), to make certain
|
16 |
|
|
// that L' and R' reach the output at the same clock. Further, just to
|
17 |
|
|
// make certain that is the case, an 'aux' input exists. This aux value
|
18 |
|
|
// will come out of this routine synchronized to the values it came in
|
19 |
|
|
// with. (i.e., both L', R', and aux all have the same delay.) Hence,
|
20 |
|
|
// a caller of this routine may set aux on the first input with valid
|
21 |
|
|
// data, and then wait to see aux set on the output to know when to find
|
22 |
|
|
// the first output with valid data.
|
23 |
|
|
//
|
24 |
|
|
// All bits are preserved until the very last clock, where any more bits
|
25 |
|
|
// than OWIDTH will be quietly discarded.
|
26 |
|
|
//
|
27 |
|
|
// This design features no overflow checking.
|
28 |
|
|
//
|
29 |
|
|
// Notes:
|
30 |
|
|
// CORDIC:
|
31 |
|
|
// Much as we might like, we can't use a cordic here.
|
32 |
|
|
// The goal is to accomplish an FFT, as defined, and a
|
33 |
|
|
// CORDIC places a scale factor onto the data. Removing
|
34 |
|
|
// the scale factor would cost two multiplies, which
|
35 |
|
|
// is precisely what we are trying to avoid.
|
36 |
|
|
//
|
37 |
|
|
//
|
38 |
|
|
// 3-MULTIPLIES:
|
39 |
|
|
// It should also be possible to do this with three multiplies
|
40 |
|
|
// and an extra two addition cycles.
|
41 |
|
|
//
|
42 |
|
|
// We want
|
43 |
|
|
// R+I = (a + jb) * (c + jd)
|
44 |
|
|
// R+I = (ac-bd) + j(ad+bc)
|
45 |
|
|
// We multiply
|
46 |
|
|
// P1 = ac
|
47 |
|
|
// P2 = bd
|
48 |
|
|
// P3 = (a+b)(c+d)
|
49 |
|
|
// Then
|
50 |
|
|
// R+I=(P1-P2)+j(P3-P2-P1)
|
51 |
|
|
//
|
52 |
|
|
// WIDTHS:
|
53 |
|
|
// On multiplying an X width number by an
|
54 |
|
|
// Y width number, X>Y, the result should be (X+Y)
|
55 |
|
|
// bits, right?
|
56 |
|
|
// -2^(X-1) <= a <= 2^(X-1) - 1
|
57 |
|
|
// -2^(Y-1) <= b <= 2^(Y-1) - 1
|
58 |
|
|
// (2^(Y-1)-1)*(-2^(X-1)) <= ab <= 2^(X-1)2^(Y-1)
|
59 |
|
|
// -2^(X+Y-2)+2^(X-1) <= ab <= 2^(X+Y-2) <= 2^(X+Y-1) - 1
|
60 |
|
|
// -2^(X+Y-1) <= ab <= 2^(X+Y-1)-1
|
61 |
|
|
// YUP! But just barely. Do this and you'll really want
|
62 |
|
|
// to drop a bit, although you will risk overflow in so
|
63 |
|
|
// doing.
|
64 |
|
|
//
|
65 |
|
|
// 20150602 -- The sync logic lines have been completely redone. The
|
66 |
|
|
// synchronization lines no longer go through the FIFO with the
|
67 |
|
|
// left hand sum, but are kept out of memory. This allows the
|
68 |
|
|
// butterfly to use more optimal memory resources, while also
|
69 |
|
|
// guaranteeing that the sync lines can be properly reset upon
|
70 |
|
|
// any reset signal.
|
71 |
|
|
//
|
72 |
|
|
//
|
73 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
74 |
|
|
// Gisselquist Technology, LLC
|
75 |
|
|
//
|
76 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
77 |
|
|
//
|
78 |
|
|
// Copyright (C) 2015-2018, Gisselquist Technology, LLC
|
79 |
|
|
//
|
80 |
39 |
dgisselq |
// This file is part of the general purpose pipelined FFT project.
|
81 |
36 |
dgisselq |
//
|
82 |
39 |
dgisselq |
// The pipelined FFT project is free software (firmware): you can redistribute
|
83 |
|
|
// it and/or modify it under the terms of the GNU Lesser General Public License
|
84 |
|
|
// as published by the Free Software Foundation, either version 3 of the
|
85 |
|
|
// License, or (at your option) any later version.
|
86 |
36 |
dgisselq |
//
|
87 |
39 |
dgisselq |
// The pipelined FFT project is distributed in the hope that it will be useful,
|
88 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
89 |
|
|
// MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
|
90 |
|
|
// General Public License for more details.
|
91 |
|
|
//
|
92 |
|
|
// You should have received a copy of the GNU Lesser General Public License
|
93 |
|
|
// along with this program. (It's in the $(ROOT)/doc directory. Run make
|
94 |
|
|
// with no target there if the PDF file isn't present.) If not, see
|
95 |
36 |
dgisselq |
// <http://www.gnu.org/licenses/> for a copy.
|
96 |
|
|
//
|
97 |
39 |
dgisselq |
// License: LGPL, v3, as defined and found on www.gnu.org,
|
98 |
|
|
// http://www.gnu.org/licenses/lgpl.html
|
99 |
36 |
dgisselq |
//
|
100 |
|
|
//
|
101 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
102 |
|
|
//
|
103 |
|
|
//
|
104 |
|
|
`default_nettype none
|
105 |
|
|
//
|
106 |
|
|
module butterfly(i_clk, i_reset, i_ce, i_coef, i_left, i_right, i_aux,
|
107 |
|
|
o_left, o_right, o_aux);
|
108 |
|
|
// Public changeable parameters ...
|
109 |
|
|
parameter IWIDTH=16,CWIDTH=20,OWIDTH=17;
|
110 |
|
|
parameter SHIFT=0;
|
111 |
|
|
// The number of clocks per each i_ce. The actual number can be
|
112 |
|
|
// more, but the algorithm depends upon at least this many for
|
113 |
|
|
// extra internal processing.
|
114 |
|
|
parameter CKPCE=1;
|
115 |
|
|
//
|
116 |
|
|
// Local/derived parameters that are calculated from the above
|
117 |
|
|
// params. Apart from algorithmic changes below, these should not
|
118 |
|
|
// be adjusted
|
119 |
|
|
//
|
120 |
|
|
// The first step is to calculate how many clocks it takes our
|
121 |
|
|
// multiply to come back with an answer within. The time in the
|
122 |
|
|
// multiply depends upon the input value with the fewest number of
|
123 |
|
|
// bits--to keep the pipeline depth short. So, let's find the
|
124 |
|
|
// fewest number of bits here.
|
125 |
|
|
localparam MXMPYBITS =
|
126 |
|
|
((IWIDTH+2)>(CWIDTH+1)) ? (CWIDTH+1) : (IWIDTH + 2);
|
127 |
|
|
//
|
128 |
|
|
// Given this "fewest" number of bits, we can calculate the
|
129 |
|
|
// number of clocks the multiply itself will take.
|
130 |
|
|
localparam MPYDELAY=((MXMPYBITS+1)/2)+2;
|
131 |
|
|
//
|
132 |
|
|
// In an environment when CKPCE > 1, the multiply delay isn't
|
133 |
|
|
// necessarily the delay felt by this algorithm--measured in
|
134 |
|
|
// i_ce's. In particular, if the multiply can operate with more
|
135 |
|
|
// operations per clock, it can appear to finish "faster".
|
136 |
|
|
// Since most of the logic in this core operates on the slower
|
137 |
|
|
// clock, we'll need to map that speed into the number of slower
|
138 |
|
|
// clock ticks that it takes.
|
139 |
|
|
localparam LCLDELAY = (CKPCE == 1) ? MPYDELAY
|
140 |
|
|
: (CKPCE == 2) ? (MPYDELAY/2+2)
|
141 |
|
|
: (MPYDELAY/3 + 2);
|
142 |
|
|
localparam LGDELAY = (MPYDELAY>64) ? 7
|
143 |
|
|
: (MPYDELAY > 32) ? 6
|
144 |
|
|
: (MPYDELAY > 16) ? 5
|
145 |
|
|
: (MPYDELAY > 8) ? 4
|
146 |
|
|
: (MPYDELAY > 4) ? 3
|
147 |
|
|
: 2;
|
148 |
|
|
localparam AUXLEN=(LCLDELAY+3);
|
149 |
|
|
localparam MPYREMAINDER = MPYDELAY - CKPCE*(MPYDELAY/CKPCE);
|
150 |
|
|
|
151 |
|
|
|
152 |
39 |
dgisselq |
input wire i_clk, i_reset, i_ce;
|
153 |
|
|
input wire [(2*CWIDTH-1):0] i_coef;
|
154 |
|
|
input wire [(2*IWIDTH-1):0] i_left, i_right;
|
155 |
|
|
input wire i_aux;
|
156 |
36 |
dgisselq |
output wire [(2*OWIDTH-1):0] o_left, o_right;
|
157 |
|
|
output reg o_aux;
|
158 |
|
|
|
159 |
39 |
dgisselq |
`ifdef FORMAL
|
160 |
|
|
localparam F_LGDEPTH = (AUXLEN > 64) ? 7
|
161 |
|
|
: (AUXLEN > 32) ? 6
|
162 |
|
|
: (AUXLEN > 16) ? 5
|
163 |
|
|
: (AUXLEN > 8) ? 4
|
164 |
|
|
: (AUXLEN > 4) ? 3 : 2;
|
165 |
|
|
|
166 |
|
|
localparam F_DEPTH = AUXLEN;
|
167 |
|
|
localparam [F_LGDEPTH-1:0] F_D = F_DEPTH[F_LGDEPTH-1:0]-1;
|
168 |
|
|
|
169 |
|
|
reg signed [IWIDTH-1:0] f_dlyleft_r [0:F_DEPTH-1];
|
170 |
|
|
reg signed [IWIDTH-1:0] f_dlyleft_i [0:F_DEPTH-1];
|
171 |
|
|
reg signed [IWIDTH-1:0] f_dlyright_r [0:F_DEPTH-1];
|
172 |
|
|
reg signed [IWIDTH-1:0] f_dlyright_i [0:F_DEPTH-1];
|
173 |
|
|
reg signed [CWIDTH-1:0] f_dlycoeff_r [0:F_DEPTH-1];
|
174 |
|
|
reg signed [CWIDTH-1:0] f_dlycoeff_i [0:F_DEPTH-1];
|
175 |
|
|
reg signed [F_DEPTH-1:0] f_dlyaux;
|
176 |
|
|
|
177 |
|
|
wire signed [IWIDTH:0] f_predifr, f_predifi;
|
178 |
|
|
wire signed [IWIDTH+CWIDTH+3-1:0] f_predifrx, f_predifix;
|
179 |
|
|
wire signed [CWIDTH:0] f_sumcoef;
|
180 |
|
|
wire signed [IWIDTH+1:0] f_sumdiff;
|
181 |
|
|
wire signed [IWIDTH:0] f_sumr, f_sumi;
|
182 |
|
|
wire signed [IWIDTH+CWIDTH+3-1:0] f_sumrx, f_sumix;
|
183 |
|
|
wire signed [IWIDTH:0] f_difr, f_difi;
|
184 |
|
|
wire signed [IWIDTH+CWIDTH+3-1:0] f_difrx, f_difix;
|
185 |
|
|
wire signed [IWIDTH+CWIDTH+3-1:0] f_widecoeff_r, f_widecoeff_i;
|
186 |
|
|
|
187 |
|
|
wire [(CWIDTH):0] fp_one_ic, fp_two_ic, fp_three_ic, f_p3c_in;
|
188 |
|
|
wire [(IWIDTH+1):0] fp_one_id, fp_two_id, fp_three_id, f_p3d_in;
|
189 |
|
|
`endif
|
190 |
|
|
|
191 |
36 |
dgisselq |
reg [(2*IWIDTH-1):0] r_left, r_right;
|
192 |
|
|
reg [(2*CWIDTH-1):0] r_coef, r_coef_2;
|
193 |
|
|
wire signed [(IWIDTH-1):0] r_left_r, r_left_i, r_right_r, r_right_i;
|
194 |
|
|
assign r_left_r = r_left[ (2*IWIDTH-1):(IWIDTH)];
|
195 |
|
|
assign r_left_i = r_left[ (IWIDTH-1):0];
|
196 |
|
|
assign r_right_r = r_right[(2*IWIDTH-1):(IWIDTH)];
|
197 |
|
|
assign r_right_i = r_right[(IWIDTH-1):0];
|
198 |
|
|
|
199 |
|
|
reg signed [(IWIDTH):0] r_sum_r, r_sum_i, r_dif_r, r_dif_i;
|
200 |
|
|
|
201 |
|
|
reg [(LGDELAY-1):0] fifo_addr;
|
202 |
|
|
wire [(LGDELAY-1):0] fifo_read_addr;
|
203 |
|
|
assign fifo_read_addr = fifo_addr - LCLDELAY[(LGDELAY-1):0];
|
204 |
|
|
reg [(2*IWIDTH+1):0] fifo_left [ 0:((1<<LGDELAY)-1)];
|
205 |
|
|
|
206 |
|
|
// Set up the input to the multiply
|
207 |
|
|
always @(posedge i_clk)
|
208 |
39 |
dgisselq |
if (i_ce)
|
209 |
|
|
begin
|
210 |
|
|
// One clock just latches the inputs
|
211 |
|
|
r_left <= i_left; // No change in # of bits
|
212 |
|
|
r_right <= i_right;
|
213 |
|
|
r_coef <= i_coef;
|
214 |
|
|
// Next clock adds/subtracts
|
215 |
|
|
r_sum_r <= r_left_r + r_right_r; // Now IWIDTH+1 bits
|
216 |
|
|
r_sum_i <= r_left_i + r_right_i;
|
217 |
|
|
r_dif_r <= r_left_r - r_right_r;
|
218 |
|
|
r_dif_i <= r_left_i - r_right_i;
|
219 |
|
|
// Other inputs are simply delayed on second clock
|
220 |
|
|
r_coef_2<= r_coef;
|
221 |
|
|
end
|
222 |
36 |
dgisselq |
|
223 |
|
|
// Don't forget to record the even side, since it doesn't need
|
224 |
|
|
// to be multiplied, but yet we still need the results in sync
|
225 |
|
|
// with the answer when it is ready.
|
226 |
|
|
initial fifo_addr = 0;
|
227 |
|
|
always @(posedge i_clk)
|
228 |
39 |
dgisselq |
if (i_reset)
|
229 |
|
|
fifo_addr <= 0;
|
230 |
|
|
else if (i_ce)
|
231 |
|
|
// Need to delay the sum side--nothing else happens
|
232 |
|
|
// to it, but it needs to stay synchronized with the
|
233 |
|
|
// right side.
|
234 |
|
|
fifo_addr <= fifo_addr + 1;
|
235 |
36 |
dgisselq |
|
236 |
|
|
always @(posedge i_clk)
|
237 |
39 |
dgisselq |
if (i_ce)
|
238 |
|
|
fifo_left[fifo_addr] <= { r_sum_r, r_sum_i };
|
239 |
36 |
dgisselq |
|
240 |
|
|
wire signed [(CWIDTH-1):0] ir_coef_r, ir_coef_i;
|
241 |
|
|
assign ir_coef_r = r_coef_2[(2*CWIDTH-1):CWIDTH];
|
242 |
|
|
assign ir_coef_i = r_coef_2[(CWIDTH-1):0];
|
243 |
|
|
wire signed [((IWIDTH+2)+(CWIDTH+1)-1):0] p_one, p_two, p_three;
|
244 |
|
|
|
245 |
|
|
|
246 |
|
|
// Multiply output is always a width of the sum of the widths of
|
247 |
|
|
// the two inputs. ALWAYS. This is independent of the number of
|
248 |
|
|
// bits in p_one, p_two, or p_three. These values needed to
|
249 |
|
|
// accumulate a bit (or two) each. However, this approach to a
|
250 |
|
|
// three multiply complex multiply cannot increase the total
|
251 |
|
|
// number of bits in our final output. We'll take care of
|
252 |
|
|
// dropping back down to the proper width, OWIDTH, in our routine
|
253 |
|
|
// below.
|
254 |
|
|
|
255 |
|
|
|
256 |
|
|
// We accomplish here "Karatsuba" multiplication. That is,
|
257 |
|
|
// by doing three multiplies we accomplish the work of four.
|
258 |
|
|
// Let's prove to ourselves that this works ... We wish to
|
259 |
|
|
// multiply: (a+jb) * (c+jd), where a+jb is given by
|
260 |
|
|
// a + jb = r_dif_r + j r_dif_i, and
|
261 |
|
|
// c + jd = ir_coef_r + j ir_coef_i.
|
262 |
|
|
// We do this by calculating the intermediate products P1, P2,
|
263 |
|
|
// and P3 as
|
264 |
|
|
// P1 = ac
|
265 |
|
|
// P2 = bd
|
266 |
|
|
// P3 = (a + b) * (c + d)
|
267 |
|
|
// and then complete our final answer with
|
268 |
|
|
// ac - bd = P1 - P2 (this checks)
|
269 |
|
|
// ad + bc = P3 - P2 - P1
|
270 |
|
|
// = (ac + bc + ad + bd) - bd - ac
|
271 |
|
|
// = bc + ad (this checks)
|
272 |
|
|
|
273 |
|
|
|
274 |
|
|
// This should really be based upon an IF, such as in
|
275 |
|
|
// if (IWIDTH < CWIDTH) then ...
|
276 |
|
|
// However, this is the only (other) way I know to do it.
|
277 |
|
|
generate if (CKPCE <= 1)
|
278 |
|
|
begin
|
279 |
|
|
|
280 |
|
|
wire [(CWIDTH):0] p3c_in;
|
281 |
|
|
wire [(IWIDTH+1):0] p3d_in;
|
282 |
|
|
assign p3c_in = ir_coef_i + ir_coef_r;
|
283 |
|
|
assign p3d_in = r_dif_r + r_dif_i;
|
284 |
|
|
|
285 |
|
|
// We need to pad these first two multiplies by an extra
|
286 |
|
|
// bit just to keep them aligned with the third,
|
287 |
|
|
// simpler, multiply.
|
288 |
|
|
longbimpy #(CWIDTH+1,IWIDTH+2) p1(i_clk, i_ce,
|
289 |
|
|
{ir_coef_r[CWIDTH-1],ir_coef_r},
|
290 |
39 |
dgisselq |
{r_dif_r[IWIDTH],r_dif_r}, p_one
|
291 |
|
|
`ifdef FORMAL
|
292 |
|
|
, fp_one_ic, fp_one_id
|
293 |
|
|
`endif
|
294 |
|
|
);
|
295 |
36 |
dgisselq |
longbimpy #(CWIDTH+1,IWIDTH+2) p2(i_clk, i_ce,
|
296 |
|
|
{ir_coef_i[CWIDTH-1],ir_coef_i},
|
297 |
39 |
dgisselq |
{r_dif_i[IWIDTH],r_dif_i}, p_two
|
298 |
|
|
`ifdef FORMAL
|
299 |
|
|
, fp_two_ic, fp_two_id
|
300 |
|
|
`endif
|
301 |
|
|
);
|
302 |
36 |
dgisselq |
longbimpy #(CWIDTH+1,IWIDTH+2) p3(i_clk, i_ce,
|
303 |
39 |
dgisselq |
p3c_in, p3d_in, p_three
|
304 |
|
|
`ifdef FORMAL
|
305 |
|
|
, fp_three_ic, fp_three_id
|
306 |
|
|
`endif
|
307 |
|
|
);
|
308 |
36 |
dgisselq |
|
309 |
|
|
end else if (CKPCE == 2)
|
310 |
|
|
begin : CKPCE_TWO
|
311 |
|
|
// Coefficient multiply inputs
|
312 |
|
|
reg [2*(CWIDTH)-1:0] mpy_pipe_c;
|
313 |
|
|
// Data multiply inputs
|
314 |
|
|
reg [2*(IWIDTH+1)-1:0] mpy_pipe_d;
|
315 |
|
|
wire signed [(CWIDTH-1):0] mpy_pipe_vc;
|
316 |
|
|
wire signed [(IWIDTH):0] mpy_pipe_vd;
|
317 |
|
|
//
|
318 |
|
|
reg signed [(CWIDTH+1)-1:0] mpy_cof_sum;
|
319 |
|
|
reg signed [(IWIDTH+2)-1:0] mpy_dif_sum;
|
320 |
|
|
|
321 |
|
|
assign mpy_pipe_vc = mpy_pipe_c[2*(CWIDTH)-1:CWIDTH];
|
322 |
|
|
assign mpy_pipe_vd = mpy_pipe_d[2*(IWIDTH+1)-1:IWIDTH+1];
|
323 |
|
|
|
324 |
|
|
reg mpy_pipe_v;
|
325 |
|
|
reg ce_phase;
|
326 |
|
|
|
327 |
|
|
reg signed [(CWIDTH+IWIDTH+3)-1:0] mpy_pipe_out;
|
328 |
|
|
reg signed [IWIDTH+CWIDTH+3-1:0] longmpy;
|
329 |
|
|
|
330 |
39 |
dgisselq |
`ifdef FORMAL
|
331 |
|
|
wire [CWIDTH:0] f_past_ic;
|
332 |
|
|
wire [IWIDTH+1:0] f_past_id;
|
333 |
|
|
wire [CWIDTH:0] f_past_mux_ic;
|
334 |
|
|
wire [IWIDTH+1:0] f_past_mux_id;
|
335 |
36 |
dgisselq |
|
336 |
39 |
dgisselq |
reg [CWIDTH:0] f_rpone_ic, f_rptwo_ic, f_rpthree_ic,
|
337 |
|
|
f_rp2one_ic, f_rp2two_ic, f_rp2three_ic;
|
338 |
|
|
reg [IWIDTH+1:0] f_rpone_id, f_rptwo_id, f_rpthree_id,
|
339 |
|
|
f_rp2one_id, f_rp2two_id, f_rp2three_id;
|
340 |
|
|
`endif
|
341 |
|
|
|
342 |
|
|
|
343 |
36 |
dgisselq |
initial ce_phase = 1'b0;
|
344 |
|
|
always @(posedge i_clk)
|
345 |
|
|
if (i_reset)
|
346 |
|
|
ce_phase <= 1'b0;
|
347 |
|
|
else if (i_ce)
|
348 |
|
|
ce_phase <= 1'b1;
|
349 |
|
|
else
|
350 |
|
|
ce_phase <= 1'b0;
|
351 |
|
|
|
352 |
|
|
always @(*)
|
353 |
|
|
mpy_pipe_v = (i_ce)||(ce_phase);
|
354 |
|
|
|
355 |
|
|
always @(posedge i_clk)
|
356 |
|
|
if (ce_phase)
|
357 |
|
|
begin
|
358 |
|
|
mpy_pipe_c[2*CWIDTH-1:0] <=
|
359 |
|
|
{ ir_coef_r, ir_coef_i };
|
360 |
|
|
mpy_pipe_d[2*(IWIDTH+1)-1:0] <=
|
361 |
|
|
{ r_dif_r, r_dif_i };
|
362 |
|
|
|
363 |
|
|
mpy_cof_sum <= ir_coef_i + ir_coef_r;
|
364 |
|
|
mpy_dif_sum <= r_dif_r + r_dif_i;
|
365 |
|
|
|
366 |
|
|
end else if (i_ce)
|
367 |
|
|
begin
|
368 |
|
|
mpy_pipe_c[2*(CWIDTH)-1:0] <= {
|
369 |
|
|
mpy_pipe_c[(CWIDTH)-1:0], {(CWIDTH){1'b0}} };
|
370 |
|
|
mpy_pipe_d[2*(IWIDTH+1)-1:0] <= {
|
371 |
|
|
mpy_pipe_d[(IWIDTH+1)-1:0], {(IWIDTH+1){1'b0}} };
|
372 |
|
|
end
|
373 |
|
|
|
374 |
|
|
longbimpy #(CWIDTH+1,IWIDTH+2) mpy0(i_clk, mpy_pipe_v,
|
375 |
39 |
dgisselq |
mpy_cof_sum, mpy_dif_sum, longmpy
|
376 |
|
|
`ifdef FORMAL
|
377 |
|
|
, f_past_ic, f_past_id
|
378 |
|
|
`endif
|
379 |
|
|
);
|
380 |
36 |
dgisselq |
|
381 |
|
|
longbimpy #(CWIDTH+1,IWIDTH+2) mpy1(i_clk, mpy_pipe_v,
|
382 |
|
|
{ mpy_pipe_vc[CWIDTH-1], mpy_pipe_vc },
|
383 |
|
|
{ mpy_pipe_vd[IWIDTH ], mpy_pipe_vd },
|
384 |
39 |
dgisselq |
mpy_pipe_out
|
385 |
|
|
`ifdef FORMAL
|
386 |
|
|
, f_past_mux_ic, f_past_mux_id
|
387 |
|
|
`endif
|
388 |
|
|
);
|
389 |
36 |
dgisselq |
|
390 |
|
|
reg signed [((IWIDTH+2)+(CWIDTH+1)-1):0]
|
391 |
|
|
rp_one, rp_two, rp_three,
|
392 |
|
|
rp2_one, rp2_two, rp2_three;
|
393 |
|
|
|
394 |
|
|
always @(posedge i_clk)
|
395 |
|
|
if (((i_ce)&&(!MPYDELAY[0]))
|
396 |
|
|
||((ce_phase)&&(MPYDELAY[0])))
|
397 |
39 |
dgisselq |
begin
|
398 |
36 |
dgisselq |
rp_one <= mpy_pipe_out;
|
399 |
39 |
dgisselq |
`ifdef FORMAL
|
400 |
|
|
f_rpone_ic <= f_past_mux_ic;
|
401 |
|
|
f_rpone_id <= f_past_mux_id;
|
402 |
|
|
`endif
|
403 |
|
|
end
|
404 |
|
|
|
405 |
36 |
dgisselq |
always @(posedge i_clk)
|
406 |
|
|
if (((i_ce)&&(MPYDELAY[0]))
|
407 |
|
|
||((ce_phase)&&(!MPYDELAY[0])))
|
408 |
39 |
dgisselq |
begin
|
409 |
36 |
dgisselq |
rp_two <= mpy_pipe_out;
|
410 |
39 |
dgisselq |
`ifdef FORMAL
|
411 |
|
|
f_rptwo_ic <= f_past_mux_ic;
|
412 |
|
|
f_rptwo_id <= f_past_mux_id;
|
413 |
|
|
`endif
|
414 |
|
|
end
|
415 |
|
|
|
416 |
36 |
dgisselq |
always @(posedge i_clk)
|
417 |
|
|
if (i_ce)
|
418 |
39 |
dgisselq |
begin
|
419 |
36 |
dgisselq |
rp_three <= longmpy;
|
420 |
39 |
dgisselq |
`ifdef FORMAL
|
421 |
|
|
f_rpthree_ic <= f_past_ic;
|
422 |
|
|
f_rpthree_id <= f_past_id;
|
423 |
|
|
`endif
|
424 |
|
|
end
|
425 |
36 |
dgisselq |
|
426 |
39 |
dgisselq |
|
427 |
36 |
dgisselq |
// Our outputs *MUST* be set on a clock where i_ce is
|
428 |
|
|
// true for the following logic to work. Make that
|
429 |
|
|
// happen here.
|
430 |
|
|
always @(posedge i_clk)
|
431 |
|
|
if (i_ce)
|
432 |
39 |
dgisselq |
begin
|
433 |
36 |
dgisselq |
rp2_one<= rp_one;
|
434 |
|
|
rp2_two <= rp_two;
|
435 |
|
|
rp2_three<= rp_three;
|
436 |
39 |
dgisselq |
`ifdef FORMAL
|
437 |
|
|
f_rp2one_ic <= f_rpone_ic;
|
438 |
|
|
f_rp2one_id <= f_rpone_id;
|
439 |
36 |
dgisselq |
|
440 |
39 |
dgisselq |
f_rp2two_ic <= f_rptwo_ic;
|
441 |
|
|
f_rp2two_id <= f_rptwo_id;
|
442 |
|
|
|
443 |
|
|
f_rp2three_ic <= f_rpthree_ic;
|
444 |
|
|
f_rp2three_id <= f_rpthree_id;
|
445 |
|
|
`endif
|
446 |
|
|
end
|
447 |
|
|
|
448 |
36 |
dgisselq |
assign p_one = rp2_one;
|
449 |
|
|
assign p_two = (!MPYDELAY[0])? rp2_two : rp_two;
|
450 |
|
|
assign p_three = ( MPYDELAY[0])? rp_three : rp2_three;
|
451 |
|
|
|
452 |
|
|
// verilator lint_off UNUSED
|
453 |
|
|
wire [2*(IWIDTH+CWIDTH+3)-1:0] unused;
|
454 |
|
|
assign unused = { rp2_two, rp2_three };
|
455 |
|
|
// verilator lint_on UNUSED
|
456 |
|
|
|
457 |
39 |
dgisselq |
`ifdef FORMAL
|
458 |
|
|
assign fp_one_ic = f_rp2one_ic;
|
459 |
|
|
assign fp_one_id = f_rp2one_id;
|
460 |
|
|
|
461 |
|
|
assign fp_two_ic = (!MPYDELAY[0])? f_rp2two_ic : f_rptwo_ic;
|
462 |
|
|
assign fp_two_id = (!MPYDELAY[0])? f_rp2two_id : f_rptwo_id;
|
463 |
|
|
|
464 |
|
|
assign fp_three_ic= (MPYDELAY[0])? f_rpthree_ic : f_rp2three_ic;
|
465 |
|
|
assign fp_three_id= (MPYDELAY[0])? f_rpthree_id : f_rp2three_id;
|
466 |
|
|
`endif
|
467 |
|
|
|
468 |
36 |
dgisselq |
end else if (CKPCE <= 3)
|
469 |
|
|
begin : CKPCE_THREE
|
470 |
|
|
// Coefficient multiply inputs
|
471 |
|
|
reg [3*(CWIDTH+1)-1:0] mpy_pipe_c;
|
472 |
|
|
// Data multiply inputs
|
473 |
|
|
reg [3*(IWIDTH+2)-1:0] mpy_pipe_d;
|
474 |
|
|
wire signed [(CWIDTH):0] mpy_pipe_vc;
|
475 |
|
|
wire signed [(IWIDTH+1):0] mpy_pipe_vd;
|
476 |
|
|
|
477 |
|
|
assign mpy_pipe_vc = mpy_pipe_c[3*(CWIDTH+1)-1:2*(CWIDTH+1)];
|
478 |
|
|
assign mpy_pipe_vd = mpy_pipe_d[3*(IWIDTH+2)-1:2*(IWIDTH+2)];
|
479 |
|
|
|
480 |
|
|
reg mpy_pipe_v;
|
481 |
|
|
reg [2:0] ce_phase;
|
482 |
|
|
|
483 |
|
|
reg signed [ (CWIDTH+IWIDTH+3)-1:0] mpy_pipe_out;
|
484 |
|
|
|
485 |
39 |
dgisselq |
`ifdef FORMAL
|
486 |
|
|
wire [CWIDTH:0] f_past_ic;
|
487 |
|
|
wire [IWIDTH+1:0] f_past_id;
|
488 |
|
|
|
489 |
|
|
reg [CWIDTH:0] f_rpone_ic, f_rptwo_ic, f_rpthree_ic,
|
490 |
|
|
f_rp2one_ic, f_rp2two_ic, f_rp2three_ic,
|
491 |
|
|
f_rp3one_ic;
|
492 |
|
|
reg [IWIDTH+1:0] f_rpone_id, f_rptwo_id, f_rpthree_id,
|
493 |
|
|
f_rp2one_id, f_rp2two_id, f_rp2three_id,
|
494 |
|
|
f_rp3one_id;
|
495 |
|
|
`endif
|
496 |
|
|
|
497 |
36 |
dgisselq |
initial ce_phase = 3'b011;
|
498 |
|
|
always @(posedge i_clk)
|
499 |
|
|
if (i_reset)
|
500 |
|
|
ce_phase <= 3'b011;
|
501 |
|
|
else if (i_ce)
|
502 |
|
|
ce_phase <= 3'b000;
|
503 |
|
|
else if (ce_phase != 3'b011)
|
504 |
|
|
ce_phase <= ce_phase + 1'b1;
|
505 |
|
|
|
506 |
|
|
always @(*)
|
507 |
|
|
mpy_pipe_v = (i_ce)||(ce_phase < 3'b010);
|
508 |
|
|
|
509 |
|
|
always @(posedge i_clk)
|
510 |
39 |
dgisselq |
if (ce_phase == 3'b000)
|
511 |
|
|
begin
|
512 |
|
|
// Second clock
|
513 |
|
|
mpy_pipe_c[3*(CWIDTH+1)-1:(CWIDTH+1)] <= {
|
514 |
|
|
ir_coef_r[CWIDTH-1], ir_coef_r,
|
515 |
|
|
ir_coef_i[CWIDTH-1], ir_coef_i };
|
516 |
|
|
mpy_pipe_c[CWIDTH:0] <= ir_coef_i + ir_coef_r;
|
517 |
|
|
mpy_pipe_d[3*(IWIDTH+2)-1:(IWIDTH+2)] <= {
|
518 |
|
|
r_dif_r[IWIDTH], r_dif_r,
|
519 |
|
|
r_dif_i[IWIDTH], r_dif_i };
|
520 |
|
|
mpy_pipe_d[(IWIDTH+2)-1:0] <= r_dif_r + r_dif_i;
|
521 |
36 |
dgisselq |
|
522 |
39 |
dgisselq |
end else if (mpy_pipe_v)
|
523 |
|
|
begin
|
524 |
|
|
mpy_pipe_c[3*(CWIDTH+1)-1:0] <= {
|
525 |
|
|
mpy_pipe_c[2*(CWIDTH+1)-1:0], {(CWIDTH+1){1'b0}} };
|
526 |
|
|
mpy_pipe_d[3*(IWIDTH+2)-1:0] <= {
|
527 |
|
|
mpy_pipe_d[2*(IWIDTH+2)-1:0], {(IWIDTH+2){1'b0}} };
|
528 |
|
|
end
|
529 |
36 |
dgisselq |
|
530 |
|
|
longbimpy #(CWIDTH+1,IWIDTH+2) mpy(i_clk, mpy_pipe_v,
|
531 |
39 |
dgisselq |
mpy_pipe_vc, mpy_pipe_vd, mpy_pipe_out
|
532 |
|
|
`ifdef FORMAL
|
533 |
|
|
, f_past_ic, f_past_id
|
534 |
|
|
`endif
|
535 |
|
|
);
|
536 |
36 |
dgisselq |
|
537 |
|
|
reg signed [((IWIDTH+2)+(CWIDTH+1)-1):0]
|
538 |
|
|
rp_one, rp_two, rp_three,
|
539 |
|
|
rp2_one, rp2_two, rp2_three,
|
540 |
|
|
rp3_one;
|
541 |
|
|
|
542 |
|
|
always @(posedge i_clk)
|
543 |
|
|
if (MPYREMAINDER == 0)
|
544 |
|
|
begin
|
545 |
|
|
|
546 |
|
|
if (i_ce)
|
547 |
39 |
dgisselq |
begin
|
548 |
36 |
dgisselq |
rp_two <= mpy_pipe_out;
|
549 |
39 |
dgisselq |
`ifdef FORMAL
|
550 |
|
|
f_rptwo_ic <= f_past_ic;
|
551 |
|
|
f_rptwo_id <= f_past_id;
|
552 |
|
|
`endif
|
553 |
|
|
end else if (ce_phase == 3'b000)
|
554 |
|
|
begin
|
555 |
36 |
dgisselq |
rp_three <= mpy_pipe_out;
|
556 |
39 |
dgisselq |
`ifdef FORMAL
|
557 |
|
|
f_rpthree_ic <= f_past_ic;
|
558 |
|
|
f_rpthree_id <= f_past_id;
|
559 |
|
|
`endif
|
560 |
|
|
end else if (ce_phase == 3'b001)
|
561 |
|
|
begin
|
562 |
36 |
dgisselq |
rp_one <= mpy_pipe_out;
|
563 |
39 |
dgisselq |
`ifdef FORMAL
|
564 |
|
|
f_rpone_ic <= f_past_ic;
|
565 |
|
|
f_rpone_id <= f_past_id;
|
566 |
|
|
`endif
|
567 |
|
|
end
|
568 |
36 |
dgisselq |
end else if (MPYREMAINDER == 1)
|
569 |
|
|
begin
|
570 |
|
|
|
571 |
|
|
if (i_ce)
|
572 |
39 |
dgisselq |
begin
|
573 |
36 |
dgisselq |
rp_one <= mpy_pipe_out;
|
574 |
39 |
dgisselq |
`ifdef FORMAL
|
575 |
|
|
f_rpone_ic <= f_past_ic;
|
576 |
|
|
f_rpone_id <= f_past_id;
|
577 |
|
|
`endif
|
578 |
|
|
end else if (ce_phase == 3'b000)
|
579 |
|
|
begin
|
580 |
36 |
dgisselq |
rp_two <= mpy_pipe_out;
|
581 |
39 |
dgisselq |
`ifdef FORMAL
|
582 |
|
|
f_rptwo_ic <= f_past_ic;
|
583 |
|
|
f_rptwo_id <= f_past_id;
|
584 |
|
|
`endif
|
585 |
|
|
end else if (ce_phase == 3'b001)
|
586 |
|
|
begin
|
587 |
36 |
dgisselq |
rp_three <= mpy_pipe_out;
|
588 |
39 |
dgisselq |
`ifdef FORMAL
|
589 |
|
|
f_rpthree_ic <= f_past_ic;
|
590 |
|
|
f_rpthree_id <= f_past_id;
|
591 |
|
|
`endif
|
592 |
|
|
end
|
593 |
36 |
dgisselq |
end else // if (MPYREMAINDER == 2)
|
594 |
|
|
begin
|
595 |
|
|
|
596 |
|
|
if (i_ce)
|
597 |
39 |
dgisselq |
begin
|
598 |
36 |
dgisselq |
rp_three <= mpy_pipe_out;
|
599 |
39 |
dgisselq |
`ifdef FORMAL
|
600 |
|
|
f_rpthree_ic <= f_past_ic;
|
601 |
|
|
f_rpthree_id <= f_past_id;
|
602 |
|
|
`endif
|
603 |
|
|
end else if (ce_phase == 3'b000)
|
604 |
|
|
begin
|
605 |
36 |
dgisselq |
rp_one <= mpy_pipe_out;
|
606 |
39 |
dgisselq |
`ifdef FORMAL
|
607 |
|
|
f_rpone_ic <= f_past_ic;
|
608 |
|
|
f_rpone_id <= f_past_id;
|
609 |
|
|
`endif
|
610 |
|
|
end else if (ce_phase == 3'b001)
|
611 |
|
|
begin
|
612 |
36 |
dgisselq |
rp_two <= mpy_pipe_out;
|
613 |
39 |
dgisselq |
`ifdef FORMAL
|
614 |
|
|
f_rptwo_ic <= f_past_ic;
|
615 |
|
|
f_rptwo_id <= f_past_id;
|
616 |
|
|
`endif
|
617 |
|
|
end
|
618 |
36 |
dgisselq |
end
|
619 |
|
|
|
620 |
|
|
always @(posedge i_clk)
|
621 |
|
|
if (i_ce)
|
622 |
|
|
begin
|
623 |
|
|
rp2_one <= rp_one;
|
624 |
|
|
rp2_two <= rp_two;
|
625 |
|
|
rp2_three <= (MPYREMAINDER == 2) ? mpy_pipe_out : rp_three;
|
626 |
|
|
rp3_one <= (MPYREMAINDER == 0) ? rp2_one : rp_one;
|
627 |
39 |
dgisselq |
`ifdef FORMAL
|
628 |
|
|
f_rp2one_ic <= f_rpone_ic;
|
629 |
|
|
f_rp2one_id <= f_rpone_id;
|
630 |
|
|
|
631 |
|
|
f_rp2two_ic <= f_rptwo_ic;
|
632 |
|
|
f_rp2two_id <= f_rptwo_id;
|
633 |
|
|
|
634 |
|
|
f_rp2three_ic <= (MPYREMAINDER==2) ? f_past_ic : f_rpthree_ic;
|
635 |
|
|
f_rp2three_id <= (MPYREMAINDER==2) ? f_past_id : f_rpthree_id;
|
636 |
|
|
f_rp3one_ic <= (MPYREMAINDER==0) ? f_rp2one_ic : f_rpone_ic;
|
637 |
|
|
f_rp3one_id <= (MPYREMAINDER==0) ? f_rp2one_id : f_rpone_id;
|
638 |
|
|
`endif
|
639 |
36 |
dgisselq |
end
|
640 |
39 |
dgisselq |
|
641 |
36 |
dgisselq |
assign p_one = rp3_one;
|
642 |
|
|
assign p_two = rp2_two;
|
643 |
|
|
assign p_three = rp2_three;
|
644 |
|
|
|
645 |
39 |
dgisselq |
`ifdef FORMAL
|
646 |
|
|
assign fp_one_ic = f_rp3one_ic;
|
647 |
|
|
assign fp_one_id = f_rp3one_id;
|
648 |
|
|
|
649 |
|
|
assign fp_two_ic = f_rp2two_ic;
|
650 |
|
|
assign fp_two_id = f_rp2two_id;
|
651 |
|
|
|
652 |
|
|
assign fp_three_ic = f_rp2three_ic;
|
653 |
|
|
assign fp_three_id = f_rp2three_id;
|
654 |
|
|
`endif
|
655 |
|
|
|
656 |
36 |
dgisselq |
end endgenerate
|
657 |
|
|
// These values are held in memory and delayed during the
|
658 |
|
|
// multiply. Here, we recover them. During the multiply,
|
659 |
|
|
// values were multiplied by 2^(CWIDTH-2)*exp{-j*2*pi*...},
|
660 |
|
|
// therefore, the left_x values need to be right shifted by
|
661 |
|
|
// CWIDTH-2 as well. The additional bits come from a sign
|
662 |
|
|
// extension.
|
663 |
|
|
wire signed [(IWIDTH+CWIDTH):0] fifo_i, fifo_r;
|
664 |
|
|
reg [(2*IWIDTH+1):0] fifo_read;
|
665 |
39 |
dgisselq |
assign fifo_r = { {2{fifo_read[2*(IWIDTH+1)-1]}},
|
666 |
|
|
fifo_read[(2*(IWIDTH+1)-1):(IWIDTH+1)], {(CWIDTH-2){1'b0}} };
|
667 |
|
|
assign fifo_i = { {2{fifo_read[(IWIDTH+1)-1]}},
|
668 |
|
|
fifo_read[((IWIDTH+1)-1):0], {(CWIDTH-2){1'b0}} };
|
669 |
36 |
dgisselq |
|
670 |
|
|
|
671 |
|
|
reg signed [(CWIDTH+IWIDTH+3-1):0] mpy_r, mpy_i;
|
672 |
|
|
|
673 |
|
|
// Let's do some rounding and remove unnecessary bits.
|
674 |
|
|
// We have (IWIDTH+CWIDTH+3) bits here, we need to drop down to
|
675 |
|
|
// OWIDTH, and SHIFT by SHIFT bits in the process. The trick is
|
676 |
|
|
// that we don't need (IWIDTH+CWIDTH+3) bits. We've accumulated
|
677 |
|
|
// them, but the actual values will never fill all these bits.
|
678 |
|
|
// In particular, we only need:
|
679 |
|
|
// IWIDTH bits for the input
|
680 |
|
|
// +1 bit for the add/subtract
|
681 |
|
|
// +CWIDTH bits for the coefficient multiply
|
682 |
|
|
// +1 bit for the add/subtract in the complex multiply
|
683 |
|
|
// ------
|
684 |
|
|
// (IWIDTH+CWIDTH+2) bits at full precision.
|
685 |
|
|
//
|
686 |
|
|
// However, the coefficient multiply multiplied by a maximum value
|
687 |
|
|
// of 2^(CWIDTH-2). Thus, we only have
|
688 |
|
|
// IWIDTH bits for the input
|
689 |
|
|
// +1 bit for the add/subtract
|
690 |
|
|
// +CWIDTH-2 bits for the coefficient multiply
|
691 |
|
|
// +1 (optional) bit for the add/subtract in the cpx mpy.
|
692 |
|
|
// -------- ... multiply. (This last bit may be shifted out.)
|
693 |
|
|
// (IWIDTH+CWIDTH) valid output bits.
|
694 |
|
|
// Now, if the user wants to keep any extras of these (via OWIDTH),
|
695 |
|
|
// or if he wishes to arbitrarily shift some of these off (via
|
696 |
|
|
// SHIFT) we accomplish that here.
|
697 |
|
|
|
698 |
|
|
wire signed [(OWIDTH-1):0] rnd_left_r, rnd_left_i, rnd_right_r, rnd_right_i;
|
699 |
|
|
|
700 |
|
|
wire signed [(CWIDTH+IWIDTH+3-1):0] left_sr, left_si;
|
701 |
|
|
assign left_sr = { {(2){fifo_r[(IWIDTH+CWIDTH)]}}, fifo_r };
|
702 |
|
|
assign left_si = { {(2){fifo_i[(IWIDTH+CWIDTH)]}}, fifo_i };
|
703 |
|
|
|
704 |
|
|
convround #(CWIDTH+IWIDTH+3,OWIDTH,SHIFT+4) do_rnd_left_r(i_clk, i_ce,
|
705 |
|
|
left_sr, rnd_left_r);
|
706 |
|
|
|
707 |
|
|
convround #(CWIDTH+IWIDTH+3,OWIDTH,SHIFT+4) do_rnd_left_i(i_clk, i_ce,
|
708 |
|
|
left_si, rnd_left_i);
|
709 |
|
|
|
710 |
|
|
convround #(CWIDTH+IWIDTH+3,OWIDTH,SHIFT+4) do_rnd_right_r(i_clk, i_ce,
|
711 |
|
|
mpy_r, rnd_right_r);
|
712 |
|
|
|
713 |
|
|
convround #(CWIDTH+IWIDTH+3,OWIDTH,SHIFT+4) do_rnd_right_i(i_clk, i_ce,
|
714 |
|
|
mpy_i, rnd_right_i);
|
715 |
|
|
|
716 |
|
|
always @(posedge i_clk)
|
717 |
39 |
dgisselq |
if (i_ce)
|
718 |
|
|
begin
|
719 |
|
|
// First clock, recover all values
|
720 |
|
|
fifo_read <= fifo_left[fifo_read_addr];
|
721 |
|
|
// These values are IWIDTH+CWIDTH+3 bits wide
|
722 |
|
|
// although they only need to be (IWIDTH+1)
|
723 |
|
|
// + (CWIDTH) bits wide. (We've got two
|
724 |
|
|
// extra bits we need to get rid of.)
|
725 |
|
|
mpy_r <= p_one - p_two;
|
726 |
|
|
mpy_i <= p_three - p_one - p_two;
|
727 |
|
|
end
|
728 |
36 |
dgisselq |
|
729 |
|
|
reg [(AUXLEN-1):0] aux_pipeline;
|
730 |
|
|
initial aux_pipeline = 0;
|
731 |
|
|
always @(posedge i_clk)
|
732 |
39 |
dgisselq |
if (i_reset)
|
733 |
|
|
aux_pipeline <= 0;
|
734 |
|
|
else if (i_ce)
|
735 |
|
|
aux_pipeline <= { aux_pipeline[(AUXLEN-2):0], i_aux };
|
736 |
36 |
dgisselq |
|
737 |
|
|
initial o_aux = 1'b0;
|
738 |
|
|
always @(posedge i_clk)
|
739 |
39 |
dgisselq |
if (i_reset)
|
740 |
|
|
o_aux <= 1'b0;
|
741 |
|
|
else if (i_ce)
|
742 |
|
|
begin
|
743 |
|
|
// Second clock, latch for final clock
|
744 |
|
|
o_aux <= aux_pipeline[AUXLEN-1];
|
745 |
|
|
end
|
746 |
36 |
dgisselq |
|
747 |
|
|
// As a final step, we pack our outputs into two packed two's
|
748 |
|
|
// complement numbers per output word, so that each output word
|
749 |
|
|
// has (2*OWIDTH) bits in it, with the top half being the real
|
750 |
|
|
// portion and the bottom half being the imaginary portion.
|
751 |
|
|
assign o_left = { rnd_left_r, rnd_left_i };
|
752 |
|
|
assign o_right= { rnd_right_r,rnd_right_i};
|
753 |
|
|
|
754 |
|
|
`ifdef FORMAL
|
755 |
|
|
initial f_dlyaux[0] = 0;
|
756 |
|
|
always @(posedge i_clk)
|
757 |
|
|
if (i_reset)
|
758 |
|
|
f_dlyaux <= 0;
|
759 |
|
|
else if (i_ce)
|
760 |
|
|
f_dlyaux <= { f_dlyaux[F_DEPTH-2:0], i_aux };
|
761 |
|
|
|
762 |
|
|
always @(posedge i_clk)
|
763 |
|
|
if (i_ce)
|
764 |
|
|
begin
|
765 |
|
|
f_dlyleft_r[0] <= i_left[ (2*IWIDTH-1):IWIDTH];
|
766 |
|
|
f_dlyleft_i[0] <= i_left[ ( IWIDTH-1):0];
|
767 |
|
|
f_dlyright_r[0] <= i_right[(2*IWIDTH-1):IWIDTH];
|
768 |
|
|
f_dlyright_i[0] <= i_right[( IWIDTH-1):0];
|
769 |
|
|
f_dlycoeff_r[0] <= i_coef[ (2*CWIDTH-1):CWIDTH];
|
770 |
|
|
f_dlycoeff_i[0] <= i_coef[ ( CWIDTH-1):0];
|
771 |
|
|
end
|
772 |
|
|
|
773 |
|
|
genvar k;
|
774 |
|
|
generate for(k=1; k<F_DEPTH; k=k+1)
|
775 |
|
|
begin : F_PROPAGATE_DELAY_LINES
|
776 |
|
|
|
777 |
|
|
|
778 |
|
|
always @(posedge i_clk)
|
779 |
|
|
if (i_ce)
|
780 |
|
|
begin
|
781 |
|
|
f_dlyleft_r[k] <= f_dlyleft_r[ k-1];
|
782 |
|
|
f_dlyleft_i[k] <= f_dlyleft_i[ k-1];
|
783 |
|
|
f_dlyright_r[k] <= f_dlyright_r[k-1];
|
784 |
|
|
f_dlyright_i[k] <= f_dlyright_i[k-1];
|
785 |
|
|
f_dlycoeff_r[k] <= f_dlycoeff_r[k-1];
|
786 |
|
|
f_dlycoeff_i[k] <= f_dlycoeff_i[k-1];
|
787 |
|
|
end
|
788 |
|
|
|
789 |
|
|
end endgenerate
|
790 |
|
|
|
791 |
|
|
`ifndef VERILATOR
|
792 |
39 |
dgisselq |
//
|
793 |
|
|
// Make some i_ce restraining assumptions. These are necessary
|
794 |
|
|
// to get the design to pass induction.
|
795 |
|
|
//
|
796 |
36 |
dgisselq |
generate if (CKPCE <= 1)
|
797 |
|
|
begin
|
798 |
|
|
|
799 |
39 |
dgisselq |
// No primary i_ce assumption. i_ce can be anything
|
800 |
|
|
//
|
801 |
|
|
// First induction i_ce assumption: No more than one
|
802 |
|
|
// empty cycle between used cycles. Without this
|
803 |
|
|
// assumption, or one like it, induction would never
|
804 |
|
|
// complete.
|
805 |
|
|
always @(posedge i_clk)
|
806 |
|
|
if ((!$past(i_ce)))
|
807 |
|
|
assume(i_ce);
|
808 |
36 |
dgisselq |
|
809 |
39 |
dgisselq |
// Second induction i_ce assumption: avoid skipping an
|
810 |
|
|
// i_ce and thus stretching out the i_ce cycle two i_ce
|
811 |
|
|
// cycles in a row. Without this assumption, induction
|
812 |
|
|
// would still complete, it would just take longer
|
813 |
|
|
always @(posedge i_clk)
|
814 |
|
|
if (($past(i_ce))&&(!$past(i_ce,2)))
|
815 |
|
|
assume(i_ce);
|
816 |
|
|
|
817 |
36 |
dgisselq |
end else if (CKPCE == 2)
|
818 |
|
|
begin : F_CKPCE_TWO
|
819 |
|
|
|
820 |
39 |
dgisselq |
// Primary i_ce assumption: Every i_ce cycle is followed
|
821 |
|
|
// by a non-i_ce cycle, so the multiplies can be
|
822 |
|
|
// multiplexed
|
823 |
36 |
dgisselq |
always @(posedge i_clk)
|
824 |
39 |
dgisselq |
if ($past(i_ce))
|
825 |
|
|
assume(!i_ce);
|
826 |
|
|
// First induction assumption: Don't let this stretch
|
827 |
|
|
// out too far. This is necessary to pass induction
|
828 |
|
|
always @(posedge i_clk)
|
829 |
|
|
if ((!$past(i_ce))&&(!$past(i_ce,2)))
|
830 |
|
|
assume(i_ce);
|
831 |
36 |
dgisselq |
|
832 |
39 |
dgisselq |
always @(posedge i_clk)
|
833 |
|
|
if ((!$past(i_ce))&&($past(i_ce,2))
|
834 |
|
|
&&(!$past(i_ce,3))&&(!$past(i_ce,4)))
|
835 |
|
|
assume(i_ce);
|
836 |
|
|
|
837 |
36 |
dgisselq |
end else if (CKPCE == 3)
|
838 |
|
|
begin : F_CKPCE_THREE
|
839 |
|
|
|
840 |
39 |
dgisselq |
// Primary i_ce assumption: Following any i_ce cycle,
|
841 |
|
|
// there must be two clock cycles with i_ce de-asserted
|
842 |
36 |
dgisselq |
always @(posedge i_clk)
|
843 |
39 |
dgisselq |
if (($past(i_ce))||($past(i_ce,2)))
|
844 |
|
|
assume(!i_ce);
|
845 |
36 |
dgisselq |
|
846 |
39 |
dgisselq |
// Induction assumption: Allow i_ce's every third or
|
847 |
|
|
// fourth clock, but don't allow them to be separated
|
848 |
|
|
// further than that
|
849 |
|
|
always @(posedge i_clk)
|
850 |
|
|
if ((!$past(i_ce))&&(!$past(i_ce,2))&&(!$past(i_ce,3)))
|
851 |
|
|
assume(i_ce);
|
852 |
|
|
|
853 |
|
|
// Second induction assumption, to speed up the proof:
|
854 |
|
|
// If it's the earliest possible opportunity for an
|
855 |
|
|
// i_ce, and the last i_ce was late, don't let this one
|
856 |
|
|
// be late as well.
|
857 |
|
|
always @(posedge i_clk)
|
858 |
|
|
if ((!$past(i_ce))&&(!$past(i_ce,2))
|
859 |
|
|
&&($past(i_ce,3))&&(!$past(i_ce,4))
|
860 |
|
|
&&(!$past(i_ce,5))&&(!$past(i_ce,6)))
|
861 |
|
|
assume(i_ce);
|
862 |
|
|
|
863 |
36 |
dgisselq |
end endgenerate
|
864 |
|
|
`endif
|
865 |
|
|
|
866 |
|
|
reg [F_LGDEPTH:0] f_startup_counter;
|
867 |
|
|
initial f_startup_counter = 0;
|
868 |
|
|
always @(posedge i_clk)
|
869 |
|
|
if (i_reset)
|
870 |
|
|
f_startup_counter <= 0;
|
871 |
|
|
else if ((i_ce)&&(!(&f_startup_counter)))
|
872 |
|
|
f_startup_counter <= f_startup_counter + 1;
|
873 |
|
|
|
874 |
|
|
always @(*)
|
875 |
|
|
begin
|
876 |
|
|
f_sumr = f_dlyleft_r[F_D] + f_dlyright_r[F_D];
|
877 |
|
|
f_sumi = f_dlyleft_i[F_D] + f_dlyright_i[F_D];
|
878 |
|
|
end
|
879 |
|
|
|
880 |
|
|
assign f_sumrx = { {(4){f_sumr[IWIDTH]}}, f_sumr, {(CWIDTH-2){1'b0}} };
|
881 |
|
|
assign f_sumix = { {(4){f_sumi[IWIDTH]}}, f_sumi, {(CWIDTH-2){1'b0}} };
|
882 |
|
|
|
883 |
|
|
always @(*)
|
884 |
|
|
begin
|
885 |
|
|
f_difr = f_dlyleft_r[F_D] - f_dlyright_r[F_D];
|
886 |
|
|
f_difi = f_dlyleft_i[F_D] - f_dlyright_i[F_D];
|
887 |
|
|
end
|
888 |
|
|
|
889 |
|
|
assign f_difrx = { {(CWIDTH+2){f_difr[IWIDTH]}}, f_difr };
|
890 |
|
|
assign f_difix = { {(CWIDTH+2){f_difi[IWIDTH]}}, f_difi };
|
891 |
|
|
|
892 |
|
|
assign f_widecoeff_r ={ {(IWIDTH+3){f_dlycoeff_r[F_D][CWIDTH-1]}},
|
893 |
|
|
f_dlycoeff_r[F_D] };
|
894 |
|
|
assign f_widecoeff_i ={ {(IWIDTH+3){f_dlycoeff_i[F_D][CWIDTH-1]}},
|
895 |
|
|
f_dlycoeff_i[F_D] };
|
896 |
|
|
|
897 |
|
|
always @(posedge i_clk)
|
898 |
|
|
if (f_startup_counter > {1'b0, F_D})
|
899 |
|
|
begin
|
900 |
|
|
assert(aux_pipeline == f_dlyaux);
|
901 |
|
|
assert(left_sr == f_sumrx);
|
902 |
|
|
assert(left_si == f_sumix);
|
903 |
|
|
assert(aux_pipeline[AUXLEN-1] == f_dlyaux[F_D]);
|
904 |
|
|
|
905 |
|
|
if ((f_difr == 0)&&(f_difi == 0))
|
906 |
|
|
begin
|
907 |
|
|
assert(mpy_r == 0);
|
908 |
|
|
assert(mpy_i == 0);
|
909 |
|
|
end else if ((f_dlycoeff_r[F_D] == 0)
|
910 |
|
|
&&(f_dlycoeff_i[F_D] == 0))
|
911 |
|
|
begin
|
912 |
|
|
assert(mpy_r == 0);
|
913 |
|
|
assert(mpy_i == 0);
|
914 |
|
|
end
|
915 |
|
|
|
916 |
|
|
if ((f_dlycoeff_r[F_D] == 1)&&(f_dlycoeff_i[F_D] == 0))
|
917 |
|
|
begin
|
918 |
|
|
assert(mpy_r == f_difrx);
|
919 |
|
|
assert(mpy_i == f_difix);
|
920 |
|
|
end
|
921 |
|
|
|
922 |
|
|
if ((f_dlycoeff_r[F_D] == 0)&&(f_dlycoeff_i[F_D] == 1))
|
923 |
|
|
begin
|
924 |
|
|
assert(mpy_r == -f_difix);
|
925 |
|
|
assert(mpy_i == f_difrx);
|
926 |
|
|
end
|
927 |
|
|
|
928 |
|
|
if ((f_difr == 1)&&(f_difi == 0))
|
929 |
|
|
begin
|
930 |
|
|
assert(mpy_r == f_widecoeff_r);
|
931 |
|
|
assert(mpy_i == f_widecoeff_i);
|
932 |
|
|
end
|
933 |
|
|
|
934 |
|
|
if ((f_difr == 0)&&(f_difi == 1))
|
935 |
|
|
begin
|
936 |
|
|
assert(mpy_r == -f_widecoeff_i);
|
937 |
|
|
assert(mpy_i == f_widecoeff_r);
|
938 |
|
|
end
|
939 |
|
|
end
|
940 |
|
|
|
941 |
|
|
// Let's see if we can improve our performance at all by
|
942 |
|
|
// moving our test one clock earlier. If nothing else, it should
|
943 |
|
|
// help induction finish one (or more) clocks ealier than
|
944 |
|
|
// otherwise
|
945 |
|
|
|
946 |
|
|
|
947 |
|
|
always @(*)
|
948 |
|
|
begin
|
949 |
|
|
f_predifr = f_dlyleft_r[F_D-1] - f_dlyright_r[F_D-1];
|
950 |
|
|
f_predifi = f_dlyleft_i[F_D-1] - f_dlyright_i[F_D-1];
|
951 |
|
|
end
|
952 |
|
|
|
953 |
|
|
assign f_predifrx = { {(CWIDTH+2){f_predifr[IWIDTH]}}, f_predifr };
|
954 |
|
|
assign f_predifix = { {(CWIDTH+2){f_predifi[IWIDTH]}}, f_predifi };
|
955 |
|
|
|
956 |
|
|
always @(*)
|
957 |
|
|
begin
|
958 |
|
|
f_sumcoef = f_dlycoeff_r[F_D-1] + f_dlycoeff_i[F_D-1];
|
959 |
|
|
f_sumdiff = f_predifr + f_predifi;
|
960 |
|
|
end
|
961 |
|
|
|
962 |
|
|
// Induction helpers
|
963 |
|
|
always @(posedge i_clk)
|
964 |
|
|
if (f_startup_counter >= { 1'b0, F_D })
|
965 |
|
|
begin
|
966 |
|
|
if (f_dlycoeff_r[F_D-1] == 0)
|
967 |
|
|
assert(p_one == 0);
|
968 |
|
|
if (f_dlycoeff_i[F_D-1] == 0)
|
969 |
|
|
assert(p_two == 0);
|
970 |
|
|
|
971 |
|
|
if (f_dlycoeff_r[F_D-1] == 1)
|
972 |
|
|
assert(p_one == f_predifrx);
|
973 |
|
|
if (f_dlycoeff_i[F_D-1] == 1)
|
974 |
|
|
assert(p_two == f_predifix);
|
975 |
|
|
|
976 |
|
|
if (f_predifr == 0)
|
977 |
|
|
assert(p_one == 0);
|
978 |
|
|
if (f_predifi == 0)
|
979 |
|
|
assert(p_two == 0);
|
980 |
|
|
|
981 |
|
|
// verilator lint_off WIDTH
|
982 |
|
|
if (f_predifr == 1)
|
983 |
|
|
assert(p_one == f_dlycoeff_r[F_D-1]);
|
984 |
|
|
if (f_predifi == 1)
|
985 |
|
|
assert(p_two == f_dlycoeff_i[F_D-1]);
|
986 |
|
|
// verilator lint_on WIDTH
|
987 |
|
|
|
988 |
|
|
if (f_sumcoef == 0)
|
989 |
|
|
assert(p_three == 0);
|
990 |
|
|
if (f_sumdiff == 0)
|
991 |
|
|
assert(p_three == 0);
|
992 |
|
|
// verilator lint_off WIDTH
|
993 |
|
|
if (f_sumcoef == 1)
|
994 |
|
|
assert(p_three == f_sumdiff);
|
995 |
|
|
if (f_sumdiff == 1)
|
996 |
|
|
assert(p_three == f_sumcoef);
|
997 |
|
|
// verilator lint_on WIDTH
|
998 |
|
|
`ifdef VERILATOR
|
999 |
39 |
dgisselq |
// Check that the multiplies match--but *ONLY* if using
|
1000 |
|
|
// Verilator, and not if using formal proper
|
1001 |
36 |
dgisselq |
assert(p_one == f_predifr * f_dlycoeff_r[F_D-1]);
|
1002 |
|
|
assert(p_two == f_predifi * f_dlycoeff_i[F_D-1]);
|
1003 |
|
|
assert(p_three == f_sumdiff * f_sumcoef);
|
1004 |
|
|
`endif // VERILATOR
|
1005 |
|
|
end
|
1006 |
|
|
|
1007 |
39 |
dgisselq |
// The following logic formally insists that our version of the
|
1008 |
|
|
// inputs to the multiply matches what the (multiclock) multiply
|
1009 |
|
|
// thinks its inputs were. While this may seem redundant, the
|
1010 |
|
|
// proof will not complete in any reasonable amount of time
|
1011 |
|
|
// without these assertions.
|
1012 |
|
|
|
1013 |
|
|
assign f_p3c_in = f_dlycoeff_i[F_D-1] + f_dlycoeff_r[F_D-1];
|
1014 |
|
|
assign f_p3d_in = f_predifi + f_predifr;
|
1015 |
|
|
|
1016 |
|
|
always @(*)
|
1017 |
|
|
if (f_startup_counter >= { 1'b0, F_D })
|
1018 |
|
|
begin
|
1019 |
|
|
assert(fp_one_ic == { f_dlycoeff_r[F_D-1][CWIDTH-1],
|
1020 |
|
|
f_dlycoeff_r[F_D-1][CWIDTH-1:0] });
|
1021 |
|
|
assert(fp_two_ic == { f_dlycoeff_i[F_D-1][CWIDTH-1],
|
1022 |
|
|
f_dlycoeff_i[F_D-1][CWIDTH-1:0] });
|
1023 |
|
|
assert(fp_one_id == { f_predifr[IWIDTH], f_predifr });
|
1024 |
|
|
assert(fp_two_id == { f_predifi[IWIDTH], f_predifi });
|
1025 |
|
|
assert(fp_three_ic == f_p3c_in);
|
1026 |
|
|
assert(fp_three_id == f_p3d_in);
|
1027 |
|
|
end
|
1028 |
|
|
|
1029 |
36 |
dgisselq |
// F_CHECK will be set externally by the solver, so that we can
|
1030 |
|
|
// double check that the solver is actually testing what we think
|
1031 |
|
|
// it is testing. We'll set it here to MPYREMAINDER, which will
|
1032 |
|
|
// essentially eliminate the check--unless overridden by the
|
1033 |
|
|
// solver.
|
1034 |
|
|
parameter F_CHECK = MPYREMAINDER;
|
1035 |
|
|
initial assert(MPYREMAINDER == F_CHECK);
|
1036 |
|
|
|
1037 |
|
|
`endif // FORMAL
|
1038 |
|
|
endmodule
|