Debian Codegraph @

himbaechel/uarch/xilinx/pack.cc

raw
1 /*
2 * nextpnr -- Next Generation Place and Route
3 *
4 * Copyright (C) 2023 Myrtle Shah <[email protected]>
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 "pack.h"
21 #include <algorithm>
22 #include <boost/optional.hpp>
23 #include <iterator>
24 #include <queue>
25 #include <unordered_set>
26 #include "chain_utils.h"
27 #include "design_utils.h"
28 #include "extra_data.h"
29 #include "log.h"
30 #include "nextpnr.h"
31 #include "pins.h"
32
33 #define HIMBAECHEL_CONSTIDS "uarch/xilinx/constids.inc"
34 #include "himbaechel_constids.h"
35
36 NEXTPNR_NAMESPACE_BEGIN
37
38 // Process the contents of packed_cells
39 void XilinxPacker::flush_cells()
40 {
41 for (auto pcell : packed_cells) {
42 for (auto &port : ctx->cells[pcell]->ports) {
43 ctx->cells[pcell]->disconnectPort(port.first);
44 }
45 ctx->cells.erase(pcell);
46 }
47 packed_cells.clear();
48 }
49
50 void XilinxPacker::xform_cell(const dict<IdString, XFormRule> &rules, CellInfo *ci)
51 {
52 auto &rule = rules.at(ci->type);
53 ci->attrs[id_X_ORIG_TYPE] = ci->type.str(ctx);
54 ci->type = rule.new_type;
55 std::vector<IdString> orig_port_names;
56 for (auto &port : ci->ports)
57 orig_port_names.push_back(port.first);
58
59 for (auto pname : orig_port_names) {
60 if (rule.port_multixform.count(pname)) {
61 auto old_port = ci->ports.at(pname);
62 ci->disconnectPort(pname);
63 ci->ports.erase(pname);
64 for (auto new_name : rule.port_multixform.at(pname)) {
65 ci->ports[new_name].name = new_name;
66 ci->ports[new_name].type = old_port.type;
67 ci->connectPort(new_name, old_port.net);
68 ci->attrs[ctx->id("X_ORIG_PORT_" + new_name.str(ctx))] = pname.str(ctx);
69 }
70 } else {
71 IdString new_name;
72 if (rule.port_xform.count(pname)) {
73 new_name = rule.port_xform.at(pname);
74 } else {
75 std::string stripped_name;
76 for (auto c : pname.str(ctx))
77 if (c != '[' && c != ']')
78 stripped_name += c;
79 new_name = ctx->id(stripped_name);
80 }
81 if (new_name != pname) {
82 ci->renamePort(pname, new_name);
83 }
84 ci->attrs[ctx->id("X_ORIG_PORT_" + new_name.str(ctx))] = pname.str(ctx);
85 }
86 }
87
88 std::vector<IdString> xform_params;
89 for (auto &param : ci->params)
90 if (rule.param_xform.count(param.first))
91 xform_params.push_back(param.first);
92 for (auto param : xform_params)
93 ci->params[rule.param_xform.at(param)] = ci->params[param];
94
95 for (auto &attr : rule.set_attrs)
96 ci->attrs[attr.first] = attr.second;
97
98 for (auto &param : rule.set_params)
99 ci->params[param.first] = param.second;
100 }
101
102 void XilinxPacker::generic_xform(const dict<IdString, XFormRule> &rules, bool print_summary)
103 {
104 std::map<std::string, int> cell_count;
105 std::map<std::string, int> new_types;
106 for (auto &cell : ctx->cells) {
107 CellInfo *ci = cell.second.get();
108 if (rules.count(ci->type)) {
109 cell_count[ci->type.str(ctx)]++;
110 xform_cell(rules, ci);
111 new_types[ci->type.str(ctx)]++;
112 }
113 }
114 if (print_summary) {
115 for (auto &nt : new_types) {
116 log_info(" Created %d %s cells from:\n", nt.second, nt.first.c_str());
117 for (auto &cc : cell_count) {
118 if (rules.at(ctx->id(cc.first)).new_type != ctx->id(nt.first))
119 continue;
120 log_info(" %6dx %s\n", cc.second, cc.first.c_str());
121 }
122 }
123 }
124 }
125
126 CellInfo *XilinxPacker::feed_through_lut(NetInfo *net, const std::vector<PortRef> &feed_users)
127 {
128 NetInfo *feedthru_net = ctx->createNet(ctx->idf("%s$legal%d", net->name.c_str(ctx), ++autoidx));
129
130 CellInfo *lut = create_lut(stringf("%s$LUT%d", net->name.c_str(ctx), ++autoidx), {net}, feedthru_net, Property(2));
131
132 for (auto &usr : feed_users) {
133 usr.cell->disconnectPort(usr.port);
134 usr.cell->connectPort(usr.port, feedthru_net);
135 }
136
137 return lut;
138 }
139
140 CellInfo *XilinxPacker::feed_through_muxf(NetInfo *net, IdString type, const std::vector<PortRef> &feed_users)
141 {
142 NetInfo *feedthru_net = ctx->createNet(ctx->idf("%s$legal$%d", net->name.c_str(ctx), ++autoidx));
143 CellInfo *mux = create_cell(type, ctx->idf("%s$MUX$%d", net->name.c_str(ctx), ++autoidx));
144 mux->connectPort(id_I0, net);
145 mux->connectPort(id_O, feedthru_net);
146 mux->connectPort(id_S, ctx->nets[ctx->id("$PACKER_GND_NET")].get());
147
148 for (auto &usr : feed_users) {
149 usr.cell->disconnectPort(usr.port);
150 usr.cell->connectPort(usr.port, feedthru_net);
151 }
152
153 return mux;
154 }
155
156 IdString XilinxPacker::int_name(IdString base, const std::string &postfix, bool is_hierarchy)
157 {
158 return ctx->id(base.str(ctx) + (is_hierarchy ? "$subcell$" : "$intcell$") + postfix);
159 }
160
161 NetInfo *XilinxPacker::create_internal_net(IdString base, const std::string &postfix, bool is_hierarchy)
162 {
163 IdString name = ctx->id(base.str(ctx) + (is_hierarchy ? "$subnet$" : "$intnet$") + postfix);
164 return ctx->createNet(name);
165 }
166
167 void XilinxPacker::pack_luts()
168 {
169 log_info("Packing LUTs..\n");
170
171 dict<IdString, XFormRule> lut_rules;
172 for (int k = 1; k <= 6; k++) {
173 IdString lut = ctx->id("LUT" + std::to_string(k));
174 lut_rules[lut].new_type = id_SLICE_LUTX;
175 for (int i = 0; i < k; i++)
176 lut_rules[lut].port_xform[ctx->id("I" + std::to_string(i))] = ctx->id("A" + std::to_string(i + 1));
177 lut_rules[lut].port_xform[id_O] = id_O6;
178 }
179 lut_rules[id_LUT6_2] = XFormRule(lut_rules[id_LUT6]);
180 generic_xform(lut_rules, true);
181 }
182
183 void XilinxPacker::pack_ffs()
184 {
185 log_info("Packing flipflops..\n");
186
187 dict<IdString, XFormRule> ff_rules;
188 ff_rules[id_FDCE].new_type = id_SLICE_FFX;
189 ff_rules[id_FDCE].port_xform[id_C] = id_CK;
190 ff_rules[id_FDCE].port_xform[id_CLR] = id_SR;
191 // ff_rules[id_FDCE].param_xform[id_IS_CLR_INVERTED] = id_IS_SR_INVERTED;
192
193 ff_rules[id_FDPE].new_type = id_SLICE_FFX;
194 ff_rules[id_FDPE].port_xform[id_C] = id_CK;
195 ff_rules[id_FDPE].port_xform[id_PRE] = id_SR;
196 // ff_rules[id_FDPE].param_xform[id_IS_PRE_INVERTED] = id_IS_SR_INVERTED;
197
198 ff_rules[id_FDRE].new_type = id_SLICE_FFX;
199 ff_rules[id_FDRE].port_xform[id_C] = id_CK;
200 ff_rules[id_FDRE].port_xform[id_R] = id_SR;
201 ff_rules[id_FDRE].set_attrs.emplace_back(id_X_FFSYNC, "1");
202 // ff_rules[id_FDRE].param_xform[id_IS_R_INVERTED] = id_IS_SR_INVERTED;
203
204 ff_rules[id_FDSE].new_type = id_SLICE_FFX;
205 ff_rules[id_FDSE].port_xform[id_C] = id_CK;
206 ff_rules[id_FDSE].port_xform[id_S] = id_SR;
207 ff_rules[id_FDSE].set_attrs.emplace_back(id_X_FFSYNC, "1");
208 // ff_rules[id_FDSE].param_xform[id_IS_S_INVERTED] = id_IS_SR_INVERTED;
209
210 ff_rules[id_FDCE_1] = XFormRule(ff_rules[id_FDCE]);
211 ff_rules[id_FDCE_1].set_params.emplace_back(id_IS_C_INVERTED, 1);
212
213 ff_rules[id_FDPE_1] = XFormRule(ff_rules[id_FDPE]);
214 ff_rules[id_FDPE_1].set_params.emplace_back(id_IS_C_INVERTED, 1);
215
216 ff_rules[id_FDRE_1] = XFormRule(ff_rules[id_FDRE]);
217 ff_rules[id_FDRE_1].set_params.emplace_back(id_IS_C_INVERTED, 1);
218
219 ff_rules[id_FDSE_1] = XFormRule(ff_rules[id_FDSE]);
220 ff_rules[id_FDSE_1].set_params.emplace_back(id_IS_C_INVERTED, 1);
221
222 generic_xform(ff_rules, true);
223 }
224
225 void XilinxPacker::pack_lutffs()
226 {
227 int pairs = 0;
228 for (auto &cell : ctx->cells) {
229 CellInfo *ci = cell.second.get();
230 if (ci->cluster != ClusterId() || !ci->constr_children.empty())
231 continue;
232 if (ci->type != id_SLICE_FFX)
233 continue;
234 NetInfo *d = ci->getPort(id_D);
235 if (d->driver.cell == nullptr || d->driver.cell->type != id_SLICE_LUTX || d->driver.port != id_O6)
236 continue;
237 CellInfo *lut = d->driver.cell;
238 if (lut->cluster != ClusterId() || !lut->constr_children.empty())
239 continue;
240 lut->constr_children.push_back(ci);
241 lut->cluster = lut->name;
242 ci->cluster = lut->name;
243 ci->constr_x = 0;
244 ci->constr_y = 0;
245 ci->constr_z = (BEL_FF - BEL_6LUT);
246 ++pairs;
247 }
248 log_info("Constrained %d LUTFF pairs.\n", pairs);
249 }
250
251 bool XilinxPacker::is_constrained(const CellInfo *cell) { return cell->cluster != ClusterId(); }
252
253 void XilinxPacker::legalise_muxf_tree(CellInfo *curr, std::vector<CellInfo *> &mux_roots, pool<IdString> &seen_leaves)
254 {
255 if (curr->type.str(ctx).substr(0, 3) == "LUT")
256 return;
257 for (IdString p : {id_I0, id_I1}) {
258 NetInfo *pn = curr->getPort(p);
259 if (pn == nullptr || pn->driver.cell == nullptr)
260 continue;
261 if (curr->type == id_MUXF7) {
262 if (pn->driver.cell->type.str(ctx).substr(0, 3) != "LUT" || is_constrained(pn->driver.cell) ||
263 seen_leaves.count(pn->driver.cell->name)) {
264 PortRef pr;
265 pr.cell = curr;
266 pr.port = p;
267 feed_through_lut(pn, {pr});
268 continue;
269 }
270 seen_leaves.insert(pn->driver.cell->name);
271 } else {
272 IdString next_type;
273 if (curr->type == id_MUXF9)
274 next_type = id_MUXF8;
275 else if (curr->type == id_MUXF8)
276 next_type = id_MUXF7;
277 else
278 NPNR_ASSERT_FALSE("bad mux type");
279 if (pn->driver.cell->type != next_type || is_constrained(pn->driver.cell) ||
280 seen_leaves.count(pn->driver.cell->name) || bool_or_default(pn->driver.cell->attrs, id_MUX_TREE_ROOT)) {
281 PortRef pr;
282 pr.cell = curr;
283 pr.port = p;
284 feed_through_muxf(pn, next_type, {pr});
285 continue;
286 }
287 seen_leaves.insert(pn->driver.cell->name);
288 }
289 legalise_muxf_tree(pn->driver.cell, mux_roots, seen_leaves);
290 }
291 }
292
293 void XilinxPacker::constrain_muxf_tree(CellInfo *curr, CellInfo *base, int zoffset)
294 {
295 if (curr->type == id_SLICE_LUTX && (curr->constr_abs_z || curr->cluster != ClusterId()))
296 return;
297
298 int base_z = 0;
299 if (base->type == id_MUXF7)
300 base_z = BEL_F7MUX;
301 else if (base->type == id_MUXF8)
302 base_z = BEL_F8MUX;
303 else if (base->type == id_MUXF9)
304 base_z = BEL_F9MUX;
305 else if (base->constr_abs_z)
306 base_z = base->constr_z;
307 else
308 NPNR_ASSERT_FALSE("unexpected mux base type");
309 int curr_z = zoffset * 16;
310 int input_spacing = 0;
311 if (curr->type == id_MUXF7) {
312 curr_z += BEL_F7MUX;
313 input_spacing = 1;
314 } else if (curr->type == id_MUXF8) {
315 curr_z += BEL_F8MUX;
316 input_spacing = 2;
317 } else if (curr->type == id_MUXF9) {
318 curr_z += BEL_F9MUX;
319 input_spacing = 4;
320 } else
321 curr_z += BEL_6LUT;
322 if (curr != base) {
323 curr->constr_x = 0;
324 curr->constr_y = 0;
325 curr->constr_z = curr_z - base_z;
326 curr->constr_abs_z = false;
327 curr->cluster = base->name;
328 base->constr_children.push_back(curr);
329 }
330 if (curr->type.in(id_MUXF7, id_MUXF8, id_MUXF9)) {
331 NetInfo *i0 = curr->getPort(id_I0), *i1 = curr->getPort(id_I1);
332 if (i0 != nullptr && i0->driver.cell != nullptr)
333 constrain_muxf_tree(i0->driver.cell, base, zoffset + input_spacing);
334 if (i1 != nullptr && i1->driver.cell != nullptr)
335 constrain_muxf_tree(i1->driver.cell, base, zoffset);
336 }
337 }
338
339 void XilinxPacker::pack_muxfs()
340 {
341 log_info("Packing MUX[789]s..\n");
342 std::vector<CellInfo *> mux_roots;
343 for (auto &cell : ctx->cells) {
344 CellInfo *ci = cell.second.get();
345 ci->attrs.erase(id_MUX_TREE_ROOT);
346 if (ci->type == id_MUXF9) {
347 log_error("MUXF9 is not supported on xc7!\n");
348 } else if (ci->type == id_MUXF8) {
349 NetInfo *o = ci->getPort(id_O);
350 if (o == nullptr || o->users.entries() != 1 || (*o->users.begin()).cell->type != id_MUXF9 ||
351 is_constrained((*o->users.begin()).cell) || (*o->users.begin()).port == id_S)
352 mux_roots.push_back(ci);
353 } else if (ci->type == id_MUXF7) {
354 NetInfo *o = ci->getPort(id_O);
355 if (o == nullptr || o->users.entries() != 1 || (*o->users.begin()).cell->type != id_MUXF8 ||
356 is_constrained((*o->users.begin()).cell) || (*o->users.begin()).port == id_S)
357 mux_roots.push_back(ci);
358 }
359 }
360 for (auto root : mux_roots)
361 root->attrs[id_MUX_TREE_ROOT] = 1;
362 pool<IdString> seen_leaves;
363 for (auto root : mux_roots)
364 legalise_muxf_tree(root, mux_roots, seen_leaves);
365 for (auto root : mux_roots) {
366 root->cluster = root->name;
367 constrain_muxf_tree(root, root, 0);
368 }
369 }
370
371 void XilinxPacker::finalise_muxfs()
372 {
373 dict<IdString, XFormRule> muxf_rules;
374 muxf_rules[id_MUXF9].new_type = id_F9MUX;
375 muxf_rules[id_MUXF9].port_xform[id_I0] = id_0;
376 muxf_rules[id_MUXF9].port_xform[id_I1] = id_1;
377 muxf_rules[id_MUXF9].port_xform[id_S] = id_S0;
378 muxf_rules[id_MUXF9].port_xform[id_O] = id_OUT;
379 muxf_rules[id_MUXF8].new_type = id_SELMUX2_1;
380 muxf_rules[id_MUXF8].port_xform = muxf_rules[id_MUXF9].port_xform;
381 muxf_rules[id_MUXF7].new_type = id_SELMUX2_1;
382 muxf_rules[id_MUXF7].port_xform = muxf_rules[id_MUXF9].port_xform;
383 generic_xform(muxf_rules, true);
384 }
385
386 void XilinxPacker::pack_srls()
387 {
388 dict<IdString, XFormRule> srl_rules;
389 srl_rules[id_SRL16E].new_type = id_SLICE_LUTX;
390 srl_rules[id_SRL16E].port_xform[id_CLK] = id_CLK;
391 srl_rules[id_SRL16E].port_xform[id_CE] = id_WE;
392 srl_rules[id_SRL16E].port_xform[id_D] = id_DI2;
393 srl_rules[id_SRL16E].port_xform[id_Q] = id_O6;
394 srl_rules[id_SRL16E].set_attrs.emplace_back(id_X_LUT_AS_SRL, "1");
395
396 srl_rules[id_SRLC32E].new_type = id_SLICE_LUTX;
397 srl_rules[id_SRLC32E].port_xform[id_CLK] = id_CLK;
398 srl_rules[id_SRLC32E].port_xform[id_CE] = id_WE;
399 srl_rules[id_SRLC32E].port_xform[id_D] = id_DI1;
400 srl_rules[id_SRLC32E].port_xform[id_Q] = id_O6;
401 srl_rules[id_SRLC32E].set_attrs.emplace_back(id_X_LUT_AS_SRL, "1");
402 // FIXME: Q31 support
403 generic_xform(srl_rules, true);
404 // Fixup SRL inputs
405 for (auto &cell : ctx->cells) {
406 CellInfo *ci = cell.second.get();
407 if (ci->type != id_SLICE_LUTX)
408 continue;
409 std::string orig_type = str_or_default(ci->attrs, id_X_ORIG_TYPE);
410 if (orig_type == "SRL16E") {
411 for (int i = 3; i >= 0; i--) {
412 ci->renamePort(ctx->id("A" + std::to_string(i)), ctx->id("A" + std::to_string(i + 2)));
413 }
414 for (auto tp : {id_A1, id_A6}) {
415 ci->ports[tp].name = tp;
416 ci->ports[tp].type = PORT_IN;
417 ci->connectPort(tp, ctx->nets[ctx->id("$PACKER_VCC_NET")].get());
418 }
419 } else if (orig_type == "SRLC32E") {
420 for (int i = 4; i >= 0; i--) {
421 ci->renamePort(ctx->id("A" + std::to_string(i)), ctx->id("A" + std::to_string(i + 2)));
422 }
423 for (auto tp : {id_A1}) {
424 ci->ports[tp].name = tp;
425 ci->ports[tp].type = PORT_IN;
426 ci->connectPort(tp, ctx->nets[ctx->id("$PACKER_VCC_NET")].get());
427 }
428 }
429 }
430 }
431
432 void XilinxPacker::pack_constants()
433 {
434 log_info("Packing constants..\n");
435 if (tied_pins.empty())
436 get_tied_pins(ctx, tied_pins);
437 if (invertible_pins.empty())
438 get_invertible_pins(ctx, invertible_pins);
439 if (!ctx->cells.count(ctx->id("$PACKER_GND_DRV"))) {
440 CellInfo *gnd_cell = ctx->createCell(ctx->id("$PACKER_GND_DRV"), id_PSEUDO_GND);
441 gnd_cell->addOutput(id_Y);
442 NetInfo *gnd_net = ctx->createNet(ctx->id("$PACKER_GND_NET"));
443 gnd_net->constant_value = id_GND;
444 gnd_cell->connectPort(id_Y, gnd_net);
445
446 CellInfo *vcc_cell = ctx->createCell(ctx->id("$PACKER_VCC_DRV"), id_PSEUDO_VCC);
447 vcc_cell->addOutput(id_Y);
448 NetInfo *vcc_net = ctx->createNet(ctx->id("$PACKER_VCC_NET"));
449 vcc_net->constant_value = id_VCC;
450 vcc_cell->connectPort(id_Y, vcc_net);
451 }
452 NetInfo *gnd = ctx->nets[ctx->id("$PACKER_GND_NET")].get(), *vcc = ctx->nets[ctx->id("$PACKER_VCC_NET")].get();
453
454 std::vector<IdString> dead_nets;
455
456 std::vector<std::tuple<CellInfo *, IdString, bool>> const_ports;
457
458 for (auto &cell : ctx->cells) {
459 CellInfo *ci = cell.second.get();
460 if (!tied_pins.count(ci->type))
461 continue;
462 auto &tp = tied_pins.at(ci->type);
463 for (auto port : tp) {
464 if (cell.second->ports.count(port.first) && cell.second->ports.at(port.first).net != nullptr &&
465 cell.second->ports.at(port.first).net->driver.cell != nullptr)
466 continue;
467 const_ports.emplace_back(ci, port.first, port.second);
468 }
469 }
470
471 for (auto &net : ctx->nets) {
472 NetInfo *ni = net.second.get();
473 if (ni->driver.cell != nullptr && ni->driver.cell->type == id_GND) {
474 IdString drv_cell = ni->driver.cell->name;
475 for (auto &usr : ni->users) {
476 const_ports.emplace_back(usr.cell, usr.port, false);
477 usr.cell->ports.at(usr.port).net = nullptr;
478 }
479 dead_nets.push_back(net.first);
480 ctx->cells.erase(drv_cell);
481 } else if (ni->driver.cell != nullptr && ni->driver.cell->type == id_VCC) {
482 IdString drv_cell = ni->driver.cell->name;
483 for (auto &usr : ni->users) {
484 const_ports.emplace_back(usr.cell, usr.port, true);
485 usr.cell->ports.at(usr.port).net = nullptr;
486 }
487 dead_nets.push_back(net.first);
488 ctx->cells.erase(drv_cell);
489 }
490 }
491
492 for (auto port : const_ports) {
493 CellInfo *ci;
494 IdString pname;
495 bool cval;
496 std::tie(ci, pname, cval) = port;
497
498 if (!ci->ports.count(pname)) {
499 ci->addInput(pname);
500 }
501 if (ci->ports.at(pname).net != nullptr) {
502 // Case where a port with a default tie value is previously connected to an undriven net
503 NPNR_ASSERT(ci->ports.at(pname).net->driver.cell == nullptr);
504 ci->disconnectPort(pname);
505 }
506
507 if (!cval && invertible_pins.count(ci->type) && invertible_pins.at(ci->type).count(pname)) {
508 // Invertible pins connected to zero are optimised to a connection to Vcc (which is easier to route)
509 // and an inversion
510 ci->params[ctx->idf("IS_%s_INVERTED", pname.c_str(ctx))] = Property(1);
511 cval = true;
512 }
513
514 ci->connectPort(pname, cval ? vcc : gnd);
515 }
516
517 for (auto dn : dead_nets) {
518 ctx->nets.erase(dn);
519 }
520 }
521
522 void XilinxPacker::rename_net(IdString old, IdString newname)
523 {
524 std::unique_ptr<NetInfo> ni;
525 std::swap(ni, ctx->nets[old]);
526 ctx->nets.erase(old);
527 ni->name = newname;
528 ctx->nets[newname] = std::move(ni);
529 }
530
531 void XilinxPacker::tie_port(CellInfo *ci, const std::string &port, bool value, bool inv)
532 {
533 IdString p = ctx->id(port);
534 if (!ci->ports.count(p)) {
535 ci->addInput(p);
536 }
537 if (value || inv)
538 ci->connectPort(p, ctx->nets.at(ctx->id("$PACKER_VCC_NET")).get());
539 else
540 ci->connectPort(p, ctx->nets.at(ctx->id("$PACKER_GND_NET")).get());
541 if (!value && inv)
542 ci->params[ctx->idf("IS_%s_INVERTED", port.c_str())] = Property(1);
543 }
544
545 void XC7Packer::pack_bram()
546 {
547 log_info("Packing BRAM..\n");
548
549 // Rules for normal TDP BRAM
550 dict<IdString, XFormRule> bram_rules;
551 bram_rules[id_RAMB18E1].new_type = id_RAMB18E1_RAMB18E1;
552 bram_rules[id_RAMB18E1].port_multixform[ctx->id("WEA[0]")] = {id_WEA0, id_WEA1};
553 bram_rules[id_RAMB18E1].port_multixform[ctx->id("WEA[1]")] = {id_WEA2, id_WEA3};
554 bram_rules[id_RAMB36E1].new_type = id_RAMB36E1_RAMB36E1;
555
556 // Some ports have upper/lower bel pins in 36-bit mode
557 std::vector<std::pair<IdString, std::vector<std::string>>> ul_pins;
558 get_bram36_ul_pins(ctx, ul_pins);
559 for (auto &ul : ul_pins) {
560 for (auto &bp : ul.second)
561 bram_rules[id_RAMB36E1].port_multixform[ul.first].push_back(ctx->id(bp));
562 }
563 bram_rules[id_RAMB36E1].port_multixform[ctx->id("ADDRARDADDR[15]")].push_back(id_ADDRARDADDRL15);
564 bram_rules[id_RAMB36E1].port_multixform[ctx->id("ADDRBWRADDR[15]")].push_back(id_ADDRBWRADDRL15);
565
566 // Special rules for SDP rules, relating to WE connectivity
567 dict<IdString, XFormRule> sdp_bram_rules = bram_rules;
568 for (int i = 0; i < 4; i++) {
569 // Connects to two WEBWE bel pins
570 sdp_bram_rules[id_RAMB18E1].port_multixform[ctx->idf("WEBWE[%d]", i)].push_back(ctx->idf("WEBWE%d", i * 2));
571 sdp_bram_rules[id_RAMB18E1].port_multixform[ctx->idf("WEBWE[%d]", i)].push_back(ctx->idf("WEBWE%d", i * 2 + 1));
572 // Not used in SDP mode
573 sdp_bram_rules[id_RAMB18E1].port_multixform[ctx->idf("WEA[%d]", i)] = {};
574 }
575
576 for (int i = 0; i < 8; i++) {
577 sdp_bram_rules[id_RAMB36E1].port_multixform[ctx->idf("WEBWE[%d]", i)].clear();
578 // Connects to two WEBWE bel pins
579 sdp_bram_rules[id_RAMB36E1].port_multixform[ctx->idf("WEBWE[%d]", i)].push_back(ctx->idf("WEBWEL%d", i));
580 sdp_bram_rules[id_RAMB36E1].port_multixform[ctx->idf("WEBWE[%d]", i)].push_back(ctx->idf("WEBWEU%d", i));
581 // Not used in SDP mode
582 sdp_bram_rules[id_RAMB36E1].port_multixform[ctx->idf("WEA[%d]", i)] = {};
583 }
584
585 // 72-bit BRAMs: drop upper bits of WEB in TDP mode
586 for (int i = 4; i < 8; i++)
587 bram_rules[id_RAMB36E1].port_multixform[ctx->idf("WEBWE[%d]", i)] = {};
588
589 // Process SDP BRAM first
590 for (auto &cell : ctx->cells) {
591 CellInfo *ci = cell.second.get();
592 if ((ci->type == id_RAMB18E1 && int_or_default(ci->params, ctx->id("WRITE_WIDTH_B"), 0) == 36) ||
593 (ci->type == id_RAMB36E1 && int_or_default(ci->params, ctx->id("WRITE_WIDTH_B"), 0) == 72))
594 xform_cell(sdp_bram_rules, ci);
595 }
596
597 // Rewrite byte enables according to data width
598 for (auto &cell : ctx->cells) {
599 CellInfo *ci = cell.second.get();
600 if (ci->type.in(id_RAMB18E1, id_RAMB36E1)) {
601 for (char port : {'A', 'B'}) {
602 int write_width = int_or_default(ci->params, ctx->idf("WRITE_WIDTH_%c", port), 18);
603 int we_width;
604 if (ci->type == id_RAMB36E1)
605 we_width = 4;
606 else
607 we_width = (port == 'B') ? 4 : 2;
608 if (write_width >= (9 * we_width))
609 continue;
610 int used_we_width = std::max(write_width / 9, 1);
611 for (int i = used_we_width; i < we_width; i++) {
612 NetInfo *low_we = ci->getPort(ctx->id(std::string(port == 'B' ? "WEBWE[" : "WEA[") +
613 std::to_string(i % used_we_width) + "]"));
614 IdString curr_we = ctx->id(std::string(port == 'B' ? "WEBWE[" : "WEA[") + std::to_string(i) + "]");
615 if (!ci->ports.count(curr_we)) {
616 ci->ports[curr_we].type = PORT_IN;
617 ci->ports[curr_we].name = curr_we;
618 }
619 ci->disconnectPort(curr_we);
620 ci->connectPort(curr_we, low_we);
621 }
622 }
623 }
624 }
625
626 generic_xform(bram_rules, false);
627
628 // These pins have no logical mapping, so must be tied after transformation
629 for (auto &cell : ctx->cells) {
630 CellInfo *ci = cell.second.get();
631 if (ci->type == id_RAMB18E1_RAMB18E1) {
632 int wwa = int_or_default(ci->params, id_WRITE_WIDTH_A, 0);
633 for (int i = ((wwa == 0) ? 0 : 2); i < 4; i++) {
634 IdString port = ctx->idf("WEA%d", i);
635 if (!ci->ports.count(port)) {
636 ci->ports[port].name = port;
637 ci->ports[port].type = PORT_IN;
638 ci->connectPort(port, ctx->nets[ctx->id("$PACKER_GND_NET")].get());
639 }
640 }
641 int wwb = int_or_default(ci->params, id_WRITE_WIDTH_B, 0);
642 if (wwb != 36) {
643 for (int i = 4; i < 8; i++) {
644 IdString port = ctx->id("WEBWE" + std::to_string(i));
645 if (!ci->ports.count(port)) {
646 ci->ports[port].name = port;
647 ci->ports[port].type = PORT_IN;
648 ci->connectPort(port, ctx->nets[ctx->id("$PACKER_GND_NET")].get());
649 }
650 }
651 }
652 for (auto p : {id_ADDRATIEHIGH0, id_ADDRATIEHIGH1, id_ADDRBTIEHIGH0, id_ADDRBTIEHIGH1}) {
653 if (!ci->ports.count(p)) {
654 ci->ports[p].name = p;
655 ci->ports[p].type = PORT_IN;
656 } else {
657 ci->disconnectPort(p);
658 }
659 ci->connectPort(p, ctx->nets[ctx->id("$PACKER_VCC_NET")].get());
660 }
661 } else if (ci->type == id_RAMB36E1_RAMB36E1) {
662 for (auto p : {id_ADDRARDADDRL15, id_ADDRBWRADDRL15}) {
663 if (!ci->ports.count(p)) {
664 ci->ports[p].name = p;
665 ci->ports[p].type = PORT_IN;
666 } else {
667 ci->disconnectPort(p);
668 }
669 ci->connectPort(p, ctx->nets[ctx->id("$PACKER_VCC_NET")].get());
670 }
671 if (int_or_default(ci->params, id_WRITE_WIDTH_A, 0) == 1) {
672 ci->disconnectPort(id_DIADI1);
673 ci->connectPort(id_DIADI1, ci->getPort(id_DIADI0));
674 ci->attrs[id_X_ORIG_PORT_DIADI1] = std::string("DIADI[0]");
675 ci->disconnectPort(id_DIPADIP0);
676 ci->disconnectPort(id_DIPADIP1);
677 }
678 if (int_or_default(ci->params, id_WRITE_WIDTH_B, 0) == 1) {
679 ci->disconnectPort(id_DIBDI1);
680 ci->connectPort(id_DIBDI1, ci->getPort(id_DIBDI0));
681 ci->attrs[id_X_ORIG_PORT_DIBDI1] = std::string("DIBDI[0]");
682 ci->disconnectPort(id_DIPBDIP0);
683 ci->disconnectPort(id_DIPBDIP1);
684 }
685 if (int_or_default(ci->params, id_WRITE_WIDTH_B, 0) != 72) {
686 for (std::string s : {"L", "U"}) {
687 for (int i = 4; i < 8; i++) {
688 IdString port = ctx->idf("WEBWE%s%d", s.c_str(), i);
689 if (!ci->ports.count(port)) {
690 ci->ports[port].name = port;
691 ci->ports[port].type = PORT_IN;
692 ci->connectPort(port, ctx->nets[ctx->id("$PACKER_GND_NET")].get());
693 }
694 }
695 }
696 } else {
697 // Tie WEA low
698 for (std::string s : {"L", "U"}) {
699 for (int i = 0; i < 4; i++) {
700 IdString port = ctx->idf("WEA%s%d", s.c_str(), i);
701 if (!ci->ports.count(port)) {
702 ci->ports[port].name = port;
703 ci->ports[port].type = PORT_IN;
704 ci->connectPort(port, ctx->nets[ctx->id("$PACKER_GND_NET")].get());
705 }
706 }
707 }
708 }
709 }
710 }
711 }
712
713 void XilinxPacker::pack_inverters()
714 {
715 // FIXME: fold where possible
716 for (auto &cell : ctx->cells) {
717 CellInfo *ci = cell.second.get();
718 if (ci->type == id_INV) {
719 ci->params[id_INIT] = Property(1, 2);
720 ci->renamePort(id_I, id_I0);
721 ci->type = id_LUT1;
722 }
723 }
724 }
725
726 void XilinxImpl::pack()
727 {
728 const ArchArgs &args = ctx->args;
729 if (args.options.count("xdc")) {
730 parse_xdc(args.options["xdc"].as<std::string>());
731 }
732
733 XC7Packer packer(ctx, this);
734 packer.pack_constants();
735 packer.pack_inverters();
736 packer.pack_io();
737 packer.prepare_clocking();
738 packer.pack_constants();
739 packer.pack_iologic();
740 packer.pack_idelayctrl();
741 packer.pack_clocking();
742 packer.pack_muxfs();
743 packer.pack_carries();
744 packer.pack_srls();
745 packer.pack_luts();
746 packer.pack_dram();
747 packer.pack_bram();
748 packer.pack_dsps();
749 packer.pack_ffs();
750 packer.finalise_muxfs();
751 packer.pack_lutffs();
752 }
753 NEXTPNR_NAMESPACE_END