2013-01-05 04:13:26 -06:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kernel/register.h"
|
|
|
|
#include "kernel/log.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
static void normalize_sig(RTLIL::Module *module, RTLIL::SigSpec &sig)
|
|
|
|
{
|
2014-07-26 07:32:50 -05:00
|
|
|
for (auto &conn : module->connections())
|
2013-01-05 04:13:26 -06:00
|
|
|
sig.replace(conn.first, conn.second);
|
|
|
|
}
|
|
|
|
|
2014-08-06 07:31:38 -05:00
|
|
|
static bool find_sig_before_dff(RTLIL::Module *module, std::vector<RTLIL::Cell*> &dff_cells, RTLIL::SigSpec &sig, RTLIL::SigSpec &clk, bool &clk_polarity, bool after = false)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
normalize_sig(module, sig);
|
|
|
|
|
2014-07-23 08:36:09 -05:00
|
|
|
for (auto &bit : sig)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2014-07-23 08:36:09 -05:00
|
|
|
if (bit.wire == NULL)
|
2013-01-05 04:13:26 -06:00
|
|
|
continue;
|
|
|
|
|
2014-08-06 07:31:38 -05:00
|
|
|
for (auto cell : dff_cells)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
if (clk != RTLIL::SigSpec(RTLIL::State::Sx)) {
|
2014-07-31 09:38:54 -05:00
|
|
|
if (cell->getPort("\\CLK") != clk)
|
2013-01-05 04:13:26 -06:00
|
|
|
continue;
|
|
|
|
if (cell->parameters["\\CLK_POLARITY"].as_bool() != clk_polarity)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-07-31 09:38:54 -05:00
|
|
|
RTLIL::SigSpec q_norm = cell->getPort(after ? "\\D" : "\\Q");
|
2013-01-05 04:13:26 -06:00
|
|
|
normalize_sig(module, q_norm);
|
|
|
|
|
2014-07-31 09:38:54 -05:00
|
|
|
RTLIL::SigSpec d = q_norm.extract(bit, &cell->getPort(after ? "\\Q" : "\\D"));
|
2014-07-22 13:15:14 -05:00
|
|
|
if (d.size() != 1)
|
2013-01-05 04:13:26 -06:00
|
|
|
continue;
|
|
|
|
|
2014-07-23 08:36:09 -05:00
|
|
|
bit = d;
|
2014-07-31 09:38:54 -05:00
|
|
|
clk = cell->getPort("\\CLK");
|
2013-01-05 04:13:26 -06:00
|
|
|
clk_polarity = cell->parameters["\\CLK_POLARITY"].as_bool();
|
|
|
|
goto replaced_this_bit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
replaced_this_bit:;
|
|
|
|
}
|
|
|
|
|
2013-12-01 07:08:18 -06:00
|
|
|
return true;
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2014-08-06 07:31:38 -05:00
|
|
|
static void handle_wr_cell(RTLIL::Module *module, std::vector<RTLIL::Cell*> &dff_cells, RTLIL::Cell *cell)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
log("Checking cell `%s' in module `%s': ", cell->name.c_str(), module->name.c_str());
|
|
|
|
|
|
|
|
RTLIL::SigSpec clk = RTLIL::SigSpec(RTLIL::State::Sx);
|
|
|
|
bool clk_polarity = 0;
|
|
|
|
|
2014-07-31 09:38:54 -05:00
|
|
|
RTLIL::SigSpec sig_addr = cell->getPort("\\ADDR");
|
2014-08-06 07:31:38 -05:00
|
|
|
if (!find_sig_before_dff(module, dff_cells, sig_addr, clk, clk_polarity)) {
|
2013-01-05 04:13:26 -06:00
|
|
|
log("no (compatible) $dff for address input found.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-31 09:38:54 -05:00
|
|
|
RTLIL::SigSpec sig_data = cell->getPort("\\DATA");
|
2014-08-06 07:31:38 -05:00
|
|
|
if (!find_sig_before_dff(module, dff_cells, sig_data, clk, clk_polarity)) {
|
2013-01-05 04:13:26 -06:00
|
|
|
log("no (compatible) $dff for data input found.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-31 09:38:54 -05:00
|
|
|
RTLIL::SigSpec sig_en = cell->getPort("\\EN");
|
2014-08-06 07:31:38 -05:00
|
|
|
if (!find_sig_before_dff(module, dff_cells, sig_en, clk, clk_polarity)) {
|
2013-01-05 04:13:26 -06:00
|
|
|
log("no (compatible) $dff for enable input found.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-01 07:08:18 -06:00
|
|
|
if (clk != RTLIL::SigSpec(RTLIL::State::Sx)) {
|
2014-07-31 09:38:54 -05:00
|
|
|
cell->setPort("\\CLK", clk);
|
|
|
|
cell->setPort("\\ADDR", sig_addr);
|
|
|
|
cell->setPort("\\DATA", sig_data);
|
|
|
|
cell->setPort("\\EN", sig_en);
|
2013-12-01 07:08:18 -06:00
|
|
|
cell->parameters["\\CLK_ENABLE"] = RTLIL::Const(1);
|
|
|
|
cell->parameters["\\CLK_POLARITY"] = RTLIL::Const(clk_polarity);
|
|
|
|
log("merged $dff to cell.\n");
|
2014-08-06 07:31:38 -05:00
|
|
|
return;
|
2013-12-01 07:08:18 -06:00
|
|
|
}
|
2014-06-01 04:32:27 -05:00
|
|
|
|
|
|
|
log("no (compatible) $dff found.\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void disconnect_dff(RTLIL::Module *module, RTLIL::SigSpec sig)
|
|
|
|
{
|
|
|
|
normalize_sig(module, sig);
|
|
|
|
sig.sort_and_unify();
|
|
|
|
|
|
|
|
std::stringstream sstr;
|
2014-07-31 06:19:47 -05:00
|
|
|
sstr << "$memory_dff_disconnected$" << (autoidx++);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-07-26 13:12:50 -05:00
|
|
|
RTLIL::SigSpec new_sig = module->addWire(sstr.str(), sig.size());
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-07-27 03:41:42 -05:00
|
|
|
for (auto cell : module->cells())
|
2014-07-26 08:57:57 -05:00
|
|
|
if (cell->type == "$dff") {
|
2014-07-31 09:38:54 -05:00
|
|
|
RTLIL::SigSpec new_q = cell->getPort("\\Q");
|
2014-07-26 13:12:50 -05:00
|
|
|
new_q.replace(sig, new_sig);
|
2014-07-31 09:38:54 -05:00
|
|
|
cell->setPort("\\Q", new_q);
|
2014-07-26 08:57:57 -05:00
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2014-08-06 07:31:38 -05:00
|
|
|
static void handle_rd_cell(RTLIL::Module *module, std::vector<RTLIL::Cell*> &dff_cells, RTLIL::Cell *cell)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
log("Checking cell `%s' in module `%s': ", cell->name.c_str(), module->name.c_str());
|
|
|
|
|
|
|
|
bool clk_polarity = 0;
|
|
|
|
|
|
|
|
RTLIL::SigSpec clk_data = RTLIL::SigSpec(RTLIL::State::Sx);
|
2014-07-31 09:38:54 -05:00
|
|
|
RTLIL::SigSpec sig_data = cell->getPort("\\DATA");
|
2014-08-06 07:31:38 -05:00
|
|
|
if (find_sig_before_dff(module, dff_cells, sig_data, clk_data, clk_polarity, true) &&
|
2014-02-03 06:01:45 -06:00
|
|
|
clk_data != RTLIL::SigSpec(RTLIL::State::Sx))
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
disconnect_dff(module, sig_data);
|
2014-07-31 09:38:54 -05:00
|
|
|
cell->setPort("\\CLK", clk_data);
|
|
|
|
cell->setPort("\\DATA", sig_data);
|
2013-01-05 04:13:26 -06:00
|
|
|
cell->parameters["\\CLK_ENABLE"] = RTLIL::Const(1);
|
|
|
|
cell->parameters["\\CLK_POLARITY"] = RTLIL::Const(clk_polarity);
|
2014-02-03 06:01:45 -06:00
|
|
|
cell->parameters["\\TRANSPARENT"] = RTLIL::Const(0);
|
2013-01-05 04:13:26 -06:00
|
|
|
log("merged data $dff to cell.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-03 06:01:45 -06:00
|
|
|
RTLIL::SigSpec clk_addr = RTLIL::SigSpec(RTLIL::State::Sx);
|
2014-07-31 09:38:54 -05:00
|
|
|
RTLIL::SigSpec sig_addr = cell->getPort("\\ADDR");
|
2014-08-06 07:31:38 -05:00
|
|
|
if (find_sig_before_dff(module, dff_cells, sig_addr, clk_addr, clk_polarity) &&
|
2014-02-03 06:01:45 -06:00
|
|
|
clk_addr != RTLIL::SigSpec(RTLIL::State::Sx))
|
|
|
|
{
|
2014-07-31 09:38:54 -05:00
|
|
|
cell->setPort("\\CLK", clk_addr);
|
|
|
|
cell->setPort("\\ADDR", sig_addr);
|
2014-02-03 06:01:45 -06:00
|
|
|
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
|
|
|
log("no (compatible) $dff found.\n");
|
|
|
|
}
|
|
|
|
|
2014-08-06 07:31:38 -05:00
|
|
|
static void handle_module(RTLIL::Module *module, bool flag_wr_only)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2014-08-06 07:31:38 -05:00
|
|
|
std::vector<RTLIL::Cell*> dff_cells;
|
|
|
|
|
|
|
|
for (auto cell : module->cells())
|
|
|
|
if (cell->type == "$dff")
|
|
|
|
dff_cells.push_back(cell);
|
|
|
|
|
|
|
|
for (auto cell : module->selected_cells()) {
|
2014-07-27 03:41:42 -05:00
|
|
|
if (cell->type == "$memwr" && !cell->parameters["\\CLK_ENABLE"].as_bool())
|
2014-08-06 07:31:38 -05:00
|
|
|
handle_wr_cell(module, dff_cells, cell);
|
2014-07-27 03:41:42 -05:00
|
|
|
if (!flag_wr_only && cell->type == "$memrd" && !cell->parameters["\\CLK_ENABLE"].as_bool())
|
2014-08-06 07:31:38 -05:00
|
|
|
handle_rd_cell(module, dff_cells, 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") { }
|
|
|
|
virtual void help()
|
|
|
|
{
|
|
|
|
// |---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");
|
2014-02-03 06:01:45 -06:00
|
|
|
log(" -wr_only\n");
|
|
|
|
log(" do not merge registers on read ports\n");
|
|
|
|
log("\n");
|
2013-03-01 03:17:35 -06:00
|
|
|
}
|
2014-02-03 06:01:45 -06:00
|
|
|
virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
|
|
|
|
{
|
|
|
|
bool flag_wr_only = false;
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
log_header("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++) {
|
|
|
|
if (args[argidx] == "-wr_only") {
|
|
|
|
flag_wr_only = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(args, argidx, design);
|
|
|
|
|
2014-08-06 07:31:38 -05:00
|
|
|
for (auto mod : design->selected_modules())
|
|
|
|
handle_module(mod, flag_wr_only);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
} MemoryDffPass;
|
|
|
|
|