HAL
lut_table_model.cpp
Go to the documentation of this file.
1 
3 
5 
6 #include <algorithm>
7 #include <variant>
8 
9 namespace hal
10 {
11 
13  {
14  LutEntry emptyEntry;
15  emptyEntry.output = "No LUT gate selected yet...";
16  mLutEntries.append(emptyEntry);
17  mOutputPin = "-";
18  }
19 
20  int LUTTableModel::columnCount(const QModelIndex& parent) const
21  {
22  Q_UNUSED(parent)
23  return (mInputPins.size() + 1);
24  }
25 
26  int LUTTableModel::rowCount(const QModelIndex& parent) const
27  {
28  Q_UNUSED(parent)
29  return mLutEntries.size();
30  }
31 
32  QVariant LUTTableModel::data(const QModelIndex& index, int role) const
33  {
34  if (role == Qt::DisplayRole)
35  {
36  if (index.column() < mInputPins.size())
37  {
38  return mLutEntries[index.row()].inputBits[index.column()];
39  }
40  else
41  {
42  return mLutEntries[index.row()].output;
43  }
44  }
45 
46  else if (role == Qt::TextAlignmentRole)
47  {
48  return Qt::AlignCenter;
49  }
50 
51  return QVariant();
52  }
53 
54  QVariant LUTTableModel::headerData(int section, Qt::Orientation orientation, int role) const
55  {
56  if (role != Qt::DisplayRole)
57  return QVariant();
58  if (orientation == Qt::Horizontal)
59  {
60  if (section < mInputPins.size())
61  return mInputPins[section];
62  else
63  return mOutputPin;
64  }
65 
66  return QVariant();
67  }
68 
69  bool LUTTableModel::setData(const QModelIndex& index, const QVariant& value, int role)
70  {
71  // TODO
72  Q_UNUSED(index)
73  Q_UNUSED(value)
74  Q_UNUSED(role)
75  return false;
76  }
77 
78  void LUTTableModel::setBooleanFunction(const BooleanFunction lutFunction, const QString outputPinName)
79  {
81  mOutputPin = outputPinName;
82 
83  // Collect the variables as headers
84  mInputPins.clear();
85  std::vector<std::string> bfVariables = utils::to_vector(lutFunction.get_variable_names());
86  std::sort(bfVariables.begin(), bfVariables.end(), std::greater<>());
87  for (std::string v : bfVariables)
88  {
89  mInputPins.append(QString::fromStdString(v));
90  }
91  std::reverse(bfVariables.begin(), bfVariables.end());
92 
93  auto ttVariant = lutFunction.compute_truth_table(bfVariables, false);
94  if (ttVariant.is_ok())
95  {
96  // can only deal with single-bit Boolean functions, but that should be the case for LUTs anyway
97  std::vector<BooleanFunction::Value> truthTable = ttVariant.get().at(0);
98  mLutEntries.clear();
99  for (u32 truthTableIdx = 0; truthTableIdx < truthTable.size(); truthTableIdx++)
100  {
101  LutEntry entry;
102  // Get input bits
103  for (int i = 0; i < mInputPins.size(); i++)
104  {
105  u32 shift = mInputPins.size() - i - 1;
106  u8 inputBit = u8((truthTableIdx >> shift) & 1);
107  entry.inputBits.append(inputBit);
108  }
109  // Get output bit
110  BooleanFunction::Value val = truthTable[truthTableIdx];
111  if (val == BooleanFunction::Value::ZERO)
112  entry.output = "0";
113  else if (val == BooleanFunction::Value::ONE)
114  entry.output = "1";
115  else if (val == BooleanFunction::Value::Z)
116  entry.output = "Z";
117  else
118  entry.output = "X";
119  mLutEntries.append(entry);
120  }
122  }
123  }
124 
125 } // namespace hal
std::set< std::string > get_variable_names() const
Result< std::vector< std::vector< Value > > > compute_truth_table(const std::vector< std::string > &ordered_variables={}, bool remove_unknown_variables=false) const
Value
represents the type of the node
bool setData(const QModelIndex &index, const QVariant &value, int role) override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
QVariant data(const QModelIndex &index, int role) const override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
void setBooleanFunction(const BooleanFunction lutFunction, const QString outputPinName)
LUTTableModel(QObject *parent=nullptr)
uint8_t u8
Definition: defines.h:39
CORE_API std::vector< T > to_vector(const Container< T, Args... > &container)
Definition: utils.h:513
quint32 u32
void layoutAboutToBeChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
void layoutChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
int column() const const
int row() const const
Q_EMITQ_EMIT
QObject * parent() const const
QString fromStdString(const std::string &str)
AlignCenter
DisplayRole
Orientation
void append(const T &value)
void clear()
int size() const const