Debian Codegraph @

himbaechel/uarch/gowin/gowin.cc

raw
1 #include <map>
2 #include <regex>
3
4 #include "himbaechel_api.h"
5 #include "himbaechel_helpers.h"
6 #include "log.h"
7 #include "nextpnr.h"
8
9 #define GEN_INIT_CONSTIDS
10 #define HIMBAECHEL_CONSTIDS "uarch/gowin/constids.inc"
11 #include "himbaechel_constids.h"
12
13 #include "array2d.h"
14 #include "cst.h"
15 #include "globals.h"
16 #include "gowin.h"
17 #include "gowin_utils.h"
18 #include "pack.h"
19
20 #include "placer_heap.h"
21
22 NEXTPNR_NAMESPACE_BEGIN
23
24 namespace {
25 struct GowinImpl : HimbaechelAPI
26 {
27
28 ~GowinImpl() {};
29 po::options_description getUArchOptions() override;
30 void init_database(Arch *arch) override;
31 void init(Context *ctx) override;
32
33 void pack() override;
34 void prePlace() override;
35 void postPlace() override;
36 void preRoute() override;
37 void postRoute() override;
38
39 bool isBelLocationValid(BelId bel, bool explain_invalid) const override;
40 void notifyBelChange(BelId bel, CellInfo *cell) override;
41
42 // Bel bucket functions
43 IdString getBelBucketForCellType(IdString cell_type) const override;
44
45 bool isValidBelForCellType(IdString cell_type, BelId bel) const override;
46
47 // wires
48 bool checkPipAvail(PipId pip) const override;
49 bool checkPipAvailForNet(PipId pip, const NetInfo *net) const override;
50
51 // Cluster
52 bool isClusterStrict(const CellInfo *cell) const override { return true; }
53 bool getClusterPlacement(ClusterId cluster, BelId root_bel,
54 std::vector<std::pair<CellInfo *, BelId>> &placement) const override;
55
56 void configurePlacerHeap(PlacerHeapCfg &cfg) override;
57
58 void drawBel(std::vector<GraphicElement> &g, GraphicElement::style_t style, IdString bel_type, Loc loc) override;
59
60 private:
61 HimbaechelHelpers h;
62 GowinUtils gwu;
63
64 IdString chip;
65 IdString partno;
66
67 std::set<BelId> inactive_bels;
68
69 // Validity checking
70 struct GowinCellInfo
71 {
72 // slice info
73 const NetInfo *lut_f = nullptr;
74 const NetInfo *ff_d = nullptr, *ff_ce = nullptr, *ff_clk = nullptr, *ff_lsr = nullptr;
75 const NetInfo *alu_sum = nullptr;
76 // dsp info
77 const NetInfo *dsp_asign = nullptr, *dsp_bsign = nullptr, *dsp_asel = nullptr, *dsp_bsel = nullptr,
78 *dsp_ce = nullptr, *dsp_clk = nullptr, *dsp_reset = nullptr;
79 const NetInfo *dsp_5a_clk0 = nullptr, *dsp_5a_clk1 = nullptr, *dsp_5a_ce0 = nullptr, *dsp_5a_ce1 = nullptr,
80 *dsp_5a_reset0 = nullptr, *dsp_5a_reset1 = nullptr;
81 bool dsp_soa_reg;
82 };
83 std::vector<GowinCellInfo> fast_cell_info;
84 void assign_cell_info();
85
86 // If there is an unused LUT adjacent to FF, use it
87 void create_passthrough_luts(void);
88
89 // Remember HCLK sections that have been reserved to route HCLK signals
90 std::set<BelId> routing_reserved_hclk_sections;
91
92 // dsp control nets
93 // Each DSP and each macro has a small set of control wires that are
94 // allocated to internal primitives as needed. It is assumed that most
95 // primitives use the same signals for CE, CLK and especially RESET, so
96 // these wires are few and need to be controlled.
97 struct dsp_info
98 {
99 dict<IdString, int> ce; // CE nets counter
100 dict<IdString, int> clk; // CLK nets counter
101 dict<IdString, int> reset; // RESET nets counter
102 int mode9bit; // 9 bit elements counter
103 int mode18bit; // 18 bit elements counter
104 };
105 dict<BelId, dsp_info> dsp_info;
106 dict<BelId, CellInfo *> dsp_bel2cell; // Remember the connection with cells
107 // since this information is already lost during unbinding
108 void adjust_dsp_pin_mapping(void);
109
110 // Place explicityl constrained or implicitly constrained (by IOLOGIC) CLKDIV and CLKDIV2 cells
111 // to avoid routing conflicts and maximize utilization
112 void place_constrained_hclk_cells();
113
114 // bel placement validation
115 bool slice_valid(int x, int y, int z) const;
116 bool dsp_valid(Loc l, IdString bel_type, bool explain_invalid) const;
117 bool hclk_valid(BelId bel, IdString bel_type) const;
118
119 array2d<std::vector<CellInfo *>> fast_logic_cell;
120 };
121
122 struct GowinArch : HimbaechelArch
123 {
124 GowinArch() : HimbaechelArch("gowin") {};
125
126 bool match_device(const std::string &device) override { return device.size() > 2 && device.substr(0, 2) == "GW"; }
127
128 std::unique_ptr<HimbaechelAPI> create(const std::string &device) override { return std::make_unique<GowinImpl>(); }
129 } gowinArch;
130
131 po::options_description GowinImpl::getUArchOptions()
132 {
133 po::options_description specific("Gowin specific options");
134 specific.add_options()("family", po::value<std::string>(), "GOWIN chip family");
135 specific.add_options()("cst", po::value<std::string>(), "name of constraints file");
136 specific.add_options()("ireg_in_iob", "place input registers in IOB");
137 specific.add_options()("oreg_in_iob", "place output registers in IOB");
138 specific.add_options()("ioreg_in_iob", "place I/O registers in IOB");
139 specific.add_options()("disable_gp_clock_routing", "disable clock network routing from GP pins");
140 specific.add_options()("sspi_as_gpio", "use SSPI pins as GPIO");
141 specific.add_options()("i2c_as_gpio", "use I2C pins as GPIO");
142 return specific;
143 }
144
145 void GowinImpl::init_database(Arch *arch)
146 {
147 init_uarch_constids(arch);
148 const ArchArgs &args = arch->args;
149 std::string family;
150 if (args.options.count("family")) {
151 family = args.options["family"].as<std::string>();
152 } else {
153 bool GW2 = args.device.rfind("GW2A", 0) == 0;
154 if (GW2) {
155 log_error("For the GW2A series you need to specify --vopt family=GW2A-18 or --vopt family=GW2A-18C\n");
156 } else {
157 std::regex devicere = std::regex("GW5A(T|ST)?-LV(25|60|138)[A-Z]*.*");
158 std::smatch match;
159 if (std::regex_match(args.device, match, devicere)) {
160 family = stringf("GW5A%s-%s%s", match[1].str().c_str(), match[2].str().c_str(),
161 match[2].str() == "25" ? "A" : "C");
162 } else {
163 std::regex devicere = std::regex("GW1N([SZ]?)[A-Z]*-(LV|UV|UX)([0-9])(C?).*");
164 std::smatch match;
165 if (!std::regex_match(args.device, match, devicere)) {
166 log_error("Invalid device %s\n", args.device.c_str());
167 }
168 family = stringf("GW1N%s-%s", match[1].str().c_str(), match[3].str().c_str());
169 if (family.rfind("GW1N-9", 0) == 0) {
170 log_error("For the GW1N-9 series you need to specify --vopt family=GW1N-9 or --vopt "
171 "family=GW1N-9C\n");
172 }
173 }
174 }
175 }
176
177 arch->load_chipdb(stringf("gowin/chipdb-%s.bin", family.c_str()));
178
179 // These fields go in the header of the output JSON file and can help
180 // gowin_pack support different architectures
181 arch->settings[arch->id("packer.arch")] = std::string("himbaechel/gowin");
182 arch->settings[arch->id("packer.chipdb")] = family;
183
184 chip = arch->id(family);
185 std::string pn = args.device;
186 partno = arch->id(pn);
187 arch->settings[arch->id("packer.partno")] = pn;
188 }
189
190 void GowinImpl::init(Context *ctx)
191 {
192 h.init(ctx);
193 HimbaechelAPI::init(ctx);
194
195 gwu.init(ctx);
196
197 const ArchArgs &args = ctx->args;
198
199 // package and speed class
200 std::regex speedre = std::regex("(.*)(C[0-9]/I[0-9])$");
201 std::smatch match;
202
203 IdString spd;
204 IdString package_idx;
205 std::string pn = args.device;
206 if (std::regex_match(pn, match, speedre)) {
207 package_idx = ctx->id(match[1]);
208 spd = ctx->id(match[2]);
209 ctx->set_speed_grade(match[2]);
210 } else {
211 if (pn.length() > 2 && pn.compare(pn.length() - 3, 2, "ES")) {
212 package_idx = ctx->id(pn.substr(0, pn.length() - 2));
213 spd = ctx->id("ES");
214 ctx->set_speed_grade("ES");
215 }
216 }
217
218 // log_info("search for %s, packages:%ld\n", package_idx.c_str(ctx), ctx->chip_info->packages.ssize());
219 for (int i = 0; i < ctx->chip_info->packages.ssize(); ++i) {
220 // log_info("i:%d %s\n", i, IdString(ctx->chip_info->packages[i].name).c_str(ctx));
221 if (IdString(ctx->chip_info->packages[i].name) == package_idx) {
222 ctx->package_info = &ctx->chip_info->packages[i];
223 break;
224 }
225 }
226 if (ctx->package_info == nullptr) {
227 log_error("No package for partnumber %s\n", partno.c_str(ctx));
228 }
229
230 // constraints
231 if (args.options.count("cst")) {
232 ctx->settings[ctx->id("cst.filename")] = args.options["cst"].as<std::string>();
233 }
234
235 // place registers in IO blocks
236 if (args.options.count("ireg_in_iob")) {
237 ctx->settings[id_IREG_IN_IOB] = Property(1);
238 }
239 if (args.options.count("oreg_in_iob")) {
240 ctx->settings[id_OREG_IN_IOB] = Property(1);
241 }
242 if (args.options.count("ioreg_in_iob")) {
243 ctx->settings[id_IOREG_IN_IOB] = Property(1);
244 }
245
246 // smart clock routing
247 if (args.options.count("disable_gp_clock_routing")) {
248 ctx->settings[id_NO_GP_CLOCK_ROUTING] = Property(1);
249 }
250 }
251
252 // We do not allow the use of global wires that bypass a special router.
253 bool GowinImpl::checkPipAvail(PipId pip) const
254 {
255 return (ctx->getWireConstantValue(ctx->getPipSrcWire(pip)) != IdString()) ||
256 (!(gwu.is_global_pip(pip) || gwu.is_segment_pip(pip)));
257 }
258
259 bool GowinImpl::checkPipAvailForNet(PipId pip, const NetInfo *net) const
260 {
261 return (net->constant_value == IdString() && ctx->getWireConstantValue(ctx->getPipSrcWire(pip)) != IdString()) ||
262 (!(gwu.is_global_pip(pip) || gwu.is_segment_pip(pip)));
263 }
264
265 void GowinImpl::pack()
266 {
267 if (ctx->settings.count(ctx->id("cst.filename"))) {
268 std::string filename = ctx->settings[ctx->id("cst.filename")].as_string();
269 std::ifstream in(filename);
270 if (!in) {
271 log_error("failed to open CST file '%s'\n", filename.c_str());
272 }
273 if (!gowin_apply_constraints(ctx, in)) {
274 log_error("failed to parse CST file '%s'\n", filename.c_str());
275 }
276 }
277 gowin_pack(ctx);
278 }
279
280 // One DSP macro, in a rough approximation, consists of 5 large operating
281 // blocks (pre-adders, multipliers and alu), at almost every input (blocks
282 // usually have two of them) you can turn on registers, in addition, there are
283 // registers on a dedicated operand shift line between DSP and registers at
284 // the outputs. As we see, the number of registers is large, but the DSP has
285 // only four inputs for each of the CE, CLK and RESET signals, and here we tell
286 // gowin_pack which version of each signal is used by which block.
287 // We also indicate to the router which Bel's pin to use.
288 void GowinImpl::adjust_dsp_pin_mapping(void)
289 {
290 if (gwu.has_5A_DSP()) {
291 return;
292 }
293 for (auto b2c : dsp_bel2cell) {
294 BelId bel = b2c.first;
295 Loc loc = ctx->getBelLocation(bel);
296 CellInfo *ci = b2c.second;
297 const auto dsp_data = fast_cell_info.at(ci->flat_index);
298
299 auto set_cell_bel_pin = [&](dict<IdString, int> nets, IdString pin, IdString net_name, const char *fmt,
300 const char *fmt_double = nullptr) {
301 int i = 0;
302 for (auto net_cnt : nets) {
303 if (net_cnt.first == net_name) {
304 break;
305 }
306 ++i;
307 }
308 ci->cell_bel_pins.at(pin).clear();
309 if (fmt_double == nullptr) {
310 ci->cell_bel_pins.at(pin).push_back(ctx->idf(fmt, i));
311 } else {
312 ci->cell_bel_pins.at(pin).push_back(ctx->idf(fmt_double, i, 0));
313 ci->cell_bel_pins.at(pin).push_back(ctx->idf(fmt_double, i, 1));
314 }
315 ci->setAttr(pin, i);
316 };
317
318 if (dsp_data.dsp_reset != nullptr) {
319 BelId dsp = ctx->getBelByLocation(Loc(loc.x, loc.y, BelZ::DSP_Z));
320 set_cell_bel_pin(dsp_info.at(dsp).reset, id_RESET, dsp_data.dsp_reset->name, "RESET%d",
321 ci->type == id_MULT36X36 ? "RESET%d%d" : nullptr);
322 }
323 if (dsp_data.dsp_ce != nullptr) {
324 BelId dsp = ctx->getBelByLocation(Loc(loc.x, loc.y, gwu.get_dsp_macro(loc.z)));
325 set_cell_bel_pin(dsp_info.at(dsp).ce, id_CE, dsp_data.dsp_ce->name, "CE%d",
326 ci->type == id_MULT36X36 ? "CE%d%d" : nullptr);
327 }
328 if (dsp_data.dsp_clk != nullptr) {
329 BelId dsp = ctx->getBelByLocation(Loc(loc.x, loc.y, gwu.get_dsp_macro(loc.z)));
330 set_cell_bel_pin(dsp_info.at(dsp).clk, id_CLK, dsp_data.dsp_clk->name, "CLK%d",
331 ci->type == id_MULT36X36 ? "CLK%d%d" : nullptr);
332 }
333 }
334 }
335
336 /*
337 Each HCLK section can serve one of three purposes:
338 1. A simple routing path to IOLOGIC FCLK or PLL inputs
339 2. CLKDIV2
340 3. CLKDIV (only one section at any time)
341
342 Our task is to distribute HCLK signal providers to sections in a way that maximizes utilization while
343 enforcing user constraints on CLKDIV placement. We achieve this by solving two bipartite matchings:
344 - The first determines the best HCLK to place a CLKDIV within the established graph. This is then refined
345 to determine what section to assign the CLKDIV to based on what IOLOGIC it connects to
346 - The second determines which HCLK sections to use as CLKDIV2 or to reserve for routing.
347 */
348 void GowinImpl::place_constrained_hclk_cells()
349 {
350 log_info("Running custom HCLK placer...\n");
351 std::map<IdStringList, IdString> constrained_clkdivs;
352 std::map<BelId, std::set<std::pair<IdString, int>>> bel_cell_map;
353 std::vector<std::pair<IdString, int>> alias_cells;
354 std::map<std::pair<IdString, int>, BelId> final_placement;
355
356 const bool chip_has_clkdiv_hclk_connection = gwu.has_CLKDIV_HCLK();
357 const bool chip_has_pll_hclk = gwu.has_PLL_HCLK();
358 pool<BelId> free_pll_bels;
359 if (chip_has_pll_hclk) {
360 // gather free PLL bels
361 for (auto bel : ctx->getBels()) {
362 if (ctx->getBelType(bel).in(id_rPLL, id_PLLVR) && !ctx->getBoundBelCell(bel)) {
363 free_pll_bels.insert(bel);
364 }
365 }
366 }
367
368 const auto is_hclk_user = [&](const CellInfo *ci) -> bool {
369 if ((is_iologici(ci) || is_iologico(ci)) &&
370 !ci->type.in(id_ODDR, id_ODDRC, id_IDDR, id_IDDRC, id_IOLOGICI_EMPTY, id_IOLOGICO_EMPTY)) {
371 return true;
372 }
373 if (chip_has_pll_hclk && is_pll(ci)) {
374 return true;
375 }
376 return false;
377 };
378
379 // returns the list of networks connected to the cell
380 std::vector<const NetInfo *> net_list;
381 auto get_nets = [&](const CellInfo *ci) -> void {
382 net_list.clear();
383 if (is_iologici(ci) || is_iologico(ci)) {
384 net_list.push_back(ci->getPort(id_FCLK));
385 return;
386 }
387 if (chip_has_pll_hclk && is_pll(ci)) {
388 net_list.push_back(ci->getPort(id_CLKIN));
389 net_list.push_back(ci->getPort(id_CLKFB));
390 return;
391 }
392 };
393
394 // cell, port
395 std::set<IdString> seen_hclk_users;
396 for (auto &cell : ctx->cells) {
397 auto ci = cell.second.get();
398
399 if (is_clkdiv(ci) && ci->attrs.count(id_BEL)) {
400 BelId constrained_bel = ctx->getBelByName(IdStringList::parse(ctx, ci->attrs.at(id_BEL).as_string()));
401 NPNR_ASSERT(constrained_bel != BelId() && ctx->getBelType(constrained_bel) == id_CLKDIV);
402 auto hclk_id_loc = gwu.get_hclk_id(constrained_bel);
403 constrained_clkdivs[hclk_id_loc] = ci->name;
404 }
405
406 if ((seen_hclk_users.find(ci->name) != seen_hclk_users.end()))
407 continue;
408
409 if (is_hclk_user(ci)) {
410 get_nets(ci);
411 for (auto &hclk_net : net_list) {
412 if (!hclk_net) {
413 continue;
414 }
415 CellInfo *hclk_driver = hclk_net->driver.cell;
416 if (!hclk_driver)
417 continue;
418 if (!(chip_has_clkdiv_hclk_connection || hclk_driver->type == id_CLKDIV2)) {
419 continue;
420 }
421
422 int alias_count = 0;
423 std::set<std::set<BelId>> seen_options;
424 for (auto user : hclk_net->users) {
425 std::vector<BelId> bel_candidates;
426 std::set<BelId> these_options;
427
428 if (!is_hclk_user(user.cell)) {
429 continue;
430 }
431 seen_hclk_users.insert(user.cell->name);
432
433 if (ctx->debug) {
434 log_info("Custom HCLK Placer: Found HCLK user: %s\n", user.cell->name.c_str(ctx));
435 }
436
437 if (is_pll(user.cell) && user.cell->bel == BelId()) {
438 if (free_pll_bels.empty()) {
439 log_error("No BELs for %s\n", ctx->nameOf(user.cell));
440 }
441 ctx->bindBel(free_pll_bels.pop(), user.cell, PlaceStrength::STRENGTH_LOCKED);
442 }
443
444 gwu.find_connected_bels(user.cell, user.port, id_CLKDIV2, id_CLKOUT, 1000000, bel_candidates);
445 these_options.insert(bel_candidates.begin(), bel_candidates.end());
446
447 if (seen_options.find(these_options) != seen_options.end())
448 continue;
449 seen_options.insert(these_options);
450
451 // When an HCLK signal is routed to different (and disconnected) FCLKs, we treat each new
452 // HCLK-FCLK connection as a pseudo-HCLK cell since it must also be assigned an HCLK section
453 auto alias_index = std::pair<IdString, int>(hclk_driver->name, alias_count);
454 alias_cells.push_back(alias_index);
455 alias_count++;
456
457 for (auto option : these_options) {
458 bel_cell_map[option].insert(alias_index);
459 }
460 }
461 }
462 }
463 }
464
465 // First matching. We use the upper CLKDIV2 as the ID for an HCLK
466 std::map<IdStringList, std::set<IdString>> clkdiv_graph;
467 for (auto bel_cell_candidates : bel_cell_map) {
468 auto bel = bel_cell_candidates.first;
469 auto hclk_id_loc = gwu.get_hclk_id(bel);
470 if (constrained_clkdivs.find(hclk_id_loc) != constrained_clkdivs.end()) {
471 continue;
472 }
473 for (auto candidate : bel_cell_candidates.second) {
474 auto ci = ctx->cells.at(candidate.first).get();
475 if ((ci->type != id_CLKDIV) || ci->attrs.count(id_BEL)) {
476 continue;
477 }
478 clkdiv_graph[hclk_id_loc].insert(candidate.first);
479 }
480 }
481
482 if (ctx->debug) {
483 log_info("<-----CUSTOM HCLK PLACER: Constrained CLKDIVs----->\n");
484 for (auto match_pair : constrained_clkdivs) {
485 log_info("%s cell <-----> CLKDIV at HCLK %s\n", match_pair.second.c_str(ctx), match_pair.second.c_str(ctx));
486 }
487 log("\n");
488 }
489
490 auto matching = gwu.find_maximum_bipartite_matching<IdStringList, IdString>(
491 clkdiv_graph); // these will serve as constraints
492 constrained_clkdivs.insert(matching.begin(), matching.end());
493
494 if (ctx->debug) {
495 log_info("<-----CUSTOM HCLK PLACER: First Matching(CLKDIV) Results----->\n");
496 for (auto match_pair : matching) {
497 log_info("%s cell <-----> CLKDIV at HCLK %s\n", match_pair.second.c_str(ctx), match_pair.second.c_str(ctx));
498 }
499 log("\n");
500 }
501
502 // Refine matching to HCLK section, based on what connections actually exist
503 std::map<IdString, std::pair<IdString, int>> true_clkdivs;
504 std::set<BelId> used_bels;
505 for (auto constr_pair : constrained_clkdivs) {
506 BelId option0 = ctx->getBelByName(constr_pair.first);
507 BelId option1 = gwu.get_other_hclk_clkdiv2(option0);
508
509 // On the GW1N-9 devices, only the lower CLKDIV can be fed by a CLKDIV2
510 std::vector<BelId> options = {option1, option0};
511 if (chip.str(ctx) == "GW1N-9C") {
512 auto ci = ctx->cells.at(constr_pair.second).get();
513 for (auto cluster_child_cell : ci->constr_children)
514 if (cluster_child_cell->type == id_CLKDIV2 && options.back() == option0) {
515 options.pop_back();
516 break;
517 }
518 }
519
520 bool placed = false;
521 for (auto option : options) {
522 if (placed || (used_bels.find(option) != used_bels.end()))
523 continue;
524 for (auto option_cell : bel_cell_map[option]) {
525 if ((option_cell.first != constr_pair.second) ||
526 (true_clkdivs.find(option_cell.first) != true_clkdivs.end()))
527 continue;
528 final_placement[option_cell] = option;
529 true_clkdivs[option_cell.first] = option_cell;
530 used_bels.insert(option);
531 placed = true;
532 break;
533 }
534 }
535 // This must be a constrained CLKDIV that either does not serve IOLOGIC Or
536 // does not have a direct (HCLK-FCLK) connection IOLOGIC it serves
537 // We create a new alias to represent this
538 if (!placed) {
539 auto new_alias = std::pair<IdString, int>(constr_pair.second, -1);
540 for (auto option : options)
541 bel_cell_map[option].insert(new_alias);
542 alias_cells.push_back(new_alias);
543 true_clkdivs[constr_pair.second] = new_alias;
544 }
545 }
546
547 // Second Matching for CLKDIV2 and routing reservation
548 std::map<IdStringList, std::set<std::pair<IdString, int>>> full_hclk_graph;
549 for (auto bel_cell_candidates : bel_cell_map) {
550 auto bel = bel_cell_candidates.first;
551 auto bel_name = ctx->getBelName(bel);
552 if (!used_bels.count(bel)) {
553 for (auto candidate : bel_cell_candidates.second) {
554 if (((candidate.second == -1) || (!true_clkdivs.count(candidate.first)) ||
555 !(true_clkdivs[candidate.first] == candidate))) {
556 full_hclk_graph[bel_name].insert(candidate);
557 }
558 }
559 }
560 }
561
562 auto full_matching = gwu.find_maximum_bipartite_matching(full_hclk_graph);
563 for (auto belname_cellalias : full_matching) {
564 auto bel = ctx->getBelByName(belname_cellalias.first);
565 NPNR_ASSERT(!used_bels.count(bel));
566 final_placement[belname_cellalias.second] = bel;
567 }
568
569 if (ctx->debug) {
570 log_info("<-----CUSTOM HCLK PLACER: Second Matching(CLKDIV2 and Routing) Results------>\n");
571 for (auto match_pair : full_matching) {
572 auto alias = match_pair.second;
573 auto bel = match_pair.first;
574 auto cell_type = ctx->cells.at(alias.first).get()->type;
575 log_info("%s cell %s Alias %d <-----> HCLK Section at %s\n", cell_type.c_str(ctx), alias.first.c_str(ctx),
576 alias.second, bel.str(ctx).c_str());
577 }
578 log("\n");
579 }
580
581 for (auto cell_alias : alias_cells) {
582 auto ci = ctx->cells.at(cell_alias.first).get();
583
584 if (final_placement.find(cell_alias) == final_placement.end() && ctx->debug)
585 if (ci->type == id_CLKDIV2 || ci->type == id_CLKDIV)
586 log_info("Custom HCLK Placer: Unable to place HCLK cell %s; no BELs available to implement cell type "
587 "%s\n",
588 ci->name.c_str(ctx), ci->type.c_str(ctx));
589 else
590 log_info("Custom HCLK Placer: Unable to guarantee route for HCLK signal from %s to IOLOGIC\n",
591 ci->name.c_str(ctx));
592
593 else {
594 auto placement = final_placement[cell_alias];
595 if (ctx->debug)
596 log_info("Custom HCLK Placer: Placing %s Alias %d at %s\n", cell_alias.first.c_str(ctx),
597 cell_alias.second, ctx->nameOfBel(placement));
598 if (ci->type == id_CLKDIV2)
599 ctx->bindBel(placement, ci, STRENGTH_LOCKED);
600
601 else if ((ci->type == id_CLKDIV) && (true_clkdivs[cell_alias.first] == cell_alias)) {
602 NetInfo *in = ci->getPort(id_HCLKIN);
603 if (in && in->driver.cell->type == id_CLKDIV2) {
604 ctx->bindBel(placement, in->driver.cell, STRENGTH_LOCKED);
605 }
606 auto clkdiv_bel = gwu.get_clkdiv_for_clkdiv2(placement);
607 ctx->bindBel(clkdiv_bel, ci, STRENGTH_LOCKED);
608 } else {
609 if (ctx->debug)
610 log_info("Custom HCLK Placer: Reserving HCLK %s to route clock from %s\n",
611 ctx->nameOfBel(placement), ci->name.c_str(ctx));
612 routing_reserved_hclk_sections.insert(placement);
613 }
614 }
615 if (ci->attrs.count(id_BEL))
616 ci->unsetAttr(id_BEL);
617 }
618 }
619
620 void GowinImpl::prePlace()
621 {
622 place_constrained_hclk_cells();
623 assign_cell_info();
624 fast_logic_cell.reset(ctx->getGridDimX(), ctx->getGridDimY());
625 for (auto bel : ctx->getBels()) {
626 if (ctx->getBelType(bel) == id_LUT4) {
627 Loc loc = ctx->getBelLocation(bel);
628 fast_logic_cell.at(loc.x, loc.y).resize(37);
629 }
630 }
631 }
632
633 void GowinImpl::postPlace()
634 {
635 gwu.has_SP32();
636 if (ctx->debug) {
637 log_info("================== Final Placement ===================\n");
638 for (auto &cell : ctx->cells) {
639 auto ci = cell.second.get();
640 if (ci->bel != BelId()) {
641 log_info("%s: %s\n", ctx->nameOfBel(ci->bel), ctx->nameOf(ci));
642 } else {
643 log_info("unknown: %s\n", ctx->nameOf(ci));
644 }
645 }
646 log_break();
647 }
648
649 // adjust cell pin to bel pin mapping for DSP cells (CE, CLK and RESET pins)
650 adjust_dsp_pin_mapping();
651 create_passthrough_luts();
652 }
653
654 void GowinImpl::preRoute() { gowin_route_globals(ctx); }
655
656 void GowinImpl::postRoute()
657 {
658 std::set<IdString> visited_hclk_users;
659
660 for (auto &cell : ctx->cells) {
661 auto ci = cell.second.get();
662 if (ci->type.in(id_IOLOGICI, id_IOLOGICO, id_IOLOGIC) ||
663 ((is_iologici(ci) || is_iologico(ci)) && !ci->type.in(id_ODDR, id_ODDRC, id_IDDR, id_IDDRC))) {
664 if (visited_hclk_users.find(ci->name) == visited_hclk_users.end()) {
665 // mark FCLK<-HCLK connections
666 const NetInfo *h_net = ci->getPort(id_FCLK);
667 if (h_net) {
668 for (auto &user : h_net->users) {
669 if (user.port != id_FCLK) {
670 continue;
671 }
672 user.cell->setAttr(id_IOLOGIC_FCLK, Property("UNKNOWN"));
673 visited_hclk_users.insert(user.cell->name);
674 PipId up_pip = h_net->wires.at(ctx->getNetinfoSinkWire(h_net, user, 0)).pip;
675 IdString up_wire_name = ctx->getWireName(ctx->getPipSrcWire(up_pip))[1];
676 if (up_wire_name.in(id_HCLK_OUT0, id_HCLK_OUT1, id_HCLK_OUT2, id_HCLK_OUT3)) {
677 user.cell->setAttr(id_IOLOGIC_FCLK, Property(up_wire_name.str(ctx)));
678 if (ctx->debug) {
679 log_info("set IOLOGIC_FCLK to %s\n", up_wire_name.c_str(ctx));
680 }
681 }
682 if (ctx->debug) {
683 log_info("HCLK user cell:%s, port:%s, wire:%s, pip:%s, up wire:%s\n",
684 ctx->nameOf(user.cell), user.port.c_str(ctx),
685 ctx->nameOfWire(ctx->getNetinfoSinkWire(h_net, user, 0)), ctx->nameOfPip(up_pip),
686 ctx->nameOfWire(ctx->getPipSrcWire(up_pip)));
687 }
688 }
689 }
690 }
691 } else {
692 if (is_pll(ci)) {
693 // CLKIN is connected to HLCK?
694 NetInfo *h_net = ci->getPort(id_CLKIN);
695 if (h_net == nullptr || h_net->wires.empty()) {
696 continue;
697 }
698 PortRef pr = {ci, id_CLKIN};
699 PipId up_pip = h_net->wires.at(ctx->getNetinfoSinkWire(h_net, pr, 0)).pip;
700 IdString up_wire_name = ctx->getWireName(ctx->getPipSrcWire(up_pip))[1];
701 if (up_wire_name.in(id_HCLK_OUT0, id_HCLK_OUT1)) {
702 ci->setParam(id_INSEL, Property("CLKIN3"));
703 } else {
704 if (up_wire_name.in(id_HCLK_OUT2, id_HCLK_OUT3)) {
705 ci->setParam(id_INSEL, Property("CLKIN4"));
706 }
707 }
708 // CLKFB is connected to HLCK?
709 h_net = ci->getPort(id_CLKFB);
710 if (h_net == nullptr || h_net->wires.empty()) {
711 continue;
712 }
713 pr.port = id_CLKFB;
714 up_pip = h_net->wires.at(ctx->getNetinfoSinkWire(h_net, pr, 0)).pip;
715 up_wire_name = ctx->getWireName(ctx->getPipSrcWire(up_pip))[1];
716 if (up_wire_name.in(id_HCLK_OUT0, id_HCLK_OUT1)) {
717 ci->setParam(id_FBSEL, Property("CLKFB1"));
718 } else {
719 if (up_wire_name.in(id_HCLK_OUT2, id_HCLK_OUT3)) {
720 ci->setParam(id_FBSEL, Property("CLKFB4"));
721 }
722 }
723 }
724 }
725 }
726 std::vector<CellInfo *> to_remove;
727 for (auto &cell : ctx->cells) {
728 CellInfo *ci = cell.second.get();
729 if (ci->type.in(id_BLOCKER_LUT, id_BLOCKER_FF)) {
730 to_remove.push_back(ci);
731 }
732 }
733 for (auto ci : to_remove) {
734 auto root = ctx->cells.at(ci->cluster).get();
735 root->constr_children.erase(std::remove_if(root->constr_children.begin(), root->constr_children.end(),
736 [&](CellInfo *c) { return c == ci; }));
737 ctx->cells.erase(ci->name);
738 }
739 }
740
741 bool GowinImpl::isBelLocationValid(BelId bel, bool explain_invalid) const
742 {
743 Loc l = ctx->getBelLocation(bel);
744 IdString bel_type = ctx->getBelType(bel);
745 if (!ctx->getBoundBelCell(bel)) {
746 return true;
747 }
748 switch (bel_type.hash()) {
749 case ID_LUT4: /* fall-through */
750 case ID_DFF:
751 return slice_valid(l.x, l.y, l.z / 2);
752 case ID_ALU:
753 return slice_valid(l.x, l.y, l.z - BelZ::ALU0_Z);
754 case ID_RAM16SDP4:
755 return slice_valid(l.x, l.y, 0);
756 case ID_MUX2_LUT5:
757 return slice_valid(l.x, l.y, (l.z - BelZ::MUX20_Z) / 2);
758 case ID_MUX2_LUT6:
759 return slice_valid(l.x, l.y, (l.z - BelZ::MUX21_Z) / 2 + 1);
760 case ID_MUX2_LUT7:
761 return slice_valid(l.x, l.y, 3);
762 case ID_MUX2_LUT8:
763 return slice_valid(l.x, l.y, 7);
764 case ID_PADD9: /* fall-through */
765 case ID_PADD18: /* fall-through */
766 case ID_MULT9X9: /* fall-through */
767 case ID_MULT12X12: /* fall-through */
768 case ID_MULT18X18: /* fall-through */
769 case ID_MULTADDALU18X18: /* fall-through */
770 case ID_MULTALU18X18: /* fall-through */
771 case ID_MULTALU36X18: /* fall-through */
772 case ID_MULT36X36: /* fall-through */
773 case ID_ALU54D:
774 return dsp_valid(l, bel_type, explain_invalid);
775 case ID_CLKDIV2: /* fall-through */
776 case ID_CLKDIV:
777 return hclk_valid(bel, bel_type);
778 }
779 return true;
780 }
781
782 // Bel bucket functions
783 IdString GowinImpl::getBelBucketForCellType(IdString cell_type) const
784 {
785 if (cell_type.in(id_IBUF, id_OBUF)) {
786 return id_IOB;
787 }
788 if (cell_type.in(id_MIPI_OBUF, id_MIPI_OBUF_A)) {
789 return id_MIPI_OBUF;
790 }
791 if (type_is_lut(cell_type) || cell_type == id_BLOCKER_LUT) {
792 return id_LUT4;
793 }
794 if (type_is_dff(cell_type) || cell_type == id_BLOCKER_FF) {
795 return id_DFF;
796 }
797 if (type_is_ssram(cell_type)) {
798 return id_RAM16SDP4;
799 }
800 if (type_is_iologici(cell_type)) {
801 return id_IOLOGICI;
802 }
803 if (type_is_iologico(cell_type)) {
804 return id_IOLOGICO;
805 }
806 if (type_is_bsram(cell_type)) {
807 return id_BSRAM;
808 }
809 if (cell_type == id_GOWIN_GND) {
810 return id_GND;
811 }
812 if (cell_type == id_GOWIN_VCC) {
813 return id_VCC;
814 }
815 return cell_type;
816 }
817
818 bool GowinImpl::isValidBelForCellType(IdString cell_type, BelId bel) const
819 {
820 if (cell_type == id_DUMMY_CELL) {
821 return true;
822 }
823
824 IdString bel_type = ctx->getBelType(bel);
825 if (bel_type == id_IOB) {
826 return cell_type.in(id_IBUF, id_OBUF);
827 }
828 if (bel_type == id_MIPI_OBUF) {
829 return cell_type.in(id_MIPI_OBUF, id_MIPI_OBUF_A);
830 }
831 if (bel_type == id_LUT4) {
832 return type_is_lut(cell_type) || cell_type == id_BLOCKER_LUT;
833 }
834 if (bel_type == id_DFF) {
835 return type_is_dff(cell_type) || cell_type == id_BLOCKER_FF;
836 }
837 if (bel_type == id_RAM16SDP4) {
838 return type_is_ssram(cell_type);
839 }
840 if (bel_type == id_IOLOGICI) {
841 return type_is_iologici(cell_type);
842 }
843 if (bel_type == id_IOLOGICO) {
844 return type_is_iologico(cell_type);
845 }
846 if (bel_type == id_BSRAM) {
847 return type_is_bsram(cell_type);
848 }
849 if (bel_type == id_GND) {
850 return cell_type == id_GOWIN_GND;
851 }
852 if (bel_type == id_VCC) {
853 return cell_type == id_GOWIN_VCC;
854 }
855 return (bel_type == cell_type);
856 }
857
858 void GowinImpl::assign_cell_info()
859 {
860 fast_cell_info.resize(ctx->cells.size());
861 for (auto &cell : ctx->cells) {
862 CellInfo *ci = cell.second.get();
863 auto &fc = fast_cell_info.at(ci->flat_index);
864 if (is_lut(ci)) {
865 fc.lut_f = ci->getPort(id_F);
866 continue;
867 }
868 if (is_dff(ci)) {
869 fc.ff_d = ci->getPort(id_D);
870 fc.ff_clk = ci->getPort(id_CLK);
871 fc.ff_ce = ci->getPort(id_CE);
872 for (IdString port : {id_SET, id_RESET, id_PRESET, id_CLEAR}) {
873 fc.ff_lsr = ci->getPort(port);
874 if (fc.ff_lsr != nullptr) {
875 break;
876 }
877 }
878 continue;
879 }
880 if (is_alu(ci)) {
881 fc.alu_sum = ci->getPort(id_SUM);
882 continue;
883 }
884 auto get_net = [&](IdString port_id) {
885 NetInfo *ni = ci->getPort(port_id);
886 if (ni != nullptr && ni->driver.cell == nullptr) {
887 ni = nullptr;
888 }
889 return ni;
890 };
891 if (is_dsp(ci)) {
892 fc.dsp_reset = get_net(id_RESET);
893 fc.dsp_clk = get_net(id_CLK);
894 fc.dsp_ce = get_net(id_CE);
895 fc.dsp_asign = get_net(id_ASIGN);
896 fc.dsp_bsign = get_net(id_BSIGN);
897 fc.dsp_asel = get_net(id_ASEL);
898 fc.dsp_bsel = get_net(id_BSEL);
899 fc.dsp_soa_reg = ci->params.count(id_SOA_REG) && ci->params.at(id_SOA_REG).as_int64() == 1;
900 fc.dsp_5a_clk0 = get_net(id_CLK0);
901 fc.dsp_5a_clk1 = get_net(id_CLK1);
902 fc.dsp_5a_ce0 = get_net(id_CE0);
903 fc.dsp_5a_ce1 = get_net(id_CE1);
904 fc.dsp_5a_reset0 = get_net(id_RESET0);
905 fc.dsp_5a_reset1 = get_net(id_RESET1);
906 }
907 }
908 }
909
910 // If there is an unused LUT next to the DFF, use its inputs for the D input
911 void GowinImpl::create_passthrough_luts(void)
912 {
913 std::vector<std::unique_ptr<CellInfo>> new_cells;
914 for (auto &cell : ctx->cells) {
915 CellInfo *ci = cell.second.get();
916 if (is_dff(ci)) {
917 Loc loc = ctx->getBelLocation(ci->bel);
918 BelId lut_bel = ctx->getBelByLocation(Loc(loc.x, loc.y, loc.z - 1));
919 CellInfo *lut = ctx->getBoundBelCell(lut_bel);
920 CellInfo *alu = ctx->getBoundBelCell(ctx->getBelByLocation(Loc(loc.x, loc.y, loc.z / 2 + BelZ::ALU0_Z)));
921 const CellInfo *ramw = ctx->getBoundBelCell(ctx->getBelByLocation(Loc(loc.x, loc.y, BelZ::RAMW_Z)));
922
923 if (!(lut || alu || ramw)) {
924 if (ctx->debug) {
925 log_info("Found an unused LUT:%s, ", ctx->nameOfBel(lut_bel));
926 }
927 // make LUT
928 auto lut_cell = gwu.create_cell(gwu.create_aux_name(ci->name, 0, "_passthrough_lut$"), id_LUT4);
929 CellInfo *lut = lut_cell.get();
930 NetInfo *d_net = ci->getPort(id_D);
931 NPNR_ASSERT(d_net != nullptr);
932
933 if (d_net->name == ctx->id("$PACKER_GND") || d_net->name == ctx->id("$PACKER_VCC")) {
934 if (ctx->debug) {
935 log_info("make a constant %s.\n", d_net->name == ctx->id("$PACKER_VCC") ? "VCC" : "GND");
936 }
937 ci->disconnectPort(id_D);
938 if (d_net->name == ctx->id("$PACKER_GND")) {
939 lut->setParam(id_INIT, 0x0000);
940 } else {
941 lut->setParam(id_INIT, 0xffff);
942 }
943 } else {
944 if (ctx->debug) {
945 log_info("make a pass-through.\n");
946 }
947 IdString lut_input = id_I3;
948 int lut_init = 0xff00;
949
950 lut->addInput(lut_input);
951 lut->cell_bel_pins[lut_input].clear();
952 lut->cell_bel_pins.at(lut_input).push_back(lut_input);
953 ci->movePortTo(id_D, lut, lut_input);
954 lut->setParam(id_INIT, lut_init);
955 }
956 lut->addOutput(id_F);
957 lut->cell_bel_pins[id_F].clear();
958 lut->cell_bel_pins.at(id_F).push_back(id_F);
959 ci->connectPorts(id_D, lut, id_F);
960
961 ctx->bindBel(lut_bel, lut, PlaceStrength::STRENGTH_LOCKED);
962 new_cells.push_back(std::move(lut_cell));
963 }
964 }
965 }
966 for (auto &cell : new_cells) {
967 ctx->cells[cell->name] = std::move(cell);
968 }
969 }
970
971 // DFFs must be same type or compatible
972 inline bool incompatible_ffs(const CellInfo *ff, const CellInfo *adj_ff)
973 {
974 return ff->type != adj_ff->type &&
975 ((ff->type == id_DFFS && adj_ff->type != id_DFFR) || (ff->type == id_DFFR && adj_ff->type != id_DFFS) ||
976 (ff->type == id_DFFSE && adj_ff->type != id_DFFRE) || (ff->type == id_DFFRE && adj_ff->type != id_DFFSE) ||
977 (ff->type == id_DFFP && adj_ff->type != id_DFFC) || (ff->type == id_DFFC && adj_ff->type != id_DFFP) ||
978 (ff->type == id_DFFPE && adj_ff->type != id_DFFCE) || (ff->type == id_DFFCE && adj_ff->type != id_DFFPE) ||
979 (ff->type == id_DFFNS && adj_ff->type != id_DFFNR) || (ff->type == id_DFFNR && adj_ff->type != id_DFFNS) ||
980 (ff->type == id_DFFNSE && adj_ff->type != id_DFFNRE) ||
981 (ff->type == id_DFFNRE && adj_ff->type != id_DFFNSE) ||
982 (ff->type == id_DFFNP && adj_ff->type != id_DFFNC) || (ff->type == id_DFFNC && adj_ff->type != id_DFFNP) ||
983 (ff->type == id_DFFNPE && adj_ff->type != id_DFFNCE) ||
984 (ff->type == id_DFFNCE && adj_ff->type != id_DFFNPE) || (ff->type == id_DFF && adj_ff->type != id_DFF) ||
985 (ff->type == id_DFFE && adj_ff->type != id_DFFE) || (ff->type == id_DFFN && adj_ff->type != id_DFFN) ||
986 (ff->type == id_DFFNE && adj_ff->type != id_DFFNE));
987 }
988
989 // placement validation
990 bool GowinImpl::dsp_valid(Loc l, IdString bel_type, bool explain_invalid) const
991 {
992 const CellInfo *dsp = ctx->getBoundBelCell(ctx->getBelByLocation(l));
993 const auto &dsp_data = fast_cell_info.at(dsp->flat_index);
994
995 BelId dsp_macro_bel = ctx->getBelByLocation(Loc(l.x, l.y, gwu.get_dsp_macro(l.z)));
996 if (dsp_info.count(dsp_macro_bel)) {
997 if (dsp_info.at(dsp_macro_bel).mode9bit && dsp_info.at(dsp_macro_bel).mode18bit) {
998 if (explain_invalid) {
999 log_nonfatal_error("Different operand lengths (9 and 18) are not permitted in one DSP macro.\n");
1000 }
1001 return false;
1002 }
1003 }
1004
1005 // check for shift out register - there is only one for macro
1006 if (dsp_data.dsp_soa_reg) {
1007 if (l.z == BelZ::MULT18X18_0_1_Z || l.z == BelZ::MULT18X18_1_1_Z || l.z == BelZ::MULT9X9_0_0_Z ||
1008 l.z == BelZ::MULT9X9_0_1_Z || l.z == BelZ::MULT9X9_1_0_Z || l.z == BelZ::MULT9X9_1_1_Z) {
1009 if (explain_invalid) {
1010 log_nonfatal_error(
1011 "It is not possible to place the DSP so that the SOA register is on the macro boundary.\n");
1012 }
1013 return false;
1014 }
1015 }
1016
1017 if (bel_type.in(id_MULT9X9, id_PADD9)) {
1018 int pair_z = gwu.get_dsp_paired_9(l.z);
1019 const CellInfo *adj_dsp9 = ctx->getBoundBelCell(ctx->getBelByLocation(Loc(l.x, l.y, pair_z)));
1020 if (adj_dsp9 != nullptr) {
1021 const auto &adj_dsp9_data = fast_cell_info.at(adj_dsp9->flat_index);
1022 if ((dsp_data.dsp_asign != adj_dsp9_data.dsp_asign) || (dsp_data.dsp_bsign != adj_dsp9_data.dsp_bsign) ||
1023 (dsp_data.dsp_asel != adj_dsp9_data.dsp_asel) || (dsp_data.dsp_bsel != adj_dsp9_data.dsp_bsel) ||
1024 (dsp_data.dsp_reset != adj_dsp9_data.dsp_reset) || (dsp_data.dsp_ce != adj_dsp9_data.dsp_ce) ||
1025 (dsp_data.dsp_clk != adj_dsp9_data.dsp_clk)) {
1026 if (explain_invalid) {
1027 log_nonfatal_error("For 9bit primitives the control signals must be same.\n");
1028 }
1029 return false;
1030 }
1031 }
1032 }
1033
1034 if (bel_type == id_MULT12X12) {
1035 int pair_z = gwu.get_dsp_paired_12(l.z);
1036 const CellInfo *adj_dsp12 = ctx->getBoundBelCell(ctx->getBelByLocation(Loc(l.x, l.y, pair_z)));
1037 if (adj_dsp12 != nullptr) {
1038 const auto &adj_dsp12_data = fast_cell_info.at(adj_dsp12->flat_index);
1039 if ((dsp_data.dsp_5a_clk0 != adj_dsp12_data.dsp_5a_clk0) ||
1040 (dsp_data.dsp_5a_clk1 != adj_dsp12_data.dsp_5a_clk1) ||
1041 (dsp_data.dsp_5a_ce0 != adj_dsp12_data.dsp_5a_ce0) ||
1042 (dsp_data.dsp_5a_ce1 != adj_dsp12_data.dsp_5a_ce1) ||
1043 (dsp_data.dsp_5a_reset0 != adj_dsp12_data.dsp_5a_reset0) ||
1044 (dsp_data.dsp_5a_reset1 != adj_dsp12_data.dsp_5a_reset1)) {
1045 if (explain_invalid) {
1046 log_nonfatal_error("For MULT12X12 primitives the control signals must be same.\n");
1047 }
1048 return false;
1049 }
1050 }
1051 }
1052
1053 // check for control nets "overflow"
1054 BelId dsp_bel = ctx->getBelByLocation(Loc(l.x, l.y, BelZ::DSP_Z));
1055 if (dsp_info.count(dsp_bel)) {
1056 if (dsp_info.at(dsp_bel).reset.size() > 4) {
1057 if (explain_invalid) {
1058 log_nonfatal_error("More than 4 different networks for RESET signals in one DSP are not allowed.\n");
1059 }
1060 return false;
1061 }
1062 }
1063 if (dsp_info.count(dsp_macro_bel)) {
1064 if (dsp_info.at(dsp_macro_bel).ce.size() > 4 || dsp_info.at(dsp_macro_bel).clk.size() > 4) {
1065 if (explain_invalid) {
1066 log_nonfatal_error(
1067 "More than 4 different networks for CE or CLK signals in one DSP macro are not allowed.\n");
1068 }
1069 return false;
1070 }
1071 }
1072 return true;
1073 }
1074
1075 bool GowinImpl::slice_valid(int x, int y, int z) const
1076 {
1077 auto &bels = fast_logic_cell.at(x, y);
1078 const CellInfo *lut = bels.at(z * 2);
1079 const CellInfo *ff = bels.at(z * 2 + 1);
1080 // There are only 6 ALUs
1081 const CellInfo *alu = (z < 6) ? bels.at(z + BelZ::ALU0_Z) : nullptr;
1082 const CellInfo *ramw = bels.at(BelZ::RAMW_Z);
1083
1084 auto is_not_blocker = [](const CellInfo *ci) { return ci && !ci->type.in(id_BLOCKER_LUT, id_BLOCKER_FF); };
1085
1086 if (alu && lut && lut->type != id_BLOCKER_LUT) {
1087 return false;
1088 }
1089
1090 if (ramw) {
1091 // FFs in slices 4 and 5 are not allowed
1092 // also temporarily disallow FF to be placed near RAM
1093 if (is_not_blocker(bels.at(0 * 2 + 1)) || is_not_blocker(bels.at(1 * 2 + 1)) ||
1094 is_not_blocker(bels.at(2 * 2 + 1)) || is_not_blocker(bels.at(3 * 2 + 1)) ||
1095 is_not_blocker(bels.at(4 * 2 + 1)) || is_not_blocker(bels.at(5 * 2 + 1))) {
1096 return false;
1097 }
1098 if (gwu.has_DFF67()) {
1099 if (is_not_blocker(bels.at(6 * 2 + 1)) || is_not_blocker(bels.at(7 * 2 + 1))) {
1100 return false;
1101 }
1102 }
1103 // ALU/LUTs in slices 4, 5, 6, 7 are not allowed
1104 for (int i = 4; i < 8; ++i) {
1105 if (is_not_blocker(bels.at(i * 2))) {
1106 return false;
1107 }
1108 if (i < 6 && bels.at(i + BelZ::ALU0_Z)) {
1109 return false;
1110 }
1111 }
1112 }
1113
1114 // check for ALU/LUT in the adjacent cell
1115 int adj_lut_z = (1 - (z & 1) * 2 + z) * 2;
1116 int adj_alu_z = adj_lut_z / 2 + BelZ::ALU0_Z;
1117 const CellInfo *adj_lut = bels.at(adj_lut_z);
1118 const CellInfo *adj_ff = bels.at(adj_lut_z + 1);
1119 const CellInfo *adj_alu = adj_alu_z < (6 + BelZ::ALU0_Z) ? bels.at(adj_alu_z) : nullptr;
1120
1121 if ((alu && ((adj_lut && adj_lut->type != id_BLOCKER_LUT) || (adj_ff && !adj_alu))) ||
1122 (((lut && lut->type != id_BLOCKER_LUT) || (ff && !alu)) && adj_alu)) {
1123 return false;
1124 }
1125
1126 if (ff && ff->type != id_BLOCKER_FF) {
1127 static std::vector<int> mux_z = {BelZ::MUX20_Z, BelZ::MUX21_Z, BelZ::MUX20_Z + 4, BelZ::MUX23_Z,
1128 BelZ::MUX20_Z + 8, BelZ::MUX21_Z + 8, BelZ::MUX20_Z + 12, BelZ::MUX27_Z};
1129 const auto &ff_data = fast_cell_info.at(ff->flat_index);
1130 const NetInfo *src;
1131 // check implcit LUT(ALU) -> FF connection
1132 NPNR_ASSERT(!ramw); // XXX shouldn't happen for now
1133 if (lut || alu) {
1134 if (lut && lut->type != id_BLOCKER_LUT) {
1135 src = fast_cell_info.at(lut->flat_index).lut_f;
1136 } else {
1137 src = fast_cell_info.at(alu->flat_index).alu_sum;
1138 }
1139 if (ff_data.ff_d != src) {
1140 return false;
1141 }
1142 }
1143 if (adj_ff) {
1144 if (incompatible_ffs(ff, adj_ff)) {
1145 return false;
1146 }
1147
1148 // CE, LSR and CLK must match
1149 const auto &adj_ff_data = fast_cell_info.at(adj_ff->flat_index);
1150 if (adj_ff_data.ff_lsr != ff_data.ff_lsr) {
1151 return false;
1152 }
1153 if (adj_ff_data.ff_clk != ff_data.ff_clk) {
1154 return false;
1155 }
1156 if (adj_ff_data.ff_ce != ff_data.ff_ce) {
1157 return false;
1158 }
1159 }
1160 // Check whether the current architecture allows 6 and 7 DFFs
1161 if (z > 3 && gwu.has_DFF67()) {
1162 // The 4th, 5th, 6th, and 7th DFFs have the same control wires. Let's check this.
1163 const int adj_top_ff_z = (5 - (z >> 1)) * 4 + 1;
1164 for (int i = 0; i < 4; i += 2) {
1165 const CellInfo *adj_top_ff = bels.at(adj_top_ff_z + i);
1166 if (adj_top_ff) {
1167 const auto &adj_top_ff_data = fast_cell_info.at(adj_top_ff->flat_index);
1168 if (adj_top_ff_data.ff_lsr != ff_data.ff_lsr) {
1169 return false;
1170 }
1171 if (adj_top_ff_data.ff_clk != ff_data.ff_clk) {
1172 return false;
1173 }
1174 if (adj_top_ff_data.ff_ce != ff_data.ff_ce) {
1175 return false;
1176 }
1177 // It is sufficient to check only one DFF.
1178 break;
1179 }
1180 }
1181 }
1182 }
1183 return true;
1184 }
1185 /*
1186 Every HCLK section can be used in one of 3 ways:
1187 1. As a simple routing path to IOLOGIC FCLK
1188 2. As a CLKDIV2
1189 3. As a CLKDIV (potentially fed by the CLKDIV2 in its section)
1190
1191 Here we validate that the placement of cells fits within these 3 use cases, while ensuring that
1192 we enforce the constraint that only 1 CLKDIV can be used per HCLK (there is only 1 CLKDIV in each
1193 HCLK but we pretend there are two because doing so makes it easier to enforce the real constraint
1194 that HCLK signals don't crisscross between HCLK sections even after "transformation" by a CLKDIV
1195 or CLKDIV2)
1196 */
1197 bool GowinImpl::hclk_valid(BelId bel, IdString bel_type) const
1198 {
1199 if (bel_type == id_CLKDIV2) {
1200 if (routing_reserved_hclk_sections.count(bel))
1201 return false;
1202 auto clkdiv_cell = ctx->getBoundBelCell(gwu.get_clkdiv_for_clkdiv2(bel));
1203 if (clkdiv_cell && ctx->getBoundBelCell(bel)->cluster != clkdiv_cell->name)
1204 return false;
1205 return true;
1206 } else if (bel_type == id_CLKDIV) {
1207 BelId clkdiv2_bel = gwu.get_clkdiv2_for_clkdiv(bel);
1208 if (routing_reserved_hclk_sections.count(clkdiv2_bel)) {
1209 return false;
1210 }
1211
1212 auto other_clkdiv_cell = ctx->getBoundBelCell(gwu.get_other_hclk_clkdiv(bel));
1213 if (other_clkdiv_cell)
1214 return false;
1215
1216 auto clkdiv2_bel_cell = ctx->getBoundBelCell(clkdiv2_bel);
1217 if (clkdiv2_bel_cell && clkdiv2_bel_cell->cluster != ctx->getBoundBelCell(bel)->name)
1218 return false;
1219
1220 if (clkdiv2_bel_cell && chip.str(ctx) == "GW1N-9C") {
1221 // On the GW1N(R)-9C, it appears that only the 'odd' CLKDIV2 is connected to CLKDIV
1222 Loc loc = ctx->getBelLocation(bel);
1223 if (loc.z == BelZ::CLKDIV_0_Z || loc.z == BelZ::CLKDIV_2_Z)
1224 return false;
1225 }
1226
1227 return true;
1228 }
1229 return false;
1230 }
1231
1232 // Cluster
1233 bool GowinImpl::getClusterPlacement(ClusterId cluster, BelId root_bel,
1234 std::vector<std::pair<CellInfo *, BelId>> &placement) const
1235 {
1236 CellInfo *root_ci = getClusterRootCell(cluster);
1237 if (!root_ci->type.in(id_PADD9, id_MULT9X9, id_PADD18, id_MULT18X18, id_MULTALU18X18, id_MULTALU36X18,
1238 id_MULTADDALU18X18, id_ALU54D, id_MULTADDALU12X12, id_MULTALU27X18)) {
1239 return HimbaechelAPI::getClusterPlacement(cluster, root_bel, placement);
1240 }
1241
1242 NPNR_ASSERT(root_bel != BelId());
1243 if (!isValidBelForCellType(root_ci->type, root_bel)) {
1244 return false;
1245 }
1246
1247 IdString bel_type = ctx->getBelType(root_bel);
1248 // non-chain DSP
1249 if (root_ci->constr_children.size() == 1 && bel_type.in(id_PADD9, id_MULT9X9)) {
1250 return HimbaechelAPI::getClusterPlacement(cluster, root_bel, placement);
1251 }
1252
1253 placement.clear();
1254 Loc root_loc = ctx->getBelLocation(root_bel);
1255 placement.emplace_back(root_ci, root_bel);
1256
1257 Loc mult_loc = root_loc;
1258 for (auto child : root_ci->constr_children) {
1259 Loc child_loc;
1260 child_loc.y = root_loc.y;
1261 if (child->type == id_DUMMY_CELL) {
1262 child_loc.x = mult_loc.x + child->constr_x;
1263 child_loc.z = mult_loc.z + child->constr_z;
1264 } else {
1265 child_loc = gwu.get_dsp_next_in_chain(mult_loc, child->type);
1266 mult_loc = child_loc;
1267 }
1268
1269 BelId child_bel = ctx->getBelByLocation(child_loc);
1270 if (child_bel == BelId() || !isValidBelForCellType(child->type, child_bel))
1271 return false;
1272 placement.emplace_back(child, child_bel);
1273 }
1274 return true;
1275 }
1276
1277 void GowinImpl::notifyBelChange(BelId bel, CellInfo *cell)
1278 {
1279
1280 IdString bel_type = ctx->getBelType(bel);
1281 switch (bel_type.hash()) {
1282 case ID_LUT4: /* fall-through */
1283 case ID_DFF:
1284 case ID_ALU:
1285 case ID_RAM16SDP4:
1286 case ID_MUX2_LUT5:
1287 case ID_MUX2_LUT6:
1288 case ID_MUX2_LUT7:
1289 case ID_MUX2_LUT8:
1290 auto loc = ctx->getBelLocation(bel);
1291 fast_logic_cell.at(loc.x, loc.y).at(loc.z) = cell;
1292 return;
1293 }
1294
1295 if (cell != nullptr && !is_dsp(cell)) {
1296 return;
1297 }
1298 if (cell == nullptr && dsp_bel2cell.count(bel) == 0) {
1299 return;
1300 }
1301
1302 // trace DSP control networks
1303 IdString cell_type = id_DUMMY_CELL;
1304 if (cell != nullptr) {
1305 cell_type = cell->type;
1306 }
1307 Loc loc = ctx->getBelLocation(bel);
1308 Loc l = loc;
1309 l.z = gwu.get_dsp(loc.z);
1310 BelId dsp = ctx->getBelByLocation(l);
1311 l.z = gwu.get_dsp_macro(loc.z);
1312 BelId dsp_macro = ctx->getBelByLocation(l);
1313
1314 if (cell) {
1315 bool mode9 = cell_type.in(id_PADD9, id_MULT9X9);
1316 if (mode9) {
1317 dsp_info[dsp_macro].mode9bit++;
1318 } else {
1319 dsp_info[dsp_macro].mode18bit++;
1320 }
1321
1322 const auto &dsp_cell_data = fast_cell_info.at(cell->flat_index);
1323 if (dsp_cell_data.dsp_reset != nullptr) {
1324 dsp_info[dsp].reset[dsp_cell_data.dsp_reset->name]++;
1325 }
1326 if (dsp_cell_data.dsp_ce != nullptr) {
1327 dsp_info.at(dsp_macro).ce[dsp_cell_data.dsp_ce->name]++;
1328 }
1329 if (dsp_cell_data.dsp_clk != nullptr) {
1330 dsp_info.at(dsp_macro).clk[dsp_cell_data.dsp_clk->name]++;
1331 }
1332 dsp_bel2cell[bel] = cell;
1333 } else {
1334 bool mode9 = dsp_bel2cell.at(bel)->type.in(id_PADD9, id_MULT9X9);
1335 if (mode9) {
1336 dsp_info.at(dsp_macro).mode9bit--;
1337 } else {
1338 dsp_info.at(dsp_macro).mode18bit--;
1339 }
1340
1341 const auto &dsp_cell_data = fast_cell_info.at(dsp_bel2cell.at(bel)->flat_index);
1342 if (dsp_cell_data.dsp_reset != nullptr) {
1343 dsp_info.at(dsp).reset.at(dsp_cell_data.dsp_reset->name)--;
1344 }
1345 if (dsp_cell_data.dsp_ce != nullptr) {
1346 dsp_info.at(dsp_macro).ce.at(dsp_cell_data.dsp_ce->name)--;
1347 }
1348 if (dsp_cell_data.dsp_clk != nullptr) {
1349 dsp_info.at(dsp_macro).clk.at(dsp_cell_data.dsp_clk->name)--;
1350 }
1351 dsp_bel2cell.erase(bel);
1352 }
1353 }
1354
1355 void GowinImpl::configurePlacerHeap(PlacerHeapCfg &cfg)
1356 {
1357 // Use cell groups to enforce a legalisation order
1358 cfg.cellGroups.emplace_back();
1359 cfg.cellGroups.back().insert(id_RAM16SDP4);
1360 cfg.cellGroups.emplace_back();
1361 cfg.cellGroups.back().insert(id_ALU);
1362
1363 cfg.placeAllAtOnce = true;
1364
1365 // Treat control and constants like IO buffers, because they have only one possible location
1366 cfg.ioBufTypes.insert(id_GOWIN_VCC);
1367 cfg.ioBufTypes.insert(id_GOWIN_GND);
1368 cfg.ioBufTypes.insert(id_PINCFG);
1369 cfg.ioBufTypes.insert(id_GSR);
1370 }
1371
1372 void GowinImpl::drawBel(std::vector<GraphicElement> &g, GraphicElement::style_t style, IdString bel_type, Loc loc)
1373 {
1374 GraphicElement el;
1375 el.type = GraphicElement::TYPE_BOX;
1376 el.style = style;
1377 switch (bel_type.index) {
1378 case id_LUT4.index:
1379 el.x1 = loc.x + 0.75;
1380 el.x2 = el.x1 + 0.08;
1381 el.y1 = loc.y + 0.1 + 0.1 * (loc.z / 2);
1382 el.y2 = el.y1 + 0.04;
1383 g.push_back(el);
1384 break;
1385 case id_ALU.index:
1386 el.x1 = loc.x + 0.75;
1387 el.x2 = el.x1 + 0.08;
1388 el.y1 = loc.y + 0.1 + 0.1 * (loc.z - BelZ::ALU0_Z);
1389 el.y2 = el.y1 + 0.04;
1390 g.push_back(el);
1391 break;
1392 case id_DFF.index:
1393 el.x1 = loc.x + 0.9;
1394 el.x2 = el.x1 + 0.02;
1395 el.y1 = loc.y + 0.1 + 0.1 * (loc.z / 2);
1396 el.y2 = el.y1 + 0.04;
1397 g.push_back(el);
1398 break;
1399 case id_RAM16SDP4.index:
1400 el.x1 = loc.x + 0.85;
1401 el.x2 = el.x1 + 0.01;
1402 el.y1 = loc.y + 0.1;
1403 el.y2 = el.y1 + 0.65;
1404 g.push_back(el);
1405 break;
1406 }
1407 }
1408
1409 } // namespace
1410
1411 NEXTPNR_NAMESPACE_END