9#include <QGraphicsRectItem>
10#include <QGraphicsScene>
11#include <QGraphicsSceneMouseEvent>
12#include <QGraphicsSimpleTextItem>
13#include <QGraphicsView>
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;
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;
40 inline qreal calcBoxWidth(std::size_t bitsWidth) {
41 return std::max<qreal>(550.0, bitsWidth * CELL_W + 35.0);
49 static SliceGeometry calculate(std::size_t totalWidth, std::size_t msb, std::size_t lsb) {
51 Layout::LEFT_M + (totalWidth - msb - 1) * Layout::CELL_W,
52 (msb - lsb + 1) * Layout::CELL_W
59class ZoomView final :
public QGraphicsView {
61 using QGraphicsView::QGraphicsView;
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;
69 const qreal factor = in ? ZOOM_IN : ZOOM_OUT;
70 const qreal nextScale = transform().m11() * factor;
72 if (nextScale >= MIN_SCALE && nextScale <= MAX_SCALE) {
73 scale(factor, factor);
78 void wheelEvent(QWheelEvent* event)
override {
79 zoom(event->angleDelta().y() > 0);
84class FormatBox final :
public QGraphicsRectItem {
86 FormatBox(
const QRectF& rect, std::function<
void()> onDoubleClick)
87 : QGraphicsRectItem(rect), open(std::move(onDoubleClick)) {
88 setCursor(Qt::PointingHandCursor);
91 void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
override {
96 std::function<void()> open;
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);
107 item->setScale(scale);
108 item->setAcceptedMouseButtons(Qt::NoButton);
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);
116 const qreal scale = std::min(maxScale, (geom.w - 6.0) / std::max<qreal>(1.0, item->boundingRect().width()));
117 item->setScale(scale);
119 const qreal xPos = centered
120 ? geom.x + (geom.w - item->boundingRect().
width() * scale) / 2.0
123 item->setPos(xPos, yOffset);
124 item->setToolTip(tooltip);
127QString formatBinaryString(
const sisl::Integer& value, std::size_t width) {
128 QString result(width + 2, QLatin1Char(
'0'));
131 for (std::size_t i = 0; i <
width; ++i) {
132 if ((value >> i) & 1) {
133 result[
width + 1 - i] =
'1';
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);
143 const bool coversEntireField = (range.lsb == 0 && range.msb + 1 == field.width);
144 if (coversEntireField)
return name;
146 if (range.msb == range.lsb) {
147 return QStringLiteral(
"%1[%2]").arg(name).arg(range.msb);
149 return QStringLiteral(
"%1[%2:%3]").arg(name).arg(range.msb).arg(range.lsb);
152std::size_t getEffectiveWidth(
const sisl::FormatDescription& format) {
153 if (format.width)
return *format.width;
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);
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;
168 return it != fixedFields.end() ? &(*it) :
nullptr;
175 static_cast<ZoomView*
>(
view)->
zoom(in);
181 :
ink(pal.color(QPalette::Text)),
182 base(pal.color(QPalette::Base)),
183 accent(pal.color(QPalette::Highlight)),
184 shaded(pal.color(QPalette::AlternateBase)),
186 mid(pal.color(QPalette::Mid)) {}
194 auto* layout =
new QVBoxLayout(
this);
195 auto* topBar =
new QHBoxLayout();
197 backButton =
new QPushButton(tr(
"← Formats"),
this);
198 hintLabel =
new QLabel(tr(
"Double-click a format to see its instructions"),
this);
201 topBar->addStretch();
203 layout->addLayout(topBar);
205 scene =
new QGraphicsScene(
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);
229 scene->setSceneRect(
scene->itemsBoundingRect().adjusted(-25, -20, 45, 25));
230 view->resetTransform();
242 qreal y = Layout::BASE_Y;
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);
257 const auto width = getEffectiveWidth(format);
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");
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); }); })
268 box->setPen(QPen(
theme.mid));
271 addText(
scene, title, Layout::LEFT_M, y,
theme.ink);
276 y += Layout::ROW_H + 42;
282 [](
const auto& item) { return !item.format; });
283 if (!hasStandalone)
return;
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); });
289 box->setPen(QPen(
theme.mid));
292 addText(scene, tr(
"Standalone instructions · double-click"), Layout::LEFT_M, y + 12,
theme.ink);
298 addText(
scene, tr(
"Enum values"), Layout::LEFT_M, y,
theme.ink, 1.25);
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);
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);
323 const QString pageTitle = formatName ? QString::fromStdString(*formatName) : tr(
"Standalone instructions");
324 addText(
scene, pageTitle, Layout::LEFT_M, 0,
theme.ink, Layout::TITLE_SCALE);
337 QWidget::changeEvent(event);
338 if (event->type() != QEvent::PaletteChange || !
scene ||
scene->items().isEmpty())
342 const auto transform =
view->transform();
343 const int horizontal =
view->horizontalScrollBar()->value();
344 const int vertical =
view->verticalScrollBar()->value();
349 view->setTransform(transform);
350 view->horizontalScrollBar()->setValue(horizontal);
351 view->verticalScrollBar()->setValue(vertical);
356 [&](
const auto& item) { return item.name == formatName; });
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);
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);
375 addText(
scene, tr(
"Instructions"), Layout::LEFT_M, y,
theme.ink, Layout::SUB_TITLE_SCALE);
378 bool hasAnyInstructions =
false;
380 for (
const auto& instruction :
description.instructions) {
381 if (instruction.format != formatName)
continue;
382 hasAnyInstructions =
true;
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);
387 drawLayout(instruction.fields, instruction.width, instruction.fixed_fields, y + 22,
theme);
394 if (!hasAnyInstructions) {
395 addText(
scene, tr(
"No instructions use this format."), Layout::LEFT_M, y,
theme.ink);
401 if (alias.target != inst.name)
continue;
403 auto mergedFixedFields = inst.fixed_fields;
404 mergedFixedFields.insert(mergedFixedFields.end(), alias.additional_fixed_fields.begin(), alias.additional_fixed_fields.end());
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));
409 addText(
scene, title, Layout::LEFT_M + 16, y,
theme.ink);
419 const std::vector<sisl::FixedFieldDescription>& fixedFields,
422 std::vector<bool> indexes(width,
false);
423 std::vector<bool> occupied(width,
false);
426 indexes.front() =
true;
427 indexes.back() =
true;
430 const qreal top = y + 17;
439 const std::vector<sisl::FixedFieldDescription>& fixedFields,
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);
448 for (
const auto& mapping : field.mappings) {
449 const auto high = mapping.instruction_bits.msb;
450 const auto low = mapping.instruction_bits.lsb;
452 if (high >= width || high < low)
continue;
454 indexes[high] =
true;
456 for (
auto bit = low; bit <= high; ++bit) occupied[bit] =
true;
458 const SliceGeometry geom = SliceGeometry::calculate(width, high, low);
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);
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));
467 rect->setToolTip(tooltip);
469 const QColor textColor = isFixed ?
theme.highlightedText :
theme.ink;
472 addFittedText(
scene, formatSliceLabel(field, mapping), geom, top + 5, textColor, tooltip, 1.0,
false);
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);
478 const QString bitString = (geom.w < 70) ? encoded.mid(2) : encoded;
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);
490 std::vector<bool>& occupied,
491 std::vector<bool>& indexes) {
492 for (std::size_t bit = 0; bit < width;) {
498 const auto low = bit;
499 while (bit < width && !occupied[bit]) {
502 const auto high = bit - 1;
504 indexes[high] =
true;
507 const SliceGeometry geom = SliceGeometry::calculate(width, high, low);
509 auto* rect =
scene->addRect(geom.x, top, geom.w, Layout::RECT_H, QPen(
theme.ink), QBrush(
theme.base));
510 rect->setAcceptedMouseButtons(Qt::NoButton);
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);
518 for (std::size_t bit = 0; bit < width; ++bit) {
519 if (!indexes[bit])
continue;
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;
524 index->setPos(targetX + (Layout::CELL_W - index->boundingRect().width() * Layout::INDEX_SCALE) / 2.0, y);
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)