5 state <std::set<SigBit>> sigAset sigBset
6 state <SigSpec> sigA sigB sigCD sigH sigO sigOused
7 state <Cell*> addAB muxAB
10 select mul->type.in($mul, \SB_MAC16)
11 select GetSize(mul->getPort(\A)) + GetSize(mul->getPort(\B)) > 10
15 SigSpec A = port(mul, \A);
17 sigAset = A.to_sigbit_set();
18 SigSpec B = port(mul, \B);
20 sigBset = B.to_sigbit_set();
24 if (mul->type == $mul)
25 sigH = mul->getPort(\Y);
26 else if (mul->type == \SB_MAC16)
27 sigH = mul->getPort(\O);
29 if (GetSize(sigH) <= 10)
34 if mul->type != \SB_MAC16 || !param(mul, \A_REG).as_bool()
36 select ffA->type.in($dff)
37 filter includes(port(ffA, \Q).to_sigbit_set(), sigAset)
41 code sigA clock clock_pol
45 for (auto b : port(ffA, \Q))
46 if (b.wire->get_bool_attribute(\keep))
49 clock = port(ffA, \CLK).as_bit();
50 clock_pol = param(ffA, \CLK_POLARITY).as_bool();
52 sigA.replace(port(ffA, \Q), port(ffA, \D));
57 if mul->type != \SB_MAC16 || !param(mul, \B_REG).as_bool()
59 select ffB->type.in($dff)
60 filter includes(port(ffB, \Q).to_sigbit_set(), sigBset)
64 code sigB clock clock_pol
68 for (auto b : port(ffB, \Q))
69 if (b.wire->get_bool_attribute(\keep))
72 SigBit c = port(ffB, \CLK).as_bit();
73 bool cp = param(ffB, \CLK_POLARITY).as_bool();
75 if (clock != SigBit() && (c != clock || cp != clock_pol))
81 sigB.replace(port(ffB, \Q), port(ffB, \D));
86 if mul->type != \SB_MAC16 || (!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())
87 select ffFJKG->type.in($dff)
88 select nusers(port(ffFJKG, \D)) == 2
89 index <SigSpec> port(ffFJKG, \D) === sigH
90 // Ensure pipeline register is not already used
94 code sigH sigO clock clock_pol
96 sigH = port(ffFJKG, \Q);
98 if (b.wire->get_bool_attribute(\keep))
101 SigBit c = port(ffFJKG, \CLK).as_bit();
102 bool cp = param(ffFJKG, \CLK_POLARITY).as_bool();
104 if (clock != SigBit() && (c != clock || cp != clock_pol))
115 select addA->type.in($add)
116 select nusers(port(addA, \A)) == 2
117 filter param(addA, \A_WIDTH).as_int() <= GetSize(sigH)
118 //index <SigSpec> port(addA, \A) === sigH.extract(0, param(addA, \A_WIDTH).as_int())
119 filter port(addA, \A) == sigH.extract(0, param(addA, \A_WIDTH).as_int())
125 select addB->type.in($add, $sub)
126 select nusers(port(addB, \B)) == 2
127 filter param(addB, \B_WIDTH).as_int() <= GetSize(sigH)
128 //index <SigSpec> port(addB, \B) === sigH.extract(0, param(addB, \B_WIDTH).as_int())
129 filter port(addB, \B) == sigH.extract(0, param(addB, \B_WIDTH).as_int())
133 code addAB sigCD sigO
134 bool CD_SIGNED = false;
137 sigCD = port(addAB, \B);
138 CD_SIGNED = param(addAB, \B_SIGNED).as_bool();
142 sigCD = port(addAB, \A);
143 CD_SIGNED = param(addAB, \A_SIGNED).as_bool();
146 if (mul->type == \SB_MAC16) {
147 // Ensure that adder is not used
148 if (param(mul, \TOPOUTPUT_SELECT).as_int() != 3 ||
149 param(mul, \BOTOUTPUT_SELECT).as_int() != 3)
153 int natural_mul_width = GetSize(sigA) + GetSize(sigB);
154 int actual_mul_width = GetSize(sigH);
155 int actual_acc_width = GetSize(sigCD);
157 if ((actual_acc_width > actual_mul_width) && (natural_mul_width > actual_mul_width))
159 // If accumulator, check adder width and signedness
160 if (sigCD == sigH && (actual_acc_width != actual_mul_width) && (param(mul, \A_SIGNED).as_bool() != param(addAB, \A_SIGNED).as_bool()))
163 sigO = port(addAB, \Y);
164 sigCD.extend_u0(32, CD_SIGNED);
169 select muxA->type.in($mux)
170 index <int> nusers(port(muxA, \A)) === 2
171 index <SigSpec> port(muxA, \A) === sigO
177 select muxB->type.in($mux)
178 index <int> nusers(port(muxB, \B)) === 2
179 index <SigSpec> port(muxB, \B) === sigO
190 // Extract the bits of P that actually have a consumer
191 // (as opposed to being a dummy)
193 for (int i = 0; i < GetSize(sigO); i++)
194 if (!sigO[i].wire || nusers(sigO[i]) == 1)
195 sigOused.append(State::Sx);
197 sigOused.append(sigO[i]);
201 if nusers(sigOused.extract(0,std::min(16,GetSize(sigOused)))) == 2
202 select ffO_lo->type.in($dff)
208 SigSpec O = sigOused.extract(0,std::min(16,param(ffO_lo, \WIDTH).as_int()));
210 if (!includes(port(ffO_lo, \D).to_sigbit_set(), O.to_sigbit_set()))
216 if GetSize(sigOused) > 16
217 if nusers(sigOused.extract_end(16)) == 2
218 select ffO_hi->type.in($dff)
224 SigSpec O = sigOused.extract_end(16);
226 if (!includes(port(ffO_hi, \D).to_sigbit_set(), O.to_sigbit_set()))
231 code clock clock_pol sigO sigCD
232 if (ffO_lo || ffO_hi) {
233 if (mul->type == \SB_MAC16) {
234 // Ensure that register is not already used
235 if (param(mul, \TOPOUTPUT_SELECT).as_int() == 1 ||
236 param(mul, \BOTOUTPUT_SELECT).as_int() == 1)
239 // Ensure that OLOADTOP/OLOADBOT is unused or zero
240 if ((mul->hasPort(\OLOADTOP) && !port(mul, \OLOADTOP).is_fully_zero())
241 || (mul->hasPort(\OLOADBOT) && !port(mul, \OLOADBOT).is_fully_zero()))
246 for (auto b : port(ffO_lo, \Q))
247 if (b.wire->get_bool_attribute(\keep))
250 SigBit c = port(ffO_lo, \CLK).as_bit();
251 bool cp = param(ffO_lo, \CLK_POLARITY).as_bool();
253 if (clock != SigBit() && (c != clock || cp != clock_pol))
259 sigO.replace(port(ffO_lo, \D), port(ffO_lo, \Q));
263 for (auto b : port(ffO_hi, \Q))
264 if (b.wire->get_bool_attribute(\keep))
267 SigBit c = port(ffO_hi, \CLK).as_bit();
268 bool cp = param(ffO_hi, \CLK_POLARITY).as_bool();
270 if (clock != SigBit() && (c != clock || cp != clock_pol))
276 sigO.replace(port(ffO_hi, \D), port(ffO_hi, \Q));
279 // Loading value into output register is not
280 // supported unless using accumulator
285 sigCD = port(muxAB, \B);
287 sigCD = port(muxAB, \A);
289 sigCD.extend_u0(32, addAB && param(addAB, \A_SIGNED).as_bool() && param(addAB, \B_SIGNED).as_bool());