2015-02-13 07:34:51 -06:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
2021-06-07 17:39:36 -05:00
|
|
|
* Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2015-02-13 07:34:51 -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
|
|
|
*
|
2015-02-13 07:34:51 -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/yosys.h"
|
|
|
|
#include "kernel/sigtools.h"
|
|
|
|
#include "kernel/celltypes.h"
|
|
|
|
#include "kernel/utils.h"
|
|
|
|
|
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
struct CheckPass : public Pass {
|
|
|
|
CheckPass() : Pass("check", "check for obvious problems in the design") { }
|
2020-06-18 18:34:52 -05:00
|
|
|
void help() override
|
2015-02-13 07:34:51 -06:00
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
2015-02-15 05:58:12 -06:00
|
|
|
log(" check [options] [selection]\n");
|
2015-02-13 07:34:51 -06:00
|
|
|
log("\n");
|
|
|
|
log("This pass identifies the following problems in the current design:\n");
|
|
|
|
log("\n");
|
2020-11-02 00:33:03 -06:00
|
|
|
log(" - combinatorial loops\n");
|
|
|
|
log(" - two or more conflicting drivers for one wire\n");
|
|
|
|
log(" - used wires that do not have a driver\n");
|
2015-02-13 07:34:51 -06:00
|
|
|
log("\n");
|
2019-10-03 04:49:56 -05:00
|
|
|
log("Options:\n");
|
2015-02-15 05:58:12 -06:00
|
|
|
log("\n");
|
2020-11-02 00:33:03 -06:00
|
|
|
log(" -noinit\n");
|
|
|
|
log(" also check for wires which have the 'init' attribute set\n");
|
2017-01-04 11:12:41 -06:00
|
|
|
log("\n");
|
2020-11-02 00:33:03 -06:00
|
|
|
log(" -initdrv\n");
|
|
|
|
log(" also check for wires that have the 'init' attribute set and are not\n");
|
|
|
|
log(" driven by an FF cell type\n");
|
2019-10-02 06:35:03 -05:00
|
|
|
log("\n");
|
2020-11-02 00:33:03 -06:00
|
|
|
log(" -mapped\n");
|
|
|
|
log(" also check for internal cells that have not been mapped to cells of the\n");
|
|
|
|
log(" target architecture\n");
|
2019-10-03 04:49:56 -05:00
|
|
|
log("\n");
|
2020-11-02 00:33:03 -06:00
|
|
|
log(" -allow-tbuf\n");
|
|
|
|
log(" modify the -mapped behavior to still allow $_TBUF_ cells\n");
|
2019-10-03 04:49:56 -05:00
|
|
|
log("\n");
|
2020-11-02 00:33:03 -06:00
|
|
|
log(" -assert\n");
|
|
|
|
log(" produce a runtime error if any problems are found in the current design\n");
|
2015-02-22 06:02:48 -06:00
|
|
|
log("\n");
|
2015-02-13 07:34:51 -06:00
|
|
|
}
|
2020-06-18 18:34:52 -05:00
|
|
|
void execute(std::vector<std::string> args, RTLIL::Design *design) override
|
2015-02-13 07:34:51 -06:00
|
|
|
{
|
|
|
|
int counter = 0;
|
2015-02-15 05:58:12 -06:00
|
|
|
bool noinit = false;
|
2017-01-04 11:12:41 -06:00
|
|
|
bool initdrv = false;
|
2019-10-02 06:35:03 -05:00
|
|
|
bool mapped = false;
|
2019-10-03 04:49:56 -05:00
|
|
|
bool allow_tbuf = false;
|
2015-02-22 06:00:41 -06:00
|
|
|
bool assert_mode = false;
|
2015-02-13 07:34:51 -06:00
|
|
|
|
2015-02-15 05:58:12 -06:00
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++) {
|
|
|
|
if (args[argidx] == "-noinit") {
|
|
|
|
noinit = true;
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-04 11:12:41 -06:00
|
|
|
if (args[argidx] == "-initdrv") {
|
|
|
|
initdrv = true;
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-02 06:35:03 -05:00
|
|
|
if (args[argidx] == "-mapped") {
|
|
|
|
mapped = true;
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-03 04:49:56 -05:00
|
|
|
if (args[argidx] == "-allow-tbuf") {
|
|
|
|
allow_tbuf = true;
|
|
|
|
continue;
|
|
|
|
}
|
2015-02-22 06:00:41 -06:00
|
|
|
if (args[argidx] == "-assert") {
|
|
|
|
assert_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
2015-02-15 05:58:12 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(args, argidx, design);
|
2015-02-13 07:34:51 -06:00
|
|
|
|
2016-04-21 16:28:37 -05:00
|
|
|
log_header(design, "Executing CHECK pass (checking for obvious problems).\n");
|
2015-02-13 07:34:51 -06:00
|
|
|
|
|
|
|
for (auto module : design->selected_whole_modules_warn())
|
|
|
|
{
|
2020-11-02 00:33:03 -06:00
|
|
|
log("Checking module %s...\n", log_id(module));
|
2015-02-13 07:34:51 -06:00
|
|
|
|
|
|
|
SigMap sigmap(module);
|
|
|
|
dict<SigBit, vector<string>> wire_drivers;
|
2015-07-27 02:54:58 -05:00
|
|
|
dict<SigBit, int> wire_drivers_count;
|
2015-02-13 07:34:51 -06:00
|
|
|
pool<SigBit> used_wires;
|
|
|
|
TopoSort<string> topo;
|
|
|
|
|
2020-11-03 09:36:27 -06:00
|
|
|
for (auto &proc_it : module->processes)
|
|
|
|
{
|
|
|
|
std::vector<RTLIL::CaseRule*> all_cases = {&proc_it.second->root_case};
|
|
|
|
for (size_t i = 0; i < all_cases.size(); i++) {
|
|
|
|
for (auto action : all_cases[i]->actions) {
|
|
|
|
for (auto bit : sigmap(action.first))
|
2023-06-23 11:07:28 -05:00
|
|
|
wire_drivers[bit].push_back(
|
|
|
|
stringf("action %s <= %s (case rule) in process %s",
|
|
|
|
log_signal(action.first), log_signal(action.second), log_id(proc_it.first)));
|
|
|
|
|
2020-11-03 09:36:27 -06:00
|
|
|
for (auto bit : sigmap(action.second))
|
|
|
|
if (bit.wire) used_wires.insert(bit);
|
|
|
|
}
|
|
|
|
for (auto switch_ : all_cases[i]->switches) {
|
|
|
|
for (auto case_ : switch_->cases) {
|
|
|
|
all_cases.push_back(case_);
|
|
|
|
for (auto compare : case_->compare)
|
|
|
|
for (auto bit : sigmap(compare))
|
|
|
|
if (bit.wire) used_wires.insert(bit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto &sync : proc_it.second->syncs) {
|
|
|
|
for (auto bit : sigmap(sync->signal))
|
|
|
|
if (bit.wire) used_wires.insert(bit);
|
|
|
|
for (auto action : sync->actions) {
|
|
|
|
for (auto bit : sigmap(action.first))
|
2023-06-23 11:07:28 -05:00
|
|
|
wire_drivers[bit].push_back(
|
|
|
|
stringf("action %s <= %s (sync rule) in process %s",
|
|
|
|
log_signal(action.first), log_signal(action.second), log_id(proc_it.first)));
|
2020-11-03 09:36:27 -06:00
|
|
|
for (auto bit : sigmap(action.second))
|
|
|
|
if (bit.wire) used_wires.insert(bit);
|
|
|
|
}
|
2021-02-22 17:21:46 -06:00
|
|
|
for (auto memwr : sync->mem_write_actions) {
|
|
|
|
for (auto bit : sigmap(memwr.address))
|
|
|
|
if (bit.wire) used_wires.insert(bit);
|
|
|
|
for (auto bit : sigmap(memwr.data))
|
|
|
|
if (bit.wire) used_wires.insert(bit);
|
|
|
|
for (auto bit : sigmap(memwr.enable))
|
|
|
|
if (bit.wire) used_wires.insert(bit);
|
|
|
|
}
|
2020-11-03 09:36:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-13 07:34:51 -06:00
|
|
|
for (auto cell : module->cells())
|
2019-10-02 06:35:03 -05:00
|
|
|
{
|
|
|
|
if (mapped && cell->type.begins_with("$") && design->module(cell->type) == nullptr) {
|
2019-10-03 04:49:56 -05:00
|
|
|
if (allow_tbuf && cell->type == ID($_TBUF_)) goto cell_allowed;
|
2019-10-02 06:35:03 -05:00
|
|
|
log_warning("Cell %s.%s is an unmapped internal cell of type %s.\n", log_id(module), log_id(cell), log_id(cell->type));
|
|
|
|
counter++;
|
2019-10-03 04:49:56 -05:00
|
|
|
cell_allowed:;
|
2019-10-02 06:35:03 -05:00
|
|
|
}
|
|
|
|
for (auto &conn : cell->connections()) {
|
|
|
|
SigSpec sig = sigmap(conn.second);
|
|
|
|
bool logic_cell = yosys_celltypes.cell_evaluable(cell->type);
|
|
|
|
if (cell->input(conn.first))
|
|
|
|
for (auto bit : sig)
|
|
|
|
if (bit.wire) {
|
|
|
|
if (logic_cell)
|
|
|
|
topo.edge(stringf("wire %s", log_signal(bit)),
|
|
|
|
stringf("cell %s (%s)", log_id(cell), log_id(cell->type)));
|
|
|
|
used_wires.insert(bit);
|
|
|
|
}
|
|
|
|
if (cell->output(conn.first))
|
|
|
|
for (int i = 0; i < GetSize(sig); i++) {
|
2015-02-13 07:34:51 -06:00
|
|
|
if (logic_cell)
|
2019-10-02 06:35:03 -05:00
|
|
|
topo.edge(stringf("cell %s (%s)", log_id(cell), log_id(cell->type)),
|
|
|
|
stringf("wire %s", log_signal(sig[i])));
|
2023-06-23 11:07:28 -05:00
|
|
|
|
|
|
|
if (sig[i].wire || !cell->input(conn.first))
|
2019-10-02 06:35:03 -05:00
|
|
|
wire_drivers[sig[i]].push_back(stringf("port %s[%d] of cell %s (%s)",
|
|
|
|
log_id(conn.first), i, log_id(cell), log_id(cell->type)));
|
2015-02-13 07:34:51 -06:00
|
|
|
}
|
2019-10-02 06:35:03 -05:00
|
|
|
if (!cell->input(conn.first) && cell->output(conn.first))
|
|
|
|
for (auto bit : sig)
|
|
|
|
if (bit.wire) wire_drivers_count[bit]++;
|
|
|
|
}
|
2015-02-13 07:34:51 -06:00
|
|
|
}
|
|
|
|
|
2017-01-04 11:12:41 -06:00
|
|
|
pool<SigBit> init_bits;
|
|
|
|
|
2015-02-13 07:34:51 -06:00
|
|
|
for (auto wire : module->wires()) {
|
|
|
|
if (wire->port_input) {
|
|
|
|
SigSpec sig = sigmap(wire);
|
|
|
|
for (int i = 0; i < GetSize(sig); i++)
|
2023-06-23 11:07:28 -05:00
|
|
|
if (sig[i].wire || !wire->port_output)
|
|
|
|
wire_drivers[sig[i]].push_back(stringf("module input %s[%d]", log_id(wire), i));
|
2015-02-13 07:34:51 -06:00
|
|
|
}
|
|
|
|
if (wire->port_output)
|
2015-02-13 07:40:49 -06:00
|
|
|
for (auto bit : sigmap(wire))
|
|
|
|
if (bit.wire) used_wires.insert(bit);
|
2015-07-27 02:54:58 -05:00
|
|
|
if (wire->port_input && !wire->port_output)
|
|
|
|
for (auto bit : sigmap(wire))
|
|
|
|
if (bit.wire) wire_drivers_count[bit]++;
|
2020-04-02 11:51:32 -05:00
|
|
|
if (wire->attributes.count(ID::init)) {
|
|
|
|
Const initval = wire->attributes.at(ID::init);
|
2017-01-04 11:12:41 -06:00
|
|
|
for (int i = 0; i < GetSize(initval) && i < GetSize(wire); i++)
|
|
|
|
if (initval[i] == State::S0 || initval[i] == State::S1)
|
|
|
|
init_bits.insert(sigmap(SigBit(wire, i)));
|
|
|
|
if (noinit) {
|
|
|
|
log_warning("Wire %s.%s has an unprocessed 'init' attribute.\n", log_id(module), log_id(wire));
|
|
|
|
counter++;
|
|
|
|
}
|
2015-02-15 05:58:12 -06:00
|
|
|
}
|
2015-02-13 07:34:51 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 11:07:28 -05:00
|
|
|
for (auto state : {State::S0, State::S1, State::Sx})
|
|
|
|
if (wire_drivers.count(state)) {
|
|
|
|
string message = stringf("Drivers conflicting with a constant %s driver:\n", log_signal(state));
|
|
|
|
for (auto str : wire_drivers[state])
|
|
|
|
message += stringf(" %s\n", str.c_str());
|
|
|
|
log_warning("%s", message.c_str());
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
|
2015-02-13 07:34:51 -06:00
|
|
|
for (auto it : wire_drivers)
|
2015-07-27 02:54:58 -05:00
|
|
|
if (wire_drivers_count[it.first] > 1) {
|
2015-02-13 07:34:51 -06:00
|
|
|
string message = stringf("multiple conflicting drivers for %s.%s:\n", log_id(module), log_signal(it.first));
|
|
|
|
for (auto str : it.second)
|
|
|
|
message += stringf(" %s\n", str.c_str());
|
|
|
|
log_warning("%s", message.c_str());
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto bit : used_wires)
|
|
|
|
if (!wire_drivers.count(bit)) {
|
|
|
|
log_warning("Wire %s.%s is used but has no driver.\n", log_id(module), log_signal(bit));
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
topo.sort();
|
|
|
|
for (auto &loop : topo.loops) {
|
|
|
|
string message = stringf("found logic loop in module %s:\n", log_id(module));
|
|
|
|
for (auto &str : loop)
|
|
|
|
message += stringf(" %s\n", str.c_str());
|
|
|
|
log_warning("%s", message.c_str());
|
|
|
|
counter++;
|
|
|
|
}
|
2017-01-04 11:12:41 -06:00
|
|
|
|
|
|
|
if (initdrv)
|
|
|
|
{
|
|
|
|
for (auto cell : module->cells())
|
|
|
|
{
|
2020-04-08 10:36:12 -05:00
|
|
|
if (RTLIL::builtin_ff_cell_types().count(cell->type) == 0)
|
2017-01-04 11:12:41 -06:00
|
|
|
continue;
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
for (auto bit : sigmap(cell->getPort(ID::Q)))
|
2017-01-04 11:12:41 -06:00
|
|
|
init_bits.erase(bit);
|
|
|
|
}
|
|
|
|
|
|
|
|
SigSpec init_sig(init_bits);
|
|
|
|
init_sig.sort_and_unify();
|
|
|
|
|
|
|
|
for (auto chunk : init_sig.chunks()) {
|
|
|
|
log_warning("Wire %s.%s has 'init' attribute and is not driven by an FF cell.\n", log_id(module), log_signal(chunk));
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
}
|
2015-02-13 07:34:51 -06:00
|
|
|
}
|
|
|
|
|
2020-11-02 00:33:03 -06:00
|
|
|
log("Found and reported %d problems.\n", counter);
|
2015-02-22 06:00:41 -06:00
|
|
|
|
2015-02-22 09:29:44 -06:00
|
|
|
if (assert_mode && counter > 0)
|
2015-02-22 06:00:41 -06:00
|
|
|
log_error("Found %d problems in 'check -assert'.\n", counter);
|
2015-02-13 07:34:51 -06:00
|
|
|
}
|
|
|
|
} CheckPass;
|
2015-07-02 04:14:30 -05:00
|
|
|
|
2015-02-13 07:34:51 -06:00
|
|
|
PRIVATE_NAMESPACE_END
|