2014-02-20 13:44:11 -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/sigtools.h"
|
|
|
|
#include "kernel/rtlil.h"
|
|
|
|
#include "kernel/log.h"
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
2014-02-20 13:44:11 -06:00
|
|
|
struct ConnwrappersWorker
|
|
|
|
{
|
|
|
|
struct portdecl_t {
|
|
|
|
// key: celltype, portname;
|
|
|
|
std::string widthparam, signparam;
|
|
|
|
bool is_signed;
|
|
|
|
};
|
|
|
|
|
2014-08-02 06:11:01 -05:00
|
|
|
std::set<RTLIL::IdString> decl_celltypes;
|
|
|
|
std::map<std::pair<RTLIL::IdString, RTLIL::IdString>, portdecl_t> decls;
|
2014-02-20 13:44:11 -06:00
|
|
|
|
|
|
|
void add_port(std::string celltype, std::string portname, std::string widthparam, std::string signparam)
|
|
|
|
{
|
|
|
|
std::pair<std::string, std::string> key(RTLIL::escape_id(celltype), RTLIL::escape_id(portname));
|
|
|
|
decl_celltypes.insert(key.first);
|
|
|
|
|
|
|
|
if (decls.count(key))
|
|
|
|
log_cmd_error("Duplicate port decl: %s %s\n", celltype.c_str(), portname.c_str());
|
|
|
|
|
|
|
|
portdecl_t decl;
|
|
|
|
decl.widthparam = RTLIL::escape_id(widthparam);
|
|
|
|
decl.signparam = RTLIL::escape_id(signparam);
|
|
|
|
decl.is_signed = false;
|
|
|
|
decls[key] = decl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_port(std::string celltype, std::string portname, std::string widthparam, bool is_signed)
|
|
|
|
{
|
|
|
|
std::pair<std::string, std::string> key(RTLIL::escape_id(celltype), RTLIL::escape_id(portname));
|
|
|
|
decl_celltypes.insert(key.first);
|
|
|
|
|
|
|
|
if (decls.count(key))
|
|
|
|
log_cmd_error("Duplicate port decl: %s %s\n", celltype.c_str(), portname.c_str());
|
|
|
|
|
|
|
|
portdecl_t decl;
|
|
|
|
decl.widthparam = RTLIL::escape_id(widthparam);
|
|
|
|
decl.is_signed = is_signed;
|
|
|
|
decls[key] = decl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void work(RTLIL::Design *design, RTLIL::Module *module)
|
|
|
|
{
|
|
|
|
std::map<RTLIL::SigBit, std::pair<bool, RTLIL::SigSpec>> extend_map;
|
|
|
|
SigMap sigmap(module);
|
|
|
|
|
2014-07-26 18:51:45 -05:00
|
|
|
for (auto &it : module->cells_)
|
2014-02-20 13:44:11 -06:00
|
|
|
{
|
|
|
|
RTLIL::Cell *cell = it.second;
|
|
|
|
|
|
|
|
if (!decl_celltypes.count(cell->type))
|
|
|
|
continue;
|
|
|
|
|
2014-07-26 07:32:50 -05:00
|
|
|
for (auto &conn : cell->connections())
|
2014-02-20 13:44:11 -06:00
|
|
|
{
|
2014-08-02 06:11:01 -05:00
|
|
|
std::pair<RTLIL::IdString, RTLIL::IdString> key(cell->type, conn.first);
|
2014-02-20 13:44:11 -06:00
|
|
|
|
|
|
|
if (!decls.count(key))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
portdecl_t &decl = decls.at(key);
|
|
|
|
|
|
|
|
if (!cell->parameters.count(decl.widthparam))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!decl.signparam.empty() && !cell->parameters.count(decl.signparam))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int inner_width = cell->parameters.at(decl.widthparam).as_int();
|
2014-07-22 13:15:14 -05:00
|
|
|
int outer_width = conn.second.size();
|
2014-02-20 13:44:11 -06:00
|
|
|
bool is_signed = decl.signparam.empty() ? decl.is_signed : cell->parameters.at(decl.signparam).as_bool();
|
|
|
|
|
|
|
|
if (inner_width >= outer_width)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
RTLIL::SigSpec sig = sigmap(conn.second);
|
|
|
|
extend_map[sig.extract(inner_width - 1, 1)] = std::pair<bool, RTLIL::SigSpec>(is_signed,
|
|
|
|
sig.extract(inner_width, outer_width - inner_width));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-26 18:51:45 -05:00
|
|
|
for (auto &it : module->cells_)
|
2014-02-20 13:44:11 -06:00
|
|
|
{
|
|
|
|
RTLIL::Cell *cell = it.second;
|
|
|
|
|
|
|
|
if (!design->selected(module, cell))
|
|
|
|
continue;
|
|
|
|
|
2014-07-26 08:57:57 -05:00
|
|
|
for (auto &conn : cell->connections_)
|
2014-02-20 13:44:11 -06:00
|
|
|
{
|
|
|
|
std::vector<RTLIL::SigBit> sigbits = sigmap(conn.second).to_sigbit_vector();
|
|
|
|
RTLIL::SigSpec old_sig;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sigbits.size(); i++)
|
|
|
|
{
|
|
|
|
if (!extend_map.count(sigbits[i]))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bool is_signed = extend_map.at(sigbits[i]).first;
|
|
|
|
RTLIL::SigSpec extend_sig = extend_map.at(sigbits[i]).second;
|
|
|
|
|
|
|
|
int extend_width = 0;
|
|
|
|
RTLIL::SigBit extend_bit = is_signed ? sigbits[i] : RTLIL::SigBit(RTLIL::State::S0);
|
2014-07-22 13:15:14 -05:00
|
|
|
while (extend_width < extend_sig.size() && i + extend_width + 1 < sigbits.size() &&
|
2014-02-20 13:44:11 -06:00
|
|
|
sigbits[i + extend_width + 1] == extend_bit) extend_width++;
|
|
|
|
|
|
|
|
if (extend_width == 0)
|
|
|
|
continue;
|
|
|
|
|
2014-07-22 13:15:14 -05:00
|
|
|
if (old_sig.size() == 0)
|
2014-02-20 13:44:11 -06:00
|
|
|
old_sig = conn.second;
|
|
|
|
|
|
|
|
conn.second.replace(i+1, extend_sig.extract(0, extend_width));
|
|
|
|
i += extend_width;
|
|
|
|
}
|
|
|
|
|
2014-07-22 13:15:14 -05:00
|
|
|
if (old_sig.size())
|
2014-02-20 13:44:11 -06:00
|
|
|
log("Connected extended bits of %s.%s:%s: %s -> %s\n", RTLIL::id2cstr(module->name), RTLIL::id2cstr(cell->name),
|
|
|
|
RTLIL::id2cstr(conn.first), log_signal(old_sig), log_signal(conn.second));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ConnwrappersPass : public Pass {
|
|
|
|
ConnwrappersPass() : Pass("connwrappers", "replace undef values with defined constants") { }
|
|
|
|
virtual void help()
|
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
|
|
|
log(" connwrappers [options] [selection]\n");
|
|
|
|
log("\n");
|
|
|
|
log("Wrappers are used in coarse-grain synthesis to wrap cells with smaller ports\n");
|
|
|
|
log("in wrapper cells with a (larger) constant port size. I.e. the upper bits\n");
|
|
|
|
log("of the wrapper outut are signed/unsigned bit extended. This command uses this\n");
|
|
|
|
log("knowlege to rewire the inputs of the driven cells to match the output of\n");
|
|
|
|
log("the driving cell.\n");
|
|
|
|
log("\n");
|
|
|
|
log(" -signed <cell_type> <port_name> <width_param>\n");
|
|
|
|
log(" -unsigned <cell_type> <port_name> <width_param>\n");
|
|
|
|
log(" consider the specified signed/unsigned wrapper output\n");
|
|
|
|
log("\n");
|
|
|
|
log(" -port <cell_type> <port_name> <width_param> <sign_param>\n");
|
|
|
|
log(" use the specified parameter to decide if signed or unsigned\n");
|
|
|
|
log("\n");
|
|
|
|
log("The options -signed, -unsigned, and -port can be specified multiple times.\n");
|
|
|
|
log("\n");
|
|
|
|
}
|
|
|
|
virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
|
|
|
|
{
|
|
|
|
ConnwrappersWorker worker;
|
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++)
|
|
|
|
{
|
|
|
|
if (args[argidx] == "-signed" && argidx+3 < args.size()) {
|
|
|
|
worker.add_port(args[argidx+1], args[argidx+2], args[argidx+3], true);
|
|
|
|
argidx += 3;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (args[argidx] == "-unsigned" && argidx+3 < args.size()) {
|
|
|
|
worker.add_port(args[argidx+1], args[argidx+2], args[argidx+3], false);
|
|
|
|
argidx += 3;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (args[argidx] == "-port" && argidx+4 < args.size()) {
|
|
|
|
worker.add_port(args[argidx+1], args[argidx+2], args[argidx+3], args[argidx+4]);
|
|
|
|
argidx += 4;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(args, argidx, design);
|
|
|
|
|
|
|
|
log_header("Executing CONNWRAPPERS pass (connect extended ports of wrapper cells).\n");
|
|
|
|
|
2014-07-27 03:18:00 -05:00
|
|
|
for (auto &mod_it : design->modules_)
|
2014-02-20 13:44:11 -06:00
|
|
|
if (design->selected(mod_it.second))
|
|
|
|
worker.work(design, mod_it.second);
|
|
|
|
}
|
|
|
|
} ConnwrappersPass;
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
PRIVATE_NAMESPACE_END
|