Codebase list ponyprog / lintian-fixes/main SrcPony / bitfield.cpp
lintian-fixes/main

Tree @lintian-fixes/main (Download .tar.gz)

bitfield.cpp @lintian-fixes/mainraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//=========================================================================//
//                                                                         //
//  PonyProg - Serial Device Programmer                                    //
//                                                                         //
//  Copyright (C) 1997-2021   Claudio Lanconelli                           //
//                                                                         //
//  http://ponyprog.sourceforge.net                                        //
//                                                                         //
//-------------------------------------------------------------------------//
//                                                                         //
// This program is free software; you can redistribute it and/or           //
// modify it under the terms of the GNU  General Public License            //
// as published by the Free Software Foundation; either version2 of        //
// the License, or (at your option) any later version.                     //
//                                                                         //
// This program is distributed in the hope that it will be useful,         //
// but WITHOUT ANY WARRANTY; without even the implied warranty of          //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       //
// General Public License for more details.                                //
//                                                                         //
// You should have received a copy of the GNU  General Public License      //
// along with this program (see LICENSE);     if not, write to the         //
// Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. //
//                                                                         //
//=========================================================================//


#include <QDebug>
#include <QString>
#include <QMessageBox>

#include "bitfield.h"


BitFieldWidget::BitFieldWidget(QWidget *parent, QVector<BitInfo> &vInfo, QVector<MaskDescr> &vMask, unsigned int field, const QString &chipNm) :
	QWidget(parent)
{
	setupUi(this);

	chipName = chipNm;

	vecInfo = &vInfo;

	vecDescr = &vMask;

	bField = field;

	lstComboBoxes = (QVector<QComboBox *>() << comboBox0 << comboBox1 << comboBox2 << comboBox3);

	for (int i = 0; i < lstComboBoxes.count(); i++)
	{
		lstComboBoxes.at(i)->setHidden(true);
	}

	initWidget();
}


BitFieldWidget::~BitFieldWidget()
{
	qDebug() << "BitFieldWidget::~BitFieldWidget()";
}


void BitFieldWidget::initWidget()
{

	if (vecInfo->count() > 0)
	{
		int lastBit = 0;
		for (int i = 0; i < vecInfo->count(); i++)
		{
			QTreeWidgetItem *itm = new QTreeWidgetItem();
			int bitOffset = vecInfo->at(i).bit;
			lastBit = bitOffset;
			QString sDes = vecInfo->at(i).ShortDescr;
			itm->setText(0, QString().sprintf("Bit %d, ", bitOffset) + sDes);
			if (vecInfo->at(i).LongDescr.length() > 0)
			{
				itm->setText(1, vecInfo->at(i).LongDescr);
			}

			itm->setFlags(itm->flags() | Qt::ItemIsUserCheckable);

			itm->setCheckState(0, Qt::Unchecked);

			if (bField & (1 << bitOffset))
			{
				itm->setCheckState(0, Qt::Checked);
			}
			if (sDes == "SPIEN")
			{
				itm->setFlags(itm->flags() ^ Qt::ItemIsEnabled);
				itm->setCheckState(0, Qt::Checked);
				bField |= (1 << bitOffset);
			}
			if (sDes == "RSTDISBL")
			{
				itm->setCheckState(0, Qt::Unchecked);
				bField &= ~(1 << bitOffset);
			}

			treeWidget->addTopLevelItem(itm);
		}

		// reset not used bits
		for (unsigned int i = (lastBit + 1); i < 32; i++)
		{
			bField &= ~(1U << i);
		}

		treeWidget->expandAll();
		treeWidget->resizeColumnToContents(0);

		connect(treeWidget, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(onBitClicked(QTreeWidgetItem *, int)));
	}

	scanMasks();

	createComboLists();

	// init of comboboxes
	for (int i = 0; i < treeWidget->topLevelItemCount(); i++)
	{
		if (treeWidget->topLevelItem(i)->text(1).length() == 0)
		{
			emit onBitClicked(treeWidget->topLevelItem(i), 0);
		}
	}
}


unsigned int BitFieldWidget::getBitfield()
{
	return bField;
}


void BitFieldWidget::createComboLists()
{
	for (int i = 0; i < maskList.count(); i++)
	{
		QStringList lst;
		QString currentMask = maskList.at(i);
		// two loops are not optimal!
		foreach (MaskDescr mdes, *vecDescr)
		{
			if (mdes.mask.indexOf(QRegExp(currentMask)) >= 0)
			{
				int posBeg = mdes.LongDescr.indexOf("=$n");
				if (posBeg > 0)
				{
					QString tStr = mdes.LongDescr;
					// TODO replace text with text from mdes.ExtDescr
					int posExtText = mdes.ExtDescr.indexOf(chipName + ": ");
					if (posExtText >= 0)
					{
						posExtText += chipName.length();
						QString addrTxt = mdes.ExtDescr.simplified().mid(posExtText + 2, 5);

						tStr.replace(posBeg + 1, 5, addrTxt);
					}

					lst << tStr;
				}
				else
				{
					lst << mdes.LongDescr;
				}
			}
		}

		// when not all combinations are descripted
// 		if (isExp(lst.count()) == false)
		if ((1 << maskBitSum.at(i)) != lst.count())
		{
			lst << "Undefined combination";
		}

		lstComboBoxes.at(i)->setHidden(false);
		lstComboBoxes.at(i)->addItems(lst);

		connect(lstComboBoxes.at(i), SIGNAL(activated(int)), this, SLOT(onComboSelected(int)));
	}
}


void BitFieldWidget::scanMasks()
{
	maskList.clear();
	maskBitSum.clear();

	// analyse from mask entries
	foreach (MaskDescr mdes, *vecDescr)
	{
		QString cMask = mdes.mask;
		cMask.replace(QRegExp("\\d+"), "\\d+");
		// at string begin only
		cMask = "^" + cMask;
		if (maskList.indexOf(cMask) == -1)
		{
			maskList << cMask;
			int numBits = 0;
			QRegExp rx("(\\d+)");
			int pos = 0;
			while ((pos = rx.indexIn(mdes.mask, pos)) != -1)
			{
				QString n =  rx.cap(1);
				numBits += n.length();

				pos += rx.matchedLength();
			}

			qDebug() << mdes.mask << "bits" << numBits;
			maskBitSum << numBits;
		}
	}

	qDebug() << maskList;
}


bool BitFieldWidget::isExp(unsigned int a)
{
	return (a > 0 && (a & (a - 1)) == 0);
}


void BitFieldWidget::onComboSelected(int idx)
{
	QComboBox *s = static_cast<QComboBox *>(sender());
	int globIdx = 0;
	int nMask = -1;

	// no changes in this case
	if (s->currentText() == "Undefined combination")
	{
		return;
	}

	// we have only 4 comboboxes for lock
	for (int i = 0; i < 4; i++)
	{
		if (lstComboBoxes.at(i) == s)
		{
			nMask = i;
			break;
		}
		globIdx += lstComboBoxes.at(i)->count();

		// correcture for undefined
		if (lstComboBoxes.at(i)->findText("Undefined combination") > 0)
		{
			globIdx--;
		}
	}

	if (nMask < 0)
	{
		return;
	}

	globIdx += idx;

	qDebug() << "onComboSelected index" << globIdx;

	if (vecDescr->at(globIdx).mask.length() == 0)
	{
		return;
	}

	// deactivate signal from checkbutton
	disconnect(treeWidget, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(onBitClicked(QTreeWidgetItem *, int)));

	// possible list
	QStringList mskList = vecDescr->at(globIdx).mask.split(" ");
	qDebug() << mskList << vecDescr->at(globIdx).LongDescr;

	foreach (QString cMask, mskList)
	{
		setMaskBits(cMask);
	}

	// activate signal
	connect(treeWidget, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(onBitClicked(QTreeWidgetItem *, int)));

	emit displayBitFields(bField);
}


void BitFieldWidget::setMaskBits(const QString &cMask)
{
	int p = cMask.indexOf("=");
	if (p < 0)
	{
		// not correct format
		return;
	}

	QString mskName = cMask.left(p);
	QString bitStr = cMask.mid(p + 1);
	bool ok;

	unsigned int localField = bitStr.toInt(&ok, 2);



	int idx;
	// at begin of string only
	mskName = "^" + mskName + "\\d+";

	qDebug() << cMask <<  "converted to" << mskName << (bin) << localField << (dec);

	// search in QTreeWidget the names
	for (idx = 0; idx < treeWidget->topLevelItemCount(); idx++)
	{
		QString t = treeWidget->topLevelItem(idx)->text(0);
		int pos = t.indexOf(", ");

		if (pos > 0)
		{
			QString nm = t.mid(pos + 2);

			// first element found
			if (nm.indexOf(QRegExp(mskName)) == 0)
			{
				break;
			}
		}
	}

	if (idx >= treeWidget->topLevelItemCount())
	{
		qDebug() << "setMaskBits is wrong";
		return;
	}

	int bitOffset = vecInfo->at(idx).bit;

	for (int i = 0; i < bitStr.length(); i++)
	{
		Qt::CheckState st;
		if (localField & 0x01)
		{
			st = Qt::Unchecked;

			bField |= (1 << bitOffset);
		}
		else
		{
			st = Qt::Checked;

			bField &= ~(1 << bitOffset);
		}

		localField >>= 1;

		treeWidget->topLevelItem(idx)->setCheckState(0, st);

		QString t = treeWidget->topLevelItem(idx)->text(0);
		int pos = t.indexOf(", ");

		if (pos > 0)
		{
			QString nm = t.mid(pos + 2);
			if (nm.indexOf(QRegExp(mskName)) < 0)
			{
				break;
			}
		}
		else
		{
			break;
		}

		bitOffset++;
		idx++;
	}
}



void BitFieldWidget::onBitClicked(QTreeWidgetItem *itm, int col)
{
	Qt::CheckState st = itm->checkState(0);
	QString t =  itm->text(0);
	int pos = t.indexOf(",");

	if (pos < 0)
	{
		return;
	}

	QString fuseName = t.mid(pos + 2);

	if (fuseName == "RSTDISBL")
	{
		QMessageBox msgBox(QMessageBox::Warning, "Warning", "Attention! If you disable Reset pin you can't access the the chip anymore", QMessageBox::Yes | QMessageBox::No);
// 		msgBox.setStyleSheet(programStyleSheet);
		msgBox.setButtonText(QMessageBox::Yes, "Yes");
		msgBox.setButtonText(QMessageBox::No, "No");
		int res = msgBox.exec();
		if (res == QMessageBox::No)
		{
			return;
		}
	}

	t = t.left(pos);
	t.remove("Bit ");

	int bOffset = t.toInt();

	if (st == Qt::Checked)
	{
		bField |= (1 << bOffset);
	}
	else
	{
		bField &= ~(1 << bOffset);
	}

	displayBitFields(bField);

	qDebug() << fuseName << maskList;

	int idxCombo = -1;

	for (int i = 0; i < maskList.count(); i++)
	{
		QString m = maskList.at(i);
		QStringList mSplitted = m.split(" ");

		for (int r = 0; r < mSplitted.count(); r++)
		{
			QString msplt = mSplitted.at(r);
			msplt.remove("=");
			if (fuseName.indexOf(QRegExp(msplt)) >= 0)
			{
				idxCombo = i;
				qDebug() << "found " << msplt << i;
				break;
			}
		}
		if (idxCombo != -1)
		{
			break;
		}
	}

	// maskListFuse.at(idxCombo) has mask to search
	// and idxCombo is the number of list
	if (idxCombo == -1)
	{
		return;
	}

	QString completeMask = maskList.at(idxCombo);

	// now replace the \\d+ with bit information from treewidget
	QStringList mSplitted = completeMask.split(" ");
	for (int r = 0; r < mSplitted.count(); r++)
	{
		QString orig = mSplitted.at(r);
		QString msplt = orig;
		msplt.remove("=");
		qDebug() << msplt;

		QString bitString;
		for (int iTree = 0; iTree < treeWidget->topLevelItemCount(); ++iTree)
		{
			QString t = treeWidget->topLevelItem(iTree)->text(0);
			int pos = t.indexOf(", ");
			if (pos <= 0)
			{
				continue;
			}

			t = t.mid(pos + 2);

			if (t.indexOf(QRegExp(msplt)) >= 0)
			{
				if (treeWidget->topLevelItem(iTree)->checkState(0) == Qt::Checked)
				{
					bitString = "0" + bitString;
				}
				else
				{
					bitString = "1" + bitString;
				}
			}
		}

		if (bitString.length() > 0)
		{
			msplt.replace("\\d+", QString("=" + bitString));
			completeMask.replace(orig, msplt);
		}
	}

	qDebug() << "converted: " << completeMask << "idx" << idxCombo;

	if (completeMask.length() > 0)
	{
		int i;
		for (i = 0; i < vecDescr->count(); i++)
		{
			if (vecDescr->at(i).mask.indexOf(QRegExp(completeMask)) == 0)
			{
				QString comboText = vecDescr->at(i).LongDescr;
				int c = lstComboBoxes.at(idxCombo)->findText(comboText);
				if (c >= 0)
				{
					lstComboBoxes.at(idxCombo)->setCurrentIndex(c);
				}

				break;
			}
		}
		if (i == vecDescr->count())
		{
			int p = lstComboBoxes.at(idxCombo)->findText("Undefined combination");
			if (p >= 0)
			{
				lstComboBoxes.at(idxCombo)->setCurrentIndex(p);
			}
		}
	}
}