2013-01-05 04:13:26 -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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONSTEVAL_H
|
|
|
|
#define CONSTEVAL_H
|
|
|
|
|
|
|
|
#include "kernel/rtlil.h"
|
|
|
|
#include "kernel/sigtools.h"
|
|
|
|
#include "kernel/celltypes.h"
|
2014-09-06 12:44:28 -05:00
|
|
|
#include "kernel/macc.h"
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
YOSYS_NAMESPACE_BEGIN
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
struct ConstEval
|
|
|
|
{
|
|
|
|
RTLIL::Module *module;
|
|
|
|
SigMap assign_map;
|
|
|
|
SigMap values_map;
|
|
|
|
SigPool stop_signals;
|
|
|
|
SigSet<RTLIL::Cell*> sig2driver;
|
|
|
|
std::set<RTLIL::Cell*> busy;
|
|
|
|
std::vector<SigMap> stack;
|
|
|
|
|
|
|
|
ConstEval(RTLIL::Module *module) : module(module), assign_map(module)
|
|
|
|
{
|
|
|
|
CellTypes ct;
|
|
|
|
ct.setup_internals();
|
|
|
|
ct.setup_stdcells();
|
|
|
|
|
2014-07-26 18:51:45 -05:00
|
|
|
for (auto &it : module->cells_) {
|
2013-01-05 04:13:26 -06:00
|
|
|
if (!ct.cell_known(it.second->type))
|
|
|
|
continue;
|
2014-07-26 07:32:50 -05:00
|
|
|
for (auto &it2 : it.second->connections())
|
2013-01-05 04:13:26 -06:00
|
|
|
if (ct.cell_output(it.second->type, it2.first))
|
|
|
|
sig2driver.insert(assign_map(it2.second), it.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
values_map.clear();
|
|
|
|
stop_signals.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void push()
|
|
|
|
{
|
|
|
|
stack.push_back(values_map);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pop()
|
|
|
|
{
|
|
|
|
values_map.swap(stack.back());
|
|
|
|
stack.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(RTLIL::SigSpec sig, RTLIL::Const value)
|
|
|
|
{
|
|
|
|
assign_map.apply(sig);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
RTLIL::SigSpec current_val = values_map(sig);
|
2014-10-10 09:59:44 -05:00
|
|
|
for (int i = 0; i < GetSize(current_val); i++)
|
2014-07-28 04:08:55 -05:00
|
|
|
log_assert(current_val[i].wire != NULL || current_val[i] == value.bits[i]);
|
2013-01-05 04:13:26 -06:00
|
|
|
#endif
|
|
|
|
values_map.add(sig, RTLIL::SigSpec(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void stop(RTLIL::SigSpec sig)
|
|
|
|
{
|
|
|
|
assign_map.apply(sig);
|
|
|
|
stop_signals.add(sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool eval(RTLIL::Cell *cell, RTLIL::SigSpec &undef)
|
|
|
|
{
|
2014-09-08 06:28:23 -05:00
|
|
|
if (cell->type == "$lcu")
|
|
|
|
{
|
|
|
|
RTLIL::SigSpec sig_p = cell->getPort("\\P");
|
|
|
|
RTLIL::SigSpec sig_g = cell->getPort("\\G");
|
|
|
|
RTLIL::SigSpec sig_ci = cell->getPort("\\CI");
|
|
|
|
RTLIL::SigSpec sig_co = values_map(assign_map(cell->getPort("\\CO")));
|
|
|
|
|
|
|
|
if (sig_co.is_fully_const())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!eval(sig_p, undef, cell))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!eval(sig_g, undef, cell))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!eval(sig_ci, undef, cell))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (sig_p.is_fully_def() && sig_g.is_fully_def() && sig_ci.is_fully_def())
|
|
|
|
{
|
2014-10-10 09:59:44 -05:00
|
|
|
RTLIL::Const coval(RTLIL::Sx, GetSize(sig_co));
|
2014-09-08 06:28:23 -05:00
|
|
|
bool carry = sig_ci.as_bool();
|
|
|
|
|
2014-10-10 09:59:44 -05:00
|
|
|
for (int i = 0; i < GetSize(coval); i++) {
|
2014-09-08 06:28:23 -05:00
|
|
|
carry = (sig_g[i] == RTLIL::S1) || (sig_p[i] == RTLIL::S1 && carry);
|
|
|
|
coval.bits[i] = carry ? RTLIL::S1 : RTLIL::S0;
|
|
|
|
}
|
|
|
|
|
|
|
|
set(sig_co, coval);
|
|
|
|
}
|
|
|
|
else
|
2014-10-10 09:59:44 -05:00
|
|
|
set(sig_co, RTLIL::Const(RTLIL::Sx, GetSize(sig_co)));
|
2014-09-08 06:28:23 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
RTLIL::SigSpec sig_a, sig_b, sig_s, sig_y;
|
|
|
|
|
2014-07-31 09:38:54 -05:00
|
|
|
log_assert(cell->hasPort("\\Y"));
|
|
|
|
sig_y = values_map(assign_map(cell->getPort("\\Y")));
|
2013-01-05 04:13:26 -06:00
|
|
|
if (sig_y.is_fully_const())
|
|
|
|
return true;
|
|
|
|
|
2014-07-31 09:38:54 -05:00
|
|
|
if (cell->hasPort("\\S")) {
|
|
|
|
sig_s = cell->getPort("\\S");
|
2013-01-05 04:13:26 -06:00
|
|
|
if (!eval(sig_s, undef, cell))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-31 09:38:54 -05:00
|
|
|
if (cell->hasPort("\\A"))
|
|
|
|
sig_a = cell->getPort("\\A");
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-07-31 09:38:54 -05:00
|
|
|
if (cell->hasPort("\\B"))
|
|
|
|
sig_b = cell->getPort("\\B");
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-08-14 04:39:46 -05:00
|
|
|
if (cell->type == "$mux" || cell->type == "$pmux" || cell->type == "$_MUX_")
|
2013-11-06 07:12:44 -06:00
|
|
|
{
|
|
|
|
std::vector<RTLIL::SigSpec> y_candidates;
|
2014-01-03 10:30:50 -06:00
|
|
|
int count_maybe_set_s_bits = 0;
|
2013-11-06 07:12:44 -06:00
|
|
|
int count_set_s_bits = 0;
|
|
|
|
|
2014-07-22 13:15:14 -05:00
|
|
|
for (int i = 0; i < sig_s.size(); i++)
|
2013-11-06 07:12:44 -06:00
|
|
|
{
|
|
|
|
RTLIL::State s_bit = sig_s.extract(i, 1).as_const().bits.at(0);
|
2014-07-22 13:15:14 -05:00
|
|
|
RTLIL::SigSpec b_slice = sig_b.extract(sig_y.size()*i, sig_y.size());
|
2013-11-06 07:12:44 -06:00
|
|
|
|
|
|
|
if (s_bit == RTLIL::State::Sx || s_bit == RTLIL::State::S1)
|
|
|
|
y_candidates.push_back(b_slice);
|
|
|
|
|
2014-01-03 10:30:50 -06:00
|
|
|
if (s_bit == RTLIL::State::S1 || s_bit == RTLIL::State::Sx)
|
|
|
|
count_maybe_set_s_bits++;
|
|
|
|
|
2013-11-06 07:12:44 -06:00
|
|
|
if (s_bit == RTLIL::State::S1)
|
|
|
|
count_set_s_bits++;
|
|
|
|
}
|
|
|
|
|
2014-08-14 04:39:46 -05:00
|
|
|
if (count_set_s_bits == 0)
|
2013-11-06 07:12:44 -06:00
|
|
|
y_candidates.push_back(sig_a);
|
|
|
|
|
|
|
|
std::vector<RTLIL::Const> y_values;
|
|
|
|
|
2014-07-28 04:08:55 -05:00
|
|
|
log_assert(y_candidates.size() > 0);
|
2013-11-06 07:12:44 -06:00
|
|
|
for (auto &yc : y_candidates) {
|
|
|
|
if (!eval(yc, undef, cell))
|
|
|
|
return false;
|
|
|
|
y_values.push_back(yc.as_const());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y_values.size() > 1)
|
|
|
|
{
|
|
|
|
std::vector<RTLIL::State> master_bits = y_values.at(0).bits;
|
|
|
|
|
|
|
|
for (size_t i = 1; i < y_values.size(); i++) {
|
|
|
|
std::vector<RTLIL::State> &slave_bits = y_values.at(i).bits;
|
2014-07-28 04:08:55 -05:00
|
|
|
log_assert(master_bits.size() == slave_bits.size());
|
2013-11-06 07:12:44 -06:00
|
|
|
for (size_t j = 0; j < master_bits.size(); j++)
|
|
|
|
if (master_bits[j] != slave_bits[j])
|
|
|
|
master_bits[j] = RTLIL::State::Sx;
|
|
|
|
}
|
|
|
|
|
|
|
|
set(sig_y, RTLIL::Const(master_bits));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
set(sig_y, y_values.front());
|
|
|
|
}
|
2014-09-08 05:15:39 -05:00
|
|
|
else if (cell->type == "$fa")
|
|
|
|
{
|
|
|
|
RTLIL::SigSpec sig_c = cell->getPort("\\C");
|
|
|
|
RTLIL::SigSpec sig_x = cell->getPort("\\X");
|
2014-10-10 09:59:44 -05:00
|
|
|
int width = GetSize(sig_c);
|
2014-09-08 05:15:39 -05:00
|
|
|
|
|
|
|
if (!eval(sig_a, undef, cell))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!eval(sig_b, undef, cell))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!eval(sig_c, undef, cell))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
RTLIL::Const t1 = const_xor(sig_a.as_const(), sig_b.as_const(), false, false, width);
|
|
|
|
RTLIL::Const val_y = const_xor(t1, sig_c.as_const(), false, false, width);
|
|
|
|
|
|
|
|
RTLIL::Const t2 = const_and(sig_a.as_const(), sig_b.as_const(), false, false, width);
|
|
|
|
RTLIL::Const t3 = const_and(sig_c.as_const(), t1, false, false, width);
|
|
|
|
RTLIL::Const val_x = const_or(t2, t3, false, false, width);
|
|
|
|
|
2014-10-10 09:59:44 -05:00
|
|
|
for (int i = 0; i < GetSize(val_y); i++)
|
2014-09-08 09:59:39 -05:00
|
|
|
if (val_y.bits[i] == RTLIL::Sx)
|
|
|
|
val_x.bits[i] = RTLIL::Sx;
|
|
|
|
|
2014-09-08 05:15:39 -05:00
|
|
|
set(sig_y, val_y);
|
|
|
|
set(sig_x, val_x);
|
|
|
|
}
|
2014-09-01 09:35:46 -05:00
|
|
|
else if (cell->type == "$alu")
|
|
|
|
{
|
|
|
|
bool signed_a = cell->parameters.count("\\A_SIGNED") > 0 && cell->parameters["\\A_SIGNED"].as_bool();
|
|
|
|
bool signed_b = cell->parameters.count("\\B_SIGNED") > 0 && cell->parameters["\\B_SIGNED"].as_bool();
|
|
|
|
|
|
|
|
RTLIL::SigSpec sig_ci = cell->getPort("\\CI");
|
|
|
|
RTLIL::SigSpec sig_bi = cell->getPort("\\BI");
|
|
|
|
|
|
|
|
if (!eval(sig_a, undef, cell))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!eval(sig_b, undef, cell))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!eval(sig_ci, undef, cell))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!eval(sig_bi, undef, cell))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
RTLIL::SigSpec sig_x = cell->getPort("\\X");
|
|
|
|
RTLIL::SigSpec sig_co = cell->getPort("\\CO");
|
|
|
|
|
|
|
|
bool any_input_undef = !(sig_a.is_fully_def() && sig_b.is_fully_def() && sig_ci.is_fully_def() && sig_bi.is_fully_def());
|
2014-10-10 09:59:44 -05:00
|
|
|
sig_a.extend_u0(GetSize(sig_y), signed_a);
|
|
|
|
sig_b.extend_u0(GetSize(sig_y), signed_b);
|
2014-09-01 09:35:46 -05:00
|
|
|
|
|
|
|
bool carry = sig_ci[0] == RTLIL::S1;
|
|
|
|
bool b_inv = sig_bi[0] == RTLIL::S1;
|
|
|
|
|
2014-10-10 09:59:44 -05:00
|
|
|
for (int i = 0; i < GetSize(sig_y); i++)
|
2014-09-01 09:35:46 -05:00
|
|
|
{
|
|
|
|
RTLIL::SigSpec x_inputs = { sig_a[i], sig_b[i], sig_bi[0] };
|
|
|
|
|
|
|
|
if (!x_inputs.is_fully_def()) {
|
|
|
|
set(sig_x[i], RTLIL::Sx);
|
|
|
|
} else {
|
|
|
|
bool bit_a = sig_a[i] == RTLIL::S1;
|
|
|
|
bool bit_b = (sig_b[i] == RTLIL::S1) != b_inv;
|
|
|
|
bool bit_x = bit_a != bit_b;
|
|
|
|
set(sig_x[i], bit_x ? RTLIL::S1 : RTLIL::S0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (any_input_undef) {
|
|
|
|
set(sig_y[i], RTLIL::Sx);
|
|
|
|
set(sig_co[i], RTLIL::Sx);
|
|
|
|
} else {
|
|
|
|
bool bit_a = sig_a[i] == RTLIL::S1;
|
|
|
|
bool bit_b = (sig_b[i] == RTLIL::S1) != b_inv;
|
|
|
|
bool bit_y = (bit_a != bit_b) != carry;
|
|
|
|
carry = (bit_a && bit_b) || (bit_a && carry) || (bit_b && carry);
|
|
|
|
set(sig_y[i], bit_y ? RTLIL::S1 : RTLIL::S0);
|
|
|
|
set(sig_co[i], carry ? RTLIL::S1 : RTLIL::S0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-06 12:44:28 -05:00
|
|
|
else if (cell->type == "$macc")
|
|
|
|
{
|
|
|
|
Macc macc;
|
|
|
|
macc.from_cell(cell);
|
|
|
|
|
|
|
|
if (!eval(macc.bit_ports, undef, cell))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (auto &port : macc.ports) {
|
|
|
|
if (!eval(port.in_a, undef, cell))
|
|
|
|
return false;
|
|
|
|
if (!eval(port.in_b, undef, cell))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-10 09:59:44 -05:00
|
|
|
RTLIL::Const result(0, GetSize(cell->getPort("\\Y")));
|
2014-09-06 12:44:28 -05:00
|
|
|
if (!macc.eval(result))
|
|
|
|
log_abort();
|
|
|
|
|
|
|
|
set(cell->getPort("\\Y"), result);
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
else
|
2013-11-06 07:12:44 -06:00
|
|
|
{
|
2014-08-16 11:18:30 -05:00
|
|
|
RTLIL::SigSpec sig_c, sig_d;
|
|
|
|
|
|
|
|
if (cell->type.in("$_AOI3_", "$_OAI3_", "$_AOI4_", "$_OAI4_")) {
|
|
|
|
if (cell->hasPort("\\C"))
|
|
|
|
sig_c = cell->getPort("\\C");
|
|
|
|
if (cell->hasPort("\\D"))
|
|
|
|
sig_d = cell->getPort("\\D");
|
|
|
|
}
|
|
|
|
|
2014-07-22 13:15:14 -05:00
|
|
|
if (sig_a.size() > 0 && !eval(sig_a, undef, cell))
|
2013-11-06 07:12:44 -06:00
|
|
|
return false;
|
2014-07-22 13:15:14 -05:00
|
|
|
if (sig_b.size() > 0 && !eval(sig_b, undef, cell))
|
2013-11-06 07:12:44 -06:00
|
|
|
return false;
|
2014-08-16 11:18:30 -05:00
|
|
|
if (sig_c.size() > 0 && !eval(sig_c, undef, cell))
|
|
|
|
return false;
|
|
|
|
if (sig_d.size() > 0 && !eval(sig_d, undef, cell))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
set(sig_y, CellTypes::eval(cell, sig_a.as_const(), sig_b.as_const(),
|
|
|
|
sig_c.as_const(), sig_d.as_const()));
|
2013-11-06 07:12:44 -06:00
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool eval(RTLIL::SigSpec &sig, RTLIL::SigSpec &undef, RTLIL::Cell *busy_cell = NULL)
|
|
|
|
{
|
|
|
|
assign_map.apply(sig);
|
|
|
|
values_map.apply(sig);
|
|
|
|
|
|
|
|
if (sig.is_fully_const())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (stop_signals.check_any(sig)) {
|
|
|
|
undef = stop_signals.extract(sig);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (busy_cell) {
|
|
|
|
if (busy.count(busy_cell) > 0) {
|
|
|
|
undef = sig;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
busy.insert(busy_cell);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<RTLIL::Cell*> driver_cells;
|
|
|
|
sig2driver.find(sig, driver_cells);
|
|
|
|
for (auto cell : driver_cells) {
|
|
|
|
if (!eval(cell, undef)) {
|
|
|
|
if (busy_cell)
|
|
|
|
busy.erase(busy_cell);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (busy_cell)
|
|
|
|
busy.erase(busy_cell);
|
|
|
|
|
|
|
|
values_map.apply(sig);
|
|
|
|
if (sig.is_fully_const())
|
|
|
|
return true;
|
|
|
|
|
2014-07-24 15:47:57 -05:00
|
|
|
for (auto &c : sig.chunks())
|
|
|
|
if (c.wire != NULL)
|
|
|
|
undef.append(c);
|
2013-01-05 04:13:26 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool eval(RTLIL::SigSpec &sig)
|
|
|
|
{
|
|
|
|
RTLIL::SigSpec undef;
|
|
|
|
return eval(sig, undef);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
YOSYS_NAMESPACE_END
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
#endif
|