2018-07-21 01:41:18 -05:00
|
|
|
/* -*- c++ -*-
|
2013-06-07 06:59:13 -05:00
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-06-07 06:59:13 -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
|
|
|
*
|
2013-06-07 06:59:13 -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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SATGEN_H
|
|
|
|
#define SATGEN_H
|
|
|
|
|
|
|
|
#include "kernel/rtlil.h"
|
|
|
|
#include "kernel/sigtools.h"
|
|
|
|
#include "kernel/celltypes.h"
|
2014-09-06 12:44:11 -05:00
|
|
|
#include "kernel/macc.h"
|
2013-06-07 06:59:13 -05:00
|
|
|
|
2014-03-12 04:17:51 -05:00
|
|
|
#include "libs/ezsat/ezminisat.h"
|
2014-09-27 09:17:53 -05:00
|
|
|
|
|
|
|
YOSYS_NAMESPACE_BEGIN
|
|
|
|
|
2015-02-21 05:15:41 -06:00
|
|
|
// defined in kernel/register.cc
|
|
|
|
extern struct SatSolver *yosys_satsolver_list;
|
|
|
|
extern struct SatSolver *yosys_satsolver;
|
|
|
|
|
|
|
|
struct SatSolver
|
|
|
|
{
|
|
|
|
string name;
|
|
|
|
SatSolver *next;
|
|
|
|
virtual ezSAT *create() = 0;
|
|
|
|
|
|
|
|
SatSolver(string name) : name(name) {
|
|
|
|
next = yosys_satsolver_list;
|
|
|
|
yosys_satsolver_list = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~SatSolver() {
|
|
|
|
auto p = &yosys_satsolver_list;
|
|
|
|
while (*p) {
|
|
|
|
if (*p == this)
|
|
|
|
*p = next;
|
|
|
|
else
|
|
|
|
p = &(*p)->next;
|
|
|
|
}
|
|
|
|
if (yosys_satsolver == this)
|
|
|
|
yosys_satsolver = yosys_satsolver_list;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ezSatPtr : public std::unique_ptr<ezSAT> {
|
|
|
|
ezSatPtr() : unique_ptr<ezSAT>(yosys_satsolver->create()) { }
|
|
|
|
};
|
2013-06-07 06:59:13 -05:00
|
|
|
|
|
|
|
struct SatGen
|
|
|
|
{
|
|
|
|
ezSAT *ez;
|
|
|
|
SigMap *sigmap;
|
|
|
|
std::string prefix;
|
2013-06-09 11:07:05 -05:00
|
|
|
SigPool initial_state;
|
2014-02-04 05:46:16 -06:00
|
|
|
std::map<std::string, RTLIL::SigSpec> asserts_a, asserts_en;
|
2015-02-26 11:04:10 -06:00
|
|
|
std::map<std::string, RTLIL::SigSpec> assumes_a, assumes_en;
|
2014-10-03 11:51:50 -05:00
|
|
|
std::map<std::string, std::map<RTLIL::SigBit, int>> imported_signals;
|
2016-07-22 03:28:45 -05:00
|
|
|
std::map<std::pair<std::string, int>, bool> initstates;
|
2013-08-15 04:40:01 -05:00
|
|
|
bool ignore_div_by_zero;
|
2013-11-24 21:51:33 -06:00
|
|
|
bool model_undef;
|
2013-06-07 06:59:13 -05:00
|
|
|
|
2014-01-02 09:52:33 -06:00
|
|
|
SatGen(ezSAT *ez, SigMap *sigmap, std::string prefix = std::string()) :
|
|
|
|
ez(ez), sigmap(sigmap), prefix(prefix), ignore_div_by_zero(false), model_undef(false)
|
2013-06-07 06:59:13 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-01-02 09:52:33 -06:00
|
|
|
void setContext(SigMap *sigmap, std::string prefix = std::string())
|
2013-06-07 06:59:13 -05:00
|
|
|
{
|
|
|
|
this->sigmap = sigmap;
|
|
|
|
this->prefix = prefix;
|
|
|
|
}
|
|
|
|
|
2014-10-03 11:51:50 -05:00
|
|
|
std::vector<int> importSigSpecWorker(RTLIL::SigSpec sig, std::string &pf, bool undef_mode, bool dup_undef)
|
2013-06-07 06:59:13 -05:00
|
|
|
{
|
2013-11-25 08:12:01 -06:00
|
|
|
log_assert(!undef_mode || model_undef);
|
2013-11-09 04:38:17 -06:00
|
|
|
sigmap->apply(sig);
|
2013-06-07 06:59:13 -05:00
|
|
|
|
|
|
|
std::vector<int> vec;
|
2014-10-10 09:59:44 -05:00
|
|
|
vec.reserve(GetSize(sig));
|
2013-06-07 06:59:13 -05:00
|
|
|
|
2014-07-23 09:09:27 -05:00
|
|
|
for (auto &bit : sig)
|
|
|
|
if (bit.wire == NULL) {
|
2013-11-25 09:50:45 -06:00
|
|
|
if (model_undef && dup_undef && bit == RTLIL::State::Sx)
|
2014-03-06 06:08:44 -06:00
|
|
|
vec.push_back(ez->frozen_literal());
|
2013-11-25 09:50:45 -06:00
|
|
|
else
|
2014-10-10 10:06:02 -05:00
|
|
|
vec.push_back(bit == (undef_mode ? RTLIL::State::Sx : RTLIL::State::S1) ? ez->CONST_TRUE : ez->CONST_FALSE);
|
2013-06-09 06:35:46 -05:00
|
|
|
} else {
|
2014-10-10 10:22:08 -05:00
|
|
|
std::string name = pf + (bit.wire->width == 1 ? stringf("%s", log_id(bit.wire)) : stringf("%s [%d]", log_id(bit.wire->name), bit.offset));
|
2014-03-06 06:08:44 -06:00
|
|
|
vec.push_back(ez->frozen_literal(name));
|
2014-10-03 11:51:50 -05:00
|
|
|
imported_signals[pf][bit] = vec.back();
|
2013-06-09 06:35:46 -05:00
|
|
|
}
|
2013-06-07 06:59:13 -05:00
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
2013-11-24 21:51:33 -06:00
|
|
|
std::vector<int> importSigSpec(RTLIL::SigSpec sig, int timestep = -1)
|
|
|
|
{
|
2013-11-25 08:12:01 -06:00
|
|
|
log_assert(timestep != 0);
|
2013-11-24 21:51:33 -06:00
|
|
|
std::string pf = prefix + (timestep == -1 ? "" : stringf("@%d:", timestep));
|
2013-11-25 09:50:45 -06:00
|
|
|
return importSigSpecWorker(sig, pf, false, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<int> importDefSigSpec(RTLIL::SigSpec sig, int timestep = -1)
|
|
|
|
{
|
|
|
|
log_assert(timestep != 0);
|
|
|
|
std::string pf = prefix + (timestep == -1 ? "" : stringf("@%d:", timestep));
|
|
|
|
return importSigSpecWorker(sig, pf, false, true);
|
2013-11-24 21:51:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<int> importUndefSigSpec(RTLIL::SigSpec sig, int timestep = -1)
|
|
|
|
{
|
2013-11-25 08:12:01 -06:00
|
|
|
log_assert(timestep != 0);
|
2013-11-24 21:51:33 -06:00
|
|
|
std::string pf = "undef:" + prefix + (timestep == -1 ? "" : stringf("@%d:", timestep));
|
2013-11-25 09:50:45 -06:00
|
|
|
return importSigSpecWorker(sig, pf, true, false);
|
|
|
|
}
|
|
|
|
|
2014-10-03 11:51:50 -05:00
|
|
|
int importSigBit(RTLIL::SigBit bit, int timestep = -1)
|
|
|
|
{
|
|
|
|
log_assert(timestep != 0);
|
|
|
|
std::string pf = prefix + (timestep == -1 ? "" : stringf("@%d:", timestep));
|
|
|
|
return importSigSpecWorker(bit, pf, false, false).front();
|
|
|
|
}
|
|
|
|
|
2015-01-31 06:06:41 -06:00
|
|
|
int importDefSigBit(RTLIL::SigBit bit, int timestep = -1)
|
|
|
|
{
|
|
|
|
log_assert(timestep != 0);
|
|
|
|
std::string pf = prefix + (timestep == -1 ? "" : stringf("@%d:", timestep));
|
|
|
|
return importSigSpecWorker(bit, pf, false, true).front();
|
|
|
|
}
|
|
|
|
|
|
|
|
int importUndefSigBit(RTLIL::SigBit bit, int timestep = -1)
|
|
|
|
{
|
|
|
|
log_assert(timestep != 0);
|
|
|
|
std::string pf = "undef:" + prefix + (timestep == -1 ? "" : stringf("@%d:", timestep));
|
|
|
|
return importSigSpecWorker(bit, pf, true, false).front();
|
|
|
|
}
|
|
|
|
|
2014-10-03 11:51:50 -05:00
|
|
|
bool importedSigBit(RTLIL::SigBit bit, int timestep = -1)
|
|
|
|
{
|
|
|
|
log_assert(timestep != 0);
|
|
|
|
std::string pf = prefix + (timestep == -1 ? "" : stringf("@%d:", timestep));
|
2014-10-16 23:02:38 -05:00
|
|
|
return imported_signals[pf].count(bit) != 0;
|
2014-10-03 11:51:50 -05:00
|
|
|
}
|
|
|
|
|
2014-02-04 05:46:16 -06:00
|
|
|
void getAsserts(RTLIL::SigSpec &sig_a, RTLIL::SigSpec &sig_en, int timestep = -1)
|
|
|
|
{
|
|
|
|
std::string pf = prefix + (timestep == -1 ? "" : stringf("@%d:", timestep));
|
|
|
|
sig_a = asserts_a[pf];
|
|
|
|
sig_en = asserts_en[pf];
|
|
|
|
}
|
|
|
|
|
2015-02-26 11:04:10 -06:00
|
|
|
void getAssumes(RTLIL::SigSpec &sig_a, RTLIL::SigSpec &sig_en, int timestep = -1)
|
|
|
|
{
|
|
|
|
std::string pf = prefix + (timestep == -1 ? "" : stringf("@%d:", timestep));
|
|
|
|
sig_a = assumes_a[pf];
|
|
|
|
sig_en = assumes_en[pf];
|
|
|
|
}
|
|
|
|
|
2014-01-19 08:37:56 -06:00
|
|
|
int importAsserts(int timestep = -1)
|
|
|
|
{
|
|
|
|
std::vector<int> check_bits, enable_bits;
|
2014-02-04 05:46:16 -06:00
|
|
|
std::string pf = prefix + (timestep == -1 ? "" : stringf("@%d:", timestep));
|
2014-01-19 08:37:56 -06:00
|
|
|
if (model_undef) {
|
2014-02-04 05:46:16 -06:00
|
|
|
check_bits = ez->vec_and(ez->vec_not(importUndefSigSpec(asserts_a[pf], timestep)), importDefSigSpec(asserts_a[pf], timestep));
|
|
|
|
enable_bits = ez->vec_and(ez->vec_not(importUndefSigSpec(asserts_en[pf], timestep)), importDefSigSpec(asserts_en[pf], timestep));
|
2014-01-19 08:37:56 -06:00
|
|
|
} else {
|
2014-02-04 05:46:16 -06:00
|
|
|
check_bits = importDefSigSpec(asserts_a[pf], timestep);
|
|
|
|
enable_bits = importDefSigSpec(asserts_en[pf], timestep);
|
2014-01-19 08:37:56 -06:00
|
|
|
}
|
|
|
|
return ez->vec_reduce_and(ez->vec_or(check_bits, ez->vec_not(enable_bits)));
|
|
|
|
}
|
|
|
|
|
2015-02-26 11:04:10 -06:00
|
|
|
int importAssumes(int timestep = -1)
|
|
|
|
{
|
|
|
|
std::vector<int> check_bits, enable_bits;
|
|
|
|
std::string pf = prefix + (timestep == -1 ? "" : stringf("@%d:", timestep));
|
|
|
|
if (model_undef) {
|
|
|
|
check_bits = ez->vec_and(ez->vec_not(importUndefSigSpec(assumes_a[pf], timestep)), importDefSigSpec(assumes_a[pf], timestep));
|
|
|
|
enable_bits = ez->vec_and(ez->vec_not(importUndefSigSpec(assumes_en[pf], timestep)), importDefSigSpec(assumes_en[pf], timestep));
|
|
|
|
} else {
|
|
|
|
check_bits = importDefSigSpec(assumes_a[pf], timestep);
|
|
|
|
enable_bits = importDefSigSpec(assumes_en[pf], timestep);
|
|
|
|
}
|
|
|
|
return ez->vec_reduce_and(ez->vec_or(check_bits, ez->vec_not(enable_bits)));
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:28:51 -06:00
|
|
|
int signals_eq(RTLIL::SigSpec lhs, RTLIL::SigSpec rhs, int timestep_lhs = -1, int timestep_rhs = -1)
|
2013-11-25 09:50:45 -06:00
|
|
|
{
|
2013-12-07 10:28:51 -06:00
|
|
|
if (timestep_rhs < 0)
|
|
|
|
timestep_rhs = timestep_lhs;
|
|
|
|
|
2014-07-28 04:08:55 -05:00
|
|
|
log_assert(lhs.size() == rhs.size());
|
2013-11-25 09:50:45 -06:00
|
|
|
|
2013-12-07 10:28:51 -06:00
|
|
|
std::vector<int> vec_lhs = importSigSpec(lhs, timestep_lhs);
|
|
|
|
std::vector<int> vec_rhs = importSigSpec(rhs, timestep_rhs);
|
2013-11-25 09:50:45 -06:00
|
|
|
|
|
|
|
if (!model_undef)
|
|
|
|
return ez->vec_eq(vec_lhs, vec_rhs);
|
|
|
|
|
2013-12-07 10:28:51 -06:00
|
|
|
std::vector<int> undef_lhs = importUndefSigSpec(lhs, timestep_lhs);
|
|
|
|
std::vector<int> undef_rhs = importUndefSigSpec(rhs, timestep_rhs);
|
2013-11-25 09:50:45 -06:00
|
|
|
|
|
|
|
std::vector<int> eq_bits;
|
2014-07-22 13:15:14 -05:00
|
|
|
for (int i = 0; i < lhs.size(); i++)
|
2013-11-25 09:50:45 -06:00
|
|
|
eq_bits.push_back(ez->AND(ez->IFF(undef_lhs.at(i), undef_rhs.at(i)),
|
|
|
|
ez->IFF(ez->OR(vec_lhs.at(i), undef_lhs.at(i)), ez->OR(vec_rhs.at(i), undef_rhs.at(i)))));
|
|
|
|
return ez->expression(ezSAT::OpAnd, eq_bits);
|
2013-11-24 21:51:33 -06:00
|
|
|
}
|
|
|
|
|
2013-12-29 10:39:49 -06:00
|
|
|
void extendSignalWidth(std::vector<int> &vec_a, std::vector<int> &vec_b, RTLIL::Cell *cell, size_t y_width = 0, bool forced_signed = false)
|
2013-06-07 07:37:33 -05:00
|
|
|
{
|
2013-12-29 10:39:49 -06:00
|
|
|
bool is_signed = forced_signed;
|
2020-04-02 11:51:32 -05:00
|
|
|
if (!forced_signed && cell->parameters.count(ID::A_SIGNED) > 0 && cell->parameters.count(ID::B_SIGNED) > 0)
|
|
|
|
is_signed = cell->parameters[ID::A_SIGNED].as_bool() && cell->parameters[ID::B_SIGNED].as_bool();
|
2013-06-10 09:09:29 -05:00
|
|
|
while (vec_a.size() < vec_b.size() || vec_a.size() < y_width)
|
2014-10-10 10:06:02 -05:00
|
|
|
vec_a.push_back(is_signed && vec_a.size() > 0 ? vec_a.back() : ez->CONST_FALSE);
|
2013-06-10 09:09:29 -05:00
|
|
|
while (vec_b.size() < vec_a.size() || vec_b.size() < y_width)
|
2014-10-10 10:06:02 -05:00
|
|
|
vec_b.push_back(is_signed && vec_b.size() > 0 ? vec_b.back() : ez->CONST_FALSE);
|
2013-06-07 07:37:33 -05:00
|
|
|
}
|
|
|
|
|
2013-12-29 10:39:49 -06:00
|
|
|
void extendSignalWidth(std::vector<int> &vec_a, std::vector<int> &vec_b, std::vector<int> &vec_y, RTLIL::Cell *cell, bool forced_signed = false)
|
2013-06-07 07:37:33 -05:00
|
|
|
{
|
2013-12-29 10:39:49 -06:00
|
|
|
extendSignalWidth(vec_a, vec_b, cell, vec_y.size(), forced_signed);
|
2013-06-07 07:37:33 -05:00
|
|
|
while (vec_y.size() < vec_a.size())
|
|
|
|
vec_y.push_back(ez->literal());
|
|
|
|
}
|
|
|
|
|
2013-12-29 10:39:49 -06:00
|
|
|
void extendSignalWidthUnary(std::vector<int> &vec_a, std::vector<int> &vec_y, RTLIL::Cell *cell, bool forced_signed = false)
|
2013-06-12 12:09:14 -05:00
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
bool is_signed = forced_signed || (cell->parameters.count(ID::A_SIGNED) > 0 && cell->parameters[ID::A_SIGNED].as_bool());
|
2013-06-12 12:09:14 -05:00
|
|
|
while (vec_a.size() < vec_y.size())
|
2014-10-10 10:06:02 -05:00
|
|
|
vec_a.push_back(is_signed && vec_a.size() > 0 ? vec_a.back() : ez->CONST_FALSE);
|
2013-06-12 12:09:14 -05:00
|
|
|
while (vec_y.size() < vec_a.size())
|
|
|
|
vec_y.push_back(ez->literal());
|
|
|
|
}
|
|
|
|
|
2013-11-25 09:50:45 -06:00
|
|
|
void undefGating(std::vector<int> &vec_y, std::vector<int> &vec_yy, std::vector<int> &vec_undef)
|
|
|
|
{
|
2014-07-28 04:08:55 -05:00
|
|
|
log_assert(model_undef);
|
|
|
|
log_assert(vec_y.size() == vec_yy.size());
|
2014-07-21 05:03:41 -05:00
|
|
|
if (vec_y.size() > vec_undef.size()) {
|
|
|
|
std::vector<int> trunc_y(vec_y.begin(), vec_y.begin() + vec_undef.size());
|
|
|
|
std::vector<int> trunc_yy(vec_yy.begin(), vec_yy.begin() + vec_undef.size());
|
|
|
|
ez->assume(ez->expression(ezSAT::OpAnd, ez->vec_or(vec_undef, ez->vec_iff(trunc_y, trunc_yy))));
|
|
|
|
} else {
|
2014-07-28 04:08:55 -05:00
|
|
|
log_assert(vec_y.size() == vec_undef.size());
|
2014-07-21 05:03:41 -05:00
|
|
|
ez->assume(ez->expression(ezSAT::OpAnd, ez->vec_or(vec_undef, ez->vec_iff(vec_y, vec_yy))));
|
|
|
|
}
|
2013-11-25 09:50:45 -06:00
|
|
|
}
|
|
|
|
|
2020-07-20 15:49:30 -05:00
|
|
|
std::pair<std::vector<int>, std::vector<int>> mux(int s, int undef_s, const std::vector<int> &a, const std::vector<int> &undef_a, const std::vector<int> &b, const std::vector<int> &undef_b) {
|
|
|
|
std::vector<int> res;
|
|
|
|
std::vector<int> undef_res;
|
|
|
|
res = ez->vec_ite(s, b, a);
|
|
|
|
if (model_undef) {
|
|
|
|
std::vector<int> unequal_ab = ez->vec_not(ez->vec_iff(a, b));
|
|
|
|
std::vector<int> undef_ab = ez->vec_or(unequal_ab, ez->vec_or(undef_a, undef_b));
|
|
|
|
undef_res = ez->vec_ite(undef_s, undef_ab, ez->vec_ite(s, undef_b, undef_a));
|
|
|
|
}
|
|
|
|
return std::make_pair(res, undef_res);
|
|
|
|
}
|
|
|
|
|
2014-08-16 11:18:30 -05:00
|
|
|
void undefGating(int y, int yy, int undef)
|
|
|
|
{
|
|
|
|
ez->assume(ez->OR(undef, ez->IFF(y, yy)));
|
|
|
|
}
|
|
|
|
|
2016-07-22 03:28:45 -05:00
|
|
|
void setInitState(int timestep)
|
|
|
|
{
|
|
|
|
auto key = make_pair(prefix, timestep);
|
|
|
|
log_assert(initstates.count(key) == 0 || initstates.at(key) == true);
|
|
|
|
initstates[key] = true;
|
|
|
|
}
|
|
|
|
|
2020-07-18 17:17:02 -05:00
|
|
|
bool importCell(RTLIL::Cell *cell, int timestep = -1);
|
2013-06-07 06:59:13 -05:00
|
|
|
};
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
YOSYS_NAMESPACE_END
|
|
|
|
|
2013-06-07 06:59:13 -05:00
|
|
|
#endif
|