2015-04-04 12:00:15 -05:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2015-04-04 12:00:15 -05: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
|
|
|
*
|
2015-04-04 12:00:15 -05: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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kernel/yosys.h"
|
|
|
|
#include "kernel/sigtools.h"
|
|
|
|
|
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
struct DffinitPass : public Pass {
|
|
|
|
DffinitPass() : Pass("dffinit", "set INIT param on FF cells") { }
|
2018-07-21 01:41:18 -05:00
|
|
|
void help() YS_OVERRIDE
|
2015-04-04 12:00:15 -05:00
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
|
|
|
log(" dffinit [options] [selection]\n");
|
|
|
|
log("\n");
|
|
|
|
log("This pass sets an FF cell parameter to the the initial value of the net it\n");
|
|
|
|
log("drives. (This is primarily used in FPGA flows.)\n");
|
|
|
|
log("\n");
|
|
|
|
log(" -ff <cell_name> <output_port> <init_param>\n");
|
|
|
|
log(" operate on the specified cell type. this option can be used\n");
|
|
|
|
log(" multiple times.\n");
|
|
|
|
log("\n");
|
2018-01-09 11:42:19 -06:00
|
|
|
log(" -highlow\n");
|
|
|
|
log(" use the string values \"high\" and \"low\" to represent a single-bit\n");
|
|
|
|
log(" initial value of 1 or 0. (multi-bit values are not supported in this\n");
|
|
|
|
log(" mode.)\n");
|
|
|
|
log("\n");
|
2018-12-18 01:37:43 -06:00
|
|
|
log(" -strinit <string for high> <string for low> \n");
|
|
|
|
log(" use string values in the command line to represent a single-bit\n");
|
|
|
|
log(" initial value of 1 or 0. (multi-bit values are not supported in this\n");
|
|
|
|
log(" mode.)\n");
|
|
|
|
log("\n");
|
2018-12-18 09:10:40 -06:00
|
|
|
log(" -noreinit\n");
|
|
|
|
log(" fail if the FF cell has already a defined initial value set in other\n");
|
|
|
|
log(" passes and the initial value of the net it drives is not equal to\n");
|
|
|
|
log(" the already defined initial value.\n");
|
|
|
|
log("\n");
|
2015-04-04 12:00:15 -05:00
|
|
|
}
|
2018-07-21 01:41:18 -05:00
|
|
|
void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
|
2015-04-04 12:00:15 -05:00
|
|
|
{
|
2016-04-21 16:28:37 -05:00
|
|
|
log_header(design, "Executing DFFINIT pass (set INIT param on FF cells).\n");
|
2015-04-04 12:00:15 -05:00
|
|
|
|
|
|
|
dict<IdString, dict<IdString, IdString>> ff_types;
|
2018-12-18 09:10:40 -06:00
|
|
|
bool highlow_mode = false, noreinit = false;
|
2018-12-18 01:37:43 -06:00
|
|
|
std::string high_string, low_string;
|
2015-04-04 12:00:15 -05:00
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++) {
|
2018-01-09 11:42:19 -06:00
|
|
|
if (args[argidx] == "-highlow") {
|
|
|
|
highlow_mode = true;
|
2018-12-18 01:37:43 -06:00
|
|
|
high_string = "high";
|
|
|
|
low_string = "low";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (args[argidx] == "-strinit" && argidx+2 < args.size()) {
|
|
|
|
highlow_mode = true;
|
|
|
|
high_string = args[++argidx];
|
|
|
|
low_string = args[++argidx];
|
2018-01-09 11:42:19 -06:00
|
|
|
continue;
|
|
|
|
}
|
2015-04-04 12:00:15 -05:00
|
|
|
if (args[argidx] == "-ff" && argidx+3 < args.size()) {
|
|
|
|
IdString cell_name = RTLIL::escape_id(args[++argidx]);
|
|
|
|
IdString output_port = RTLIL::escape_id(args[++argidx]);
|
|
|
|
IdString init_param = RTLIL::escape_id(args[++argidx]);
|
|
|
|
ff_types[cell_name][output_port] = init_param;
|
|
|
|
continue;
|
|
|
|
}
|
2018-12-18 09:10:40 -06:00
|
|
|
if (args[argidx] == "-noreinit") {
|
|
|
|
noreinit = true;
|
|
|
|
continue;
|
|
|
|
}
|
2015-04-04 12:00:15 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(args, argidx, design);
|
|
|
|
|
|
|
|
for (auto module : design->selected_modules())
|
|
|
|
{
|
|
|
|
SigMap sigmap(module);
|
|
|
|
dict<SigBit, State> init_bits;
|
|
|
|
pool<SigBit> cleanup_bits;
|
2015-08-18 06:50:15 -05:00
|
|
|
pool<SigBit> used_bits;
|
2015-04-04 12:00:15 -05:00
|
|
|
|
2015-08-18 06:50:15 -05:00
|
|
|
for (auto wire : module->selected_wires()) {
|
2020-04-02 11:51:32 -05:00
|
|
|
if (wire->attributes.count(ID::init)) {
|
|
|
|
Const value = wire->attributes.at(ID::init);
|
2015-10-25 13:30:49 -05:00
|
|
|
for (int i = 0; i < min(GetSize(value), GetSize(wire)); i++)
|
2019-05-03 10:06:16 -05:00
|
|
|
if (value[i] != State::Sx)
|
|
|
|
init_bits[sigmap(SigBit(wire, i))] = value[i];
|
2015-04-04 12:00:15 -05:00
|
|
|
}
|
2015-08-18 06:50:15 -05:00
|
|
|
if (wire->port_output)
|
|
|
|
for (auto bit : sigmap(wire))
|
|
|
|
used_bits.insert(bit);
|
|
|
|
}
|
2015-04-04 12:00:15 -05:00
|
|
|
|
|
|
|
for (auto cell : module->selected_cells())
|
|
|
|
{
|
2015-08-18 06:50:15 -05:00
|
|
|
for (auto it : cell->connections())
|
|
|
|
if (!cell->known() || cell->input(it.first))
|
|
|
|
for (auto bit : sigmap(it.second))
|
|
|
|
used_bits.insert(bit);
|
|
|
|
|
2015-04-04 12:00:15 -05:00
|
|
|
if (ff_types.count(cell->type) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (auto &it : ff_types[cell->type])
|
|
|
|
{
|
|
|
|
if (!cell->hasPort(it.first))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
SigSpec sig = sigmap(cell->getPort(it.first));
|
|
|
|
Const value;
|
|
|
|
|
|
|
|
if (cell->hasParam(it.second))
|
|
|
|
value = cell->getParam(it.second);
|
|
|
|
|
|
|
|
for (int i = 0; i < GetSize(sig); i++) {
|
|
|
|
if (init_bits.count(sig[i]) == 0)
|
|
|
|
continue;
|
2015-11-08 12:16:56 -06:00
|
|
|
while (GetSize(value.bits) <= i)
|
2015-04-04 12:00:15 -05:00
|
|
|
value.bits.push_back(State::S0);
|
2019-05-03 10:05:37 -05:00
|
|
|
if (noreinit && value.bits[i] != State::Sx && value.bits[i] != init_bits.at(sig[i]))
|
|
|
|
log_error("Trying to assign a different init value for %s.%s.%s which technically "
|
|
|
|
"have a conflicted init value.\n",
|
|
|
|
log_id(module), log_id(cell), log_id(it.second));
|
2015-04-04 12:00:15 -05:00
|
|
|
value.bits[i] = init_bits.at(sig[i]);
|
|
|
|
cleanup_bits.insert(sig[i]);
|
|
|
|
}
|
|
|
|
|
2018-01-09 11:42:19 -06:00
|
|
|
if (highlow_mode && GetSize(value) != 0) {
|
|
|
|
if (GetSize(value) != 1)
|
|
|
|
log_error("Multi-bit init value for %s.%s.%s is incompatible with -highlow mode.\n",
|
|
|
|
log_id(module), log_id(cell), log_id(it.second));
|
|
|
|
if (value[0] == State::S1)
|
2018-12-18 01:37:43 -06:00
|
|
|
value = Const(high_string);
|
2018-01-09 11:42:19 -06:00
|
|
|
else
|
2018-12-18 01:37:43 -06:00
|
|
|
value = Const(low_string);
|
2018-01-09 11:42:19 -06:00
|
|
|
}
|
|
|
|
|
2020-04-14 09:33:09 -05:00
|
|
|
if (value.size() != 0) {
|
|
|
|
log("Setting %s.%s.%s (port=%s, net=%s) to %s.\n", log_id(module), log_id(cell), log_id(it.second),
|
|
|
|
log_id(it.first), log_signal(sig), log_signal(value));
|
|
|
|
cell->setParam(it.second, value);
|
|
|
|
}
|
2015-04-04 12:00:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto wire : module->selected_wires())
|
2020-04-02 11:51:32 -05:00
|
|
|
if (wire->attributes.count(ID::init)) {
|
|
|
|
Const &value = wire->attributes.at(ID::init);
|
2015-04-04 12:00:15 -05:00
|
|
|
bool do_cleanup = true;
|
2015-10-25 13:30:49 -05:00
|
|
|
for (int i = 0; i < min(GetSize(value), GetSize(wire)); i++) {
|
2015-08-18 06:50:15 -05:00
|
|
|
SigBit bit = sigmap(SigBit(wire, i));
|
|
|
|
if (cleanup_bits.count(bit) || !used_bits.count(bit))
|
|
|
|
value[i] = State::Sx;
|
|
|
|
else if (value[i] != State::Sx)
|
2015-04-04 12:00:15 -05:00
|
|
|
do_cleanup = false;
|
2015-08-18 06:50:15 -05:00
|
|
|
}
|
2015-04-04 12:00:15 -05:00
|
|
|
if (do_cleanup) {
|
|
|
|
log("Removing init attribute from wire %s.%s.\n", log_id(module), log_id(wire));
|
2020-04-02 11:51:32 -05:00
|
|
|
wire->attributes.erase(ID::init);
|
2015-04-04 12:00:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} DffinitPass;
|
2015-07-02 04:14:30 -05:00
|
|
|
|
2015-04-04 12:00:15 -05:00
|
|
|
PRIVATE_NAMESPACE_END
|