2013-01-05 04:13:26 -06:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-01-05 04:13:26 -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
|
|
|
*
|
2013-01-05 04:13:26 -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/sigtools.h"
|
|
|
|
#include "kernel/log.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
YOSYS_NAMESPACE_BEGIN
|
2013-10-18 17:50:13 -05:00
|
|
|
extern void proc_clean_case(RTLIL::CaseRule *cs, bool &did_something, int &count, int max_depth);
|
2014-09-27 09:17:53 -05:00
|
|
|
YOSYS_NAMESPACE_END
|
2013-10-18 17:50:13 -05:00
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
bool check_signal(RTLIL::Module *mod, RTLIL::SigSpec signal, RTLIL::SigSpec ref, bool &polarity)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2014-07-22 13:15:14 -05:00
|
|
|
if (signal.size() != 1)
|
2013-01-05 04:13:26 -06:00
|
|
|
return false;
|
|
|
|
if (signal == ref)
|
|
|
|
return true;
|
|
|
|
|
2014-07-27 03:41:42 -05:00
|
|
|
for (auto cell : mod->cells())
|
|
|
|
{
|
2014-07-31 09:38:54 -05:00
|
|
|
if (cell->type == "$reduce_or" && cell->getPort("\\Y") == signal)
|
|
|
|
return check_signal(mod, cell->getPort("\\A"), ref, polarity);
|
2014-07-27 03:41:42 -05:00
|
|
|
|
2014-07-31 09:38:54 -05:00
|
|
|
if (cell->type == "$reduce_bool" && cell->getPort("\\Y") == signal)
|
|
|
|
return check_signal(mod, cell->getPort("\\A"), ref, polarity);
|
2014-07-27 03:41:42 -05:00
|
|
|
|
2014-07-31 09:38:54 -05:00
|
|
|
if (cell->type == "$logic_not" && cell->getPort("\\Y") == signal) {
|
2013-01-05 04:13:26 -06:00
|
|
|
polarity = !polarity;
|
2014-07-31 09:38:54 -05:00
|
|
|
return check_signal(mod, cell->getPort("\\A"), ref, polarity);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2014-07-27 03:41:42 -05:00
|
|
|
|
2014-07-31 09:38:54 -05:00
|
|
|
if (cell->type == "$not" && cell->getPort("\\Y") == signal) {
|
2013-01-05 04:13:26 -06:00
|
|
|
polarity = !polarity;
|
2014-07-31 09:38:54 -05:00
|
|
|
return check_signal(mod, cell->getPort("\\A"), ref, polarity);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2014-07-27 03:41:42 -05:00
|
|
|
|
2019-08-06 18:18:18 -05:00
|
|
|
if (cell->type.in("$eq", "$eqx") && cell->getPort("\\Y") == signal) {
|
2014-07-31 09:38:54 -05:00
|
|
|
if (cell->getPort("\\A").is_fully_const()) {
|
|
|
|
if (!cell->getPort("\\A").as_bool())
|
2013-01-05 04:13:26 -06:00
|
|
|
polarity = !polarity;
|
2014-07-31 09:38:54 -05:00
|
|
|
return check_signal(mod, cell->getPort("\\B"), ref, polarity);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2014-07-31 09:38:54 -05:00
|
|
|
if (cell->getPort("\\B").is_fully_const()) {
|
|
|
|
if (!cell->getPort("\\B").as_bool())
|
2013-01-05 04:13:26 -06:00
|
|
|
polarity = !polarity;
|
2014-07-31 09:38:54 -05:00
|
|
|
return check_signal(mod, cell->getPort("\\A"), ref, polarity);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
}
|
2014-07-27 03:41:42 -05:00
|
|
|
|
2019-08-06 18:18:18 -05:00
|
|
|
if (cell->type.in("$ne", "$nex") && cell->getPort("\\Y") == signal) {
|
2014-07-31 09:38:54 -05:00
|
|
|
if (cell->getPort("\\A").is_fully_const()) {
|
|
|
|
if (cell->getPort("\\A").as_bool())
|
2013-01-05 04:13:26 -06:00
|
|
|
polarity = !polarity;
|
2014-07-31 09:38:54 -05:00
|
|
|
return check_signal(mod, cell->getPort("\\B"), ref, polarity);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2014-07-31 09:38:54 -05:00
|
|
|
if (cell->getPort("\\B").is_fully_const()) {
|
|
|
|
if (cell->getPort("\\B").as_bool())
|
2013-01-05 04:13:26 -06:00
|
|
|
polarity = !polarity;
|
2014-07-31 09:38:54 -05:00
|
|
|
return check_signal(mod, cell->getPort("\\A"), ref, polarity);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
void apply_const(RTLIL::Module *mod, const RTLIL::SigSpec rspec, RTLIL::SigSpec &rval, RTLIL::CaseRule *cs, RTLIL::SigSpec const_sig, bool polarity, bool unknown)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
for (auto &action : cs->actions) {
|
|
|
|
if (unknown)
|
2014-07-22 13:15:14 -05:00
|
|
|
rspec.replace(action.first, RTLIL::SigSpec(RTLIL::State::Sm, action.second.size()), &rval);
|
2013-01-05 04:13:26 -06:00
|
|
|
else
|
|
|
|
rspec.replace(action.first, action.second, &rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto sw : cs->switches) {
|
2014-07-22 13:15:14 -05:00
|
|
|
if (sw->signal.size() == 0) {
|
2013-01-05 04:13:26 -06:00
|
|
|
for (auto cs2 : sw->cases)
|
|
|
|
apply_const(mod, rspec, rval, cs2, const_sig, polarity, unknown);
|
|
|
|
}
|
|
|
|
bool this_polarity = polarity;
|
|
|
|
if (check_signal(mod, sw->signal, const_sig, this_polarity)) {
|
|
|
|
for (auto cs2 : sw->cases) {
|
|
|
|
for (auto comp : cs2->compare)
|
|
|
|
if (comp == RTLIL::SigSpec(this_polarity, 1))
|
|
|
|
goto matched_case;
|
|
|
|
if (cs2->compare.size() == 0) {
|
|
|
|
matched_case:
|
|
|
|
apply_const(mod, rspec, rval, cs2, const_sig, polarity, false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (auto cs2 : sw->cases)
|
|
|
|
apply_const(mod, rspec, rval, cs2, const_sig, polarity, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
void eliminate_const(RTLIL::Module *mod, RTLIL::CaseRule *cs, RTLIL::SigSpec const_sig, bool polarity)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
for (auto sw : cs->switches) {
|
|
|
|
bool this_polarity = polarity;
|
|
|
|
if (check_signal(mod, sw->signal, const_sig, this_polarity)) {
|
|
|
|
bool found_rem_path = false;
|
|
|
|
for (size_t i = 0; i < sw->cases.size(); i++) {
|
|
|
|
RTLIL::CaseRule *cs2 = sw->cases[i];
|
|
|
|
for (auto comp : cs2->compare)
|
|
|
|
if (comp == RTLIL::SigSpec(this_polarity, 1))
|
|
|
|
goto matched_case;
|
|
|
|
if (found_rem_path) {
|
|
|
|
matched_case:
|
|
|
|
sw->cases.erase(sw->cases.begin() + (i--));
|
|
|
|
delete cs2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
found_rem_path = true;
|
|
|
|
cs2->compare.clear();
|
|
|
|
}
|
|
|
|
sw->signal = RTLIL::SigSpec();
|
|
|
|
} else {
|
|
|
|
for (auto cs2 : sw->cases)
|
|
|
|
eliminate_const(mod, cs2, const_sig, polarity);
|
|
|
|
}
|
|
|
|
}
|
2013-10-18 17:50:13 -05:00
|
|
|
|
|
|
|
int dummy_count = 0;
|
|
|
|
bool did_something = true;
|
|
|
|
while (did_something) {
|
|
|
|
did_something = false;
|
|
|
|
proc_clean_case(cs, did_something, dummy_count, 1);
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
void proc_arst(RTLIL::Module *mod, RTLIL::Process *proc, SigMap &assign_map)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2013-10-18 17:50:13 -05:00
|
|
|
restart_proc_arst:
|
2013-01-05 04:13:26 -06:00
|
|
|
if (proc->root_case.switches.size() != 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
RTLIL::SigSpec root_sig = proc->root_case.switches[0]->signal;
|
|
|
|
|
|
|
|
for (auto &sync : proc->syncs) {
|
|
|
|
if (sync->type == RTLIL::SyncType::STp || sync->type == RTLIL::SyncType::STn) {
|
|
|
|
bool polarity = sync->type == RTLIL::SyncType::STp;
|
|
|
|
if (check_signal(mod, root_sig, sync->signal, polarity)) {
|
2014-02-21 16:34:45 -06:00
|
|
|
if (proc->syncs.size() == 1) {
|
|
|
|
log("Found VHDL-style edge-trigger %s in `%s.%s'.\n", log_signal(sync->signal), mod->name.c_str(), proc->name.c_str());
|
|
|
|
} else {
|
|
|
|
log("Found async reset %s in `%s.%s'.\n", log_signal(sync->signal), mod->name.c_str(), proc->name.c_str());
|
|
|
|
sync->type = sync->type == RTLIL::SyncType::STp ? RTLIL::SyncType::ST1 : RTLIL::SyncType::ST0;
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
for (auto &action : sync->actions) {
|
2019-07-09 03:14:52 -05:00
|
|
|
RTLIL::SigSpec rspec = assign_map(action.second);
|
2014-07-22 13:15:14 -05:00
|
|
|
RTLIL::SigSpec rval = RTLIL::SigSpec(RTLIL::State::Sm, rspec.size());
|
2014-10-10 09:59:44 -05:00
|
|
|
for (int i = 0; i < GetSize(rspec); i++)
|
2014-07-23 08:36:09 -05:00
|
|
|
if (rspec[i].wire == NULL)
|
|
|
|
rval[i] = rspec[i];
|
2013-01-05 04:13:26 -06:00
|
|
|
RTLIL::SigSpec last_rval;
|
|
|
|
for (int count = 0; rval != last_rval; count++) {
|
|
|
|
last_rval = rval;
|
|
|
|
apply_const(mod, rspec, rval, &proc->root_case, root_sig, polarity, false);
|
|
|
|
assign_map.apply(rval);
|
|
|
|
if (rval.is_fully_const())
|
|
|
|
break;
|
|
|
|
if (count > 100)
|
|
|
|
log_error("Async reset %s yields endless loop at value %s for signal %s.\n",
|
|
|
|
log_signal(sync->signal), log_signal(rval), log_signal(action.first));
|
|
|
|
rspec = rval;
|
|
|
|
}
|
|
|
|
if (rval.has_marked_bits())
|
|
|
|
log_error("Async reset %s yields non-constant value %s for signal %s.\n",
|
|
|
|
log_signal(sync->signal), log_signal(rval), log_signal(action.first));
|
|
|
|
action.second = rval;
|
|
|
|
}
|
|
|
|
eliminate_const(mod, &proc->root_case, root_sig, polarity);
|
2013-10-18 17:50:13 -05:00
|
|
|
goto restart_proc_arst;
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ProcArstPass : public Pass {
|
2013-03-01 02:26:29 -06:00
|
|
|
ProcArstPass() : Pass("proc_arst", "detect asynchronous resets") { }
|
2018-07-21 01:41:18 -05:00
|
|
|
void help() YS_OVERRIDE
|
2013-03-01 02:26:29 -06:00
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
2013-11-20 14:00:43 -06:00
|
|
|
log(" proc_arst [-global_arst [!]<netname>] [selection]\n");
|
2013-03-01 02:26:29 -06:00
|
|
|
log("\n");
|
|
|
|
log("This pass identifies asynchronous resets in the processes and converts them\n");
|
|
|
|
log("to a different internal representation that is suitable for generating\n");
|
|
|
|
log("flip-flop cells with asynchronous resets.\n");
|
|
|
|
log("\n");
|
2013-11-20 14:00:43 -06:00
|
|
|
log(" -global_arst [!]<netname>\n");
|
|
|
|
log(" In modules that have a net with the given name, use this net as async\n");
|
|
|
|
log(" reset for registers that have been assign initial values in their\n");
|
|
|
|
log(" declaration ('reg foobar = constant_value;'). Use the '!' modifier for\n");
|
|
|
|
log(" active low reset signals. Note: the frontend stores the default value\n");
|
|
|
|
log(" in the 'init' attribute on the net.\n");
|
|
|
|
log("\n");
|
2013-03-01 02:26:29 -06:00
|
|
|
}
|
2018-07-21 01:41:18 -05:00
|
|
|
void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2013-11-20 14:00:43 -06:00
|
|
|
std::string global_arst;
|
|
|
|
bool global_arst_neg = false;
|
|
|
|
|
2016-04-21 16:28:37 -05:00
|
|
|
log_header(design, "Executing PROC_ARST pass (detect async resets in processes).\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2013-11-20 14:00:43 -06:00
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++)
|
|
|
|
{
|
|
|
|
if (args[argidx] == "-global_arst" && argidx+1 < args.size()) {
|
|
|
|
global_arst = args[++argidx];
|
|
|
|
if (!global_arst.empty() && global_arst[0] == '!') {
|
|
|
|
global_arst_neg = true;
|
|
|
|
global_arst = global_arst.substr(1);
|
|
|
|
}
|
|
|
|
global_arst = RTLIL::escape_id(global_arst);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
extra_args(args, argidx, design);
|
2015-04-09 08:12:26 -05:00
|
|
|
pool<Wire*> delete_initattr_wires;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-07-27 03:41:42 -05:00
|
|
|
for (auto mod : design->modules())
|
|
|
|
if (design->selected(mod)) {
|
|
|
|
SigMap assign_map(mod);
|
|
|
|
for (auto &proc_it : mod->processes) {
|
|
|
|
if (!design->selected(mod, proc_it.second))
|
2013-11-20 14:00:43 -06:00
|
|
|
continue;
|
2014-07-27 03:41:42 -05:00
|
|
|
proc_arst(mod, proc_it.second, assign_map);
|
|
|
|
if (global_arst.empty() || mod->wire(global_arst) == nullptr)
|
2013-11-20 14:00:43 -06:00
|
|
|
continue;
|
|
|
|
std::vector<RTLIL::SigSig> arst_actions;
|
|
|
|
for (auto sync : proc_it.second->syncs)
|
|
|
|
if (sync->type == RTLIL::SyncType::STp || sync->type == RTLIL::SyncType::STn)
|
|
|
|
for (auto &act : sync->actions) {
|
|
|
|
RTLIL::SigSpec arst_sig, arst_val;
|
2014-07-22 13:15:14 -05:00
|
|
|
for (auto &chunk : act.first.chunks())
|
2013-11-20 14:00:43 -06:00
|
|
|
if (chunk.wire && chunk.wire->attributes.count("\\init")) {
|
|
|
|
RTLIL::SigSpec value = chunk.wire->attributes.at("\\init");
|
2015-01-01 04:41:52 -06:00
|
|
|
value.extend_u0(chunk.wire->width, false);
|
2013-11-20 14:00:43 -06:00
|
|
|
arst_sig.append(chunk);
|
|
|
|
arst_val.append(value.extract(chunk.offset, chunk.width));
|
2015-04-09 08:12:26 -05:00
|
|
|
delete_initattr_wires.insert(chunk.wire);
|
2013-11-20 14:00:43 -06:00
|
|
|
}
|
2014-07-22 13:15:14 -05:00
|
|
|
if (arst_sig.size()) {
|
2013-11-20 14:00:43 -06:00
|
|
|
log("Added global reset to process %s: %s <- %s\n",
|
|
|
|
proc_it.first.c_str(), log_signal(arst_sig), log_signal(arst_val));
|
|
|
|
arst_actions.push_back(RTLIL::SigSig(arst_sig, arst_val));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!arst_actions.empty()) {
|
|
|
|
RTLIL::SyncRule *sync = new RTLIL::SyncRule;
|
|
|
|
sync->type = global_arst_neg ? RTLIL::SyncType::ST0 : RTLIL::SyncType::ST1;
|
2014-07-27 03:41:42 -05:00
|
|
|
sync->signal = mod->wire(global_arst);
|
2013-11-20 14:00:43 -06:00
|
|
|
sync->actions = arst_actions;
|
|
|
|
proc_it.second->syncs.push_back(sync);
|
|
|
|
}
|
|
|
|
}
|
2013-03-01 02:26:29 -06:00
|
|
|
}
|
2015-04-09 08:12:26 -05:00
|
|
|
|
|
|
|
for (auto wire : delete_initattr_wires)
|
|
|
|
wire->attributes.erase("\\init");
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
} ProcArstPass;
|
2015-07-02 04:14:30 -05:00
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
PRIVATE_NAMESPACE_END
|