1 // This file describes the second of three pattern matcher setups that
2 // forms the `xilinx_dsp` pass described in xilinx_dsp.cc
3 // At a high level, it works as follows:
4 // (1) Starting from a DSP48E1 cell that (a) doesn't have a CREG already,
5 // and (b) uses the 'C' port
6 // (2) Match the driver of the 'C' input to a possible $dff cell (CREG)
7 // (attached to at most two $mux cells that implement clock-enable or
8 // reset functionality, using a subpattern discussed below)
10 // - Running CREG packing after xilinx_dsp_pack is necessary since there is no
11 // guarantee that the cell ordering corresponds to the "expected" case (i.e.
12 // the order in which they appear in the source) thus the possiblity existed
13 // that a register got packed as a CREG into a downstream DSP that should
14 // have otherwise been a PREG of an upstream DSP that had not been visited
16 // - The reason this is separated out from the xilinx_dsp.pmg file is
17 // for efficiency --- each *.pmg file creates a class of the same basename,
18 // which when constructed, creates a custom database tailored to the
19 // pattern(s) contained within. Since the pattern in this file must be
20 // executed after the pattern contained in xilinx_dsp.pmg, it is necessary
21 // to reconstruct this database. Separating the two patterns into
22 // independent files causes two smaller, more specific, databases.
24 pattern xilinx_dsp_packC
26 udata <std::function<SigSpec(const SigSpec&)>> unextend
28 state <SigSpec> sigC sigP
29 state <bool> ffCcepol ffCrstpol
30 state <Cell*> ffC ffCcemux ffCrstmux
32 // Variables used for subpatterns
33 state <SigSpec> argQ argD
34 state <bool> ffcepol ffrstpol
36 udata <SigSpec> dffD dffQ
37 udata <SigBit> dffclock
38 udata <Cell*> dff dffcemux dffrstmux
39 udata <bool> dffcepol dffrstpol
41 // (1) Starting from a DSP48E1 cell that (a) doesn't have a CREG already,
42 // and (b) uses the 'C' port
44 select dsp->type.in(\DSP48E1)
45 select param(dsp, \CREG, 1).as_int() == 0
46 select nusers(port(dsp, \C, SigSpec())) > 1
50 unextend = [](const SigSpec &sig) {
52 for (i = GetSize(sig)-1; i > 0; i--)
53 if (sig[i] != sig[i-1])
55 // Do not remove non-const sign bit
58 return sig.extract(0, i);
60 sigC = unextend(port(dsp, \C, SigSpec()));
62 SigSpec P = port(dsp, \P);
63 if (param(dsp, \USE_MULT, Const("MULTIPLY")).decode_string() == "MULTIPLY") {
64 // Only care about those bits that are used
66 for (i = 0; i < GetSize(P); i++) {
67 if (nusers(P[i]) <= 1)
71 log_assert(nusers(P.extract_end(i)) <= 1);
76 clock = port(dsp, \CLK, SigBit());
79 // (2) Match the driver of the 'C' input to a possible $dff cell (CREG)
80 // (attached to at most two $mux cells that implement clock-enable or
81 // reset functionality, using the in_dffe subpattern)
82 code argQ ffC ffCcemux ffCrstmux ffCcepol ffCrstpol sigC clock
89 ffCrstmux = dffrstmux;
90 ffCrstpol = dffrstpol;
105 // #######################
107 // Subpattern for matching against input registers, based on knowledge of the
108 // 'Q' input. Typically, identifying registers with clock-enable and reset
109 // capability would be a task would be handled by other Yosys passes such as
110 // dff2dffe, but since DSP inference happens much before this, these patterns
111 // have to be manually identified.
113 // (1) Starting from a $dff cell that (partially or fully) drives the given
115 // (2) Match for a $mux cell implementing synchronous reset semantics ---
116 // one that exclusively drives the 'D' input of the $dff, with one of its
117 // $mux inputs being fully zero
118 // (3) Match for a $mux cell implement clock enable semantics --- one that
119 // exclusively drives the 'D' input of the $dff (or the other input of
120 // the reset $mux) and where one of this $mux's inputs is connected to
121 // the 'Q' output of the $dff
127 for (const auto &c : argQ.chunks()) {
128 // Abandon matches when 'Q' is a constant
131 // Abandon matches when 'Q' has the keep attribute set
132 if (c.wire->get_bool_attribute(\keep))
134 // Abandon matches when 'Q' has a non-zero init attribute set
135 // (not supported by DSP48E1)
136 Const init = c.wire->attributes.at(\init, Const());
137 for (auto b : init.extract(c.offset, c.width))
138 if (b != State::Sx && b != State::S0)
143 // (1) Starting from a $dff cell that (partially or fully) drives the given
146 select ff->type.in($dff)
147 // DSP48E1 does not support clock inversion
148 select param(ff, \CLK_POLARITY).as_bool()
150 slice offset GetSize(port(ff, \D))
151 index <SigBit> port(ff, \Q)[offset] === argQ[0]
153 // Check that the rest of argQ is present
154 filter GetSize(port(ff, \Q)) >= offset + GetSize(argQ)
155 filter port(ff, \Q).extract(offset, GetSize(argQ)) == argQ
157 filter clock == SigBit() || port(ff, \CLK) == clock
163 SigSpec Q = port(ff, \Q);
165 dffclock = port(ff, \CLK);
169 dffD.replace(argQ, argD);
170 // Only search for ffrstmux if dffD only
171 // has two (ff, ffrstmux) users
172 if (nusers(dffD) > 2)
176 // (2) Match for a $mux cell implementing synchronous reset semantics ---
177 // exclusively drives the 'D' input of the $dff, with one of the $mux
178 // inputs being fully zero
181 select ffrstmux->type.in($mux)
182 index <SigSpec> port(ffrstmux, \Y) === argD
184 choice <IdString> BA {\B, \A}
185 // DSP48E1 only supports reset to zero
186 select port(ffrstmux, BA).is_fully_zero()
188 define <bool> pol (BA == \B)
195 dffrstmux = ffrstmux;
196 dffrstpol = ffrstpol;
197 argD = port(ffrstmux, ffrstpol ? \A : \B);
198 dffD.replace(port(ffrstmux, \Y), argD);
200 // Only search for ffcemux if argQ has at
201 // least 3 users (ff, <upstream>, ffrstmux) and
202 // dffD only has two (ff, ffrstmux)
203 if (!(nusers(argQ) >= 3 && nusers(dffD) == 2))
210 // (3) Match for a $mux cell implement clock enable semantics --- one that
211 // exclusively drives the 'D' input of the $dff (or the other input of
212 // the reset $mux) and where one of this $mux's inputs is connected to
213 // the 'Q' output of the $dff
216 select ffcemux->type.in($mux)
217 index <SigSpec> port(ffcemux, \Y) === argD
218 choice <IdString> AB {\A, \B}
219 index <SigSpec> port(ffcemux, AB) === argQ
220 define <bool> pol (AB == \A)
229 argD = port(ffcemux, ffcepol ? \B : \A);
230 dffD.replace(port(ffcemux, \Y), argD);