Add v2 memory cells.
[yosys.git] / kernel / mem.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2020 Marcelina Kościelnicka <mwk@0x04.net>
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 "kernel/mem.h"
21 #include "kernel/ff.h"
22
23 USING_YOSYS_NAMESPACE
24
25 void Mem::remove() {
26 if (cell) {
27 module->remove(cell);
28 cell = nullptr;
29 }
30 if (mem) {
31 module->memories.erase(mem->name);
32 delete mem;
33 mem = nullptr;
34 }
35 for (auto &port : rd_ports) {
36 if (port.cell) {
37 module->remove(port.cell);
38 port.cell = nullptr;
39 }
40 }
41 for (auto &port : wr_ports) {
42 if (port.cell) {
43 module->remove(port.cell);
44 port.cell = nullptr;
45 }
46 }
47 for (auto &init : inits) {
48 if (init.cell) {
49 module->remove(init.cell);
50 init.cell = nullptr;
51 }
52 }
53 }
54
55 void Mem::emit() {
56 check();
57 std::vector<int> rd_left;
58 for (int i = 0; i < GetSize(rd_ports); i++) {
59 auto &port = rd_ports[i];
60 if (port.removed) {
61 if (port.cell) {
62 module->remove(port.cell);
63 }
64 } else {
65 rd_left.push_back(i);
66 }
67 }
68 std::vector<int> wr_left;
69 for (int i = 0; i < GetSize(wr_ports); i++) {
70 auto &port = wr_ports[i];
71 if (port.removed) {
72 if (port.cell) {
73 module->remove(port.cell);
74 }
75 } else {
76 wr_left.push_back(i);
77 }
78 }
79 std::vector<int> init_left;
80 for (int i = 0; i < GetSize(inits); i++) {
81 auto &init = inits[i];
82 if (init.removed) {
83 if (init.cell) {
84 module->remove(init.cell);
85 }
86 } else {
87 init_left.push_back(i);
88 }
89 }
90 for (int i = 0; i < GetSize(rd_left); i++)
91 if (i != rd_left[i])
92 std::swap(rd_ports[i], rd_ports[rd_left[i]]);
93 rd_ports.resize(GetSize(rd_left));
94 for (int i = 0; i < GetSize(wr_left); i++)
95 if (i != wr_left[i])
96 std::swap(wr_ports[i], wr_ports[wr_left[i]]);
97 wr_ports.resize(GetSize(wr_left));
98 for (int i = 0; i < GetSize(init_left); i++)
99 if (i != init_left[i])
100 std::swap(inits[i], inits[init_left[i]]);
101 inits.resize(GetSize(init_left));
102
103 for (auto &port : rd_ports) {
104 for (int i = 0; i < GetSize(wr_left); i++) {
105 port.transparency_mask[i] = port.transparency_mask[wr_left[i]];
106 port.collision_x_mask[i] = port.collision_x_mask[wr_left[i]];
107 }
108 port.transparency_mask.resize(GetSize(wr_left));
109 port.collision_x_mask.resize(GetSize(wr_left));
110 }
111 for (auto &port : wr_ports) {
112 for (int i = 0; i < GetSize(wr_left); i++)
113 port.priority_mask[i] = port.priority_mask[wr_left[i]];
114 port.priority_mask.resize(GetSize(wr_left));
115 }
116
117 if (packed) {
118 if (mem) {
119 module->memories.erase(mem->name);
120 delete mem;
121 mem = nullptr;
122 }
123 if (!cell) {
124 if (memid.empty())
125 memid = NEW_ID;
126 cell = module->addCell(memid, ID($mem_v2));
127 }
128 cell->type = ID($mem_v2);
129 cell->attributes = attributes;
130 cell->parameters[ID::MEMID] = Const(memid.str());
131 cell->parameters[ID::WIDTH] = Const(width);
132 cell->parameters[ID::OFFSET] = Const(start_offset);
133 cell->parameters[ID::SIZE] = Const(size);
134 Const rd_wide_continuation, rd_clk_enable, rd_clk_polarity, rd_transparency_mask, rd_collision_x_mask;
135 Const wr_wide_continuation, wr_clk_enable, wr_clk_polarity, wr_priority_mask;
136 Const rd_ce_over_srst, rd_arst_value, rd_srst_value, rd_init_value;
137 SigSpec rd_clk, rd_en, rd_addr, rd_data;
138 SigSpec wr_clk, wr_en, wr_addr, wr_data;
139 SigSpec rd_arst, rd_srst;
140 int abits = 0;
141 for (auto &port : rd_ports)
142 abits = std::max(abits, GetSize(port.addr));
143 for (auto &port : wr_ports)
144 abits = std::max(abits, GetSize(port.addr));
145 cell->parameters[ID::ABITS] = Const(abits);
146 std::vector<int> wr_port_xlat;
147 for (int i = 0; i < GetSize(wr_ports); i++)
148 for (int j = 0; j < (1 << wr_ports[i].wide_log2); j++)
149 wr_port_xlat.push_back(i);
150 for (auto &port : rd_ports) {
151 if (port.cell) {
152 module->remove(port.cell);
153 port.cell = nullptr;
154 }
155 for (int sub = 0; sub < (1 << port.wide_log2); sub++)
156 {
157 rd_wide_continuation.bits.push_back(State(sub != 0));
158 rd_clk_enable.bits.push_back(State(port.clk_enable));
159 rd_clk_polarity.bits.push_back(State(port.clk_polarity));
160 rd_ce_over_srst.bits.push_back(State(port.ce_over_srst));
161 rd_clk.append(port.clk);
162 rd_arst.append(port.arst);
163 rd_srst.append(port.srst);
164 rd_en.append(port.en);
165 SigSpec addr = port.sub_addr(sub);
166 addr.extend_u0(abits, false);
167 rd_addr.append(addr);
168 log_assert(GetSize(addr) == abits);
169 for (auto idx : wr_port_xlat) {
170 rd_transparency_mask.bits.push_back(State(bool(port.transparency_mask[idx])));
171 rd_collision_x_mask.bits.push_back(State(bool(port.collision_x_mask[idx])));
172 }
173 }
174 rd_data.append(port.data);
175 for (auto &bit : port.arst_value)
176 rd_arst_value.bits.push_back(bit);
177 for (auto &bit : port.srst_value)
178 rd_srst_value.bits.push_back(bit);
179 for (auto &bit : port.init_value)
180 rd_init_value.bits.push_back(bit);
181 }
182 if (rd_ports.empty()) {
183 rd_wide_continuation = State::S0;
184 rd_clk_enable = State::S0;
185 rd_clk_polarity = State::S0;
186 rd_ce_over_srst = State::S0;
187 rd_arst_value = State::S0;
188 rd_srst_value = State::S0;
189 rd_init_value = State::S0;
190 }
191 if (rd_ports.empty() || wr_ports.empty()) {
192 rd_transparency_mask = State::S0;
193 rd_collision_x_mask = State::S0;
194 }
195 cell->parameters[ID::RD_PORTS] = Const(GetSize(rd_clk));
196 cell->parameters[ID::RD_CLK_ENABLE] = rd_clk_enable;
197 cell->parameters[ID::RD_CLK_POLARITY] = rd_clk_polarity;
198 cell->parameters[ID::RD_TRANSPARENCY_MASK] = rd_transparency_mask;
199 cell->parameters[ID::RD_COLLISION_X_MASK] = rd_collision_x_mask;
200 cell->parameters[ID::RD_WIDE_CONTINUATION] = rd_wide_continuation;
201 cell->parameters[ID::RD_CE_OVER_SRST] = rd_ce_over_srst;
202 cell->parameters[ID::RD_ARST_VALUE] = rd_arst_value;
203 cell->parameters[ID::RD_SRST_VALUE] = rd_srst_value;
204 cell->parameters[ID::RD_INIT_VALUE] = rd_init_value;
205 cell->setPort(ID::RD_CLK, rd_clk);
206 cell->setPort(ID::RD_EN, rd_en);
207 cell->setPort(ID::RD_ARST, rd_arst);
208 cell->setPort(ID::RD_SRST, rd_srst);
209 cell->setPort(ID::RD_ADDR, rd_addr);
210 cell->setPort(ID::RD_DATA, rd_data);
211 for (auto &port : wr_ports) {
212 if (port.cell) {
213 module->remove(port.cell);
214 port.cell = nullptr;
215 }
216 for (int sub = 0; sub < (1 << port.wide_log2); sub++)
217 {
218 wr_wide_continuation.bits.push_back(State(sub != 0));
219 wr_clk_enable.bits.push_back(State(port.clk_enable));
220 wr_clk_polarity.bits.push_back(State(port.clk_polarity));
221 wr_clk.append(port.clk);
222 for (auto idx : wr_port_xlat)
223 wr_priority_mask.bits.push_back(State(bool(port.priority_mask[idx])));
224 SigSpec addr = port.sub_addr(sub);
225 addr.extend_u0(abits, false);
226 wr_addr.append(addr);
227 log_assert(GetSize(addr) == abits);
228 }
229 wr_en.append(port.en);
230 wr_data.append(port.data);
231 }
232 if (wr_ports.empty()) {
233 wr_wide_continuation = State::S0;
234 wr_clk_enable = State::S0;
235 wr_clk_polarity = State::S0;
236 wr_priority_mask = State::S0;
237 }
238 cell->parameters[ID::WR_PORTS] = Const(GetSize(wr_clk));
239 cell->parameters[ID::WR_CLK_ENABLE] = wr_clk_enable;
240 cell->parameters[ID::WR_CLK_POLARITY] = wr_clk_polarity;
241 cell->parameters[ID::WR_PRIORITY_MASK] = wr_priority_mask;
242 cell->parameters[ID::WR_WIDE_CONTINUATION] = wr_wide_continuation;
243 cell->setPort(ID::WR_CLK, wr_clk);
244 cell->setPort(ID::WR_EN, wr_en);
245 cell->setPort(ID::WR_ADDR, wr_addr);
246 cell->setPort(ID::WR_DATA, wr_data);
247 for (auto &init : inits) {
248 if (init.cell) {
249 module->remove(init.cell);
250 init.cell = nullptr;
251 }
252 }
253 cell->parameters[ID::INIT] = get_init_data();
254 } else {
255 if (cell) {
256 module->remove(cell);
257 cell = nullptr;
258 }
259 if (!mem) {
260 if (memid.empty())
261 memid = NEW_ID;
262 mem = new RTLIL::Memory;
263 mem->name = memid;
264 module->memories[memid] = mem;
265 }
266 mem->width = width;
267 mem->start_offset = start_offset;
268 mem->size = size;
269 mem->attributes = attributes;
270 for (auto &port : rd_ports) {
271 if (!port.cell)
272 port.cell = module->addCell(NEW_ID, ID($memrd_v2));
273 port.cell->type = ID($memrd_v2);
274 port.cell->attributes = port.attributes;
275 port.cell->parameters[ID::MEMID] = memid.str();
276 port.cell->parameters[ID::ABITS] = GetSize(port.addr);
277 port.cell->parameters[ID::WIDTH] = width << port.wide_log2;
278 port.cell->parameters[ID::CLK_ENABLE] = port.clk_enable;
279 port.cell->parameters[ID::CLK_POLARITY] = port.clk_polarity;
280 port.cell->parameters[ID::CE_OVER_SRST] = port.ce_over_srst;
281 port.cell->parameters[ID::ARST_VALUE] = port.arst_value;
282 port.cell->parameters[ID::SRST_VALUE] = port.srst_value;
283 port.cell->parameters[ID::INIT_VALUE] = port.init_value;
284 port.cell->parameters[ID::TRANSPARENCY_MASK] = port.transparency_mask;
285 port.cell->parameters[ID::COLLISION_X_MASK] = port.collision_x_mask;
286 port.cell->parameters.erase(ID::TRANSPARENT);
287 port.cell->setPort(ID::CLK, port.clk);
288 port.cell->setPort(ID::EN, port.en);
289 port.cell->setPort(ID::ARST, port.arst);
290 port.cell->setPort(ID::SRST, port.srst);
291 port.cell->setPort(ID::ADDR, port.addr);
292 port.cell->setPort(ID::DATA, port.data);
293 }
294 int idx = 0;
295 for (auto &port : wr_ports) {
296 if (!port.cell)
297 port.cell = module->addCell(NEW_ID, ID($memwr_v2));
298 port.cell->type = ID($memwr_v2);
299 port.cell->attributes = port.attributes;
300 if (port.cell->parameters.count(ID::PRIORITY))
301 port.cell->parameters.erase(ID::PRIORITY);
302 port.cell->parameters[ID::MEMID] = memid.str();
303 port.cell->parameters[ID::ABITS] = GetSize(port.addr);
304 port.cell->parameters[ID::WIDTH] = width << port.wide_log2;
305 port.cell->parameters[ID::CLK_ENABLE] = port.clk_enable;
306 port.cell->parameters[ID::CLK_POLARITY] = port.clk_polarity;
307 port.cell->parameters[ID::PORTID] = idx++;
308 port.cell->parameters[ID::PRIORITY_MASK] = port.priority_mask;
309 port.cell->setPort(ID::CLK, port.clk);
310 port.cell->setPort(ID::EN, port.en);
311 port.cell->setPort(ID::ADDR, port.addr);
312 port.cell->setPort(ID::DATA, port.data);
313 }
314 idx = 0;
315 for (auto &init : inits) {
316 bool v2 = !init.en.is_fully_ones();
317 if (!init.cell)
318 init.cell = module->addCell(NEW_ID, v2 ? ID($meminit_v2) : ID($meminit));
319 else
320 init.cell->type = v2 ? ID($meminit_v2) : ID($meminit);
321 init.cell->attributes = init.attributes;
322 init.cell->parameters[ID::MEMID] = memid.str();
323 init.cell->parameters[ID::ABITS] = GetSize(init.addr);
324 init.cell->parameters[ID::WIDTH] = width;
325 init.cell->parameters[ID::WORDS] = GetSize(init.data) / width;
326 init.cell->parameters[ID::PRIORITY] = idx++;
327 init.cell->setPort(ID::ADDR, init.addr);
328 init.cell->setPort(ID::DATA, init.data);
329 if (v2)
330 init.cell->setPort(ID::EN, init.en);
331 else
332 init.cell->unsetPort(ID::EN);
333 }
334 }
335 }
336
337 void Mem::clear_inits() {
338 for (auto &init : inits)
339 init.removed = true;
340 }
341
342 void Mem::coalesce_inits() {
343 // start address -> end address
344 std::map<int, int> chunks;
345 // Figure out chunk boundaries.
346 for (auto &init : inits) {
347 if (init.removed)
348 continue;
349 bool valid = false;
350 for (auto bit : init.en)
351 if (bit == State::S1)
352 valid = true;
353 if (!valid) {
354 init.removed = true;
355 continue;
356 }
357 int addr = init.addr.as_int();
358 int addr_e = addr + GetSize(init.data) / width;
359 auto it_e = chunks.upper_bound(addr_e);
360 auto it = it_e;
361 while (it != chunks.begin()) {
362 --it;
363 if (it->second < addr) {
364 ++it;
365 break;
366 }
367 }
368 if (it == it_e) {
369 // No overlapping inits — add this one to index.
370 chunks[addr] = addr_e;
371 } else {
372 // We have an overlap — all chunks in the [it, it_e)
373 // range will be merged with this init.
374 if (it->first < addr)
375 addr = it->first;
376 auto it_last = it_e;
377 it_last--;
378 if (it_last->second > addr_e)
379 addr_e = it_last->second;
380 chunks.erase(it, it_e);
381 chunks[addr] = addr_e;
382 }
383 }
384 // Group inits by the chunk they belong to.
385 dict<int, std::vector<int>> inits_by_chunk;
386 for (int i = 0; i < GetSize(inits); i++) {
387 auto &init = inits[i];
388 if (init.removed)
389 continue;
390 auto it = chunks.upper_bound(init.addr.as_int());
391 --it;
392 inits_by_chunk[it->first].push_back(i);
393 int addr = init.addr.as_int();
394 int addr_e = addr + GetSize(init.data) / width;
395 log_assert(addr >= it->first && addr_e <= it->second);
396 }
397 // Process each chunk.
398 for (auto &it : inits_by_chunk) {
399 int caddr = it.first;
400 int caddr_e = chunks[caddr];
401 auto &chunk_inits = it.second;
402 if (GetSize(chunk_inits) == 1) {
403 auto &init = inits[chunk_inits[0]];
404 if (!init.en.is_fully_ones()) {
405 for (int i = 0; i < GetSize(init.data); i++)
406 if (init.en[i % width] != State::S1)
407 init.data[i] = State::Sx;
408 init.en = Const(State::S1, width);
409 }
410 continue;
411 }
412 Const cdata(State::Sx, (caddr_e - caddr) * width);
413 for (int idx : chunk_inits) {
414 auto &init = inits[idx];
415 int offset = (init.addr.as_int() - caddr) * width;
416 log_assert(offset >= 0);
417 log_assert(offset + GetSize(init.data) <= GetSize(cdata));
418 for (int i = 0; i < GetSize(init.data); i++)
419 if (init.en[i % width] == State::S1)
420 cdata.bits[i+offset] = init.data.bits[i];
421 init.removed = true;
422 }
423 MemInit new_init;
424 new_init.addr = caddr;
425 new_init.data = cdata;
426 new_init.en = Const(State::S1, width);
427 inits.push_back(new_init);
428 }
429 }
430
431 Const Mem::get_init_data() const {
432 Const init_data(State::Sx, width * size);
433 for (auto &init : inits) {
434 if (init.removed)
435 continue;
436 int offset = (init.addr.as_int() - start_offset) * width;
437 for (int i = 0; i < GetSize(init.data); i++)
438 if (0 <= i+offset && i+offset < GetSize(init_data) && init.en[i % width] == State::S1)
439 init_data.bits[i+offset] = init.data.bits[i];
440 }
441 return init_data;
442 }
443
444 void Mem::check() {
445 int max_wide_log2 = 0;
446 for (auto &port : rd_ports) {
447 if (port.removed)
448 continue;
449 log_assert(GetSize(port.clk) == 1);
450 log_assert(GetSize(port.en) == 1);
451 log_assert(GetSize(port.arst) == 1);
452 log_assert(GetSize(port.srst) == 1);
453 log_assert(GetSize(port.data) == (width << port.wide_log2));
454 log_assert(GetSize(port.init_value) == (width << port.wide_log2));
455 log_assert(GetSize(port.arst_value) == (width << port.wide_log2));
456 log_assert(GetSize(port.srst_value) == (width << port.wide_log2));
457 if (!port.clk_enable) {
458 log_assert(port.en == State::S1);
459 log_assert(port.arst == State::S0);
460 log_assert(port.srst == State::S0);
461 }
462 for (int j = 0; j < port.wide_log2; j++) {
463 log_assert(port.addr[j] == State::S0);
464 }
465 max_wide_log2 = std::max(max_wide_log2, port.wide_log2);
466 log_assert(GetSize(port.transparency_mask) == GetSize(wr_ports));
467 log_assert(GetSize(port.collision_x_mask) == GetSize(wr_ports));
468 for (int j = 0; j < GetSize(wr_ports); j++) {
469 auto &wport = wr_ports[j];
470 if ((port.transparency_mask[j] || port.collision_x_mask[j]) && !wport.removed) {
471 log_assert(port.clk_enable);
472 log_assert(wport.clk_enable);
473 log_assert(port.clk == wport.clk);
474 log_assert(port.clk_polarity == wport.clk_polarity);
475 }
476 log_assert(!port.transparency_mask[j] || !port.collision_x_mask[j]);
477 }
478 }
479 for (int i = 0; i < GetSize(wr_ports); i++) {
480 auto &port = wr_ports[i];
481 if (port.removed)
482 continue;
483 log_assert(GetSize(port.clk) == 1);
484 log_assert(GetSize(port.en) == (width << port.wide_log2));
485 log_assert(GetSize(port.data) == (width << port.wide_log2));
486 for (int j = 0; j < port.wide_log2; j++) {
487 log_assert(port.addr[j] == State::S0);
488 }
489 max_wide_log2 = std::max(max_wide_log2, port.wide_log2);
490 log_assert(GetSize(port.priority_mask) == GetSize(wr_ports));
491 for (int j = 0; j < GetSize(wr_ports); j++) {
492 auto &wport = wr_ports[j];
493 if (port.priority_mask[j] && !wport.removed) {
494 log_assert(j < i);
495 log_assert(port.clk_enable == wport.clk_enable);
496 if (port.clk_enable) {
497 log_assert(port.clk == wport.clk);
498 log_assert(port.clk_polarity == wport.clk_polarity);
499 }
500 }
501 }
502 }
503 int mask = (1 << max_wide_log2) - 1;
504 log_assert(!(start_offset & mask));
505 log_assert(!(size & mask));
506 }
507
508 namespace {
509
510 struct MemIndex {
511 dict<IdString, pool<Cell *>> rd_ports;
512 dict<IdString, pool<Cell *>> wr_ports;
513 dict<IdString, pool<Cell *>> inits;
514 MemIndex (Module *module) {
515 for (auto cell: module->cells()) {
516 if (cell->type.in(ID($memwr), ID($memwr_v2)))
517 wr_ports[cell->parameters.at(ID::MEMID).decode_string()].insert(cell);
518 else if (cell->type.in(ID($memrd), ID($memrd_v2)))
519 rd_ports[cell->parameters.at(ID::MEMID).decode_string()].insert(cell);
520 else if (cell->type.in(ID($meminit), ID($meminit_v2)))
521 inits[cell->parameters.at(ID::MEMID).decode_string()].insert(cell);
522 }
523 }
524 };
525
526 Mem mem_from_memory(Module *module, RTLIL::Memory *mem, const MemIndex &index) {
527 Mem res(module, mem->name, mem->width, mem->start_offset, mem->size);
528 res.packed = false;
529 res.mem = mem;
530 res.attributes = mem->attributes;
531 std::vector<bool> rd_transparent;
532 std::vector<int> wr_portid;
533 if (index.rd_ports.count(mem->name)) {
534 for (auto cell : index.rd_ports.at(mem->name)) {
535 MemRd mrd;
536 bool is_compat = cell->type == ID($memrd);
537 mrd.cell = cell;
538 mrd.attributes = cell->attributes;
539 mrd.clk_enable = cell->parameters.at(ID::CLK_ENABLE).as_bool();
540 mrd.clk_polarity = cell->parameters.at(ID::CLK_POLARITY).as_bool();
541 mrd.clk = cell->getPort(ID::CLK);
542 mrd.en = cell->getPort(ID::EN);
543 mrd.addr = cell->getPort(ID::ADDR);
544 mrd.data = cell->getPort(ID::DATA);
545 mrd.wide_log2 = ceil_log2(GetSize(mrd.data) / mem->width);
546 bool transparent = false;
547 if (is_compat) {
548 transparent = cell->parameters.at(ID::TRANSPARENT).as_bool();
549 mrd.ce_over_srst = false;
550 mrd.arst_value = Const(State::Sx, mem->width << mrd.wide_log2);
551 mrd.srst_value = Const(State::Sx, mem->width << mrd.wide_log2);
552 mrd.init_value = Const(State::Sx, mem->width << mrd.wide_log2);
553 mrd.srst = State::S0;
554 mrd.arst = State::S0;
555 if (!mrd.clk_enable) {
556 // Fix some patterns that we'll allow for backwards compatibility,
557 // but don't want to see moving forwards: async transparent
558 // ports (inherently meaningless) and async ports without
559 // const 1 tied to EN bit (which may mean a latch in the future).
560 transparent = false;
561 if (mrd.en == State::Sx)
562 mrd.en = State::S1;
563 }
564 } else {
565 mrd.ce_over_srst = cell->parameters.at(ID::CE_OVER_SRST).as_bool();
566 mrd.arst_value = cell->parameters.at(ID::ARST_VALUE);
567 mrd.srst_value = cell->parameters.at(ID::SRST_VALUE);
568 mrd.init_value = cell->parameters.at(ID::INIT_VALUE);
569 mrd.arst = cell->getPort(ID::ARST);
570 mrd.srst = cell->getPort(ID::SRST);
571 }
572 res.rd_ports.push_back(mrd);
573 rd_transparent.push_back(transparent);
574 }
575 }
576 if (index.wr_ports.count(mem->name)) {
577 std::vector<std::pair<int, MemWr>> ports;
578 for (auto cell : index.wr_ports.at(mem->name)) {
579 MemWr mwr;
580 bool is_compat = cell->type == ID($memwr);
581 mwr.cell = cell;
582 mwr.attributes = cell->attributes;
583 mwr.clk_enable = cell->parameters.at(ID::CLK_ENABLE).as_bool();
584 mwr.clk_polarity = cell->parameters.at(ID::CLK_POLARITY).as_bool();
585 mwr.clk = cell->getPort(ID::CLK);
586 mwr.en = cell->getPort(ID::EN);
587 mwr.addr = cell->getPort(ID::ADDR);
588 mwr.data = cell->getPort(ID::DATA);
589 mwr.wide_log2 = ceil_log2(GetSize(mwr.data) / mem->width);
590 ports.push_back(std::make_pair(cell->parameters.at(is_compat ? ID::PRIORITY : ID::PORTID).as_int(), mwr));
591 }
592 std::sort(ports.begin(), ports.end(), [](const std::pair<int, MemWr> &a, const std::pair<int, MemWr> &b) { return a.first < b.first; });
593 for (auto &it : ports) {
594 res.wr_ports.push_back(it.second);
595 wr_portid.push_back(it.first);
596 }
597 for (int i = 0; i < GetSize(res.wr_ports); i++) {
598 auto &port = res.wr_ports[i];
599 bool is_compat = port.cell->type == ID($memwr);
600 if (is_compat) {
601 port.priority_mask.resize(GetSize(res.wr_ports));
602 for (int j = 0; j < i; j++) {
603 auto &oport = res.wr_ports[j];
604 if (port.clk_enable != oport.clk_enable)
605 continue;
606 if (port.clk_enable && port.clk != oport.clk)
607 continue;
608 if (port.clk_enable && port.clk_polarity != oport.clk_polarity)
609 continue;
610 port.priority_mask[j] = true;
611 }
612 } else {
613 Const orig_prio_mask = port.cell->parameters.at(ID::PRIORITY_MASK);
614 for (int orig_portid : wr_portid) {
615 bool has_prio = orig_portid < GetSize(orig_prio_mask) && orig_prio_mask[orig_portid] == State::S1;
616 port.priority_mask.push_back(has_prio);
617 }
618 }
619 }
620 }
621 if (index.inits.count(mem->name)) {
622 std::vector<std::pair<int, MemInit>> inits;
623 for (auto cell : index.inits.at(mem->name)) {
624 MemInit init;
625 init.cell = cell;
626 init.attributes = cell->attributes;
627 auto addr = cell->getPort(ID::ADDR);
628 auto data = cell->getPort(ID::DATA);
629 if (!addr.is_fully_const())
630 log_error("Non-constant address %s in memory initialization %s.\n", log_signal(addr), log_id(cell));
631 if (!data.is_fully_const())
632 log_error("Non-constant data %s in memory initialization %s.\n", log_signal(data), log_id(cell));
633 init.addr = addr.as_const();
634 init.data = data.as_const();
635 if (cell->type == ID($meminit_v2)) {
636 auto en = cell->getPort(ID::EN);
637 if (!en.is_fully_const())
638 log_error("Non-constant enable %s in memory initialization %s.\n", log_signal(en), log_id(cell));
639 init.en = en.as_const();
640 } else {
641 init.en = RTLIL::Const(State::S1, mem->width);
642 }
643 inits.push_back(std::make_pair(cell->parameters.at(ID::PRIORITY).as_int(), init));
644 }
645 std::sort(inits.begin(), inits.end(), [](const std::pair<int, MemInit> &a, const std::pair<int, MemInit> &b) { return a.first < b.first; });
646 for (auto &it : inits)
647 res.inits.push_back(it.second);
648 }
649 for (int i = 0; i < GetSize(res.rd_ports); i++) {
650 auto &port = res.rd_ports[i];
651 bool is_compat = port.cell->type == ID($memrd);
652 if (is_compat) {
653 port.transparency_mask.resize(GetSize(res.wr_ports));
654 port.collision_x_mask.resize(GetSize(res.wr_ports));
655 if (!rd_transparent[i])
656 continue;
657 if (!port.clk_enable)
658 continue;
659 for (int j = 0; j < GetSize(res.wr_ports); j++) {
660 auto &wport = res.wr_ports[j];
661 if (!wport.clk_enable)
662 continue;
663 if (port.clk != wport.clk)
664 continue;
665 if (port.clk_polarity != wport.clk_polarity)
666 continue;
667 port.transparency_mask[j] = true;
668 }
669 } else {
670 Const orig_trans_mask = port.cell->parameters.at(ID::TRANSPARENCY_MASK);
671 Const orig_cx_mask = port.cell->parameters.at(ID::COLLISION_X_MASK);
672 for (int orig_portid : wr_portid) {
673 port.transparency_mask.push_back(orig_portid < GetSize(orig_trans_mask) && orig_trans_mask[orig_portid] == State::S1);
674 port.collision_x_mask.push_back(orig_portid < GetSize(orig_cx_mask) && orig_cx_mask[orig_portid] == State::S1);
675 }
676 }
677 }
678 res.check();
679 return res;
680 }
681
682 Mem mem_from_cell(Cell *cell) {
683 Mem res(cell->module, cell->parameters.at(ID::MEMID).decode_string(),
684 cell->parameters.at(ID::WIDTH).as_int(),
685 cell->parameters.at(ID::OFFSET).as_int(),
686 cell->parameters.at(ID::SIZE).as_int()
687 );
688 bool is_compat = cell->type == ID($mem);
689 int abits = cell->parameters.at(ID::ABITS).as_int();
690 res.packed = true;
691 res.cell = cell;
692 res.attributes = cell->attributes;
693 Const &init = cell->parameters.at(ID::INIT);
694 if (!init.is_fully_undef()) {
695 int pos = 0;
696 while (pos < res.size) {
697 Const word = init.extract(pos * res.width, res.width, State::Sx);
698 if (word.is_fully_undef()) {
699 pos++;
700 } else {
701 int epos;
702 for (epos = pos; epos < res.size; epos++) {
703 Const eword = init.extract(epos * res.width, res.width, State::Sx);
704 if (eword.is_fully_undef())
705 break;
706 }
707 MemInit minit;
708 minit.addr = res.start_offset + pos;
709 minit.data = init.extract(pos * res.width, (epos - pos) * res.width, State::Sx);
710 minit.en = RTLIL::Const(State::S1, res.width);
711 res.inits.push_back(minit);
712 pos = epos;
713 }
714 }
715 }
716 int n_rd_ports = cell->parameters.at(ID::RD_PORTS).as_int();
717 int n_wr_ports = cell->parameters.at(ID::WR_PORTS).as_int();
718 Const rd_wide_continuation = is_compat ? Const(State::S0, n_rd_ports) : cell->parameters.at(ID::RD_WIDE_CONTINUATION);
719 Const wr_wide_continuation = is_compat ? Const(State::S0, n_wr_ports) : cell->parameters.at(ID::WR_WIDE_CONTINUATION);
720 for (int i = 0, ni; i < n_rd_ports; i = ni) {
721 ni = i + 1;
722 while (ni < n_rd_ports && rd_wide_continuation[ni] == State::S1)
723 ni++;
724 MemRd mrd;
725 mrd.wide_log2 = ceil_log2(ni - i);
726 log_assert(ni - i == (1 << mrd.wide_log2));
727 mrd.clk_enable = cell->parameters.at(ID::RD_CLK_ENABLE).extract(i, 1).as_bool();
728 mrd.clk_polarity = cell->parameters.at(ID::RD_CLK_POLARITY).extract(i, 1).as_bool();
729 mrd.clk = cell->getPort(ID::RD_CLK).extract(i, 1);
730 mrd.en = cell->getPort(ID::RD_EN).extract(i, 1);
731 mrd.addr = cell->getPort(ID::RD_ADDR).extract(i * abits, abits);
732 mrd.data = cell->getPort(ID::RD_DATA).extract(i * res.width, (ni - i) * res.width);
733 if (is_compat) {
734 mrd.ce_over_srst = false;
735 mrd.arst_value = Const(State::Sx, res.width << mrd.wide_log2);
736 mrd.srst_value = Const(State::Sx, res.width << mrd.wide_log2);
737 mrd.init_value = Const(State::Sx, res.width << mrd.wide_log2);
738 mrd.arst = State::S0;
739 mrd.srst = State::S0;
740 } else {
741 mrd.ce_over_srst = cell->parameters.at(ID::RD_CE_OVER_SRST).extract(i, 1).as_bool();
742 mrd.arst_value = cell->parameters.at(ID::RD_ARST_VALUE).extract(i * res.width, (ni - i) * res.width);
743 mrd.srst_value = cell->parameters.at(ID::RD_SRST_VALUE).extract(i * res.width, (ni - i) * res.width);
744 mrd.init_value = cell->parameters.at(ID::RD_INIT_VALUE).extract(i * res.width, (ni - i) * res.width);
745 mrd.arst = cell->getPort(ID::RD_ARST).extract(i, 1);
746 mrd.srst = cell->getPort(ID::RD_SRST).extract(i, 1);
747 }
748 if (!is_compat) {
749 Const transparency_mask = cell->parameters.at(ID::RD_TRANSPARENCY_MASK).extract(i * n_wr_ports, n_wr_ports);
750 Const collision_x_mask = cell->parameters.at(ID::RD_COLLISION_X_MASK).extract(i * n_wr_ports, n_wr_ports);
751 for (int j = 0; j < n_wr_ports; j++)
752 if (wr_wide_continuation[j] != State::S1) {
753 mrd.transparency_mask.push_back(transparency_mask[j] == State::S1);
754 mrd.collision_x_mask.push_back(collision_x_mask[j] == State::S1);
755 }
756 }
757 res.rd_ports.push_back(mrd);
758 }
759 for (int i = 0, ni; i < n_wr_ports; i = ni) {
760 ni = i + 1;
761 while (ni < n_wr_ports && wr_wide_continuation[ni] == State::S1)
762 ni++;
763 MemWr mwr;
764 mwr.wide_log2 = ceil_log2(ni - i);
765 log_assert(ni - i == (1 << mwr.wide_log2));
766 mwr.clk_enable = cell->parameters.at(ID::WR_CLK_ENABLE).extract(i, 1).as_bool();
767 mwr.clk_polarity = cell->parameters.at(ID::WR_CLK_POLARITY).extract(i, 1).as_bool();
768 mwr.clk = cell->getPort(ID::WR_CLK).extract(i, 1);
769 mwr.en = cell->getPort(ID::WR_EN).extract(i * res.width, (ni - i) * res.width);
770 mwr.addr = cell->getPort(ID::WR_ADDR).extract(i * abits, abits);
771 mwr.data = cell->getPort(ID::WR_DATA).extract(i * res.width, (ni - i) * res.width);
772 if (!is_compat) {
773 Const priority_mask = cell->parameters.at(ID::WR_PRIORITY_MASK).extract(i * n_wr_ports, n_wr_ports);
774 for (int j = 0; j < n_wr_ports; j++)
775 if (wr_wide_continuation[j] != State::S1)
776 mwr.priority_mask.push_back(priority_mask[j] == State::S1);
777 }
778 res.wr_ports.push_back(mwr);
779 }
780 if (is_compat) {
781 for (int i = 0; i < GetSize(res.wr_ports); i++) {
782 auto &port = res.wr_ports[i];
783 port.priority_mask.resize(GetSize(res.wr_ports));
784 for (int j = 0; j < i; j++) {
785 auto &oport = res.wr_ports[j];
786 if (port.clk_enable != oport.clk_enable)
787 continue;
788 if (port.clk_enable && port.clk != oport.clk)
789 continue;
790 if (port.clk_enable && port.clk_polarity != oport.clk_polarity)
791 continue;
792 port.priority_mask[j] = true;
793 }
794 }
795 for (int i = 0; i < GetSize(res.rd_ports); i++) {
796 auto &port = res.rd_ports[i];
797 port.transparency_mask.resize(GetSize(res.wr_ports));
798 port.collision_x_mask.resize(GetSize(res.wr_ports));
799 if (!cell->parameters.at(ID::RD_TRANSPARENT).extract(i, 1).as_bool())
800 continue;
801 if (!port.clk_enable)
802 continue;
803 for (int j = 0; j < GetSize(res.wr_ports); j++) {
804 auto &wport = res.wr_ports[j];
805 if (!wport.clk_enable)
806 continue;
807 if (port.clk != wport.clk)
808 continue;
809 if (port.clk_polarity != wport.clk_polarity)
810 continue;
811 port.transparency_mask[j] = true;
812 }
813 }
814 }
815 res.check();
816 return res;
817 }
818
819 }
820
821 std::vector<Mem> Mem::get_all_memories(Module *module) {
822 std::vector<Mem> res;
823 MemIndex index(module);
824 for (auto it: module->memories) {
825 res.push_back(mem_from_memory(module, it.second, index));
826 }
827 for (auto cell: module->cells()) {
828 if (cell->type.in(ID($mem), ID($mem_v2)))
829 res.push_back(mem_from_cell(cell));
830 }
831 return res;
832 }
833
834 std::vector<Mem> Mem::get_selected_memories(Module *module) {
835 std::vector<Mem> res;
836 MemIndex index(module);
837 for (auto it: module->memories) {
838 if (module->design->selected(module, it.second))
839 res.push_back(mem_from_memory(module, it.second, index));
840 }
841 for (auto cell: module->selected_cells()) {
842 if (cell->type.in(ID($mem), ID($mem_v2)))
843 res.push_back(mem_from_cell(cell));
844 }
845 return res;
846 }
847
848 Cell *Mem::extract_rdff(int idx, FfInitVals *initvals) {
849 MemRd &port = rd_ports[idx];
850
851 if (!port.clk_enable)
852 return nullptr;
853
854 Cell *c;
855
856 // There are two ways to handle rdff extraction when transparency is involved:
857 //
858 // - if all of the following conditions are true, put the FF on address input:
859 //
860 // - the port has no clock enable, no reset, and no initial value
861 // - the port is transparent wrt all write ports (implying they also share
862 // the clock domain)
863 //
864 // - otherwise, put the FF on the data output, and make bypass paths for
865 // all write ports wrt which this port is transparent
866 bool trans_use_addr = true;
867 for (int i = 0; i < GetSize(wr_ports); i++)
868 if (!port.transparency_mask[i] && !wr_ports[i].removed)
869 trans_use_addr = false;
870
871 // If there are no write ports at all, we could possibly use either way; do data
872 // FF in this case.
873 if (GetSize(wr_ports) == 0)
874 trans_use_addr = false;
875
876 if (port.en != State::S1 || port.srst != State::S0 || port.arst != State::S0 || !port.init_value.is_fully_undef())
877 trans_use_addr = false;
878
879 if (trans_use_addr)
880 {
881 // Do not put a register in front of constant address bits — this is both
882 // unnecessary and will break wide ports.
883 int width = 0;
884 for (int i = 0; i < GetSize(port.addr); i++)
885 if (port.addr[i].wire)
886 width++;
887
888 if (width)
889 {
890 SigSpec sig_q = module->addWire(stringf("$%s$rdreg[%d]$q", memid.c_str(), idx), width);
891 SigSpec sig_d;
892
893 int pos = 0;
894 for (int i = 0; i < GetSize(port.addr); i++)
895 if (port.addr[i].wire) {
896 sig_d.append(port.addr[i]);
897 port.addr[i] = sig_q[pos++];
898 }
899
900 c = module->addDff(stringf("$%s$rdreg[%d]", memid.c_str(), idx), port.clk, sig_d, sig_q, port.clk_polarity);
901 } else {
902 c = nullptr;
903 }
904 }
905 else
906 {
907 log_assert(port.arst == State::S0 || port.srst == State::S0);
908
909 SigSpec async_d = module->addWire(stringf("$%s$rdreg[%d]$d", memid.c_str(), idx), GetSize(port.data));
910 SigSpec sig_d = async_d;
911
912 for (int i = 0; i < GetSize(wr_ports); i++) {
913 auto &wport = wr_ports[i];
914 if (wport.removed)
915 continue;
916 if (port.transparency_mask[i] || port.collision_x_mask[i]) {
917 log_assert(wport.clk_enable);
918 log_assert(wport.clk == port.clk);
919 log_assert(wport.clk_enable == port.clk_enable);
920 int min_wide_log2 = std::min(port.wide_log2, wport.wide_log2);
921 int max_wide_log2 = std::max(port.wide_log2, wport.wide_log2);
922 bool wide_write = wport.wide_log2 > port.wide_log2;
923 for (int sub = 0; sub < (1 << max_wide_log2); sub += (1 << min_wide_log2)) {
924 SigSpec raddr = port.addr;
925 SigSpec waddr = wport.addr;
926 if (wide_write)
927 waddr = wport.sub_addr(sub);
928 else
929 raddr = port.sub_addr(sub);
930 SigSpec addr_eq;
931 if (raddr != waddr)
932 addr_eq = module->Eq(stringf("$%s$rdtransen[%d][%d][%d]$d", memid.c_str(), idx, i, sub), raddr, waddr);
933 int pos = 0;
934 int ewidth = width << min_wide_log2;
935 int wsub = wide_write ? sub : 0;
936 int rsub = wide_write ? 0 : sub;
937 while (pos < ewidth) {
938 int epos = pos;
939 while (epos < ewidth && wport.en[epos + wsub * width] == wport.en[pos + wsub * width])
940 epos++;
941 SigSpec cur = sig_d.extract(pos + rsub * width, epos-pos);
942 SigSpec other = port.transparency_mask[i] ? wport.data.extract(pos + wsub * width, epos-pos) : Const(State::Sx, epos-pos);
943 SigSpec cond;
944 if (raddr != waddr)
945 cond = module->And(stringf("$%s$rdtransgate[%d][%d][%d][%d]$d", memid.c_str(), idx, i, sub, pos), wport.en[pos + wsub * width], addr_eq);
946 else
947 cond = wport.en[pos + wsub * width];
948 SigSpec merged = module->Mux(stringf("$%s$rdtransmux[%d][%d][%d][%d]$d", memid.c_str(), idx, i, sub, pos), cur, other, cond);
949 sig_d.replace(pos + rsub * width, merged);
950 pos = epos;
951 }
952 }
953 }
954 }
955
956 IdString name = stringf("$%s$rdreg[%d]", memid.c_str(), idx);
957 FfData ff(initvals);
958 ff.width = GetSize(port.data);
959 ff.has_clk = true;
960 ff.sig_clk = port.clk;
961 ff.pol_clk = port.clk_polarity;
962 if (port.en != State::S1) {
963 ff.has_en = true;
964 ff.pol_en = true;
965 ff.sig_en = port.en;
966 }
967 if (port.arst != State::S0) {
968 ff.has_arst = true;
969 ff.pol_arst = true;
970 ff.sig_arst = port.arst;
971 ff.val_arst = port.arst_value;
972 }
973 if (port.srst != State::S0) {
974 ff.has_srst = true;
975 ff.pol_srst = true;
976 ff.sig_srst = port.srst;
977 ff.val_srst = port.srst_value;
978 ff.ce_over_srst = ff.has_en && port.ce_over_srst;
979 }
980 ff.sig_d = sig_d;
981 ff.sig_q = port.data;
982 ff.val_init = port.init_value;
983 port.data = async_d;
984 c = ff.emit(module, name);
985 }
986
987 log("Extracted %s FF from read port %d of %s.%s: %s\n", trans_use_addr ? "addr" : "data",
988 idx, log_id(module), log_id(memid), log_id(c));
989
990 port.en = State::S1;
991 port.clk = State::S0;
992 port.arst = State::S0;
993 port.srst = State::S0;
994 port.clk_enable = false;
995 port.clk_polarity = true;
996 port.ce_over_srst = false;
997 port.arst_value = Const(State::Sx, GetSize(port.data));
998 port.srst_value = Const(State::Sx, GetSize(port.data));
999 port.init_value = Const(State::Sx, GetSize(port.data));
1000
1001 for (int i = 0; i < GetSize(wr_ports); i++) {
1002 port.transparency_mask[i] = false;
1003 port.collision_x_mask[i] = false;
1004 }
1005
1006 return c;
1007 }
1008
1009 void Mem::narrow() {
1010 // NOTE: several passes depend on this function not modifying
1011 // the design at all until (and unless) emit() is called.
1012 // Be careful to preserve this.
1013 std::vector<MemRd> new_rd_ports;
1014 std::vector<MemWr> new_wr_ports;
1015 std::vector<std::pair<int, int>> new_rd_map;
1016 std::vector<std::pair<int, int>> new_wr_map;
1017 for (int i = 0; i < GetSize(rd_ports); i++) {
1018 auto &port = rd_ports[i];
1019 for (int sub = 0; sub < (1 << port.wide_log2); sub++) {
1020 new_rd_map.push_back(std::make_pair(i, sub));
1021 }
1022 }
1023 for (int i = 0; i < GetSize(wr_ports); i++) {
1024 auto &port = wr_ports[i];
1025 for (int sub = 0; sub < (1 << port.wide_log2); sub++) {
1026 new_wr_map.push_back(std::make_pair(i, sub));
1027 }
1028 }
1029 for (auto &it : new_rd_map) {
1030 MemRd &orig = rd_ports[it.first];
1031 MemRd port = orig;
1032 if (it.second != 0)
1033 port.cell = nullptr;
1034 if (port.wide_log2) {
1035 port.data = port.data.extract(it.second * width, width);
1036 port.init_value = port.init_value.extract(it.second * width, width);
1037 port.arst_value = port.arst_value.extract(it.second * width, width);
1038 port.srst_value = port.srst_value.extract(it.second * width, width);
1039 port.addr = port.sub_addr(it.second);
1040 port.wide_log2 = 0;
1041 }
1042 port.transparency_mask.clear();
1043 port.collision_x_mask.clear();
1044 for (auto &it2 : new_wr_map)
1045 port.transparency_mask.push_back(orig.transparency_mask[it2.first]);
1046 for (auto &it2 : new_wr_map)
1047 port.collision_x_mask.push_back(orig.collision_x_mask[it2.first]);
1048 new_rd_ports.push_back(port);
1049 }
1050 for (auto &it : new_wr_map) {
1051 MemWr &orig = wr_ports[it.first];
1052 MemWr port = orig;
1053 if (it.second != 0)
1054 port.cell = nullptr;
1055 if (port.wide_log2) {
1056 port.data = port.data.extract(it.second * width, width);
1057 port.en = port.en.extract(it.second * width, width);
1058 port.addr = port.sub_addr(it.second);
1059 port.wide_log2 = 0;
1060 }
1061 port.priority_mask.clear();
1062 for (auto &it2 : new_wr_map)
1063 port.priority_mask.push_back(orig.priority_mask[it2.first]);
1064 new_wr_ports.push_back(port);
1065 }
1066 std::swap(rd_ports, new_rd_ports);
1067 std::swap(wr_ports, new_wr_ports);
1068 }
1069
1070 void Mem::emulate_priority(int idx1, int idx2, FfInitVals *initvals)
1071 {
1072 auto &port1 = wr_ports[idx1];
1073 auto &port2 = wr_ports[idx2];
1074 if (!port2.priority_mask[idx1])
1075 return;
1076 for (int i = 0; i < GetSize(rd_ports); i++) {
1077 auto &rport = rd_ports[i];
1078 if (rport.removed)
1079 continue;
1080 if (rport.transparency_mask[idx1] && !(rport.transparency_mask[idx2] || rport.collision_x_mask[idx2]))
1081 emulate_transparency(idx1, i, initvals);
1082 }
1083 int min_wide_log2 = std::min(port1.wide_log2, port2.wide_log2);
1084 int max_wide_log2 = std::max(port1.wide_log2, port2.wide_log2);
1085 bool wide1 = port1.wide_log2 > port2.wide_log2;
1086 for (int sub = 0; sub < (1 << max_wide_log2); sub += (1 << min_wide_log2)) {
1087 SigSpec addr1 = port1.addr;
1088 SigSpec addr2 = port2.addr;
1089 if (wide1)
1090 addr1 = port1.sub_addr(sub);
1091 else
1092 addr2 = port2.sub_addr(sub);
1093 SigSpec addr_eq = module->Eq(NEW_ID, addr1, addr2);
1094 int ewidth = width << min_wide_log2;
1095 int sub1 = wide1 ? sub : 0;
1096 int sub2 = wide1 ? 0 : sub;
1097 dict<std::pair<SigBit, SigBit>, SigBit> cache;
1098 for (int pos = 0; pos < ewidth; pos++) {
1099 SigBit &en1 = port1.en[pos + sub1 * width];
1100 SigBit &en2 = port2.en[pos + sub2 * width];
1101 std::pair<SigBit, SigBit> key(en1, en2);
1102 if (cache.count(key)) {
1103 en1 = cache[key];
1104 } else {
1105 SigBit active2 = module->And(NEW_ID, addr_eq, en2);
1106 SigBit nactive2 = module->Not(NEW_ID, active2);
1107 en1 = cache[key] = module->And(NEW_ID, en1, nactive2);
1108 }
1109 }
1110 }
1111 port2.priority_mask[idx1] = false;
1112 }
1113
1114 void Mem::emulate_transparency(int widx, int ridx, FfInitVals *initvals) {
1115 auto &wport = wr_ports[widx];
1116 auto &rport = rd_ports[ridx];
1117 log_assert(rport.transparency_mask[widx]);
1118 // If other write ports have priority over this one, emulate their transparency too.
1119 for (int i = GetSize(wr_ports) - 1; i > widx; i--) {
1120 if (wr_ports[i].removed)
1121 continue;
1122 if (rport.transparency_mask[i] && wr_ports[i].priority_mask[widx])
1123 emulate_transparency(i, ridx, initvals);
1124 }
1125 int min_wide_log2 = std::min(rport.wide_log2, wport.wide_log2);
1126 int max_wide_log2 = std::max(rport.wide_log2, wport.wide_log2);
1127 bool wide_write = wport.wide_log2 > rport.wide_log2;
1128 // The write data FF doesn't need full reset/init behavior, as it'll be masked by
1129 // the mux whenever this would be relevant. It does, however, need to have the same
1130 // clock enable signal as the read port.
1131 SigSpec wdata_q = module->addWire(NEW_ID, GetSize(wport.data));
1132 module->addDffe(NEW_ID, rport.clk, rport.en, wport.data, wdata_q, rport.clk_polarity, true);
1133 for (int sub = 0; sub < (1 << max_wide_log2); sub += (1 << min_wide_log2)) {
1134 SigSpec raddr = rport.addr;
1135 SigSpec waddr = wport.addr;
1136 for (int j = min_wide_log2; j < max_wide_log2; j++)
1137 if (wide_write)
1138 waddr = wport.sub_addr(sub);
1139 else
1140 raddr = rport.sub_addr(sub);
1141 SigSpec addr_eq;
1142 if (raddr != waddr)
1143 addr_eq = module->Eq(NEW_ID, raddr, waddr);
1144 int pos = 0;
1145 int ewidth = width << min_wide_log2;
1146 int wsub = wide_write ? sub : 0;
1147 int rsub = wide_write ? 0 : sub;
1148 SigSpec rdata_a = module->addWire(NEW_ID, ewidth);
1149 while (pos < ewidth) {
1150 int epos = pos;
1151 while (epos < ewidth && wport.en[epos + wsub * width] == wport.en[pos + wsub * width])
1152 epos++;
1153 SigSpec cond;
1154 if (raddr != waddr)
1155 cond = module->And(NEW_ID, wport.en[pos + wsub * width], addr_eq);
1156 else
1157 cond = wport.en[pos + wsub * width];
1158 SigSpec cond_q = module->addWire(NEW_ID);
1159 // The FF for storing the bypass enable signal must be carefully
1160 // constructed to preserve the overall init/reset/enable behavior
1161 // of the whole port.
1162 FfData ff(initvals);
1163 ff.width = 1;
1164 ff.sig_q = cond_q;
1165 ff.has_d = true;
1166 ff.sig_d = cond;
1167 ff.has_clk = true;
1168 ff.sig_clk = rport.clk;
1169 ff.pol_clk = rport.clk_polarity;
1170 if (rport.en != State::S1) {
1171 ff.has_en = true;
1172 ff.sig_en = rport.en;
1173 ff.pol_en = true;
1174 }
1175 if (rport.arst != State::S0) {
1176 ff.has_arst = true;
1177 ff.sig_arst = rport.arst;
1178 ff.pol_arst = true;
1179 ff.val_arst = State::S0;
1180 }
1181 if (rport.srst != State::S0) {
1182 ff.has_srst = true;
1183 ff.sig_srst = rport.srst;
1184 ff.pol_srst = true;
1185 ff.val_srst = State::S0;
1186 ff.ce_over_srst = rport.ce_over_srst;
1187 }
1188 if (!rport.init_value.is_fully_undef())
1189 ff.val_init = State::S0;
1190 else
1191 ff.val_init = State::Sx;
1192 ff.emit(module, NEW_ID);
1193 // And the final bypass mux.
1194 SigSpec cur = rdata_a.extract(pos, epos-pos);
1195 SigSpec other = wdata_q.extract(pos + wsub * width, epos-pos);
1196 SigSpec dest = rport.data.extract(pos + rsub * width, epos-pos);
1197 module->addMux(NEW_ID, cur, other, cond_q, dest);
1198 pos = epos;
1199 }
1200 rport.data.replace(rsub * width, rdata_a);
1201 }
1202 rport.transparency_mask[widx] = false;
1203 rport.collision_x_mask[widx] = true;
1204 }
1205
1206 void Mem::prepare_wr_merge(int idx1, int idx2, FfInitVals *initvals) {
1207 log_assert(idx1 < idx2);
1208 auto &port1 = wr_ports[idx1];
1209 auto &port2 = wr_ports[idx2];
1210 // If port 2 has priority over a port before port 1, make port 1 have priority too.
1211 for (int i = 0; i < idx1; i++)
1212 if (port2.priority_mask[i])
1213 port1.priority_mask[i] = true;
1214 // If port 2 has priority over a port after port 1, emulate it.
1215 for (int i = idx1 + 1; i < idx2; i++)
1216 if (port2.priority_mask[i] && !wr_ports[i].removed)
1217 emulate_priority(i, idx2, initvals);
1218 // If some port had priority over port 2, make it have priority over the merged port too.
1219 for (int i = idx2 + 1; i < GetSize(wr_ports); i++) {
1220 auto &oport = wr_ports[i];
1221 if (oport.priority_mask[idx2])
1222 oport.priority_mask[idx1] = true;
1223 }
1224 // Make sure all read ports have identical collision/transparency behavior wrt both
1225 // ports.
1226 for (int i = 0; i < GetSize(rd_ports); i++) {
1227 auto &rport = rd_ports[i];
1228 if (rport.removed)
1229 continue;
1230 // If collision already undefined with both ports, it's fine.
1231 if (rport.collision_x_mask[idx1] && rport.collision_x_mask[idx2])
1232 continue;
1233 // If one port has undefined collision, change it to the behavior
1234 // of the other port.
1235 if (rport.collision_x_mask[idx1]) {
1236 rport.collision_x_mask[idx1] = false;
1237 rport.transparency_mask[idx1] = rport.transparency_mask[idx2];
1238 continue;
1239 }
1240 if (rport.collision_x_mask[idx2]) {
1241 rport.collision_x_mask[idx2] = false;
1242 rport.transparency_mask[idx2] = rport.transparency_mask[idx1];
1243 continue;
1244 }
1245 // If transparent with both ports, also fine.
1246 if (rport.transparency_mask[idx1] && rport.transparency_mask[idx2])
1247 continue;
1248 // If transparent with only one, emulate it, and remove the collision-X
1249 // flag that emulate_transparency will set (to align with the other port).
1250 if (rport.transparency_mask[idx1]) {
1251 emulate_transparency(i, idx1, initvals);
1252 rport.collision_x_mask[idx1] = false;
1253 continue;
1254 }
1255 if (rport.transparency_mask[idx2]) {
1256 emulate_transparency(i, idx2, initvals);
1257 rport.collision_x_mask[idx2] = false;
1258 continue;
1259 }
1260 // If we got here, it's transparent with neither port, which is fine.
1261 }
1262 }
1263
1264 void Mem::prepare_rd_merge(int idx1, int idx2, FfInitVals *initvals) {
1265 auto &port1 = rd_ports[idx1];
1266 auto &port2 = rd_ports[idx2];
1267 // Note that going through write ports in order is important, since
1268 // emulating transparency of a write port can change transparency
1269 // mask for higher-numbered ports (due to transitive transparency
1270 // emulation needed because of write port priority).
1271 for (int i = 0; i < GetSize(wr_ports); i++) {
1272 if (wr_ports[i].removed)
1273 continue;
1274 // Both ports undefined, OK.
1275 if (port1.collision_x_mask[i] && port2.collision_x_mask[i])
1276 continue;
1277 // Only one port undefined — change its behavior
1278 // to align with the other port.
1279 if (port1.collision_x_mask[i]) {
1280 port1.collision_x_mask[i] = false;
1281 port1.transparency_mask[i] = port2.transparency_mask[i];
1282 continue;
1283 }
1284 if (port2.collision_x_mask[i]) {
1285 port2.collision_x_mask[i] = false;
1286 port2.transparency_mask[i] = port1.transparency_mask[i];
1287 continue;
1288 }
1289 // Both ports transparent, OK.
1290 if (port1.transparency_mask[i] && port2.transparency_mask[i])
1291 continue;
1292 // Only one port transparent — emulate transparency
1293 // on the other.
1294 if (port1.transparency_mask[i]) {
1295 emulate_transparency(i, idx1, initvals);
1296 port1.collision_x_mask[i] = false;
1297 continue;
1298 }
1299 if (port2.transparency_mask[i]) {
1300 emulate_transparency(i, idx2, initvals);
1301 port2.collision_x_mask[i] = false;
1302 continue;
1303 }
1304 // No ports transparent, OK.
1305 }
1306
1307 }
1308
1309 void Mem::widen_prep(int wide_log2) {
1310 // Make sure start_offset and size are aligned to the port width,
1311 // adjust if necessary.
1312 int mask = ((1 << wide_log2) - 1);
1313 int delta = start_offset & mask;
1314 start_offset -= delta;
1315 size += delta;
1316 if (size & mask) {
1317 size |= mask;
1318 size++;
1319 }
1320 }
1321
1322 void Mem::widen_wr_port(int idx, int wide_log2) {
1323 widen_prep(wide_log2);
1324 auto &port = wr_ports[idx];
1325 log_assert(port.wide_log2 <= wide_log2);
1326 if (port.wide_log2 < wide_log2) {
1327 SigSpec new_data, new_en;
1328 SigSpec addr_lo = port.addr.extract(0, wide_log2);
1329 for (int sub = 0; sub < (1 << wide_log2); sub += (1 << port.wide_log2))
1330 {
1331 Const cur_addr_lo(sub, wide_log2);
1332 if (addr_lo == cur_addr_lo) {
1333 // Always writes to this subword.
1334 new_data.append(port.data);
1335 new_en.append(port.en);
1336 } else if (addr_lo.is_fully_const()) {
1337 // Never writes to this subword.
1338 new_data.append(Const(State::Sx, GetSize(port.data)));
1339 new_en.append(Const(State::S0, GetSize(port.data)));
1340 } else {
1341 // May or may not write to this subword.
1342 new_data.append(port.data);
1343 SigSpec addr_eq = module->Eq(NEW_ID, addr_lo, cur_addr_lo);
1344 SigSpec en = module->Mux(NEW_ID, Const(State::S0, GetSize(port.data)), port.en, addr_eq);
1345 new_en.append(en);
1346 }
1347 }
1348 port.addr.replace(port.wide_log2, Const(State::S0, wide_log2 - port.wide_log2));
1349 port.data = new_data;
1350 port.en = new_en;
1351 port.wide_log2 = wide_log2;
1352 }
1353 }