Debian Codegraph @

himbaechel/uarch/gatemate/pack_io.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 "pack.h"
21
22 #define HIMBAECHEL_CONSTIDS "uarch/gatemate/constids.inc"
23 #include "himbaechel_constids.h"
24
25 NEXTPNR_NAMESPACE_BEGIN
26
27 std::string get_die_name(int total_dies, int die)
28 {
29 if (total_dies == 1)
30 return "";
31 return stringf(" on die '%d%c'", int(die / total_dies) + 1, 'A' + int(die % total_dies));
32 }
33
34 void GateMatePacker::pack_io()
35 {
36 log_info("Packing IOs..\n");
37 // Trim nextpnr IOBs - assume IO buffer insertion has been done in synthesis
38 for (auto &port : ctx->ports) {
39 if (!ctx->cells.count(port.first))
40 log_error("Port '%s' doesn't seem to have a corresponding top level IO.\n", ctx->nameOf(port.first));
41 CellInfo *ci = ctx->cells.at(port.first).get();
42
43 PortRef top_port;
44 top_port.cell = nullptr;
45 bool is_npnr_iob = false;
46
47 if (ci->type == ctx->id("$nextpnr_ibuf") || ci->type == ctx->id("$nextpnr_iobuf")) {
48 // Might have an input buffer connected to it
49 is_npnr_iob = true;
50 NetInfo *o = ci->getPort(id_O);
51 if (o == nullptr)
52 ;
53 else if (o->users.entries() > 1)
54 log_error("Top level pin '%s' has multiple input buffers.\n", ctx->nameOf(port.first));
55 else if (o->users.entries() == 1)
56 top_port = *o->users.begin();
57 }
58 if (ci->type == ctx->id("$nextpnr_obuf") || ci->type == ctx->id("$nextpnr_iobuf")) {
59 // Might have an output buffer connected to it
60 is_npnr_iob = true;
61 NetInfo *i = ci->getPort(id_I);
62 if (i != nullptr && i->driver.cell != nullptr) {
63 if (top_port.cell != nullptr)
64 log_error("Top level pin '%s' has multiple input/output buffers.\n", ctx->nameOf(port.first));
65 top_port = i->driver;
66 }
67 // Edge case of a bidirectional buffer driving an output pin
68 if (i->users.entries() > 2) {
69 log_error("Top level pin '%s' has illegal buffer configuration.\n", ctx->nameOf(port.first));
70 } else if (i->users.entries() == 2) {
71 if (top_port.cell != nullptr)
72 log_error("Top level pin '%s' has illegal buffer configuration.\n", ctx->nameOf(port.first));
73 for (auto &usr : i->users) {
74 if (usr.cell->type == ctx->id("$nextpnr_obuf") || usr.cell->type == ctx->id("$nextpnr_iobuf"))
75 continue;
76 top_port = usr;
77 break;
78 }
79 }
80 }
81 if (!is_npnr_iob)
82 log_error("Port '%s' doesn't seem to have a corresponding top level IO (internal cell type mismatch).\n",
83 ctx->nameOf(port.first));
84
85 if (top_port.cell == nullptr) {
86 log_info("Trimming port '%s' as it is unused.\n", ctx->nameOf(port.first));
87 } else {
88 // Copy attributes to real IO buffer
89 for (auto &attrs : ci->attrs)
90 top_port.cell->attrs[attrs.first] = attrs.second;
91 for (auto &params : ci->params) {
92 IdString key = params.first;
93 if (key == id_LOC &&
94 top_port.cell->type.in(id_CC_LVDS_IBUF, id_CC_LVDS_OBUF, id_CC_LVDS_TOBUF, id_CC_LVDS_IOBUF)) {
95 if (top_port.port.in(id_I_P, id_O_P, id_IO_P))
96 key = id_PIN_NAME_P;
97 if (top_port.port.in(id_I_N, id_O_N, id_IO_N))
98 key = id_PIN_NAME_N;
99 }
100 if (top_port.cell->params.count(key)) {
101 if (top_port.cell->params[key] != params.second) {
102 std::string val = params.second.is_string ? params.second.as_string()
103 : std::to_string(params.second.as_int64());
104 log_warning("Overriding parameter '%s' with value '%s' for cell '%s'.\n", key.c_str(ctx),
105 val.c_str(), ctx->nameOf(top_port.cell));
106 }
107 }
108 top_port.cell->params[key] = params.second;
109 }
110
111 // Make sure that top level net is set correctly
112 port.second.net = top_port.cell->ports.at(top_port.port).net;
113 }
114 // Now remove the nextpnr-inserted buffer
115 ci->disconnectPort(id_I);
116 ci->disconnectPort(id_O);
117 ctx->cells.erase(port.first);
118 }
119
120 for (auto &cell : ctx->cells) {
121 CellInfo &ci = *cell.second;
122 if (!ci.type.in(id_CC_IBUF, id_CC_OBUF, id_CC_TOBUF, id_CC_IOBUF, id_CC_LVDS_IBUF, id_CC_LVDS_OBUF,
123 id_CC_LVDS_TOBUF, id_CC_LVDS_IOBUF))
124 continue;
125
126 bool is_lvds = ci.type.in(id_CC_LVDS_IBUF, id_CC_LVDS_OBUF, id_CC_LVDS_TOBUF, id_CC_LVDS_IOBUF);
127
128 std::string loc = str_or_default(ci.params, is_lvds ? id_PIN_NAME_P : id_PIN_NAME, "UNPLACED");
129 if (ci.params.count(id_LOC)) {
130 std::string new_loc = str_or_default(ci.params, id_LOC, "UNPLACED");
131 if (loc != "UNPLACED" && loc != new_loc)
132 log_warning("Overriding location of cell '%s' from '%s' with '%s'.\n", ctx->nameOf(&ci), loc.c_str(),
133 new_loc.c_str());
134 loc = new_loc;
135 }
136
137 if (loc == "SER_CLK" || loc == "SER_CLK_N") {
138 if (ci.type.in(id_CC_IBUF)) {
139 log_info(" Constraining '%s' to pad '%s'\n", ci.name.c_str(ctx), loc.c_str());
140 NetInfo *ser_clk = ci.getPort(id_I);
141 for (auto s : ci.getPort(id_Y)->users) {
142 s.cell->disconnectPort(s.port);
143 s.cell->connectPort(s.port, ser_clk);
144 }
145 net_SER_CLK = ser_clk;
146 ci.disconnectPort(id_I);
147 packed_cells.emplace(ci.name);
148 continue;
149 } else {
150 log_error("SER_CLK and SER_CLK_N pins can only be used on input port.\n");
151 }
152 }
153 if (loc == "UNPLACED") {
154 const ArchArgs &args = ctx->args;
155 if (args.options.count("allow-unconstrained"))
156 log_warning("IO '%s' is unconstrained in CCF and will be automatically placed.\n", ctx->nameOf(&ci));
157 else
158 log_error("IO '%s' is unconstrained in CCF (override this error with "
159 "--vopt allow-unconstrained).\n",
160 ctx->nameOf(&ci));
161 }
162
163 disconnect_if_gnd(&ci, id_T);
164 if (ci.type == id_CC_TOBUF && !ci.getPort(id_T))
165 ci.type = id_CC_OBUF;
166 if (ci.type == id_CC_LVDS_TOBUF && !ci.getPort(id_T))
167 ci.type = id_CC_LVDS_OBUF;
168
169 if (ci.type.in(id_CC_IBUF, id_CC_IOBUF))
170 copy_constraint(ci.getPort(id_I), ci.getPort(id_Y));
171 if (ci.type.in(id_CC_LVDS_IBUF, id_CC_LVDS_IOBUF)) {
172 copy_constraint(ci.getPort(id_I_P), ci.getPort(id_Y));
173 copy_constraint(ci.getPort(id_I_N), ci.getPort(id_Y));
174 }
175
176 std::vector<IdString> keys;
177 for (auto &p : ci.params) {
178
179 if (p.first.in(id_PIN_NAME, id_PIN_NAME_P, id_PIN_NAME_N)) {
180 std::string pname = p.second.as_string();
181 if (pname != "UNPLACED" && ctx->get_package_pin_bel(ctx->id(pname)) == BelId())
182 log_error("Unknown %s '%s' for cell '%s'.\n", p.first.c_str(ctx), pname.c_str(),
183 ci.name.c_str(ctx));
184 keys.push_back(p.first);
185 continue;
186 }
187 if (p.first.in(id_V_IO, id_LOC)) {
188 keys.push_back(p.first);
189 continue;
190 }
191 if (p.first.in(id_DIE))
192 continue;
193 if (ci.type.in(id_CC_IBUF, id_CC_IOBUF) &&
194 p.first.in(id_PULLUP, id_PULLDOWN, id_KEEPER, id_SCHMITT_TRIGGER, id_DELAY_IBF, id_FF_IBF))
195 continue;
196 if (ci.type.in(id_CC_TOBUF) && p.first.in(id_PULLUP, id_PULLDOWN, id_KEEPER))
197 continue;
198 if (ci.type.in(id_CC_OBUF, id_CC_TOBUF, id_CC_IOBUF)) {
199 if (p.first.in(id_DRIVE, id_SLEW)) {
200 if (p.second.is_string && p.second.as_string() == "UNDEFINED")
201 keys.push_back(p.first);
202 continue;
203 }
204 if (p.first.in(id_DELAY_OBF, id_FF_OBF))
205 continue;
206 }
207 if (ci.type.in(id_CC_LVDS_IBUF, id_CC_LVDS_IOBUF) && p.first.in(id_LVDS_RTERM, id_DELAY_IBF, id_FF_IBF))
208 continue;
209 if (ci.type.in(id_CC_LVDS_OBUF, id_CC_LVDS_TOBUF, id_CC_LVDS_IOBUF) &&
210 p.first.in(id_LVDS_BOOST, id_DELAY_OBF, id_FF_OBF))
211 continue;
212 log_warning("Removing unsupported parameter '%s' for type '%s'.\n", p.first.c_str(ctx), ci.type.c_str(ctx));
213 keys.push_back(p.first);
214 }
215 if (ci.params.count(id_SLEW)) {
216 std::string val = str_or_default(ci.params, id_SLEW, "UNDEFINED");
217 if (val == "UNDEFINED")
218 keys.push_back(id_SLEW);
219 else if (val == "FAST")
220 ci.params[id_SLEW] = Property(Property::State::S0);
221 else if (val == "SLOW")
222 ci.params[id_SLEW] = Property(Property::State::S1);
223 else
224 log_error("Unknown value '%s' for SLEW parameter of '%s' cell.\n", val.c_str(), ci.name.c_str(ctx));
225 }
226 if (is_lvds) {
227 std::string p_pin = str_or_default(ci.params, id_PIN_NAME_P, "UNPLACED");
228 std::string n_pin = str_or_default(ci.params, id_PIN_NAME_N, "UNPLACED");
229 if (p_pin == "UNPLACED" || n_pin == "UNPLACED")
230 log_error("Both LVDS pins must be set to a valid locations.\n");
231 if (p_pin.substr(0, 6) != n_pin.substr(0, 6) || p_pin[7] != n_pin[7])
232 log_error("LVDS pads '%s' and '%s' do not match.\n", p_pin.c_str(), n_pin.c_str());
233 if (p_pin[6] != 'A')
234 log_error("LVDS positive pad must be from type A.\n");
235 if (n_pin[6] != 'B')
236 log_error("LVDS negative pad must be from type B.\n");
237 }
238 for (auto key : keys)
239 ci.params.erase(key);
240
241 // For output pins set SLEW to SLOW if not defined
242 if (!ci.params.count(id_SLEW) && ci.type.in(id_CC_OBUF, id_CC_TOBUF, id_CC_IOBUF))
243 ci.params[id_SLEW] = Property(Property::State::S1);
244
245 int keeper = int_or_default(ci.params, id_KEEPER, 0);
246 ci.unsetParam(id_KEEPER);
247 if (keeper && (int_or_default(ci.params, id_PULLUP, 0) + int_or_default(ci.params, id_PULLDOWN, 0) > 1))
248 log_error("PULLUP/PULLDOWN and KEEPER are mutually exclusive parameters, issue for '%s' cell.\n",
249 ci.name.c_str(ctx));
250 if (keeper) {
251 ci.params[id_PULLUP] = Property(Property::State::S1);
252 ci.params[id_PULLDOWN] = Property(Property::State::S1);
253 }
254
255 if (is_lvds)
256 ci.params[id_LVDS_EN] = Property(Property::State::S1);
257
258 // DELAY_IBF and DELAY_OBF must be set depending of type
259 // Also we need to enable input/output
260 if (ci.type.in(id_CC_IBUF, id_CC_IOBUF, id_CC_LVDS_IBUF, id_CC_LVDS_IOBUF)) {
261 ci.params[id_DELAY_IBF] = Property(1 << int_or_default(ci.params, id_DELAY_IBF, 0), 16);
262 if (is_lvds)
263 ci.params[id_LVDS_IE] = Property(Property::State::S1);
264 else
265 ci.params[id_INPUT_ENABLE] = Property(Property::State::S1);
266 }
267 if (ci.type.in(id_CC_OBUF, id_CC_TOBUF, id_CC_IOBUF, id_CC_LVDS_OBUF, id_CC_LVDS_TOBUF, id_CC_LVDS_IOBUF)) {
268 ci.params[id_DELAY_OBF] = Property(1 << int_or_default(ci.params, id_DELAY_OBF, 0), 16);
269 ci.params[id_OE_ENABLE] = Property(Property::State::S1);
270 }
271 if (ci.params.count(id_DRIVE)) {
272 int val = int_or_default(ci.params, id_DRIVE, 0);
273 if (val != 3 && val != 6 && val != 9 && val != 12)
274 log_error("Unsupported value '%d' for DRIVE parameter of '%s' cell.\n", val, ci.name.c_str(ctx));
275 ci.params[id_DRIVE] = Property((val - 3) / 3, 2);
276 }
277 for (auto &p : ci.params) {
278 if (p.first.in(id_PULLUP, id_PULLDOWN, id_KEEPER, id_SCHMITT_TRIGGER, id_FF_OBF, id_FF_IBF, id_LVDS_RTERM,
279 id_LVDS_BOOST)) {
280 int val = int_or_default(ci.params, p.first, 0);
281 if (val != 0 && val != 1)
282 log_error("Unsupported value '%d' for %s parameter of '%s' cell.\n", val, p.first.c_str(ctx),
283 ci.name.c_str(ctx));
284 ci.params[p.first] = Property(val, 1);
285 }
286 }
287
288 static dict<IdString, IdString> map_types = {
289 {id_CC_IBUF, id_CPE_IBUF}, {id_CC_OBUF, id_CPE_OBUF},
290 {id_CC_TOBUF, id_CPE_TOBUF}, {id_CC_IOBUF, id_CPE_IOBUF},
291 {id_CC_LVDS_IBUF, id_CPE_LVDS_IBUF}, {id_CC_LVDS_TOBUF, id_CPE_LVDS_TOBUF},
292 {id_CC_LVDS_OBUF, id_CPE_LVDS_OBUF}, {id_CC_LVDS_IOBUF, id_CPE_LVDS_IOBUF},
293 };
294 ci.type = map_types[ci.type];
295
296 if (loc.empty() || loc == "UNPLACED") {
297 // Skip SER_CLK and SER_CLK_N if found in available_pads
298 auto it = uarch->available_pads.begin();
299 while (it != uarch->available_pads.end()) {
300 std::string pad_name = it->str(ctx);
301 if (pad_name == "SER_CLK" || pad_name == "SER_CLK_N") {
302 ++it;
303 continue;
304 }
305 break;
306 }
307
308 if (it == uarch->available_pads.end())
309 log_error("No more pads available.\n");
310
311 IdString id = *it;
312 uarch->available_pads.erase(id);
313 loc = id.c_str(ctx);
314 }
315
316 BelId bel;
317 if (ci.params.count(id_DIE)) {
318 std::string die_name = str_or_default(ci.params, id_DIE, "");
319 int die = uarch->die_to_index[ctx->id(die_name)];
320 ci.unsetParam(id_DIE);
321 if (uarch->locations.count(std::make_pair(ctx->id(loc), die)))
322 bel = ctx->getBelByLocation(uarch->locations[std::make_pair(ctx->id(loc), die)]);
323 else
324 log_error("Unable to place '%s' to specified '%s' die.\n", ci.name.c_str(ctx), die_name.c_str());
325 } else {
326 if (uarch->locations.count(std::make_pair(ctx->id(loc), uarch->preferred_die)))
327 bel = ctx->getBelByLocation(uarch->locations[std::make_pair(ctx->id(loc), uarch->preferred_die)]);
328 else
329 bel = ctx->get_package_pin_bel(ctx->id(loc));
330 }
331 if (bel == BelId())
332 log_error("Unable to constrain IO '%s', device does not have a pin named '%s'.\n", ci.name.c_str(ctx),
333 loc.c_str());
334 log_info(" Constraining '%s' to pad '%s'%s.\n", ci.name.c_str(ctx), loc.c_str(),
335 get_die_name(uarch->dies, uarch->tile_extra_data(bel.tile)->die).c_str());
336 if (!ctx->checkBelAvail(bel)) {
337 log_error("Can't place %s at %s because it's already taken by %s.\n", ctx->nameOf(&ci), ctx->nameOfBel(bel),
338 ctx->nameOf(ctx->getBoundBelCell(bel)));
339 }
340
341 // Place IOSEL
342 CellInfo *iosel = create_cell_ptr(id_IOSEL, ctx->idf("%s$iosel", ci.name.c_str(ctx)));
343 iosel->params = ci.params;
344 ctx->bindBel(bel, iosel, PlaceStrength::STRENGTH_FIXED);
345
346 // Rewire
347 ci.movePortTo(id_A, iosel, id_A);
348 ci.movePortTo(id_T, iosel, id_T);
349 ci.movePortTo(id_Y, iosel, id_Y);
350
351 // Place GPIO
352 Loc l = ctx->getBelLocation(bel);
353 bel = ctx->getBelByLocation({l.x, l.y, 0});
354 ctx->bindBel(bel, &ci, PlaceStrength::STRENGTH_FIXED);
355
356 // Remove not needed params
357 ci.params.clear();
358
359 // Add connections between GPIO and IOSEL
360 if (iosel->getPort(id_A))
361 ci.connectPorts(id_A, iosel, id_GPIO_OUT);
362 if (iosel->getPort(id_Y))
363 ci.connectPorts(id_Y, iosel, id_GPIO_IN);
364 if (iosel->getPort(id_T))
365 ci.connectPorts(id_T, iosel, id_GPIO_EN);
366 }
367 flush_cells();
368 }
369
370 void GateMatePacker::pack_io_sel()
371 {
372 log_info("Packing IO SELs..\n");
373 std::vector<CellInfo *> cells;
374 for (auto &cell : ctx->cells) {
375 CellInfo &ci = *cell.second;
376 if (!uarch->getBelBucketForCellType(ci.type).in(id_IOSEL))
377 continue;
378
379 cells.push_back(&ci);
380 }
381
382 std::vector<std::array<std::pair<CellInfo *, CellInfo *>, 9>> ddr(
383 uarch->dies, std::array<std::pair<CellInfo *, CellInfo *>, 9>{});
384 auto set_out_clk = [&](CellInfo *cell, CellInfo *target) -> bool {
385 NetInfo *clk_net = cell->getPort(id_CLK);
386 if (clk_net) {
387 if (clk_net == net_PACKER_GND) {
388 cell->disconnectPort(id_CLK);
389 } else if (clk_net == net_PACKER_VCC) {
390 cell->disconnectPort(id_CLK);
391 } else {
392 if (!uarch->global_signals.count(clk_net)) {
393 cell->movePortTo(id_CLK, target, id_OUT4);
394 target->params[id_SEL_OUT_CLOCK] = Property(Property::State::S1);
395 return true;
396 } else {
397 int index = uarch->global_signals[clk_net];
398 NetInfo *net = target->getPort(ctx->idf("CLOCK%d", index + 1));
399 if (net) {
400 if (net != clk_net)
401 log_error("Not able to connected different CLK signal to cell '%s'.\n",
402 cell->name.c_str(ctx));
403 } else {
404 cell->movePortTo(id_CLK, target, ctx->idf("CLOCK%d", index + 1));
405 }
406 target->params[id_OUT_CLOCK] = Property(index, 2);
407 }
408 }
409 }
410 return false;
411 };
412 auto set_in_clk = [&](CellInfo *cell, CellInfo *target) {
413 NetInfo *clk_net = cell->getPort(id_CLK);
414 if (clk_net) {
415 if (clk_net == net_PACKER_GND) {
416 cell->disconnectPort(id_CLK);
417 } else if (clk_net == net_PACKER_VCC) {
418 cell->disconnectPort(id_CLK);
419 } else {
420 if (!uarch->global_signals.count(clk_net)) {
421 cell->movePortTo(id_CLK, target, id_OUT4);
422 target->params[id_SEL_IN_CLOCK] = Property(Property::State::S1);
423 } else {
424 int index = uarch->global_signals[clk_net];
425 NetInfo *net = target->getPort(ctx->idf("CLOCK%d", index + 1));
426 if (net) {
427 if (net != clk_net)
428 log_error("Not able to connected different CLK signal to cell '%s'.\n",
429 cell->name.c_str(ctx));
430 } else {
431 cell->movePortTo(id_CLK, target, ctx->idf("CLOCK%d", index + 1));
432 }
433 target->params[id_IN_CLOCK] = Property(index, 2);
434 }
435 }
436 }
437 };
438
439 auto merge_ibf = [&](NetInfo *di_net, CellInfo &ci, bool use_custom_clock) -> bool {
440 CellInfo *dff = (*di_net->users.begin()).cell;
441 if (is_gpio_valid_dff(dff)) {
442 if (!uarch->global_signals.count(ci.getPort(id_CLK)) && use_custom_clock) {
443 log_warning("Found DFF %s cell, but not enough CLK signals.\n", dff->name.c_str(ctx));
444 return false;
445 }
446 // We configure both GPIO IN and let router decide
447 ci.params[id_IN1_FF] = Property(Property::State::S1);
448 ci.params[id_IN2_FF] = Property(Property::State::S1);
449 packed_cells.emplace(dff->name);
450 ci.disconnectPort(id_Y);
451 dff->movePortTo(id_Q, &ci, id_IN1);
452 set_in_clk(dff, &ci);
453 bool invert = bool_or_default(dff->params, id_CLK_INV, 0);
454 if (invert) {
455 ci.params[id_INV_IN1_CLOCK] = Property(Property::State::S1);
456 ci.params[id_INV_IN2_CLOCK] = Property(Property::State::S1);
457 }
458 return true;
459 } else {
460 log_warning("DFF '%s' cell for IO '%s', but unable to merge.\n", dff->name.c_str(ctx), ci.name.c_str(ctx));
461 }
462 return false;
463 };
464
465 auto merge_iddr = [&](NetInfo *di_net, CellInfo &ci, bool use_custom_clock) -> bool {
466 CellInfo *iddr = (*di_net->users.begin()).cell;
467 if (!uarch->global_signals.count(ci.getPort(id_CLK)) && use_custom_clock) {
468 log_warning("Found IDDR %s cell, but not enough CLK signals.\n", iddr->name.c_str(ctx));
469 return false;
470 }
471
472 ci.params[id_IN1_FF] = Property(Property::State::S1);
473 ci.params[id_IN2_FF] = Property(Property::State::S1);
474 packed_cells.emplace(iddr->name);
475 ci.disconnectPort(id_Y);
476
477 iddr->movePortTo(id_Q0, &ci, id_IN1);
478 iddr->movePortTo(id_Q1, &ci, id_IN2);
479
480 set_in_clk(iddr, &ci);
481 bool invert = bool_or_default(iddr->params, id_CLK_INV, 0);
482 if (invert) {
483 ci.params[id_INV_IN1_CLOCK] = Property(Property::State::S1);
484 } else {
485 ci.params[id_INV_IN2_CLOCK] = Property(Property::State::S1);
486 }
487 return true;
488 };
489
490 // Invert CPE out for output enable (OUT3)
491 bool is_inverted[4] = {false, false, true, false};
492
493 for (auto &cell : cells) {
494 CellInfo &ci = *cell;
495 bool ff_obf = bool_or_default(ci.params, id_FF_OBF, 0);
496 bool ff_ibf = bool_or_default(ci.params, id_FF_IBF, 0);
497 ci.unsetParam(id_FF_OBF);
498 ci.unsetParam(id_FF_IBF);
499
500 if (ci.getPort(id_T)) {
501 ci.params[id_OE_SIGNAL] = Property(0b10, 2);
502 ci.renamePort(id_T, id_OUT3);
503 }
504
505 ci.cluster = ci.name;
506
507 NetInfo *do_net = ci.getPort(id_A);
508 bool use_custom_clock = false;
509 if (do_net) {
510 if (do_net == net_PACKER_GND) {
511 ci.params[id_OUT23_14_SEL] = Property(Property::State::S0);
512 ci.disconnectPort(id_A);
513 } else if (do_net == net_PACKER_VCC) {
514 ci.params[id_OUT23_14_SEL] = Property(Property::State::S1);
515 ci.disconnectPort(id_A);
516 } else {
517 ci.params[id_OUT_SIGNAL] = Property(Property::State::S1);
518 bool ff_obf_merged = false;
519 if (ff_obf && do_net->driver.cell && do_net->driver.cell->type == id_CC_DFF &&
520 do_net->users.entries() == 1) {
521 CellInfo *dff = do_net->driver.cell;
522 if (is_gpio_valid_dff(dff)) {
523 ci.params[id_OUT1_FF] = Property(Property::State::S1);
524 packed_cells.emplace(dff->name);
525 ci.disconnectPort(id_A);
526 dff->movePortTo(id_D, &ci, id_OUT1);
527 use_custom_clock = set_out_clk(dff, &ci);
528 bool invert = bool_or_default(dff->params, id_CLK_INV, 0);
529 if (invert) {
530 ci.params[id_INV_OUT1_CLOCK] = Property(Property::State::S1);
531 ci.params[id_INV_OUT2_CLOCK] = Property(Property::State::S1);
532 }
533 ff_obf_merged = true;
534 } else {
535 log_warning("DFF '%s' cell for IO '%s', but unable to merge.\n", dff->name.c_str(ctx),
536 ci.name.c_str(ctx));
537 }
538 }
539 bool oddr_merged = false;
540 if (do_net->driver.cell && do_net->driver.cell->type == id_CC_LUT1 && do_net->users.entries() == 1) {
541 NetInfo *net = do_net->driver.cell->getPort(id_I0);
542 if (net->driver.cell && net->driver.cell->type == id_CC_ODDR && net->users.entries() == 1) {
543 packed_cells.insert(do_net->driver.cell->name);
544 // Inverting both input is equal to inverter at output
545 is_inverted[0] = true;
546 is_inverted[1] = true;
547 do_net = net;
548 }
549 }
550 if (do_net->driver.cell && do_net->driver.cell->type == id_CC_ODDR && do_net->users.entries() == 1) {
551 CellInfo *oddr = do_net->driver.cell;
552 ci.params[id_OUT1_FF] = Property(Property::State::S1);
553 ci.params[id_OUT2_FF] = Property(Property::State::S1);
554 ci.params[id_USE_DDR] = Property(Property::State::S1);
555 packed_cells.emplace(oddr->name);
556 ci.disconnectPort(id_A);
557 oddr->movePortTo(id_D0, &ci, id_OUT2);
558 oddr->movePortTo(id_D1, &ci, id_OUT1);
559 const auto &pad = uarch->bel_to_pad[ci.bel];
560 int die = uarch->tile_extra_data(ci.bel.tile)->die;
561 auto [cpe_half, cpe_ramio] = ddr[die][pad->pad_bank];
562 if (cpe_half) {
563 if (cpe_half->getPort(id_D0_10) != oddr->getPort(id_DDR))
564 log_error("DDR port use signal different than already occupied DDR source.\n");
565 ci.addInput(id_DDR);
566 ci.connectPort(id_DDR, cpe_ramio->getPort(id_RAM_O));
567 } else {
568 auto l = reinterpret_cast<const GateMatePadExtraDataPOD *>(pad->extra_data.get());
569 oddr->movePortTo(id_DDR, &ci, id_DDR);
570 ddr[die][pad->pad_bank] = move_ram_o(&ci, id_DDR, false, Loc(l->x, l->y, l->z));
571 uarch->ddr_nets.insert(ddr[die][pad->pad_bank].first->getPort(id_D0_10)->name);
572 }
573 use_custom_clock = set_out_clk(oddr, &ci);
574 bool invert = bool_or_default(oddr->params, id_CLK_INV, 0);
575 if (!invert) {
576 ci.params[id_INV_OUT1_CLOCK] = Property(Property::State::S1);
577 } else {
578 ci.params[id_INV_OUT2_CLOCK] = Property(Property::State::S1);
579 }
580 oddr_merged = true;
581 }
582 if (!ff_obf_merged && !oddr_merged)
583 ci.renamePort(id_A, id_OUT1);
584 }
585 }
586 NetInfo *di_net = ci.getPort(id_Y);
587 if (di_net) {
588 bool ff_ibf_merged = false;
589 if (ff_ibf && di_net->users.entries() == 1 && (*di_net->users.begin()).cell->type == id_CC_DFF) {
590 ff_ibf_merged = merge_ibf(di_net, ci, use_custom_clock);
591 }
592 bool iddr_merged = false;
593 if (di_net->users.entries() == 1 && (*di_net->users.begin()).cell->type == id_CC_IDDR &&
594 (*di_net->users.begin()).port == id_D) {
595 iddr_merged = merge_iddr(di_net, ci, use_custom_clock);
596 }
597
598 if (!ff_ibf_merged && !iddr_merged)
599 ci.renamePort(id_Y, id_IN1);
600 }
601
602 Loc root_loc = ctx->getBelLocation(ci.bel);
603 for (int i = 0; i < 4; i++) {
604 CellInfo *cpe = move_ram_o_fixed(&ci, ctx->idf("OUT%d", i + 1), root_loc).first;
605 if (cpe && is_inverted[i])
606 cpe->params[id_INIT_L10] = Property(LUT_INV_D0, 4);
607 }
608 }
609 flush_cells();
610 }
611
612 bool GateMatePacker::is_gpio_valid_dff(CellInfo *dff)
613 {
614 NetInfo *en_net = dff->getPort(id_EN);
615 bool invert = bool_or_default(dff->params, id_EN_INV, 0);
616 if (en_net) {
617 if (en_net == net_PACKER_GND) {
618 if (!invert)
619 return false;
620 dff->disconnectPort(id_EN);
621 } else if (en_net == net_PACKER_VCC) {
622 if (invert)
623 return false;
624 dff->disconnectPort(id_EN);
625 } else {
626 return false;
627 }
628 }
629 dff->unsetParam(id_EN_INV);
630
631 NetInfo *sr_net = dff->getPort(id_SR);
632 invert = bool_or_default(dff->params, id_SR_INV, 0);
633 if (sr_net) {
634 if ((sr_net == net_PACKER_GND) || (sr_net == net_PACKER_VCC)) {
635 bool sr_signal = sr_net == net_PACKER_VCC;
636 if (sr_signal ^ invert)
637 log_error("Currently unsupported DFF configuration for '%s'\n.", dff->name.c_str(ctx));
638 dff->disconnectPort(id_SR);
639 } else {
640 return false;
641 }
642 }
643 dff->unsetParam(id_SR_VAL);
644 dff->unsetParam(id_SR_INV);
645
646 // Sanity check for CLK signal, that it must exist
647 NetInfo *clk_net = dff->getPort(id_CLK);
648 if (clk_net) {
649 if (clk_net == net_PACKER_GND) {
650 return false;
651 } else if (clk_net == net_PACKER_VCC) {
652 return false;
653 }
654 } else {
655 return false;
656 }
657
658 return true;
659 }
660
661 NEXTPNR_NAMESPACE_END