HAL  v4.5.0-299-g9ee015483
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
wavedata_table_editor.cpp
Go to the documentation of this file.
2 #include "hal_core/netlist/net.h"
6 #include <QHeaderView>
7 #include <QDebug>
8 #include <QDir>
9 
10 namespace hal {
12  : QTableWidget(parent), mMaxTime(0), mDisableCellParser(false), mDisplayHexValues(true), mCurrentTime(0), mCurrentRow(0)
13  {;}
14 
15  void WavedataTableEditor::setup(const std::vector<NetlistSimulatorController::InputColumnHeader> &inpColHeads, bool omitClock)
16  {
17  // called again whenever the wizard page is entered, start with an empty table
18  disconnect(this, &QTableWidget::itemChanged, this, &WavedataTableEditor::handleItemChanged);
19  mInputColumnHeader.clear();
20  clear();
21 
22  for (NetlistSimulatorController::InputColumnHeader ich : inpColHeads)
23  {
24  if (omitClock && ich.is_clock) continue;
25  mInputColumnHeader.append(ich);
26  }
27 
28  setupHeader();
29 
30  setRowCount(2);
31  int n = mInputColumnHeader.size()+1;
32  for (int i=0; i<n; i++) setValueCell(0,i,0);
33 
34  // Must not change start time t=0
35  item(0,0)->setFlags(item(0,0)->flags() & (~Qt::ItemIsEditable));
36  connect(this, &QTableWidget::itemChanged, this, &WavedataTableEditor::handleItemChanged);
37  }
38 
39  void WavedataTableEditor::setupHeader()
40  {
41  QStringList headerLabel;
42  for (NetlistSimulatorController::InputColumnHeader ich : mInputColumnHeader)
43  {
44  QString colName = QString::fromStdString(ich.name);
45  if (ich.nets.size() > 1) colName += QString("[%1:0]").arg(ich.nets.size()-1);
46  headerLabel << colName;
47  }
48  int n = mInputColumnHeader.size()+1;
49  setColumnCount(n);
50  headerLabel.prepend("Time");
51  setHorizontalHeaderLabels(headerLabel);
52  }
53 
54  void WavedataTableEditor::setValueCell(int irow, int icol, int val)
55  {
56  QTableWidgetItem* valItem = new QTableWidgetItem(val > 9 && mDisplayHexValues
57  ? "0x" + QString::number(val,16)
58  : QString::number(val));
60  setItem (irow, icol, valItem);
61  }
62 
63  void WavedataTableEditor::loadWaveData(const QString& saleaDirectoryFile)
64  {
65  mDisableCellParser = true;
66  SaleaeParser parser(saleaDirectoryFile.toStdString());
67 
68  mCurrentTime = 0;
69  mCurrentRow = 0;
70  int nrows = 1;
71  int ncols = columnCount() - 1; // columns with values
72  mCurrentValue = new int[ncols];
73  memset(mCurrentValue,0,ncols*sizeof(int));
74 
75  for (int icol = 0; icol < ncols; icol++)
76  {
77  const NetlistSimulatorController::InputColumnHeader& ich = mInputColumnHeader.at(icol);
78 
79  int nBits = ich.nets.size();
80 
81  for (int i=0; i<nBits; i++)
82  {
83  int mask = 1 << i;
84 
85  const Net* n = ich.nets.at(i);
86 
87  parser.register_callback(n->get_name(), n->get_id(), [icol,mask,ncols,this](const void* obj, uint64_t t, int val) {
88  if (t != mCurrentTime)
89  {
90  mCurrentRow++;
91  if (mCurrentRow+1 >= rowCount()) setRowCount(mCurrentRow+2);
92  QTableWidgetItem* tItem = new QTableWidgetItem(QString::number(t));
93  tItem->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
94  setItem(mCurrentRow,0,tItem);
95  mCurrentTime = t;
96  for (int i=0; i<ncols; i++)
97  setValueCell(mCurrentRow-1, i+1, mCurrentValue[i]);
98  }
99 
100  if (val)
101  mCurrentValue[icol] |= mask;
102  else
103  mCurrentValue[icol] &= (~mask);
104  }, nullptr);
105  }
106  }
107 
108  clear();
109  setupHeader();
110  setRowCount(2);
111  QTableWidgetItem* zeroItem = new QTableWidgetItem("0");
113  zeroItem->setFlags(zeroItem->flags() & (~Qt::ItemIsEditable));
114  setItem(0,0,zeroItem);
115 
116  while (parser.next_event()) {;} // loop over entries calling callback
117 
118  for (int i=0; i<ncols; i++)
119  setValueCell(mCurrentRow, i+1, mCurrentValue[i]);
120 
121  delete [] mCurrentValue;
122 
123  mDisableCellParser = false;
124  }
125 
126  void WavedataTableEditor::handleItemChanged(QTableWidgetItem* changedItem)
127  {
128  if (mDisableCellParser) return;
129 
130  mDisableCellParser = true;
131  int cur = rowCount() - 1;
132  if (cur > 0 && !changedItem->column() && changedItem->row() == cur)
133  {
134  setRowCount(cur+2);
135  for (int icol=1; icol < columnCount(); icol++)
136  {
137  QTableWidgetItem* testItem = item(cur, icol);
138  QTableWidgetItem* lastItem = item(cur-1, icol);
139  if ((!testItem || testItem->text().isEmpty()) && lastItem)
140  {
141  QTableWidgetItem* twi = lastItem->clone();
143  setItem(cur, icol, twi);
144  }
145  }
146  }
147  if (!changedItem->column())
148  {
149  // time column
150  int irow = changedItem->row();
151  if (cur > 0 && irow == cur)
152  {
153  setRowCount(cur+2);
154  for (int icol=1; icol < columnCount(); icol++)
155  {
156  QTableWidgetItem* testItem = item(cur, icol);
157  QTableWidgetItem* lastItem = item(cur-1, icol);
158  if ((!testItem || testItem->text().isEmpty()) && lastItem)
159  {
160  QTableWidgetItem* twi = lastItem->clone();
162  setItem(cur,icol, twi);
163  }
164  }
165  Q_EMIT lineAdded();
166  }
167  bool numericOk = false;
168  qlonglong thisVal = changedItem->text().toLongLong(&numericOk);
169  qlonglong prevVal = irow ? item(irow-1,0)->text().toLongLong() : 0;
170  qlonglong nextVal = 0;
171  if (rowCount() > irow+1 && item(irow+1,0))
172  {
173  qlonglong tempNextVal = item(irow+1,0)->text().toLongLong(&numericOk);
174  if (numericOk && tempNextVal >= prevVal+2) nextVal = tempNextVal;
175  }
176  if (nextVal)
177  {
178  // changed time value has successor
179  if (!numericOk || thisVal <= prevVal || thisVal >= nextVal)
180  changedItem->setText(QString::number((prevVal+nextVal)/2));
181  }
182  else
183  {
184  // changed last time value
185  if (!numericOk || thisVal <= prevVal)
186  changedItem->setText(QString::number(prevVal+1000));
187  }
188  }
189  else
190  {
191  // wave value column
192  int val = intCellValue(changedItem->row(),changedItem->column());
193  if (val == sIllegalValue)
194  changedItem->setText("0");
195  else
196  {
197  if (val > 9 && mDisplayHexValues)
198  changedItem->setText("0x" + QString::number(val,16));
199  else
200  changedItem->setText(QString::number(val,10));
201  }
202  }
204  mDisableCellParser = false;
205  }
206 
207  int WavedataTableEditor::validLines() const
208  {
209  int retval = 0;
210  qlonglong tlast = -1;
211  bool numericOk = false;
212  for (int irow = 0; irow < rowCount(); irow++)
213  {
214  QTableWidgetItem* timeCell = item(irow,0);
215  if (!timeCell) continue;
216  qlonglong t = timeCell->text().toLongLong(&numericOk);
217  if (!numericOk) continue;
218  if (t <= tlast) continue;
219  bool incomplete = false;
220  for (int icol=1; icol<columnCount(); icol++)
221  {
222  QTableWidgetItem* testItem = item(irow,icol);
223  if (!testItem || testItem->text().isEmpty())
224  {
225  incomplete = true;
226  break;
227  }
228  }
229  if (incomplete) continue;
230  ++retval;
231  tlast = t;
232  }
233  return retval;
234  }
235 
236  int WavedataTableEditor::intCellValue(int irow, int icol) const
237  {
238  QString txt;
239  int nBits = mInputColumnHeader.at(icol-1).nets.size();
240  int upperLimit = 1 << nBits;
241 
242  if (item(irow,icol))
243  txt = item(irow,icol)->text();
244  if (txt.isEmpty())
245  return sIllegalValue;
246 
247  bool numberOk = true;
248  int tempValue = -1;
249 
250  // most common input 0,1,x,z
251  if (txt.size() == 1)
252  {
253  switch (txt.at(0).toUpper().unicode())
254  {
255  case 'Z':
256  return -2;
257  case 'X':
258  return -1;
259  default:
260  // legal values 0-9
261  tempValue = txt.toInt(&numberOk);
262  if (!numberOk || tempValue >= upperLimit) return sIllegalValue;
263  return tempValue;
264  }
265  }
266 
267  // txt size at least 2
268  if (txt.at(0) == '"')
269  {
270  // enter ASCII character, eg. "A -> 0x41 (65)
271  tempValue = txt.at(1).unicode();
272  }
273  else
274  {
275  int apos = txt.indexOf('\'');
276  if (apos < 0)
277  {
278  if (txt.at(0) == '0')
279  {
280  switch (txt.at(1).toUpper().unicode())
281  {
282  case 'X':
283  tempValue = txt.mid(2).toInt(&numberOk, 16);
284  break;
285  case 'O':
286  tempValue = txt.mid(2).toInt(&numberOk, 8);
287  break;
288  case 'B':
289  tempValue = txt.mid(2).toInt(&numberOk, 2);
290  break;
291  default:
292  tempValue = txt.toInt(&numberOk, 10);
293  break;
294  }
295  }
296  else
297  tempValue = txt.toInt(&numberOk, 10);
298  }
299  else
300  {
301  if (apos + 2 >= txt.size())
302  return sIllegalValue;
303  switch (txt.at(apos+1).toUpper().unicode())
304  {
305  case 'H':
306  tempValue = txt.mid(apos+2).toInt(&numberOk, 16);
307  break;
308  case 'O':
309  tempValue = txt.mid(apos+2).toInt(&numberOk, 8);
310  break;
311  case 'B':
312  tempValue = txt.mid(apos+2).toInt(&numberOk, 2);
313  break;
314  case 'D':
315  tempValue = txt.mid(apos+2).toInt(&numberOk, 10);
316  break;
317  default:
318  return sIllegalValue;
319  }
320  }
321  }
322 
323  if (!numberOk || tempValue >= upperLimit)
324  return sIllegalValue;
325 
326  return tempValue;
327  }
328 
329  void WavedataTableEditor::generateSimulationInput(const QString& workdir)
330  {
331  QDir saleaeDir = QDir(workdir).absoluteFilePath("saleae");
332  QString saleaeDirectoryFilename = saleaeDir.absoluteFilePath("saleae.json");
333  SaleaeWriter* writer = new SaleaeWriter(saleaeDirectoryFilename.toStdString());
334  int iTableCol = 1;
335  QTableWidgetItem* timeCell = item(0,0);
336  mMaxTime = timeCell ? timeCell->text().toULongLong() : 0;
337  int sofIndex = 0;
338  int maxIndex = 0;
339  for (auto it = mInputColumnHeader.constBegin(); it != mInputColumnHeader.constEnd(); ++it)
340  maxIndex += it->nets.size();
341 
342  SaleaeOutputFile** sofArray;
343  int* valArray;
344  sofArray = new SaleaeOutputFile*[maxIndex];
345  valArray = new int[maxIndex];
346 
347  for (auto it = mInputColumnHeader.constBegin(); it != mInputColumnHeader.constEnd(); ++it)
348  {
349  int tableVal = intCellValue(0,iTableCol);
350  if (tableVal == WavedataTableEditor::sIllegalValue)
351  tableVal = 0;
352  int mask = 1;
353  int iBit = 0;
354  for (const Net* n : it->nets)
355  {
356  SaleaeOutputFile* sof = writer->add_or_replace_waveform(n->get_name(),n->get_id());
357  int netVal = tableVal & mask;
358  sof->writeTimeValue(mMaxTime,netVal);
359  sofArray[sofIndex + iBit] = sof;
360  valArray[sofIndex + iBit] = netVal;
361  mask <<= 1;
362  ++iBit;
363  }
364  ++iTableCol;
365  sofIndex += it->nets.size();
366  }
367 
368  for (int irow = 1; irow < rowCount(); irow++)
369  {
370  sofIndex = 0;
371  timeCell = item(irow,0);
372  if (!timeCell) continue;
373  uint64_t tval = timeCell->text().toULongLong();
374  if (tval <= mMaxTime) continue;
375  mMaxTime = tval;
376  iTableCol = 1;
377  for (auto it = mInputColumnHeader.constBegin(); it != mInputColumnHeader.constEnd(); ++it)
378  {
379  int tableVal = intCellValue(irow,iTableCol);
380  if (tableVal == WavedataTableEditor::sIllegalValue)
381  tableVal = 0;
382  int mask = 1;
383 
384  for (int iBit = 0; iBit < it->nets.size(); ++iBit)
385  {
386  int netVal = tableVal & mask;
387  if (netVal != valArray[sofIndex+iBit])
388  {
389  SaleaeOutputFile* sof = sofArray[sofIndex + iBit];
390  Q_ASSERT(sof);
391  sof->writeTimeValue(mMaxTime,netVal);
392  valArray[sofIndex + iBit] = netVal;
393  }
394  mask <<= 1;
395  }
396  ++iTableCol;
397  sofIndex += it->nets.size();
398  }
399  }
400 
401  delete writer;
402  delete [] sofArray;
403  delete [] valArray;
404  }
405 
406  void WavedataTableEditor::setDisplayHexValues(bool hex)
407  {
408  if (hex == mDisplayHexValues) return; // nothing to do;
409 
410  mDisableCellParser = true;
411  mDisplayHexValues = hex;
412 
413  for (int icol = 1; icol < columnCount(); icol++)
414  for (int irow = 0; irow < rowCount(); irow++)
415  {
416  QTableWidgetItem* cell = item(irow,icol);
417  if (!cell) continue;
418  int val = intCellValue(irow,icol);
419  QString txt = QString::number(val, mDisplayHexValues ? 16 : 10);
420  if (mDisplayHexValues && val > 9) txt.prepend("0x");
421  cell->setText(txt);
422  }
423 
424  mDisableCellParser = false;
425  }
426 
427 }
Definition: net.h:58
u32 get_id() const
Definition: net.cpp:88
const std::string & get_name() const
Definition: net.cpp:98
void writeTimeValue(uint64_t t, int32_t val)
Write single data tuple to disk.
The SaleaeParser class is an engine which allows to parse any number of SALEAE files into a sequence ...
Definition: saleae_parser.h:74
bool register_callback(const std::string &name, uint32_t id, std::function< void(void *, uint64_t, int)> callback, void *obj)
Registers callback which gets executed upon next_event()
The SaleaeWriter class is a helper class which provides utility methods when importing data to SALEAE...
Definition: saleae_writer.h:42
SaleaeOutputFile * add_or_replace_waveform(const std::string &name, uint32_t id)
Create new empty output file. Will delete existing binary file if directory links already a data file...
WavedataTableEditor(QWidget *parent=nullptr)
void loadWaveData(const QString &saleaDirectoryFile)
void setup(const std::vector< NetlistSimulatorController::InputColumnHeader > &inpColHeads, bool omitClock)
Definition: defines.h:45
QChar toUpper() const const
ushort unicode() const const
QString absoluteFilePath(const QString &fileName) const const
void prepend(const T &value)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
const QChar at(int position) const const
QString fromStdString(const std::string &str)
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString mid(int position, int n) const const
QString number(int n, int base)
QString & prepend(QChar ch)
int size() const const
int toInt(bool *ok, int base) const const
qlonglong toLongLong(bool *ok, int base) const const
std::string toStdString() const const
qulonglong toULongLong(bool *ok, int base) const const
AlignRight
ItemIsEditable
QTableWidgetItem * item(int row, int column) const const
void itemChanged(QTableWidgetItem *item)
void setColumnCount(int columns)
void setHorizontalHeaderLabels(const QStringList &labels)
void setItem(int row, int column, QTableWidgetItem *item)
void setRowCount(int rows)
virtual QTableWidgetItem * clone() const const
int column() const const
Qt::ItemFlags flags() const const
int row() const const
void setFlags(Qt::ItemFlags flags)
void setText(const QString &text)
void setTextAlignment(int alignment)
QString text() const const
QRegion mask() const const