SILICON
Loading...
Searching...
No Matches
graphicalMultiplexer.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
19
20#include <algorithm>
21#include <stdexcept>
22#include <variant>
23
24#include <QGraphicsRectItem>
25#include <QPainter>
26
27#include <ui/shell/theme.hpp>
29
30namespace SILICON::ui {
31using namespace SILICON::core;
32using namespace SILICON::extra;
33
34namespace {
35std::shared_ptr<Multiplexer> makeMultiplexer()
36{
37 return std::make_shared<Multiplexer>(Bus(2), Bus(1), Wire_ptr{});
38}
39
40std::shared_ptr<Demultiplexer> makeDemultiplexer()
41{
42 return std::make_shared<Demultiplexer>(Bus(1), Bus(1), Bus(2));
43}
44
45std::shared_ptr<Decoder> makeDecoder()
46{
47 return std::make_shared<Decoder>(Bus(1), Bus(1), Bus(2));
48}
49
50int getMuxWidth(const int numberOfInputs)
51{
52 return std::max(150, 40 + (numberOfInputs - 1) * 40);
53}
54
55class LabeledMuxRectItem : public QGraphicsRectItem {
56public:
57 explicit LabeledMuxRectItem(const QRectF& rect, QString label)
58 : QGraphicsRectItem(rect), label(std::move(label))
59 {
60 }
61
62 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
63 QWidget* widget) override
64 {
65 Q_UNUSED(option);
66 Q_UNUSED(widget);
67
68 painter->save();
69 painter->setPen(QPen(ThemeEngine::getColor(theme::ColorToken::Ink), 3));
71 painter->drawRect(rect());
72
73 painter->setPen(QPen(ThemeEngine::getColor(theme::ColorToken::Ink)));
74 painter->setFont(QFont("NovaMono", 9));
75 painter->drawText(rect().adjusted(6, 6, -6, -6), label,
76 QTextOption(Qt::AlignCenter));
77 painter->restore();
78 }
79
80private:
81 QString label;
82};
83
84QGraphicsItem* busShape(const int busSize, const int selectionSize, const bool demux,
85 const int distinctInputs = 0)
86{
87 const int dataBusCount = distinctInputs > 0 ? distinctInputs : 1 << selectionSize;
88 const int width = getMuxWidth(dataBusCount);
89 constexpr int HEIGHT = 90;
90 QString label = demux ? QString("%1 demux 1 to %2").arg(busSize).arg(dataBusCount)
91 : QString("%1 mux %2 to 1").arg(busSize).arg(dataBusCount);
92 if (!demux && dataBusCount != 1 << selectionSize)
93 label = QString("%1-bit mux\n%2 choices / %3 selections")
94 .arg(busSize)
95 .arg(dataBusCount)
96 .arg(1 << selectionSize);
97 return new LabeledMuxRectItem(QRectF(0, 0, width, HEIGHT), label);
98}
99
100std::vector<PortPair> northBusPorts(const int count, const QString& prefix,
101 const int width)
102{
103 std::vector<PortPair> ports;
104 ports.reserve(static_cast<std::size_t>(count));
105
106 const int startX = (width - (count - 1) * 40) / 2;
107 for (int index = 0; index < count; ++index)
108 ports.emplace_back(PortPair{QString("%1[%2]").arg(prefix).arg(index),
109 DiagramScene::snapToGrid(QPoint(startX + index * 40, -20))});
110
111 return ports;
112}
113} // namespace
114
115// --- Graphical Multiplexer -------------------------------------------------------------
116
118 : GraphicalLogicComponent(makeMultiplexer(),
119 new ThemedSvgItem(":/other_components/MUX.svg"), parent, true)
120{
121 printPortNames = true;
123 updateLayout(1, 1);
124}
125
127{
128 auto* multiplexer = dynamic_cast<Multiplexer*>(associatedComponent.get());
129 if (!multiplexer)
130 throw std::logic_error("GraphicalMultiplexer requires a Multiplexer component");
131 return multiplexer;
132}
133
135{
137 return;
138
139 associatedComponent->setPropertyCallback("selectionSize", [this](const PropertyValue& value) {
140 return applySelectionSize(std::get<int>(value));
141 });
142
143 associatedComponent->setPropertyCallback("busSize", [this](const PropertyValue& value) {
144 return applyBusSize(std::get<int>(value));
145 });
146 associatedComponent->setPropertyCallback(
147 "selectionTable", [this](const PropertyValue& value) {
148 const auto result =
149 getComponentAsMultiplexer()->setSelectionTable(std::get<std::string>(value));
151 associatedComponent->getPropertyValue<int>("selectionSize").value_or(1),
152 associatedComponent->getPropertyValue<int>("busSize").value_or(1));
153 return result;
154 });
155}
156
157void GraphicalMultiplexer::updateLayout(const int selectionSize, const int busSize)
158{
159 const int dataBusCount = static_cast<int>(associatedComponent->inputBuses().size()) - 1;
160
161 if (busSize > 1 || selectionSize > 1 || dataBusCount != 1 << selectionSize) {
162 setItemShape(busShape(busSize, selectionSize, false, dataBusCount));
163 const int width = getMuxWidth(dataBusCount);
164
165 auto inputs = northBusPorts(dataBusCount, "d", width);
166 inputs.emplace_back("sel", QPoint(-20, 50));
167 setPorts(inputs, {PortPair{"out", DiagramScene::snapToGrid(QPoint(width / 2, 110))}});
168 const auto& table = getComponentAsMultiplexer()->selectionTableIndices();
169 if (!table.empty()) {
170 std::vector<QString> values(static_cast<std::size_t>(dataBusCount));
171 for (std::size_t first = 0; first < table.size();) {
172 const int port = table[first];
173 std::size_t end = first + 1;
174 while (end < table.size() && table[end] == port)
175 ++end;
176 if (port >= 0 && port < dataBusCount) {
177 auto& text = values[static_cast<std::size_t>(port)];
178 if (!text.isEmpty())
179 text += ", ";
180 text += end == first + 1 ? QString::number(first)
181 : QString("%1–%2").arg(first).arg(end - 1);
182 }
183 first = end;
184 }
185 for (int port = 0; port < dataBusCount; ++port)
186 getInputPorts()[port]->setToolTip(
187 QString("Selected when sel is %1").arg(values[static_cast<std::size_t>(port)]));
188 }
189 return;
190 }
191
192 setItemShape(new ThemedSvgItem(":/other_components/MUX.svg"));
193 if (selectionSize == 1) {
194 setPorts({PortPair{"d[0]", QPoint(-20, 30)}, PortPair{"d[1]", QPoint(-20, 70)},
195 PortPair{"sel", QPoint(60, -10)}},
196 {PortPair{"out", QPoint(120, 50)}});
197 }
198}
199
201{
202 const int appliedSelectionSize = getComponentAsMultiplexer()->setSelectionSize(selectionSize);
203 const int appliedBusSize = associatedComponent->getPropertyValue<int>("busSize").value_or(1);
204 updateLayout(appliedSelectionSize, appliedBusSize);
205 return appliedSelectionSize;
206}
207
209{
210 const int appliedBusSize = getComponentAsMultiplexer()->setBusSize(busSize);
211 const int appliedSelectionSize = associatedComponent->getPropertyValue<int>("selectionSize").value_or(1);
212 updateLayout(appliedSelectionSize, appliedBusSize);
213 return appliedBusSize;
214}
215
217{
219 if (!component)
220 return;
221
223 const int appliedSelSize = getComponentAsMultiplexer()->setSelectionSize(component->getPropertyValue<int>("selectionSize").value_or(1));
224 const int appliedBusSize = getComponentAsMultiplexer()->setBusSize(component->getPropertyValue<int>("busSize").value_or(1));
225 updateLayout(appliedSelSize, appliedBusSize);
226}
227
228
229// --- Graphical Demultiplexer -----------------------------------------------------------
230
232 : GraphicalLogicComponent(makeDemultiplexer(),
233 new ThemedSvgItem(":/other_components/DEMUX.svg"), parent,
234 true)
235{
236 printPortNames = true;
238 updateLayout(1, 1);
239}
240
242{
243 auto* demultiplexer = dynamic_cast<Demultiplexer*>(associatedComponent.get());
244 if (!demultiplexer)
245 throw std::logic_error("GraphicalDemultiplexer requires a Demultiplexer component");
246 return demultiplexer;
247}
248
250{
252 return;
253
254 associatedComponent->setPropertyCallback("selectionSize", [this](const PropertyValue& value) {
255 return applySelectionSize(std::get<int>(value));
256 });
257
258 associatedComponent->setPropertyCallback("busSize", [this](const PropertyValue& value) {
259 return applyBusSize(std::get<int>(value));
260 });
261}
262
263void GraphicalDemultiplexer::updateLayout(const int selectionSize, const int busSize)
264{
265 const int outputBusCount = 1 << selectionSize;
266
267 if (busSize > 1) {
268 setItemShape(busShape(busSize, selectionSize, true));
269 const int width = getMuxWidth(outputBusCount);
270
271 setPorts({PortPair{"data", QPoint(width / 2, 110)}, PortPair{"sel", QPoint(-20, 45)}},
272 northBusPorts(outputBusCount, "o", width));
273 return;
274 }
275
276 setItemShape(new ThemedSvgItem(":/other_components/DEMUX.svg"));
277 setPorts({PortPair{"d", QPoint(-20, 50)}, PortPair{"sel", QPoint(60, -10)}},
278 {PortPair{"out", QPoint(120, 50)}});
279}
280
282{
283 const int appliedSelectionSize = getComponentAsDemultiplexer()->setSelectionSize(selectionSize);
284 const int appliedBusSize = associatedComponent->getPropertyValue<int>("busSize").value_or(1);
285 updateLayout(appliedSelectionSize, appliedBusSize);
286 return appliedSelectionSize;
287}
288
290{
291 const int appliedBusSize = getComponentAsDemultiplexer()->setBusSize(busSize);
292 const int appliedSelectionSize = associatedComponent->getPropertyValue<int>("selectionSize").value_or(1);
293 updateLayout(appliedSelectionSize, appliedBusSize);
294 return appliedBusSize;
295}
296
298{
300 if (!component)
301 return;
302
304 const int appliedSelSize = getComponentAsDemultiplexer()->setSelectionSize(component->getPropertyValue<int>("selectionSize").value_or(1));
305 const int appliedBusSize = getComponentAsDemultiplexer()->setBusSize(component->getPropertyValue<int>("busSize").value_or(1));
306 updateLayout(appliedSelSize, appliedBusSize);
307}
308
309// --- Graphical Decoder -----------------------------------------------------------------
310
313 makeDecoder(), new ThemedSvgItem(":/other_components/DECODER.svg"), parent, true)
314{
315 printPortNames = true;
318}
319
321{
322 auto* decoder = dynamic_cast<Decoder*>(associatedComponent.get());
323 if (!decoder)
324 throw std::logic_error("GraphicalDecoder requires a Decoder component");
325 return decoder;
326}
327
329{
331 return;
332
333 associatedComponent->setPropertyCallback("selectionSize", [this](const PropertyValue& value) {
334 return applySelectionSize(std::get<int>(value));
335 });
336}
337
338int GraphicalDecoder::applySelectionSize(const int selectionSize)
339{
340 const int appliedSelectionSize = getComponentAsDecoder()->setSelectionSize(selectionSize);
341
342 setPorts({PortPair{"en", QPoint(40, -10)}, PortPair{"sel", QPoint(60, -10)}},
343 {PortPair{"out", QPoint(120, 50)}});
344
345 return appliedSelectionSize;
346}
347
349{
351 if (!component)
352 return;
353
355 applySelectionSize(component->getPropertyValue<int>("selectionSize").value_or(1));
356}
357
358} // namespace SILICON::ui
int setSelectionSize(int selectionSize)
int setSelectionSize(int selectionSize)
std::string setSelectionTable(const std::string &specification)
const std::vector< unsigned short > & selectionTableIndices() const
int setSelectionSize(int selectionSize)
static QPoint snapToGrid(QPointF point)
Snaps a point to the grid.
const std::vector< Port * > & getInputPorts() const
Borrows input ports; copy before replacing the component ports.
bool printPortNames
Whether to print port names.
void setItemShape(QGraphicsItem *shape)
Sets the component shape graphics item.
GraphicalDecoder(QGraphicsItem *parent=nullptr)
void setComponent(const Component_ptr &component) override
Sets the associated logical component.
int applySelectionSize(int selectionSize)
GraphicalDemultiplexer(QGraphicsItem *parent=nullptr)
void setComponent(const Component_ptr &component) override
Sets the associated logical component.
Demultiplexer * getComponentAsDemultiplexer() const
void updateLayout(int selectionSize, int busSize)
void setPorts(const std::vector< PortPair > &busToPortInputs, const std::vector< PortPair > &busToPortOutputs) override
Sets ports and validates against component I/O.
GraphicalLogicComponent(ItemCategory category, const Component_ptr &component, QGraphicsItem *shape, QGraphicsItem *parent, bool scanShape=false)
virtual void setComponent(const Component_ptr &component)
Sets the associated logical component.
Component_ptr associatedComponent
The logical component this graphical component represents.
GraphicalMultiplexer(QGraphicsItem *parent=nullptr)
void setComponent(const Component_ptr &component) override
Sets the associated logical component.
void updateLayout(int selectionSize, int busSize)
static const QColor & getColor(theme::ColorToken token)
Definition theme.hpp:92
int width(RTLIL::Module *module, RTLIL::Cell *cell, RTLIL::IdString key)
std::shared_ptr< Component > Component_ptr
std::shared_ptr< Wire > Wire_ptr
Definition wire.hpp:116
std::variant< int, bool, std::string, BusValue > PropertyValue
Variant type for component properties.
std::pair< QString, QPoint > PortPair