24#include <QGraphicsRectItem>
31using namespace SILICON::core;
32using namespace SILICON::extra;
35std::shared_ptr<Multiplexer> makeMultiplexer()
40std::shared_ptr<Demultiplexer> makeDemultiplexer()
42 return std::make_shared<Demultiplexer>(
Bus(1),
Bus(1),
Bus(2));
45std::shared_ptr<Decoder> makeDecoder()
47 return std::make_shared<Decoder>(
Bus(1),
Bus(1),
Bus(2));
50int getMuxWidth(
const int numberOfInputs)
52 return std::max(150, 40 + (numberOfInputs - 1) * 40);
55class LabeledMuxRectItem :
public QGraphicsRectItem {
57 explicit LabeledMuxRectItem(
const QRectF& rect, QString label)
58 : QGraphicsRectItem(rect), label(std::move(label))
62 void paint(QPainter* painter,
const QStyleOptionGraphicsItem* option,
63 QWidget* widget)
override
71 painter->drawRect(rect());
74 painter->setFont(QFont(
"NovaMono", 9));
75 painter->drawText(rect().adjusted(6, 6, -6, -6), label,
76 QTextOption(Qt::AlignCenter));
84QGraphicsItem* busShape(
const int busSize,
const int selectionSize,
const bool demux,
85 const int distinctInputs = 0)
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")
96 .arg(1 << selectionSize);
97 return new LabeledMuxRectItem(QRectF(0, 0, width, HEIGHT), label);
100std::vector<PortPair> northBusPorts(
const int count,
const QString& prefix,
103 std::vector<PortPair> ports;
104 ports.reserve(
static_cast<std::size_t
>(count));
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))});
119 new
ThemedSvgItem(
":/other_components/MUX.svg"), parent, true)
130 throw std::logic_error(
"GraphicalMultiplexer requires a Multiplexer component");
161 if (busSize > 1 || selectionSize > 1 || dataBusCount != 1 << selectionSize) {
162 setItemShape(busShape(busSize, selectionSize,
false, dataBusCount));
163 const int width = getMuxWidth(dataBusCount);
165 auto inputs = northBusPorts(dataBusCount,
"d", width);
166 inputs.emplace_back(
"sel", QPoint(-20, 50));
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)
176 if (port >= 0 && port < dataBusCount) {
177 auto& text = values[
static_cast<std::size_t
>(port)];
180 text += end == first + 1 ? QString::number(first)
181 : QString(
"%1–%2").arg(first).arg(end - 1);
185 for (
int port = 0; port < dataBusCount; ++port)
187 QString(
"Selected when sel is %1").arg(values[
static_cast<std::size_t
>(port)]));
193 if (selectionSize == 1) {
196 {
PortPair{
"out", QPoint(120, 50)}});
203 const int appliedBusSize =
associatedComponent->getPropertyValue<
int>(
"busSize").value_or(1);
205 return appliedSelectionSize;
211 const int appliedSelectionSize =
associatedComponent->getPropertyValue<
int>(
"selectionSize").value_or(1);
213 return appliedBusSize;
245 throw std::logic_error(
"GraphicalDemultiplexer requires a Demultiplexer component");
246 return demultiplexer;
265 const int outputBusCount = 1 << selectionSize;
269 const int width = getMuxWidth(outputBusCount);
272 northBusPorts(outputBusCount,
"o", width));
278 {
PortPair{
"out", QPoint(120, 50)}});
284 const int appliedBusSize =
associatedComponent->getPropertyValue<
int>(
"busSize").value_or(1);
286 return appliedSelectionSize;
292 const int appliedSelectionSize =
associatedComponent->getPropertyValue<
int>(
"selectionSize").value_or(1);
294 return appliedBusSize;
313 makeDecoder(), new
ThemedSvgItem(
":/other_components/DECODER.svg"), parent, true)
324 throw std::logic_error(
"GraphicalDecoder requires a Decoder component");
343 {
PortPair{
"out", QPoint(120, 50)}});
345 return appliedSelectionSize;
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.
Decoder * getComponentAsDecoder() const
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)
int applyBusSize(int busSize)
int applySelectionSize(int selectionSize)
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)
Multiplexer * getComponentAsMultiplexer() const
void setComponent(const Component_ptr &component) override
Sets the associated logical component.
void updateLayout(int selectionSize, int busSize)
int applyBusSize(int busSize)
int applySelectionSize(int selectionSize)
static const QColor & getColor(theme::ColorToken token)
int width(RTLIL::Module *module, RTLIL::Cell *cell, RTLIL::IdString key)
std::shared_ptr< Component > Component_ptr
std::shared_ptr< Wire > Wire_ptr
std::variant< int, bool, std::string, BusValue > PropertyValue
Variant type for component properties.
std::pair< QString, QPoint > PortPair