2013-01-05 04:13:26 -06:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
|
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"
|
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;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
vector<Cell*> dff_cells;
|
|
|
|
dict<SigBit, SigBit> invbits;
|
|
|
|
dict<SigBit, int> sigbit_users_count;
|
2015-09-25 05:23:11 -05:00
|
|
|
dict<SigSpec, Cell*> mux_cells_a, mux_cells_b;
|
2015-10-31 16:01:41 -05:00
|
|
|
pool<Cell*> forward_merged_dffs, candidate_dffs;
|
2018-05-28 10:16:15 -05:00
|
|
|
pool<SigBit> init_bits;
|
2015-06-14 09:15:51 -05:00
|
|
|
|
2018-05-28 10:16:15 -05:00
|
|
|
MemoryDffWorker(Module *module) : module(module), sigmap(module)
|
|
|
|
{
|
|
|
|
for (auto wire : module->wires()) {
|
|
|
|
if (wire->attributes.count("\\init") == 0)
|
|
|
|
continue;
|
|
|
|
SigSpec sig = sigmap(wire);
|
|
|
|
Const initval = wire->attributes.count("\\init");
|
|
|
|
for (int i = 0; i < GetSize(sig) && i < GetSize(initval); i++)
|
|
|
|
if (initval[i] == State::S0 || initval[i] == State::S1)
|
|
|
|
init_bits.insert(sig[i]);
|
|
|
|
}
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
bool find_sig_before_dff(RTLIL::SigSpec &sig, RTLIL::SigSpec &clk, bool &clk_polarity, bool after = false)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2015-06-14 09:15:51 -05:00
|
|
|
sigmap.apply(sig);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
for (auto &bit : sig)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2015-06-14 09:15:51 -05:00
|
|
|
if (bit.wire == NULL)
|
|
|
|
continue;
|
2015-06-09 00:19:04 -05:00
|
|
|
|
2018-05-28 10:16:15 -05:00
|
|
|
if (!after && init_bits.count(sigmap(bit)))
|
|
|
|
return false;
|
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
for (auto cell : dff_cells)
|
|
|
|
{
|
2015-10-31 16:01:41 -05:00
|
|
|
if (after && forward_merged_dffs.count(cell))
|
|
|
|
continue;
|
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
SigSpec this_clk = cell->getPort("\\CLK");
|
|
|
|
bool this_clk_polarity = cell->parameters["\\CLK_POLARITY"].as_bool();
|
2015-06-09 00:19:04 -05:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
if (invbits.count(this_clk)) {
|
|
|
|
this_clk = invbits.at(this_clk);
|
|
|
|
this_clk_polarity = !this_clk_polarity;
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
if (clk != RTLIL::SigSpec(RTLIL::State::Sx)) {
|
|
|
|
if (this_clk != clk)
|
|
|
|
continue;
|
|
|
|
if (this_clk_polarity != clk_polarity)
|
|
|
|
continue;
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
RTLIL::SigSpec q_norm = cell->getPort(after ? "\\D" : "\\Q");
|
|
|
|
sigmap.apply(q_norm);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
RTLIL::SigSpec d = q_norm.extract(bit, &cell->getPort(after ? "\\Q" : "\\D"));
|
|
|
|
if (d.size() != 1)
|
|
|
|
continue;
|
|
|
|
|
2018-05-28 10:16:15 -05:00
|
|
|
if (after && init_bits.count(d))
|
|
|
|
return false;
|
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
bit = d;
|
|
|
|
clk = this_clk;
|
|
|
|
clk_polarity = this_clk_polarity;
|
2015-10-31 16:01:41 -05:00
|
|
|
candidate_dffs.insert(cell);
|
2015-06-14 09:15:51 -05:00
|
|
|
goto replaced_this_bit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
replaced_this_bit:;
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
return true;
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
void handle_wr_cell(RTLIL::Cell *cell)
|
|
|
|
{
|
|
|
|
log("Checking cell `%s' in module `%s': ", cell->name.c_str(), module->name.c_str());
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
RTLIL::SigSpec clk = RTLIL::SigSpec(RTLIL::State::Sx);
|
|
|
|
bool clk_polarity = 0;
|
2015-10-31 16:01:41 -05:00
|
|
|
candidate_dffs.clear();
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
RTLIL::SigSpec sig_addr = cell->getPort("\\ADDR");
|
|
|
|
if (!find_sig_before_dff(sig_addr, clk, clk_polarity)) {
|
|
|
|
log("no (compatible) $dff for address input found.\n");
|
|
|
|
return;
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
RTLIL::SigSpec sig_data = cell->getPort("\\DATA");
|
|
|
|
if (!find_sig_before_dff(sig_data, clk, clk_polarity)) {
|
|
|
|
log("no (compatible) $dff for data input found.\n");
|
|
|
|
return;
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
RTLIL::SigSpec sig_en = cell->getPort("\\EN");
|
|
|
|
if (!find_sig_before_dff(sig_en, clk, clk_polarity)) {
|
|
|
|
log("no (compatible) $dff for enable input found.\n");
|
|
|
|
return;
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-10-31 16:01:41 -05:00
|
|
|
if (clk != RTLIL::SigSpec(RTLIL::State::Sx))
|
|
|
|
{
|
|
|
|
for (auto cell : candidate_dffs)
|
|
|
|
forward_merged_dffs.insert(cell);
|
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
cell->setPort("\\CLK", clk);
|
|
|
|
cell->setPort("\\ADDR", sig_addr);
|
|
|
|
cell->setPort("\\DATA", sig_data);
|
|
|
|
cell->setPort("\\EN", sig_en);
|
|
|
|
cell->parameters["\\CLK_ENABLE"] = RTLIL::Const(1);
|
|
|
|
cell->parameters["\\CLK_POLARITY"] = RTLIL::Const(clk_polarity);
|
2015-10-31 16:01:41 -05:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
log("merged $dff to cell.\n");
|
|
|
|
return;
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
log("no (compatible) $dff found.\n");
|
2013-12-01 07:08:18 -06:00
|
|
|
}
|
2014-06-01 04:32:27 -05:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
void disconnect_dff(RTLIL::SigSpec sig)
|
|
|
|
{
|
|
|
|
sigmap.apply(sig);
|
|
|
|
sig.sort_and_unify();
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
std::stringstream sstr;
|
|
|
|
sstr << "$memory_dff_disconnected$" << (autoidx++);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
RTLIL::SigSpec new_sig = module->addWire(sstr.str(), sig.size());
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
for (auto cell : module->cells())
|
|
|
|
if (cell->type == "$dff") {
|
|
|
|
RTLIL::SigSpec new_q = cell->getPort("\\Q");
|
|
|
|
new_q.replace(sig, new_sig);
|
|
|
|
cell->setPort("\\Q", new_q);
|
|
|
|
}
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
void handle_rd_cell(RTLIL::Cell *cell)
|
|
|
|
{
|
|
|
|
log("Checking cell `%s' in module `%s': ", cell->name.c_str(), module->name.c_str());
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
bool clk_polarity = 0;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
RTLIL::SigSpec clk_data = RTLIL::SigSpec(RTLIL::State::Sx);
|
|
|
|
RTLIL::SigSpec sig_data = cell->getPort("\\DATA");
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
for (auto bit : sigmap(sig_data))
|
|
|
|
if (sigbit_users_count[bit] > 1)
|
|
|
|
goto skip_ff_after_read_merging;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-09-25 05:23:11 -05:00
|
|
|
if (mux_cells_a.count(sig_data) || mux_cells_b.count(sig_data))
|
2015-06-14 09:15:51 -05:00
|
|
|
{
|
2016-02-13 10:31:24 -06:00
|
|
|
bool enable_invert = mux_cells_a.count(sig_data) != 0;
|
2015-09-25 05:23:11 -05:00
|
|
|
Cell *mux = enable_invert ? mux_cells_a.at(sig_data) : mux_cells_b.at(sig_data);
|
|
|
|
SigSpec check_q = sigmap(mux->getPort(enable_invert ? "\\B" : "\\A"));
|
|
|
|
|
|
|
|
sig_data = sigmap(mux->getPort("\\Y"));
|
|
|
|
for (auto bit : sig_data)
|
|
|
|
if (sigbit_users_count[bit] > 1)
|
|
|
|
goto skip_ff_after_read_merging;
|
|
|
|
|
|
|
|
if (find_sig_before_dff(sig_data, clk_data, clk_polarity, true) && clk_data != RTLIL::SigSpec(RTLIL::State::Sx) && sig_data == check_q)
|
|
|
|
{
|
|
|
|
disconnect_dff(sig_data);
|
|
|
|
cell->setPort("\\CLK", clk_data);
|
|
|
|
cell->setPort("\\EN", enable_invert ? module->LogicNot(NEW_ID, mux->getPort("\\S")) : mux->getPort("\\S"));
|
|
|
|
cell->setPort("\\DATA", sig_data);
|
|
|
|
cell->parameters["\\CLK_ENABLE"] = RTLIL::Const(1);
|
|
|
|
cell->parameters["\\CLK_POLARITY"] = RTLIL::Const(clk_polarity);
|
|
|
|
cell->parameters["\\TRANSPARENT"] = RTLIL::Const(0);
|
|
|
|
log("merged data $dff with rd enable to cell.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (find_sig_before_dff(sig_data, clk_data, clk_polarity, true) && clk_data != RTLIL::SigSpec(RTLIL::State::Sx))
|
|
|
|
{
|
|
|
|
disconnect_dff(sig_data);
|
|
|
|
cell->setPort("\\CLK", clk_data);
|
|
|
|
cell->setPort("\\EN", State::S1);
|
|
|
|
cell->setPort("\\DATA", sig_data);
|
|
|
|
cell->parameters["\\CLK_ENABLE"] = RTLIL::Const(1);
|
|
|
|
cell->parameters["\\CLK_POLARITY"] = RTLIL::Const(clk_polarity);
|
|
|
|
cell->parameters["\\TRANSPARENT"] = RTLIL::Const(0);
|
|
|
|
log("merged data $dff to cell.\n");
|
|
|
|
return;
|
|
|
|
}
|
2015-06-14 09:15:51 -05:00
|
|
|
}
|
2014-02-03 06:01:45 -06:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
skip_ff_after_read_merging:;
|
|
|
|
RTLIL::SigSpec clk_addr = RTLIL::SigSpec(RTLIL::State::Sx);
|
|
|
|
RTLIL::SigSpec sig_addr = cell->getPort("\\ADDR");
|
|
|
|
if (find_sig_before_dff(sig_addr, clk_addr, clk_polarity) &&
|
|
|
|
clk_addr != RTLIL::SigSpec(RTLIL::State::Sx))
|
|
|
|
{
|
|
|
|
cell->setPort("\\CLK", clk_addr);
|
2015-09-25 05:23:11 -05:00
|
|
|
cell->setPort("\\EN", State::S1);
|
2015-06-14 09:15:51 -05:00
|
|
|
cell->setPort("\\ADDR", sig_addr);
|
|
|
|
cell->parameters["\\CLK_ENABLE"] = RTLIL::Const(1);
|
|
|
|
cell->parameters["\\CLK_POLARITY"] = RTLIL::Const(clk_polarity);
|
|
|
|
cell->parameters["\\TRANSPARENT"] = RTLIL::Const(1);
|
|
|
|
log("merged address $dff to cell.\n");
|
|
|
|
return;
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
log("no (compatible) $dff found.\n");
|
|
|
|
}
|
2014-08-06 07:31:38 -05:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
void run(bool flag_wr_only)
|
|
|
|
{
|
|
|
|
for (auto wire : module->wires()) {
|
|
|
|
if (wire->port_output)
|
|
|
|
for (auto bit : sigmap(wire))
|
|
|
|
sigbit_users_count[bit]++;
|
2015-06-09 00:19:04 -05:00
|
|
|
}
|
2014-08-06 07:31:38 -05:00
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
for (auto cell : module->cells()) {
|
|
|
|
if (cell->type == "$dff")
|
|
|
|
dff_cells.push_back(cell);
|
2015-09-25 05:23:11 -05:00
|
|
|
if (cell->type == "$mux") {
|
|
|
|
mux_cells_a[sigmap(cell->getPort("\\A"))] = cell;
|
|
|
|
mux_cells_b[sigmap(cell->getPort("\\B"))] = cell;
|
|
|
|
}
|
2015-06-14 09:15:51 -05:00
|
|
|
if (cell->type == "$not" || cell->type == "$_NOT_" || (cell->type == "$logic_not" && GetSize(cell->getPort("\\A")) == 1)) {
|
|
|
|
SigSpec sig_a = cell->getPort("\\A");
|
|
|
|
SigSpec sig_y = cell->getPort("\\Y");
|
|
|
|
if (cell->type == "$not")
|
|
|
|
sig_a.extend_u0(GetSize(sig_y), cell->getParam("\\A_SIGNED").as_bool());
|
|
|
|
if (cell->type == "$logic_not")
|
|
|
|
sig_y.extend_u0(1);
|
|
|
|
for (int i = 0; i < GetSize(sig_y); i++)
|
|
|
|
invbits[sig_y[i]] = sig_a[i];
|
|
|
|
}
|
|
|
|
for (auto &conn : cell->connections())
|
|
|
|
if (!cell->known() || cell->input(conn.first))
|
|
|
|
for (auto bit : sigmap(conn.second))
|
|
|
|
sigbit_users_count[bit]++;
|
|
|
|
}
|
2014-09-16 05:40:58 -05:00
|
|
|
|
|
|
|
for (auto cell : module->selected_cells())
|
2015-06-14 09:15:51 -05:00
|
|
|
if (cell->type == "$memwr" && !cell->parameters["\\CLK_ENABLE"].as_bool())
|
|
|
|
handle_wr_cell(cell);
|
|
|
|
|
|
|
|
if (!flag_wr_only)
|
|
|
|
for (auto cell : module->selected_cells())
|
|
|
|
if (cell->type == "$memrd" && !cell->parameters["\\CLK_ENABLE"].as_bool())
|
|
|
|
handle_rd_cell(cell);
|
|
|
|
}
|
|
|
|
};
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
struct MemoryDffPass : public Pass {
|
2013-03-01 03:17:35 -06:00
|
|
|
MemoryDffPass() : Pass("memory_dff", "merge input/output DFFs into memories") { }
|
2018-07-21 01:41:18 -05:00
|
|
|
void help() YS_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");
|
|
|
|
log("This pass detects DFFs at memory ports and merges them into the memory port.\n");
|
|
|
|
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");
|
2015-06-14 09:15:51 -05:00
|
|
|
log(" -nordfff\n");
|
2014-02-03 06:01:45 -06:00
|
|
|
log(" do not merge registers on read ports\n");
|
|
|
|
log("\n");
|
2013-03-01 03:17:35 -06:00
|
|
|
}
|
2018-07-21 01:41:18 -05:00
|
|
|
void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
|
2014-02-03 06:01:45 -06:00
|
|
|
{
|
|
|
|
bool flag_wr_only = false;
|
|
|
|
|
2016-04-21 16:28:37 -05:00
|
|
|
log_header(design, "Executing MEMORY_DFF pass (merging $dff cells to $memrd and $memwr).\n");
|
2014-02-03 06:01:45 -06:00
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++) {
|
2015-06-14 09:15:51 -05:00
|
|
|
if (args[argidx] == "-nordff" || args[argidx] == "-wr_only") {
|
2014-02-03 06:01:45 -06:00
|
|
|
flag_wr_only = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(args, argidx, design);
|
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
for (auto mod : design->selected_modules()) {
|
|
|
|
MemoryDffWorker worker(mod);
|
|
|
|
worker.run(flag_wr_only);
|
|
|
|
}
|
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
|