SILICON
Loading...
Searching...
No Matches
sislVisualizer.cpp
Go to the documentation of this file.
1#include "sislVisualizer.hpp"
2#include <QEvent>
3#include <QScrollBar>
4
5#include <algorithm>
6#include <cmath>
7#include <utility>
8
9#include <QGraphicsRectItem>
10#include <QGraphicsScene>
11#include <QGraphicsSceneMouseEvent>
12#include <QGraphicsSimpleTextItem>
13#include <QGraphicsView>
14#include <QHBoxLayout>
15#include <QLabel>
16#include <QPalette>
17#include <QPainter>
18#include <QPushButton>
19#include <QTimer>
20#include <QVBoxLayout>
21#include <QWheelEvent>
22
23namespace SILICON::ui {
24namespace {
25
26namespace Layout {
27 constexpr qreal CELL_W = 27.0;
28 constexpr qreal LEFT_M = 34.0;
29 constexpr qreal ROW_H = 55.0;
30 constexpr qreal RECT_H = 33.0;
31 constexpr qreal BOX_PAD_X = 15.0;
32 constexpr qreal BOX_PAD_Y = 5.0;
33 constexpr qreal BASE_Y = 48.0;
34
35 constexpr qreal TITLE_SCALE = 1.4;
36 constexpr qreal SUB_TITLE_SCALE = 1.15;
37 constexpr qreal NORMAL_SCALE = 1.0;
38 constexpr qreal INDEX_SCALE = 0.7;
39
40 inline qreal calcBoxWidth(std::size_t bitsWidth) {
41 return std::max<qreal>(550.0, bitsWidth * CELL_W + 35.0);
42 }
43} // namespace Layout
44
45struct SliceGeometry {
46 qreal x;
47 qreal w;
48
49 static SliceGeometry calculate(std::size_t totalWidth, std::size_t msb, std::size_t lsb) {
50 return {
51 Layout::LEFT_M + (totalWidth - msb - 1) * Layout::CELL_W,
52 (msb - lsb + 1) * Layout::CELL_W
53 };
54 }
55};
56
57// --- Custom UI Elements ---
58
59class ZoomView final : public QGraphicsView {
60public:
61 using QGraphicsView::QGraphicsView;
62
63 void zoom(bool in) {
64 constexpr qreal ZOOM_IN = 1.15;
65 constexpr qreal ZOOM_OUT = 1.0 / ZOOM_IN;
66 constexpr qreal MIN_SCALE = 0.2;
67 constexpr qreal MAX_SCALE = 4.0;
68
69 const qreal factor = in ? ZOOM_IN : ZOOM_OUT;
70 const qreal nextScale = transform().m11() * factor;
71
72 if (nextScale >= MIN_SCALE && nextScale <= MAX_SCALE) {
73 scale(factor, factor);
74 }
75 }
76
77protected:
78 void wheelEvent(QWheelEvent* event) override {
79 zoom(event->angleDelta().y() > 0);
80 event->accept();
81 }
82};
83
84class FormatBox final : public QGraphicsRectItem {
85public:
86 FormatBox(const QRectF& rect, std::function<void()> onDoubleClick)
87 : QGraphicsRectItem(rect), open(std::move(onDoubleClick)) {
88 setCursor(Qt::PointingHandCursor);
89 }
90protected:
91 void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) override {
92 open();
93 event->accept();
94 }
95private:
96 std::function<void()> open;
97};
98
99
100// --- Helper Functions ---
101
102QGraphicsSimpleTextItem* addText(QGraphicsScene* scene, const QString& value, qreal x, qreal y,
103 const QColor& color, qreal scale = Layout::NORMAL_SCALE) {
104 auto* item = scene->addSimpleText(value);
105 item->setBrush(color);
106 item->setPos(x, y);
107 item->setScale(scale);
108 item->setAcceptedMouseButtons(Qt::NoButton);
109 return item;
110}
111
112void addFittedText(QGraphicsScene* scene, const QString& text, const SliceGeometry& geom, qreal yOffset,
113 const QColor& color, const QString& tooltip, qreal maxScale, bool centered) {
114 auto* item = addText(scene, text, 0, geom.x, color); // Temp X pos
115
116 const qreal scale = std::min(maxScale, (geom.w - 6.0) / std::max<qreal>(1.0, item->boundingRect().width()));
117 item->setScale(scale);
118
119 const qreal xPos = centered
120 ? geom.x + (geom.w - item->boundingRect().width() * scale) / 2.0
121 : geom.x + 3.0;
122
123 item->setPos(xPos, yOffset);
124 item->setToolTip(tooltip);
125}
126
127QString formatBinaryString(const sisl::Integer& value, std::size_t width) {
128 QString result(width + 2, QLatin1Char('0'));
129 result[0] = '0';
130 result[1] = 'b';
131 for (std::size_t i = 0; i < width; ++i) {
132 if ((value >> i) & 1) {
133 result[width + 1 - i] = '1';
134 }
135 }
136 return result;
137}
138
139QString formatSliceLabel(const sisl::FieldDescription& field, const sisl::FieldMapping& mapping) {
140 const auto& range = mapping.field_bits;
141 const QString name = QString::fromStdString(field.name);
142
143 const bool coversEntireField = (range.lsb == 0 && range.msb + 1 == field.width);
144 if (coversEntireField) return name;
145
146 if (range.msb == range.lsb) {
147 return QStringLiteral("%1[%2]").arg(name).arg(range.msb);
148 }
149 return QStringLiteral("%1[%2:%3]").arg(name).arg(range.msb).arg(range.lsb);
150}
151
152std::size_t getEffectiveWidth(const sisl::FormatDescription& format) {
153 if (format.width) return *format.width;
154
155 std::size_t width = 0;
156 for (const auto& field : format.fields) {
157 for (const auto& mapping : field.mappings) {
158 width = std::max(width, mapping.instruction_bits.msb + 1);
159 }
160 }
161 return width;
162}
163
164const sisl::FixedFieldDescription* findFixedField(const std::string& name, const std::vector<sisl::FixedFieldDescription>& fixedFields) {
165 auto it = std::find_if(fixedFields.begin(), fixedFields.end(), [&](const auto& item) {
166 return item.name == name;
167 });
168 return it != fixedFields.end() ? &(*it) : nullptr;
169}
170
171} // namespace
172
174{
175 static_cast<ZoomView*>(view)->zoom(in);
176}
177
178// --- Class Implementation ---
179
180ThemeColors::ThemeColors(const QPalette& pal)
181 : ink(pal.color(QPalette::Text)),
182 base(pal.color(QPalette::Base)),
183 accent(pal.color(QPalette::Highlight)),
184 shaded(pal.color(QPalette::AlternateBase)),
185 highlightedText(pal.color(QPalette::HighlightedText)),
186 mid(pal.color(QPalette::Mid)) {}
187
188
189SislVisualizer::SislVisualizer(QWidget* parent) : QWidget(parent) {
190 setupUI();
191}
192
194 auto* layout = new QVBoxLayout(this);
195 auto* topBar = new QHBoxLayout();
196
197 backButton = new QPushButton(tr("← Formats"), this);
198 hintLabel = new QLabel(tr("Double-click a format to see its instructions"), this);
199
200 topBar->addWidget(backButton);
201 topBar->addStretch();
202 topBar->addWidget(hintLabel);
203 layout->addLayout(topBar);
204
205 scene = new QGraphicsScene(this);
206 view = new ZoomView(scene, this);
207 view->setRenderHint(QPainter::Antialiasing);
208 view->setAlignment(Qt::AlignLeft | Qt::AlignTop);
209 view->setDragMode(QGraphicsView::ScrollHandDrag);
210 view->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
211 layout->addWidget(view);
212
213 callbacks.connect(backButton, &QPushButton::clicked, [this] { showOverview(); });
214 hide();
215}
216
217void SislVisualizer::setDescription(sisl::IsaDescription value) {
218 description = std::move(value);
219 showOverview();
220}
221
222void SislVisualizer::resetScene(bool isOverview) {
223 scene->clear();
224 backButton->setVisible(!isOverview);
225 hintLabel->setVisible(isOverview);
226}
227
229 scene->setSceneRect(scene->itemsBoundingRect().adjusted(-25, -20, 45, 25));
230 view->resetTransform();
231}
232
233// --- Overview Routines ---
234
236 overview = true;
237 resetScene(true);
238 ThemeColors theme(palette());
239
241
242 qreal y = Layout::BASE_Y;
246
248}
249
251 const QString title = tr("%1 · Instruction formats").arg(QString::fromStdString(description.name));
252 addText(scene, title, Layout::LEFT_M, 0, palette().color(QPalette::Text), Layout::TITLE_SCALE);
253}
254
256 for (const auto& format : description.formats) {
257 const auto width = getEffectiveWidth(format);
258
259 QString title = QString::fromStdString(format.name);
260 if (format.parent) title += tr(" : %1").arg(QString::fromStdString(*format.parent));
261 title += format.width ? tr(" · %1 bits").arg(static_cast<qulonglong>(*format.width)) : tr(" · width unspecified");
262
263 auto* box = new FormatBox(
264 QRectF(Layout::LEFT_M - Layout::BOX_PAD_X, y - Layout::BOX_PAD_Y, Layout::calcBoxWidth(width), Layout::ROW_H + 25),
265 callbacks.guard([this, name = format.name] { QTimer::singleShot(0, callbacks.context(), [this, name] { showFormat(name); }); })
266 );
267
268 box->setPen(QPen(theme.mid));
269 scene->addItem(box);
270
271 addText(scene, title, Layout::LEFT_M, y, theme.ink);
272 if (width > 0) {
273 drawLayout(format.fields, width, {}, y + 22, theme);
274 }
275
276 y += Layout::ROW_H + 42;
277 }
278}
279
281 const bool hasStandalone = std::any_of(description.instructions.begin(), description.instructions.end(),
282 [](const auto& item) { return !item.format; });
283 if (!hasStandalone) return;
284
285 auto* box = new FormatBox(QRectF(Layout::LEFT_M - Layout::BOX_PAD_X, y, Layout::calcBoxWidth(0), 48), callbacks.guard([this] {
286 QTimer::singleShot(0, callbacks.context(), [this] { showFormat(std::nullopt); });
287 }));
288
289 box->setPen(QPen(theme.mid));
290 scene->addItem(box);
291
292 addText(scene, tr("Standalone instructions · double-click"), Layout::LEFT_M, y + 12, theme.ink);
293 y += 70;
294}
295
297 y += 15;
298 addText(scene, tr("Enum values"), Layout::LEFT_M, y, theme.ink, 1.25);
299 y += 35;
300
301 for (const auto& enumeration : description.enums) {
302 const QString title = QString::fromStdString(enumeration.name) + tr(" · %1 bits").arg(static_cast<qulonglong>(enumeration.width));
303 addText(scene, title, Layout::LEFT_M, y, theme.ink);
304 y += 25;
305
306 for (const auto& [name, value] : enumeration.members) {
307 const QString itemText = QStringLiteral("%1 = %2").arg(QString::fromStdString(name), formatBinaryString(value, enumeration.width));
308 addText(scene, itemText, Layout::LEFT_M + 20, y, theme.ink);
309 y += 22;
310 }
311 y += 15;
312 }
313}
314
315// --- Format Detail Routines ---
316
317void SislVisualizer::showFormat(const std::optional<std::string>& formatName) {
318 overview = false;
319 currentFormat = formatName;
320 resetScene(false);
321 ThemeColors theme(palette());
322
323 const QString pageTitle = formatName ? QString::fromStdString(*formatName) : tr("Standalone instructions");
324 addText(scene, pageTitle, Layout::LEFT_M, 0, theme.ink, Layout::TITLE_SCALE);
325
326 qreal y = 50;
327 if (formatName) {
328 drawFormatHeader(*formatName, theme, y);
329 }
330
331 drawInstructionsForFormat(formatName, theme, y);
333}
334
336{
337 QWidget::changeEvent(event);
338 if (event->type() != QEvent::PaletteChange || !scene || scene->items().isEmpty())
339 return;
340 // Scene brushes are snapshots of QPalette. Rebuild the active view while retaining
341 // navigation, zoom and scroll position; scene items do not receive PaletteChange.
342 const auto transform = view->transform();
343 const int horizontal = view->horizontalScrollBar()->value();
344 const int vertical = view->verticalScrollBar()->value();
345 if (overview)
346 showOverview();
347 else
349 view->setTransform(transform);
350 view->horizontalScrollBar()->setValue(horizontal);
351 view->verticalScrollBar()->setValue(vertical);
352}
353
354void SislVisualizer::drawFormatHeader(const std::string& formatName, const ThemeColors& theme, qreal& y) {
355 auto format = std::find_if(description.formats.begin(), description.formats.end(),
356 [&](const auto& item) { return item.name == formatName; });
357
358 if (format == description.formats.end()) return;
359
360 const auto width = getEffectiveWidth(*format);
361 auto* box = scene->addRect(Layout::LEFT_M - Layout::BOX_PAD_X, y - Layout::BOX_PAD_Y,
362 Layout::calcBoxWidth(width), 83, QPen(theme.mid));
363 box->setAcceptedMouseButtons(Qt::NoButton);
364
365 const QString inheritance = format->parent ? tr(" · inherits %1").arg(QString::fromStdString(*format->parent)) : QString();
366 addText(scene, tr("Format structure%1").arg(inheritance), Layout::LEFT_M, y, theme.ink);
367
368 if (width > 0) {
369 drawLayout(format->fields, width, {}, y + 22, theme);
370 }
371 y += 115;
372}
373
374void SislVisualizer::drawInstructionsForFormat(const std::optional<std::string>& formatName, const ThemeColors& theme, qreal& y) {
375 addText(scene, tr("Instructions"), Layout::LEFT_M, y, theme.ink, Layout::SUB_TITLE_SCALE);
376 y += 38;
377
378 bool hasAnyInstructions = false;
379
380 for (const auto& instruction : description.instructions) {
381 if (instruction.format != formatName) continue;
382 hasAnyInstructions = true;
383
384 const QString instName = QString::fromStdString(instruction.assembly.empty() ? instruction.name : instruction.assembly);
385 addText(scene, instName, Layout::LEFT_M, y, theme.ink, 1.1);
386
387 drawLayout(instruction.fields, instruction.width, instruction.fixed_fields, y + 22, theme);
388 y += 105;
389
390 drawAliases(instruction, theme, y);
391 y += 10;
392 }
393
394 if (!hasAnyInstructions) {
395 addText(scene, tr("No instructions use this format."), Layout::LEFT_M, y, theme.ink);
396 }
397}
398
399void SislVisualizer::drawAliases(const sisl::InstructionDescription& inst, const ThemeColors& theme, qreal& y) {
400 for (const auto& alias : description.aliases) {
401 if (alias.target != inst.name) continue;
402
403 auto mergedFixedFields = inst.fixed_fields;
404 mergedFixedFields.insert(mergedFixedFields.end(), alias.additional_fixed_fields.begin(), alias.additional_fixed_fields.end());
405
406 const QString aliasName = QString::fromStdString(alias.assembly.empty() ? alias.name : alias.assembly);
407 const QString title = tr("Alias: %1 → %2").arg(aliasName, QString::fromStdString(alias.target));
408
409 addText(scene, title, Layout::LEFT_M + 16, y, theme.ink);
410 drawLayout(inst.fields, inst.width, mergedFixedFields, y + 22, theme);
411 y += 100;
412 }
413}
414
415// --- Layout Engine ---
416
417void SislVisualizer::drawLayout(const std::vector<sisl::FieldDescription>& fields,
418 std::size_t width,
419 const std::vector<sisl::FixedFieldDescription>& fixedFields,
420 qreal y,
421 const ThemeColors& theme) {
422 std::vector<bool> indexes(width, false);
423 std::vector<bool> occupied(width, false);
424
425 if (width > 0) {
426 indexes.front() = true;
427 indexes.back() = true;
428 }
429
430 const qreal top = y + 17;
431
432 drawFieldSlices(fields, width, fixedFields, top, theme, occupied, indexes);
433 drawUnoccupiedSlices(width, top, theme, occupied, indexes);
434 drawAxisIndices(width, y, theme, indexes);
435}
436
437void SislVisualizer::drawFieldSlices(const std::vector<sisl::FieldDescription>& fields,
438 std::size_t width,
439 const std::vector<sisl::FixedFieldDescription>& fixedFields,
440 qreal top,
441 const ThemeColors& theme,
442 std::vector<bool>& occupied,
443 std::vector<bool>& indexes) {
444 for (const auto& field : fields) {
445 const sisl::FixedFieldDescription* fixedInfo = findFixedField(field.name, fixedFields);
446 const bool isFixed = (fixedInfo != nullptr);
447
448 for (const auto& mapping : field.mappings) {
449 const auto high = mapping.instruction_bits.msb;
450 const auto low = mapping.instruction_bits.lsb;
451
452 if (high >= width || high < low) continue;
453
454 indexes[high] = true;
455 indexes[low] = true;
456 for (auto bit = low; bit <= high; ++bit) occupied[bit] = true;
457
458 const SliceGeometry geom = SliceGeometry::calculate(width, high, low);
459
460 auto* rect = scene->addRect(geom.x, top, geom.w, Layout::RECT_H, QPen(theme.ink), QBrush(isFixed ? theme.accent : theme.shaded));
461 rect->setAcceptedMouseButtons(Qt::NoButton);
462
463 QString tooltip = QStringLiteral("%1 : %2 bits").arg(QString::fromStdString(field.name)).arg(field.width);
464 if (!field.enumeration.empty()) tooltip += QStringLiteral(" (%1)").arg(QString::fromStdString(field.enumeration));
465 if (isFixed && fixedInfo->enum_member) tooltip += QStringLiteral(" = %1").arg(QString::fromStdString(*fixedInfo->enum_member));
466
467 rect->setToolTip(tooltip);
468
469 const QColor textColor = isFixed ? theme.highlightedText : theme.ink;
470
471 if (!isFixed) {
472 addFittedText(scene, formatSliceLabel(field, mapping), geom, top + 5, textColor, tooltip, 1.0, false);
473 } else {
474 const auto sliceWidth = high - low + 1;
475 const auto sliceVal = (fixedInfo->value >> mapping.field_bits.lsb) & ((sisl::Integer{1} << sliceWidth) - 1);
476 const QString encoded = formatBinaryString(sliceVal, sliceWidth);
477
478 const QString bitString = (geom.w < 70) ? encoded.mid(2) : encoded; // Trim "0b" prefix if too small
479
480 addFittedText(scene, bitString, geom, top + 1, textColor, tooltip, 0.8, true);
481 addFittedText(scene, formatSliceLabel(field, mapping), geom, top + 18, textColor, tooltip, 0.55, true);
482 }
483 }
484 }
485}
486
488 qreal top,
489 const ThemeColors& theme,
490 std::vector<bool>& occupied,
491 std::vector<bool>& indexes) {
492 for (std::size_t bit = 0; bit < width;) {
493 if (occupied[bit]) {
494 ++bit;
495 continue;
496 }
497
498 const auto low = bit;
499 while (bit < width && !occupied[bit]) {
500 ++bit;
501 }
502 const auto high = bit - 1;
503
504 indexes[high] = true;
505 indexes[low] = true;
506
507 const SliceGeometry geom = SliceGeometry::calculate(width, high, low);
508
509 auto* rect = scene->addRect(geom.x, top, geom.w, Layout::RECT_H, QPen(theme.ink), QBrush(theme.base));
510 rect->setAcceptedMouseButtons(Qt::NoButton);
511
512 auto* dash = addText(scene, QStringLiteral("—"), 0, top + 5, theme.ink);
513 dash->setPos(geom.x + (geom.w - dash->boundingRect().width()) / 2.0, top + 5);
514 }
515}
516
517void SislVisualizer::drawAxisIndices(std::size_t width, qreal y, const ThemeColors& theme, const std::vector<bool>& indexes) {
518 for (std::size_t bit = 0; bit < width; ++bit) {
519 if (!indexes[bit]) continue;
520
521 auto* index = addText(scene, QString::number(bit), 0, y, theme.ink, Layout::INDEX_SCALE);
522 const qreal targetX = Layout::LEFT_M + (width - bit - 1) * Layout::CELL_W;
523
524 index->setPos(targetX + (Layout::CELL_W - index->boundingRect().width() * Layout::INDEX_SCALE) / 2.0, y);
525 }
526}
527
528} // namespace SILICON::ui
void changeEvent(QEvent *event) override
void drawAllEnums(const ThemeColors &theme, qreal &y)
void showFormat(const std::optional< std::string > &formatName)
void drawAxisIndices(std::size_t width, qreal y, const ThemeColors &theme, const std::vector< bool > &indexes)
void drawUnoccupiedSlices(std::size_t width, qreal top, const ThemeColors &theme, std::vector< bool > &occupied, std::vector< bool > &indexes)
void drawInstructionsForFormat(const std::optional< std::string > &formatName, const ThemeColors &theme, qreal &y)
void drawStandaloneInstructionsBox(const ThemeColors &theme, qreal &y)
void drawAliases(const sisl::InstructionDescription &inst, const ThemeColors &theme, qreal &y)
SislVisualizer(QWidget *parent=nullptr)
void resetScene(bool showHint)
sisl::IsaDescription description
void drawFieldSlices(const std::vector< sisl::FieldDescription > &fields, std::size_t width, const std::vector< sisl::FixedFieldDescription > &fixedFields, qreal top, const ThemeColors &theme, std::vector< bool > &occupied, std::vector< bool > &indexes)
void drawFormatHeader(const std::string &formatName, const ThemeColors &theme, qreal &y)
void setDescription(sisl::IsaDescription description)
void drawAllFormatBoxes(const ThemeColors &theme, qreal &y)
std::optional< std::string > currentFormat
void drawLayout(const std::vector< sisl::FieldDescription > &fields, std::size_t width, const std::vector< sisl::FixedFieldDescription > &fixedFields, qreal y, const ThemeColors &theme)
int width(RTLIL::Module *module, RTLIL::Cell *cell, RTLIL::IdString key)
ThemeColors(const QPalette &pal)