5 state <SigSpec> sigA sigB sigCD sigH sigO sigOused
6 state <Cell*> addAB muxAB
9 select mul->type.in($mul, \SB_MAC16)
10 select GetSize(mul->getPort(\A)) + GetSize(mul->getPort(\B)) > 10
14 if (mul->type == $mul)
15 sigH = mul->getPort(\Y);
16 else if (mul->type == \SB_MAC16)
17 sigH = mul->getPort(\O);
19 if (GetSize(sigH) <= 10)
24 if mul->type != \SB_MAC16 || !param(mul, \A_REG).as_bool()
25 if !port(mul, \A).remove_const().empty()
26 select ffA->type.in($dff)
27 filter includes(port(ffA, \Q).to_sigbit_set(), port(mul, \A).remove_const().to_sigbit_set())
31 code sigA clock clock_pol
35 for (auto b : port(ffA, \Q))
36 if (b.wire->get_bool_attribute(\keep))
39 clock = port(ffA, \CLK).as_bit();
40 clock_pol = param(ffA, \CLK_POLARITY).as_bool();
42 sigA.replace(port(ffA, \Q), port(ffA, \D));
47 if mul->type != \SB_MAC16 || !param(mul, \B_REG).as_bool()
48 if !port(mul, \B).remove_const().empty()
49 select ffB->type.in($dff)
50 filter includes(port(ffB, \Q).to_sigbit_set(), port(mul, \B).remove_const().to_sigbit_set())
54 code sigB clock clock_pol
58 for (auto b : port(ffB, \Q))
59 if (b.wire->get_bool_attribute(\keep))
62 SigBit c = port(ffB, \CLK).as_bit();
63 bool cp = param(ffB, \CLK_POLARITY).as_bool();
65 if (clock != SigBit() && (c != clock || cp != clock_pol))
71 sigB.replace(port(ffB, \Q), port(ffB, \D));
76 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())
77 select ffH->type.in($dff)
78 select nusers(port(ffH, \D)) == 2
79 index <SigSpec> port(ffH, \D) === sigH
80 // Ensure pipeline register is not already used
84 code sigH sigO clock clock_pol
90 if (b.wire->get_bool_attribute(\keep))
95 SigBit c = port(ffH, \CLK).as_bit();
96 bool cp = param(ffH, \CLK_POLARITY).as_bool();
98 if (clock != SigBit() && (c != clock || cp != clock_pol))
107 select addA->type.in($add)
108 select nusers(port(addA, \A)) == 2
109 filter param(addA, \A_WIDTH).as_int() <= GetSize(sigH)
110 //index <SigSpec> port(addA, \A) === sigH.extract(0, param(addA, \A_WIDTH).as_int())
111 filter port(addA, \A) == sigH.extract(0, param(addA, \A_WIDTH).as_int())
117 select addB->type.in($add, $sub)
118 select nusers(port(addB, \B)) == 2
119 filter param(addB, \B_WIDTH).as_int() <= GetSize(sigH)
120 //index <SigSpec> port(addB, \B) === sigH.extract(0, param(addB, \B_WIDTH).as_int())
121 filter port(addB, \B) == sigH.extract(0, param(addB, \B_WIDTH).as_int())
125 code addAB sigCD sigO
126 bool CD_SIGNED = false;
129 sigCD = port(addAB, \B);
130 CD_SIGNED = param(addAB, \B_SIGNED).as_bool();
134 sigCD = port(addAB, \A);
135 CD_SIGNED = param(addAB, \A_SIGNED).as_bool();
138 if (mul->type == \SB_MAC16) {
139 // Ensure that adder is not used
140 if (param(mul, \TOPOUTPUT_SELECT).as_int() != 3 ||
141 param(mul, \BOTOUTPUT_SELECT).as_int() != 3)
145 int natural_mul_width = GetSize(sigA) + GetSize(sigB);
146 int actual_mul_width = GetSize(sigH);
147 int actual_acc_width = GetSize(sigCD);
149 if ((actual_acc_width > actual_mul_width) && (natural_mul_width > actual_mul_width))
151 // If accumulator, check adder width and signedness
152 if (sigCD == sigH && (actual_acc_width != actual_mul_width) && (param(mul, \A_SIGNED).as_bool() != param(addAB, \A_SIGNED).as_bool()))
155 sigO = port(addAB, \Y);
156 sigCD.extend_u0(32, CD_SIGNED);
161 select muxA->type.in($mux)
162 index <int> nusers(port(muxA, \A)) === 2
163 index <SigSpec> port(muxA, \A) === sigO
169 select muxB->type.in($mux)
170 index <int> nusers(port(muxB, \B)) === 2
171 index <SigSpec> port(muxB, \B) === sigO
182 // Extract the bits of P that actually have a consumer
183 // (as opposed to being a dummy)
185 for (int i = 0; i < GetSize(sigO); i++)
186 if (!sigO[i].wire || nusers(sigO[i]) == 1)
187 sigOused.append(State::Sx);
189 sigOused.append(sigO[i]);
193 if nusers(sigOused.extract(0,std::min(16,GetSize(sigOused)))) == 2
194 select ffO_lo->type.in($dff)
195 filter includes(port(ffO_lo, \D).to_sigbit_set(), sigOused.extract(0,std::min(16,param(ffO_lo, \WIDTH).as_int())).remove_const().to_sigbit_set())
200 if GetSize(sigOused) > 16
201 if nusers(sigOused.extract_end(16)) == 2
202 select ffO_hi->type.in($dff)
203 filter includes(port(ffO_hi, \D).to_sigbit_set(), sigOused.extract_end(16).remove_const().to_sigbit_set())
207 code clock clock_pol sigO sigCD
208 if (ffO_lo || ffO_hi) {
209 if (mul->type == \SB_MAC16) {
210 // Ensure that register is not already used
211 if (param(mul, \TOPOUTPUT_SELECT).as_int() == 1 ||
212 param(mul, \BOTOUTPUT_SELECT).as_int() == 1)
215 // Ensure that OLOADTOP/OLOADBOT is unused or zero
216 if ((mul->hasPort(\OLOADTOP) && !port(mul, \OLOADTOP).is_fully_zero())
217 || (mul->hasPort(\OLOADBOT) && !port(mul, \OLOADBOT).is_fully_zero()))
222 for (auto b : port(ffO_lo, \Q))
223 if (b.wire->get_bool_attribute(\keep))
226 SigBit c = port(ffO_lo, \CLK).as_bit();
227 bool cp = param(ffO_lo, \CLK_POLARITY).as_bool();
229 if (clock != SigBit() && (c != clock || cp != clock_pol))
235 sigO.replace(port(ffO_lo, \D), port(ffO_lo, \Q));
239 for (auto b : port(ffO_hi, \Q))
240 if (b.wire->get_bool_attribute(\keep))
243 SigBit c = port(ffO_hi, \CLK).as_bit();
244 bool cp = param(ffO_hi, \CLK_POLARITY).as_bool();
246 if (clock != SigBit() && (c != clock || cp != clock_pol))
252 sigO.replace(port(ffO_hi, \D), port(ffO_hi, \Q));
255 // Loading value into output register is not
256 // supported unless using accumulator
261 sigCD = port(muxAB, \B);
263 sigCD = port(muxAB, \A);
265 sigCD.extend_u0(32, addAB && param(addAB, \A_SIGNED).as_bool() && param(addAB, \B_SIGNED).as_bool());