1 pattern xilinx_dsp_packC
3 udata <std::function<SigSpec(const SigSpec&)>> unextend
5 state <SigSpec> sigC sigP
6 state <bool> ffCcepol ffCrstpol
7 state <Cell*> ffC ffCcemux ffCrstmux
10 state <SigSpec> argQ argD
11 state <bool> ffcepol ffrstpol
13 udata <SigSpec> dffD dffQ
14 udata <SigBit> dffclock
15 udata <Cell*> dff dffcemux dffrstmux
16 udata <bool> dffcepol dffrstpol
19 select dsp->type.in(\DSP48E1)
20 select param(dsp, \CREG, 1).as_int() == 0
21 select nusers(port(dsp, \C, SigSpec())) > 1
24 code argQ ffC ffCcemux ffCrstmux ffCcepol ffCrstpol sigC sigP clock
25 unextend = [](const SigSpec &sig) {
27 for (i = GetSize(sig)-1; i > 0; i--)
28 if (sig[i] != sig[i-1])
30 // Do not remove non-const sign bit
33 return sig.extract(0, i);
35 sigC = unextend(port(dsp, \C, SigSpec()));
37 SigSpec P = port(dsp, \P);
38 if (param(dsp, \USE_MULT, Const("MULTIPLY")).decode_string() == "MULTIPLY") {
39 // Only care about those bits that are used
41 for (i = 0; i < GetSize(P); i++) {
42 if (nusers(P[i]) <= 1)
46 log_assert(nusers(P.extract_end(i)) <= 1);
54 clock = port(dsp, \CLK, SigBit());
62 ffCrstmux = dffrstmux;
63 ffCrstpol = dffrstpol;
78 // #######################
85 for (auto c : argQ.chunks()) {
88 if (c.wire->get_bool_attribute(\keep))
90 Const init = c.wire->attributes.at(\init, State::Sx);
91 if (!init.is_fully_undef() && !init.is_fully_zero())
97 select ff->type.in($dff)
98 // DSP48E1 does not support clock inversion
99 select param(ff, \CLK_POLARITY).as_bool()
101 slice offset GetSize(port(ff, \D))
102 index <SigBit> port(ff, \Q)[offset] === argQ[0]
104 // Check that the rest of argQ is present
105 filter GetSize(port(ff, \Q)) >= offset + GetSize(argQ)
106 filter port(ff, \Q).extract(offset, GetSize(argQ)) == argQ
113 if (clock != SigBit() && port(ff, \CLK) != clock)
116 SigSpec Q = port(ff, \Q);
118 dffclock = port(ff, \CLK);
122 dffD.replace(argQ, argD);
123 // Only search for ffrstmux if dffD only
124 // has two (ff, ffrstmux) users
125 if (nusers(dffD) > 2)
132 select ffrstmux->type.in($mux)
133 index <SigSpec> port(ffrstmux, \Y) === argD
135 choice <IdString> BA {\B, \A}
136 // DSP48E1 only supports reset to zero
137 select port(ffrstmux, BA).is_fully_zero()
139 define <bool> pol (BA == \B)
146 dffrstmux = ffrstmux;
147 dffrstpol = ffrstpol;
148 argD = port(ffrstmux, ffrstpol ? \A : \B);
149 dffD.replace(port(ffrstmux, \Y), argD);
151 // Only search for ffcemux if argQ has at
152 // least 3 users (ff, <upstream>, ffrstmux) and
153 // dffD only has two (ff, ffrstmux)
154 if (!(nusers(argQ) >= 3 && nusers(dffD) == 2))
163 select ffcemux->type.in($mux)
164 index <SigSpec> port(ffcemux, \Y) === argD
165 choice <IdString> AB {\A, \B}
166 index <SigSpec> port(ffcemux, AB) === argQ
167 define <bool> pol (AB == \A)
176 argD = port(ffcemux, ffcepol ? \B : \A);
177 dffD.replace(port(ffcemux, \Y), argD);