2013-01-05 04:13:26 -06:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
2021-06-07 17:39:36 -05:00
|
|
|
* Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-01-05 04:13:26 -06:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-01-05 04:13:26 -06:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
#include "kernel/yosys.h"
|
|
|
|
#include "kernel/sigtools.h"
|
2020-07-18 19:28:55 -05:00
|
|
|
#include "kernel/ffinit.h"
|
2020-10-23 10:48:00 -05:00
|
|
|
#include "kernel/mem.h"
|
2021-03-15 09:38:45 -05:00
|
|
|
#include "kernel/ff.h"
|
|
|
|
#include "kernel/ffmerge.h"
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
struct MemoryDffWorker
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2015-06-14 09:15:51 -05:00
|
|
|
Module *module;
|
|
|
|
SigMap sigmap;
|
2020-07-18 19:28:55 -05:00
|
|
|
FfInitVals initvals;
|
2021-03-15 09:38:45 -05:00
|
|
|
FfMergeHelper merger;
|
2015-06-14 09:15:51 -05:00
|
|
|
|
2018-05-28 10:16:15 -05:00
|
|
|
MemoryDffWorker(Module *module) : module(module), sigmap(module)
|
|
|
|
{
|
2020-07-18 19:28:55 -05:00
|
|
|
initvals.set(&sigmap, module);
|
2021-03-15 09:38:45 -05:00
|
|
|
merger.set(&initvals, module);
|
2018-05-28 10:16:15 -05:00
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2021-03-15 09:38:45 -05:00
|
|
|
void handle_rd_port(Mem &mem, int idx)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2021-03-15 09:38:45 -05:00
|
|
|
auto &port = mem.rd_ports[idx];
|
|
|
|
log("Checking read port `%s'[%d] in module `%s': ", mem.memid.c_str(), idx, module->name.c_str());
|
2020-07-20 16:58:00 -05:00
|
|
|
|
2021-03-15 09:38:45 -05:00
|
|
|
FfData ff;
|
|
|
|
pool<std::pair<Cell *, int>> bits;
|
|
|
|
if (!merger.find_output_ff(port.data, ff, bits)) {
|
|
|
|
log("no output FF found.\n");
|
|
|
|
return;
|
2020-07-20 16:58:00 -05:00
|
|
|
}
|
2021-03-15 09:38:45 -05:00
|
|
|
if (!ff.has_clk) {
|
|
|
|
log("output latches are not supported.\n");
|
|
|
|
return;
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2021-03-15 09:38:45 -05:00
|
|
|
if (ff.has_sr) {
|
|
|
|
// Latches and FFs with SR are not supported.
|
|
|
|
log("output FF has both set and reset, not supported.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ff.has_srst || ff.has_arst || !ff.val_init.is_fully_undef()) {
|
|
|
|
// TODO: not supported yet
|
|
|
|
log("output FF has reset and/or init value, not supported yet.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
merger.remove_output_ff(bits);
|
|
|
|
if (ff.has_en && !ff.pol_en)
|
|
|
|
ff.sig_en = module->LogicNot(NEW_ID, ff.sig_en);
|
|
|
|
if (ff.has_arst && !ff.pol_arst)
|
|
|
|
ff.sig_arst = module->LogicNot(NEW_ID, ff.sig_arst);
|
|
|
|
if (ff.has_srst && !ff.pol_srst)
|
|
|
|
ff.sig_srst = module->LogicNot(NEW_ID, ff.sig_srst);
|
|
|
|
port.clk = ff.sig_clk;
|
|
|
|
port.clk_enable = true;
|
|
|
|
port.clk_polarity = ff.pol_clk;
|
|
|
|
if (ff.has_en)
|
|
|
|
port.en = ff.sig_en;
|
|
|
|
else
|
|
|
|
port.en = State::S1;
|
|
|
|
#if 0
|
|
|
|
if (ff.has_arst) {
|
|
|
|
port.arst = ff.sig_arst;
|
|
|
|
port.arst_value = ff.val_arst;
|
|
|
|
} else {
|
|
|
|
port.arst = State::S0;
|
|
|
|
}
|
|
|
|
if (ff.has_srst) {
|
|
|
|
port.srst = ff.sig_srst;
|
|
|
|
port.srst_value = ff.val_srst;
|
|
|
|
port.ce_over_srst = ff.ce_over_srst;
|
|
|
|
} else {
|
|
|
|
port.srst = State::S0;
|
|
|
|
}
|
|
|
|
port.init_value = ff.val_init;
|
|
|
|
#endif
|
|
|
|
port.data = ff.sig_q;
|
|
|
|
mem.emit();
|
|
|
|
log("merged output FF to cell.\n");
|
2015-06-14 09:15:51 -05:00
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2021-03-15 09:38:45 -05:00
|
|
|
void handle_rd_port_addr(Mem &mem, int idx)
|
2015-06-14 09:15:51 -05:00
|
|
|
{
|
2020-10-23 10:48:00 -05:00
|
|
|
auto &port = mem.rd_ports[idx];
|
2021-03-15 09:38:45 -05:00
|
|
|
log("Checking read port address `%s'[%d] in module `%s': ", mem.memid.c_str(), idx, module->name.c_str());
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2021-03-15 09:38:45 -05:00
|
|
|
FfData ff;
|
|
|
|
pool<std::pair<Cell *, int>> bits;
|
|
|
|
if (!merger.find_input_ff(port.addr, ff, bits)) {
|
|
|
|
log("no address FF found.\n");
|
2021-03-04 18:23:25 -06:00
|
|
|
return;
|
2015-06-14 09:15:51 -05:00
|
|
|
}
|
2021-03-15 09:38:45 -05:00
|
|
|
if (!ff.has_clk) {
|
|
|
|
log("address latches are not supported.\n");
|
2015-06-14 09:15:51 -05:00
|
|
|
return;
|
|
|
|
}
|
2021-03-15 09:38:45 -05:00
|
|
|
if (ff.has_sr || ff.has_arst) {
|
|
|
|
log("address FF has async set and/or reset, not supported.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Trick part: this transform is invalid if the initial
|
|
|
|
// value of the FF is fully-defined. However, we
|
|
|
|
// cannot simply reject FFs with any defined init bit,
|
|
|
|
// as this is often the result of merging a const bit.
|
|
|
|
if (ff.val_init.is_fully_def()) {
|
|
|
|
log("address FF has fully-defined init value, not supported.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < GetSize(mem.wr_ports); i++) {
|
|
|
|
auto &wport = mem.wr_ports[i];
|
|
|
|
if (!wport.clk_enable || wport.clk != ff.sig_clk || wport.clk_polarity != ff.pol_clk) {
|
|
|
|
log("address FF clock is not compatible with write clock.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now we're commited to merge it.
|
|
|
|
merger.mark_input_ff(bits);
|
|
|
|
// If the address FF has enable and/or sync reset, unmap it.
|
|
|
|
ff.unmap_ce_srst(module);
|
|
|
|
port.clk = ff.sig_clk;
|
|
|
|
port.en = State::S1;
|
|
|
|
port.addr = ff.sig_d;
|
|
|
|
port.clk_enable = true;
|
|
|
|
port.clk_polarity = ff.pol_clk;
|
|
|
|
port.transparent = true;
|
|
|
|
mem.emit();
|
|
|
|
log("merged address FF to cell.\n");
|
2015-06-14 09:15:51 -05:00
|
|
|
}
|
2014-08-06 07:31:38 -05:00
|
|
|
|
2021-02-23 12:42:51 -06:00
|
|
|
void run()
|
2015-06-14 09:15:51 -05:00
|
|
|
{
|
2021-03-15 09:38:45 -05:00
|
|
|
std::vector<Mem> memories = Mem::get_selected_memories(module);
|
|
|
|
for (auto &mem : memories) {
|
|
|
|
for (int i = 0; i < GetSize(mem.rd_ports); i++) {
|
|
|
|
if (!mem.rd_ports[i].clk_enable)
|
|
|
|
handle_rd_port(mem, i);
|
2015-06-14 09:15:51 -05:00
|
|
|
}
|
|
|
|
}
|
2021-03-15 09:38:45 -05:00
|
|
|
for (auto &mem : memories) {
|
2020-10-23 10:48:00 -05:00
|
|
|
for (int i = 0; i < GetSize(mem.rd_ports); i++) {
|
|
|
|
if (!mem.rd_ports[i].clk_enable)
|
2021-03-15 09:38:45 -05:00
|
|
|
handle_rd_port_addr(mem, i);
|
2020-10-23 10:48:00 -05:00
|
|
|
}
|
|
|
|
}
|
2015-06-14 09:15:51 -05:00
|
|
|
}
|
|
|
|
};
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
struct MemoryDffPass : public Pass {
|
2021-02-23 12:42:51 -06:00
|
|
|
MemoryDffPass() : Pass("memory_dff", "merge input/output DFFs into memory read ports") { }
|
2020-06-18 18:34:52 -05:00
|
|
|
void help() override
|
2013-03-01 03:17:35 -06:00
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
2014-02-03 06:01:45 -06:00
|
|
|
log(" memory_dff [options] [selection]\n");
|
2013-03-01 03:17:35 -06:00
|
|
|
log("\n");
|
2021-02-23 12:42:51 -06:00
|
|
|
log("This pass detects DFFs at memory read ports and merges them into the memory port.\n");
|
2013-03-01 03:17:35 -06:00
|
|
|
log("I.e. it consumes an asynchronous memory port and the flip-flops at its\n");
|
|
|
|
log("interface and yields a synchronous memory port.\n");
|
|
|
|
log("\n");
|
|
|
|
}
|
2020-06-18 18:34:52 -05:00
|
|
|
void execute(std::vector<std::string> args, RTLIL::Design *design) override
|
2014-02-03 06:01:45 -06:00
|
|
|
{
|
2021-02-23 12:42:51 -06:00
|
|
|
log_header(design, "Executing MEMORY_DFF pass (merging $dff cells to $memrd).\n");
|
2014-02-03 06:01:45 -06:00
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(args, argidx, design);
|
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
for (auto mod : design->selected_modules()) {
|
|
|
|
MemoryDffWorker worker(mod);
|
2021-02-23 12:42:51 -06:00
|
|
|
worker.run();
|
2015-06-14 09:15:51 -05:00
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
} MemoryDffPass;
|
2015-07-02 04:14:30 -05:00
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
PRIVATE_NAMESPACE_END
|