2014-01-17 16:14:36 -06:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2014-01-17 16:14:36 -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
|
|
|
*
|
2014-01-17 16:14:36 -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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kernel/register.h"
|
|
|
|
#include "kernel/celltypes.h"
|
|
|
|
#include "kernel/sigtools.h"
|
|
|
|
#include "kernel/rtlil.h"
|
|
|
|
#include "kernel/log.h"
|
|
|
|
|
2018-06-01 04:49:58 -05:00
|
|
|
#define MODE_ZERO 0
|
|
|
|
#define MODE_ONE 1
|
|
|
|
#define MODE_UNDEF 2
|
|
|
|
#define MODE_RANDOM 3
|
|
|
|
#define MODE_ANYSEQ 4
|
|
|
|
#define MODE_ANYCONST 5
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
2014-07-23 08:36:09 -05:00
|
|
|
struct SetundefWorker
|
2014-01-17 16:14:36 -06:00
|
|
|
{
|
2014-07-23 08:36:09 -05:00
|
|
|
int next_bit_mode;
|
|
|
|
uint32_t next_bit_state;
|
2017-05-28 04:59:05 -05:00
|
|
|
vector<SigSpec*> siglist;
|
2014-01-17 16:14:36 -06:00
|
|
|
|
2014-07-23 08:36:09 -05:00
|
|
|
RTLIL::State next_bit()
|
|
|
|
{
|
2018-06-01 04:49:58 -05:00
|
|
|
if (next_bit_mode == MODE_ZERO)
|
2014-07-23 08:36:09 -05:00
|
|
|
return RTLIL::State::S0;
|
2014-01-17 16:14:36 -06:00
|
|
|
|
2018-06-01 04:49:58 -05:00
|
|
|
if (next_bit_mode == MODE_ONE)
|
2014-07-23 08:36:09 -05:00
|
|
|
return RTLIL::State::S1;
|
2014-01-17 16:14:36 -06:00
|
|
|
|
2018-06-01 04:49:58 -05:00
|
|
|
if (next_bit_mode == MODE_UNDEF)
|
2018-03-12 07:52:35 -05:00
|
|
|
return RTLIL::State::Sx;
|
|
|
|
|
2018-06-01 04:49:58 -05:00
|
|
|
if (next_bit_mode == MODE_RANDOM)
|
|
|
|
{
|
|
|
|
// xorshift32
|
|
|
|
next_bit_state ^= next_bit_state << 13;
|
|
|
|
next_bit_state ^= next_bit_state >> 17;
|
|
|
|
next_bit_state ^= next_bit_state << 5;
|
|
|
|
log_assert(next_bit_state != 0);
|
|
|
|
|
|
|
|
return ((next_bit_state >> (next_bit_state & 15)) & 16) ? RTLIL::State::S0 : RTLIL::State::S1;
|
|
|
|
}
|
2014-07-23 08:36:09 -05:00
|
|
|
|
2018-06-01 04:49:58 -05:00
|
|
|
log_abort();
|
2014-07-23 08:36:09 -05:00
|
|
|
}
|
2014-01-17 16:14:36 -06:00
|
|
|
|
|
|
|
void operator()(RTLIL::SigSpec &sig)
|
|
|
|
{
|
2018-06-01 04:49:58 -05:00
|
|
|
if (next_bit_mode == MODE_ANYSEQ || next_bit_mode == MODE_ANYCONST) {
|
2017-05-28 04:59:05 -05:00
|
|
|
siglist.push_back(&sig);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-23 08:36:09 -05:00
|
|
|
for (auto &bit : sig)
|
|
|
|
if (bit.wire == NULL && bit.data > RTLIL::State::S1)
|
|
|
|
bit = next_bit();
|
2014-01-17 16:14:36 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SetundefPass : public Pass {
|
|
|
|
SetundefPass() : Pass("setundef", "replace undef values with defined constants") { }
|
|
|
|
virtual void help()
|
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
|
|
|
log(" setundef [options] [selection]\n");
|
|
|
|
log("\n");
|
2017-05-28 04:31:35 -05:00
|
|
|
log("This command replaces undef (x) constants with defined (0/1) constants.\n");
|
2014-01-17 16:14:36 -06:00
|
|
|
log("\n");
|
|
|
|
log(" -undriven\n");
|
|
|
|
log(" also set undriven nets to constant values\n");
|
|
|
|
log("\n");
|
|
|
|
log(" -zero\n");
|
|
|
|
log(" replace with bits cleared (0)\n");
|
|
|
|
log("\n");
|
|
|
|
log(" -one\n");
|
|
|
|
log(" replace with bits set (1)\n");
|
|
|
|
log("\n");
|
2018-03-12 07:52:35 -05:00
|
|
|
log(" -undef\n");
|
|
|
|
log(" replace with undef (x) bits, may be used with -undriven\n");
|
|
|
|
log("\n");
|
2017-05-28 04:59:05 -05:00
|
|
|
log(" -anyseq\n");
|
|
|
|
log(" replace with $anyseq drivers (for formal)\n");
|
|
|
|
log("\n");
|
2018-06-01 04:49:58 -05:00
|
|
|
log(" -anyconst\n");
|
|
|
|
log(" replace with $anyconst drivers (for formal)\n");
|
|
|
|
log("\n");
|
2014-01-17 16:14:36 -06:00
|
|
|
log(" -random <seed>\n");
|
|
|
|
log(" replace with random bits using the specified integer als seed\n");
|
|
|
|
log(" value for the random number generator.\n");
|
|
|
|
log("\n");
|
2016-06-03 04:38:31 -05:00
|
|
|
log(" -init\n");
|
|
|
|
log(" also create/update init values for flip-flops\n");
|
|
|
|
log("\n");
|
2014-01-17 16:14:36 -06:00
|
|
|
}
|
|
|
|
virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
|
|
|
|
{
|
|
|
|
bool got_value = false;
|
|
|
|
bool undriven_mode = false;
|
2016-06-03 04:38:31 -05:00
|
|
|
bool init_mode = false;
|
2014-07-23 08:36:09 -05:00
|
|
|
SetundefWorker worker;
|
2014-01-17 16:14:36 -06:00
|
|
|
|
2016-11-08 11:53:36 -06:00
|
|
|
log_header(design, "Executing SETUNDEF pass (replace undef values with defined constants).\n");
|
|
|
|
|
2014-01-17 16:14:36 -06:00
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++)
|
|
|
|
{
|
|
|
|
if (args[argidx] == "-undriven") {
|
|
|
|
undriven_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (args[argidx] == "-zero") {
|
|
|
|
got_value = true;
|
2018-06-01 04:49:58 -05:00
|
|
|
worker.next_bit_mode = MODE_ZERO;
|
2018-05-13 09:36:12 -05:00
|
|
|
worker.next_bit_state = 0;
|
2014-01-17 16:14:36 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (args[argidx] == "-one") {
|
|
|
|
got_value = true;
|
2018-06-01 04:49:58 -05:00
|
|
|
worker.next_bit_mode = MODE_ONE;
|
2018-05-13 09:36:12 -05:00
|
|
|
worker.next_bit_state = 0;
|
2014-01-17 16:14:36 -06:00
|
|
|
continue;
|
|
|
|
}
|
2017-05-28 04:59:05 -05:00
|
|
|
if (args[argidx] == "-anyseq") {
|
|
|
|
got_value = true;
|
2018-06-01 04:49:58 -05:00
|
|
|
worker.next_bit_mode = MODE_ANYSEQ;
|
|
|
|
worker.next_bit_state = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (args[argidx] == "-anyconst") {
|
|
|
|
got_value = true;
|
|
|
|
worker.next_bit_mode = MODE_ANYCONST;
|
2018-05-13 09:36:12 -05:00
|
|
|
worker.next_bit_state = 0;
|
2017-05-28 04:59:05 -05:00
|
|
|
continue;
|
|
|
|
}
|
2018-03-12 07:52:35 -05:00
|
|
|
if (args[argidx] == "-undef") {
|
|
|
|
got_value = true;
|
2018-06-01 04:49:58 -05:00
|
|
|
worker.next_bit_mode = MODE_UNDEF;
|
2018-05-13 09:36:12 -05:00
|
|
|
worker.next_bit_state = 0;
|
2018-03-12 07:52:35 -05:00
|
|
|
continue;
|
|
|
|
}
|
2016-06-03 04:38:31 -05:00
|
|
|
if (args[argidx] == "-init") {
|
|
|
|
init_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
2014-01-17 16:14:36 -06:00
|
|
|
if (args[argidx] == "-random" && !got_value && argidx+1 < args.size()) {
|
|
|
|
got_value = true;
|
2018-06-01 04:49:58 -05:00
|
|
|
worker.next_bit_mode = MODE_RANDOM;
|
2014-07-23 08:36:09 -05:00
|
|
|
worker.next_bit_state = atoi(args[++argidx].c_str()) + 1;
|
2014-01-17 16:14:36 -06:00
|
|
|
for (int i = 0; i < 10; i++)
|
2014-07-23 08:36:09 -05:00
|
|
|
worker.next_bit();
|
2014-01-17 16:14:36 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(args, argidx, design);
|
|
|
|
|
|
|
|
if (!got_value)
|
2017-05-28 04:59:05 -05:00
|
|
|
log_cmd_error("One of the options -zero, -one, -anyseq, or -random <seed> must be specified.\n");
|
2014-01-17 16:14:36 -06:00
|
|
|
|
2016-06-03 04:38:31 -05:00
|
|
|
for (auto module : design->selected_modules())
|
2014-01-17 16:14:36 -06:00
|
|
|
{
|
|
|
|
if (undriven_mode)
|
|
|
|
{
|
|
|
|
if (!module->processes.empty())
|
|
|
|
log_error("The 'setundef' command can't operate in -undriven mode on modules with processes. Run 'proc' first.\n");
|
|
|
|
|
|
|
|
SigMap sigmap(module);
|
|
|
|
SigPool undriven_signals;
|
|
|
|
|
2014-07-26 18:49:51 -05:00
|
|
|
for (auto &it : module->wires_)
|
2016-11-08 11:53:36 -06:00
|
|
|
undriven_signals.add(sigmap(it.second));
|
|
|
|
|
|
|
|
for (auto &it : module->wires_)
|
|
|
|
if (it.second->port_input)
|
|
|
|
undriven_signals.del(sigmap(it.second));
|
2014-01-17 16:14:36 -06:00
|
|
|
|
|
|
|
CellTypes ct(design);
|
2014-07-26 18:51:45 -05:00
|
|
|
for (auto &it : module->cells_)
|
2014-07-26 07:32:50 -05:00
|
|
|
for (auto &conn : it.second->connections())
|
2014-01-17 16:14:36 -06:00
|
|
|
if (!ct.cell_known(it.second->type) || ct.cell_output(it.second->type, conn.first))
|
|
|
|
undriven_signals.del(sigmap(conn.second));
|
|
|
|
|
|
|
|
RTLIL::SigSpec sig = undriven_signals.export_all();
|
2014-07-22 13:15:14 -05:00
|
|
|
for (auto &c : sig.chunks()) {
|
2014-01-17 16:14:36 -06:00
|
|
|
RTLIL::SigSpec bits;
|
|
|
|
for (int i = 0; i < c.width; i++)
|
2014-07-23 08:36:09 -05:00
|
|
|
bits.append(worker.next_bit());
|
2014-07-26 07:32:50 -05:00
|
|
|
module->connect(RTLIL::SigSig(c, bits));
|
2014-01-17 16:14:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 04:38:31 -05:00
|
|
|
if (init_mode)
|
|
|
|
{
|
|
|
|
SigMap sigmap(module);
|
|
|
|
pool<SigBit> ffbits;
|
|
|
|
pool<Wire*> initwires;
|
|
|
|
|
|
|
|
pool<IdString> fftypes;
|
|
|
|
fftypes.insert("$dff");
|
|
|
|
fftypes.insert("$dffe");
|
|
|
|
fftypes.insert("$dffsr");
|
|
|
|
fftypes.insert("$adff");
|
|
|
|
|
|
|
|
std::vector<char> list_np = {'N', 'P'}, list_01 = {'0', '1'};
|
|
|
|
|
|
|
|
for (auto c1 : list_np)
|
|
|
|
fftypes.insert(stringf("$_DFF_%c_", c1));
|
|
|
|
|
|
|
|
for (auto c1 : list_np)
|
|
|
|
for (auto c2 : list_np)
|
|
|
|
fftypes.insert(stringf("$_DFFE_%c%c_", c1, c2));
|
|
|
|
|
|
|
|
for (auto c1 : list_np)
|
|
|
|
for (auto c2 : list_np)
|
|
|
|
for (auto c3 : list_01)
|
|
|
|
fftypes.insert(stringf("$_DFF_%c%c%c_", c1, c2, c3));
|
|
|
|
|
|
|
|
for (auto c1 : list_np)
|
|
|
|
for (auto c2 : list_np)
|
|
|
|
for (auto c3 : list_np)
|
|
|
|
fftypes.insert(stringf("$_DFFSR_%c%c%c_", c1, c2, c3));
|
|
|
|
|
|
|
|
for (auto cell : module->cells())
|
|
|
|
{
|
|
|
|
if (!fftypes.count(cell->type))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (auto bit : sigmap(cell->getPort("\\Q")))
|
|
|
|
ffbits.insert(bit);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto wire : module->wires())
|
|
|
|
{
|
|
|
|
if (!wire->attributes.count("\\init"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (auto bit : sigmap(wire))
|
|
|
|
ffbits.erase(bit);
|
|
|
|
|
|
|
|
initwires.insert(wire);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int wire_types = 0; wire_types < 2; wire_types++)
|
|
|
|
for (auto wire : module->wires())
|
|
|
|
{
|
|
|
|
if (wire->name[0] == (wire_types ? '\\' : '$'))
|
|
|
|
next_wire:
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (auto bit : sigmap(wire))
|
|
|
|
if (!ffbits.count(bit))
|
|
|
|
goto next_wire;
|
|
|
|
|
|
|
|
for (auto bit : sigmap(wire))
|
|
|
|
ffbits.erase(bit);
|
|
|
|
|
|
|
|
initwires.insert(wire);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto wire : initwires)
|
|
|
|
{
|
|
|
|
Const &initval = wire->attributes["\\init"];
|
|
|
|
|
|
|
|
for (int i = 0; i < GetSize(wire); i++)
|
|
|
|
if (GetSize(initval) <= i)
|
|
|
|
initval.bits.push_back(worker.next_bit());
|
|
|
|
else if (initval.bits[i] == State::Sx)
|
|
|
|
initval.bits[i] = worker.next_bit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-23 08:36:09 -05:00
|
|
|
module->rewrite_sigspecs(worker);
|
2017-05-28 04:59:05 -05:00
|
|
|
|
2018-06-01 04:49:58 -05:00
|
|
|
if (worker.next_bit_mode == MODE_ANYSEQ || worker.next_bit_mode == MODE_ANYCONST)
|
2017-05-28 04:59:05 -05:00
|
|
|
{
|
|
|
|
vector<SigSpec*> siglist;
|
|
|
|
siglist.swap(worker.siglist);
|
|
|
|
|
|
|
|
for (auto sigptr : siglist)
|
|
|
|
{
|
|
|
|
SigSpec &sig = *sigptr;
|
|
|
|
int cursor = 0;
|
|
|
|
|
|
|
|
while (cursor < GetSize(sig))
|
|
|
|
{
|
|
|
|
int width = 0;
|
|
|
|
while (cursor+width < GetSize(sig) && sig[cursor+width] == State::Sx)
|
|
|
|
width++;
|
|
|
|
|
|
|
|
if (width > 0) {
|
2018-06-01 04:49:58 -05:00
|
|
|
if (worker.next_bit_mode == MODE_ANYSEQ)
|
|
|
|
sig.replace(cursor, module->Anyseq(NEW_ID, width));
|
|
|
|
else
|
|
|
|
sig.replace(cursor, module->Anyconst(NEW_ID, width));
|
2017-05-28 04:59:05 -05:00
|
|
|
cursor += width;
|
|
|
|
} else {
|
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-17 16:14:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} SetundefPass;
|
2015-07-02 04:14:30 -05:00
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
PRIVATE_NAMESPACE_END
|