Debian Codegraph @

himbaechel/uarch/gatemate/pack_cpe.cc

raw
1 /*
2 * nextpnr -- Next Generation Place and Route
3 *
4 * Copyright (C) 2024 The Project Peppercorn Authors.
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20 #include "design_utils.h"
21 #include "gatemate_util.h"
22 #include "pack.h"
23
24 #define HIMBAECHEL_CONSTIDS "uarch/gatemate/constids.inc"
25 #include "himbaechel_constids.h"
26
27 NEXTPNR_NAMESPACE_BEGIN
28
29 // Return true if a cell is a flipflop
30 inline bool is_dff(const BaseCtx *ctx, const CellInfo *cell) { return cell->type.in(id_CC_DFF, id_CC_DLT); }
31
32 bool GateMatePacker::are_ffs_compatible(CellInfo *dff, CellInfo *other)
33 {
34 if (!other)
35 return true;
36 if (dff->getPort(id_CLK_INT) != other->getPort(id_CLK))
37 return false;
38 if (dff->getPort(id_EN_INT) != other->getPort(id_EN))
39 return false;
40 if (dff->getPort(id_SR) != other->getPort(id_SR))
41 return false;
42 if (uarch->get_dff_config(dff) != uarch->get_dff_config(other))
43 return false;
44 return true;
45 }
46
47 void GateMatePacker::dff_to_cpe(CellInfo *dff)
48 {
49 bool invert;
50 bool is_latch = dff->type == id_CC_DLT;
51 if (is_latch) {
52 NetInfo *g_net = dff->getPort(id_G);
53 invert = bool_or_default(dff->params, id_G_INV, 0);
54 if (g_net) {
55 if (g_net == net_PACKER_GND) {
56 dff->params[id_C_CPE_CLK] = Property(invert ? 0b11 : 0b00, 2);
57 dff->disconnectPort(id_G);
58 } else if (g_net == net_PACKER_VCC) {
59 dff->params[id_C_CPE_CLK] = Property(invert ? 0b00 : 0b11, 2);
60 dff->disconnectPort(id_G);
61 } else {
62 dff->params[id_C_CPE_CLK] = Property(invert ? 0b01 : 0b10, 2);
63 }
64 } else {
65 dff->params[id_C_CPE_CLK] = Property(invert ? 0b11 : 0b00, 2);
66 }
67 dff->unsetParam(id_G_INV);
68 dff->renamePort(id_G, id_CLK);
69
70 dff->params[id_C_CPE_EN] = Property(0b11, 2);
71 dff->params[id_C_L_D] = Property(0b1, 1);
72 } else {
73 NetInfo *en_net = dff->getPort(id_EN);
74 bool invert = bool_or_default(dff->params, id_EN_INV, 0);
75 if (en_net) {
76 if (en_net == net_PACKER_GND) {
77 dff->params[id_C_CPE_EN] = Property(invert ? 0b11 : 0b00, 2);
78 dff->disconnectPort(id_EN);
79 } else if (en_net == net_PACKER_VCC) {
80 dff->params[id_C_CPE_EN] = Property(invert ? 0b00 : 0b11, 2);
81 dff->disconnectPort(id_EN);
82 } else {
83 dff->params[id_C_CPE_EN] = Property(invert ? 0b01 : 0b10, 2);
84 }
85 } else {
86 dff->params[id_C_CPE_EN] = Property(invert ? 0b11 : 0b00, 2);
87 }
88 dff->unsetParam(id_EN_INV);
89
90 NetInfo *clk_net = dff->getPort(id_CLK);
91 invert = bool_or_default(dff->params, id_CLK_INV, 0);
92 if (clk_net) {
93 if (clk_net == net_PACKER_GND) {
94 dff->params[id_C_CPE_CLK] = Property(invert ? 0b11 : 0b00, 2);
95 dff->disconnectPort(id_CLK);
96 } else if (clk_net == net_PACKER_VCC) {
97 dff->params[id_C_CPE_CLK] = Property(invert ? 0b00 : 0b11, 2);
98 dff->disconnectPort(id_CLK);
99 } else {
100 dff->params[id_C_CPE_CLK] = Property(invert ? 0b01 : 0b10, 2);
101 }
102 } else {
103 dff->params[id_C_CPE_CLK] = Property(invert ? 0b11 : 0b00, 2);
104 }
105 dff->unsetParam(id_CLK_INV);
106 }
107
108 NetInfo *sr_net = dff->getPort(id_SR);
109 invert = bool_or_default(dff->params, id_SR_INV, 0);
110 bool sr_val = bool_or_default(dff->params, id_SR_VAL, 0);
111 if (sr_net) {
112 if (sr_net == net_PACKER_VCC || sr_net == net_PACKER_GND) {
113 bool sr_signal = sr_net == net_PACKER_VCC;
114 if (sr_signal ^ invert) {
115 if (sr_val) {
116 dff->params[id_C_CPE_RES] = Property(0b11, 2);
117 dff->params[id_C_CPE_SET] = Property(0b00, 2);
118 } else {
119 dff->params[id_C_CPE_RES] = Property(0b00, 2);
120 dff->params[id_C_CPE_SET] = Property(0b11, 2);
121 }
122 } else {
123 dff->params[id_C_CPE_RES] = Property(0b11, 2);
124 dff->params[id_C_CPE_SET] = Property(0b11, 2);
125 }
126 dff->disconnectPort(id_SR);
127 } else {
128 if (sr_val) {
129 dff->params[id_C_CPE_RES] = Property(0b11, 2);
130 dff->params[id_C_CPE_SET] = Property(invert ? 0b10 : 0b01, 2);
131 if (is_latch)
132 dff->renamePort(id_SR, id_EN);
133 else
134 dff->params[id_C_EN_SR] = Property(0b1, 1);
135 } else {
136 dff->params[id_C_CPE_RES] = Property(invert ? 0b10 : 0b01, 2);
137 dff->params[id_C_CPE_SET] = Property(0b11, 2);
138 }
139 }
140 } else {
141 dff->params[id_C_CPE_RES] = Property(0b11, 2);
142 dff->params[id_C_CPE_SET] = Property(0b11, 2);
143 }
144 dff->unsetParam(id_SR_VAL);
145 dff->unsetParam(id_SR_INV);
146
147 if (dff->params.count(id_INIT) && dff->params[id_INIT].is_fully_def()) {
148 bool init = bool_or_default(dff->params, id_INIT, 0);
149 if (init)
150 dff->params[id_FF_INIT] = Property(0b11, 2);
151 else
152 dff->params[id_FF_INIT] = Property(0b10, 2);
153 dff->unsetParam(id_INIT);
154 } else {
155 dff->unsetParam(id_INIT);
156 }
157 }
158
159 void GateMatePacker::dff_update_params()
160 {
161 for (auto &cell : ctx->cells) {
162 CellInfo &ci = *cell.second;
163 if (!ci.type.in(id_CC_DFF, id_CC_DLT))
164 continue;
165 dff_to_cpe(&ci);
166 }
167 }
168
169 void GateMatePacker::override_region(CellInfo *first, CellInfo *second)
170 {
171 if (first->region) {
172 if (second->region && second->region->name != first->region->name) {
173 log_warning("Unable to place cell '%s' in '%s' region.\n", first->name.c_str(ctx),
174 first->region->name.c_str(ctx));
175 }
176 } else {
177 second->region = first->region;
178 }
179 }
180
181 void GateMatePacker::pack_cpe()
182 {
183 log_info("Packing CPEs..\n");
184 std::vector<CellInfo *> l2t5_list;
185
186 auto merge_dff = [&](CellInfo &ci, CellInfo *dff) {
187 dff->cluster = ci.name;
188 override_region(dff, &ci);
189 dff->constr_abs_z = false;
190 dff->constr_z = +2;
191 ci.cluster = ci.name;
192 ci.constr_children.push_back(dff);
193 dff->renamePort(id_D, id_DIN);
194 dff->renamePort(id_Q, id_DOUT);
195 dff->renamePort(id_CLK, id_CLK_INT);
196 dff->renamePort(id_EN, id_EN_INT);
197 dff->type = (dff->type == id_CC_DLT) ? id_CPE_LATCH : id_CPE_FF;
198 };
199
200 for (auto &cell : ctx->cells) {
201 CellInfo &ci = *cell.second;
202 if (!ci.type.in(id_CC_L2T4, id_CC_L2T5, id_CC_LUT2, id_CC_LUT1, id_CC_MX2))
203 continue;
204 bool is_l2t5 = false;
205 if (ci.type == id_CC_L2T5) {
206 l2t5_list.push_back(&ci);
207 ci.renamePort(id_I0, id_D0_00);
208 ci.renamePort(id_I1, id_D1_00);
209 ci.renamePort(id_I2, id_D0_01);
210 ci.renamePort(id_I3, id_D1_01);
211
212 ci.renamePort(id_O, id_OUT);
213 rename_param(&ci, id_INIT_L02, id_INIT_L00, 4);
214 rename_param(&ci, id_INIT_L03, id_INIT_L01, 4);
215 rename_param(&ci, id_INIT_L11, id_INIT_L10, 4);
216 ci.cluster = ci.name;
217 ci.constr_abs_z = true;
218 ci.constr_z = CPE_LT_L_Z;
219 ci.type = id_CPE_L2T4;
220 is_l2t5 = true;
221 } else if (ci.type == id_CC_MX2) {
222 ci.renamePort(id_D1, id_D0_00);
223 NetInfo *sel = ci.getPort(id_S0);
224 ci.renamePort(id_S0, id_D1_00);
225 ci.addInput(id_D0_01);
226 ci.connectPort(id_D0_01, sel);
227 ci.renamePort(id_D0, id_D1_01);
228 ci.params[id_INIT_L00] = Property(LUT_AND, 4);
229 ci.params[id_INIT_L01] = Property(LUT_AND_INV_D0, 4);
230 ci.params[id_INIT_L10] = Property(LUT_OR, 4);
231 ci.renamePort(id_Y, id_OUT);
232 ci.type = id_CPE_L2T4;
233 } else {
234 ci.renamePort(id_O, id_OUT);
235 if (ci.type.in(id_CC_LUT1, id_CC_LUT2)) {
236 ci.renamePort(id_I0, id_D0_10);
237 ci.renamePort(id_I1, id_D1_10);
238 uint8_t val = int_or_default(ci.params, id_INIT, 0);
239 if (ci.type == id_CC_LUT1)
240 val = val << 2 | val;
241 ci.params[id_INIT_L10] = Property(val, 4);
242 ci.unsetParam(id_INIT);
243 } else {
244 ci.renamePort(id_I0, id_D0_00);
245 ci.renamePort(id_I1, id_D1_00);
246 ci.renamePort(id_I2, id_D0_01);
247 ci.renamePort(id_I3, id_D1_01);
248 }
249 ci.type = id_CPE_L2T4;
250 }
251 NetInfo *o = ci.getPort(id_OUT);
252 if (o) {
253 if (o->users.entries() == 1) {
254 // When only it is driving FF
255 CellInfo *dff = net_only_drives(ctx, o, is_dff, id_D, true);
256 if (dff)
257 merge_dff(ci, dff);
258 } else if (!is_l2t5) {
259 CellInfo *dff = net_only_drives(ctx, o, is_dff, id_D, false);
260 // When driving FF + other logic
261 if (dff) {
262 // Make sure main logic is in upper half
263 ci.constr_abs_z = true;
264 ci.constr_z = CPE_LT_U_Z;
265
266 merge_dff(ci, dff);
267
268 // Lower half propagate output from upper one
269 CellInfo *lower = create_cell_ptr(id_CPE_L2T4, ctx->idf("%s$lower", ci.name.c_str(ctx)));
270 ci.constr_children.push_back(lower);
271 lower->cluster = ci.name;
272 lower->region = ci.region;
273 lower->constr_abs_z = true;
274 lower->constr_z = CPE_LT_L_Z;
275 lower->params[id_INIT_L20] = Property(LUT_D0, 4);
276 ci.movePortTo(id_OUT, lower, id_OUT);
277
278 // Reconnect net
279 NetInfo *ci_out_conn = ctx->createNet(ctx->idf("%s$out", ci.name.c_str(ctx)));
280 ci.connectPort(id_OUT, ci_out_conn);
281 lower->addInput(id_COMBIN);
282 lower->connectPort(id_COMBIN, ci_out_conn);
283 dff->disconnectPort(id_DIN);
284 dff->connectPort(id_DIN, ci_out_conn);
285
286 // Attach if only remaining cell is FF
287 CellInfo *other = net_only_drives(ctx, o, is_dff, id_D, true);
288 if (other && are_ffs_compatible(dff, other)) {
289 merge_dff(ci, other);
290 other->constr_abs_z = true;
291 other->constr_z = 3;
292 }
293 }
294 }
295 }
296 }
297 for (auto ci : l2t5_list) {
298 CellInfo *upper = create_cell_ptr(id_CPE_L2T4, ctx->idf("%s$upper", ci->name.c_str(ctx)));
299 upper->cluster = ci->name;
300 upper->region = ci->region;
301 upper->constr_abs_z = true;
302 upper->constr_z = CPE_LT_U_Z;
303 ci->movePortTo(id_I4, upper, id_D0_10);
304 upper->params[id_INIT_L10] = Property(LUT_D0, 4);
305 ci->constr_children.push_back(upper);
306
307 NetInfo *ci_out_conn = ctx->createNet(ctx->idf("%s$combin", ci->name.c_str(ctx)));
308 upper->connectPort(id_OUT, ci_out_conn);
309 ci->addInput(id_COMBIN);
310 ci->connectPort(id_COMBIN, ci_out_conn);
311 }
312 l2t5_list.clear();
313 flush_cells();
314
315 std::vector<CellInfo *> mux_list;
316 for (auto &cell : ctx->cells) {
317 CellInfo &ci = *cell.second;
318 if (!ci.type.in(id_CC_MX4))
319 continue;
320 mux_list.push_back(&ci);
321 }
322 for (auto &cell : mux_list) {
323 CellInfo &ci = *cell;
324 ci.cluster = ci.name;
325 ci.renamePort(id_Y, id_OUT);
326
327 uint8_t select = 0;
328 uint8_t invert = 0;
329 for (int i = 0; i < 4; i++) {
330 NetInfo *net = ci.getPort(ctx->idf("D%d", i));
331 if (net) {
332 if (net == net_PACKER_GND) {
333 ci.disconnectPort(ctx->idf("D%d", i));
334 } else if (net == net_PACKER_VCC) {
335 invert |= 1 << i;
336 ci.disconnectPort(ctx->idf("D%d", i));
337 } else {
338 select |= 1 << i;
339 }
340 }
341 }
342 if (1) { // Pack MX4a for now
343 ci.params[id_C_FUNCTION] = Property(C_MX4, 3);
344 ci.params[id_INIT_L02] = Property(LUT_D0, 4); // IN5
345 ci.params[id_INIT_L03] = Property(LUT_D0, 4); // IN7
346 ci.params[id_INIT_L11] = Property(invert, 4); // Inversion bits
347 ci.params[id_INIT_L20] = Property(LUT_D1, 4); // Always D1
348 ci.type = id_CPE_LT_L;
349 ci.renamePort(id_S0, id_D0_00); // IN5
350 ci.renamePort(id_S1, id_D0_01); // IN7
351
352 CellInfo *upper = create_cell_ptr(id_CPE_LT_U, ctx->idf("%s$upper", ci.name.c_str(ctx)));
353 upper->cluster = ci.name;
354 upper->region = ci.region;
355 upper->constr_abs_z = false;
356 upper->constr_z = -1;
357 upper->params[id_INIT_L10] = Property(select, 4); // Selection bits
358 upper->params[id_C_FUNCTION] = Property(C_MX4, 3);
359
360 ci.movePortTo(id_D0, upper, id_IN1);
361 ci.movePortTo(id_D1, upper, id_IN2);
362 ci.movePortTo(id_D2, upper, id_IN3);
363 ci.movePortTo(id_D3, upper, id_IN4);
364 ci.constr_children.push_back(upper);
365 } else {
366 ci.params[id_C_FUNCTION] = Property(C_MX4, 3);
367 ci.params[id_INIT_L11] = Property(select, 4); // Selection bits
368 ci.params[id_INIT_L20] = Property(LUT_D0, 4); // Always D0
369 ci.type = id_CPE_LT_L;
370 ci.renamePort(id_D0, id_IN1);
371 ci.renamePort(id_D1, id_IN2);
372 ci.renamePort(id_D2, id_IN3);
373 ci.renamePort(id_D3, id_IN4);
374
375 CellInfo *upper = create_cell_ptr(id_CPE_LT_U, ctx->idf("%s$upper", ci.name.c_str(ctx)));
376 upper->cluster = ci.name;
377 upper->region = ci.region;
378 upper->constr_abs_z = false;
379 upper->constr_z = -1;
380 upper->params[id_INIT_L00] = Property(LUT_D0, 4); // IN1
381 upper->params[id_INIT_L01] = Property(LUT_D0, 4); // IN3
382 upper->params[id_INIT_L10] = Property(invert, 4); // Inversion bits
383 upper->params[id_C_FUNCTION] = Property(C_MX4, 3);
384 ci.movePortTo(id_S0, upper, id_D0_00); // IN1
385 ci.movePortTo(id_S1, upper, id_D0_01); // IN3
386 ci.constr_children.push_back(upper);
387 }
388
389 NetInfo *o = ci.getPort(id_OUT);
390 if (o) {
391 CellInfo *dff = net_only_drives(ctx, o, is_dff, id_D, true);
392 if (dff)
393 merge_dff(ci, dff);
394 }
395 }
396 mux_list.clear();
397
398 std::vector<CellInfo *> dff_list;
399 for (auto &cell : ctx->cells) {
400 CellInfo &ci = *cell.second;
401 if (!ci.type.in(id_CC_DFF, id_CC_DLT))
402 continue;
403 dff_list.push_back(&ci);
404 }
405 for (auto &cell : dff_list) {
406 CellInfo &ci = *cell;
407 CellInfo *lt = create_cell_ptr(id_CPE_L2T4, ctx->idf("%s$lt", ci.name.c_str(ctx)));
408 lt->cluster = ci.name;
409 lt->region = ci.region;
410 lt->constr_abs_z = false;
411 lt->constr_z = -2;
412 ci.cluster = ci.name;
413 ci.constr_children.push_back(lt);
414 ci.renamePort(id_Q, id_DOUT);
415 ci.renamePort(id_CLK, id_CLK_INT);
416 ci.renamePort(id_EN, id_EN_INT);
417 NetInfo *d_net = ci.getPort(id_D);
418 if (d_net == net_PACKER_GND) {
419 lt->params[id_INIT_L10] = Property(LUT_ZERO, 4);
420 ci.disconnectPort(id_D);
421 } else if (d_net == net_PACKER_VCC) {
422 lt->params[id_INIT_L10] = Property(LUT_ONE, 4);
423 ci.disconnectPort(id_D);
424 } else {
425 lt->params[id_INIT_L10] = Property(LUT_D0, 4);
426 ci.movePortTo(id_D, lt, id_D0_10);
427 }
428 ci.type = (ci.type == id_CC_DLT) ? id_CPE_LATCH : id_CPE_FF;
429 NetInfo *conn = ctx->createNet(ctx->idf("%s$di", ci.name.c_str(ctx)));
430 lt->connectPort(id_OUT, conn);
431 ci.addInput(id_DIN);
432 ci.connectPort(id_DIN, conn);
433 }
434 dff_list.clear();
435 }
436
437 static bool is_addf_ci(NetInfo *net)
438 {
439 return net && net->users.entries() == 1 && (*net->users.begin()).cell->type == id_CC_ADDF &&
440 (*net->users.begin()).port == id_CI;
441 }
442
443 void GateMatePacker::pack_addf()
444 {
445 log_info("Packing ADDFs..\n");
446
447 std::vector<CellInfo *> root_cys;
448 for (auto &cell : ctx->cells) {
449 CellInfo *ci = cell.second.get();
450 if (ci->type != id_CC_ADDF)
451 continue;
452 NetInfo *ci_net = ci->getPort(id_CI);
453
454 if (!ci_net || !ci_net->driver.cell ||
455 !(ci_net->driver.cell->type == id_CC_ADDF && ci_net->driver.port == id_CO)) {
456 root_cys.push_back(ci);
457 }
458 }
459 std::vector<std::vector<CellInfo *>> groups;
460 for (auto root : root_cys) {
461 std::vector<CellInfo *> group;
462 CellInfo *cy = root;
463 group.push_back(cy);
464 while (true) {
465 NetInfo *co_net = cy->getPort(id_CO);
466 if (co_net) {
467 bool found = false;
468 for (auto &usr : co_net->users) {
469 if (usr.cell->type == id_CC_ADDF && usr.port == id_CI) {
470 if (found)
471 log_error("Only one other ADDF can be connected.\n");
472 cy = usr.cell;
473 group.push_back(cy);
474 found = true;
475 }
476 }
477 if (!found)
478 break;
479 } else
480 break;
481 }
482 groups.push_back(group);
483 }
484
485 // Merge two ADDF cells to one CPE when possible
486 // use artificial CC_ADDF2 cell for that
487 for (size_t i = 0; i < groups.size(); i++) {
488 std::vector<CellInfo *> regrouped;
489 size_t pos = 0;
490 auto &grp = groups.at(i);
491 while (pos < grp.size()) {
492 bool merged = false;
493 CellInfo *cy = grp.at(pos);
494 NetInfo *co_net = cy->getPort(id_CO);
495 bool last = pos + 1 == grp.size();
496 if (!last && is_addf_ci(co_net)) {
497 CellInfo *cy2 = grp.at(pos + 1);
498 co_net = cy2->getPort(id_CO);
499 last = pos + 2 == grp.size();
500 if (!co_net || last || is_addf_ci(co_net)) {
501 cy2->type = id_CC_ADDF2;
502 cy2->disconnectPort(id_CI);
503 // Do actual merge of cells
504 cy->movePortTo(id_A, cy2, id_A2);
505 cy->movePortTo(id_B, cy2, id_B2);
506 cy->movePortTo(id_S, cy2, id_S2);
507 cy->disconnectPort(id_CO);
508 cy->movePortTo(id_CI, cy2, id_CI);
509 packed_cells.insert(cy->name);
510 regrouped.push_back(cy2);
511 merged = true;
512 pos++;
513 }
514 }
515 if (!merged)
516 regrouped.push_back(cy);
517 pos++;
518 }
519 grp = regrouped;
520 }
521 flush_cells();
522
523 auto merge_input = [&](CellInfo *cell, CellInfo *target, IdString port, IdString config, IdString in1,
524 IdString in2) {
525 NetInfo *net = cell->getPort(port);
526 if (net == net_PACKER_GND) {
527 target->params[config] = Property(LUT_ZERO, 4);
528 cell->disconnectPort(port);
529 } else if (net == net_PACKER_VCC) {
530 target->params[config] = Property(LUT_ONE, 4);
531 cell->disconnectPort(port);
532 } else {
533 if (net && net->driver.cell && net->driver.cell->type.in(id_CC_LUT1, id_CC_LUT2) &&
534 (net->users.entries() == 1)) {
535 CellInfo *lut2 = net->driver.cell;
536 uint8_t val = int_or_default(lut2->params, id_INIT, 0);
537 if (lut2->type == id_CC_LUT1)
538 val = val << 2 | val;
539
540 target->params[config] = Property(val, 4);
541 lut2->movePortTo(id_I0, target, in1);
542 lut2->movePortTo(id_I1, target, in2);
543 cell->disconnectPort(port);
544 packed_cells.insert(lut2->name);
545 } else {
546 if (cell == target)
547 cell->renamePort(port, in1);
548 else
549 cell->movePortTo(port, target, in1);
550 target->params[config] = Property(LUT_D0, 4);
551 }
552 }
553 };
554
555 auto merge_dff = [&](CellInfo *cell, IdString port, CellInfo *other) -> CellInfo * {
556 NetInfo *o = cell->getPort(port);
557 if (o) {
558 CellInfo *dff = net_only_drives(ctx, o, is_dff, id_D, true);
559 if (dff && are_ffs_compatible(dff, other)) {
560 dff->cluster = cell->cluster;
561 override_region(dff, cell);
562 dff->constr_abs_z = false;
563 dff->constr_z = +2;
564 cell->constr_children.push_back(dff);
565 dff->renamePort(id_D, id_DIN);
566 dff->renamePort(id_Q, id_DOUT);
567 dff->renamePort(id_CLK, id_CLK_INT);
568 dff->renamePort(id_EN, id_EN_INT);
569 dff->type = (dff->type == id_CC_DLT) ? id_CPE_LATCH : id_CPE_FF;
570 return dff;
571 }
572 }
573 return nullptr;
574 };
575
576 for (auto &grp : splitNestedVector(groups)) {
577 CellInfo *root = grp.front();
578 root->cluster = root->name;
579
580 CellInfo *ci_upper = create_cell_ptr(id_CPE_DUMMY, ctx->idf("%s$ci_upper", root->name.c_str(ctx)));
581 root->constr_children.push_back(ci_upper);
582 ci_upper->cluster = root->name;
583 ci_upper->region = root->region;
584 ci_upper->constr_abs_z = false;
585 ci_upper->constr_z = -1;
586 ci_upper->constr_y = -1;
587
588 CellInfo *ci_lower = create_cell_ptr(id_CPE_L2T4, ctx->idf("%s$ci", root->name.c_str(ctx)));
589 root->constr_children.push_back(ci_lower);
590 ci_lower->cluster = root->name;
591 ci_lower->region = root->region;
592 ci_lower->constr_abs_z = false;
593 ci_lower->constr_y = -1;
594 ci_lower->params[id_INIT_L00] = Property(LUT_ZERO, 4);
595 ci_lower->params[id_INIT_L10] = Property(LUT_D0, 4);
596
597 CellInfo *ci_cplines = create_cell_ptr(id_CPE_CPLINES, ctx->idf("%s$ci_cplines", root->name.c_str(ctx)));
598 ci_cplines->params[id_C_SELY1] = Property(1, 1);
599 ci_cplines->params[id_C_SEL_C] = Property(0, 1);
600 ci_cplines->params[id_C_CY1_I] = Property(1, 1);
601 root->constr_children.push_back(ci_cplines);
602 ci_cplines->cluster = root->name;
603 ci_cplines->region = root->region;
604 ci_cplines->constr_abs_z = true;
605 ci_cplines->constr_y = -1;
606 ci_cplines->constr_z = CPE_CPLINES_Z;
607 NetInfo *ci_out_conn = ctx->createNet(ctx->idf("%s$out", ci_lower->name.c_str(ctx)));
608 ci_lower->connectPort(id_OUT, ci_out_conn);
609 ci_cplines->connectPort(id_OUT1, ci_out_conn);
610
611 NetInfo *ci_net = root->getPort(id_CI);
612 if (ci_net == net_PACKER_GND) {
613 ci_lower->params[id_INIT_L00] = Property(LUT_ZERO, 4);
614 root->disconnectPort(id_CI);
615 } else if (ci_net == net_PACKER_VCC) {
616 ci_lower->params[id_INIT_L00] = Property(LUT_ONE, 4);
617 root->disconnectPort(id_CI);
618 } else {
619 root->movePortTo(id_CI, ci_lower, id_IN1); // IN5
620 ci_lower->params[id_INIT_L00] = Property(LUT_D0, 4);
621 }
622
623 NetInfo *ci_conn = ctx->createNet(ctx->idf("%s$ci_net", root->name.c_str(ctx)));
624 ci_cplines->connectPort(id_COUTY1, ci_conn);
625
626 root->addInput(id_CINY1);
627 root->connectPort(id_CINY1, ci_conn);
628
629 for (size_t i = 0; i < grp.size(); i++) {
630 CellInfo *cy = grp.at(i);
631 if (i != 0) {
632 cy->cluster = root->name;
633 override_region(cy, root);
634 root->constr_children.push_back(cy);
635 cy->constr_abs_z = false;
636 cy->constr_y = +i;
637 cy->renamePort(id_CI, id_CINY1);
638 }
639
640 bool merged = cy->type != id_CC_ADDF;
641 if (merged) {
642 merge_input(cy, cy, id_A2, id_INIT_L02, id_D0_02, id_D1_02); // IN5,IN6
643 merge_input(cy, cy, id_B2, id_INIT_L03, id_D0_03, id_D1_03); // IN7,IN8
644 cy->params[id_INIT_L11] = Property(LUT_XOR, 4);
645 } else {
646 cy->params[id_INIT_L02] = Property(LUT_ZERO, 4);
647 cy->params[id_INIT_L03] = Property(LUT_ZERO, 4);
648 cy->params[id_INIT_L11] = Property(LUT_XOR, 4);
649 cy->params[id_INIT_L20] = Property(LUT_XOR, 4);
650 }
651 cy->params[id_C_FUNCTION] = Property(merged ? C_ADDF2 : C_ADDF, 3);
652 cy->type = id_CPE_LT_L;
653
654 CellInfo *upper = create_cell_ptr(id_CPE_LT_U, ctx->idf("%s$upper", cy->name.c_str(ctx)));
655 upper->cluster = root->name;
656 upper->region = root->region;
657 root->constr_children.push_back(upper);
658 upper->constr_abs_z = false;
659 upper->constr_y = +i;
660 upper->constr_z = -1;
661 CellInfo *other_dff = nullptr;
662 if (merged) {
663 cy->movePortTo(id_S, upper, id_OUT);
664 cy->renamePort(id_S2, id_OUT);
665 other_dff = merge_dff(upper, id_OUT, other_dff);
666 } else {
667 cy->renamePort(id_S, id_OUT);
668 }
669 merge_dff(cy, id_OUT, other_dff);
670 merge_input(cy, upper, id_A, id_INIT_L00, id_D0_00, id_D1_00);
671 merge_input(cy, upper, id_B, id_INIT_L01, id_D0_01, id_D1_01);
672 upper->params[id_INIT_L10] = Property(LUT_XOR, 4);
673 upper->params[id_C_FUNCTION] = Property(merged ? C_ADDF2 : C_ADDF, 3);
674
675 if (i == grp.size() - 1) {
676 if (!cy->getPort(id_CO))
677 break;
678 CellInfo *co_upper = create_cell_ptr(id_CPE_DUMMY, ctx->idf("%s$co_upper", cy->name.c_str(ctx)));
679 co_upper->cluster = root->name;
680 co_upper->region = root->region;
681 root->constr_children.push_back(co_upper);
682 co_upper->constr_abs_z = false;
683 co_upper->constr_z = -1;
684 co_upper->constr_y = +i + 1;
685 CellInfo *co_lower = create_cell_ptr(id_CPE_L2T4, ctx->idf("%s$co", cy->name.c_str(ctx)));
686 co_lower->cluster = root->name;
687 co_lower->region = root->region;
688 root->constr_children.push_back(co_lower);
689 co_lower->constr_abs_z = false;
690 co_lower->constr_y = +i + 1;
691 co_lower->params[id_C_FUNCTION] = Property(C_EN_CIN, 3);
692 co_lower->params[id_INIT_L10] = Property(LUT_D1, 4);
693 co_lower->params[id_INIT_L20] = Property(LUT_D1, 4);
694
695 NetInfo *co_conn = ctx->createNet(ctx->idf("%s$co_net", cy->name.c_str(ctx)));
696
697 co_lower->connectPort(id_CINY1, co_conn);
698 cy->addOutput(id_COUTY1);
699 cy->connectPort(id_COUTY1, co_conn);
700
701 cy->movePortTo(id_CO, co_lower, id_OUT);
702 } else {
703 NetInfo *co_net = cy->getPort(id_CO);
704 if (!co_net || co_net->users.entries() == 1) {
705 cy->renamePort(id_CO, id_COUTY1);
706 } else {
707 for (auto &usr : co_net->users) {
708 if (usr.cell->type == id_CC_ADDF || usr.port == id_CI) {
709 usr.cell->disconnectPort(id_CI);
710 NetInfo *co_conn = ctx->createNet(ctx->idf("%s$co_net", cy->name.c_str(ctx)));
711 cy->addOutput(id_COUTY1);
712 cy->connectPort(id_COUTY1, co_conn);
713 usr.cell->connectPort(id_CI, co_conn);
714 break;
715 }
716 }
717 cy->movePortTo(id_CO, upper, id_CPOUT);
718 }
719 }
720 }
721 }
722 flush_cells();
723 }
724
725 void GateMatePacker::pack_constants()
726 {
727 log_info("Packing constants..\n");
728 // Replace constants with LUTs
729 const dict<IdString, Property> vcc_params = {{id_INIT_L10, Property(LUT_ONE, 4)}};
730 const dict<IdString, Property> gnd_params = {{id_INIT_L10, Property(LUT_ZERO, 4)}};
731
732 h.replace_constants(CellTypePort(id_CPE_L2T4, id_OUT), CellTypePort(id_CPE_L2T4, id_OUT), vcc_params, gnd_params);
733 net_PACKER_VCC = ctx->nets.at(ctx->id("$PACKER_VCC")).get();
734 net_PACKER_GND = ctx->nets.at(ctx->id("$PACKER_GND")).get();
735 net_SER_CLK = nullptr;
736 }
737
738 void GateMatePacker::remove_constants()
739 {
740 log_info("Removing unused constants..\n");
741 auto fnd_cell = ctx->cells.find(ctx->id("$PACKER_VCC_DRV"));
742 if (fnd_cell != ctx->cells.end()) {
743 auto fnd_net = ctx->nets.find(ctx->id("$PACKER_VCC"));
744 if (fnd_net != ctx->nets.end() && fnd_net->second->users.entries() == 0) {
745 BelId bel = (*fnd_cell).second.get()->bel;
746 if (bel != BelId())
747 ctx->unbindBel(bel);
748 ctx->cells.erase(fnd_cell);
749 ctx->nets.erase(fnd_net);
750 log_info(" Removed unused VCC cell.\n");
751 }
752 }
753 fnd_cell = ctx->cells.find(ctx->id("$PACKER_GND_DRV"));
754 if (fnd_cell != ctx->cells.end()) {
755 auto fnd_net = ctx->nets.find(ctx->id("$PACKER_GND"));
756 if (fnd_net != ctx->nets.end() && fnd_net->second->users.entries() == 0) {
757 BelId bel = (*fnd_cell).second.get()->bel;
758 if (bel != BelId())
759 ctx->unbindBel(bel);
760 ctx->cells.erase(fnd_cell);
761 ctx->nets.erase(fnd_net);
762 log_info(" Removed unused GND cell.\n");
763 }
764 }
765 }
766
767 std::pair<CellInfo *, CellInfo *> GateMatePacker::move_ram_io(CellInfo *cell, IdString iPort, IdString oPort,
768 bool place, Loc cpe_loc)
769 {
770 CellInfo *cpe_half = nullptr;
771 // CellInfo *cpe_ff = nullptr;
772 CellInfo *cpe_ramio = nullptr;
773 NetInfo *i_net = cell->getPort(iPort);
774 NetInfo *o_net = cell->getPort(oPort);
775 if (!i_net && !o_net)
776 return std::make_pair(nullptr, nullptr);
777 if (!i_net && iPort != IdString() && oPort == IdString())
778 return std::make_pair(nullptr, nullptr);
779 if (!o_net && oPort != IdString() && iPort == IdString())
780 return std::make_pair(nullptr, nullptr);
781
782 IdString ram_io_type = id_CPE_RAMIO;
783 if (!o_net)
784 ram_io_type = id_CPE_RAMI;
785 if (!i_net)
786 ram_io_type = id_CPE_RAMO;
787
788 if (o_net) {
789 if (o_net != net_PACKER_GND && o_net != net_PACKER_VCC &&
790 o_net->driver.cell->getPort(o_net->driver.port)->users.entries() == 1 &&
791 o_net->driver.cell->type.in(id_CC_LUT1, id_CC_LUT2, id_CC_L2T4, id_CC_MX2 /*, id_CC_DFF, id_CC_DLT*/)) {
792 CellInfo &ci = *o_net->driver.cell;
793 /* if (ci.type.in(id_CC_DFF, id_CC_DLT)) {
794 cpe_half = create_cell_ptr(id_CPE_L2T4, ctx->idf("%s$%s_cpe", cell->name.c_str(ctx), oPort.c_str(ctx)));
795 ci.renamePort(id_Q, id_DOUT);
796 ci.renamePort(id_CLK, id_CLK_INT);
797 ci.renamePort(id_EN, id_EN_INT);
798 NetInfo *d_net = ci.getPort(id_D);
799 if (d_net == net_PACKER_GND) {
800 cpe_half->params[id_INIT_L10] = Property(LUT_ZERO, 4);
801 ci.disconnectPort(id_D);
802 } else if (d_net == net_PACKER_VCC) {
803 cpe_half->params[id_INIT_L10] = Property(LUT_ONE, 4);
804 ci.disconnectPort(id_D);
805 } else {
806 cpe_half->params[id_INIT_L10] = Property(LUT_D0, 4);
807 }
808 ci.movePortTo(id_D, cpe_half, id_D0_10);
809 ci.type = (ci.type == id_CC_DLT) ? id_CPE_LATCH : id_CPE_FF;
810 NetInfo *conn = ctx->createNet(ctx->idf("%s$di", ci.name.c_str(ctx)));
811 cpe_half->connectPort(id_OUT, conn);
812 ci.addInput(id_DIN);
813 ci.connectPort(id_DIN, conn);
814 cpe_ff = &ci;
815 } else */
816 if (ci.type == id_CC_MX2) {
817 ci.renamePort(id_D1, id_D0_00);
818 NetInfo *sel = ci.getPort(id_S0);
819 ci.renamePort(id_S0, id_D1_00);
820 ci.addInput(id_D0_01);
821 ci.connectPort(id_D0_01, sel);
822 ci.renamePort(id_D0, id_D1_01);
823 ci.params[id_INIT_L00] = Property(LUT_AND, 4);
824 ci.params[id_INIT_L01] = Property(LUT_AND_INV_D0, 4);
825 ci.params[id_INIT_L10] = Property(LUT_OR, 4);
826 ci.renamePort(id_Y, id_OUT);
827 ci.type = id_CPE_L2T4;
828 cpe_half = &ci;
829 } else {
830 ci.renamePort(id_O, id_OUT);
831 if (ci.type.in(id_CC_LUT1, id_CC_LUT2)) {
832 ci.renamePort(id_I0, id_D0_10);
833 ci.renamePort(id_I1, id_D1_10);
834 uint8_t val = int_or_default(ci.params, id_INIT, 0);
835 if (ci.type == id_CC_LUT1)
836 val = val << 2 | val;
837 ci.params[id_INIT_L10] = Property(val, 4);
838 ci.unsetParam(id_INIT);
839 } else {
840 ci.renamePort(id_I0, id_D0_00);
841 ci.renamePort(id_I1, id_D1_00);
842 ci.renamePort(id_I2, id_D0_01);
843 ci.renamePort(id_I3, id_D1_01);
844 }
845 ci.type = id_CPE_L2T4;
846 cpe_half = &ci;
847 }
848
849 cpe_ramio = create_cell_ptr(ram_io_type, ctx->idf("%s_ramo", ci.name.c_str(ctx)));
850 // lut tree part is already connected to RAMIO part
851 cell->movePortTo(oPort, cpe_ramio, id_I);
852 } else {
853 cpe_half = create_cell_ptr(id_CPE_L2T4, ctx->idf("%s$%s_cpe", cell->name.c_str(ctx), oPort.c_str(ctx)));
854 if (o_net == net_PACKER_GND) {
855 cpe_half->params[id_INIT_L10] = Property(LUT_ZERO, 4);
856 cell->disconnectPort(oPort);
857 } else if (o_net == net_PACKER_VCC) {
858 cpe_half->params[id_INIT_L10] = Property(LUT_ONE, 4);
859 cell->disconnectPort(oPort);
860 } else {
861 cpe_half->params[id_INIT_L10] = Property(LUT_D0, 4);
862 cell->movePortTo(oPort, cpe_half, id_D0_10);
863 }
864
865 cpe_ramio = create_cell_ptr(ram_io_type, ctx->idf("%s$%s_ramo", cell->name.c_str(ctx), oPort.c_str(ctx)));
866 // Connect lut tree part and RAMIO part
867 NetInfo *out = ctx->createNet(ctx->idf("%s$out", cpe_half->name.c_str(ctx)));
868 cpe_half->connectPort(id_OUT, out);
869 cpe_ramio->connectPort(id_I, out);
870 }
871
872 // Route output to RAM_O output
873 cpe_ramio->params[id_C_RAM_O] = Property(1, 1);
874
875 NetInfo *ram_o = ctx->createNet(ctx->idf("%s$ram_o", cpe_half->name.c_str(ctx)));
876 // Reconnect original cell input port to RAM_O
877 cell->connectPort(oPort, ram_o);
878 cpe_ramio->connectPort(id_RAM_O, ram_o);
879 }
880
881 if (i_net) {
882 if (!cpe_half)
883 cpe_half = create_cell_ptr(id_CPE_DUMMY, ctx->idf("%s$%s_cpe", cell->name.c_str(ctx), iPort.c_str(ctx)));
884
885 if (!cpe_ramio)
886 cpe_ramio = create_cell_ptr(ram_io_type, ctx->idf("%s$%s_rami", cell->name.c_str(ctx), iPort.c_str(ctx)));
887
888 cpe_ramio->params[id_C_RAM_I] = Property(1, 1);
889
890 NetInfo *ram_i = ctx->createNet(ctx->idf("%s$ram_i", cpe_half->name.c_str(ctx)));
891 cell->movePortTo(iPort, cpe_ramio, id_OUT);
892 cell->connectPort(iPort, ram_i);
893 cpe_ramio->connectPort(id_RAM_I, ram_i);
894 }
895
896 // Constrain RAMIO
897 if (place) {
898 cell->constr_children.push_back(cpe_ramio);
899 cpe_ramio->cluster = cell->cluster;
900 cpe_ramio->region = cell->region;
901 cpe_ramio->constr_abs_z = false;
902 cpe_ramio->constr_z = PLACE_DB_CONSTR + (oPort != IdString() ? oPort.index : iPort.index);
903 } else {
904 BelId b = ctx->getBelByLocation(cpe_loc);
905 ctx->bindBel(b, cpe_ramio, PlaceStrength::STRENGTH_FIXED);
906 }
907 // Constrain CPE LUT tree
908 if (place) {
909 cpe_ramio->constr_children.push_back(cpe_half);
910 cpe_half->cluster = cell->cluster;
911 cpe_half->region = cell->region;
912 cpe_half->constr_abs_z = false;
913 cpe_half->constr_z = -4;
914 } else {
915 BelId b = ctx->getBelByLocation(Loc(cpe_loc.x, cpe_loc.y, cpe_loc.z - 4));
916 ctx->bindBel(b, cpe_half, PlaceStrength::STRENGTH_FIXED);
917 }
918 // Constrain CPE FF
919 /*
920 if (cpe_ff) {
921 if (place) {
922 cpe_ramio->constr_children.push_back(cpe_ff);
923 cpe_ff->cluster = cell->cluster;
924 cpe_ff->region = cell->region;
925 cpe_ff->constr_abs_z = false;
926 cpe_ff->constr_z = -2;
927 } else {
928 BelId b = ctx->getBelByLocation(Loc(cpe_loc.x, cpe_loc.y, cpe_loc.z - 2));
929 ctx->bindBel(b, cpe_ff, PlaceStrength::STRENGTH_FIXED);
930 }
931 }
932 */
933
934 return std::make_pair(cpe_half, cpe_ramio);
935 }
936
937 std::pair<CellInfo *, CellInfo *> GateMatePacker::move_ram_i(CellInfo *cell, IdString origPort, bool place, Loc cpe_loc)
938 {
939 return move_ram_io(cell, origPort, IdString(), place, cpe_loc);
940 }
941
942 std::pair<CellInfo *, CellInfo *> GateMatePacker::move_ram_o(CellInfo *cell, IdString origPort, bool place, Loc cpe_loc)
943 {
944 return move_ram_io(cell, IdString(), origPort, place, cpe_loc);
945 }
946
947 std::pair<CellInfo *, CellInfo *> GateMatePacker::move_ram_i_fixed(CellInfo *cell, IdString origPort, Loc fixed)
948 {
949 return move_ram_io(cell, origPort, IdString(), false, uarch->getRelativeConstraint(fixed, origPort));
950 }
951
952 std::pair<CellInfo *, CellInfo *> GateMatePacker::move_ram_o_fixed(CellInfo *cell, IdString origPort, Loc fixed)
953 {
954 return move_ram_io(cell, IdString(), origPort, false, uarch->getRelativeConstraint(fixed, origPort));
955 }
956
957 std::pair<CellInfo *, CellInfo *> GateMatePacker::move_ram_io_fixed(CellInfo *cell, IdString iPort, IdString oPort,
958 Loc fixed)
959 {
960 return move_ram_io(cell, iPort, oPort, false, uarch->getRelativeConstraint(fixed, oPort));
961 }
962
963 NEXTPNR_NAMESPACE_END