Debian Codegraph @

himbaechel/uarch/gatemate/gatemate.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 <utility>
21
22 #include "gatemate.h"
23 #include "log.h"
24 #include "nextpnr_assertions.h"
25 #include "placer_heap.h"
26
27 #define GEN_INIT_CONSTIDS
28 #define HIMBAECHEL_CONSTIDS "uarch/gatemate/constids.inc"
29
30 #include "himbaechel_constids.h"
31
32 NEXTPNR_NAMESPACE_BEGIN
33
34 GateMateImpl::~GateMateImpl() {};
35
36 po::options_description GateMateImpl::getUArchOptions()
37 {
38 po::options_description specific("GateMate specific options");
39 specific.add_options()("out", po::value<std::string>(), "textual configuration bitstream output file");
40 specific.add_options()("ccf", po::value<std::string>(), "name of constraints file");
41 specific.add_options()("allow-unconstrained", "allow unconstrained IOs");
42 specific.add_options()("fpga_mode", po::value<std::string>(), "performance mode (1:lowpower, 2:economy, 3:speed)");
43 specific.add_options()("time_mode", po::value<std::string>(), "timing mode (1:best, 2:typical, 3:worst)");
44 specific.add_options()("strategy", po::value<std::string>(),
45 "multi-die clock placement strategy (mirror, full or clk1)");
46 specific.add_options()("force_die", po::value<std::string>(), "force specific die (example 1A,1B...)");
47 specific.add_options()("clk-cp", "use CP lines for CLK and EN");
48 specific.add_options()("no-cpe-cp", "do not use CP lines pass through CPE");
49 specific.add_options()("no-bridges", "do not use CPE in bridge mode");
50 return specific;
51 }
52
53 static int parse_mode(const std::string &val, const std::map<std::string, int> &map, const char *error_msg)
54 {
55 try {
56 int i = std::stoi(val);
57 if (i >= 1 && i <= 3)
58 return i;
59 } catch (...) {
60 auto it = map.find(val);
61 if (it != map.end())
62 return it->second;
63 }
64 log_error("Invalid mode='%s', %s", val.c_str(), error_msg);
65 }
66
67 void GateMateImpl::init_database(Arch *arch)
68 {
69 const ArchArgs &args = arch->args;
70 init_uarch_constids(arch);
71 arch->load_chipdb(stringf("gatemate/chipdb-%s.bin", args.device.c_str()));
72 arch->set_package("FBGA324");
73 dies = std::stoi(args.device.substr(6));
74 fpga_mode = 3;
75 timing_mode = 3;
76
77 static const std::map<std::string, int> fpga_map = {{"lowpower", 1}, {"economy", 2}, {"speed", 3}};
78 static const std::map<std::string, int> timing_map = {{"best", 1}, {"typical", 2}, {"worst", 3}};
79
80 if (args.options.count("fpga_mode"))
81 fpga_mode = parse_mode(args.options["fpga_mode"].as<std::string>(), fpga_map,
82 "performance valid values are {1:lowpower, 2:economy, 3:speed}.\n");
83 if (args.options.count("time_mode"))
84 timing_mode = parse_mode(args.options["time_mode"].as<std::string>(), timing_map,
85 "timing valid values are {1:best, 2:typical, 3:worst}.\n");
86
87 std::string speed_grade = "";
88 switch (timing_mode) {
89 case 1:
90 speed_grade = "best_";
91 break;
92 case 2:
93 speed_grade = "typ_";
94 break;
95 default:
96 speed_grade = "worst_";
97 break;
98 }
99 log_info("Using timing mode '%s'.\n", timing_mode == 1 ? "BEST"
100 : timing_mode == 2 ? "TYPICAL"
101 : timing_mode == 3 ? "WORST"
102 : "");
103
104 switch (fpga_mode) {
105 case 1:
106 speed_grade += "lpr";
107 break;
108 case 2:
109 speed_grade += "eco";
110 break;
111 default:
112 speed_grade += "spd";
113 }
114 log_info("Using performance mode '%s'.\n", fpga_mode == 1 ? "LOWPOWER"
115 : fpga_mode == 2 ? "ECONOMY"
116 : fpga_mode == 3 ? "SPEED"
117 : "");
118 arch->set_speed_grade(speed_grade);
119 use_cp_for_clk = args.options.count("clk-cp") == 1;
120 use_cp_for_cpe = args.options.count("no-cpe-cp") == 0;
121 use_bridges = args.options.count("no-bridges") == 0;
122 }
123
124 void GateMateImpl::init(Context *ctx)
125 {
126 HimbaechelAPI::init(ctx);
127 for (const auto &pad : ctx->package_info->pads) {
128 available_pads.emplace(IdString(pad.package_pin));
129 BelId bel = ctx->getBelByName(IdStringList::concat(IdString(pad.tile), IdString(pad.bel)));
130 bel_to_pad.emplace(bel, &pad);
131 locations.emplace(std::make_pair(IdString(pad.package_pin), tile_extra_data(bel.tile)->die),
132 ctx->getBelLocation(bel));
133 }
134 available_pads.emplace(ctx->id("SER_CLK"));
135 available_pads.emplace(ctx->id("SER_CLK_N"));
136 for (auto bel : ctx->getBels()) {
137 auto *ptr = bel_extra_data(bel);
138 std::map<IdString, const GateMateBelPinConstraintPOD *> pins;
139 for (const auto &p : ptr->constraints)
140 pins.emplace(IdString(p.name), &p);
141 pin_to_constr.emplace(bel, pins);
142 if (ctx->getBelType(bel).in(id_CLKIN, id_GLBOUT, id_PLL, id_USR_RSTN, id_CFG_CTRL, id_SERDES)) {
143 locations.emplace(std::make_pair(ctx->getBelName(bel)[1], tile_extra_data(bel.tile)->die),
144 ctx->getBelLocation(bel));
145 }
146 }
147 const auto &sp = reinterpret_cast<const GateMateSpeedGradeExtraDataPOD *>(ctx->speed_grade->extra_data.get());
148 for (int i = 0; i < sp->timings.ssize(); i++) {
149 timing.emplace(IdString(sp->timings[i].name), &sp->timings[i]);
150 }
151 for (int num = 0; num < 2; num++) {
152 int index = (num == 0) ? 0 : 2;
153 ram_signal_clk.emplace(ctx->idf("ENA[%d]", index), num);
154 ram_signal_clk.emplace(ctx->idf("ENB[%d]", index), num + 2);
155 ram_signal_clk.emplace(ctx->idf("GLWEA[%d]", index), num);
156 ram_signal_clk.emplace(ctx->idf("GLWEB[%d]", index), num + 2);
157 ram_signal_clk.emplace(ctx->idf("ECC1B_ERRA[%d]", index), num);
158 ram_signal_clk.emplace(ctx->idf("ECC1B_ERRB[%d]", index), num + 2);
159 ram_signal_clk.emplace(ctx->idf("ECC2B_ERRA[%d]", index), num);
160 ram_signal_clk.emplace(ctx->idf("ECC2B_ERRB[%d]", index), num + 2);
161 for (int i = 0; i < 20; i++) {
162 ram_signal_clk.emplace(ctx->idf("WEA[%d]", i + num * 20), num);
163 ram_signal_clk.emplace(ctx->idf("WEB[%d]", i + num * 20), num + 2);
164 }
165
166 for (int i = 0; i < 16; i++) {
167 ram_signal_clk.emplace(ctx->idf("ADDRA%d[%d]", num, i), num);
168 ram_signal_clk.emplace(ctx->idf("ADDRB%d[%d]", num, i), num + 2);
169 }
170
171 for (int i = 0; i < 20; i++) {
172 ram_signal_clk.emplace(ctx->idf("DIA[%d]", i + num * 20), num);
173 ram_signal_clk.emplace(ctx->idf("DOA[%d]", i + num * 20), num);
174 ram_signal_clk.emplace(ctx->idf("DIB[%d]", i + num * 20), num + 2);
175 ram_signal_clk.emplace(ctx->idf("DOB[%d]", i + num * 20), num + 2);
176 }
177 }
178
179 const GateMateChipExtraDataPOD *extra =
180 reinterpret_cast<const GateMateChipExtraDataPOD *>(ctx->chip_info->extra_data.get());
181
182 const ArchArgs &args = ctx->args;
183 std::string die_name;
184 if (args.options.count("force_die"))
185 die_name = args.options["force_die"].as<std::string>();
186 bool found = false;
187 int index = 0;
188 for (auto &die : extra->dies) {
189 IdString name(die.name);
190 index_to_die[index] = name;
191 die_to_index[name] = index++;
192 ctx->createRectangularRegion(name, die.x1, die.y1, die.x2, die.y2);
193 if (die_name == name.c_str(ctx)) {
194 found = true;
195 forced_die = name;
196 }
197 }
198 if (!die_name.empty() && !found)
199 log_error("Unable to select forced die '%s'.\n", die_name.c_str());
200 }
201
202 bool GateMateImpl::isBelLocationValid(BelId bel, bool explain_invalid) const
203 {
204 CellInfo *cell = ctx->getBoundBelCell(bel);
205 if (cell == nullptr) {
206 return true;
207 }
208
209 if (getBelBucketForBel(bel) == id_CPE_FF) {
210 Loc loc = ctx->getBelLocation(bel);
211 const CellInfo *adj_half = ctx->getBoundBelCell(
212 ctx->getBelByLocation(Loc(loc.x, loc.y, loc.z == CPE_FF_L_Z ? CPE_FF_U_Z : CPE_FF_L_Z)));
213 if (adj_half) {
214 const auto &half_data = fast_cell_info.at(cell->flat_index);
215 if (half_data.used) {
216 const auto &adj_data = fast_cell_info.at(adj_half->flat_index);
217 if (adj_data.used) {
218 if (adj_data.config != half_data.config)
219 return false;
220 if (adj_data.ff_en != half_data.ff_en)
221 return false;
222 if (adj_data.ff_clk != half_data.ff_clk)
223 return false;
224 if (adj_data.ff_sr != half_data.ff_sr)
225 return false;
226 }
227 }
228 }
229 return true;
230 } else if (getBelBucketForBel(bel) == id_RAM_HALF) {
231 Loc loc = ctx->getBelLocation(bel);
232 const CellInfo *adj_half =
233 ctx->getBoundBelCell(ctx->getBelByLocation(Loc(loc.x, loc.z == RAM_HALF_L_Z ? loc.y - 8 : loc.y + 8,
234 loc.z == RAM_HALF_L_Z ? RAM_FULL_Z : RAM_HALF_L_Z)));
235 if (adj_half) {
236 const auto &half_data = fast_cell_info.at(cell->flat_index);
237 if (half_data.used) {
238 const auto &adj_data = fast_cell_info.at(adj_half->flat_index);
239 if (adj_data.used) {
240 if (adj_data.config != half_data.config)
241 return false;
242 }
243 }
244 }
245 return true;
246 }
247
248 return true;
249 }
250
251 Loc GateMateImpl::getRelativeConstraint(Loc &root_loc, IdString id) const
252 {
253 Loc child_loc;
254 BelId root_bel = ctx->getBelByLocation(root_loc);
255 if (pin_to_constr.count(root_bel)) {
256 auto &constr = pin_to_constr.at(root_bel);
257 if (constr.count(id)) {
258 auto &p = constr.at(id);
259 child_loc.x = root_loc.x + p->constr_x;
260 child_loc.y = root_loc.y + p->constr_y;
261 child_loc.z = p->constr_z;
262 } else {
263 log_error("Constrain info not available for pin '%s'.\n", id.c_str(ctx));
264 }
265 } else {
266 log_error("Bel info not available for constraints.\n");
267 }
268 return child_loc;
269 }
270
271 bool GateMateImpl::getChildPlacement(const BaseClusterInfo *cluster, Loc root_loc,
272 std::vector<std::pair<CellInfo *, BelId>> &placement) const
273 {
274 for (auto child : cluster->constr_children) {
275 Loc child_loc;
276 if (child->constr_z >= PLACE_DB_CONSTR) {
277 child_loc = getRelativeConstraint(root_loc, IdString(child->constr_z - PLACE_DB_CONSTR));
278 } else {
279 child_loc.x = root_loc.x + child->constr_x;
280 child_loc.y = root_loc.y + child->constr_y;
281 child_loc.z = child->constr_abs_z ? child->constr_z : (root_loc.z + child->constr_z);
282 }
283 if (child_loc.x < 0 || child_loc.x >= ctx->getGridDimX())
284 return false;
285 if (child_loc.y < 0 || child_loc.y >= ctx->getGridDimY())
286 return false;
287 BelId child_bel = ctx->getBelByLocation(child_loc);
288 if (child_bel == BelId() || !this->isValidBelForCellType(child->type, child_bel))
289 return false;
290 placement.emplace_back(child, child_bel);
291 if (!getChildPlacement(child, child_loc, placement))
292 return false;
293 }
294 return true;
295 }
296
297 bool GateMateImpl::getClusterPlacement(ClusterId cluster, BelId root_bel,
298 std::vector<std::pair<CellInfo *, BelId>> &placement) const
299 {
300 CellInfo *root_cell = get_cluster_root(ctx, cluster);
301 placement.clear();
302 NPNR_ASSERT(root_bel != BelId());
303 Loc root_loc = ctx->getBelLocation(root_bel);
304 if (root_cell->constr_abs_z) {
305 // Coerce root to absolute z constraint
306 root_loc.z = root_cell->constr_z;
307 root_bel = ctx->getBelByLocation(root_loc);
308 if (root_bel == BelId() || !this->isValidBelForCellType(root_cell->type, root_bel))
309 return false;
310 }
311 placement.emplace_back(root_cell, root_bel);
312 return getChildPlacement(root_cell, root_loc, placement);
313 }
314
315 void GateMateImpl::prePlace() { assign_cell_info(); }
316
317 void GateMateImpl::postPlace()
318 {
319 repack();
320 ctx->assignArchInfo();
321 used_cpes.resize(ctx->getGridDimX() * ctx->getGridDimY());
322 pip_data.resize(ctx->getGridDimX() * ctx->getGridDimY());
323 pip_mask.resize(ctx->getGridDimX() * ctx->getGridDimY());
324
325 auto set_param_mask_data = [&](CellInfo *cell, IdString param, uint32_t pip_mask, uint32_t &mask, uint32_t &data) {
326 if (cell->params.count(param)) {
327 mask |= pip_mask;
328 if (int_or_default(cell->params, param, 0))
329 data |= pip_mask;
330 }
331 };
332 for (auto &cell : ctx->cells) {
333 // We need to skip CPE_MULT since using CP outputs is mandatory
334 // even if output is actually not connected
335 bool marked_used = cell.second.get()->type == id_CPE_MULT;
336 // Can not use FF for OUT2 if CPE is used in bridge mode
337 if (cell.second.get()->type == id_CPE_FF && ctx->getBelLocation(cell.second.get()->bel).z == CPE_FF_U_Z)
338 marked_used = true;
339 if (marked_used)
340 used_cpes[cell.second.get()->bel.tile] = true;
341
342 uint32_t mask = pip_mask[cell.second.get()->bel.tile];
343 uint32_t data = pip_data[cell.second.get()->bel.tile];
344 if (cell.second.get()->type == id_CPE_MULT) {
345 mask |= PipMask::IS_MULT;
346 data |= PipMask::IS_MULT;
347 }
348 if (cell.second.get()->type.in(id_CPE_ADDF, id_CPE_ADDF2)) {
349 data |= PipMask::IS_ADDF;
350 mask |= PipMask::IS_ADDF;
351 }
352 if (cell.second.get()->type == id_CPE_COMP) {
353 data |= PipMask::IS_COMP;
354 mask |= PipMask::IS_COMP;
355 }
356 set_param_mask_data(cell.second.get(), id_C_SELX, PipMask::C_SELX, mask, data);
357 set_param_mask_data(cell.second.get(), id_C_SELY1, PipMask::C_SELY1, mask, data);
358 set_param_mask_data(cell.second.get(), id_C_SELY2, PipMask::C_SELY2, mask, data);
359 set_param_mask_data(cell.second.get(), id_C_SEL_C, PipMask::C_SEL_C, mask, data);
360 set_param_mask_data(cell.second.get(), id_C_SEL_P, PipMask::C_SEL_P, mask, data);
361 set_param_mask_data(cell.second.get(), id_C_Y12, PipMask::C_Y12, mask, data);
362 set_param_mask_data(cell.second.get(), id_C_CX_I, PipMask::C_CX_I, mask, data);
363 set_param_mask_data(cell.second.get(), id_C_CY1_I, PipMask::C_CY1_I, mask, data);
364 set_param_mask_data(cell.second.get(), id_C_CY2_I, PipMask::C_CY2_I, mask, data);
365 set_param_mask_data(cell.second.get(), id_C_PX_I, PipMask::C_PX_I, mask, data);
366 set_param_mask_data(cell.second.get(), id_C_PY1_I, PipMask::C_PY1_I, mask, data);
367 set_param_mask_data(cell.second.get(), id_C_PY2_I, PipMask::C_PY2_I, mask, data);
368 pip_mask[cell.second.get()->bel.tile] = mask;
369 pip_data[cell.second.get()->bel.tile] = data;
370 }
371 }
372 bool GateMateImpl::checkPipAvail(PipId pip) const
373 {
374 const auto &extra_data = *pip_extra_data(pip);
375 if (!use_cp_for_clk && extra_data.type == PipExtra::PIP_EXTRA_MUX) {
376 if (extra_data.value == 1 && IdString(extra_data.name).in(id_C_CLKSEL, id_C_ENSEL))
377 return false;
378 }
379 if (!use_cp_for_cpe && extra_data.type == PipExtra::PIP_EXTRA_MUX && extra_data.resource != 0 &&
380 extra_data.resource <= PipMask::C_PY2_I) {
381 return false;
382 }
383 if (!use_bridges && extra_data.type == PipExtra::PIP_EXTRA_MUX &&
384 IdString(extra_data.name) == ctx->id("CPE.C_SN")) {
385 return false;
386 }
387 if (extra_data.type == PipExtra::PIP_EXTRA_MUX && (extra_data.block != 0)) {
388 if (pip_mask[pip.tile] & extra_data.block) {
389 return false;
390 }
391 }
392 if (extra_data.type == PipExtra::PIP_EXTRA_MUX && (extra_data.resource != 0)) {
393 if (pip_mask[pip.tile] & extra_data.resource) {
394 if ((pip_data[pip.tile] & extra_data.resource) != (extra_data.value ? extra_data.resource : 0)) {
395 return false;
396 }
397 }
398 }
399 if (extra_data.type != PipExtra::PIP_EXTRA_MUX || (extra_data.flags & MUX_ROUTING) == 0)
400 return true;
401 if (used_cpes[pip.tile])
402 return false;
403 return true;
404 }
405
406 void GateMateImpl::preRoute()
407 {
408 route_mult();
409 route_clock();
410 ctx->assignArchInfo();
411
412 for (auto &net : ctx->nets) {
413 NetInfo *ni = net.second.get();
414 if (ni->wires.empty())
415 continue;
416 for (auto &w : ni->wires) {
417 if (w.second.pip != PipId()) {
418 const auto &extra_data = *pip_extra_data(w.second.pip);
419 if (extra_data.type == PipExtra::PIP_EXTRA_MUX && extra_data.resource != 0) {
420 pip_mask[w.second.pip.tile] |= extra_data.resource;
421 pip_data[w.second.pip.tile] |= extra_data.value ? extra_data.resource : 0;
422 }
423 }
424 }
425 }
426 }
427
428 void GateMateImpl::reassign_bridges(NetInfo *ni, const dict<WireId, PipMap> &net_wires, WireId wire,
429 dict<WireId, IdString> &wire_to_net, int &num)
430 {
431 wire_to_net.insert({wire, ni->name});
432
433 for (auto pip : ctx->getPipsDownhill(wire)) {
434 auto dst = ctx->getPipDstWire(pip);
435
436 // Ignore wires not part of the net
437 auto it = net_wires.find(dst);
438 if (it == net_wires.end())
439 continue;
440 // Ignore pips if the wire is driven by another pip.
441 if (pip != it->second.pip)
442 continue;
443 // Ignore wires already visited.
444 if (wire_to_net.count(dst))
445 continue;
446
447 const auto &extra_data = *pip_extra_data(pip);
448 // If not a bridge, just recurse.
449 if (extra_data.type != PipExtra::PIP_EXTRA_MUX || !(extra_data.flags & MUX_ROUTING)) {
450 reassign_bridges(ni, net_wires, dst, wire_to_net, num);
451 continue;
452 }
453
454 // We have a bridge that needs to be translated to a bel.
455 IdString name = ctx->idf("%s$bridge%d", ni->name.c_str(ctx), num);
456
457 IdStringList id = ctx->getPipName(pip);
458 Loc loc = ctx->getPipLocation(pip);
459 BelId bel = ctx->getBelByLocation({loc.x, loc.y, CPE_BRIDGE_Z});
460 CellInfo *cell = ctx->createCell(name, id_CPE_BRIDGE);
461 ctx->bindBel(bel, cell, PlaceStrength::STRENGTH_FIXED);
462 cell->params[id_C_BR] = Property(Property::State::S1, 1);
463 cell->params[id_C_SN] = Property(extra_data.value, 3);
464
465 NetInfo *new_net = ctx->createNet(ctx->idf("%s$muxout", name.c_str(ctx)));
466 IdString in_port = ctx->idf("IN%d", extra_data.value + 1);
467
468 auto add_port = [&](const IdString id, PortType dir) {
469 cell->ports[id].name = id;
470 cell->ports[id].type = dir;
471 cell->cell_bel_pins[id] = std::vector{id};
472 };
473
474 add_port(in_port, PORT_IN);
475 add_port(id_MUXOUT, PORT_OUT);
476
477 cell->connectPort(in_port, ni);
478 cell->connectPort(id_MUXOUT, new_net);
479 pass_backtrace[cell->name][id_MUXOUT] = in_port;
480
481 num++;
482
483 reassign_bridges(new_net, net_wires, dst, wire_to_net, num);
484 }
485 }
486
487 void GateMateImpl::reassign_cplines(NetInfo *ni, const dict<WireId, PipMap> &net_wires, WireId wire,
488 dict<WireId, IdString> &wire_to_net, int &num, IdString in_port)
489 {
490 wire_to_net.insert({wire, ni->name});
491
492 for (auto pip : ctx->getPipsDownhill(wire)) {
493 auto dst = ctx->getPipDstWire(pip);
494
495 // Ignore wires not part of the net
496 auto it = net_wires.find(dst);
497 if (it == net_wires.end())
498 continue;
499 // Ignore pips if the wire is driven by another pip.
500 if (pip != it->second.pip)
501 continue;
502 // Ignore wires already visited.
503 if (wire_to_net.count(dst))
504 continue;
505
506 const auto &extra_data = *pip_extra_data(pip);
507 // If not a CP line pip, just recurse.
508 if (extra_data.type != PipExtra::PIP_EXTRA_MUX || extra_data.resource == 0) {
509 reassign_cplines(ni, net_wires, dst, wire_to_net, num, in_port);
510 continue;
511 }
512
513 // We have a bridge that needs to be translated to a bel.
514 IdStringList id = ctx->getPipName(pip);
515 Loc loc = ctx->getPipLocation(pip);
516 BelId bel = ctx->getBelByLocation({loc.x, loc.y, CPE_CPLINES_Z});
517 CellInfo *cell = ctx->getBoundBelCell(bel);
518 if (!cell) {
519 IdString name = ctx->idf("cplines$%s", id[0].c_str(ctx));
520 cell = ctx->createCell(name, id_CPE_CPLINES);
521 auto add_port = [&](const IdString id, PortType dir) {
522 cell->ports[id].name = id;
523 cell->ports[id].type = dir;
524 cell->cell_bel_pins[id] = std::vector{id};
525 };
526
527 add_port(id_OUT1, PORT_IN);
528 add_port(id_OUT2, PORT_IN);
529 add_port(id_COMPOUT, PORT_IN);
530
531 add_port(id_CINX, PORT_IN);
532 add_port(id_PINX, PORT_IN);
533 add_port(id_CINY1, PORT_IN);
534 add_port(id_PINY1, PORT_IN);
535 add_port(id_CINY2, PORT_IN);
536 add_port(id_PINY2, PORT_IN);
537
538 add_port(id_COUTX, PORT_OUT);
539 add_port(id_POUTX, PORT_OUT);
540 add_port(id_COUTY1, PORT_OUT);
541 add_port(id_POUTY1, PORT_OUT);
542 add_port(id_COUTY2, PORT_OUT);
543 add_port(id_POUTY2, PORT_OUT);
544
545 ctx->bindBel(bel, cell, PlaceStrength::STRENGTH_FIXED);
546 }
547
548 cell->setParam(ctx->getGroupName(ctx->getResourceKeyForPip(pip))[1],
549 Property(extra_data.value, extra_data.bits));
550
551 // We have to discover the ports needed by this config.
552 auto input_port_map = dict<IdString, IdString>{
553 {ctx->id("CPE.CINX"), id_CINX},
554 {ctx->id("CPE.CINY1"), id_CINY1},
555 {ctx->id("CPE.CINY2"), id_CINY2},
556 {ctx->id("CPE.PINX"), id_PINX},
557 {ctx->id("CPE.PINY1"), id_PINY1},
558 {ctx->id("CPE.PINY2"), id_PINY2},
559 {ctx->id("CPE.OUT1_IN_int"), id_OUT1},
560 {ctx->id("CPE.OUT2_IN_int"), id_OUT2},
561 {ctx->id("CPE.COMPOUT_IN_int"), id_COMPOUT},
562 };
563
564 auto input_port_name = input_port_map.find(ctx->getWireName(ctx->getPipSrcWire(pip))[1]);
565 if (input_port_name != input_port_map.end()) {
566 if (cell->getPort(input_port_name->second) == nullptr) {
567 cell->connectPort(input_port_name->second, ni);
568 in_port = input_port_name->second;
569 } else
570 NPNR_ASSERT(cell->getPort(input_port_name->second) == ni);
571 }
572
573 auto output_port_map =
574 dict<IdString, IdString>{{ctx->id("CPE.COUTX"), id_COUTX}, {ctx->id("CPE.COUTY1"), id_COUTY1},
575 {ctx->id("CPE.COUTY2"), id_COUTY2}, {ctx->id("CPE.POUTX"), id_POUTX},
576 {ctx->id("CPE.POUTY1"), id_POUTY1}, {ctx->id("CPE.POUTY2"), id_POUTY2}};
577
578 auto output_port_name = output_port_map.find(ctx->getWireName(ctx->getPipDstWire(pip))[1]);
579 if (output_port_name != output_port_map.end()) {
580 NetInfo *new_net =
581 ctx->createNet(ctx->idf("%s$%s", cell->name.c_str(ctx), output_port_name->second.c_str(ctx)));
582
583 cell->addOutput(output_port_name->second);
584 cell->connectPort(output_port_name->second, new_net);
585 pass_backtrace[cell->name][output_port_name->second] = in_port;
586
587 num++;
588
589 reassign_cplines(new_net, net_wires, dst, wire_to_net, num, in_port);
590 } else {
591 // this is an internal resource pip; recurse anyway.
592 reassign_cplines(ni, net_wires, dst, wire_to_net, num, in_port);
593 }
594 }
595 }
596
597 void GateMateImpl::postRoute()
598 {
599 int num = 0;
600
601 pool<IdString> nets_with_bridges;
602 pool<IdString> nets_with_cplines;
603
604 for (auto &net : ctx->nets) {
605 NetInfo *ni = net.second.get();
606 for (auto &w : ni->wires) {
607 if (w.second.pip != PipId()) {
608 const auto &extra_data = *pip_extra_data(w.second.pip);
609 if (extra_data.type == PipExtra::PIP_EXTRA_MUX && (extra_data.flags & MUX_ROUTING)) {
610 nets_with_bridges.insert(ni->name);
611 }
612 }
613 }
614 }
615
616 for (auto net_name : nets_with_bridges) {
617 auto *ni = ctx->nets.at(net_name).get();
618 auto net_wires = ni->wires; // copy wires to preserve across unbind/rebind.
619 auto wire_to_net = dict<WireId, IdString>{};
620 auto wire_to_port = dict<WireId, std::vector<PortRef>>{};
621
622 for (auto &usr : ni->users)
623 for (auto sink_wire : ctx->getNetinfoSinkWires(ni, usr)) {
624 auto result = wire_to_port.find(sink_wire);
625 if (result == wire_to_port.end())
626 wire_to_port.insert({sink_wire, std::vector<PortRef>{usr}});
627 else
628 result->second.push_back(usr);
629 }
630
631 // traverse the routing tree to assign bridge nets to wires.
632 reassign_bridges(ni, net_wires, ctx->getNetinfoSourceWire(ni), wire_to_net, num);
633
634 for (auto &pair : net_wires)
635 ctx->unbindWire(pair.first);
636
637 for (auto &pair : net_wires) {
638 auto wire = pair.first;
639 auto pip = pair.second.pip;
640 auto strength = pair.second.strength;
641 auto *net = ctx->nets.at(wire_to_net.at(wire)).get();
642 if (pip == PipId())
643 ctx->bindWire(wire, net, strength);
644 else
645 ctx->bindPip(pip, net, strength);
646
647 if (wire_to_port.count(wire)) {
648 for (auto sink : wire_to_port.at(wire)) {
649 NPNR_ASSERT(sink.cell != nullptr && sink.port != IdString());
650 sink.cell->disconnectPort(sink.port);
651 sink.cell->connectPort(sink.port, net);
652 }
653 }
654 }
655 }
656
657 num = 0;
658
659 for (auto &net : ctx->nets) {
660 NetInfo *ni = net.second.get();
661 for (auto &w : ni->wires) {
662 if (w.second.pip != PipId()) {
663 const auto &extra_data = *pip_extra_data(w.second.pip);
664 if (extra_data.type == PipExtra::PIP_EXTRA_MUX && extra_data.resource != 0) {
665 nets_with_cplines.insert(ni->name);
666 }
667 }
668 }
669 }
670
671 for (auto net_name : nets_with_cplines) {
672 auto *ni = ctx->nets.at(net_name).get();
673 auto net_wires = ni->wires; // copy wires to preserve across unbind/rebind.
674 auto wire_to_net = dict<WireId, IdString>{};
675 auto wire_to_port = dict<WireId, std::vector<PortRef>>{};
676
677 for (auto &usr : ni->users)
678 for (auto sink_wire : ctx->getNetinfoSinkWires(ni, usr)) {
679 auto result = wire_to_port.find(sink_wire);
680 if (result == wire_to_port.end())
681 wire_to_port.insert({sink_wire, std::vector<PortRef>{usr}});
682 else
683 result->second.push_back(usr);
684 }
685
686 // traverse the routing tree to assign bridge nets to wires.
687 reassign_cplines(ni, net_wires, ctx->getNetinfoSourceWire(ni), wire_to_net, num, IdString());
688
689 for (auto &pair : net_wires)
690 ctx->unbindWire(pair.first);
691
692 for (auto &pair : net_wires) {
693 auto wire = pair.first;
694 auto pip = pair.second.pip;
695 auto strength = pair.second.strength;
696 auto *net = ctx->nets.at(wire_to_net.at(wire)).get();
697 if (pip == PipId())
698 ctx->bindWire(wire, net, strength);
699 else
700 ctx->bindPip(pip, net, strength);
701
702 if (wire_to_port.count(wire)) {
703 for (auto sink : wire_to_port.at(wire)) {
704 NPNR_ASSERT(sink.cell != nullptr && sink.port != IdString());
705 sink.cell->disconnectPort(sink.port);
706 sink.cell->connectPort(sink.port, net);
707 }
708 }
709 }
710 }
711
712 dict<IdString, int> cfg;
713 dict<IdString, IdString> port_mapping;
714 auto add_input = [&](IdString orig_port, IdString port, bool merged) -> bool {
715 static dict<IdString, IdString> convert_port = {
716 {ctx->id("CPE.IN1"), id_IN1}, {ctx->id("CPE.IN2"), id_IN2}, {ctx->id("CPE.IN3"), id_IN3},
717 {ctx->id("CPE.IN4"), id_IN4}, {ctx->id("CPE.IN5"), id_IN1}, {ctx->id("CPE.IN6"), id_IN2},
718 {ctx->id("CPE.IN7"), id_IN3}, {ctx->id("CPE.IN8"), id_IN4}, {ctx->id("CPE.PINY1"), id_PINY1},
719 {ctx->id("CPE.PINY2"), id_PINY2}, {ctx->id("CPE.CINY2"), id_CINY2}, {ctx->id("CPE.CLK"), id_CLK},
720 {ctx->id("CPE.EN"), id_EN}, {ctx->id("CPE.CINX"), id_CINX}, {ctx->id("CPE.PINX"), id_PINX}};
721 static dict<IdString, IdString> convert_port_merged = {
722 {ctx->id("CPE.IN1"), id_IN1}, {ctx->id("CPE.IN2"), id_IN2}, {ctx->id("CPE.IN3"), id_IN3},
723 {ctx->id("CPE.IN4"), id_IN4}, {ctx->id("CPE.IN5"), id_IN5}, {ctx->id("CPE.IN6"), id_IN6},
724 {ctx->id("CPE.IN7"), id_IN7}, {ctx->id("CPE.IN8"), id_IN8}, {ctx->id("CPE.PINY1"), id_PINY1},
725 {ctx->id("CPE.PINY2"), id_PINY2}, {ctx->id("CPE.CINY2"), id_CINY2}, {ctx->id("CPE.CLK"), id_CLK},
726 {ctx->id("CPE.EN"), id_EN}, {ctx->id("CPE.CINX"), id_CINX}, {ctx->id("CPE.PINX"), id_PINX}};
727 if (convert_port.count(port)) {
728 port_mapping.emplace(orig_port, merged ? convert_port_merged[port] : convert_port[port]);
729 return true;
730 };
731 return false;
732 };
733 auto check_input = [&](CellInfo *cell, IdString port, bool merged) {
734 if (cell->getPort(port)) {
735 NetInfo *net = cell->getPort(port);
736 WireId src = ctx->getBelPinWire(cell->bel, port);
737 // In current chip db real CPE input is max 4 pips away
738 for (int i = 0; i < 4; i++) {
739 if (net->wires.count(src)) {
740 auto &p = net->wires.at(src);
741 src = ctx->getPipSrcWire(p.pip);
742 const auto &extra_data = *pip_extra_data(p.pip);
743 if (extra_data.type == PipExtra::PIP_EXTRA_MUX) {
744 cfg.emplace(IdString(extra_data.name), extra_data.value);
745 if (add_input(port, ctx->getWireName(src)[1], merged))
746 break;
747 }
748 }
749 }
750 }
751 };
752 auto swap_lut2_inputs = [&](int lut) -> int {
753 // bit permutation: [3,1,2,0]
754 return ((lut & 0b1000)) | // b3 -> bit 3
755 ((lut & 0b0010) << 1) | // b1 -> bit 2
756 ((lut & 0b0100) >> 1) | // b2 -> bit 1
757 ((lut & 0b0001)); // b0 -> bit 0
758 };
759
760 log_info("Update configuration based on routing..\n");
761 for (auto &cell : ctx->cells) {
762 if (cell.second->type.in(id_CPE_L2T4)) {
763 cfg.clear();
764 port_mapping.clear();
765
766 int l00 = int_or_default(cell.second->params, id_INIT_L00, 0);
767 int l01 = int_or_default(cell.second->params, id_INIT_L01, 0);
768 int l10 = int_or_default(cell.second->params, id_INIT_L10, 0);
769
770 check_input(cell.second.get(), id_D0_00, false);
771 check_input(cell.second.get(), id_D1_00, false);
772 check_input(cell.second.get(), id_D0_01, false);
773 check_input(cell.second.get(), id_D1_01, false);
774 check_input(cell.second.get(), id_D0_10, false);
775 check_input(cell.second.get(), id_D1_10, false);
776 if (cfg.count(id_LUT2_11) || cfg.count(id_LUT2_10)) {
777 if (cfg.count(id_LUT2_11)) { // lower
778 if (cfg.count(id_LUT2_02) && !cfg.count(id_LUT2_03)) {
779 // both inputs on 02
780 l00 = l10; // config is now in 02
781 l10 = 0b1010; // LUT_D0 - we propagate only
782 } else if (!cfg.count(id_LUT2_02) && cfg.count(id_LUT2_03)) {
783 // both inputs on 03
784 l01 = l10; // config is now in 03
785 l10 = 0b1010; // LUT_D0 - we propagate only
786 } else {
787 // one input on 02, other on 03 (or LUT1)
788 if (cfg.count(id_LUT2_02))
789 l00 = 0b1010; // LUT_D0 - we propagate only
790 if (cfg.count(id_LUT2_03))
791 l01 = 0b1010; // LUT_D0 - we propagate only
792 }
793
794 if (cfg.at(id_LUT2_11) == 1)
795 l10 = swap_lut2_inputs(l10);
796 if (cfg.count(id_LUT2_02) && (cfg.at(id_LUT2_02) == 1))
797 l00 = swap_lut2_inputs(l00);
798 if (cfg.count(id_LUT2_03) && (cfg.at(id_LUT2_03) == 1))
799 l01 = swap_lut2_inputs(l01);
800 } else { // upper part
801 if (cfg.count(id_LUT2_00) && !cfg.count(id_LUT2_01)) {
802 // both inputs on 02
803 l00 = l10; // config is now in 02
804 l10 = 0b1010; // LUT_D0 - we propagate only
805 } else if (!cfg.count(id_LUT2_00) && cfg.count(id_LUT2_01)) {
806 // both inputs on 03
807 l01 = l10; // config is now in 03
808 l10 = 0b1010; // LUT_D0 - we propagate only
809 } else {
810 // one input on 02, other on 03 (or LUT1)
811 if (cfg.count(id_LUT2_00))
812 l00 = 0b1010; // LUT_D0 - we propagate only
813 if (cfg.count(id_LUT2_01))
814 l01 = 0b1010; // LUT_D0 - we propagate only
815 }
816
817 if (cfg.at(id_LUT2_10) == 1)
818 l10 = swap_lut2_inputs(l10);
819 if (cfg.count(id_LUT2_00) && (cfg.at(id_LUT2_00) == 1))
820 l00 = swap_lut2_inputs(l00);
821 if (cfg.count(id_LUT2_01) && (cfg.at(id_LUT2_01) == 1))
822 l01 = swap_lut2_inputs(l01);
823 }
824 cell.second->params[id_INIT_L00] = Property(l00, 4);
825 cell.second->params[id_INIT_L01] = Property(l01, 4);
826 cell.second->params[id_INIT_L10] = Property(l10, 4);
827
828 cell.second->renamePort(id_D0_10, port_mapping[id_D0_10]);
829 cell.second->renamePort(id_D1_10, port_mapping[id_D1_10]);
830 } else {
831 if (cfg.count(id_LUT2_00) && cfg.at(id_LUT2_00) == 1)
832 l00 = swap_lut2_inputs(l00);
833 if (cfg.count(id_LUT2_01) && cfg.at(id_LUT2_01) == 1)
834 l01 = swap_lut2_inputs(l01);
835 if (cfg.count(id_LUT2_02) && cfg.at(id_LUT2_02) == 1)
836 l00 = swap_lut2_inputs(l00);
837 if (cfg.count(id_LUT2_03) && cfg.at(id_LUT2_03) == 1)
838 l01 = swap_lut2_inputs(l01);
839
840 cell.second->params[id_INIT_L00] = Property(l00, 4);
841 cell.second->params[id_INIT_L01] = Property(l01, 4);
842 cell.second->params[id_INIT_L10] = Property(l10, 4);
843
844 cell.second->renamePort(id_D0_00, port_mapping[id_D0_00]);
845 cell.second->renamePort(id_D1_00, port_mapping[id_D1_00]);
846 cell.second->renamePort(id_D0_01, port_mapping[id_D0_01]);
847 cell.second->renamePort(id_D1_01, port_mapping[id_D1_01]);
848 }
849 if (cfg.count(id_C_I1) && cfg.at(id_C_I1) == 1)
850 cell.second->params[id_C_I1] = Property(1, 1);
851 if (cfg.count(id_C_I2) && cfg.at(id_C_I2) == 1)
852 cell.second->params[id_C_I2] = Property(1, 1);
853 if (cfg.count(id_C_I3) && cfg.at(id_C_I3) == 1)
854 cell.second->params[id_C_I3] = Property(1, 1);
855 if (cfg.count(id_C_I4) && cfg.at(id_C_I4) == 1)
856 cell.second->params[id_C_I4] = Property(1, 1);
857 }
858 if (cell.second->type.in(id_CPE_MX4, id_CPE_ADDF, id_CPE_ADDF2)) {
859 cfg.clear();
860 port_mapping.clear();
861
862 int l00 = int_or_default(cell.second->params, id_INIT_L00, 0);
863 int l01 = int_or_default(cell.second->params, id_INIT_L01, 0);
864 int l02 = int_or_default(cell.second->params, id_INIT_L02, 0);
865 int l03 = int_or_default(cell.second->params, id_INIT_L03, 0);
866
867 check_input(cell.second.get(), id_D0_00, true);
868 check_input(cell.second.get(), id_D1_00, true);
869 check_input(cell.second.get(), id_D0_01, true);
870 check_input(cell.second.get(), id_D1_01, true);
871 check_input(cell.second.get(), id_D0_02, true);
872 check_input(cell.second.get(), id_D1_02, true);
873 check_input(cell.second.get(), id_D0_03, true);
874 check_input(cell.second.get(), id_D1_03, true);
875
876 if (cfg.count(id_LUT2_00) && cfg.at(id_LUT2_00) == 1)
877 l00 = swap_lut2_inputs(l00);
878 if (cfg.count(id_LUT2_01) && cfg.at(id_LUT2_01) == 1)
879 l01 = swap_lut2_inputs(l01);
880 if (cfg.count(id_LUT2_02) && cfg.at(id_LUT2_02) == 1)
881 l02 = swap_lut2_inputs(l02);
882 if (cfg.count(id_LUT2_03) && cfg.at(id_LUT2_03) == 1)
883 l03 = swap_lut2_inputs(l03);
884
885 cell.second->params[id_INIT_L00] = Property(l00, 4);
886 cell.second->params[id_INIT_L01] = Property(l01, 4);
887 cell.second->params[id_INIT_L02] = Property(l02, 4);
888 cell.second->params[id_INIT_L03] = Property(l03, 4);
889
890 cell.second->renamePort(id_D0_00, port_mapping[id_D0_00]);
891 cell.second->renamePort(id_D1_00, port_mapping[id_D1_00]);
892 cell.second->renamePort(id_D0_01, port_mapping[id_D0_01]);
893 cell.second->renamePort(id_D1_01, port_mapping[id_D1_01]);
894 cell.second->renamePort(id_D0_02, port_mapping[id_D0_02]);
895 cell.second->renamePort(id_D1_02, port_mapping[id_D1_02]);
896 cell.second->renamePort(id_D0_03, port_mapping[id_D0_03]);
897 cell.second->renamePort(id_D1_03, port_mapping[id_D1_03]);
898
899 if (cfg.count(id_C_I1) && cfg.at(id_C_I1) == 1)
900 cell.second->params[id_C_I1] = Property(1, 1);
901 if (cfg.count(id_C_I2) && cfg.at(id_C_I2) == 1)
902 cell.second->params[id_C_I2] = Property(1, 1);
903 if (cfg.count(id_C_I3) && cfg.at(id_C_I3) == 1)
904 cell.second->params[id_C_I3] = Property(1, 1);
905 if (cfg.count(id_C_I4) && cfg.at(id_C_I4) == 1)
906 cell.second->params[id_C_I4] = Property(1, 1);
907 }
908 if (cell.second->type.in(id_CPE_FF, id_CPE_FF_L, id_CPE_FF_U, id_CPE_LATCH)) {
909 cfg.clear();
910 port_mapping.clear();
911 check_input(cell.second.get(), id_CLK_INT, false);
912 check_input(cell.second.get(), id_EN_INT, false);
913 if (cfg.count(id_C_CLKSEL) && cfg.at(id_C_CLKSEL) == 1) {
914 uint8_t val = int_or_default(cell.second->params, id_C_CPE_CLK, 0) & 1;
915 cell.second->params[id_C_CPE_CLK] = Property(val ? 3 : 0, 2);
916 cell.second->params[id_C_CLKSEL] = Property(1, 1);
917 }
918 if (cfg.count(id_C_ENSEL) && cfg.at(id_C_ENSEL) == 1) {
919 uint8_t val = int_or_default(cell.second->params, id_C_CPE_EN, 0) & 1;
920 cell.second->params[id_C_CPE_EN] = Property(val ? 3 : 0, 2);
921 cell.second->params[id_C_ENSEL] = Property(1, 1);
922 }
923 cell.second->renamePort(id_CLK_INT, port_mapping[id_CLK_INT]);
924 cell.second->renamePort(id_EN_INT, port_mapping[id_EN_INT]);
925 }
926 }
927 ctx->assignArchInfo();
928
929 const ArchArgs &args = ctx->args;
930 if (args.options.count("out")) {
931 write_bitstream(args.device, args.options["out"].as<std::string>());
932 }
933 }
934
935 BoundingBox GateMateImpl::getRouteBoundingBox(WireId src, WireId dst) const
936 {
937 int x0, y0, x1, y1;
938 auto expand = [&](int x, int y) {
939 x0 = std::min(x0, x);
940 x1 = std::max(x1, x);
941 y0 = std::min(y0, y);
942 y1 = std::max(y1, y);
943 };
944 tile_xy(ctx->chip_info, src.tile, x0, y0);
945 x1 = x0;
946 y1 = y0;
947 int dx, dy;
948 tile_xy(ctx->chip_info, dst.tile, dx, dy);
949 expand(dx, dy);
950
951 return {(x0 & 0xfffe), (y0 & 0xfffe), (x1 & 0xfffe) + 1, (y1 & 0xfffe) + 1};
952 }
953
954 void GateMateImpl::expandBoundingBox(BoundingBox &bb) const
955 {
956 bb.x0 = std::max((bb.x0 & 0xfffe) - 4, 0);
957 bb.y0 = std::max((bb.y0 & 0xfffe) - 4, 0);
958 bb.x1 = std::min((bb.x1 & 0xfffe) + 5, ctx->getGridDimX());
959 bb.y1 = std::min((bb.y1 & 0xfffe) + 5, ctx->getGridDimY());
960 }
961
962 GroupId GateMateImpl::getResourceKeyForPip(PipId pip) const
963 {
964 const auto &extra_data = *pip_extra_data(pip);
965 if (extra_data.type != PipExtra::PIP_EXTRA_MUX || extra_data.group_index == 0)
966 return GroupId();
967
968 return GroupId(pip.tile, extra_data.group_index);
969 }
970
971 int GateMateImpl::getResourceValueForPip(PipId pip) const
972 {
973 const auto &extra_data = *pip_extra_data(pip);
974 if (extra_data.type != PipExtra::PIP_EXTRA_MUX || extra_data.resource == 0)
975 return 0;
976 return extra_data.value;
977 }
978
979 bool GateMateImpl::isGroupResource(GroupId group) const { return ctx->getGroupType(group) == id_RESOURCE; }
980
981 void GateMateImpl::configurePlacerHeap(PlacerHeapCfg &cfg)
982 {
983 cfg.chainRipup = true;
984 cfg.placeAllAtOnce = true;
985 }
986
987 int GateMateImpl::get_dff_config(CellInfo *dff) const
988 {
989 int val = 0;
990 val |= int_or_default(dff->params, id_C_CPE_EN, 0);
991 val <<= 2;
992 val |= int_or_default(dff->params, id_C_CPE_CLK, 0);
993 val <<= 2;
994 val |= int_or_default(dff->params, id_C_CPE_RES, 0);
995 val <<= 2;
996 val |= int_or_default(dff->params, id_C_CPE_SET, 0);
997 val <<= 2;
998 val |= int_or_default(dff->params, id_C_EN_SR, 0);
999 val <<= 1;
1000 val |= int_or_default(dff->params, id_C_L_D, 0);
1001 val <<= 1;
1002 val |= int_or_default(dff->params, id_FF_INIT, 0);
1003 return val;
1004 }
1005
1006 int GateMateImpl::get_ram_config(CellInfo *ram) const
1007 {
1008 int val = 0;
1009 val |= int_or_default(ram->params, id_RAM_cfg_ecc_enable, 0);
1010 val <<= 2;
1011 val |= int_or_default(ram->params, id_RAM_cfg_sram_mode, 0);
1012 return val;
1013 }
1014
1015 void GateMateImpl::assign_cell_info()
1016 {
1017 fast_cell_info.resize(ctx->cells.size());
1018 for (auto &cell : ctx->cells) {
1019 CellInfo *ci = cell.second.get();
1020 auto &fc = fast_cell_info.at(ci->flat_index);
1021 if (getBelBucketForCellType(ci->type) == id_CPE_FF) {
1022 fc.ff_en = ci->getPort(id_EN_INT);
1023 fc.ff_clk = ci->getPort(id_CLK_INT);
1024 fc.ff_sr = ci->getPort(id_SR);
1025 fc.config = get_dff_config(ci);
1026 fc.used = true;
1027 }
1028 if (getBelBucketForCellType(ci->type) == id_RAM_HALF) {
1029 fc.config = get_ram_config(ci);
1030 fc.used = true;
1031 }
1032 }
1033 }
1034
1035 // Bel bucket functions
1036 IdString GateMateImpl::getBelBucketForCellType(IdString cell_type) const
1037 {
1038 if (cell_type.in(id_CPE_IBUF, id_CPE_OBUF, id_CPE_TOBUF, id_CPE_IOBUF, id_CPE_LVDS_IBUF, id_CPE_LVDS_TOBUF,
1039 id_CPE_LVDS_OBUF, id_CPE_LVDS_IOBUF))
1040 return id_GPIO;
1041 else if (cell_type.in(id_CPE_LT_U, id_CPE_LT_L, id_CPE_LT, id_CPE_L2T4))
1042 return id_CPE_LT;
1043 else if (cell_type.in(id_CPE_FF_U, id_CPE_FF_L, id_CPE_FF, id_CPE_LATCH))
1044 return id_CPE_FF;
1045 else if (cell_type.in(id_CPE_RAMIO, id_CPE_RAMI, id_CPE_RAMO))
1046 return id_CPE_RAMIO;
1047 else if (cell_type.in(id_RAM, id_RAM_HALF, id_RAM_HALF_DUMMY))
1048 return id_RAM_HALF;
1049 else
1050 return cell_type;
1051 }
1052
1053 BelBucketId GateMateImpl::getBelBucketForBel(BelId bel) const
1054 {
1055 IdString bel_type = ctx->getBelType(bel);
1056 if (bel_type.in(id_CPE_LT_U, id_CPE_LT_L))
1057 return id_CPE_LT;
1058 else if (bel_type.in(id_CPE_FF_U, id_CPE_FF_L))
1059 return id_CPE_FF;
1060 else if (bel_type.in(id_CPE_RAMIO_U, id_CPE_RAMIO_L))
1061 return id_CPE_RAMIO;
1062 else if (bel_type.in(id_RAM, id_RAM_HALF_L))
1063 return id_RAM_HALF;
1064 return bel_type;
1065 }
1066
1067 bool GateMateImpl::isValidBelForCellType(IdString cell_type, BelId bel) const
1068 {
1069 IdString bel_type = ctx->getBelType(bel);
1070 if (bel_type == id_GPIO)
1071 return cell_type.in(id_CPE_IBUF, id_CPE_OBUF, id_CPE_TOBUF, id_CPE_IOBUF, id_CPE_LVDS_IBUF, id_CPE_LVDS_TOBUF,
1072 id_CPE_LVDS_OBUF, id_CPE_LVDS_IOBUF);
1073 else if (bel_type == id_CPE_LT_U)
1074 return cell_type.in(id_CPE_LT_U, id_CPE_LT, id_CPE_L2T4, id_CPE_DUMMY);
1075 else if (bel_type == id_CPE_LT_L)
1076 return cell_type.in(id_CPE_LT_L, id_CPE_LT, id_CPE_L2T4, id_CPE_DUMMY);
1077 else if (bel_type == id_CPE_FF_U)
1078 return cell_type.in(id_CPE_FF_U, id_CPE_FF, id_CPE_LATCH);
1079 else if (bel_type == id_CPE_FF_L)
1080 return cell_type.in(id_CPE_FF_L, id_CPE_FF, id_CPE_LATCH);
1081 else if (bel_type.in(id_CPE_RAMIO_U, id_CPE_RAMIO_L))
1082 return cell_type.in(id_CPE_RAMIO, id_CPE_RAMI, id_CPE_RAMO);
1083 else if (bel_type == id_RAM)
1084 return cell_type.in(id_RAM_HALF, id_RAM);
1085 else if (bel_type == id_RAM_HALF_L)
1086 return cell_type.in(id_RAM_HALF, id_RAM_HALF_DUMMY);
1087 else
1088 return (bel_type == cell_type);
1089 }
1090
1091 bool GateMateImpl::isPipInverting(PipId pip) const
1092 {
1093 const auto &extra_data = *pip_extra_data(pip);
1094 return extra_data.type == PipExtra::PIP_EXTRA_MUX && (extra_data.flags & MUX_INVERT);
1095 }
1096
1097 const GateMateTileExtraDataPOD *GateMateImpl::tile_extra_data(int tile) const
1098 {
1099 return reinterpret_cast<const GateMateTileExtraDataPOD *>(ctx->chip_info->tile_insts[tile].extra_data.get());
1100 }
1101
1102 const GateMateBelExtraDataPOD *GateMateImpl::bel_extra_data(BelId bel) const
1103 {
1104 return reinterpret_cast<const GateMateBelExtraDataPOD *>(chip_bel_info(ctx->chip_info, bel).extra_data.get());
1105 }
1106
1107 const GateMatePipExtraDataPOD *GateMateImpl::pip_extra_data(PipId pip) const
1108 {
1109 return reinterpret_cast<const GateMatePipExtraDataPOD *>(chip_pip_info(ctx->chip_info, pip).extra_data.get());
1110 }
1111
1112 struct GateMateArch : HimbaechelArch
1113 {
1114 GateMateArch() : HimbaechelArch("gatemate") {};
1115 bool match_device(const std::string &device) override
1116 {
1117 return device.size() > 6 && device.substr(0, 6) == "CCGM1A";
1118 }
1119 std::unique_ptr<HimbaechelAPI> create(const std::string &device) override
1120 {
1121 return std::make_unique<GateMateImpl>();
1122 }
1123 } gateMateArch;
1124
1125 NEXTPNR_NAMESPACE_END