16int width(RTLIL::Module* module, RTLIL::Cell* cell, RTLIL::IdString key)
18 if (!cell->hasParam(key))
19 log_error(
"SILICON: missing %s in module %s cell %s (%s).\n", log_id(key),
20 log_id(module), log_id(cell), log_id(cell->type));
21 int result = cell->getParam(key).as_int();
22 if (result <= 0 || result > std::numeric_limits<unsigned short>::max())
23 log_error(
"SILICON: invalid %s in module %s cell %s (%s): %d.\n", log_id(key),
24 log_id(module), log_id(cell), log_id(cell->type), result);
28bool flag(RTLIL::Module* module, RTLIL::Cell* cell, RTLIL::IdString key)
30 if (!cell->hasParam(key))
31 log_error(
"SILICON: missing %s in module %s cell %s (%s).\n", log_id(key),
32 log_id(module), log_id(cell), log_id(cell->type));
33 auto value = cell->getParam(key);
34 if (!value.is_fully_def() || (value.as_int() != 0 && value.as_int() != 1))
35 log_error(
"SILICON: invalid %s in module %s cell %s (%s).\n", log_id(key),
36 log_id(module), log_id(cell), log_id(cell->type));
37 return value.as_int() == 1;
40RTLIL::SigSpec
port(RTLIL::Module* module, RTLIL::Cell* cell, RTLIL::IdString key,
43 auto value = cell->getPort(key);
44 if (GetSize(value) != expected)
45 log_error(
"SILICON: invalid %s width in module %s cell %s (%s): expected %d, got %d.\n",
46 log_id(key), log_id(module), log_id(cell), log_id(cell->type), expected,
51void narrowTruth(RTLIL::Module* module, RTLIL::Cell* cell,
int yWidth)
53 auto y =
port(module, cell, ID::Y, yWidth);
55 module->connect(y.extract(1, yWidth - 1), RTLIL::SigSpec(RTLIL::State::S0, yWidth - 1));
56 cell->setPort(ID::Y, y.extract(0, 1));
61 int originalWidth,
int targetWidth,
bool signExtend)
63 auto signal =
port(module, cell, key, originalWidth);
64 signal.extend_u0(targetWidth, signExtend);
65 cell->setPort(key, signal);
70 for (
auto* module : design->selected_modules())
71 for (
auto* cell : module->selected_cells()) {
72 int mode = cell->type == ID($logic_and) ? 0 : cell->type == ID($logic_or) ? 1
73 : cell->type == ID($logic_not) ? 2 : -1;
74 if (mode < 0)
continue;
75 int aWidth =
width(module, cell, ID::A_WIDTH);
76 int yWidth =
width(module, cell, ID::Y_WIDTH);
77 (void)
flag(module, cell, ID::A_SIGNED);
78 port(module, cell, ID::A, aWidth);
80 int bWidth =
width(module, cell, ID::B_WIDTH);
81 (void)
flag(module, cell, ID::B_SIGNED);
82 port(module, cell, ID::B, bWidth);
83 cell->setParam(ID::B_WIDTH, bWidth);
86 cell->parameters.clear();
87 cell->setParam(
id(
"MODE"), mode);
88 cell->setParam(ID::A_WIDTH, aWidth);
89 if (mode != 2) cell->setParam(ID::B_WIDTH, GetSize(cell->getPort(ID::B)));
90 cell->type =
id(
"SILICON_LOGIC");
96 for (
auto* module : design->selected_modules())
97 for (
auto* cell : module->selected_cells()) {
98 bool invert = cell->type == ID($ne);
99 int mode = (cell->type == ID($eq) || invert) ? 0 : cell->type == ID($lt) ? 1
100 : cell->type == ID($le) ? 2 : cell->type == ID($gt) ? 3
101 : cell->type == ID($ge) ? 4 : -1;
102 if (mode < 0)
continue;
103 int aWidth =
width(module, cell, ID::A_WIDTH);
104 int bWidth =
width(module, cell, ID::B_WIDTH);
105 int yWidth =
width(module, cell, ID::Y_WIDTH);
106 bool aSigned =
flag(module, cell, ID::A_SIGNED);
107 bool bSigned =
flag(module, cell, ID::B_SIGNED);
108 bool sign = aSigned && bSigned;
109 int targetWidth = std::max(aWidth, bWidth);
114 RTLIL::SigSpec equal(module->addWire(NEW_ID));
115 module->addNot(NEW_ID, equal, cell->getPort(ID::Y));
116 cell->setPort(ID::Y, equal);
118 cell->parameters.clear();
119 cell->setParam(ID::WIDTH, targetWidth);
120 cell->setParam(
id(
"MODE"), mode);
121 cell->setParam(
id(
"SIGNED"), sign ? 1 : 0);
122 cell->type =
id(
"SILICON_COMPARE");
129 const RTLIL::SigSpec& amount,
bool left,
130 RTLIL::Cell*& finalMux)
132 const int dataWidth = GetSize(value);
134 for (
int distance = 1; bit < GetSize(amount) && distance < dataWidth;
135 ++bit, distance *= 2) {
136 RTLIL::SigSpec shifted;
138 shifted = RTLIL::SigSpec(RTLIL::State::Sx, distance);
139 shifted.append(value.extract(0, dataWidth - distance));
141 shifted = value.extract(distance, dataWidth - distance);
142 shifted.append(RTLIL::SigSpec(RTLIL::State::Sx, distance));
144 RTLIL::SigSpec next(module->addWire(NEW_ID, dataWidth));
145 finalMux =
module->addMux(NEW_ID, value, shifted, amount[bit], next);
148 if (bit < GetSize(amount)) {
149 RTLIL::SigSpec overflow(module->addWire(NEW_ID));
150 module->addReduceOr(NEW_ID, amount.extract(bit, GetSize(amount) - bit), overflow);
151 RTLIL::SigSpec next(module->addWire(NEW_ID, dataWidth));
152 finalMux =
module->addMux(NEW_ID, value, RTLIL::SigSpec(RTLIL::State::Sx, dataWidth),
161 const int aWidth =
width(module, cell, ID::A_WIDTH);
162 const int bWidth =
width(module, cell, ID::B_WIDTH);
163 const int yWidth =
width(module, cell, ID::Y_WIDTH);
164 (void)
flag(module, cell, ID::A_SIGNED);
165 const bool signedOffset =
flag(module, cell, ID::B_SIGNED);
166 auto a =
port(module, cell, ID::A, aWidth);
167 const auto b =
port(module, cell, ID::B, bWidth);
168 const auto y =
port(module, cell, ID::Y, yWidth);
171 a.append(RTLIL::SigSpec(RTLIL::State::Sx, yWidth - aWidth));
172 RTLIL::Cell* finalMux =
nullptr;
175 RTLIL::SigSpec magnitude(module->addWire(NEW_ID, bWidth));
176 module->addNeg(NEW_ID, b, magnitude);
177 const auto negative =
selectionShift(module, a, magnitude,
true, finalMux);
178 RTLIL::SigSpec selected(module->addWire(NEW_ID, GetSize(a)));
179 finalMux =
module->addMux(NEW_ID, result, negative, b[bWidth - 1], selected);
184 if (cell->get_bool_attribute(ID::keep)) {
185 log_assert(finalMux !=
nullptr);
186 finalMux->attributes[ID::keep] = cell->attributes.at(ID::keep);
188 module->connect(y, result.extract(0, yWidth));
189 module->remove(cell);
194 for (
auto* module : design->selected_modules()) {
196 const auto selected =
module->selected_cells();
197 const std::vector<RTLIL::Cell*> cells(selected.begin(), selected.end());
198 for (
auto* cell : cells) {
199 if (cell->type == ID($shiftx)) {
203 int direction = cell->type == ID($shl) ? 0 :
204 (cell->type == ID($shr) || cell->type == ID($sshr)) ? 1 : -1;
205 if (direction < 0)
continue;
206 bool arithmeticCell = cell->type == ID($sshr);
207 int aWidth =
width(module, cell, ID::A_WIDTH);
208 int bWidth =
width(module, cell, ID::B_WIDTH);
209 int yWidth =
width(module, cell, ID::Y_WIDTH);
210 bool signedInput =
flag(module, cell, ID::A_SIGNED);
211 if (
flag(module, cell, ID::B_SIGNED))
212 log_error(
"SILICON: unsupported signed shift amount in module %s cell %s (%s).\n",
213 log_id(module), log_id(cell), log_id(cell->type));
215 port(module, cell, ID::B, bWidth);
216 port(module, cell, ID::Y, yWidth);
217 cell->parameters.clear();
218 cell->setParam(ID::WIDTH, yWidth);
219 cell->setParam(
id(
"B_WIDTH"), bWidth);
220 cell->setParam(
id(
"DIRECTION"), direction);
221 cell->setParam(
id(
"ARITHMETIC"), arithmeticCell && signedInput ? 1 : 0);
222 cell->type =
id(
"SILICON_SHIFT");
230 static const pool<RTLIL::IdString> types = {
231 ID($and), ID($or), ID($xor), ID($not), ID($_NAND_), ID($_NOR_),
232 ID($pos), ID($add), ID($sub), ID($neg), ID($mux), ID($bmux), ID($demux),
234 id(
"SILICON_ROM"),
id(
"SILICON_LOGIC"),
id(
"SILICON_COMPARE"),
235 id(
"SILICON_SHIFT"),
id(
"SILICON_DFF"),
id(
"SILICON_DFFE"),
236 id(
"SILICON_GATE"),
id(
"SILICON_MUX"),
237 id(
"SILICON_DLATCH"),
id(
"SILICON_DFFSR"),
id(
"SILICON_DFFSRE"),
238 id(
"SILICON_JKFF"),
id(
"SILICON_HALF_ADDER"),
id(
"SILICON_FULL_ADDER"),
239 id(
"SILICON_ADDER"),
id(
"SILICON_MULTIPLIER"),
id(
"SILICON_DIVIDER"),
240 id(
"SILICON_PIPO"),
id(
"SILICON_PISO"),
241 id(
"SILICON_SIPO"),
id(
"SILICON_SISO")};
243 return types.count(type);
248 for (
auto* module : design->selected_modules())
249 for (
auto* cell : module->selected_cells()) {
250 if (
supported(cell->type) || design->module(cell->type))
continue;
251 log_error(
"SILICON: unsupported cell type %s in module %s cell %s.\n",
252 log_id(cell->type), log_id(module), log_id(cell));