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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-07-02 07:27:37 -05:00
|
|
|
#include <algorithm>
|
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"
|
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;
|
2020-07-18 19:28:55 -05:00
|
|
|
FfInitVals initvals;
|
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);
|
2018-05-28 10:16:15 -05:00
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2020-07-20 16:58:00 -05:00
|
|
|
bool find_sig_before_dff(RTLIL::SigSpec &sig, RTLIL::SigSpec &clk, bool &clk_polarity)
|
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
|
|
|
|
2020-10-22 03:37:44 -05:00
|
|
|
dict<SigBit, SigBit> cache;
|
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
for (auto &bit : sig)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2020-10-22 03:37:44 -05:00
|
|
|
if (cache.count(bit)) {
|
|
|
|
bit = cache[bit];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
if (bit.wire == NULL)
|
|
|
|
continue;
|
2015-06-09 00:19:04 -05:00
|
|
|
|
2020-07-18 19:28:55 -05:00
|
|
|
if (initvals(bit) != State::Sx)
|
2018-05-28 10:16:15 -05:00
|
|
|
return false;
|
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
for (auto cell : dff_cells)
|
|
|
|
{
|
2020-07-20 16:58:00 -05:00
|
|
|
SigSpec this_clk = cell->getPort(ID::CLK);
|
|
|
|
bool this_clk_polarity = cell->parameters[ID::CLK_POLARITY].as_bool();
|
|
|
|
|
|
|
|
if (invbits.count(this_clk)) {
|
|
|
|
this_clk = invbits.at(this_clk);
|
|
|
|
this_clk_polarity = !this_clk_polarity;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clk != RTLIL::SigSpec(RTLIL::State::Sx)) {
|
|
|
|
if (this_clk != clk)
|
|
|
|
continue;
|
|
|
|
if (this_clk_polarity != clk_polarity)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
RTLIL::SigSpec q_norm = cell->getPort(ID::Q);
|
|
|
|
sigmap.apply(q_norm);
|
|
|
|
|
|
|
|
RTLIL::SigSpec d = q_norm.extract(bit, &cell->getPort(ID::D));
|
|
|
|
if (d.size() != 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (cell->type == ID($sdffce)) {
|
|
|
|
SigSpec rval = cell->parameters[ID::SRST_VALUE];
|
|
|
|
SigSpec rbit = q_norm.extract(bit, &rval);
|
|
|
|
if (cell->parameters[ID::SRST_POLARITY].as_bool())
|
|
|
|
d = module->Mux(NEW_ID, d, rbit, cell->getPort(ID::SRST));
|
|
|
|
else
|
|
|
|
d = module->Mux(NEW_ID, rbit, d, cell->getPort(ID::SRST));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cell->type.in(ID($dffe), ID($sdffe), ID($sdffce))) {
|
|
|
|
if (cell->parameters[ID::EN_POLARITY].as_bool())
|
|
|
|
d = module->Mux(NEW_ID, bit, d, cell->getPort(ID::EN));
|
|
|
|
else
|
|
|
|
d = module->Mux(NEW_ID, d, bit, cell->getPort(ID::EN));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cell->type.in(ID($sdff), ID($sdffe))) {
|
|
|
|
SigSpec rval = cell->parameters[ID::SRST_VALUE];
|
|
|
|
SigSpec rbit = q_norm.extract(bit, &rval);
|
|
|
|
if (cell->parameters[ID::SRST_POLARITY].as_bool())
|
|
|
|
d = module->Mux(NEW_ID, d, rbit, cell->getPort(ID::SRST));
|
|
|
|
else
|
|
|
|
d = module->Mux(NEW_ID, rbit, d, cell->getPort(ID::SRST));
|
|
|
|
}
|
|
|
|
|
2020-10-22 03:37:44 -05:00
|
|
|
cache[bit] = d;
|
2020-07-20 16:58:00 -05:00
|
|
|
bit = d;
|
|
|
|
clk = this_clk;
|
|
|
|
clk_polarity = this_clk_polarity;
|
|
|
|
goto replaced_this_bit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
replaced_this_bit:;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool find_sig_after_dffe(RTLIL::SigSpec &sig, RTLIL::SigSpec &clk, bool &clk_polarity, RTLIL::SigSpec &en, bool &en_polarity)
|
|
|
|
{
|
|
|
|
sigmap.apply(sig);
|
|
|
|
|
|
|
|
for (auto &bit : sig)
|
|
|
|
{
|
|
|
|
if (bit.wire == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (auto cell : dff_cells)
|
|
|
|
{
|
|
|
|
if (!cell->type.in(ID($dff), ID($dffe)))
|
2015-10-31 16:01:41 -05:00
|
|
|
continue;
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
SigSpec this_clk = cell->getPort(ID::CLK);
|
|
|
|
bool this_clk_polarity = cell->parameters[ID::CLK_POLARITY].as_bool();
|
2020-07-20 16:58:00 -05:00
|
|
|
SigSpec this_en = State::S1;
|
|
|
|
bool this_en_polarity = true;
|
|
|
|
|
|
|
|
if (cell->type == ID($dffe)) {
|
|
|
|
this_en = cell->getPort(ID::EN);
|
|
|
|
this_en_polarity = cell->parameters[ID::EN_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
|
|
|
|
2020-07-20 16:58:00 -05:00
|
|
|
if (invbits.count(this_en)) {
|
|
|
|
this_en = invbits.at(this_en);
|
|
|
|
this_en_polarity = !this_en_polarity;
|
|
|
|
}
|
|
|
|
|
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;
|
2020-07-20 16:58:00 -05:00
|
|
|
if (this_en != en)
|
|
|
|
continue;
|
|
|
|
if (this_en_polarity != en_polarity)
|
|
|
|
continue;
|
2015-06-14 09:15:51 -05:00
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2020-07-20 16:58:00 -05:00
|
|
|
RTLIL::SigSpec q_norm = cell->getPort(ID::D);
|
2015-06-14 09:15:51 -05:00
|
|
|
sigmap.apply(q_norm);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2020-07-20 16:58:00 -05:00
|
|
|
RTLIL::SigSpec d = q_norm.extract(bit, &cell->getPort(ID::Q));
|
2015-06-14 09:15:51 -05:00
|
|
|
if (d.size() != 1)
|
|
|
|
continue;
|
|
|
|
|
2020-07-18 19:28:55 -05:00
|
|
|
if (initvals(d) != State::Sx)
|
2018-05-28 10:16:15 -05:00
|
|
|
return false;
|
|
|
|
|
2015-06-14 09:15:51 -05:00
|
|
|
bit = d;
|
|
|
|
clk = this_clk;
|
|
|
|
clk_polarity = this_clk_polarity;
|
2020-07-20 16:58:00 -05:00
|
|
|
en = this_en;
|
|
|
|
en_polarity = this_en_polarity;
|
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 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())
|
2020-07-20 16:58:00 -05:00
|
|
|
if (cell->type.in(ID($dff), ID($dffe))) {
|
2020-04-02 11:51:32 -05:00
|
|
|
RTLIL::SigSpec new_q = cell->getPort(ID::Q);
|
2015-06-14 09:15:51 -05:00
|
|
|
new_q.replace(sig, new_sig);
|
2020-04-02 11:51:32 -05:00
|
|
|
cell->setPort(ID::Q, new_q);
|
2015-06-14 09:15:51 -05:00
|
|
|
}
|
|
|
|
}
|
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;
|
2020-07-20 16:58:00 -05:00
|
|
|
bool en_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);
|
2020-07-20 16:58:00 -05:00
|
|
|
RTLIL::SigSpec en_data;
|
2020-04-02 11:51:32 -05:00
|
|
|
RTLIL::SigSpec sig_data = cell->getPort(ID::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
|
|
|
|
2021-03-04 18:23:25 -06:00
|
|
|
if (find_sig_after_dffe(sig_data, clk_data, clk_polarity, en_data, en_polarity) && clk_data != RTLIL::SigSpec(RTLIL::State::Sx))
|
2015-06-14 09:15:51 -05:00
|
|
|
{
|
2021-03-04 18:23:25 -06:00
|
|
|
if (!en_polarity)
|
|
|
|
en_data = module->LogicNot(NEW_ID, en_data);
|
|
|
|
disconnect_dff(sig_data);
|
|
|
|
cell->setPort(ID::CLK, clk_data);
|
|
|
|
cell->setPort(ID::EN, en_data);
|
|
|
|
cell->setPort(ID::DATA, sig_data);
|
|
|
|
cell->parameters[ID::CLK_ENABLE] = RTLIL::Const(1);
|
|
|
|
cell->parameters[ID::CLK_POLARITY] = RTLIL::Const(clk_polarity);
|
|
|
|
cell->parameters[ID::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);
|
2020-04-02 11:51:32 -05:00
|
|
|
RTLIL::SigSpec sig_addr = cell->getPort(ID::ADDR);
|
2015-06-14 09:15:51 -05:00
|
|
|
if (find_sig_before_dff(sig_addr, clk_addr, clk_polarity) &&
|
|
|
|
clk_addr != RTLIL::SigSpec(RTLIL::State::Sx))
|
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
cell->setPort(ID::CLK, clk_addr);
|
|
|
|
cell->setPort(ID::EN, State::S1);
|
|
|
|
cell->setPort(ID::ADDR, sig_addr);
|
|
|
|
cell->parameters[ID::CLK_ENABLE] = RTLIL::Const(1);
|
|
|
|
cell->parameters[ID::CLK_POLARITY] = RTLIL::Const(clk_polarity);
|
|
|
|
cell->parameters[ID::TRANSPARENT] = RTLIL::Const(1);
|
2015-06-14 09:15:51 -05:00
|
|
|
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
|
|
|
|
2021-02-23 12:42:51 -06:00
|
|
|
void run()
|
2015-06-14 09:15:51 -05:00
|
|
|
{
|
|
|
|
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()) {
|
2020-07-20 16:58:00 -05:00
|
|
|
if (cell->type.in(ID($dff), ID($dffe), ID($sdff), ID($sdffe), ID($sdffce)))
|
2015-06-14 09:15:51 -05:00
|
|
|
dff_cells.push_back(cell);
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type.in(ID($not), ID($_NOT_)) || (cell->type == ID($logic_not) && GetSize(cell->getPort(ID::A)) == 1)) {
|
2020-03-12 14:57:01 -05:00
|
|
|
SigSpec sig_a = cell->getPort(ID::A);
|
|
|
|
SigSpec sig_y = cell->getPort(ID::Y);
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type == ID($not))
|
|
|
|
sig_a.extend_u0(GetSize(sig_y), cell->getParam(ID::A_SIGNED).as_bool());
|
|
|
|
if (cell->type == ID($logic_not))
|
2015-06-14 09:15:51 -05:00
|
|
|
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())
|
2021-02-23 12:42:51 -06:00
|
|
|
if (cell->type == ID($memrd) && !cell->parameters[ID::CLK_ENABLE].as_bool())
|
|
|
|
handle_rd_cell(cell);
|
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
|