Debian Codegraph @

himbaechel/uarch/gowin/pack_dsp.cc

raw
1 #include "design_utils.h"
2 #include "log.h"
3 #include "nextpnr.h"
4
5 #define HIMBAECHEL_CONSTIDS "uarch/gowin/constids.inc"
6 #include "himbaechel_constids.h"
7 #include "himbaechel_helpers.h"
8
9 #include "gowin.h"
10 #include "gowin_utils.h"
11 #include "pack.h"
12
13 #include <cinttypes>
14
15 NEXTPNR_NAMESPACE_BEGIN
16
17 // ===================================
18 // DSP
19 // ===================================
20 void GowinPacker::pass_net_type(CellInfo *ci, IdString port)
21 {
22 const NetInfo *net = ci->getPort(port);
23 std::string connected_net = "NET";
24 if (net != nullptr) {
25 if (net->name == ctx->id("$PACKER_VCC")) {
26 connected_net = "VCC";
27 } else if (net->name == ctx->id("$PACKER_GND")) {
28 connected_net = "GND";
29 }
30 ci->setAttr(ctx->idf("NET_%s", port.c_str(ctx)), connected_net);
31 } else {
32 ci->setAttr(ctx->idf("NET_%s", port.c_str(ctx)), std::string(""));
33 }
34 }
35
36 void GowinPacker::pack_dsp(void)
37 {
38 std::vector<std::unique_ptr<CellInfo>> new_cells;
39 log_info("Pack DSP...\n");
40
41 std::vector<std::unique_ptr<CellInfo>> new_dsps;
42 std::vector<CellInfo *> dsp_heads;
43 std::vector<IdString> cells_to_remove;
44
45 auto process_dsp_cell = [&](CellInfo *ci) {
46 if (ctx->verbose) {
47 log_info(" pack %s %s\n", ci->type.c_str(ctx), ctx->nameOf(ci));
48 }
49 switch (ci->type.hash()) {
50 case ID_PADD9: {
51 gwu.remove_brackets(ci);
52 pass_net_type(ci, id_ASEL);
53
54 // ADD_SUB wire
55 IdString add_sub_net = ctx->id("$PACKER_GND");
56 if (ci->params.count(ctx->id("ADD_SUB"))) {
57 if (ci->params.at(ctx->id("ADD_SUB")).as_int64() == 1) {
58 add_sub_net = ctx->id("$PACKER_VCC");
59 }
60 }
61 ci->addInput(ctx->id("ADDSUB"));
62 ci->connectPort(ctx->id("ADDSUB"), ctx->nets.at(add_sub_net).get());
63
64 // PADD does not have outputs to the outside of the DSP -
65 // it is always connected to the inputs of the multiplier;
66 // to emulate a separate PADD primitive, we use
67 // multiplication by input C, equal to 1. We can switch the
68 // multiplier to multiplication mode by C in gowin_pack,
69 // but we will have to generate the value 1 at input C
70 // here.
71 ci->addInput(ctx->id("C0"));
72 ci->connectPort(ctx->id("C0"), ctx->nets.at(ctx->id("$PACKER_VCC")).get());
73 for (int i = 1; i < 9; ++i) {
74 ci->addInput(ctx->idf("C%d", i));
75 ci->connectPort(ctx->idf("C%d", i), ctx->nets.at(ctx->id("$PACKER_GND")).get());
76 }
77 // mark mult9x9 as used by making cluster
78 ci->cluster = ci->name;
79 ci->constr_abs_z = false;
80 ci->constr_x = 0;
81 ci->constr_y = 0;
82 ci->constr_y = 0;
83
84 IdString mult_name = gwu.create_aux_name(ci->name);
85 std::unique_ptr<CellInfo> mult_cell = gwu.create_cell(mult_name, id_DUMMY_CELL);
86 new_cells.push_back(std::move(mult_cell));
87 CellInfo *mult_ci = new_cells.back().get();
88
89 mult_ci->cluster = ci->name;
90 mult_ci->constr_x = 0;
91 mult_ci->constr_y = 0;
92 mult_ci->constr_z = gwu.get_dsp_mult_from_padd(0);
93
94 // DSP head?
95 if (gwu.dsp_bus_src(ci, "SI", 9) == nullptr && gwu.dsp_bus_dst(ci, "SBO", 9) == nullptr) {
96 for (int i = 0; i < 9; ++i) {
97 ci->disconnectPort(ctx->idf("SI%d", i));
98 ci->disconnectPort(ctx->idf("SBO%d", i));
99 }
100 dsp_heads.push_back(ci);
101 if (ctx->verbose) {
102 log_info(" found a DSP head: %s\n", ctx->nameOf(ci));
103 }
104 }
105 } break;
106 case ID_PADD18: {
107 pass_net_type(ci, id_ASEL);
108 gwu.remove_brackets(ci);
109
110 // ADD_SUB wire
111 IdString add_sub_net = ctx->id("$PACKER_GND");
112 if (ci->params.count(ctx->id("ADD_SUB"))) {
113 if (ci->params.at(ctx->id("ADD_SUB")).as_int64() == 1) {
114 add_sub_net = ctx->id("$PACKER_VCC");
115 }
116 }
117 ci->addInput(ctx->id("ADDSUB"));
118 ci->connectPort(ctx->id("ADDSUB"), ctx->nets.at(add_sub_net).get());
119
120 // XXX form C as 1
121 ci->addInput(ctx->id("C0"));
122 ci->connectPort(ctx->id("C0"), ctx->nets.at(ctx->id("$PACKER_VCC")).get());
123 for (int i = 1; i < 18; ++i) {
124 ci->addInput(ctx->idf("C%d", i));
125 ci->connectPort(ctx->idf("C%d", i), ctx->nets.at(ctx->id("$PACKER_GND")).get());
126 }
127 //
128 // add padd9s and mult9s as a children
129 ci->cluster = ci->name;
130 ci->constr_abs_z = false;
131 ci->constr_x = 0;
132 ci->constr_y = 0;
133 ci->constr_z = 0;
134 ci->constr_children.clear();
135
136 for (int i = 0; i < 2; ++i) {
137 IdString padd_name = gwu.create_aux_name(ci->name, i * 2);
138 std::unique_ptr<CellInfo> padd_cell = gwu.create_cell(padd_name, id_DUMMY_CELL);
139 new_cells.push_back(std::move(padd_cell));
140 CellInfo *padd_ci = new_cells.back().get();
141
142 padd_ci->cluster = ci->name;
143 padd_ci->constr_abs_z = false;
144 padd_ci->constr_x = 0;
145 padd_ci->constr_y = 0;
146 padd_ci->constr_z = BelZ::PADD9_0_0_Z - BelZ::PADD18_0_0_Z + i;
147
148 IdString mult_name = gwu.create_aux_name(ci->name, i * 2 + 1);
149 std::unique_ptr<CellInfo> mult_cell = gwu.create_cell(mult_name, id_DUMMY_CELL);
150 new_cells.push_back(std::move(mult_cell));
151 CellInfo *mult_ci = new_cells.back().get();
152
153 mult_ci->cluster = ci->name;
154 mult_ci->constr_abs_z = false;
155 mult_ci->constr_x = 0;
156 mult_ci->constr_y = 0;
157 mult_ci->constr_z = BelZ::MULT9X9_0_0_Z - BelZ::PADD18_0_0_Z + i;
158 }
159 // DSP head?
160 if (gwu.dsp_bus_src(ci, "SI", 18) == nullptr && gwu.dsp_bus_dst(ci, "SBO", 18) == nullptr) {
161 for (int i = 0; i < 18; ++i) {
162 ci->disconnectPort(ctx->idf("SI%d", i));
163 ci->disconnectPort(ctx->idf("SBO%d", i));
164 }
165 dsp_heads.push_back(ci);
166 if (ctx->verbose) {
167 log_info(" found a DSP head: %s\n", ctx->nameOf(ci));
168 }
169 }
170 } break;
171 case ID_MULT9X9: {
172 pass_net_type(ci, id_ASEL);
173 pass_net_type(ci, id_BSEL);
174 gwu.remove_brackets(ci);
175
176 // add padd9 as a child
177 ci->cluster = ci->name;
178 ci->constr_abs_z = false;
179 ci->constr_x = 0;
180 ci->constr_y = 0;
181 ci->constr_z = 0;
182 ci->constr_children.clear();
183
184 IdString padd_name = gwu.create_aux_name(ci->name);
185 std::unique_ptr<CellInfo> padd_cell = gwu.create_cell(padd_name, id_DUMMY_CELL);
186 new_cells.push_back(std::move(padd_cell));
187 CellInfo *padd_ci = new_cells.back().get();
188
189 padd_ci->cluster = ci->name;
190 padd_ci->constr_abs_z = false;
191 padd_ci->constr_x = 0;
192 padd_ci->constr_y = 0;
193 padd_ci->constr_z = BelZ::PADD9_0_0_Z - BelZ::MULT9X9_0_0_Z;
194
195 // DSP head?
196 if (gwu.dsp_bus_src(ci, "SIA", 9) == nullptr && gwu.dsp_bus_src(ci, "SIB", 9) == nullptr) {
197 for (int i = 0; i < 9; ++i) {
198 ci->disconnectPort(ctx->idf("SIA%d", i));
199 ci->disconnectPort(ctx->idf("SIB%d", i));
200 }
201 dsp_heads.push_back(ci);
202 if (ctx->verbose) {
203 log_info(" found a DSP head: %s\n", ctx->nameOf(ci));
204 }
205 }
206 } break;
207 case ID_MULT12X12: {
208 gwu.remove_brackets(ci);
209 } break;
210 case ID_MULT18X18: {
211 pass_net_type(ci, id_ASEL);
212 pass_net_type(ci, id_BSEL);
213 gwu.remove_brackets(ci);
214
215 // add padd9s and mult9s as a children
216 ci->cluster = ci->name;
217 ci->constr_abs_z = false;
218 ci->constr_x = 0;
219 ci->constr_y = 0;
220 ci->constr_z = 0;
221 ci->constr_children.clear();
222
223 for (int i = 0; i < 2; ++i) {
224 IdString padd_name = gwu.create_aux_name(ci->name, i * 2);
225 std::unique_ptr<CellInfo> padd_cell = gwu.create_cell(padd_name, id_DUMMY_CELL);
226 new_cells.push_back(std::move(padd_cell));
227 CellInfo *padd_ci = new_cells.back().get();
228
229 padd_ci->cluster = ci->name;
230 padd_ci->constr_abs_z = false;
231 padd_ci->constr_x = 0;
232 padd_ci->constr_y = 0;
233 padd_ci->constr_z = BelZ::PADD9_0_0_Z - BelZ::MULT18X18_0_0_Z + i;
234
235 IdString mult_name = gwu.create_aux_name(ci->name, i * 2 + 1);
236 std::unique_ptr<CellInfo> mult_cell = gwu.create_cell(mult_name, id_DUMMY_CELL);
237 new_cells.push_back(std::move(mult_cell));
238 CellInfo *mult_ci = new_cells.back().get();
239
240 mult_ci->cluster = ci->name;
241 mult_ci->constr_abs_z = false;
242 mult_ci->constr_x = 0;
243 mult_ci->constr_y = 0;
244 mult_ci->constr_z = BelZ::MULT9X9_0_0_Z - BelZ::MULT18X18_0_0_Z + i;
245 }
246
247 // DSP head?
248 if (gwu.dsp_bus_src(ci, "SIA", 18) == nullptr && gwu.dsp_bus_src(ci, "SIB", 18) == nullptr) {
249 for (int i = 0; i < 18; ++i) {
250 ci->disconnectPort(ctx->idf("SIA%d", i));
251 ci->disconnectPort(ctx->idf("SIB%d", i));
252 }
253 dsp_heads.push_back(ci);
254 if (ctx->verbose) {
255 log_info(" found a DSP head: %s\n", ctx->nameOf(ci));
256 }
257 }
258 } break;
259 case ID_ALU54D: {
260 pass_net_type(ci, id_ACCLOAD);
261 gwu.remove_brackets(ci);
262
263 // ACCLOAD - It looks like these wires are always connected to each other.
264 ci->cell_bel_pins.at(id_ACCLOAD).clear();
265 ci->cell_bel_pins.at(id_ACCLOAD).push_back(id_ACCLOAD0);
266 ci->cell_bel_pins.at(id_ACCLOAD).push_back(id_ACCLOAD1);
267
268 // add padd9s and mult9s as a children
269 ci->cluster = ci->name;
270 ci->constr_abs_z = false;
271 ci->constr_x = 0;
272 ci->constr_y = 0;
273 ci->constr_z = 0;
274 ci->constr_children.clear();
275
276 for (int i = 0; i < 4; ++i) {
277 IdString padd_name = gwu.create_aux_name(ci->name, i * 2);
278 std::unique_ptr<CellInfo> padd_cell = gwu.create_cell(padd_name, id_DUMMY_CELL);
279 new_cells.push_back(std::move(padd_cell));
280 CellInfo *padd_ci = new_cells.back().get();
281
282 padd_ci->cluster = ci->name;
283 padd_ci->constr_abs_z = false;
284 padd_ci->constr_x = 0;
285 padd_ci->constr_y = 0;
286 padd_ci->constr_z = BelZ::PADD9_0_0_Z - BelZ::ALU54D_0_Z + 4 * (i / 2) + (i % 2);
287
288 IdString mult_name = gwu.create_aux_name(ci->name, i * 2 + 1);
289 std::unique_ptr<CellInfo> mult_cell = gwu.create_cell(mult_name, id_DUMMY_CELL);
290 new_cells.push_back(std::move(mult_cell));
291 CellInfo *mult_ci = new_cells.back().get();
292
293 mult_ci->cluster = ci->name;
294 mult_ci->constr_abs_z = false;
295 mult_ci->constr_x = 0;
296 mult_ci->constr_y = 0;
297 mult_ci->constr_z = BelZ::MULT9X9_0_0_Z - BelZ::ALU54D_0_Z + 4 * (i / 2) + (i % 2);
298 }
299
300 // DSP head?
301 if (gwu.dsp_bus_src(ci, "CASI", 55) == nullptr) {
302 for (int i = 0; i < 55; ++i) {
303 ci->disconnectPort(ctx->idf("CASI%d", i));
304 }
305 dsp_heads.push_back(ci);
306 if (ctx->verbose) {
307 log_info(" found a DSP head: %s\n", ctx->nameOf(ci));
308 }
309 }
310 } break;
311 case ID_MULTALU18X18: {
312 // Ports C and D conflict so we need to know the operating mode here.
313 if (ci->params.count(id_MULTALU18X18_MODE) == 0) {
314 ci->setParam(id_MULTALU18X18_MODE, 0);
315 }
316 int multalu18x18_mode = ci->params.at(id_MULTALU18X18_MODE).as_int64();
317 if (multalu18x18_mode < 0 || multalu18x18_mode > 2) {
318 log_error("%s MULTALU18X18_MODE is not in {0, 1, 2}.\n", ctx->nameOf(ci));
319 }
320 NetInfo *vss_net = ctx->nets.at(ctx->id("$PACKER_GND")).get();
321
322 for (int i = 0; i < 54; ++i) {
323 if (i < 18) {
324 if (multalu18x18_mode != 2) {
325 ci->renamePort(ctx->idf("A[%d]", i), ctx->idf("A%d1", i));
326 ci->renamePort(ctx->idf("B[%d]", i), ctx->idf("B%d1", i));
327 } else {
328 ci->renamePort(ctx->idf("A[%d]", i), ctx->idf("A%d0", i));
329 ci->renamePort(ctx->idf("B[%d]", i), ctx->idf("B%d0", i));
330 }
331 }
332 }
333
334 gwu.remove_brackets(ci);
335 for (int i = 0; i < 54; ++i) {
336 switch (multalu18x18_mode) {
337 case 0:
338 ci->disconnectPort(ctx->idf("D%d", i));
339 break;
340 case 1:
341 ci->disconnectPort(ctx->idf("C%d", i));
342 ci->disconnectPort(ctx->idf("D%d", i));
343 break;
344 case 2:
345 ci->disconnectPort(ctx->idf("C%d", i));
346 break;
347 default:
348 break;
349 }
350 }
351 if (multalu18x18_mode != 2) {
352 ci->renamePort(id_ASIGN, id_ASIGN1);
353 ci->renamePort(id_BSIGN, id_BSIGN1);
354 ci->addInput(id_ASIGN0);
355 ci->addInput(id_BSIGN0);
356 ci->connectPort(id_ASIGN0, vss_net);
357 ci->connectPort(id_BSIGN0, vss_net);
358 ci->disconnectPort(id_DSIGN);
359 } else { // BSIGN0 and DSIGN are the same wire
360 ci->renamePort(id_ASIGN, id_ASIGN0);
361 ci->addInput(id_ASIGN1);
362 ci->connectPort(id_ASIGN1, vss_net);
363 ci->renamePort(id_BSIGN, id_BSIGN0);
364 }
365
366 // ACCLOAD - It looks like these wires are always connected to each other.
367 pass_net_type(ci, id_ACCLOAD);
368 ci->cell_bel_pins.at(id_ACCLOAD).clear();
369 ci->cell_bel_pins.at(id_ACCLOAD).push_back(id_ACCLOAD0);
370 ci->cell_bel_pins.at(id_ACCLOAD).push_back(id_ACCLOAD1);
371
372 // add padd9s and mult9s as a children
373 ci->cluster = ci->name;
374 ci->constr_abs_z = false;
375 ci->constr_x = 0;
376 ci->constr_y = 0;
377 ci->constr_z = 0;
378 ci->constr_children.clear();
379
380 for (int i = 0; i < 2; ++i) {
381 IdString padd_name = gwu.create_aux_name(ci->name, i * 2);
382 std::unique_ptr<CellInfo> padd_cell = gwu.create_cell(padd_name, id_DUMMY_CELL);
383 new_cells.push_back(std::move(padd_cell));
384 CellInfo *padd_ci = new_cells.back().get();
385
386 padd_ci->cluster = ci->name;
387 padd_ci->constr_abs_z = false;
388 padd_ci->constr_x = 0;
389 padd_ci->constr_y = 0;
390 padd_ci->constr_z = BelZ::PADD9_0_0_Z - BelZ::MULTALU18X18_0_Z + i;
391
392 IdString mult_name = gwu.create_aux_name(ci->name, i * 2 + 1);
393 std::unique_ptr<CellInfo> mult_cell = gwu.create_cell(mult_name, id_DUMMY_CELL);
394 new_cells.push_back(std::move(mult_cell));
395 CellInfo *mult_ci = new_cells.back().get();
396
397 mult_ci->cluster = ci->name;
398 mult_ci->constr_abs_z = false;
399 mult_ci->constr_x = 0;
400 mult_ci->constr_y = 0;
401 mult_ci->constr_z = BelZ::MULT9X9_0_0_Z - BelZ::MULTALU18X18_0_Z + i;
402 }
403
404 // DSP head?
405 if (gwu.dsp_bus_src(ci, "CASI", 55) == nullptr) {
406 for (int i = 0; i < 55; ++i) {
407 ci->disconnectPort(ctx->idf("CASI%d", i));
408 }
409 dsp_heads.push_back(ci);
410 if (ctx->verbose) {
411 log_info(" found a DSP head: %s\n", ctx->nameOf(ci));
412 }
413 }
414 } break;
415 case ID_MULT27X36: {
416 // We assemble that primitive from two MultAlu27x18 connected via CASO->CASI.
417 IdString l_mult_name = gwu.create_aux_name(ci->name, 1);
418 std::unique_ptr<CellInfo> l_mult_cell = gwu.create_cell(l_mult_name, id_MULTALU27X18);
419 new_dsps.push_back(std::move(l_mult_cell));
420 CellInfo *l_mult = new_dsps.back().get();
421
422 IdString h_mult_name = gwu.create_aux_name(ci->name, 2);
423 std::unique_ptr<CellInfo> h_mult_cell = gwu.create_cell(h_mult_name, id_MULTALU27X18);
424 new_dsps.push_back(std::move(h_mult_cell));
425 CellInfo *h_mult = new_dsps.back().get();
426
427 // SIAs are disconnected
428 for (int i = 0; i < 27; ++i) {
429 l_mult->addInput(ctx->idf("SIA[%d]", i));
430 h_mult->addInput(ctx->idf("SIA[%d]", i));
431 l_mult->addOutput(ctx->idf("SOA[%d]", i));
432 h_mult->addOutput(ctx->idf("SOA[%d]", i));
433 }
434
435 // unused
436 l_mult->addInput(id_CSEL);
437 h_mult->addInput(id_CSEL);
438 l_mult->addInput(id_ASEL);
439 h_mult->addInput(id_ASEL);
440 l_mult->addInput(id_ACCSEL);
441 h_mult->addInput(id_ACCSEL);
442
443 for (int i = 0; i < 48; ++i) {
444 // C is disconnected
445 l_mult->addInput(ctx->idf("C[%d]", i));
446 h_mult->addInput(ctx->idf("C[%d]", i));
447 // low mult CASI is disconnected
448 l_mult->addInput(ctx->idf("CASI[%d]", i));
449 // high mult CASO is disconnected
450 h_mult->addOutput(ctx->idf("CASO[%d]", i));
451 // low CASO -> high CASI
452 l_mult->addOutput(ctx->idf("CASO[%d]", i));
453 h_mult->addInput(ctx->idf("CASI[%d]", i));
454 l_mult->connectPorts(ctx->idf("CASO[%d]", i), h_mult, ctx->idf("CASI[%d]", i));
455 }
456
457 // input A is shared
458 ci->movePortBusTo(id_A, 0, 1, l_mult, id_A, 0, 1, 27);
459 l_mult->copyPortBusTo(id_A, 0, 1, h_mult, id_A, 0, 1, 27);
460
461 // input D is shared
462 ci->movePortBusTo(id_D, 0, 1, l_mult, id_D, 0, 1, 26);
463 l_mult->copyPortBusTo(id_D, 0, 1, h_mult, id_D, 0, 1, 26);
464
465 // input PSEL is shared
466 ci->movePortTo(id_PSEL, l_mult, id_PSEL);
467 l_mult->copyPortBusTo(id_PSEL, 0, 1, h_mult, id_PSEL, 0, 1, 26);
468
469 // input PADDSUB is shared
470 ci->movePortTo(id_PADDSUB, l_mult, id_PADDSUB);
471 l_mult->copyPortTo(id_PADDSUB, h_mult, id_PADDSUB);
472
473 // input B is divided
474 ci->movePortBusTo(id_B, 0, 1, l_mult, id_B, 0, 1, 18);
475 ci->movePortBusTo(id_B, 18, 1, h_mult, id_B, 0, 1, 18);
476
477 // output DOUT is divided
478 ci->movePortBusTo(id_DOUT, 0, 1, l_mult, id_DOUT, 0, 1, 18);
479 ci->movePortBusTo(id_DOUT, 18, 1, h_mult, id_DOUT, 0, 1, 47);
480
481 // Control inputs are shared
482 ci->movePortBusTo(id_CLK, 0, 1, l_mult, id_CLK, 0, 1, 2);
483 l_mult->copyPortBusTo(id_CLK, 0, 1, h_mult, id_CLK, 0, 1, 2);
484 ci->movePortBusTo(id_CE, 0, 1, l_mult, id_CE, 0, 1, 2);
485 l_mult->copyPortBusTo(id_CE, 0, 1, h_mult, id_CE, 0, 1, 2);
486 ci->movePortBusTo(id_RESET, 0, 1, l_mult, id_RESET, 0, 1, 2);
487 l_mult->copyPortBusTo(id_RESET, 0, 1, h_mult, id_RESET, 0, 1, 2);
488
489 // copy params
490 for (auto param : ci->params) {
491 l_mult->setParam(param.first, param.second);
492 h_mult->setParam(param.first, param.second);
493 }
494 // mark as MULT27X36
495 // we will catch these attributes during packing and add the missing parameters
496 l_mult->setAttr(id_MULT27X36_MAIN, 1);
497 h_mult->setAttr(id_MULT27X36_AUX, 1);
498
499 // remove former Mult27x36
500 cells_to_remove.push_back(ci->name);
501 } break;
502 case ID_MULTALU27X18: {
503 gwu.remove_brackets(ci);
504 pass_net_type(ci, id_ACCSEL);
505 ci->cell_bel_pins[id_ACCSEL].clear();
506 ci->cell_bel_pins.at(id_ACCSEL).push_back(id_ACCSEL0);
507 ci->cell_bel_pins.at(id_ACCSEL).push_back(id_ACCSEL1);
508
509 // mark 2 mult12x12 as parts of the cluster to prevent
510 // other multipliers from being placed there
511 ci->cluster = ci->name;
512 ci->constr_abs_z = false;
513 ci->constr_x = 0;
514 ci->constr_y = 0;
515 ci->constr_z = 0;
516 ci->constr_children.clear();
517
518 for (int i = 0; i < 2; ++i) {
519 IdString mult12x12_name = gwu.create_aux_name(ci->name, i * 2);
520 std::unique_ptr<CellInfo> mult12x12_cell = gwu.create_cell(mult12x12_name, id_DUMMY_CELL);
521 new_cells.push_back(std::move(mult12x12_cell));
522 CellInfo *mult12x12_ci = new_cells.back().get();
523
524 mult12x12_ci->cluster = ci->name;
525 mult12x12_ci->constr_abs_z = false;
526 mult12x12_ci->constr_x = 0;
527 mult12x12_ci->constr_y = 0;
528 mult12x12_ci->constr_z = BelZ::MULT12X12_0_Z - BelZ::MULTALU27X18_Z + i;
529 if (ctx->verbose) {
530 log_info(" mark %s multiplier as used by %s\n", mult12x12_name.c_str(ctx), ctx->nameOf(ci));
531 }
532 }
533
534 // DSP head? This primitive has the ability to form chains using both SO[AB] -> SI[AB] and
535 // CASO->CASI
536 bool cas_head = false;
537 if (gwu.dsp_bus_src(ci, "CASI", 48) == nullptr) {
538 for (int i = 0; i < 48; ++i) {
539 ci->disconnectPort(ctx->idf("CASI%d", i));
540 }
541 cas_head = true;
542 }
543 bool so_head = false;
544 if (gwu.dsp_bus_src(ci, "SIA", 27) == nullptr) {
545 for (int i = 0; i < 27; ++i) {
546 ci->disconnectPort(ctx->idf("SIA%d", i));
547 }
548 so_head = true;
549 }
550 if (cas_head && so_head) {
551 dsp_heads.push_back(ci);
552 if (ctx->verbose) {
553 log_info(" found a DSP head: %s\n", ctx->nameOf(ci));
554 }
555 }
556 } break;
557 case ID_MULTALU36X18: {
558 if (ci->params.count(id_MULTALU18X18_MODE) == 0) {
559 ci->setParam(id_MULTALU18X18_MODE, 0);
560 }
561 int multalu36x18_mode = ci->params.at(id_MULTALU36X18_MODE).as_int64();
562 if (multalu36x18_mode < 0 || multalu36x18_mode > 2) {
563 log_error("%s MULTALU36X18_MODE is not in {0, 1, 2}.\n", ctx->nameOf(ci));
564 }
565 NetInfo *vss_net = ctx->nets.at(ctx->id("$PACKER_GND")).get();
566
567 gwu.remove_brackets(ci);
568 for (int i = 0; i < 36; ++i) {
569 if (i < 18) {
570 ci->cell_bel_pins.at(ctx->idf("A[%d]", i)).clear();
571 ci->cell_bel_pins[ctx->idf("A%d", i)].push_back(ctx->idf("A%d0", i));
572 ci->cell_bel_pins.at(ctx->idf("A%d", i)).push_back(ctx->idf("A%d1", i));
573 }
574 }
575 for (int i = 0; i < 54; ++i) {
576 switch (multalu36x18_mode) {
577 case 1: /* fallthrough */
578 case 2:
579 ci->disconnectPort(ctx->idf("C%d", i));
580 break;
581 default:
582 break;
583 }
584 }
585
586 // both A have sign bit
587 // only MSB part of B has sign bit
588 ci->cell_bel_pins.at(id_ASIGN).clear();
589 ci->cell_bel_pins.at(id_ASIGN).push_back(id_ASIGN0);
590 ci->cell_bel_pins.at(id_ASIGN).push_back(id_ASIGN1);
591 ci->renamePort(id_BSIGN, id_BSIGN1);
592 ci->addInput(id_BSIGN0);
593 ci->connectPort(id_BSIGN0, vss_net);
594
595 pass_net_type(ci, id_ACCLOAD);
596 if (multalu36x18_mode == 1) {
597 if (ci->attrs.at(id_NET_ACCLOAD).as_string() == "GND" ||
598 ci->attrs.at(id_NET_ACCLOAD).as_string() == "VCC") {
599 ci->disconnectPort(id_ACCLOAD);
600 } else {
601 ci->addInput(id_ALUSEL4);
602 ci->addInput(id_ALUSEL6);
603 ci->cell_bel_pins.at(id_ACCLOAD).clear();
604 ci->cell_bel_pins.at(id_ACCLOAD).push_back(id_ALUSEL4);
605 ci->cell_bel_pins.at(id_ACCLOAD).push_back(id_ALUSEL6);
606 }
607 } else {
608 ci->disconnectPort(id_ACCLOAD);
609 }
610
611 // add padd9s and mult9s as a children
612 ci->cluster = ci->name;
613 ci->constr_abs_z = false;
614 ci->constr_x = 0;
615 ci->constr_y = 0;
616 ci->constr_z = 0;
617 ci->constr_children.clear();
618
619 for (int i = 0; i < 2; ++i) {
620 IdString padd_name = gwu.create_aux_name(ci->name, i * 2);
621 std::unique_ptr<CellInfo> padd_cell = gwu.create_cell(padd_name, id_DUMMY_CELL);
622 new_cells.push_back(std::move(padd_cell));
623 CellInfo *padd_ci = new_cells.back().get();
624
625 padd_ci->cluster = ci->name;
626 padd_ci->constr_abs_z = false;
627 padd_ci->constr_x = 0;
628 padd_ci->constr_y = 0;
629 padd_ci->constr_z = BelZ::PADD9_0_0_Z - BelZ::MULTALU36X18_0_Z + i;
630
631 IdString mult_name = gwu.create_aux_name(ci->name, i * 2 + 1);
632 std::unique_ptr<CellInfo> mult_cell = gwu.create_cell(mult_name, id_DUMMY_CELL);
633 new_cells.push_back(std::move(mult_cell));
634 CellInfo *mult_ci = new_cells.back().get();
635
636 mult_ci->cluster = ci->name;
637 mult_ci->constr_abs_z = false;
638 mult_ci->constr_x = 0;
639 mult_ci->constr_y = 0;
640 mult_ci->constr_z = BelZ::MULT9X9_0_0_Z - BelZ::MULTALU36X18_0_Z + i;
641 }
642
643 // DSP head?
644 if (gwu.dsp_bus_src(ci, "CASI", 55) == nullptr) {
645 for (int i = 0; i < 55; ++i) {
646 ci->disconnectPort(ctx->idf("CASI%d", i));
647 }
648 dsp_heads.push_back(ci);
649 if (ctx->verbose) {
650 log_info(" found a DSP head: %s\n", ctx->nameOf(ci));
651 }
652 }
653 } break;
654 case ID_MULTADDALU12X12: {
655 gwu.remove_brackets(ci);
656 pass_net_type(ci, id_ACCSEL);
657
658 ci->cell_bel_pins.at(id_ACCSEL).clear();
659 ci->cell_bel_pins.at(id_ACCSEL).push_back(id_ACCSEL0);
660 ci->cell_bel_pins.at(id_ACCSEL).push_back(id_ACCSEL1);
661
662 // mark 2 mult12x12 as parts of the cluster to prevent
663 // other multipliers from being placed there
664 ci->cluster = ci->name;
665 ci->constr_abs_z = false;
666 ci->constr_x = 0;
667 ci->constr_y = 0;
668 ci->constr_z = 0;
669 ci->constr_children.clear();
670
671 for (int i = 0; i < 2; ++i) {
672 IdString mult12x12_name = gwu.create_aux_name(ci->name, i * 2);
673 std::unique_ptr<CellInfo> mult12x12_cell = gwu.create_cell(mult12x12_name, id_DUMMY_CELL);
674 new_cells.push_back(std::move(mult12x12_cell));
675 CellInfo *mult12x12_ci = new_cells.back().get();
676
677 mult12x12_ci->cluster = ci->name;
678 mult12x12_ci->constr_abs_z = false;
679 mult12x12_ci->constr_x = 0;
680 mult12x12_ci->constr_y = 0;
681 mult12x12_ci->constr_z = BelZ::MULT12X12_0_Z - BelZ::MULTADDALU12X12_Z + i;
682 }
683
684 // DSP head?
685 if (gwu.dsp_bus_src(ci, "CASI", 48) == nullptr) {
686 for (int i = 0; i < 48; ++i) {
687 ci->disconnectPort(ctx->idf("CASI%d", i));
688 }
689 dsp_heads.push_back(ci);
690 if (ctx->verbose) {
691 log_info(" found a DSP head: %s\n", ctx->nameOf(ci));
692 }
693 }
694
695 } break;
696 case ID_MULTADDALU18X18: {
697 if (ci->params.count(id_MULTADDALU18X18_MODE) == 0) {
698 ci->setParam(id_MULTADDALU18X18_MODE, 0);
699 }
700 int multaddalu18x18_mode = ci->params.at(id_MULTADDALU18X18_MODE).as_int64();
701 if (multaddalu18x18_mode < 0 || multaddalu18x18_mode > 2) {
702 log_error("%s MULTADDALU18X18_MODE is not in {0, 1, 2}.\n", ctx->nameOf(ci));
703 }
704 for (int i = 0; i < 54; ++i) {
705 if (i < 18) {
706 ci->renamePort(ctx->idf("A0[%d]", i), ctx->idf("A%d0", i));
707 ci->renamePort(ctx->idf("B0[%d]", i), ctx->idf("B%d0", i));
708 ci->renamePort(ctx->idf("A1[%d]", i), ctx->idf("A%d1", i));
709 ci->renamePort(ctx->idf("B1[%d]", i), ctx->idf("B%d1", i));
710 }
711 if (multaddalu18x18_mode == 0) {
712 ci->renamePort(ctx->idf("C[%d]", i), ctx->idf("C%d", i));
713 } else {
714 ci->disconnectPort(ctx->idf("C[%d]", i));
715 }
716 }
717 gwu.remove_brackets(ci);
718
719 pass_net_type(ci, id_ASEL0);
720 pass_net_type(ci, id_ASEL1);
721 pass_net_type(ci, id_BSEL0);
722 pass_net_type(ci, id_BSEL1);
723 pass_net_type(ci, id_ACCLOAD);
724 if (multaddalu18x18_mode == 1) {
725 if (ci->attrs.at(id_NET_ACCLOAD).as_string() == "GND" ||
726 ci->attrs.at(id_NET_ACCLOAD).as_string() == "VCC") {
727 ci->disconnectPort(id_ACCLOAD);
728 } else {
729 ci->addInput(id_ALUSEL4);
730 ci->addInput(id_ALUSEL6);
731 ci->cell_bel_pins.at(id_ACCLOAD).clear();
732 ci->cell_bel_pins.at(id_ACCLOAD).push_back(id_ALUSEL4);
733 ci->cell_bel_pins.at(id_ACCLOAD).push_back(id_ALUSEL6);
734 }
735 } else {
736 ci->disconnectPort(id_ACCLOAD);
737 }
738
739 // add padd9s and mult9s as a children
740 ci->cluster = ci->name;
741 ci->constr_abs_z = false;
742 ci->constr_x = 0;
743 ci->constr_y = 0;
744 ci->constr_z = 0;
745 ci->constr_children.clear();
746
747 for (int i = 0; i < 2; ++i) {
748 IdString padd_name = gwu.create_aux_name(ci->name, i * 2);
749 std::unique_ptr<CellInfo> padd_cell = gwu.create_cell(padd_name, id_DUMMY_CELL);
750 new_cells.push_back(std::move(padd_cell));
751 CellInfo *padd_ci = new_cells.back().get();
752
753 padd_ci->cluster = ci->name;
754 padd_ci->constr_abs_z = false;
755 padd_ci->constr_x = 0;
756 padd_ci->constr_y = 0;
757 padd_ci->constr_z = BelZ::PADD9_0_0_Z - BelZ::MULTADDALU18X18_0_Z + i;
758
759 IdString mult_name = gwu.create_aux_name(ci->name, i * 2 + 1);
760 std::unique_ptr<CellInfo> mult_cell = gwu.create_cell(mult_name, id_DUMMY_CELL);
761 new_cells.push_back(std::move(mult_cell));
762 CellInfo *mult_ci = new_cells.back().get();
763
764 mult_ci->cluster = ci->name;
765 mult_ci->constr_abs_z = false;
766 mult_ci->constr_x = 0;
767 mult_ci->constr_y = 0;
768 mult_ci->constr_z = BelZ::MULT9X9_0_0_Z - BelZ::MULTADDALU18X18_0_Z + i;
769 }
770
771 // DSP head? This primitive has the ability to form chains using both SO[AB] -> SI[AB] and
772 // CASO->CASI
773 bool cas_head = false;
774 if (gwu.dsp_bus_src(ci, "CASI", 55) == nullptr) {
775 for (int i = 0; i < 55; ++i) {
776 ci->disconnectPort(ctx->idf("CASI%d", i));
777 }
778 cas_head = true;
779 }
780 bool so_head = false;
781 if (gwu.dsp_bus_src(ci, "SIA", 18) == nullptr && gwu.dsp_bus_src(ci, "SIB", 18) == nullptr) {
782 for (int i = 0; i < 18; ++i) {
783 ci->disconnectPort(ctx->idf("SIA%d", i));
784 ci->disconnectPort(ctx->idf("SIB%d", i));
785 }
786 so_head = true;
787 }
788 if (cas_head && so_head) {
789 dsp_heads.push_back(ci);
790 if (ctx->verbose) {
791 log_info(" found a DSP head: %s\n", ctx->nameOf(ci));
792 }
793 }
794 } break;
795 case ID_MULT36X36: {
796 for (int i = 0; i < 36; ++i) {
797 ci->cell_bel_pins.at(ctx->idf("A[%d]", i)).clear();
798 ci->cell_bel_pins.at(ctx->idf("A[%d]", i)).push_back(ctx->idf("A%d0", i));
799 ci->cell_bel_pins.at(ctx->idf("A[%d]", i)).push_back(ctx->idf("A%d1", i));
800 ci->cell_bel_pins.at(ctx->idf("B[%d]", i)).clear();
801 ci->cell_bel_pins.at(ctx->idf("B[%d]", i)).push_back(ctx->idf("B%d0", i));
802 ci->cell_bel_pins.at(ctx->idf("B[%d]", i)).push_back(ctx->idf("B%d1", i));
803 }
804 // only MSB sign bits
805 ci->cell_bel_pins.at(id_ASIGN).clear();
806 ci->cell_bel_pins.at(id_ASIGN).push_back(id_ASIGN0);
807 ci->cell_bel_pins.at(id_ASIGN).push_back(id_ASIGN1);
808 ci->cell_bel_pins.at(id_BSIGN).clear();
809 ci->cell_bel_pins.at(id_BSIGN).push_back(id_BSIGN0);
810 ci->cell_bel_pins.at(id_BSIGN).push_back(id_BSIGN1);
811
812 // LSB sign bits = 0
813 NetInfo *vss_net = ctx->nets.at(ctx->id("$PACKER_GND")).get();
814 ci->addInput(id_ZERO_SIGN);
815 ci->cell_bel_pins[id_ZERO_SIGN].push_back(id_ZERO_ASIGN0);
816 ci->cell_bel_pins.at(id_ZERO_SIGN).push_back(id_ZERO_BSIGN0);
817 ci->cell_bel_pins.at(id_ZERO_SIGN).push_back(id_ZERO_BSIGN1);
818 ci->cell_bel_pins.at(id_ZERO_SIGN).push_back(id_ZERO_ASIGN1);
819 ci->connectPort(id_ZERO_SIGN, vss_net);
820
821 for (int i = 0; i < 72; ++i) {
822 ci->renamePort(ctx->idf("DOUT[%d]", i), ctx->idf("DOUT%d", i));
823 }
824
825 // add padd9s and mult9s as a children
826 ci->cluster = ci->name;
827 ci->constr_abs_z = false;
828 ci->constr_x = 0;
829 ci->constr_y = 0;
830 ci->constr_z = 0;
831 ci->constr_children.clear();
832
833 for (int i = 0; i < 8; ++i) {
834 IdString padd_name = gwu.create_aux_name(ci->name, i * 2);
835 std::unique_ptr<CellInfo> padd_cell = gwu.create_cell(padd_name, id_DUMMY_CELL);
836 new_cells.push_back(std::move(padd_cell));
837 CellInfo *padd_ci = new_cells.back().get();
838
839 static int padd_z[] = {BelZ::PADD9_0_0_Z, BelZ::PADD9_0_2_Z, BelZ::PADD9_1_0_Z, BelZ::PADD9_1_2_Z};
840 padd_ci->cluster = ci->name;
841 padd_ci->constr_abs_z = false;
842 padd_ci->constr_x = 0;
843 padd_ci->constr_y = 0;
844 padd_ci->constr_z = padd_z[i / 2] - BelZ::MULT36X36_Z + i % 2;
845
846 IdString mult_name = gwu.create_aux_name(ci->name, i * 2 + 1);
847 std::unique_ptr<CellInfo> mult_cell = gwu.create_cell(mult_name, id_DUMMY_CELL);
848 new_cells.push_back(std::move(mult_cell));
849 CellInfo *mult_ci = new_cells.back().get();
850
851 static int mult_z[] = {BelZ::MULT9X9_0_0_Z, BelZ::MULT9X9_0_2_Z, BelZ::MULT9X9_1_0_Z,
852 BelZ::MULT9X9_1_2_Z};
853 mult_ci->cluster = ci->name;
854 mult_ci->constr_abs_z = false;
855 mult_ci->constr_x = 0;
856 mult_ci->constr_y = 0;
857 mult_ci->constr_z = mult_z[i / 2] - BelZ::MULT36X36_Z + i % 2;
858 }
859 } break;
860 default:
861 log_error("Unsupported DSP type '%s'\n", ci->type.c_str(ctx));
862 }
863 };
864
865 for (auto &cell : ctx->cells) {
866 auto ci = cell.second.get();
867 if (is_dsp(ci)) {
868 process_dsp_cell(ci);
869 }
870 }
871
872 // Process new DSPs if any are generated
873 for (auto &cell : new_dsps) {
874 IdString name = cell->name;
875 ctx->cells[name] = std::move(cell);
876 auto ci = ctx->cells.at(name).get();
877 process_dsp_cell(ci);
878 }
879
880 for (auto cell : cells_to_remove) {
881 ctx->cells.erase(cell);
882 }
883
884 // add new cells
885 for (auto &cell : new_cells) {
886 if (cell->cluster != ClusterId()) {
887 IdString cluster_root = cell->cluster;
888 IdString cell_name = cell->name;
889 ctx->cells[cell_name] = std::move(cell);
890 ctx->cells.at(cluster_root).get()->constr_children.push_back(ctx->cells.at(cell_name).get());
891 } else {
892 ctx->cells[cell->name] = std::move(cell);
893 }
894 }
895
896 // make CASO->CASI only chain from DSPs
897 auto make_CAS_chain = [&](CellInfo *head, int wire_num) {
898 CellInfo *cur_dsp = head;
899 while (1) {
900 CellInfo *next_dsp = gwu.dsp_bus_dst(cur_dsp, "CASO", wire_num);
901 if (next_dsp == nullptr) {
902 // End of chain
903 for (int i = 0; i < wire_num; ++i) {
904 cur_dsp->disconnectPort(ctx->idf("CASO%d", i));
905 }
906 break;
907 }
908 for (int i = 0; i < wire_num; ++i) {
909 cur_dsp->disconnectPort(ctx->idf("CASO%d", i));
910 next_dsp->disconnectPort(ctx->idf("CASI%d", i));
911 }
912 cur_dsp->setAttr(id_USE_CASCADE_OUT, 1);
913 cur_dsp = next_dsp;
914 cur_dsp->setAttr(id_USE_CASCADE_IN, 1);
915 if (ctx->verbose) {
916 log_info(" add %s to the chain.\n", ctx->nameOf(cur_dsp));
917 }
918 if (head->cluster == ClusterId()) {
919 head->cluster = head->name;
920 }
921 cur_dsp->cluster = head->name;
922 head->constr_children.push_back(cur_dsp);
923 for (auto child : cur_dsp->constr_children) {
924 child->cluster = head->name;
925 head->constr_children.push_back(child);
926 }
927 cur_dsp->constr_children.clear();
928 }
929 };
930
931 // make combined CASO->CASI and SOA->SIA chain
932 auto make_SIA_CAS_chain = [&](CellInfo *head, int cas_wire_num, int sia_wire_num, bool use_sib = true) {
933 CellInfo *cur_dsp = head;
934 while (1) {
935 bool end_of_cas_chain = false;
936 int wire_num = cas_wire_num;
937 CellInfo *next_dsp_a = gwu.dsp_bus_dst(cur_dsp, "CASO", wire_num);
938 if (next_dsp_a == nullptr) {
939 // End of CASO chain
940 for (int i = 0; i < wire_num; ++i) {
941 cur_dsp->disconnectPort(ctx->idf("CASO%d", i));
942 }
943 end_of_cas_chain = true;
944 } else {
945 for (int i = 0; i < wire_num; ++i) {
946 cur_dsp->disconnectPort(ctx->idf("CASO%d", i));
947 next_dsp_a->disconnectPort(ctx->idf("CASI%d", i));
948 }
949 }
950
951 bool end_of_so_chain = false;
952 wire_num = sia_wire_num;
953 CellInfo *next_so_dsp_a = gwu.dsp_bus_dst(cur_dsp, "SOA", wire_num);
954 CellInfo *next_so_dsp_b = use_sib ? gwu.dsp_bus_dst(cur_dsp, "SOB", wire_num) : nullptr;
955 if (next_so_dsp_a != nullptr && next_so_dsp_b != nullptr && next_so_dsp_a != next_so_dsp_b) {
956 log_error("%s is the source for two different DSPs (%s and %s) in the chain.", ctx->nameOf(cur_dsp),
957 ctx->nameOf(next_so_dsp_a), ctx->nameOf(next_so_dsp_b));
958 }
959 if (next_so_dsp_a == nullptr && (!use_sib || (use_sib && next_so_dsp_b == nullptr))) {
960 // End of SO chain
961 for (int i = 0; i < wire_num; ++i) {
962 cur_dsp->disconnectPort(ctx->idf("SOA%d", i));
963 if (use_sib) {
964 cur_dsp->disconnectPort(ctx->idf("SOB%d", i));
965 }
966 }
967 end_of_so_chain = true;
968 } else {
969 next_so_dsp_a = next_so_dsp_a != nullptr ? next_so_dsp_a : next_so_dsp_b;
970 for (int i = 0; i < wire_num; ++i) {
971 cur_dsp->disconnectPort(ctx->idf("SOA%d", i));
972 next_so_dsp_a->disconnectPort(ctx->idf("SIA%d", i));
973 if (use_sib) {
974 cur_dsp->disconnectPort(ctx->idf("SOB%d", i));
975 next_so_dsp_a->disconnectPort(ctx->idf("SIB%d", i));
976 }
977 }
978 }
979 if (end_of_cas_chain && end_of_so_chain) {
980 break;
981 }
982
983 // to the next
984 if (!end_of_cas_chain) {
985 cur_dsp->setAttr(id_USE_CASCADE_OUT, 1);
986 }
987 cur_dsp = next_dsp_a != nullptr ? next_dsp_a : next_so_dsp_a;
988 if (!end_of_cas_chain) {
989 cur_dsp->setAttr(id_USE_CASCADE_IN, 1);
990 }
991 if (ctx->verbose) {
992 log_info(" add %s to the chain. End of the SO chain:%d, end of the CAS chain:%d\n",
993 ctx->nameOf(cur_dsp), end_of_so_chain, end_of_cas_chain);
994 }
995 if (head->cluster == ClusterId()) {
996 head->cluster = head->name;
997 }
998 cur_dsp->cluster = head->name;
999 head->constr_children.push_back(cur_dsp);
1000 for (auto child : cur_dsp->constr_children) {
1001 child->cluster = head->name;
1002 head->constr_children.push_back(child);
1003 }
1004 cur_dsp->constr_children.clear();
1005 }
1006 };
1007
1008 // DSP chains
1009 for (CellInfo *head : dsp_heads) {
1010 if (ctx->verbose) {
1011 log_info("Process a DSP head: %s\n", ctx->nameOf(head));
1012 }
1013 switch (head->type.hash()) {
1014 case ID_PADD9: /* fallthrough */
1015 case ID_PADD18: {
1016 int wire_num = 9;
1017 if (head->type == id_PADD18) {
1018 wire_num = 18;
1019 }
1020
1021 CellInfo *cur_dsp = head;
1022 while (1) {
1023 CellInfo *next_dsp_a = gwu.dsp_bus_dst(cur_dsp, "SO", wire_num);
1024 CellInfo *next_dsp_b = gwu.dsp_bus_src(cur_dsp, "SBI", wire_num);
1025 if (next_dsp_a != nullptr && next_dsp_b != nullptr && next_dsp_a != next_dsp_b) {
1026 log_error("%s is the next for two different DSPs (%s and %s) in the chain.", ctx->nameOf(cur_dsp),
1027 ctx->nameOf(next_dsp_a), ctx->nameOf(next_dsp_b));
1028 }
1029 if (next_dsp_a == nullptr && next_dsp_b == nullptr) {
1030 // End of chain
1031 cur_dsp->setAttr(id_LAST_IN_CHAIN, 1);
1032 for (int i = 0; i < wire_num; ++i) {
1033 cur_dsp->disconnectPort(ctx->idf("SO%d", i));
1034 cur_dsp->disconnectPort(ctx->idf("SBI%d", i));
1035 }
1036 break;
1037 }
1038
1039 next_dsp_a = next_dsp_a != nullptr ? next_dsp_a : next_dsp_b;
1040 for (int i = 0; i < wire_num; ++i) {
1041 cur_dsp->disconnectPort(ctx->idf("SO%d", i));
1042 cur_dsp->disconnectPort(ctx->idf("SBI%d", i));
1043 next_dsp_a->disconnectPort(ctx->idf("SI%d", i));
1044 next_dsp_a->disconnectPort(ctx->idf("SBO%d", i));
1045 }
1046 cur_dsp = next_dsp_a;
1047 if (ctx->verbose) {
1048 log_info(" add %s to the chain.\n", ctx->nameOf(cur_dsp));
1049 }
1050 if (head->cluster == ClusterId()) {
1051 head->cluster = head->name;
1052 }
1053 cur_dsp->cluster = head->name;
1054 head->constr_children.push_back(cur_dsp);
1055 for (auto child : cur_dsp->constr_children) {
1056 child->cluster = head->name;
1057 head->constr_children.push_back(child);
1058 }
1059 cur_dsp->constr_children.clear();
1060 }
1061 } break;
1062 case ID_MULT9X9: /* fallthrough */
1063 case ID_MULT18X18: {
1064 int wire_num = 9;
1065 if (head->type == id_MULT18X18) {
1066 wire_num = 18;
1067 }
1068
1069 CellInfo *cur_dsp = head;
1070 while (1) {
1071 CellInfo *next_dsp_a = gwu.dsp_bus_dst(cur_dsp, "SOA", wire_num);
1072 CellInfo *next_dsp_b = gwu.dsp_bus_dst(cur_dsp, "SOB", wire_num);
1073 if (next_dsp_a != nullptr && next_dsp_b != nullptr && next_dsp_a != next_dsp_b) {
1074 log_error("%s is the source for two different DSPs (%s and %s) in the chain.", ctx->nameOf(cur_dsp),
1075 ctx->nameOf(next_dsp_a), ctx->nameOf(next_dsp_b));
1076 }
1077 if (next_dsp_a == nullptr && next_dsp_b == nullptr) {
1078 // End of chain
1079 for (int i = 0; i < wire_num; ++i) {
1080 cur_dsp->disconnectPort(ctx->idf("SOA%d", i));
1081 cur_dsp->disconnectPort(ctx->idf("SOB%d", i));
1082 }
1083 break;
1084 }
1085
1086 next_dsp_a = next_dsp_a != nullptr ? next_dsp_a : next_dsp_b;
1087 for (int i = 0; i < wire_num; ++i) {
1088 cur_dsp->disconnectPort(ctx->idf("SOA%d", i));
1089 cur_dsp->disconnectPort(ctx->idf("SOB%d", i));
1090 next_dsp_a->disconnectPort(ctx->idf("SIA%d", i));
1091 next_dsp_a->disconnectPort(ctx->idf("SIB%d", i));
1092 }
1093 cur_dsp = next_dsp_a;
1094 if (ctx->verbose) {
1095 log_info(" add %s to the chain.\n", ctx->nameOf(cur_dsp));
1096 }
1097 if (head->cluster == ClusterId()) {
1098 head->cluster = head->name;
1099 }
1100 cur_dsp->cluster = head->name;
1101 head->constr_children.push_back(cur_dsp);
1102 for (auto child : cur_dsp->constr_children) {
1103 child->cluster = head->name;
1104 head->constr_children.push_back(child);
1105 }
1106 cur_dsp->constr_children.clear();
1107 }
1108 } break;
1109 case ID_MULTALU18X18: /* fallthrough */
1110 case ID_MULTALU36X18: /* fallthrough */
1111 case ID_ALU54D: {
1112 make_CAS_chain(head, 55);
1113 } break;
1114 case ID_MULTADDALU12X12: {
1115 make_CAS_chain(head, 48);
1116 } break;
1117 case ID_MULTALU27X18: {
1118 // This primitive has the ability to form chains using both SO[AB] -> SI[AB] and CASO->CASI
1119 make_SIA_CAS_chain(head, 48, 27, false);
1120 } break;
1121 case ID_MULTADDALU18X18: {
1122 // This primitive has the ability to form chains using both SO[AB] -> SI[AB] and CASO->CASI
1123 make_SIA_CAS_chain(head, 55, 18);
1124 } break;
1125 }
1126 }
1127 }
1128 NEXTPNR_NAMESPACE_END