18#include "kernel/ffinit.h"
19#include "kernel/register.h"
20#include "kernel/rtlil.h"
21#include "kernel/sigtools.h"
22#include "kernel/yosys.h"
40 return !FfInitVals(&sigmap, module)(signal).is_fully_undef_x_only();
43#include "silicon_import_pm.h"
47using EqMember = std::pair<RTLIL::Cell*, int>;
54 RTLIL::SigSpec selector;
58struct EqDecoderGroup {
59 RTLIL::SigSpec selector;
60 std::vector<EqMember> members;
63std::vector<RTLIL::Cell*> allCells(RTLIL::Module* module)
65 std::vector<RTLIL::Cell*>
cells;
66 cells.reserve(module->cells().size());
67 for (
auto* cell : module->cells())
68 cells.push_back(cell);
72void collectSelectedCells(pool<RTLIL::Cell*>& selectedCells, RTLIL::Module* module)
74 for (
auto* cell : module->selected_cells())
75 selectedCells.insert(cell);
80 SignalUsers(RTLIL::Module* module, SigMap& sigmap) : sigmap(sigmap)
82 for (
auto port : module->ports)
83 add(module->wire(port),
nullptr);
85 for (
auto* cell : module->cells())
86 for (
const auto& connection : cell->connections())
87 add(connection.second, cell);
90 int count(
const RTLIL::SigSpec& signal)
92 pool<RTLIL::Cell*> users;
93 for (
const auto bit : sigmap(signal)) {
94 const auto it = this->users.find(bit);
95 if (it == this->users.end())
97 for (
auto* user : it->second)
100 return GetSize(users);
104 void add(
const RTLIL::SigSpec& signal, RTLIL::Cell* cell)
106 for (
const auto bit : sigmap(signal)) {
107 if (bit.wire ==
nullptr)
109 this->users[bit].insert(cell);
114 dict<RTLIL::SigBit, pool<RTLIL::Cell*>> users;
117std::optional<EqCandidate> decodeEquality(RTLIL::Cell* cell, SigMap& sigmap,
120 if (cell->type != ID($eq))
123 const int aWidth = cell->getParam(ID::A_WIDTH).as_int();
124 const int bWidth = cell->getParam(ID::B_WIDTH).as_int();
125 const int yWidth = cell->getParam(ID::Y_WIDTH).as_int();
130 const auto output = sigmap(cell->getPort(ID::Y));
131 if (GetSize(output) != 1 || users.count(output) <= 1)
134 const auto a = sigmap(cell->getPort(ID::A));
135 const auto b = sigmap(cell->getPort(ID::B));
136 if (GetSize(a) != aWidth || GetSize(b) != bWidth)
139 const bool aConstant = a.is_fully_const();
140 const bool bConstant = b.is_fully_const();
141 if (aConstant == bConstant)
147 const auto& signal = aConstant ? b : a;
148 const auto& constant = aConstant ? a : b;
149 const auto value = constant.as_const();
150 if (!
value.is_fully_def())
153 return EqCandidate{cell, signal,
value.as_int()};
156std::vector<EqDecoderGroup> collectEqDecoderGroups(RTLIL::Module* module)
169 SigMap sigmap(module);
170 SignalUsers users(module, sigmap);
172 dict<RTLIL::SigSpec, std::vector<EqMember>> buckets;
173 for (
auto* cell : module->selected_cells()) {
174 const auto candidate = decodeEquality(cell, sigmap, users);
176 buckets[candidate->selector].emplace_back(candidate->cell, candidate->value);
179 std::vector<EqDecoderGroup> groups;
180 for (
auto& [selector, bucket] : buckets) {
181 if (GetSize(bucket) < 2)
186 for (
const auto& [cell, value] : bucket) {
188 if (!values.insert(value).second) {
196 const int laneCount = 1 << GetSize(selector);
200 std::sort(bucket.begin(), bucket.end(), [](
const EqMember& lhs,
const EqMember& rhs) {
201 return lhs.first->name.str() < rhs.first->name.str();
204 groups.push_back({selector, std::move(bucket)});
207 std::sort(groups.begin(), groups.end(),
208 [](
const EqDecoderGroup& lhs,
const EqDecoderGroup& rhs) {
209 return lhs.members.front().first->name.str()
210 < rhs.members.front().first->name.str();
216void raiseDecodedPmux(silicon_import_pm& matcher)
218 auto& state = matcher.st_decoded_pmux;
223 RTLIL::SigSpec packedData;
224 for (
const auto& lane : state.lanes)
225 packedData.append(lane);
227 matcher.blacklist(state.pmux);
228 state.pmux->type = ID($bmux);
229 state.pmux->setParam(ID::S_WIDTH, state.selector.size());
230 state.pmux->setPort(ID::A, packedData);
231 state.pmux->setPort(ID::S, state.selector);
232 state.pmux->unsetPort(ID::B);
234 log(
"Raised decoded $pmux cell %s.%s to $bmux.\n", log_id(matcher.module),
238void foldMemrdAddress(silicon_import_pm& matcher)
240 auto& state = matcher.st_memrd_address;
241 matcher.blacklist(state.mem);
242 state.mem->setPort(ID::RD_ADDR, state.reg->getPort(ID::D));
243 state.mem->setPort(ID::RD_EN, state.reg->getPort(ID::EN));
244 log(
"Folded ROM address register %s and mux %s into %s.\n",
245 log_id(state.reg), log_id(state.mux), log_id(state.mem));
246 matcher.autoremove(state.mux);
247 matcher.autoremove(state.reg);
250void raiseEqDecoder(RTLIL::Module* module,
const EqDecoderGroup& group)
252 const int laneCount = 1 << GetSize(group.selector);
256 RTLIL::SigSpec outputs(module->addWire(NEW_ID, laneCount));
257 for (
const auto& [cell, value] : group.members)
258 outputs[
value] = cell->getPort(ID::Y)[0];
260 auto* demux =
module->addDemux(NEW_ID, RTLIL::Const(RTLIL::State::S1, 1),
261 group.selector, outputs);
263 const int memberCount = GetSize(group.members);
264 for (
const auto& [cell, value] : group.members) {
266 module->remove(cell);
269 log(
"Raised %d equality cells in %s to decoder %s.\n", memberCount, log_id(module),
283 log(
" silicon_pmux_bmux [selection]\n");
285 log(
"Replace $pmux cells whose mutually-exclusive select inputs compare one\n");
286 log(
"binary selector against constant values with an equivalent $bmux. Sparse\n");
287 log(
"choices are filled from the $pmux default input. Priority muxes are left\n");
292 void execute(std::vector<std::string> args, RTLIL::Design* design)
override
294 log_header(design,
"Executing SILICON_PMUX_BMUX pass.\n");
295 extra_args(args, 1, design);
297 for (
auto* module : design->selected_modules()) {
298 silicon_import_pm matcher(module, allCells(module));
299 collectSelectedCells(matcher.ud_decoded_pmux.selected_cells, module);
300 matcher.run_decoded_pmux(raiseDecodedPmux);
307 : Pass(
"silicon_eq_decoder",
"raise shared constant equality cells to $demux")
314 log(
" silicon_eq_decoder [selection]\n");
316 log(
"Replace groups of selected equal-width $eq cells that compare one selector\n");
317 log(
"against distinct fully-defined constants with an equivalent $demux decoder.\n");
318 log(
"Groups with fewer than two comparisons or duplicate values are unchanged.\n");
322 void execute(std::vector<std::string> args, RTLIL::Design* design)
override
324 log_header(design,
"Executing SILICON_EQ_DECODER pass.\n");
325 extra_args(args, 1, design);
327 for (
auto* module : design->selected_modules())
328 for (
const auto& group : collectEqDecoderGroups(module))
329 raiseEqDecoder(module, group);
335 : Pass(
"silicon_memrd_address",
"remove redundant ROM address feedback after memory_dff")
341 log(
"\n silicon_memrd_address [selection]\n\n");
342 log(
"Fold an enabled address register and its feedback mux into the read\n");
343 log(
"enable of a synchronous, read-only memory port. Run after memory_dff.\n\n");
346 void execute(std::vector<std::string> args, RTLIL::Design* design)
override
348 log_header(design,
"Executing SILICON_MEMRD_ADDRESS pass.\n");
349 extra_args(args, 1, design);
351 for (
auto* module : design->selected_modules()) {
352 silicon_import_pm matcher(module, allCells(module));
353 collectSelectedCells(matcher.ud_memrd_address.selected_cells, module);
354 matcher.run_memrd_address(foldMemrdAddress);
370 : Pass(
"silicon_memory_lower",
"lower supported Yosys memories to SILICON_ROM") {}
374 log(
"\n silicon_memory_lower [selection]\n\n");
375 log(
"Validate read-only $mem_v2 cells and emit one SILICON_ROM per read port.\n");
376 log(
"Run after memory_dff and silicon_memrd_address.\n\n");
379 void execute(std::vector<std::string> args, RTLIL::Design* design)
override
381 log_header(design,
"Executing SILICON_MEMORY_LOWER pass.\n");
382 extra_args(args, 1, design);
383 for (
auto* module : design->selected_modules()) {
384 const auto cells = allCells(module);
385 pool<RTLIL::Cell*> selected, removed;
386 collectSelectedCells(selected, module);
387 for (
auto* mem : cells) {
388 if (!selected.count(mem) || mem->type != ID($mem_v2))
continue;
389 const auto reject = [&](
const char* reason) {
390 log_error(
"Unsupported ROM %s.%s: %s.\n", log_id(module), log_id(mem), reason);
392 const int width = mem->getParam(ID::WIDTH).as_int();
393 const int size = mem->getParam(ID::SIZE).as_int();
394 const int abits = mem->getParam(ID::ABITS).as_int();
395 const int ports = mem->getParam(ID::RD_PORTS).as_int();
396 const auto isZero = [](
const RTLIL::Const& value) {
397 for (
int bit = 0; bit < GetSize(value); ++bit)
398 if (value[bit] != RTLIL::State::S0)
return false;
401 if (width <= 0 || width > std::numeric_limits<unsigned short>::max()
402 || size <= 0 || size > std::numeric_limits<int>::max() / width
403 || (size & (size - 1)) != 0
404 || abits != std::max(1,
static_cast<int>(std::bit_width(
static_cast<unsigned>(size - 1))))
405 || ports <= 0 || ports > std::numeric_limits<int>::max() / width
406 || ports > std::numeric_limits<int>::max() / std::max(1, abits)
407 || !isZero(mem->getParam(ID::OFFSET))
408 || !isZero(mem->getParam(ID::WR_PORTS)))
409 reject(
"unsupported geometry or write ports");
410 for (
auto key : {ID::RD_CE_OVER_SRST, ID::RD_TRANSPARENCY_MASK,
411 ID::RD_COLLISION_X_MASK, ID::RD_WIDE_CONTINUATION,
412 ID::WR_CLK_ENABLE, ID::WR_CLK_POLARITY,
413 ID::WR_PRIORITY_MASK, ID::WR_WIDE_CONTINUATION})
414 if (!isZero(mem->getParam(key)))
415 reject(
"unsupported read controls or write behavior");
416 for (
auto key : {ID::WR_ADDR, ID::WR_DATA, ID::WR_EN, ID::WR_CLK})
417 if (GetSize(mem->getPort(key)) != 0)
418 reject(
"memory write connections are not supported");
419 const auto init = mem->getParam(ID::INIT);
420 if (GetSize(init) != size * width || !init.is_fully_def())
421 reject(
"initialization must fully define every word");
422 const auto addr = mem->getPort(ID::RD_ADDR);
423 const auto data = mem->getPort(ID::RD_DATA);
424 const auto enable = mem->getPort(ID::RD_EN);
425 const auto clock = mem->getPort(ID::RD_CLK);
426 const auto arst = mem->getPort(ID::RD_ARST);
427 const auto srst = mem->getPort(ID::RD_SRST);
428 const auto clkEnable = mem->getParam(ID::RD_CLK_ENABLE);
429 const auto clkPolarity = mem->getParam(ID::RD_CLK_POLARITY);
430 const auto initial = mem->getParam(ID::RD_INIT_VALUE);
431 if (GetSize(addr) != ports * abits || GetSize(data) != ports * width
432 || GetSize(enable) != ports || GetSize(clock) != ports
433 || GetSize(arst) != ports || GetSize(srst) != ports
434 || GetSize(clkEnable) != ports || GetSize(clkPolarity) != ports
435 || GetSize(initial) != ports * width)
436 reject(
"invalid read port widths");
438 for (
int port = 0; port < ports; ++port) {
439 const bool sync = clkEnable[port] == RTLIL::State::S1;
440 const bool positive = clkPolarity[port] == RTLIL::State::S1;
441 if ((clkEnable[port] != RTLIL::State::S0 && !sync)
442 || (clkPolarity[port] != RTLIL::State::S0 && !positive))
443 reject(
"undefined read clock control");
445 for (
int bit = 0; bit < width; ++bit)
446 if (initial[port * width + bit] != RTLIL::State::Sx)
447 reject(
"initialized synchronous read register is unsupported");
448 if (arst[port] != RTLIL::State::S0 || srst[port] != RTLIL::State::S0)
449 reject(
"synchronous read reset is unsupported");
450 }
else if (enable[port] != RTLIL::State::S1) {
451 reject(
"asynchronous read enable must be constant one");
453 const auto readAddr = addr.extract(port * abits, abits);
454 const auto readData = data.extract(port * width, width);
455 if (size == 1 && readAddr != RTLIL::SigSpec(RTLIL::State::S0, abits))
456 reject(
"one-word memory address must be constant zero");
458 RTLIL::SigSpec output = readData;
459 RTLIL::SigSpec select(RTLIL::State::S1);
460 for (
auto* latch : cells) {
461 if (removed.count(latch) || !selected.count(latch) || latch->type != ID($dlatch)
462 || latch->getPort(ID::D) != readData)
continue;
463 if (latch->getParam(ID::EN_POLARITY).as_int() != 1
464 || GetSize(latch->getPort(ID::Q)) != width
465 || GetSize(latch->getPort(ID::EN)) != 1)
466 reject(
"unsupported ROM output latch");
467 output = latch->getPort(ID::Q);
468 select = latch->getPort(ID::EN);
469 removed.insert(latch);
473 auto* rom =
module->addCell(NEW_ID, RTLIL::escape_id("SILICON_ROM"));
474 rom->setParam(ID::WIDTH, width);
475 rom->setParam(ID::SIZE, size);
476 rom->setParam(ID::ABITS, abits);
477 rom->setParam(ID::INIT, init);
478 rom->setParam(RTLIL::escape_id(
"ID"), mem->getParam(ID::MEMID));
479 rom->setParam(RTLIL::escape_id(
"SYNC"), sync ? 1 : 0);
480 rom->setParam(ID::CLK_POLARITY, positive ? 1 : 0);
481 rom->setPort(ID::ADDR, readAddr);
482 rom->setPort(ID::DATA, output);
483 rom->setPort(RTLIL::escape_id(
"SELECT"), select);
484 rom->setPort(ID::CLK, sync ? clock.extract(port, 1) : RTLIL::SigSpec(RTLIL::State::S0));
485 rom->setPort(ID::EN, sync ? enable.extract(port, 1) : RTLIL::SigSpec(RTLIL::State::S1));
486 rom->attributes = mem->attributes;
490 for (
auto* latch : removed)
491 module->remove(latch);
QVariant value(const QSettings &settings, const Definition &setting)
Reads a setting value, falling back to its declared default when unset.
USING_YOSYS_NAMESPACE PRIVATE_NAMESPACE_BEGIN constexpr int MAX_DECODED_SELECTOR_WIDTH
constexpr int DECODER_DENSITY_FACTOR
bool hasDefinedInit(const RTLIL::SigSpec &signal, RTLIL::Module *module, const SigMap &sigmap)
void execute(std::vector< std::string > args, RTLIL::Design *design) override
void execute(std::vector< std::string > args, RTLIL::Design *design) override
SiliconMemrdAddressPass()
void execute(std::vector< std::string > args, RTLIL::Design *design) override
void execute(std::vector< std::string > args, RTLIL::Design *design) override