63std::optional<Gate>
gate(Cell* cell, SigMap& signals)
66 if (cell->type == ID($and) || cell->type == ID($or)) {
67 result.
mode = cell->type == ID($and) ? 0 : 1;
68 result.
width = cell->getParam(ID::Y_WIDTH).as_int();
70 if (cell->getParam(ID::A_WIDTH).as_int() != result.
width
71 || cell->getParam(ID::B_WIDTH).as_int() != result.
width)
73 result.
inputs = {signals(cell->getPort(ID::A)), signals(cell->getPort(ID::B))};
74 }
else if (cell->type ==
id(
"SILICON_LOGIC")) {
75 result.
mode = cell->getParam(
id(
"MODE")).as_int();
76 if (result.
mode != 0 && result.
mode != 1)
80 result.
inputs = {signals(cell->getPort(ID::A)), signals(cell->getPort(ID::B))};
81 }
else if (cell->type ==
id(
"SILICON_GATE")) {
82 result.
mode = cell->getParam(
id(
"MODE")).as_int();
83 result.
width = cell->getParam(ID::WIDTH).as_int();
84 result.
bitwise = cell->getParam(
id(
"BITWISE")).as_bool();
85 const int count = cell->getParam(
id(
"INPUTS")).as_int();
86 if (count < 2 || count > 65535 || result.
mode < 0 || result.
mode > 1)
88 for (
int i = 0; i < count; ++i)
90 signals(cell->getPort(RTLIL::escape_id(
"A" + std::to_string(i)))));
93 result.
output = signals(cell->getPort(ID::Y));
97 for (
const auto& input : result.
inputs)
98 if (input.empty() || (result.
bitwise && GetSize(input) != result.
width))
106 if (!module->processes.empty()) {
107 log_warning(
"SILICON: run proc before silicon_gates on module %s.\n", log_id(module));
111 dict<Cell*, Gate> shapes;
112 pool<Cell*> removed, hasParent;
113 std::vector<Cell*> roots;
116 for (
auto* cell : module->selected_cells()) {
117 if (cell->get_bool_attribute(ID::keep))
121 shapes.emplace(cell, std::move(*shape));
123 const auto compatible = [](
const Gate& parent,
const Gate& child) {
124 return parent.
mode == child.mode && parent.
width == child.width
125 && parent.
bitwise == child.bitwise;
127 for (
const auto& entry : shapes) {
128 const auto& parent = entry.second;
129 for (
const auto& input : parent.
inputs) {
130 auto found = topology.
producers.find(input);
134 auto child = shapes.find(found->second);
135 if (child != shapes.end() && compatible(parent, child->second))
136 hasParent.insert(child->first);
139 for (
const auto& entry : shapes)
140 if (!hasParent.count(entry.first))
141 roots.push_back(entry.first);
142 std::sort(roots.begin(), roots.end(),
143 [](Cell* a, Cell* b) { return a->name < b->name; });
147 for (
auto* root : roots) {
148 const Gate* shape = &shapes.at(root);
149 std::vector<SigSpec> pending(shape->
inputs.rbegin(), shape->
inputs.rend()), inputs;
150 pool<Cell*> absorbed;
151 pool<SigBit> rootOutputs;
152 for (
auto bit : shape->
output)
153 rootOutputs.insert(bit);
155 while (!pending.empty()) {
156 auto input = pending.back();
158 for (
auto bit : input)
159 if (rootOutputs.count(bit))
163 auto found = topology.
producers.find(input);
169 const auto childEntry = shapes.find(child);
170 const Gate* childShape =
171 childEntry != shapes.end() && topology.
privateOutput(child, input)
172 ? &childEntry->second
174 if (!childShape || !compatible(*shape, *childShape)) {
175 inputs.push_back(input);
178 if (!absorbed.insert(child).second) {
182 pending.insert(pending.end(), childShape->
inputs.rbegin(),
183 childShape->
inputs.rend());
185 if (!acyclic || absorbed.empty() || GetSize(inputs) > 65535)
187 root->type =
id(
"SILICON_GATE");
188 root->parameters.clear();
189 root->connections_.clear();
190 root->setParam(
id(
"MODE"), shape->
mode);
191 root->setParam(ID::WIDTH, shape->
width);
192 root->setParam(
id(
"BITWISE"), shape->
bitwise ? 1 : 0);
193 root->setParam(
id(
"INPUTS"), GetSize(inputs));
194 for (
int i = 0; i < GetSize(inputs); ++i)
195 root->setPort(RTLIL::escape_id(
"A" + std::to_string(i)), inputs[i]);
196 root->setPort(ID::Y, shape->
output);
197 for (
auto* child : absorbed)
198 removed.insert(child);
200 for (
auto* cell : removed)
201 module->remove(cell);
210std::optional<Mux>
mux(Cell* cell, SigMap& signals)
212 if (cell->type != ID($mux) && cell->type != ID($bmux))
215 result.
width = cell->getParam(ID::WIDTH).as_int();
216 result.
selection = signals(cell->getPort(ID::S));
217 result.
output = signals(cell->getPort(ID::Y));
218 const int bits = GetSize(result.
selection);
219 if (result.
width < 1 || result.
width > 65535 || bits < 1 || bits > 15
222 if (cell->type == ID($mux)) {
225 result.
lanes = {signals(cell->getPort(ID::A)), signals(cell->getPort(ID::B))};
227 auto data = signals(cell->getPort(ID::A));
228 if (GetSize(data) != result.
width * (1 << bits))
230 for (
int i = 0; i < 1 << bits; ++i)
233 for (
const auto& lane : result.
lanes)
234 if (GetSize(lane) != result.
width)
241 if (!module->processes.empty()) {
242 log_warning(
"SILICON: run proc before silicon_mux on module %s.\n",
250 pool<Cell*> selected, removed;
251 std::vector<Cell*> roots;
252 for (
auto* cell : module->selected_cells()) {
253 selected.insert(cell);
254 roots.push_back(cell);
256 for (
auto* root : roots) {
257 if (removed.count(root) || root->get_bool_attribute(ID::keep))
262 std::vector<Mux> children;
263 pool<Cell*> absorbed;
264 for (
const auto& lane : shape->lanes) {
265 auto found = topology.
producers.find(lane);
268 child && selected.count(child) && !removed.count(child)
272 if (!childShape || child == root || childShape->width != shape->width
273 || !absorbed.insert(child).second
274 || (!children.empty() && childShape->selection != children.front().selection))
276 children.push_back(*childShape);
278 if (children.size() != shape->lanes.size()
279 || GetSize(shape->selection) + GetSize(children.front().selection) > 8)
281 SigSpec data, selection = shape->selection;
283 children.front().selection);
285 for (
auto bit : shape->output)
287 bool feedback =
false;
288 for (
auto bit : selection)
289 feedback |= output.count(bit);
290 for (
int lane = 0; lane < GetSize(children.front().lanes); ++lane)
291 for (
const auto& child : children) {
292 data.append(child.lanes[lane]);
293 for (
auto bit : child.lanes[lane])
294 feedback |= output.count(bit);
298 root->type = ID($bmux);
299 root->parameters.clear();
300 root->connections_.clear();
301 root->setParam(ID::WIDTH, shape->width);
302 root->setParam(ID::S_WIDTH, GetSize(selection));
303 root->setPort(ID::A, data);
304 root->setPort(ID::S, selection);
305 root->setPort(ID::Y, shape->output);
306 for (
auto* child : absorbed)
307 removed.insert(child);
310 for (
auto* cell : removed)
311 module->remove(cell);
317 if (!module->processes.empty())
319 SigMap signals(module);
320 for (
auto* cell : module->selected_cells()) {
321 if (cell->get_bool_attribute(ID::keep))
323 const auto shape =
mux(cell, signals);
326 dict<SigSpec, int> indices;
328 std::vector<RTLIL::State> table;
329 for (
const auto& lane : shape->lanes) {
330 auto entry = indices.find(lane);
332 if (entry == indices.end()) {
333 index = GetSize(indices);
334 indices[lane] = index;
337 index = entry->second;
338 for (
int bit = 0; bit < GetSize(shape->selection); ++bit)
339 table.push_back(index & (1 << bit) ? RTLIL::State::S1 : RTLIL::State::S0);
341 if (GetSize(indices) == GetSize(shape->lanes))
343 cell->type =
id(
"SILICON_MUX");
344 cell->parameters.clear();
345 cell->connections_.clear();
346 cell->setParam(ID::WIDTH, shape->width);
347 cell->setParam(ID::S_WIDTH, GetSize(shape->selection));
348 cell->setParam(
id(
"INPUTS"), GetSize(indices));
349 cell->setParam(
id(
"TABLE"), RTLIL::Const(std::move(table)));
350 cell->setPort(ID::A, data);
351 cell->setPort(ID::S, shape->selection);
352 cell->setPort(ID::Y, shape->output);