Debian Codegraph @

himbaechel/uarch/gatemate/pack_bram.cc

raw
1 /*
2 * nextpnr -- Next Generation Place and Route
3 *
4 * Copyright (C) 2024 The Project Peppercorn Authors.
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20 #include "design_utils.h"
21 #include "pack.h"
22
23 #define HIMBAECHEL_CONSTIDS "uarch/gatemate/constids.inc"
24 #include "himbaechel_constids.h"
25
26 NEXTPNR_NAMESPACE_BEGIN
27
28 inline bool is_bram_40k(const BaseCtx *ctx, const CellInfo *cell) { return cell->type.in(id_CC_BRAM_40K); }
29
30 uint8_t GateMatePacker::ram_ctrl_signal(CellInfo *cell, IdString port, bool alt)
31 {
32 NetInfo *net = cell->getPort(port);
33 if (net) {
34 if (net == net_PACKER_GND) {
35 cell->disconnectPort(port);
36 return 0b00000011;
37 } else if (net == net_PACKER_VCC) {
38 cell->disconnectPort(port);
39 return 0b00010011;
40 } else {
41 return alt ? 0b00000100 : 0b00000000;
42 }
43 }
44 return 0b00000011;
45 }
46
47 uint8_t GateMatePacker::ram_clk_signal(CellInfo *cell, IdString port)
48 {
49 NetInfo *clk_net = cell->getPort(port);
50 if (!uarch->global_signals.count(clk_net)) {
51 return 0b00000000;
52 } else {
53 int index = uarch->global_signals[clk_net];
54 uint8_t val = 0;
55 switch (index) {
56 case 0:
57 val = 0b00100011;
58 if (!cell->getPort(id_CLOCK1))
59 cell->renamePort(port, id_CLOCK1);
60 else
61 cell->disconnectPort(port);
62 break;
63 case 1:
64 val = 0b00110011;
65 if (!cell->getPort(id_CLOCK2))
66 cell->renamePort(port, id_CLOCK2);
67 else
68 cell->disconnectPort(port);
69 break;
70 case 2:
71 val = 0b00000011;
72 if (!cell->getPort(id_CLOCK3))
73 cell->renamePort(port, id_CLOCK3);
74 else
75 cell->disconnectPort(port);
76 break;
77 case 3:
78 val = 0b00010011;
79 if (!cell->getPort(id_CLOCK4))
80 cell->renamePort(port, id_CLOCK4);
81 else
82 cell->disconnectPort(port);
83 break;
84 }
85 return val;
86 }
87 }
88
89 int width_to_config(int width)
90 {
91 switch (width) {
92 case 0:
93 return 0;
94 case 1:
95 return 1;
96 case 2:
97 return 2;
98 case 3 ... 5:
99 return 3;
100 case 6 ... 10:
101 return 4;
102 case 11 ... 20:
103 return 5;
104 case 21 ... 40:
105 return 6;
106 case 41 ... 80:
107 return 7;
108 default:
109 log_error("Unsupported width '%d'.\n", width);
110 }
111 }
112
113 static void rename_or_move(CellInfo *main, CellInfo *other, IdString port, IdString other_port)
114 {
115 if (main == other)
116 main->renamePort(port, other_port);
117 else
118 main->movePortTo(port, other, other_port);
119 }
120
121 void GateMatePacker::pack_ram_cell(CellInfo &ci, CellInfo *cell, bool is_split)
122 {
123 // Port Widths
124 int a_rd_width = int_or_default(cell->params, id_A_RD_WIDTH, 0);
125 int b_rd_width = int_or_default(cell->params, id_B_RD_WIDTH, 0);
126 int a_wr_width = int_or_default(cell->params, id_A_WR_WIDTH, 0);
127 int b_wr_width = int_or_default(cell->params, id_B_WR_WIDTH, 0);
128
129 std::string a_wr_mode_str = str_or_default(cell->params, id_A_WR_MODE, "NO_CHANGE");
130 if (a_wr_mode_str != "NO_CHANGE" && a_wr_mode_str != "WRITE_THROUGH")
131 log_error("Unknown A_WR_MODE parameter value '%s' for cell %s.\n", a_wr_mode_str.c_str(),
132 cell->name.c_str(ctx));
133 int a_wr_mode = a_wr_mode_str == "NO_CHANGE" ? 0 : 1;
134 std::string b_wr_mode_str = str_or_default(cell->params, id_B_WR_MODE, "NO_CHANGE");
135 if (b_wr_mode_str != "NO_CHANGE" && b_wr_mode_str != "WRITE_THROUGH")
136 log_error("Unknown B_WR_MODE parameter value '%s' for cell %s.\n", b_wr_mode_str.c_str(),
137 cell->name.c_str(ctx));
138 int b_wr_mode = b_wr_mode_str == "NO_CHANGE" ? 0 : 1;
139
140 // Inverting Control Pins
141 int a_clk_inv = int_or_default(cell->params, id_A_CLK_INV, 0);
142 int b_clk_inv = int_or_default(cell->params, id_B_CLK_INV, 0);
143 int a_en_inv = int_or_default(cell->params, id_A_EN_INV, 0);
144 int b_en_inv = int_or_default(cell->params, id_B_EN_INV, 0);
145 int a_we_inv = int_or_default(cell->params, id_A_WE_INV, 0);
146 int b_we_inv = int_or_default(cell->params, id_B_WE_INV, 0);
147
148 // Output Register
149 int a_do_reg = int_or_default(cell->params, id_A_DO_REG, 0);
150 int b_do_reg = int_or_default(cell->params, id_B_DO_REG, 0);
151
152 disconnect_if_gnd(cell, id_A_CLK);
153 disconnect_if_gnd(cell, id_B_CLK);
154
155 uint8_t cfg_a = ram_clk_signal(cell, id_A_CLK);
156 uint8_t cfg_b = ram_clk_signal(cell, id_B_CLK);
157 uint8_t a_inv = a_clk_inv << 2 | a_we_inv << 1 | a_en_inv;
158 uint8_t b_inv = b_clk_inv << 2 | b_we_inv << 1 | b_en_inv;
159 uint8_t a_en = ram_ctrl_signal(cell, id_A_EN, false);
160 uint8_t b_en = ram_ctrl_signal(cell, id_B_EN, false);
161 uint8_t a_we = ram_ctrl_signal(cell, id_A_WE, false);
162 uint8_t b_we = ram_ctrl_signal(cell, id_B_WE, false);
163
164 ci.params[id_RAM_cfg_forward_a0_clk] = Property(cfg_a, 8);
165 if (!is_split)
166 ci.params[id_RAM_cfg_forward_a1_clk] = Property(cfg_a, 8);
167
168 ci.params[id_RAM_cfg_forward_b0_clk] = Property(cfg_b, 8);
169 if (!is_split)
170 ci.params[id_RAM_cfg_forward_b1_clk] = Property(cfg_b, 8);
171
172 ci.params[id_RAM_cfg_forward_a0_en] = Property(a_en, 8);
173 ci.params[id_RAM_cfg_forward_b0_en] = Property(b_en, 8);
174
175 ci.params[id_RAM_cfg_forward_a0_we] = Property(a_we, 8);
176 ci.params[id_RAM_cfg_forward_b0_we] = Property(b_we, 8);
177
178 ci.params[id_RAM_cfg_input_config_a0] = Property(width_to_config(a_wr_width), 3);
179 ci.params[id_RAM_cfg_input_config_b0] = Property(width_to_config(b_wr_width), 3);
180 ci.params[id_RAM_cfg_output_config_a0] = Property(width_to_config(a_rd_width), 3);
181 ci.params[id_RAM_cfg_output_config_b0] = Property(width_to_config(b_rd_width), 3);
182
183 ci.params[id_RAM_cfg_a0_writemode] = Property(a_wr_mode, 1);
184 ci.params[id_RAM_cfg_b0_writemode] = Property(b_wr_mode, 1);
185
186 ci.params[id_RAM_cfg_a0_set_outputreg] = Property(a_do_reg, 1);
187 ci.params[id_RAM_cfg_b0_set_outputreg] = Property(b_do_reg, 1);
188
189 ci.params[id_RAM_cfg_inversion_a0] = Property(a_inv, 3);
190 ci.params[id_RAM_cfg_inversion_b0] = Property(b_inv, 3);
191
192 rename_or_move(cell, &ci, id_A_CLK, ctx->id("CLKA[0]"));
193 rename_or_move(cell, &ci, id_B_CLK, ctx->id("CLKB[0]"));
194 rename_or_move(cell, &ci, id_A_EN, ctx->id("ENA[0]"));
195 rename_or_move(cell, &ci, id_B_EN, ctx->id("ENB[0]"));
196 rename_or_move(cell, &ci, id_A_WE, ctx->id("GLWEA[0]"));
197 rename_or_move(cell, &ci, id_B_WE, ctx->id("GLWEB[0]"));
198 if (is_split) {
199 rename_or_move(cell, &ci, id_ECC_1B_ERR, ctx->id("ECC1B_ERRA[0]"));
200 rename_or_move(cell, &ci, id_ECC_2B_ERR, ctx->id("ECC2B_ERRA[0]"));
201 } else {
202 rename_or_move(cell, &ci, id_A_ECC_1B_ERR, ctx->id("ECC1B_ERRA[0]"));
203 rename_or_move(cell, &ci, id_B_ECC_1B_ERR, ctx->id("ECC1B_ERRB[0]"));
204 rename_or_move(cell, &ci, id_A_ECC_2B_ERR, ctx->id("ECC2B_ERRA[0]"));
205 rename_or_move(cell, &ci, id_B_ECC_2B_ERR, ctx->id("ECC2B_ERRB[0]"));
206 }
207 int items = is_split ? 20 : 40;
208 for (int i = 0; i < items; i++) {
209 rename_or_move(cell, &ci, ctx->idf("A_BM[%d]", i), ctx->idf("WEA[%d]", i));
210 rename_or_move(cell, &ci, ctx->idf("B_BM[%d]", i), ctx->idf("WEB[%d]", i));
211 }
212
213 for (int i = 0; i < 16; i++) {
214 rename_or_move(cell, &ci, ctx->idf("A_ADDR[%d]", i), ctx->idf("ADDRA0[%d]", i));
215 rename_or_move(cell, &ci, ctx->idf("B_ADDR[%d]", i), ctx->idf("ADDRB0[%d]", i));
216 }
217
218 for (int i = 0; i < items; i++) {
219 rename_or_move(cell, &ci, ctx->idf("A_DI[%d]", i), ctx->idf("DIA[%d]", i));
220 rename_or_move(cell, &ci, ctx->idf("A_DO[%d]", i), ctx->idf("DOA[%d]", i));
221 rename_or_move(cell, &ci, ctx->idf("B_DI[%d]", i), ctx->idf("DIB[%d]", i));
222 rename_or_move(cell, &ci, ctx->idf("B_DO[%d]", i), ctx->idf("DOB[%d]", i));
223 }
224 }
225
226 void GateMatePacker::pack_ram()
227 {
228 std::vector<CellInfo *> rams;
229 std::map<CellInfo *, CellInfo *> ram_cascade;
230 log_info("Packing RAMs..\n");
231 for (auto &cell : ctx->cells) {
232 CellInfo &ci = *cell.second;
233 if (!ci.type.in(id_CC_BRAM_20K, id_CC_BRAM_40K, id_CC_FIFO_40K))
234 continue;
235 int split = ci.type.in(id_CC_BRAM_20K) ? 1 : 0;
236 std::string ram_mode_str = str_or_default(ci.params, id_RAM_MODE, "SDP");
237 if (ram_mode_str != "SDP" && ram_mode_str != "TDP")
238 log_error("Unknown RAM_MODE parameter value '%s' for cell %s.\n", ram_mode_str.c_str(), ci.name.c_str(ctx));
239 std::string cas = str_or_default(ci.params, id_CAS, "NONE");
240 if (cas != "NONE" && !ci.type.in(id_CC_BRAM_40K))
241 log_error("Cascade feature only supported for CC_BRAM_40K.\n");
242
243 int items = split ? 20 : 40;
244 for (int i = 0; i < items; i++) {
245 if (!ci.getPort(ctx->idf("A_DI[%d]", i)))
246 ci.disconnectPort(ctx->idf("A_BM[%d]", i));
247 if (!ci.getPort(ctx->idf("B_DI[%d]", i)))
248 ci.disconnectPort(ctx->idf("B_BM[%d]", i));
249 }
250
251 if (split) {
252 rams.push_back(&ci);
253 } else {
254 CellInfo *upper = nullptr;
255 CellInfo *lower = nullptr;
256 if (cas != "NONE" && ram_mode_str != "TDP")
257 log_error("Cascade feature only supported in TDP mode.\n");
258 int a_rd_width = int_or_default(ci.params, id_A_WIDTH, 0);
259 int b_wr_width = int_or_default(ci.params, id_B_WIDTH, 0);
260 if (cas != "NONE" && (a_rd_width > 1 || b_wr_width > 1))
261 log_error("Cascade feature only supported in 1 bit data width mode.\n");
262 if (cas == "UPPER") {
263 if (!net_driven_by(ctx, ci.getPort(id_A_CI), is_bram_40k, id_A_CO))
264 log_error("Port A_CI of '%s' must be driven by other CC_BRAM_40K.", ci.name.c_str(ctx));
265 if (!net_driven_by(ctx, ci.getPort(id_B_CI), is_bram_40k, id_B_CO))
266 log_error("Port B_CI of '%s' must be driven by other CC_BRAM_40K.", ci.name.c_str(ctx));
267 upper = &ci;
268 lower = ci.getPort(id_A_CI)->driver.cell;
269 } else if (cas == "LOWER") {
270 if (!net_only_drives(ctx, ci.getPort(id_A_CO), is_bram_40k, id_A_CI, true))
271 log_error("Port A_CO of '%s' must be driving one other CC_BRAM_40K.", ci.name.c_str(ctx));
272 if (!net_only_drives(ctx, ci.getPort(id_B_CO), is_bram_40k, id_B_CI, true))
273 log_error("Port B_CO of '%s' must be driving one other CC_BRAM_40K.", ci.name.c_str(ctx));
274 upper = (*ci.getPort(id_A_CO)->users.begin()).cell;
275 lower = &ci;
276 }
277 if (ram_cascade.count(lower) && ram_cascade[lower] != upper)
278 log_error("RAM cell '%s' already cascaded to different RAM block.\n", ci.name.c_str(ctx));
279 ram_cascade[lower] = upper;
280
281 rams.push_back(&ci);
282 }
283 }
284
285 for (auto item : rams) {
286 CellInfo &ci = *item;
287 int split = ci.type.in(id_CC_BRAM_20K) ? 1 : 0;
288 bool is_fifo = ci.type.in(id_CC_FIFO_40K);
289
290 ci.type = split ? id_RAM_HALF : id_RAM;
291 ci.cluster = ci.name;
292
293 // Location format: D(0..N-1)X(0..3)Y(0..7) or UNPLACED
294 std::string loc = str_or_default(ci.params, id_LOC, "UNPLACED");
295 std::string cas = str_or_default(ci.params, id_CAS, "NONE");
296
297 int cascade = 0;
298 // Concepts of UPPER and LOWER are different in documentation
299 if (cas == "NONE") {
300 cascade = 0;
301 } else if (cas == "UPPER") {
302 cascade = 2;
303 ci.disconnectPort(id_A_CI);
304 ci.disconnectPort(id_B_CI);
305 } else if (cas == "LOWER") {
306 cascade = 1;
307 ci.disconnectPort(id_A_CO);
308 ci.disconnectPort(id_B_CO);
309 if (!ram_cascade.count(&ci))
310 log_error("Unable to find cascaded RAM for '%s'.\n", ci.name.c_str(ctx));
311 CellInfo *upper = ram_cascade[&ci];
312 ci.cluster = upper->name;
313 upper->constr_children.push_back(&ci);
314 ci.constr_abs_z = false;
315 ci.constr_y = -16;
316 ci.constr_z = 0;
317 } else {
318 log_error("Unknown CAS parameter value '%s' for cell %s.\n", cas.c_str(), ci.name.c_str(ctx));
319 }
320
321 // RAM and Write Modes
322 std::string ram_mode_str = str_or_default(ci.params, id_RAM_MODE, "SDP");
323 if (ram_mode_str != "SDP" && ram_mode_str != "TDP")
324 log_error("Unknown RAM_MODE parameter value '%s' for cell %s.\n", ram_mode_str.c_str(), ci.name.c_str(ctx));
325 int ram_mode = ram_mode_str == "SDP" ? 1 : 0;
326
327 // Error Checking and Correction
328 int a_ecc_en = int_or_default(ci.params, id_A_ECC_EN, 0);
329 int b_ecc_en = int_or_default(ci.params, id_B_ECC_EN, 0);
330 if (ci.params.count(id_ECC_EN)) {
331 a_ecc_en = int_or_default(ci.params, id_ECC_EN, 0);
332 }
333 ci.params[id_RAM_cfg_ecc_enable] = Property(b_ecc_en << 1 | a_ecc_en, 2);
334
335 ci.params[id_RAM_cfg_forward_a_addr] = Property(0b00000000, 8);
336 ci.params[id_RAM_cfg_forward_b_addr] = Property(0b00000000, 8);
337
338 ci.params[id_RAM_cfg_sram_mode] = Property(ram_mode << 1 | split, 2);
339
340 ci.params[id_RAM_cfg_sram_delay] = Property(0b000101, 6); // Always set to default
341 // id_RAM_cfg_datbm_sel
342 ci.params[id_RAM_cfg_cascade_enable] = Property(cascade, 2);
343
344 if (!split) {
345 CellInfo *cell = ctx->createCell(ctx->idf("%s$dummy$l", ci.name.c_str(ctx)), id_RAM_HALF_DUMMY);
346 ci.constr_children.push_back(cell);
347 cell->constr_abs_z = true;
348 cell->constr_y = +8;
349 cell->constr_z = RAM_HALF_L_Z;
350 cell->cluster = ci.cluster;
351 cell->region = ci.region;
352 cell->params[id_RAM_cfg_ecc_enable] = Property(b_ecc_en << 1 | a_ecc_en, 2);
353 cell->params[id_RAM_cfg_sram_mode] = Property(ram_mode << 1 | split, 2);
354 }
355
356 pack_ram_cell(ci, item, split);
357
358 if (is_fifo) {
359 int a_rd_width = int_or_default(ci.params, id_A_WIDTH, 0);
360 int b_wr_width = int_or_default(ci.params, id_B_WIDTH, 0);
361 if (a_rd_width != b_wr_width)
362 log_error("The FIFO configuration of A_WIDTH and B_WIDTH must be equal.\n");
363
364 if (a_rd_width != 80 && ram_mode == 1)
365 log_error("FIFO SDP is only supported in 80 bit mode.\n");
366
367 ci.params[id_RAM_cfg_input_config_b0] = Property(width_to_config(b_wr_width), 3);
368 ci.params[id_RAM_cfg_output_config_a0] = Property(width_to_config(a_rd_width), 3);
369
370 std::string fifo_mode_str = str_or_default(ci.params, id_FIFO_MODE, "SYNC");
371 if (fifo_mode_str != "SYNC" && fifo_mode_str != "ASYNC")
372 log_error("Unknown FIFO_MODE parameter value '%s' for cell %s.\n", fifo_mode_str.c_str(),
373 ci.name.c_str(ctx));
374 int fifo_mode = fifo_mode_str == "SYNC" ? 1 : 0;
375
376 if (fifo_mode)
377 ci.params[id_RAM_cfg_fifo_sync_enable] = Property(0b1, 1);
378 else
379 ci.params[id_RAM_cfg_fifo_async_enable] = Property(0b1, 1);
380
381 int dyn_stat_select = int_or_default(ci.params, id_DYN_STAT_SELECT, 0);
382 if (dyn_stat_select != 0 && dyn_stat_select != 1)
383 log_error("DYN_STAT_SELECT must be 0 or 1.\n");
384 if (dyn_stat_select != 0 && ram_mode == 1)
385 log_error("Dynamic FIFO offset configuration is not supported in SDP mode.\n");
386 ci.params[id_RAM_cfg_dyn_stat_select] = Property(dyn_stat_select << 1, 2);
387 ci.params[id_RAM_cfg_almost_empty_offset] =
388 Property(int_or_default(ci.params, id_ALMOST_EMPTY_OFFSET, 0), 15);
389 ci.params[id_RAM_cfg_almost_full_offset] =
390 Property(int_or_default(ci.params, id_ALMOST_FULL_OFFSET, 0), 15);
391
392 if (dyn_stat_select != 0 && ram_mode == 0) {
393 for (int i = 0; i < 15; ++i) {
394 // WEA[14:0] = F_ALMOST_EMPTY_OFFSET
395 ci.disconnectPort(ctx->idf("WEA[%d]", i));
396 ci.renamePort(ctx->idf("F_ALMOST_EMPTY_OFFSET[%d]", i), ctx->idf("WEA[%d]", i));
397 // WEA[34:20] = F_ALMOST_FULL_OFFSET
398 ci.disconnectPort(ctx->idf("WEA[%d]", 20 + i));
399 ci.renamePort(ctx->idf("F_ALMOST_FULL_OFFSET[%d]", i), ctx->idf("WEA[%d]", 20 + i));
400 }
401 }
402 }
403
404 for (int i = 0; i < 40; i++) {
405 move_ram_o(&ci, ctx->idf("WEA[%d]", i));
406 move_ram_o(&ci, ctx->idf("WEB[%d]", i));
407 }
408
409 for (int i = 0; i < 16; i++) {
410 move_ram_o(&ci, ctx->idf("ADDRA0[%d]", i));
411 move_ram_o(&ci, ctx->idf("ADDRB0[%d]", i));
412 move_ram_o(&ci, ctx->idf("ADDRA1[%d]", i));
413 move_ram_o(&ci, ctx->idf("ADDRB1[%d]", i));
414 }
415
416 for (int i = 0; i < 40; i++) {
417 move_ram_io(&ci, ctx->idf("DOA[%d]", i), ctx->idf("DIA[%d]", i));
418 move_ram_io(&ci, ctx->idf("DOB[%d]", i), ctx->idf("DIB[%d]", i));
419 }
420 for (int i = 0; i < 4; i++) {
421 move_ram_o(&ci, ctx->idf("CLKA[%d]", i));
422 move_ram_o(&ci, ctx->idf("CLKB[%d]", i));
423 move_ram_o(&ci, ctx->idf("ENA[%d]", i));
424 move_ram_o(&ci, ctx->idf("ENB[%d]", i));
425 move_ram_o(&ci, ctx->idf("GLWEA[%d]", i));
426 move_ram_o(&ci, ctx->idf("GLWEB[%d]", i));
427 move_ram_i(&ci, ctx->idf("ECC1B_ERRA[%d]", i));
428 move_ram_i(&ci, ctx->idf("ECC1B_ERRB[%d]", i));
429 move_ram_i(&ci, ctx->idf("ECC2B_ERRA[%d]", i));
430 move_ram_i(&ci, ctx->idf("ECC2B_ERRB[%d]", i));
431 }
432
433 if (is_fifo) {
434 int dyn_stat_select = int_or_default(ci.params, id_DYN_STAT_SELECT, 0);
435 if (dyn_stat_select == 0) {
436 for (int i = 0; i < 15; i++) {
437 ci.disconnectPort(ctx->idf("F_ALMOST_EMPTY_OFFSET[%d]", i));
438 ci.disconnectPort(ctx->idf("F_ALMOST_FULL_OFFSET[%d]", i));
439 }
440 }
441 ci.renamePort(id_F_EMPTY, ctx->id("F_EMPTY[0]"));
442 move_ram_i(&ci, ctx->id("F_EMPTY[0]"));
443 ci.renamePort(id_F_FULL, ctx->id("F_FULL[0]"));
444 move_ram_i(&ci, ctx->id("F_FULL[0]"));
445 ci.renamePort(id_F_ALMOST_FULL, ctx->id("F_AL_FULL[0]"));
446 move_ram_i(&ci, ctx->id("F_AL_FULL[0]"));
447 ci.renamePort(id_F_ALMOST_EMPTY, ctx->id("F_AL_EMPTY[0]"));
448 move_ram_i(&ci, ctx->id("F_AL_EMPTY[0]"));
449
450 ci.renamePort(id_F_WR_ERROR, ctx->id("FWR_ERR[0]"));
451 move_ram_i(&ci, ctx->id("FWR_ERR[0]"));
452 ci.renamePort(id_F_RD_ERROR, ctx->id("FRD_ERR[0]"));
453 move_ram_i(&ci, ctx->id("FRD_ERR[0]"));
454
455 ci.renamePort(id_F_RST_N, ctx->id("F_RSTN"));
456 move_ram_o(&ci, ctx->id("F_RSTN"));
457
458 for (int i = 0; i < 16; i++) {
459 ci.renamePort(ctx->idf("F_RD_PTR[%d]", i), ctx->idf("FRD_ADDR[%d]", i));
460 move_ram_i(&ci, ctx->idf("FRD_ADDR[%d]", i));
461
462 ci.renamePort(ctx->idf("F_WR_PTR[%d]", i), ctx->idf("FWR_ADDR[%d]", i));
463 move_ram_i(&ci, ctx->idf("FWR_ADDR[%d]", i));
464 }
465 }
466 }
467 flush_cells();
468 }
469
470 void GateMatePacker::remap_ram_half(CellInfo *half, CellInfo *cell, int num)
471 {
472 int index = num ? 2 : 0;
473
474 rename_or_move(half, cell, ctx->id("CLKA[0]"), ctx->idf("CLKA[%d]", index));
475 rename_or_move(half, cell, ctx->id("CLKB[0]"), ctx->idf("CLKB[%d]", index));
476 rename_or_move(half, cell, ctx->id("ENA[0]"), ctx->idf("ENA[%d]", index));
477 rename_or_move(half, cell, ctx->id("ENB[0]"), ctx->idf("ENB[%d]", index));
478 rename_or_move(half, cell, ctx->id("GLWEA[0]"), ctx->idf("GLWEA[%d]", index));
479 rename_or_move(half, cell, ctx->id("GLWEB[0]"), ctx->idf("GLWEB[%d]", index));
480 for (int i = 0; i < 20; i++) {
481 rename_or_move(half, cell, ctx->idf("WEA[%d]", i), ctx->idf("WEA[%d]", i + 20 * num));
482 rename_or_move(half, cell, ctx->idf("WEB[%d]", i), ctx->idf("WEB[%d]", i + 20 * num));
483 rename_or_move(half, cell, ctx->idf("DIA[%d]", i), ctx->idf("DIA[%d]", i + 20 * num));
484 rename_or_move(half, cell, ctx->idf("DIB[%d]", i), ctx->idf("DIB[%d]", i + 20 * num));
485 rename_or_move(half, cell, ctx->idf("DOA[%d]", i), ctx->idf("DOA[%d]", i + 20 * num));
486 rename_or_move(half, cell, ctx->idf("DOB[%d]", i), ctx->idf("DOB[%d]", i + 20 * num));
487 }
488 for (int i = 0; i < 16; i++) {
489 rename_or_move(half, cell, ctx->idf("ADDRA0[%d]", i), ctx->idf("ADDRA%d[%d]", num, i));
490 rename_or_move(half, cell, ctx->idf("ADDRB0[%d]", i), ctx->idf("ADDRB%d[%d]", num, i));
491 }
492
493 index = num ? 1 : 0;
494 rename_or_move(half, cell, ctx->id("ECC1B_ERRA[0]"), ctx->idf("ECC1B_ERRA[%d]", index));
495 rename_or_move(half, cell, ctx->id("ECC1B_ERRB[0]"), ctx->idf("ECC1B_ERRB[%d]", index));
496 rename_or_move(half, cell, ctx->id("ECC2B_ERRA[0]"), ctx->idf("ECC2B_ERRA[%d]", index));
497 rename_or_move(half, cell, ctx->id("ECC2B_ERRB[0]"), ctx->idf("ECC2B_ERRB[%d]", index));
498
499 for (int i = 1; i < 5; i++)
500 if (!cell->getPort(ctx->idf("CLOCK%d", i)))
501 rename_or_move(half, cell, ctx->idf("CLOCK%d", i), ctx->idf("CLOCK%d", i));
502
503 static dict<IdString, IdString> map_params = {
504 {id_RAM_cfg_forward_a0_clk, id_RAM_cfg_forward_a1_clk},
505 {id_RAM_cfg_forward_b0_clk, id_RAM_cfg_forward_b1_clk},
506
507 {id_RAM_cfg_forward_a0_en, id_RAM_cfg_forward_a1_en},
508 {id_RAM_cfg_forward_b0_en, id_RAM_cfg_forward_b1_en},
509
510 {id_RAM_cfg_forward_a0_we, id_RAM_cfg_forward_a1_we},
511 {id_RAM_cfg_forward_b0_we, id_RAM_cfg_forward_b1_we},
512
513 {id_RAM_cfg_input_config_a0, id_RAM_cfg_input_config_a1},
514 {id_RAM_cfg_input_config_b0, id_RAM_cfg_input_config_b1},
515 {id_RAM_cfg_output_config_a0, id_RAM_cfg_output_config_a1},
516 {id_RAM_cfg_output_config_b0, id_RAM_cfg_output_config_b1},
517
518 {id_RAM_cfg_a0_writemode, id_RAM_cfg_a1_writemode},
519 {id_RAM_cfg_b0_writemode, id_RAM_cfg_b1_writemode},
520
521 {id_RAM_cfg_a0_set_outputreg, id_RAM_cfg_a1_set_outputreg},
522 {id_RAM_cfg_b0_set_outputreg, id_RAM_cfg_b1_set_outputreg},
523
524 {id_RAM_cfg_inversion_a0, id_RAM_cfg_inversion_a1},
525 {id_RAM_cfg_inversion_b0, id_RAM_cfg_inversion_b1},
526
527 // This is for both halfs and it is same
528 {id_RAM_cfg_forward_a_addr, id_RAM_cfg_forward_a_addr},
529 {id_RAM_cfg_forward_b_addr, id_RAM_cfg_forward_b_addr},
530 {id_RAM_cfg_sram_mode, id_RAM_cfg_sram_mode},
531 {id_RAM_cfg_ecc_enable, id_RAM_cfg_ecc_enable},
532 {id_RAM_cfg_sram_delay, id_RAM_cfg_sram_delay},
533 {id_RAM_cfg_cascade_enable, id_RAM_cfg_cascade_enable},
534 };
535
536 for (auto &p : map_params) {
537 if (map_params.count(p.first)) {
538 cell->params[num ? p.second : p.first] = half->params[p.first];
539 }
540 }
541 }
542
543 void GateMatePacker::repack_ram()
544 {
545 log_info("Repacking RAMs..\n");
546 dict<Loc, std::pair<CellInfo *, CellInfo *>> rams;
547 for (auto &cell : ctx->cells) {
548 if (cell.second->type.in(id_RAM_HALF)) {
549 Loc l = ctx->getBelLocation(cell.second->bel);
550 if (l.z == RAM_FULL_Z) {
551 rams[Loc(l.x, l.y, 0)].first = cell.second.get();
552 } else {
553 rams[Loc(l.x, l.y - 8, 0)].second = cell.second.get();
554 }
555 } else if (cell.second->type.in(id_RAM_HALF_DUMMY))
556 packed_cells.insert(cell.second->name);
557 }
558 int id = 0;
559 for (auto &ram : rams) {
560 IdString name = ctx->idf("$ram$merged$id%d", id);
561 if (!ram.second.first)
562 name = ctx->idf("%s$full", ram.second.second->name.c_str(ctx));
563 if (!ram.second.second)
564 name = ctx->idf("%s$full", ram.second.first->name.c_str(ctx));
565
566 if (ram.second.first)
567 ctx->unbindBel(ram.second.first->bel);
568 if (ram.second.second)
569 ctx->unbindBel(ram.second.second->bel);
570
571 CellInfo *cell = ctx->createCell(name, id_RAM);
572 BelId bel = ctx->getBelByLocation({ram.first.x, ram.first.y, RAM_FULL_Z});
573 ctx->bindBel(bel, cell, PlaceStrength::STRENGTH_FIXED);
574
575 if (ram.second.first) {
576 remap_ram_half(ram.second.first, cell, 0);
577 packed_cells.insert(ram.second.first->name);
578 }
579 if (ram.second.second) {
580 remap_ram_half(ram.second.second, cell, 1);
581 packed_cells.insert(ram.second.second->name);
582 }
583
584 for (int i = 63; i >= 0; i--) {
585 std::vector<bool> orig_first;
586 if (ram.second.first)
587 orig_first = ram.second.first->params.at(ctx->idf("INIT_%02X", i)).extract(0, 320).as_bits();
588 std::vector<bool> orig_second;
589 if (ram.second.second)
590 orig_second = ram.second.second->params.at(ctx->idf("INIT_%02X", i)).extract(0, 320).as_bits();
591 std::string init[2];
592
593 for (int j = 0; j < 2; j++) {
594 for (int k = 0; k < 4; k++) {
595 for (int l = 0; l < 40; l++) {
596 if (ram.second.second)
597 init[j].push_back(orig_second.at(319 - (l + k * 40 + j * 160)) ? '1' : '0');
598 else
599 init[j].push_back('0');
600 }
601 for (int l = 0; l < 40; l++) {
602 if (ram.second.first)
603 init[j].push_back(orig_first.at(319 - (l + k * 40 + j * 160)) ? '1' : '0');
604 else
605 init[j].push_back('0');
606 }
607 }
608 }
609 cell->params[ctx->idf("INIT_%02X", i * 2 + 1)] = Property::from_string(init[0]);
610 cell->params[ctx->idf("INIT_%02X", i * 2 + 0)] = Property::from_string(init[1]);
611 }
612
613 id++;
614 }
615 flush_cells();
616 ctx->assignArchInfo();
617 }
618
619 NEXTPNR_NAMESPACE_END