3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2026-07-15 11:45:41 +00:00
yosys/kernel/cellaigs.cc
Emil J. Tywoniak c3ffbf6fae WIP
2026-06-12 00:18:53 +02:00

529 lines
14 KiB
C++

/*
* yosys -- Yosys Open SYnthesis Suite
*
* Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
*
* 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.
*
*/
#include "kernel/cellaigs.h"
YOSYS_NAMESPACE_BEGIN
AigNode::AigNode()
{
portbit = -1;
inverter = false;
left_parent = -1;
right_parent = -1;
}
bool AigNode::operator==(const AigNode &other) const
{
if (portname != other.portname) return false;
if (portbit != other.portbit) return false;
if (inverter != other.inverter) return false;
if (left_parent != other.left_parent) return false;
if (right_parent != other.right_parent) return false;
return true;
}
Hasher AigNode::hash_into(Hasher h) const
{
h.eat(portname);
h.eat(portbit);
h.eat(inverter);
h.eat(left_parent);
h.eat(right_parent);
return h;
}
bool Aig::operator==(const Aig &other) const
{
return name == other.name;
}
Hasher Aig::hash_into(Hasher h) const
{
h.eat(name);
return h;
}
struct AigMaker
{
Aig *aig;
Cell *cell;
idict<AigNode> aig_indices;
AigMaker(Aig *aig, Cell *cell) : aig(aig), cell(cell) {}
int node2index(const AigNode &node)
{
if (node.left_parent > node.right_parent) {
AigNode n(node);
std::swap(n.left_parent, n.right_parent);
return node2index(n);
}
if (!aig_indices.count(node)) {
aig_indices.expect(node, GetSize(aig->nodes));
aig->nodes.push_back(node);
}
return aig_indices.at(node);
}
int bool_node(bool value)
{
AigNode node;
node.inverter = value;
return node2index(node);
}
int inport(TwineRef portname, int portbit = 0, bool inverter = false)
{
if (portbit >= GetSize(cell->getPort(portname))) {
if (cell->parameters.count(cell->module->design->twines.str(portname) + "_SIGNED") && cell->getParam(cell->module->design->twines.str(portname) + "_SIGNED").as_bool())
return inport(portname, GetSize(cell->getPort(portname))-1, inverter);
return bool_node(inverter);
}
AigNode node;
node.portname = portname;
node.portbit = portbit;
node.inverter = inverter;
return node2index(node);
}
vector<int> inport_vec(TwineRef portname, int width)
{
vector<int> vec;
for (int i = 0; i < width; i++)
vec.push_back(inport(portname, i));
return vec;
}
int not_inport(TwineRef portname, int portbit = 0)
{
return inport(portname, portbit, true);
}
int not_gate(int A)
{
AigNode node(aig_indices[A]);
node.outports.clear();
node.inverter = !node.inverter;
return node2index(node);
}
int and_gate(int A, int B, bool inverter = false)
{
if (A == B)
return inverter ? not_gate(A) : A;
const AigNode &nA = aig_indices[A];
const AigNode &nB = aig_indices[B];
AigNode nB_inv(nB);
nB_inv.inverter = !nB_inv.inverter;
if (nA == nB_inv)
return bool_node(inverter);
bool nA_bool = nA.portbit < 0 && nA.left_parent < 0 && nA.right_parent < 0;
bool nB_bool = nB.portbit < 0 && nB.left_parent < 0 && nB.right_parent < 0;
if (nA_bool && nB_bool) {
bool bA = nA.inverter;
bool bB = nB.inverter;
return bool_node(inverter != (bA && bB));
}
if (nA_bool) {
bool bA = nA.inverter;
if (inverter)
return bA ? not_gate(B) : bool_node(true);
return bA ? B : bool_node(false);
}
if (nB_bool) {
bool bB = nB.inverter;
if (inverter)
return bB ? not_gate(A) : bool_node(true);
return bB ? A : bool_node(false);
}
AigNode node;
node.inverter = inverter;
node.left_parent = A;
node.right_parent = B;
return node2index(node);
}
int nand_gate(int A, int B)
{
return and_gate(A, B, true);
}
int or_gate(int A, int B)
{
int not_a = not_gate(A);
int not_b = not_gate(B);
return nand_gate(not_a, not_b);
}
int nor_gate(int A, int B)
{
int not_a = not_gate(A);
int not_b = not_gate(B);
return and_gate(not_a, not_b);
}
int xor_gate(int A, int B)
{
int a_and_b = and_gate(A, B);
int a_nor_b = nor_gate(A, B);
return nor_gate(a_and_b, a_nor_b);
}
int xnor_gate(int A, int B)
{
int a_and_b = and_gate(A, B);
int a_nor_b = nor_gate(A, B);
return or_gate(a_and_b, a_nor_b);
}
int andnot_gate(int A, int B)
{
int not_b = not_gate(B);
return and_gate(A, not_b);
}
int ornot_gate(int A, int B)
{
int not_b = not_gate(B);
return or_gate(A, not_b);
}
int mux_gate(int A, int B, int S)
{
int not_s = not_gate(S);
int a_active = and_gate(A, not_s);
int b_active = and_gate(B, S);
return or_gate(a_active, b_active);
}
vector<int> adder(const vector<int> &A, const vector<int> &B, int carry_in, vector<int> *X = nullptr, vector<int> *CO = nullptr)
{
vector<int> Y(GetSize(A));
log_assert(GetSize(A) == GetSize(B));
for (int i = 0; i < GetSize(A); i++) {
int a_xor_b = xor_gate(A[i], B[i]);
int a_or_b = or_gate(A[i], B[i]);
int a_and_b = and_gate(A[i], B[i]);
Y[i] = xor_gate(a_xor_b, carry_in);
int tmp = and_gate(a_or_b, carry_in);
int carry_out = or_gate(a_and_b, tmp);
if (X != nullptr)
X->at(i) = a_xor_b;
if (CO != nullptr)
CO->at(i) = carry_out;
carry_in = carry_out;
}
return Y;
}
void outport(int node, TwineRef portname, int portbit = 0)
{
if (portbit < GetSize(cell->getPort(portname)))
aig->nodes.at(node).outports.push_back(pair<TwineRef, int>(portname, portbit));
}
void outport_bool(int node, TwineRef portname)
{
outport(node, portname);
for (int i = 1; i < GetSize(cell->getPort(portname)); i++)
outport(bool_node(false), portname, i);
}
void outport_vec(const vector<int> &vec, TwineRef portname)
{
for (int i = 0; i < GetSize(vec); i++)
outport(vec.at(i), portname, i);
}
};
Aig::Aig(Cell *cell)
{
if (cell->type[0] != '$')
return;
AigMaker mk(this, cell);
name = cell->type.str();
string mkname_last;
bool mkname_a_signed = false;
bool mkname_b_signed = false;
bool mkname_is_signed = false;
cell->parameters.sort();
for (auto p : cell->parameters)
{
if (p.first == ID::A_WIDTH && mkname_a_signed) {
name = mkname_last + stringf(":%d%c", p.second.as_int(), mkname_is_signed ? 'S' : 'U');
} else if (p.first == ID::B_WIDTH && mkname_b_signed) {
name = mkname_last + stringf(":%d%c", p.second.as_int(), mkname_is_signed ? 'S' : 'U');
} else {
mkname_last = name;
name += stringf(":%d", p.second.as_int());
}
mkname_a_signed = false;
mkname_b_signed = false;
mkname_is_signed = false;
if (p.first == ID::A_SIGNED) {
mkname_a_signed = true;
mkname_is_signed = p.second.as_bool();
}
if (p.first == ID::B_SIGNED) {
mkname_b_signed = true;
mkname_is_signed = p.second.as_bool();
}
}
if (cell->type.in(TW($not), TW($_NOT_), TW($pos), TW($buf), TW($_BUF_)))
{
for (int i = 0; i < GetSize(cell->getPort(TW::Y)); i++) {
int A = mk.inport(TW::A, i);
int Y = cell->type.in(TW($not), TW($_NOT_)) ? mk.not_gate(A) : A;
mk.outport(Y, TW::Y, i);
}
goto optimize;
}
if (cell->type.in(TW($and), TW($_AND_), TW($_NAND_), TW($or), TW($_OR_), TW($_NOR_), TW($xor), TW($xnor), TW($_XOR_), TW($_XNOR_), TW($_ANDNOT_), TW($_ORNOT_)))
{
for (int i = 0; i < GetSize(cell->getPort(TW::Y)); i++) {
int A = mk.inport(TW::A, i);
int B = mk.inport(TW::B, i);
int Y = cell->type.in(TW($and), TW($_AND_)) ? mk.and_gate(A, B) :
cell->type.in(TW($_NAND_)) ? mk.nand_gate(A, B) :
cell->type.in(TW($or), TW($_OR_)) ? mk.or_gate(A, B) :
cell->type.in(TW($_NOR_)) ? mk.nor_gate(A, B) :
cell->type.in(TW($xor), TW($_XOR_)) ? mk.xor_gate(A, B) :
cell->type.in(TW($xnor), TW($_XNOR_)) ? mk.xnor_gate(A, B) :
cell->type.in(TW($_ANDNOT_)) ? mk.andnot_gate(A, B) :
cell->type.in(TW($_ORNOT_)) ? mk.ornot_gate(A, B) : -1;
mk.outport(Y, TW::Y, i);
}
goto optimize;
}
if (cell->type.in(TW($mux), TW($_MUX_), TW($_NMUX_)))
{
int S = mk.inport(TW::S);
for (int i = 0; i < GetSize(cell->getPort(TW::Y)); i++) {
int A = mk.inport(TW::A, i);
int B = mk.inport(TW::B, i);
int Y = mk.mux_gate(A, B, S);
if (cell->type == TW($_NMUX_))
Y = mk.not_gate(Y);
mk.outport(Y, TW::Y, i);
}
goto optimize;
}
if (cell->type.in(TW($reduce_and), TW($reduce_or), TW($reduce_xor), TW($reduce_xnor), TW($reduce_bool)))
{
int Y = mk.inport(TW::A, 0);
for (int i = 1; i < GetSize(cell->getPort(TW::A)); i++) {
int A = mk.inport(TW::A, i);
if (cell->type == TW($reduce_and)) Y = mk.and_gate(A, Y);
if (cell->type == TW($reduce_or)) Y = mk.or_gate(A, Y);
if (cell->type == TW($reduce_bool)) Y = mk.or_gate(A, Y);
if (cell->type == TW($reduce_xor)) Y = mk.xor_gate(A, Y);
if (cell->type == TW($reduce_xnor)) Y = mk.xor_gate(A, Y);
}
if (cell->type == TW($reduce_xnor))
Y = mk.not_gate(Y);
mk.outport(Y, TW::Y, 0);
for (int i = 1; i < GetSize(cell->getPort(TW::Y)); i++)
mk.outport(mk.bool_node(false), TW::Y, i);
goto optimize;
}
if (cell->type.in(TW($logic_not), TW($logic_and), TW($logic_or)))
{
int A = mk.inport(TW::A, 0), Y = -1;
for (int i = 1; i < GetSize(cell->getPort(TW::A)); i++)
A = mk.or_gate(mk.inport(TW::A, i), A);
if (cell->type.in(TW($logic_and), TW($logic_or))) {
int B = mk.inport(TW::B, 0);
for (int i = 1; i < GetSize(cell->getPort(TW::B)); i++)
B = mk.or_gate(mk.inport(TW::B, i), B);
if (cell->type == TW($logic_and)) Y = mk.and_gate(A, B);
if (cell->type == TW($logic_or)) Y = mk.or_gate(A, B);
} else {
if (cell->type == TW($logic_not)) Y = mk.not_gate(A);
}
mk.outport_bool(Y, TW::Y);
goto optimize;
}
if (cell->type.in(TW($add), TW($sub)))
{
int width = GetSize(cell->getPort(TW::Y));
vector<int> A = mk.inport_vec(TW::A, width);
vector<int> B = mk.inport_vec(TW::B, width);
int carry = mk.bool_node(false);
if (cell->type == TW($sub)) {
for (auto &n : B)
n = mk.not_gate(n);
carry = mk.not_gate(carry);
}
vector<int> Y = mk.adder(A, B, carry);
mk.outport_vec(Y, TW::Y);
goto optimize;
}
if (cell->type.in(TW($lt), TW($gt), TW($le), TW($ge)))
{
int width = std::max(GetSize(cell->getPort(TW::A)),
GetSize(cell->getPort(TW::B))) + 1;
vector<int> A = mk.inport_vec(TW::A, width);
vector<int> B = mk.inport_vec(TW::B, width);
if (cell->type.in(TW($gt), TW($ge)))
std::swap(A, B);
int carry = mk.bool_node(!cell->type.in(TW($le), TW($ge)));
for (auto &n : B)
n = mk.not_gate(n);
vector<int> Y = mk.adder(A, B, carry);
mk.outport(Y.back(), TW::Y);
for (int i = 1; i < GetSize(cell->getPort(TW::Y)); i++)
mk.outport(mk.bool_node(false), TW::Y, i);
goto optimize;
}
if (cell->type == TW($alu))
{
int width = GetSize(cell->getPort(TW::Y));
vector<int> A = mk.inport_vec(TW::A, width);
vector<int> B = mk.inport_vec(TW::B, width);
int carry = mk.inport(TW::CI);
int binv = mk.inport(TW::BI);
for (auto &n : B)
n = mk.xor_gate(n, binv);
vector<int> X(width), CO(width);
vector<int> Y = mk.adder(A, B, carry, &X, &CO);
for (int i = 0; i < width; i++)
X[i] = mk.xor_gate(A[i], B[i]);
mk.outport_vec(Y, TW::Y);
mk.outport_vec(X, TW::X);
mk.outport_vec(CO, TW::CO);
goto optimize;
}
if (cell->type.in(TW($eq), TW($ne)))
{
int width = max(GetSize(cell->getPort(TW::A)), GetSize(cell->getPort(TW::B)));
vector<int> A = mk.inport_vec(TW::A, width);
vector<int> B = mk.inport_vec(TW::B, width);
int Y = mk.bool_node(false);
for (int i = 0; i < width; i++)
Y = mk.or_gate(Y, mk.xor_gate(A[i], B[i]));
if (cell->type == TW($eq))
Y = mk.not_gate(Y);
mk.outport_bool(Y, TW::Y);
goto optimize;
}
if (cell->type == TW($_AOI3_))
{
int A = mk.inport(TW::A);
int B = mk.inport(TW::B);
int C = mk.inport(TW::C);
int Y = mk.nor_gate(mk.and_gate(A, B), C);
mk.outport(Y, TW::Y);
goto optimize;
}
if (cell->type == TW($_OAI3_))
{
int A = mk.inport(TW::A);
int B = mk.inport(TW::B);
int C = mk.inport(TW::C);
int Y = mk.nand_gate(mk.or_gate(A, B), C);
mk.outport(Y, TW::Y);
goto optimize;
}
if (cell->type == TW($_AOI4_))
{
int A = mk.inport(TW::A);
int B = mk.inport(TW::B);
int C = mk.inport(TW::C);
int D = mk.inport(TW::D);
int a_and_b = mk.and_gate(A, B);
int Y = mk.nor_gate(a_and_b, mk.and_gate(C, D));
mk.outport(Y, TW::Y);
goto optimize;
}
if (cell->type == TW($_OAI4_))
{
int A = mk.inport(TW::A);
int B = mk.inport(TW::B);
int C = mk.inport(TW::C);
int D = mk.inport(TW::D);
int a_or_b = mk.or_gate(A, B);
int Y = mk.nand_gate(a_or_b, mk.or_gate(C, D));
mk.outport(Y, TW::Y);
goto optimize;
}
name.clear();
return;
optimize:;
pool<int> used_old_ids;
vector<AigNode> new_nodes;
dict<int, int> old_to_new_ids;
old_to_new_ids[-1] = -1;
for (int i = GetSize(nodes)-1; i >= 0; i--) {
if (!nodes[i].outports.empty())
used_old_ids.insert(i);
if (!used_old_ids.count(i))
continue;
if (nodes[i].left_parent >= 0)
used_old_ids.insert(nodes[i].left_parent);
if (nodes[i].right_parent >= 0)
used_old_ids.insert(nodes[i].right_parent);
}
for (int i = 0; i < GetSize(nodes); i++) {
if (!used_old_ids.count(i))
continue;
nodes[i].left_parent = old_to_new_ids.at(nodes[i].left_parent);
nodes[i].right_parent = old_to_new_ids.at(nodes[i].right_parent);
old_to_new_ids[i] = GetSize(new_nodes);
new_nodes.push_back(nodes[i]);
}
new_nodes.swap(nodes);
}
YOSYS_NAMESPACE_END