2015-11-26 11:11:06 -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/yosys.h"
|
|
|
|
#include "kernel/sigtools.h"
|
|
|
|
|
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
struct Ice40FfinitPass : public Pass {
|
|
|
|
Ice40FfinitPass() : Pass("ice40_ffinit", "iCE40: handle FF init values") { }
|
2020-06-18 18:34:52 -05:00
|
|
|
void help() override
|
2015-11-26 11:11:06 -06:00
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
|
|
|
log(" ice40_ffinit [options] [selection]\n");
|
|
|
|
log("\n");
|
|
|
|
log("Remove zero init values for FF output signals. Add inverters to implement\n");
|
|
|
|
log("nonzero init values.\n");
|
|
|
|
log("\n");
|
|
|
|
}
|
2020-06-18 18:34:52 -05:00
|
|
|
void execute(std::vector<std::string> args, RTLIL::Design *design) override
|
2015-11-26 11:11:06 -06:00
|
|
|
{
|
2016-04-21 16:28:37 -05:00
|
|
|
log_header(design, "Executing ICE40_FFINIT pass (implement FF init values).\n");
|
2015-11-26 11:11:06 -06:00
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++)
|
|
|
|
{
|
|
|
|
// if (args[argidx] == "-singleton") {
|
|
|
|
// singleton_mode = true;
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(args, argidx, design);
|
|
|
|
|
|
|
|
for (auto module : design->selected_modules())
|
|
|
|
{
|
|
|
|
log("Handling FF init values in %s.\n", log_id(module));
|
|
|
|
|
|
|
|
SigMap sigmap(module);
|
|
|
|
pool<Wire*> init_wires;
|
|
|
|
dict<SigBit, State> initbits;
|
2016-06-30 02:58:13 -05:00
|
|
|
dict<SigBit, SigBit> initbit_to_wire;
|
2015-11-26 11:11:06 -06:00
|
|
|
pool<SigBit> handled_initbits;
|
|
|
|
|
|
|
|
for (auto wire : module->selected_wires())
|
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
if (wire->attributes.count(ID::init) == 0)
|
2015-11-26 11:11:06 -06:00
|
|
|
continue;
|
|
|
|
|
|
|
|
SigSpec wirebits = sigmap(wire);
|
2020-04-02 11:51:32 -05:00
|
|
|
Const initval = wire->attributes.at(ID::init);
|
2015-11-26 11:11:06 -06:00
|
|
|
init_wires.insert(wire);
|
|
|
|
|
|
|
|
for (int i = 0; i < GetSize(wirebits) && i < GetSize(initval); i++)
|
|
|
|
{
|
|
|
|
SigBit bit = wirebits[i];
|
|
|
|
State val = initval[i];
|
|
|
|
|
|
|
|
if (val != State::S0 && val != State::S1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (initbits.count(bit)) {
|
2019-12-30 19:38:10 -06:00
|
|
|
if (initbits.at(bit) != val) {
|
|
|
|
log_warning("Conflicting init values for signal %s (%s = %s, %s = %s).\n",
|
2016-06-30 02:58:13 -05:00
|
|
|
log_signal(bit), log_signal(SigBit(wire, i)), log_signal(val),
|
|
|
|
log_signal(initbit_to_wire[bit]), log_signal(initbits.at(bit)));
|
2019-12-30 19:38:10 -06:00
|
|
|
initbits.at(bit) = State::Sx;
|
|
|
|
}
|
2015-11-26 11:11:06 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
initbits[bit] = val;
|
2016-06-30 02:58:13 -05:00
|
|
|
initbit_to_wire[bit] = SigBit(wire, i);
|
2015-11-26 11:11:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-22 04:15:25 -06:00
|
|
|
pool<IdString> sb_dff_types = {
|
2020-04-02 11:51:32 -05:00
|
|
|
ID(SB_DFF), ID(SB_DFFE), ID(SB_DFFSR), ID(SB_DFFR), ID(SB_DFFSS), ID(SB_DFFS), ID(SB_DFFESR),
|
|
|
|
ID(SB_DFFER), ID(SB_DFFESS), ID(SB_DFFES), ID(SB_DFFN), ID(SB_DFFNE), ID(SB_DFFNSR), ID(SB_DFFNR),
|
|
|
|
ID(SB_DFFNSS), ID(SB_DFFNS), ID(SB_DFFNESR), ID(SB_DFFNER), ID(SB_DFFNESS), ID(SB_DFFNES)
|
2015-12-22 04:15:25 -06:00
|
|
|
};
|
|
|
|
|
2015-11-26 11:11:06 -06:00
|
|
|
for (auto cell : module->selected_cells())
|
|
|
|
{
|
2015-12-22 04:15:25 -06:00
|
|
|
if (!sb_dff_types.count(cell->type))
|
2015-11-26 11:11:06 -06:00
|
|
|
continue;
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
SigSpec sig_d = cell->getPort(ID::D);
|
|
|
|
SigSpec sig_q = cell->getPort(ID::Q);
|
2015-11-26 11:11:06 -06:00
|
|
|
|
2016-07-08 07:41:36 -05:00
|
|
|
if (GetSize(sig_d) < 1 || GetSize(sig_q) < 1)
|
2015-11-26 11:11:06 -06:00
|
|
|
continue;
|
|
|
|
|
2016-07-08 07:41:36 -05:00
|
|
|
SigBit bit_d = sigmap(sig_d[0]);
|
|
|
|
SigBit bit_q = sigmap(sig_q[0]);
|
|
|
|
|
|
|
|
if (!initbits.count(bit_q))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
State val = initbits.at(bit_q);
|
2019-12-30 19:38:10 -06:00
|
|
|
|
|
|
|
if (val == State::Sx)
|
|
|
|
continue;
|
|
|
|
|
2016-07-08 07:41:36 -05:00
|
|
|
handled_initbits.insert(bit_q);
|
2015-11-26 11:11:06 -06:00
|
|
|
|
|
|
|
log("FF init value for cell %s (%s): %s = %c\n", log_id(cell), log_id(cell->type),
|
2016-07-08 07:41:36 -05:00
|
|
|
log_signal(bit_q), val != State::S0 ? '1' : '0');
|
2015-11-26 11:11:06 -06:00
|
|
|
|
|
|
|
if (val == State::S0)
|
|
|
|
continue;
|
|
|
|
|
2015-12-22 04:15:25 -06:00
|
|
|
string type_str = cell->type.str();
|
|
|
|
|
|
|
|
if (type_str.back() == 'S') {
|
|
|
|
type_str.back() = 'R';
|
|
|
|
cell->type = type_str;
|
2020-04-02 11:51:32 -05:00
|
|
|
cell->setPort(ID::R, cell->getPort(ID::S));
|
2020-03-12 14:57:01 -05:00
|
|
|
cell->unsetPort(ID::S);
|
2015-12-22 04:15:25 -06:00
|
|
|
} else
|
|
|
|
if (type_str.back() == 'R') {
|
|
|
|
type_str.back() = 'S';
|
|
|
|
cell->type = type_str;
|
2020-04-02 11:51:32 -05:00
|
|
|
cell->setPort(ID::S, cell->getPort(ID::R));
|
|
|
|
cell->unsetPort(ID::R);
|
2015-12-22 04:15:25 -06:00
|
|
|
}
|
|
|
|
|
2016-07-08 07:41:36 -05:00
|
|
|
Wire *new_bit_d = module->addWire(NEW_ID);
|
|
|
|
Wire *new_bit_q = module->addWire(NEW_ID);
|
2015-11-26 11:11:06 -06:00
|
|
|
|
2016-07-08 07:41:36 -05:00
|
|
|
module->addNotGate(NEW_ID, bit_d, new_bit_d);
|
|
|
|
module->addNotGate(NEW_ID, new_bit_q, bit_q);
|
2015-11-26 11:11:06 -06:00
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
cell->setPort(ID::D, new_bit_d);
|
|
|
|
cell->setPort(ID::Q, new_bit_q);
|
2015-11-26 11:11:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto wire : init_wires)
|
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
if (wire->attributes.count(ID::init) == 0)
|
2015-11-26 11:11:06 -06:00
|
|
|
continue;
|
|
|
|
|
|
|
|
SigSpec wirebits = sigmap(wire);
|
2020-04-02 11:51:32 -05:00
|
|
|
Const &initval = wire->attributes.at(ID::init);
|
2015-11-26 11:11:06 -06:00
|
|
|
bool remove_attribute = true;
|
|
|
|
|
|
|
|
for (int i = 0; i < GetSize(wirebits) && i < GetSize(initval); i++) {
|
|
|
|
if (handled_initbits.count(wirebits[i]))
|
2015-12-22 05:18:06 -06:00
|
|
|
initval[i] = State::Sx;
|
|
|
|
else if (initval[i] != State::Sx)
|
2015-11-26 11:11:06 -06:00
|
|
|
remove_attribute = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remove_attribute)
|
2020-04-02 11:51:32 -05:00
|
|
|
wire->attributes.erase(ID::init);
|
2015-11-26 11:11:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} Ice40FfinitPass;
|
|
|
|
|
|
|
|
PRIVATE_NAMESPACE_END
|