Set ISA-dependent input validators in dialogs
MODIFY
src/addressdialog.cpp
src/addressdialog.cpp
@@ -13,17 +13,7 @@ AddressDialog::AddressDialog(QWidget* parent) : QDialog(parent), m_ui(new Ui::Ad
m_ui->setupUi(this);
QRegExpValidator* validator = new QRegExpValidator(this);
- const auto isaBytes = ProcessorHandler::currentISA()->bytes();
- if (isaBytes == 2) {
- validator->setRegExp(hexRegex16);
- } else if (isaBytes == 4) {
- validator->setRegExp(hexRegex32);
- } else if (isaBytes == 8) {
- validator->setRegExp(hexRegex64);
- } else {
- Q_UNREACHABLE();
- }
-
+ setISADepRegex(validator);
m_ui->address->setValidator(validator);
m_ui->address->setText("0x" + QString("0").repeated(ProcessorHandler::currentISA()->bytes() * 2));
setWindowTitle("Ripes");

MODIFY
src/loaddialog.cpp
src/loaddialog.cpp
@@ -41,11 +41,12 @@ LoadDialog::LoadDialog(QWidget* parent) : QDialog(parent), m_ui(new Ui::LoadDial
// Binary page
QRegExpValidator* validator = new QRegExpValidator(this);
- validator->setRegExp(hexRegex32);
+ setISADepRegex(validator);
m_ui->binaryLoadAt->setValidator(validator);
- m_ui->binaryLoadAt->setText("0x00000000");
+ m_ui->binaryLoadAt->setText("0x" + QString("0").repeated(ProcessorHandler::currentISA()->bytes() * 2));
m_ui->binaryEntryPoint->setValidator(validator);
- m_ui->binaryEntryPoint->setText("0x00000000");
+ m_ui->binaryEntryPoint->setText("0x" + QString("0").repeated(ProcessorHandler::currentISA()->bytes() * 2));
+
connect(m_ui->binaryLoadAt, &QLineEdit::textChanged, [=] { this->validateCurrentFile(); });
connect(m_ui->binaryEntryPoint, &QLineEdit::textChanged, [=] { this->validateCurrentFile(); });
@@ -167,7 +168,7 @@ ELFInfo LoadDialog::validateELFFile(const QFile& file) {
if (elfbits != ProcessorHandler::currentISA()->bits()) {
const QString bitSize = elfbits == 32 ? "32" : "64";
info.errorMessage = "Expected " + QString::number(ProcessorHandler::currentISA()->bits()) +
- " bit executable, but input file is a " + bitSize + " bit executable.";
+ " bit executable, but input program is " + bitSize + " bit.";
info.valid = false;
goto finish;
}

ADD
src/radix.cpp
+#include "radix.h"
+
+namespace Ripes {
+
+void setISADepRegex(QRegExpValidator* validator) {
+ const auto isaBytes = ProcessorHandler::currentISA()->bytes();
+ if (isaBytes == 2) {
+ validator->setRegExp(hexRegex16);
+ } else if (isaBytes == 4) {
+ validator->setRegExp(hexRegex32);
+ } else if (isaBytes == 8) {
+ validator->setRegExp(hexRegex64);
+ } else {
+ Q_UNREACHABLE();
+ }
+}
+
+QString encodeRadixValue(VInt value, const Radix type, unsigned byteWidth) {
+ switch (type) {
+ case Radix::Hex: {
+ return "0x" + QString::number(value, 16).rightJustified(byteWidth * 2, '0');
+ }
+ case Radix::Float: {
+ float _fvalue; // Copy raw data instead of reinterpret_cast to avoid type-punned pointer error
+ double _dvalue;
+ if (byteWidth <= 4) {
+ memcpy(&_fvalue, &value, sizeof(_fvalue));
+ return QString::number(_fvalue);
+ } else {
+ memcpy(&_dvalue, &value, sizeof(_dvalue));
+ return QString::number(_dvalue);
+ }
+ }
+ case Radix::Binary: {
+ return "0b" + QString::number(value, 2).rightJustified(byteWidth * CHAR_BIT, '0');
+ }
+ case Radix::Unsigned: {
+ return QString::number(value, 10);
+ }
+ case Radix::Signed: {
+ if (byteWidth == 4) {
+ return QString::number(static_cast<int32_t>(value), 10);
+ } else {
+ return QString::number(static_cast<int64_t>(value), 10);
+ }
+ }
+ case Radix::ASCII: {
+ QString str;
+ for (unsigned i = 0; i < byteWidth; i++) {
+ str.prepend(QChar::fromLatin1(value & 0xFF));
+ value >>= 8;
+ }
+ return str;
+ }
+ }
+ Q_UNREACHABLE();
+}
+
+VInt decodeRadixValue(QString value, const Radix type, bool* ok) {
+ switch (type) {
+ case Radix::Hex: {
+ return value.toUInt(ok, 16);
+ }
+ case Radix::Binary: {
+ // Qt doesn't support 0b[0-1]* conversion, so remove any possible 0b prefix
+ if (value.startsWith("0b")) {
+ value.remove(0, 2);
+ }
+ return value.toUInt(ok, 2);
+ }
+ case Radix::Unsigned: {
+ return value.toUInt(ok, 10);
+ }
+ case Radix::Signed: {
+ return value.toInt(ok, 10);
+ }
+ case Radix::Float: {
+ return value.toFloat(ok);
+ }
+ case Radix::ASCII: {
+ QString valueRev;
+ for (const auto& c : value) {
+ valueRev.prepend(c);
+ }
+ uint32_t v = 0;
+ for (int i = 0; i < valueRev.length(); i++) {
+ v |= (valueRev[i].toLatin1() & 0xFF) << (i * 8);
+ }
+ *ok = true;
+ return v;
+ }
+ }
+ Q_UNREACHABLE();
+}
+
+} // namespace Ripes

MODIFY
src/radix.h
src/radix.h
#include <QMetaType>
#include <QRegExp>
+#include <QRegExpValidator>
#include <QString>
+
#include <climits>
#include <map>
+#include "processorhandler.h"
#include "ripes_types.h"
namespace Ripes {
@@ -23,83 +26,9 @@ static const auto binRegex = QRegExp("0[bB][0-1]+");
static const auto unsignedRegex = QRegExp("[0-9]+");
static const auto signedRegex = QRegExp("[-]*[0-9]+");
-static inline QString encodeRadixValue(VInt value, const Radix type, unsigned byteWidth) {
- switch (type) {
- case Radix::Hex: {
- return "0x" + QString::number(value, 16).rightJustified(byteWidth * 2, '0');
- }
- case Radix::Float: {
- float _fvalue; // Copy raw data instead of reinterpret_cast to avoid type-punned pointer error
- double _dvalue;
- if (byteWidth <= 4) {
- memcpy(&_fvalue, &value, sizeof(_fvalue));
- return QString::number(_fvalue);
- } else {
- memcpy(&_dvalue, &value, sizeof(_dvalue));
- return QString::number(_dvalue);
- }
- }
- case Radix::Binary: {
- return "0b" + QString::number(value, 2).rightJustified(byteWidth * CHAR_BIT, '0');
- }
- case Radix::Unsigned: {
- return QString::number(value, 10);
- }
- case Radix::Signed: {
- if (byteWidth == 4) {
- return QString::number(static_cast<int32_t>(value), 10);
- } else {
- return QString::number(static_cast<int64_t>(value), 10);
- }
- }
- case Radix::ASCII: {
- QString str;
- for (unsigned i = 0; i < byteWidth; i++) {
- str.prepend(QChar::fromLatin1(value & 0xFF));
- value >>= 8;
- }
- return str;
- }
- }
- Q_UNREACHABLE();
-}
-
-static inline VInt decodeRadixValue(QString value, const Radix type, bool* ok = nullptr) {
- switch (type) {
- case Radix::Hex: {
- return value.toUInt(ok, 16);
- }
- case Radix::Binary: {
- // Qt doesn't support 0b[0-1]* conversion, so remove any possible 0b prefix
- if (value.startsWith("0b")) {
- value.remove(0, 2);
- }
- return value.toUInt(ok, 2);
- }
- case Radix::Unsigned: {
- return value.toUInt(ok, 10);
- }
- case Radix::Signed: {
- return value.toInt(ok, 10);
- }
- case Radix::Float: {
- return value.toFloat(ok);
- }
- case Radix::ASCII: {
- QString valueRev;
- for (const auto& c : value) {
- valueRev.prepend(c);
- }
- uint32_t v = 0;
- for (int i = 0; i < valueRev.length(); i++) {
- v |= (valueRev[i].toLatin1() & 0xFF) << (i * 8);
- }
- *ok = true;
- return v;
- }
- }
- Q_UNREACHABLE();
-}
+void setISADepRegex(QRegExpValidator* validator);
+QString encodeRadixValue(VInt value, const Radix type, unsigned byteWidth);
+VInt decodeRadixValue(QString value, const Radix type, bool* ok = nullptr);
} // namespace Ripes

Set ISA-dependent input validators in dialogs