2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2020 Marcelina KoĆcielnicka <mwk@0x04.net>
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.
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.
20 #include "kernel/mem.h"
21 #include "kernel/ff.h"
31 module
->memories
.erase(mem
->name
);
35 for (auto &port
: rd_ports
) {
37 module
->remove(port
.cell
);
41 for (auto &port
: wr_ports
) {
43 module
->remove(port
.cell
);
47 for (auto &init
: inits
) {
49 module
->remove(init
.cell
);
57 std::vector
<int> rd_left
;
58 for (int i
= 0; i
< GetSize(rd_ports
); i
++) {
59 auto &port
= rd_ports
[i
];
62 module
->remove(port
.cell
);
68 std::vector
<int> wr_left
;
69 for (int i
= 0; i
< GetSize(wr_ports
); i
++) {
70 auto &port
= wr_ports
[i
];
73 module
->remove(port
.cell
);
79 for (int i
= 0; i
< GetSize(rd_left
); i
++)
81 std::swap(rd_ports
[i
], rd_ports
[rd_left
[i
]]);
82 rd_ports
.resize(GetSize(rd_left
));
83 for (int i
= 0; i
< GetSize(wr_left
); i
++)
85 std::swap(wr_ports
[i
], wr_ports
[wr_left
[i
]]);
86 wr_ports
.resize(GetSize(wr_left
));
88 // for future: handle transparency mask here
90 for (auto &port
: wr_ports
) {
91 for (int i
= 0; i
< GetSize(wr_left
); i
++)
92 port
.priority_mask
[i
] = port
.priority_mask
[wr_left
[i
]];
93 port
.priority_mask
.resize(GetSize(wr_left
));
98 module
->memories
.erase(mem
->name
);
105 cell
= module
->addCell(memid
, ID($mem
));
107 cell
->attributes
= attributes
;
108 cell
->parameters
[ID::MEMID
] = Const(memid
.str());
109 cell
->parameters
[ID::WIDTH
] = Const(width
);
110 cell
->parameters
[ID::OFFSET
] = Const(start_offset
);
111 cell
->parameters
[ID::SIZE
] = Const(size
);
112 Const rd_wide_continuation
, rd_clk_enable
, rd_clk_polarity
, rd_transparent
;
113 Const wr_wide_continuation
, wr_clk_enable
, wr_clk_polarity
;
114 SigSpec rd_clk
, rd_en
, rd_addr
, rd_data
;
115 SigSpec wr_clk
, wr_en
, wr_addr
, wr_data
;
117 for (auto &port
: rd_ports
)
118 abits
= std::max(abits
, GetSize(port
.addr
));
119 for (auto &port
: wr_ports
)
120 abits
= std::max(abits
, GetSize(port
.addr
));
121 cell
->parameters
[ID::ABITS
] = Const(abits
);
122 for (auto &port
: rd_ports
) {
124 log_assert(port
.arst
== State::S0
);
125 log_assert(port
.srst
== State::S0
);
126 log_assert(port
.init_value
== Const(State::Sx
, width
<< port
.wide_log2
));
128 module
->remove(port
.cell
);
131 for (int sub
= 0; sub
< (1 << port
.wide_log2
); sub
++)
133 rd_wide_continuation
.bits
.push_back(State(sub
!= 0));
134 rd_clk_enable
.bits
.push_back(State(port
.clk_enable
));
135 rd_clk_polarity
.bits
.push_back(State(port
.clk_polarity
));
136 rd_transparent
.bits
.push_back(State(port
.transparent
));
137 rd_clk
.append(port
.clk
);
138 rd_en
.append(port
.en
);
139 SigSpec addr
= port
.sub_addr(sub
);
140 addr
.extend_u0(abits
, false);
141 rd_addr
.append(addr
);
142 log_assert(GetSize(addr
) == abits
);
144 rd_data
.append(port
.data
);
146 if (rd_ports
.empty()) {
147 rd_wide_continuation
= State::S0
;
148 rd_clk_enable
= State::S0
;
149 rd_clk_polarity
= State::S0
;
150 rd_transparent
= State::S0
;
152 cell
->parameters
[ID::RD_PORTS
] = Const(GetSize(rd_clk
));
153 cell
->parameters
[ID::RD_CLK_ENABLE
] = rd_clk_enable
;
154 cell
->parameters
[ID::RD_CLK_POLARITY
] = rd_clk_polarity
;
155 cell
->parameters
[ID::RD_TRANSPARENT
] = rd_transparent
;
156 cell
->setPort(ID::RD_CLK
, rd_clk
);
157 cell
->setPort(ID::RD_EN
, rd_en
);
158 cell
->setPort(ID::RD_ADDR
, rd_addr
);
159 cell
->setPort(ID::RD_DATA
, rd_data
);
160 for (auto &port
: wr_ports
) {
162 module
->remove(port
.cell
);
165 for (int sub
= 0; sub
< (1 << port
.wide_log2
); sub
++)
167 wr_wide_continuation
.bits
.push_back(State(sub
!= 0));
168 wr_clk_enable
.bits
.push_back(State(port
.clk_enable
));
169 wr_clk_polarity
.bits
.push_back(State(port
.clk_polarity
));
170 wr_clk
.append(port
.clk
);
171 SigSpec addr
= port
.sub_addr(sub
);
172 addr
.extend_u0(abits
, false);
173 wr_addr
.append(addr
);
174 log_assert(GetSize(addr
) == abits
);
176 wr_en
.append(port
.en
);
177 wr_data
.append(port
.data
);
179 if (wr_ports
.empty()) {
180 wr_wide_continuation
= State::S0
;
181 wr_clk_enable
= State::S0
;
182 wr_clk_polarity
= State::S0
;
184 cell
->parameters
[ID::WR_PORTS
] = Const(GetSize(wr_clk
));
185 cell
->parameters
[ID::WR_CLK_ENABLE
] = wr_clk_enable
;
186 cell
->parameters
[ID::WR_CLK_POLARITY
] = wr_clk_polarity
;
187 cell
->setPort(ID::WR_CLK
, wr_clk
);
188 cell
->setPort(ID::WR_EN
, wr_en
);
189 cell
->setPort(ID::WR_ADDR
, wr_addr
);
190 cell
->setPort(ID::WR_DATA
, wr_data
);
191 for (auto &init
: inits
) {
193 module
->remove(init
.cell
);
197 cell
->parameters
[ID::INIT
] = get_init_data();
200 module
->remove(cell
);
206 mem
= new RTLIL::Memory
;
208 module
->memories
[memid
] = mem
;
211 mem
->start_offset
= start_offset
;
213 for (auto &port
: rd_ports
) {
215 log_assert(port
.arst
== State::S0
);
216 log_assert(port
.srst
== State::S0
);
217 log_assert(port
.init_value
== Const(State::Sx
, width
<< port
.wide_log2
));
219 port
.cell
= module
->addCell(NEW_ID
, ID($memrd
));
220 port
.cell
->parameters
[ID::MEMID
] = memid
.str();
221 port
.cell
->parameters
[ID::ABITS
] = GetSize(port
.addr
);
222 port
.cell
->parameters
[ID::WIDTH
] = width
<< port
.wide_log2
;
223 port
.cell
->parameters
[ID::CLK_ENABLE
] = port
.clk_enable
;
224 port
.cell
->parameters
[ID::CLK_POLARITY
] = port
.clk_polarity
;
225 port
.cell
->parameters
[ID::TRANSPARENT
] = port
.transparent
;
226 port
.cell
->setPort(ID::CLK
, port
.clk
);
227 port
.cell
->setPort(ID::EN
, port
.en
);
228 port
.cell
->setPort(ID::ADDR
, port
.addr
);
229 port
.cell
->setPort(ID::DATA
, port
.data
);
232 for (auto &port
: wr_ports
) {
234 port
.cell
= module
->addCell(NEW_ID
, ID($memwr
));
235 port
.cell
->parameters
[ID::MEMID
] = memid
.str();
236 port
.cell
->parameters
[ID::ABITS
] = GetSize(port
.addr
);
237 port
.cell
->parameters
[ID::WIDTH
] = width
<< port
.wide_log2
;
238 port
.cell
->parameters
[ID::CLK_ENABLE
] = port
.clk_enable
;
239 port
.cell
->parameters
[ID::CLK_POLARITY
] = port
.clk_polarity
;
240 port
.cell
->parameters
[ID::PRIORITY
] = idx
++;
241 port
.cell
->setPort(ID::CLK
, port
.clk
);
242 port
.cell
->setPort(ID::EN
, port
.en
);
243 port
.cell
->setPort(ID::ADDR
, port
.addr
);
244 port
.cell
->setPort(ID::DATA
, port
.data
);
247 for (auto &init
: inits
) {
249 init
.cell
= module
->addCell(NEW_ID
, ID($meminit
));
250 init
.cell
->parameters
[ID::MEMID
] = memid
.str();
251 init
.cell
->parameters
[ID::ABITS
] = GetSize(init
.addr
);
252 init
.cell
->parameters
[ID::WIDTH
] = width
;
253 init
.cell
->parameters
[ID::WORDS
] = GetSize(init
.data
) / width
;
254 init
.cell
->parameters
[ID::PRIORITY
] = idx
++;
255 init
.cell
->setPort(ID::ADDR
, init
.addr
);
256 init
.cell
->setPort(ID::DATA
, init
.data
);
261 void Mem::clear_inits() {
262 for (auto &init
: inits
)
264 module
->remove(init
.cell
);
268 Const
Mem::get_init_data() const {
269 Const
init_data(State::Sx
, width
* size
);
270 for (auto &init
: inits
) {
271 int offset
= (init
.addr
.as_int() - start_offset
) * width
;
272 for (int i
= 0; i
< GetSize(init
.data
); i
++)
273 if (0 <= i
+offset
&& i
+offset
< GetSize(init_data
))
274 init_data
.bits
[i
+offset
] = init
.data
.bits
[i
];
280 int max_wide_log2
= 0;
281 for (auto &port
: rd_ports
) {
284 log_assert(GetSize(port
.clk
) == 1);
285 log_assert(GetSize(port
.en
) == 1);
286 log_assert(GetSize(port
.arst
) == 1);
287 log_assert(GetSize(port
.srst
) == 1);
288 log_assert(GetSize(port
.data
) == (width
<< port
.wide_log2
));
289 log_assert(GetSize(port
.init_value
) == (width
<< port
.wide_log2
));
290 log_assert(GetSize(port
.arst_value
) == (width
<< port
.wide_log2
));
291 log_assert(GetSize(port
.srst_value
) == (width
<< port
.wide_log2
));
292 if (!port
.clk_enable
) {
293 log_assert(!port
.transparent
);
294 log_assert(port
.en
== State::S1
);
295 log_assert(port
.arst
== State::S0
);
296 log_assert(port
.srst
== State::S0
);
298 for (int j
= 0; j
< port
.wide_log2
; j
++) {
299 log_assert(port
.addr
[j
] == State::S0
);
301 max_wide_log2
= std::max(max_wide_log2
, port
.wide_log2
);
303 for (int i
= 0; i
< GetSize(wr_ports
); i
++) {
304 auto &port
= wr_ports
[i
];
307 log_assert(GetSize(port
.clk
) == 1);
308 log_assert(GetSize(port
.en
) == (width
<< port
.wide_log2
));
309 log_assert(GetSize(port
.data
) == (width
<< port
.wide_log2
));
310 for (int j
= 0; j
< port
.wide_log2
; j
++) {
311 log_assert(port
.addr
[j
] == State::S0
);
313 max_wide_log2
= std::max(max_wide_log2
, port
.wide_log2
);
314 log_assert(GetSize(port
.priority_mask
) == GetSize(wr_ports
));
315 for (int j
= 0; j
< GetSize(wr_ports
); j
++) {
316 auto &wport
= wr_ports
[j
];
317 if (port
.priority_mask
[j
] && !wport
.removed
) {
319 log_assert(port
.clk_enable
== wport
.clk_enable
);
320 if (port
.clk_enable
) {
321 log_assert(port
.clk
== wport
.clk
);
322 log_assert(port
.clk_polarity
== wport
.clk_polarity
);
327 int mask
= (1 << max_wide_log2
) - 1;
328 log_assert(!(start_offset
& mask
));
329 log_assert(!(size
& mask
));
335 dict
<IdString
, pool
<Cell
*>> rd_ports
;
336 dict
<IdString
, pool
<Cell
*>> wr_ports
;
337 dict
<IdString
, pool
<Cell
*>> inits
;
338 MemIndex (Module
*module
) {
339 for (auto cell
: module
->cells()) {
340 if (cell
->type
== ID($memwr
))
341 wr_ports
[cell
->parameters
.at(ID::MEMID
).decode_string()].insert(cell
);
342 else if (cell
->type
== ID($memrd
))
343 rd_ports
[cell
->parameters
.at(ID::MEMID
).decode_string()].insert(cell
);
344 else if (cell
->type
== ID($meminit
))
345 inits
[cell
->parameters
.at(ID::MEMID
).decode_string()].insert(cell
);
350 Mem
mem_from_memory(Module
*module
, RTLIL::Memory
*mem
, const MemIndex
&index
) {
351 Mem
res(module
, mem
->name
, mem
->width
, mem
->start_offset
, mem
->size
);
354 res
.attributes
= mem
->attributes
;
355 if (index
.rd_ports
.count(mem
->name
)) {
356 for (auto cell
: index
.rd_ports
.at(mem
->name
)) {
359 mrd
.attributes
= cell
->attributes
;
360 mrd
.clk_enable
= cell
->parameters
.at(ID::CLK_ENABLE
).as_bool();
361 mrd
.clk_polarity
= cell
->parameters
.at(ID::CLK_POLARITY
).as_bool();
362 mrd
.transparent
= cell
->parameters
.at(ID::TRANSPARENT
).as_bool();
363 mrd
.clk
= cell
->getPort(ID::CLK
);
364 mrd
.en
= cell
->getPort(ID::EN
);
365 mrd
.addr
= cell
->getPort(ID::ADDR
);
366 mrd
.data
= cell
->getPort(ID::DATA
);
367 mrd
.wide_log2
= ceil_log2(GetSize(mrd
.data
) / mem
->width
);
368 mrd
.ce_over_srst
= false;
369 mrd
.arst_value
= Const(State::Sx
, mem
->width
<< mrd
.wide_log2
);
370 mrd
.srst_value
= Const(State::Sx
, mem
->width
<< mrd
.wide_log2
);
371 mrd
.init_value
= Const(State::Sx
, mem
->width
<< mrd
.wide_log2
);
372 mrd
.srst
= State::S0
;
373 mrd
.arst
= State::S0
;
374 if (!mrd
.clk_enable
) {
375 // Fix some patterns that we'll allow for backwards compatibility,
376 // but don't want to see moving forwards: async transparent
377 // ports (inherently meaningless) and async ports without
378 // const 1 tied to EN bit (which may mean a latch in the future).
379 mrd
.transparent
= false;
380 if (mrd
.en
== State::Sx
)
383 res
.rd_ports
.push_back(mrd
);
386 if (index
.wr_ports
.count(mem
->name
)) {
387 std::vector
<std::pair
<int, MemWr
>> ports
;
388 for (auto cell
: index
.wr_ports
.at(mem
->name
)) {
391 mwr
.attributes
= cell
->attributes
;
392 mwr
.clk_enable
= cell
->parameters
.at(ID::CLK_ENABLE
).as_bool();
393 mwr
.clk_polarity
= cell
->parameters
.at(ID::CLK_POLARITY
).as_bool();
394 mwr
.clk
= cell
->getPort(ID::CLK
);
395 mwr
.en
= cell
->getPort(ID::EN
);
396 mwr
.addr
= cell
->getPort(ID::ADDR
);
397 mwr
.data
= cell
->getPort(ID::DATA
);
398 mwr
.wide_log2
= ceil_log2(GetSize(mwr
.data
) / mem
->width
);
399 ports
.push_back(std::make_pair(cell
->parameters
.at(ID::PRIORITY
).as_int(), mwr
));
401 std::sort(ports
.begin(), ports
.end(), [](const std::pair
<int, MemWr
> &a
, const std::pair
<int, MemWr
> &b
) { return a
.first
< b
.first
; });
402 for (auto &it
: ports
)
403 res
.wr_ports
.push_back(it
.second
);
405 if (index
.inits
.count(mem
->name
)) {
406 std::vector
<std::pair
<int, MemInit
>> inits
;
407 for (auto cell
: index
.inits
.at(mem
->name
)) {
410 init
.attributes
= cell
->attributes
;
411 auto addr
= cell
->getPort(ID::ADDR
);
412 auto data
= cell
->getPort(ID::DATA
);
413 if (!addr
.is_fully_const())
414 log_error("Non-constant address %s in memory initialization %s.\n", log_signal(addr
), log_id(cell
));
415 if (!data
.is_fully_const())
416 log_error("Non-constant data %s in memory initialization %s.\n", log_signal(data
), log_id(cell
));
417 init
.addr
= addr
.as_const();
418 init
.data
= data
.as_const();
419 inits
.push_back(std::make_pair(cell
->parameters
.at(ID::PRIORITY
).as_int(), init
));
421 std::sort(inits
.begin(), inits
.end(), [](const std::pair
<int, MemInit
> &a
, const std::pair
<int, MemInit
> &b
) { return a
.first
< b
.first
; });
422 for (auto &it
: inits
)
423 res
.inits
.push_back(it
.second
);
425 for (int i
= 0; i
< GetSize(res
.wr_ports
); i
++) {
426 auto &port
= res
.wr_ports
[i
];
427 port
.priority_mask
.resize(GetSize(res
.wr_ports
));
428 for (int j
= 0; j
< i
; j
++) {
429 auto &oport
= res
.wr_ports
[j
];
430 if (port
.clk_enable
!= oport
.clk_enable
)
432 if (port
.clk_enable
&& port
.clk
!= oport
.clk
)
434 if (port
.clk_enable
&& port
.clk_polarity
!= oport
.clk_polarity
)
436 port
.priority_mask
[j
] = true;
443 Mem
mem_from_cell(Cell
*cell
) {
444 Mem
res(cell
->module
, cell
->parameters
.at(ID::MEMID
).decode_string(),
445 cell
->parameters
.at(ID::WIDTH
).as_int(),
446 cell
->parameters
.at(ID::OFFSET
).as_int(),
447 cell
->parameters
.at(ID::SIZE
).as_int()
449 int abits
= cell
->parameters
.at(ID::ABITS
).as_int();
452 res
.attributes
= cell
->attributes
;
453 Const
&init
= cell
->parameters
.at(ID::INIT
);
454 if (!init
.is_fully_undef()) {
456 while (pos
< res
.size
) {
457 Const word
= init
.extract(pos
* res
.width
, res
.width
, State::Sx
);
458 if (word
.is_fully_undef()) {
462 for (epos
= pos
; epos
< res
.size
; epos
++) {
463 Const eword
= init
.extract(epos
* res
.width
, res
.width
, State::Sx
);
464 if (eword
.is_fully_undef())
468 minit
.addr
= res
.start_offset
+ pos
;
469 minit
.data
= init
.extract(pos
* res
.width
, (epos
- pos
) * res
.width
, State::Sx
);
470 res
.inits
.push_back(minit
);
475 for (int i
= 0; i
< cell
->parameters
.at(ID::RD_PORTS
).as_int(); i
++) {
478 mrd
.clk_enable
= cell
->parameters
.at(ID::RD_CLK_ENABLE
).extract(i
, 1).as_bool();
479 mrd
.clk_polarity
= cell
->parameters
.at(ID::RD_CLK_POLARITY
).extract(i
, 1).as_bool();
480 mrd
.transparent
= cell
->parameters
.at(ID::RD_TRANSPARENT
).extract(i
, 1).as_bool();
481 mrd
.clk
= cell
->getPort(ID::RD_CLK
).extract(i
, 1);
482 mrd
.en
= cell
->getPort(ID::RD_EN
).extract(i
, 1);
483 mrd
.addr
= cell
->getPort(ID::RD_ADDR
).extract(i
* abits
, abits
);
484 mrd
.data
= cell
->getPort(ID::RD_DATA
).extract(i
* res
.width
, res
.width
);
485 mrd
.ce_over_srst
= false;
486 mrd
.arst_value
= Const(State::Sx
, res
.width
<< mrd
.wide_log2
);
487 mrd
.srst_value
= Const(State::Sx
, res
.width
<< mrd
.wide_log2
);
488 mrd
.init_value
= Const(State::Sx
, res
.width
<< mrd
.wide_log2
);
489 mrd
.srst
= State::S0
;
490 mrd
.arst
= State::S0
;
491 res
.rd_ports
.push_back(mrd
);
493 for (int i
= 0; i
< cell
->parameters
.at(ID::WR_PORTS
).as_int(); i
++) {
496 mwr
.clk_enable
= cell
->parameters
.at(ID::WR_CLK_ENABLE
).extract(i
, 1).as_bool();
497 mwr
.clk_polarity
= cell
->parameters
.at(ID::WR_CLK_POLARITY
).extract(i
, 1).as_bool();
498 mwr
.clk
= cell
->getPort(ID::WR_CLK
).extract(i
, 1);
499 mwr
.en
= cell
->getPort(ID::WR_EN
).extract(i
* res
.width
, res
.width
);
500 mwr
.addr
= cell
->getPort(ID::WR_ADDR
).extract(i
* abits
, abits
);
501 mwr
.data
= cell
->getPort(ID::WR_DATA
).extract(i
* res
.width
, res
.width
);
502 res
.wr_ports
.push_back(mwr
);
504 for (int i
= 0; i
< GetSize(res
.wr_ports
); i
++) {
505 auto &port
= res
.wr_ports
[i
];
506 port
.priority_mask
.resize(GetSize(res
.wr_ports
));
507 for (int j
= 0; j
< i
; j
++) {
508 auto &oport
= res
.wr_ports
[j
];
509 if (port
.clk_enable
!= oport
.clk_enable
)
511 if (port
.clk_enable
&& port
.clk
!= oport
.clk
)
513 if (port
.clk_enable
&& port
.clk_polarity
!= oport
.clk_polarity
)
515 port
.priority_mask
[j
] = true;
524 std::vector
<Mem
> Mem::get_all_memories(Module
*module
) {
525 std::vector
<Mem
> res
;
526 MemIndex
index(module
);
527 for (auto it
: module
->memories
) {
528 res
.push_back(mem_from_memory(module
, it
.second
, index
));
530 for (auto cell
: module
->cells()) {
531 if (cell
->type
== ID($mem
))
532 res
.push_back(mem_from_cell(cell
));
537 std::vector
<Mem
> Mem::get_selected_memories(Module
*module
) {
538 std::vector
<Mem
> res
;
539 MemIndex
index(module
);
540 for (auto it
: module
->memories
) {
541 if (module
->design
->selected(module
, it
.second
))
542 res
.push_back(mem_from_memory(module
, it
.second
, index
));
544 for (auto cell
: module
->selected_cells()) {
545 if (cell
->type
== ID($mem
))
546 res
.push_back(mem_from_cell(cell
));
551 Cell
*Mem::extract_rdff(int idx
, FfInitVals
*initvals
) {
552 MemRd
&port
= rd_ports
[idx
];
554 if (!port
.clk_enable
)
559 // There are two ways to handle rdff extraction when transparency is involved:
561 // - if all of the following conditions are true, put the FF on address input:
563 // - the port has no clock enable, no reset, and no initial value
564 // - the port is transparent wrt all write ports (implying they also share
567 // - otherwise, put the FF on the data output, and make bypass paths for
568 // all write ports wrt which this port is transparent
569 bool trans_use_addr
= port
.transparent
;
571 // If there are no write ports at all, we could possibly use either way; do data
573 if (GetSize(wr_ports
) == 0)
574 trans_use_addr
= false;
576 if (port
.en
!= State::S1
|| port
.srst
!= State::S0
|| port
.arst
!= State::S0
|| !port
.init_value
.is_fully_undef())
577 trans_use_addr
= false;
581 // Do not put a register in front of constant address bits â this is both
582 // unnecessary and will break wide ports.
584 for (int i
= 0; i
< GetSize(port
.addr
); i
++)
585 if (port
.addr
[i
].wire
)
590 SigSpec sig_q
= module
->addWire(stringf("$%s$rdreg[%d]$q", memid
.c_str(), idx
), width
);
594 for (int i
= 0; i
< GetSize(port
.addr
); i
++)
595 if (port
.addr
[i
].wire
) {
596 sig_d
.append(port
.addr
[i
]);
597 port
.addr
[i
] = sig_q
[pos
++];
600 c
= module
->addDff(stringf("$%s$rdreg[%d]", memid
.c_str(), idx
), port
.clk
, sig_d
, sig_q
, port
.clk_polarity
);
607 log_assert(port
.arst
== State::S0
|| port
.srst
== State::S0
);
609 SigSpec async_d
= module
->addWire(stringf("$%s$rdreg[%d]$d", memid
.c_str(), idx
), GetSize(port
.data
));
610 SigSpec sig_d
= async_d
;
612 for (int i
= 0; i
< GetSize(wr_ports
); i
++) {
613 auto &wport
= wr_ports
[i
];
614 if (port
.transparent
) {
615 log_assert(wport
.clk_enable
);
616 log_assert(wport
.clk
== port
.clk
);
617 log_assert(wport
.clk_enable
== port
.clk_enable
);
618 int min_wide_log2
= std::min(port
.wide_log2
, wport
.wide_log2
);
619 int max_wide_log2
= std::max(port
.wide_log2
, wport
.wide_log2
);
620 bool wide_write
= wport
.wide_log2
> port
.wide_log2
;
621 for (int sub
= 0; sub
< (1 << max_wide_log2
); sub
+= (1 << min_wide_log2
)) {
622 SigSpec raddr
= port
.addr
;
623 SigSpec waddr
= wport
.addr
;
625 waddr
= wport
.sub_addr(sub
);
627 raddr
= port
.sub_addr(sub
);
630 addr_eq
= module
->Eq(stringf("$%s$rdtransen[%d][%d][%d]$d", memid
.c_str(), idx
, i
, sub
), raddr
, waddr
);
632 int ewidth
= width
<< min_wide_log2
;
633 int wsub
= wide_write
? sub
: 0;
634 int rsub
= wide_write
? 0 : sub
;
635 while (pos
< ewidth
) {
637 while (epos
< ewidth
&& wport
.en
[epos
+ wsub
* width
] == wport
.en
[pos
+ wsub
* width
])
639 SigSpec cur
= sig_d
.extract(pos
+ rsub
* width
, epos
-pos
);
640 SigSpec other
= wport
.data
.extract(pos
+ wsub
* width
, epos
-pos
);
643 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
);
645 cond
= wport
.en
[pos
+ wsub
* width
];
646 SigSpec merged
= module
->Mux(stringf("$%s$rdtransmux[%d][%d][%d][%d]$d", memid
.c_str(), idx
, i
, sub
, pos
), cur
, other
, cond
);
647 sig_d
.replace(pos
+ rsub
* width
, merged
);
654 IdString name
= stringf("$%s$rdreg[%d]", memid
.c_str(), idx
);
656 ff
.width
= GetSize(port
.data
);
658 ff
.sig_clk
= port
.clk
;
659 ff
.pol_clk
= port
.clk_polarity
;
660 if (port
.en
!= State::S1
) {
665 if (port
.arst
!= State::S0
) {
668 ff
.sig_arst
= port
.arst
;
669 ff
.val_arst
= port
.arst_value
;
671 if (port
.srst
!= State::S0
) {
674 ff
.sig_srst
= port
.srst
;
675 ff
.val_srst
= port
.srst_value
;
676 ff
.ce_over_srst
= ff
.has_en
&& port
.ce_over_srst
;
679 ff
.sig_q
= port
.data
;
680 ff
.val_init
= port
.init_value
;
682 c
= ff
.emit(module
, name
);
685 log("Extracted %s FF from read port %d of %s.%s: %s\n", trans_use_addr
? "addr" : "data",
686 idx
, log_id(module
), log_id(memid
), log_id(c
));
689 port
.clk
= State::S0
;
690 port
.arst
= State::S0
;
691 port
.srst
= State::S0
;
692 port
.clk_enable
= false;
693 port
.clk_polarity
= true;
694 port
.transparent
= false;
695 port
.ce_over_srst
= false;
696 port
.arst_value
= Const(State::Sx
, GetSize(port
.data
));
697 port
.srst_value
= Const(State::Sx
, GetSize(port
.data
));
698 port
.init_value
= Const(State::Sx
, GetSize(port
.data
));
704 // NOTE: several passes depend on this function not modifying
705 // the design at all until (and unless) emit() is called.
706 // Be careful to preserve this.
707 std::vector
<MemRd
> new_rd_ports
;
708 std::vector
<MemWr
> new_wr_ports
;
709 std::vector
<std::pair
<int, int>> new_rd_map
;
710 std::vector
<std::pair
<int, int>> new_wr_map
;
711 for (int i
= 0; i
< GetSize(rd_ports
); i
++) {
712 auto &port
= rd_ports
[i
];
713 for (int sub
= 0; sub
< (1 << port
.wide_log2
); sub
++) {
714 new_rd_map
.push_back(std::make_pair(i
, sub
));
717 for (int i
= 0; i
< GetSize(wr_ports
); i
++) {
718 auto &port
= wr_ports
[i
];
719 for (int sub
= 0; sub
< (1 << port
.wide_log2
); sub
++) {
720 new_wr_map
.push_back(std::make_pair(i
, sub
));
723 for (auto &it
: new_rd_map
) {
724 MemRd
&orig
= rd_ports
[it
.first
];
728 if (port
.wide_log2
) {
729 port
.data
= port
.data
.extract(it
.second
* width
, width
);
730 port
.init_value
= port
.init_value
.extract(it
.second
* width
, width
);
731 port
.arst_value
= port
.arst_value
.extract(it
.second
* width
, width
);
732 port
.srst_value
= port
.srst_value
.extract(it
.second
* width
, width
);
733 port
.addr
= port
.sub_addr(it
.second
);
736 new_rd_ports
.push_back(port
);
738 for (auto &it
: new_wr_map
) {
739 MemWr
&orig
= wr_ports
[it
.first
];
743 if (port
.wide_log2
) {
744 port
.data
= port
.data
.extract(it
.second
* width
, width
);
745 port
.en
= port
.en
.extract(it
.second
* width
, width
);
746 port
.addr
= port
.sub_addr(it
.second
);
749 port
.priority_mask
.clear();
750 for (auto &it2
: new_wr_map
)
751 port
.priority_mask
.push_back(orig
.priority_mask
[it2
.first
]);
752 new_wr_ports
.push_back(port
);
754 std::swap(rd_ports
, new_rd_ports
);
755 std::swap(wr_ports
, new_wr_ports
);
758 void Mem::emulate_priority(int idx1
, int idx2
)
760 auto &port1
= wr_ports
[idx1
];
761 auto &port2
= wr_ports
[idx2
];
762 if (!port2
.priority_mask
[idx1
])
764 int min_wide_log2
= std::min(port1
.wide_log2
, port2
.wide_log2
);
765 int max_wide_log2
= std::max(port1
.wide_log2
, port2
.wide_log2
);
766 bool wide1
= port1
.wide_log2
> port2
.wide_log2
;
767 for (int sub
= 0; sub
< (1 << max_wide_log2
); sub
+= (1 << min_wide_log2
)) {
768 SigSpec addr1
= port1
.addr
;
769 SigSpec addr2
= port2
.addr
;
771 addr1
= port1
.sub_addr(sub
);
773 addr2
= port2
.sub_addr(sub
);
774 SigSpec addr_eq
= module
->Eq(NEW_ID
, addr1
, addr2
);
775 int ewidth
= width
<< min_wide_log2
;
776 int sub1
= wide1
? sub
: 0;
777 int sub2
= wide1
? 0 : sub
;
778 dict
<std::pair
<SigBit
, SigBit
>, SigBit
> cache
;
779 for (int pos
= 0; pos
< ewidth
; pos
++) {
780 SigBit
&en1
= port1
.en
[pos
+ sub1
* width
];
781 SigBit
&en2
= port2
.en
[pos
+ sub2
* width
];
782 std::pair
<SigBit
, SigBit
> key(en1
, en2
);
783 if (cache
.count(key
)) {
786 SigBit active2
= module
->And(NEW_ID
, addr_eq
, en2
);
787 SigBit nactive2
= module
->Not(NEW_ID
, active2
);
788 en1
= cache
[key
] = module
->And(NEW_ID
, en1
, nactive2
);
792 port2
.priority_mask
[idx1
] = false;
795 void Mem::prepare_wr_merge(int idx1
, int idx2
) {
796 log_assert(idx1
< idx2
);
797 auto &port1
= wr_ports
[idx1
];
798 auto &port2
= wr_ports
[idx2
];
799 // If port 2 has priority over a port before port 1, make port 1 have priority too.
800 for (int i
= 0; i
< idx1
; i
++)
801 if (port2
.priority_mask
[i
])
802 port1
.priority_mask
[i
] = true;
803 // If port 2 has priority over a port after port 1, emulate it.
804 for (int i
= idx1
+ 1; i
< idx2
; i
++)
805 if (port2
.priority_mask
[i
])
806 emulate_priority(i
, idx2
);
807 // If some port had priority over port 2, make it have priority over the merged port too.
808 for (int i
= idx2
+ 1; i
< GetSize(wr_ports
); i
++) {
809 auto &oport
= wr_ports
[i
];
810 if (oport
.priority_mask
[idx2
])
811 oport
.priority_mask
[idx1
] = true;
815 void Mem::widen_prep(int wide_log2
) {
816 // Make sure start_offset and size are aligned to the port width,
817 // adjust if necessary.
818 int mask
= ((1 << wide_log2
) - 1);
819 int delta
= start_offset
& mask
;
820 start_offset
-= delta
;
828 void Mem::widen_wr_port(int idx
, int wide_log2
) {
829 widen_prep(wide_log2
);
830 auto &port
= wr_ports
[idx
];
831 log_assert(port
.wide_log2
<= wide_log2
);
832 if (port
.wide_log2
< wide_log2
) {
833 SigSpec new_data
, new_en
;
834 SigSpec addr_lo
= port
.addr
.extract(0, wide_log2
);
835 for (int sub
= 0; sub
< (1 << wide_log2
); sub
+= (1 << port
.wide_log2
))
837 Const
cur_addr_lo(sub
, wide_log2
);
838 if (addr_lo
== cur_addr_lo
) {
839 // Always writes to this subword.
840 new_data
.append(port
.data
);
841 new_en
.append(port
.en
);
842 } else if (addr_lo
.is_fully_const()) {
843 // Never writes to this subword.
844 new_data
.append(Const(State::Sx
, GetSize(port
.data
)));
845 new_en
.append(Const(State::S0
, GetSize(port
.data
)));
847 // May or may not write to this subword.
848 new_data
.append(port
.data
);
849 SigSpec addr_eq
= module
->Eq(NEW_ID
, addr_lo
, cur_addr_lo
);
850 SigSpec en
= module
->Mux(NEW_ID
, Const(State::S0
, GetSize(port
.data
)), port
.en
, addr_eq
);
854 port
.addr
.replace(port
.wide_log2
, Const(State::S0
, wide_log2
- port
.wide_log2
));
855 port
.data
= new_data
;
857 port
.wide_log2
= wide_log2
;