SILICON
Loading...
Searching...
No Matches
silicon_bmux_case.cpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "kernel/register.h"
19#include "kernel/rtlil.h"
20#include "kernel/yosys.h"
21
22#include <vector>
23
24USING_YOSYS_NAMESPACE
26
27struct SiliconBmuxCasePass : public Pass {
28 SiliconBmuxCasePass() : Pass("silicon_bmux_case", "raise $bmux cells to case processes")
29 {
30 }
31
32 void help() override
33 {
34 log("\n");
35 log(" silicon_bmux_case [selection]\n");
36 log("\n");
37 log("Replace selected $bmux cells with combinational RTLIL switch processes.\n");
38 log("The Verilog backend emits those processes as case statements instead of\n");
39 log("lowering the binary muxes to nested conditional expressions.\n");
40 log("\n");
41 }
42
43 void execute(std::vector<std::string> args, RTLIL::Design* design) override
44 {
45 log_header(design, "Executing SILICON_BMUX_CASE pass.\n");
46 extra_args(args, 1, design);
47
48 for (auto* module : design->selected_modules()) {
49 std::vector<RTLIL::Cell*> muxes;
50 for (auto* cell : module->selected_cells())
51 if (cell->type == ID($bmux))
52 muxes.push_back(cell);
53
54 for (auto* cell : muxes) {
55 const RTLIL::SigSpec data = cell->getPort(ID::A);
56 const RTLIL::SigSpec selection = cell->getPort(ID::S);
57 const RTLIL::SigSpec output = cell->getPort(ID::Y);
58 const int width = cell->getParam(ID::WIDTH).as_int();
59
60 if (width <= 0 || output.size() != width || selection.empty()
61 || data.size() % width != 0) {
62 log_error("Malformed $bmux cell %s.%s.\n", log_id(module), log_id(cell));
63 }
64
65 const int lane_count = data.size() / width;
66 if (selection.size() >= 31 || lane_count != (1 << selection.size())) {
67 log_error("Inconsistent selector or data width on $bmux cell %s.%s.\n",
68 log_id(module), log_id(cell));
69 }
70
71 auto* process = module->addProcess(module->uniquify("$silicon$bmux_case"));
72 process->attributes = cell->attributes;
73
74 auto* switch_rule = new RTLIL::SwitchRule;
75 switch_rule->signal = selection;
76 process->root_case.switches.push_back(switch_rule);
77
78 for (int lane = 0; lane < lane_count; ++lane) {
79 auto* case_rule = new RTLIL::CaseRule;
80 case_rule->compare.emplace_back(lane, selection.size());
81 case_rule->actions.emplace_back(output, data.extract(lane * width, width));
82 switch_rule->cases.push_back(case_rule);
83 }
84
85 auto* default_rule = new RTLIL::CaseRule;
86 default_rule->actions.emplace_back(output, RTLIL::Const(RTLIL::State::Sx, width));
87 switch_rule->cases.push_back(default_rule);
88
89 log("Raised $bmux cell %s.%s to process %s.\n", log_id(module), log_id(cell),
90 log_id(process));
91 module->remove(cell);
92 }
93 }
94 }
96
97PRIVATE_NAMESPACE_END
void execute(std::vector< std::string > args, RTLIL::Design *design) override