4 state <bool> clock_pol cd_signed o_lo
5 state <SigSpec> sigA sigB sigCD sigH sigO
7 state <IdString> addAB muxAB
9 state <bool> ffAcepol ffBcepol ffCDcepol ffOcepol
10 state <bool> ffArstpol ffBrstpol ffCDrstpol ffFJKGrstpol ffOrstpol
12 state <Cell*> ffA ffAcemux ffArstmux ffB ffBcemux ffBrstmux ffCD ffCDcemux ffCDrstmux
13 state <Cell*> ffFJKG ffFJKGrstmux ffO ffOcemux ffOrstmux
16 state <SigSpec> argQ argD
17 state <bool> ffcepol ffrstpol
19 udata <SigSpec> dffD dffQ
20 udata <SigBit> dffclock
21 udata <Cell*> dff dffcemux dffrstmux
22 udata <bool> dffcepol dffrstpol dffclock_pol
25 select mul->type.in($mul, \SB_MAC16)
26 select GetSize(mul->getPort(\A)) + GetSize(mul->getPort(\B)) > 10
31 if (mul->type == $mul)
33 else if (mul->type == \SB_MAC16)
41 for (i = GetSize(sigA)-1; i > 0; i--)
42 if (sigA[i] != sigA[i-1])
44 // Do not remove non-const sign bit
47 sigA.remove(i, GetSize(sigA)-i);
49 for (i = GetSize(sigB)-1; i > 0; i--)
50 if (sigB[i] != sigB[i-1])
52 // Do not remove non-const sign bit
55 sigB.remove(i, GetSize(sigB)-i);
57 // Only care about those bits that are used
58 for (i = 0; i < GetSize(O); i++) {
59 if (nusers(O[i]) <= 1)
63 log_assert(nusers(O.extract_end(i)) <= 1);
66 code argQ ffA ffAcemux ffArstmux ffAcepol ffArstpol sigA clock clock_pol
67 if (mul->type != \SB_MAC16 || !param(mul, \A_REG).as_bool()) {
73 clock_pol = dffclock_pol;
75 ffArstmux = dffrstmux;
76 ffArstpol = dffrstpol;
87 code argQ ffB ffBcemux ffBrstmux ffBcepol ffBrstpol sigB clock clock_pol
88 if (mul->type != \SB_MAC16 || !param(mul, \B_REG).as_bool()) {
94 clock_pol = dffclock_pol;
96 ffBrstmux = dffrstmux;
97 ffBrstpol = dffrstpol;
108 code argD ffFJKG ffFJKGrstmux ffFJKGrstpol sigH sigO clock clock_pol
109 if (nusers(sigH) == 2 &&
110 (mul->type != \SB_MAC16 ||
111 (!param(mul, \TOP_8x8_MULT_REG).as_bool() && !param(mul, \BOT_8x8_MULT_REG).as_bool() && !param(mul, \PIPELINE_16x16_MULT_REG1).as_bool() && !param(mul, \PIPELINE_16x16_MULT_REG2).as_bool()))) {
113 subpattern(out_dffe);
117 clock_pol = dffclock_pol;
119 ffFJKGrstmux = dffrstmux;
120 ffFJKGrstpol = dffrstpol;
122 // F/J/K/G do not have a CE-like (hold) input
126 // Reset signal of F/J (IRSTTOP) and K/G (IRSTBOT)
127 // shared with A and B
128 if ((ffArstmux != NULL) != (ffFJKGrstmux != NULL))
130 if ((ffBrstmux != NULL) != (ffFJKGrstmux != NULL))
133 if (port(ffArstmux, \S) != port(ffFJKGrstmux, \S))
135 if (ffArstpol != ffFJKGrstpol)
139 if (port(ffBrstmux, \S) != port(ffFJKGrstmux, \S))
141 if (ffBrstpol != ffFJKGrstpol)
153 if mul->type != \SB_MAC16 || (param(mul, \TOPOUTPUT_SELECT).as_int() == 3 && param(mul, \BOTOUTPUT_SELECT).as_int() == 3)
154 select add->type.in($add)
155 choice <IdString> AB {\A, \B}
156 select nusers(port(add, AB)) == 2
157 index <SigBit> port(add, AB)[0] === sigH[0]
158 filter GetSize(port(add, AB)) <= GetSize(sigH)
159 filter port(add, AB) == sigH.extract(0, GetSize(port(add, AB)))
164 code sigCD sigO cd_signed
166 sigCD = port(add, addAB == \A ? \B : \A);
167 cd_signed = param(add, addAB == \A ? \B_SIGNED : \A_SIGNED).as_bool();
169 int natural_mul_width = GetSize(sigA) + GetSize(sigB);
170 int actual_mul_width = GetSize(sigH);
171 int actual_acc_width = GetSize(sigCD);
173 if ((actual_acc_width > actual_mul_width) && (natural_mul_width > actual_mul_width))
175 // If accumulator, check adder width and signedness
176 if (sigCD == sigH && (actual_acc_width != actual_mul_width) && (param(mul, \A_SIGNED).as_bool() != param(add, \A_SIGNED).as_bool()))
179 sigO = port(add, \Y);
184 select mux->type == $mux
185 choice <IdString> AB {\A, \B}
186 index <int> nusers(port(mux, AB)) === 2
187 index <SigSpec> port(mux, AB) === sigO
194 sigO = port(mux, \Y);
197 code argD ffO ffOcemux ffOrstmux ffOcepol ffOrstpol sigO sigCD clock clock_pol cd_signed o_lo
198 if (mul->type != \SB_MAC16 ||
199 // Ensure that register is not already used
200 ((mul->parameters.at(\TOPOUTPUT_SELECT, 0).as_int() != 1 && mul->parameters.at(\BOTOUTPUT_SELECT, 0).as_int() != 1) &&
201 // Ensure that OLOADTOP/OLOADBOT is unused or zero
202 (mul->connections_.at(\OLOADTOP, State::S0).is_fully_zero() && mul->connections_.at(\OLOADBOT, State::S0).is_fully_zero()))) {
206 // First try entire sigO
207 if (nusers(sigO) == 2) {
209 subpattern(out_dffe);
212 // Otherwise try just its least significant 16 bits
213 if (!dff && GetSize(sigO) > 16) {
214 argD = sigO.extract(0, 16);
215 if (nusers(argD) == 2) {
216 subpattern(out_dffe);
224 clock_pol = dffclock_pol;
226 ffOrstmux = dffrstmux;
227 ffOrstpol = dffrstpol;
234 sigO.replace(sigO.extract(0, GetSize(dffQ)), dffQ);
237 // Loading value into output register is not
238 // supported unless using accumulator
242 sigCD = port(mux, muxAB == \B ? \A : \B);
244 cd_signed = add && param(add, \A_SIGNED).as_bool() && param(add, \B_SIGNED).as_bool();
247 sigCD.extend_u0(32, cd_signed);
254 // #######################
257 arg argD argQ clock clock_pol
261 for (auto c : argQ.chunks()) {
264 if (c.wire->get_bool_attribute(\keep))
270 select ff->type.in($dff)
271 // DSP48E1 does not support clock inversion
272 select param(ff, \CLK_POLARITY).as_bool()
274 slice offset GetSize(port(ff, \D))
275 index <SigBit> port(ff, \Q)[offset] === argQ[0]
277 // Check that the rest of argQ is present
278 filter GetSize(port(ff, \Q)) >= offset + GetSize(argQ)
279 filter port(ff, \Q).extract(offset, GetSize(argQ)) == argQ
286 if (clock != SigBit()) {
287 if (port(ff, \CLK) != clock)
289 if (param(ff, \CLK_POLARITY).as_bool() != clock_pol)
293 SigSpec Q = port(ff, \Q);
295 dffclock = port(ff, \CLK);
296 dffclock_pol = param(ff, \CLK_POLARITY).as_bool();
300 dffD.replace(argQ, argD);
301 // Only search for ffrstmux if dffD only
302 // has two (ff, ffrstmux) users
303 if (nusers(dffD) > 2)
310 select ffrstmux->type.in($mux)
311 index <SigSpec> port(ffrstmux, \Y) === argD
313 choice <IdString> BA {\B, \A}
314 // DSP48E1 only supports reset to zero
315 select port(ffrstmux, BA).is_fully_zero()
317 define <bool> pol (BA == \B)
324 dffrstmux = ffrstmux;
325 dffrstpol = ffrstpol;
326 argD = port(ffrstmux, ffrstpol ? \A : \B);
327 dffD.replace(port(ffrstmux, \Y), argD);
329 // Only search for ffcemux if argQ has at
330 // least 3 users (ff, <upstream>, ffrstmux) and
331 // dffD only has two (ff, ffrstmux)
332 if (!(nusers(argQ) >= 3 && nusers(dffD) == 2))
341 select ffcemux->type.in($mux)
342 index <SigSpec> port(ffcemux, \Y) === argD
343 choice <IdString> AB {\A, \B}
344 index <SigSpec> port(ffcemux, AB) === argQ
345 define <bool> pol (AB == \A)
354 argD = port(ffcemux, ffcepol ? \B : \A);
355 dffD.replace(port(ffcemux, \Y), argD);
361 // #######################
364 arg argD argQ clock clock_pol
371 select ffcemux->type.in($mux)
372 // ffcemux output must have two users: ffcemux and ff.D
373 select nusers(port(ffcemux, \Y)) == 2
375 choice <IdString> AB {\A, \B}
376 // keep-last-value net must have at least three users: ffcemux, ff, downstream sink(s)
377 select nusers(port(ffcemux, AB)) >= 3
379 slice offset GetSize(port(ffcemux, \Y))
380 define <IdString> BA (AB == \A ? \B : \A)
381 index <SigBit> port(ffcemux, BA)[offset] === argD[0]
383 // Check that the rest of argD is present
384 filter GetSize(BA) >= offset + GetSize(argD)
385 filter port(ffcemux, BA).extract(offset, GetSize(argD)) == argD
388 define <bool> pol (BA == \B)
397 SigSpec BA = port(ffcemux, ffcepol ? \B : \A);
398 if (ffoffset + GetSize(argD) > GetSize(BA))
400 for (int i = 1; i < GetSize(argD); i++)
401 if (BA[ffoffset+i] != argD[i])
404 SigSpec Y = port(ffcemux, \Y);
407 argQ.replace(BA, port(ffcemux, ffcepol ? \A : \B));
415 select ffrstmux->type.in($mux)
416 // ffrstmux output must have two users: ffrstmux and ff.D
417 select nusers(port(ffrstmux, \Y)) == 2
419 choice <IdString> BA {\B, \A}
420 // DSP48E1 only supports reset to zero
421 select port(ffrstmux, BA).is_fully_zero()
423 slice offset GetSize(port(ffrstmux, \Y))
424 define <IdString> AB (BA == \B ? \A : \B)
425 index <SigBit> port(ffrstmux, AB)[offset] === argD[0]
427 // Check that offset is consistent
428 filter !ffcemux || ffoffset == offset
429 // Check that the rest of argD is present
430 filter GetSize(AB) >= offset + GetSize(argD)
431 filter port(ffrstmux, AB).extract(offset, GetSize(argD)) == argD
434 define <bool> pol (AB == \A)
441 dffrstmux = ffrstmux;
443 SigSpec AB = port(ffrstmux, ffrstpol ? \A : \B);
444 SigSpec Y = port(ffrstmux, \Y);
447 dffrstmux = ffrstmux;
448 dffrstpol = ffrstpol;
453 select ff->type.in($dff)
454 // DSP48E1 does not support clock inversion
455 select param(ff, \CLK_POLARITY).as_bool()
457 slice offset GetSize(port(ff, \D))
458 index <SigBit> port(ff, \D)[offset] === argD[0]
460 // Check that offset is consistent
461 filter (!ffcemux && !ffrstmux) || ffoffset == offset
462 // Check that the rest of argD is present
463 filter GetSize(port(ff, \D)) >= offset + GetSize(argD)
464 filter port(ff, \D).extract(offset, GetSize(argD)) == argD
465 // Check that FF.Q is connected to CE-mux
466 filter !ffcemux || port(ff, \Q).extract(offset, GetSize(argQ)) == argQ
475 if (clock != SigBit()) {
476 if (port(ff, \CLK) != clock)
478 if (param(ff, \CLK_POLARITY).as_bool() != clock_pol)
482 SigSpec D = port(ff, \D);
483 SigSpec Q = port(ff, \Q);
489 for (auto c : argQ.chunks()) {
490 if (c.wire->get_bool_attribute(\keep))
492 Const init = c.wire->attributes.at(\init, State::Sx);
493 if (!init.is_fully_undef() && !init.is_fully_zero())
499 dffclock = port(ff, \CLK);
500 dffclock_pol = param(ff, \CLK_POLARITY).as_bool();
502 // No enable/reset mux possible without flop
503 else if (dffcemux || dffrstmux)