HAL  v4.5.0-83-g30c8f0afc
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
logic_evaluator_truthtable.cpp
Go to the documentation of this file.
2 #include "hal_core/netlist/net.h"
3 
4 #include <QGridLayout>
5 #include <QTableView>
6 #include <QMenuBar>
7 #include <QActionGroup>
8 #include <QHeaderView>
9 #include <QDialogButtonBox>
10 #include <QLabel>
11 
12 namespace hal {
14  : mRows(nrows)
15  {
16  mArray = new int[mRows];
17  for (int i=0; i<mRows; i++)
18  {
19  mArray[i] = values.at(i);
20  }
21  }
22 
24  {
25  for (int irow : sortRows)
26  {
27  if (mArray[irow] < other.mArray[irow]) return true;
28  if (mArray[irow] > other.mArray[irow]) return false;
29  }
30  return false; // equal
31  }
32 
34  {
35  delete [] mArray;
36  }
37 
39  {
40  return mArray[irow];
41  }
42 
43  //-----------------------------
45  : mDisplayFormat(MAXFORMAT), // will be set by setDisplayFormat
46  mInputList(inpList), mOutputList(outList), mInputSize(inpList.size()), mOutputSize(outList.size())
47  {;}
48 
50  {
51  for (LogicEvaluatorTruthtableColumn* letc : mColumnList)
52  delete letc;
53  }
54 
56  {
57  mColumnList.append(letc);
58  }
59 
61  {
62  QList<int> sortRows;
63  sortRows.append(irow);
64  std::sort(mColumnList.begin(),mColumnList.end(),[sortRows](const LogicEvaluatorTruthtableColumn* a, const LogicEvaluatorTruthtableColumn* b){return a->lessThan(*b,sortRows); } );
66  }
67 
69  {
70  if (sortRows.isEmpty()) return;
71  std::sort(mColumnList.begin(),mColumnList.end(),[sortRows](const LogicEvaluatorTruthtableColumn* a, const LogicEvaluatorTruthtableColumn* b){return a->lessThan(*b,sortRows); } );
73  }
74 
76  {
77  int val = mColumnList.at(index.column())->data(index.row());
78 
79  switch (role)
80  {
81  case Qt::DisplayRole:
82  switch (mDisplayFormat) {
83  case ZeroOne:
84  return val;
85  case LowHigh:
86  return val ? "H" : "L";
87  case BlueRed:
88  return QChar(0x2588);
89  }
90  break;
91  case Qt::ForegroundRole:
92  if (mDisplayFormat == BlueRed)
93  return val ? QColor("#FF0000") : QColor("#00A0FF");
94  break;
95  case Qt::BackgroundRole:
96  if (index.row() >= mInputSize)
97  return QColor("#080930");
98  return QColor("302A12");
99  break;
102  default:
103  break;
104  }
105  return QVariant();
106  }
107 
108  QVariant LogicEvaluatorTruthtableModel::headerData(int section, Qt::Orientation orientation, int role) const
109  {
110  if (role!=Qt::DisplayRole) return QAbstractTableModel::headerData(section, orientation, role);
111  if (orientation == Qt::Horizontal)
112  return QString::number(section, 16);
113  if (section < mInputSize)
114  return QString::fromStdString(mInputList.at(section)->get_name());
115  return QString::fromStdString(mOutputList.at(section-mInputSize)->get_name());
116  }
117 
119  {
120  Q_UNUSED(parent);
121  return mInputSize + mOutputSize;
122  }
123 
125  {
126  Q_UNUSED(parent);
127  return (1 << mInputList.size());
128  }
129 
130  //--------------------------------
132  : QDialog(parent), mNets(nets)
133  {
134  QVBoxLayout* layout = new QVBoxLayout(this);
135  const char* ordinal[] = { "st", "nd", "th"};
136  for (int i=0; i<5; i++)
137  {
138  if (i) layout->addStretch();
139  layout->addWidget(new QLabel(QString("%1%2 sort key").arg(i+1).arg(i<3?ordinal[i]:ordinal[2]),this));
140  mSortKey[i] = new QComboBox(this);
141  mSortKey[i]->addItem("--not used--", (const void*) nullptr);
142  for (const Net* n : mNets)
143  mSortKey[i]->addItem(QString::fromStdString(n->get_name()),(const void*)n);
144  layout->addWidget(mSortKey[i]);
145  }
150  layout->addWidget(bbox);
151  }
152 
154  {
155  QList<int> retval;
156  QSet<const Net*> selected;
157  for (int i=0; i<5; i++)
158  {
159  int inx = mSortKey[i]->currentIndex();
160  if (!inx) break;
161  --inx; // skip not used entry
162  const Net* n = mNets.at(inx);
163  if (!selected.contains(n))
164  {
165  retval.append(inx);
166  selected.insert(n);
167  }
168  }
169  return retval;
170  }
171 
172  //--------------------------------
174  : mModel(model), mColumnDubbleClicked(-1)
175  {
176  setWindowTitle("Truth Table from Logic Evaluator");
177  QGridLayout* layout = new QGridLayout(this);
178  QMenuBar* menuBar = new QMenuBar(this);
179  QMenu* displForm = menuBar->addMenu("Format");
180 
181  const char* displayFormatLabel[] = {"0 / 1", "L / H", "blue / red"};
182  QActionGroup* actGroup = new QActionGroup(this);
183  for (int i=0; i< LogicEvaluatorTruthtableModel::MAXFORMAT; i++)
184  {
185  mActionDisplayFormat[i] = displForm->addAction(displayFormatLabel[i]);
186  mActionDisplayFormat[i]->setCheckable(true);
187  actGroup->addAction(mActionDisplayFormat[i]);
188  }
189  connect(actGroup, &QActionGroup::triggered, this, &LogicEvaluatorTruthtable::handleDisplayFormatChanged);
190  mActionDisplayFormat[LogicEvaluatorTruthtableModel::ZeroOne]->setChecked(true);
191  handleDisplayFormatChanged(mActionDisplayFormat[LogicEvaluatorTruthtableModel::ZeroOne]);
192 
193  QMenu* sortMenu = menuBar->addMenu("Sort");
194  QAction* actSort = sortMenu->addAction("Sort");
195  connect(actSort, &QAction::triggered, this, &LogicEvaluatorTruthtable::handleSortTriggered);
196 
197  QTableView* view = new QTableView(this);
198  view->setModel(mModel);
199  for (int icol=0; icol<mModel->columnCount(); icol++)
200  view->setColumnWidth(icol, 32);
202  connect(view->horizontalHeader(), &QHeaderView::sectionDoubleClicked, this, &LogicEvaluatorTruthtable::handleColumnDubbleClicked);
203  layout->addWidget(view);
204  layout->setMenuBar(menuBar);
205  }
206 
208  {
209  if (df == mDisplayFormat) return;
210  mDisplayFormat = df;
212  }
213 
214  void LogicEvaluatorTruthtable::handleSortTriggered()
215  {
216  QList<const Net*> nets = mModel->getNets();
217  LogicEvaluatorTruthtableSort lets(nets,this);
218  if (lets.exec() == QDialog::Accepted)
219  {
220  mModel->sortModelRows(lets.sortOrder());
221  }
222  }
223 
224  void LogicEvaluatorTruthtable::handleColumnDubbleClicked(int icol)
225  {
226  mColumnDubbleClicked = icol;
227  accept();
228  }
229 
231  {
232  QMap<const Net*,int> retval;
233  if (icol < 0 || icol >= mColumnList.size()) return retval;
234  LogicEvaluatorTruthtableColumn* letc = mColumnList.at(icol);
235  int irow = 0;
236  for (const Net* n : mInputList)
237  retval[n] = letc->data(irow++);
238  return retval;
239  }
240 
242  {
243  return mModel->selectedColumn(mColumnDubbleClicked);
244  }
245 
246  void LogicEvaluatorTruthtable::handleDisplayFormatChanged(QAction* act)
247  {
248  for (int i=0; i<LogicEvaluatorTruthtableModel::MAXFORMAT; i++)
249  if (mActionDisplayFormat[i] == act)
250  {
252  break;
253  }
254  }
255 }
u32 size
LogicEvaluatorTruthtableColumn(int nrows, QList< int > values)
bool lessThan(const LogicEvaluatorTruthtableColumn &other, const QList< int > &sortRows) const
LogicEvaluatorTruthtable(LogicEvaluatorTruthtableModel *model, QWidget *parent=nullptr)
QMap< const Net *, int > selectedColumn() const
int rowCount(const QModelIndex &parent=QModelIndex()) const override
LogicEvaluatorTruthtableModel(const QList< const Net * > &inpList, const QList< const Net * > &outList, QObject *parent=nullptr)
void sortModelRows(const QList< int > &sortRows)
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
QMap< const Net *, int > selectedColumn(int icol) const
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
void addColumn(LogicEvaluatorTruthtableColumn *letc)
LogicEvaluatorTruthtableSort(QList< const Net * > &nets, QWidget *parent=nullptr)
Definition: net.h:58
Definition: defines.h:45
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector< int > &roles)
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
void setCheckable(bool)
void setChecked(bool)
void triggered(bool checked)
QAction * addAction(QAction *action)
void triggered(QAction *action)
void addItem(const QString &text, const QVariant &userData)
virtual void accept()
virtual void reject()
void sectionClicked(int logicalIndex)
void sectionDoubleClicked(int logicalIndex)
void addWidget(QWidget *w)
void setMenuBar(QWidget *widget)
void append(const T &value)
const T & at(int i) const const
bool isEmpty() const const
QAction * addAction(const QString &text)
QAction * addMenu(QMenu *menu)
int column() const const
int row() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
bool contains(const T &value) const const
QSet::iterator insert(const T &value)
QString fromStdString(const std::string &str)
QString number(int n, int base)
AlignHCenter
DisplayRole
Orientation
QHeaderView * horizontalHeader() const const
void setColumnWidth(int column, int width)
virtual void setModel(QAbstractItemModel *model) override
QHeaderView * verticalHeader() const const
QLayout * layout() const const
void setSizePolicy(QSizePolicy)
void setWindowTitle(const QString &)