aboutsummaryrefslogtreecommitdiff
path: root/qtmips_machine
diff options
context:
space:
mode:
authorKarel Kočí <cynerd@email.cz>2017-09-02 15:34:07 +0200
committerKarel Kočí <cynerd@email.cz>2017-09-02 15:34:07 +0200
commit375607d9969896c894fa2642ce4a0b8b519a2adb (patch)
treecb947a843d85f1cd278da15cff432c849986166c /qtmips_machine
parent82884b3ef726889f837542d2b5e98e2cd8246c00 (diff)
downloadqtmips-375607d9969896c894fa2642ce4a0b8b519a2adb.tar.gz
qtmips-375607d9969896c894fa2642ce4a0b8b519a2adb.tar.bz2
qtmips-375607d9969896c894fa2642ce4a0b8b519a2adb.zip
Use QString and QVector instead of std ones and more
Diffstat (limited to 'qtmips_machine')
-rw-r--r--qtmips_machine/instruction.cpp44
-rw-r--r--qtmips_machine/instruction.h12
-rw-r--r--qtmips_machine/instructions/arithmetic.cpp9
-rw-r--r--qtmips_machine/instructions/arithmetic.h4
-rw-r--r--qtmips_machine/instructions/jumpbranch.cpp8
-rw-r--r--qtmips_machine/instructions/jumpbranch.h6
-rw-r--r--qtmips_machine/instructions/loadstore.cpp8
-rw-r--r--qtmips_machine/instructions/loadstore.h4
-rw-r--r--qtmips_machine/instructions/nop.cpp6
-rw-r--r--qtmips_machine/instructions/nop.h2
-rw-r--r--qtmips_machine/instructions/shift.cpp8
-rw-r--r--qtmips_machine/instructions/shift.h4
-rw-r--r--qtmips_machine/memory.cpp4
-rw-r--r--qtmips_machine/programloader.cpp6
-rw-r--r--qtmips_machine/programloader.h6
-rw-r--r--qtmips_machine/programmemory.cpp10
-rw-r--r--qtmips_machine/programmemory.h3
-rw-r--r--qtmips_machine/qtmips_machine.pro2
-rw-r--r--qtmips_machine/qtmipsexception.cpp14
-rw-r--r--qtmips_machine/qtmipsexception.h10
-rw-r--r--qtmips_machine/registers.cpp9
-rw-r--r--qtmips_machine/tests/testprogrammemory.cpp41
-rw-r--r--qtmips_machine/tests/tests.pro3
-rw-r--r--qtmips_machine/tests/tst_machine.h3
-rw-r--r--qtmips_machine/utils.cpp30
-rw-r--r--qtmips_machine/utils.h13
26 files changed, 130 insertions, 139 deletions
diff --git a/qtmips_machine/instruction.cpp b/qtmips_machine/instruction.cpp
index c5d5345..e03f794 100644
--- a/qtmips_machine/instruction.cpp
+++ b/qtmips_machine/instruction.cpp
@@ -1,7 +1,4 @@
#include "instruction.h"
-#include <sstream>
-#include <iostream>
-#include "utils.h"
InstructionR::InstructionR(std::uint8_t rs, std::uint8_t rd, std::uint8_t rt, std::uint8_t sa) {
this->rs = rs;
@@ -12,20 +9,18 @@ InstructionR::InstructionR(std::uint8_t rs, std::uint8_t rd, std::uint8_t rt, st
// TODO for registers output as register ($0)!
-std::vector<std::string> InstructionR::to_strs() {
- std::vector<std::string> str;
+QVector<QString> InstructionR::to_strs() {
+ QVector<QString> str;
// Instruction name
- str.push_back("unknown"); // unknown instruction, should be replaced by child
-
+ str << "unknown"; // unknown instruction, should be replaced by child
// Source register
- str.push_back(to_string_hex((unsigned)this->rs));
+ str << QString::number((unsigned)this->rs, 16);
// Target register
- str.push_back(to_string_hex((unsigned)this->rt));
+ str << QString::number((unsigned)this->rt, 16);
// Destination register
- str.push_back(to_string_hex((unsigned)this->rd));
+ str << QString::number((unsigned)this->rd, 16);
// Shift amount
- str.push_back(to_string_hex((unsigned)this->sa));
-
+ str << QString::number((unsigned)this->sa, 16);
return str;
}
@@ -35,18 +30,16 @@ InstructionI::InstructionI(std::uint8_t rs, std::uint8_t rt, std::uint16_t immed
this->immediate = immediate;
}
-std::vector<std::string> InstructionI::to_strs() {
- std::vector<std::string> str;
+QVector<QString> InstructionI::to_strs() {
+ QVector<QString> str;
// Instruction name
- str.push_back("unknown"); // unknown instruction, should be replaced by child
-
+ str << "unknown"; // unknown instruction, should be replaced by child
// Source register
- str.push_back(to_string_hex((unsigned)this->rs));
+ str << QString::number((unsigned)this->rs, 16);
// Target register
- str.push_back(to_string_hex((unsigned)this->rt));
+ str << QString::number((unsigned)this->rt, 16);
// Immediate value
- str.push_back(to_string_hex((unsigned)this->immediate));
-
+ str << QString::number((unsigned)this->immediate, 16);
return str;
}
@@ -54,14 +47,11 @@ InstructionJ::InstructionJ(std::uint32_t address) {
this->address = address;
}
-std::vector<std::string> InstructionJ::to_strs() {
- std::vector<std::string> str;
+QVector<QString> InstructionJ::to_strs() {
+ QVector<QString> str;
// Instruction name
- str.push_back("unknown"); // unknown instruction, should be replaced by child
-
- std::stringstream ss;
+ str << "unknown"; // unknown instruction, should be replaced by child
// Source register
- str.push_back(to_string_hex((unsigned)this->address));
-
+ str << QString::number((unsigned)this->address, 16);
return str;
}
diff --git a/qtmips_machine/instruction.h b/qtmips_machine/instruction.h
index d276a83..99a3ba4 100644
--- a/qtmips_machine/instruction.h
+++ b/qtmips_machine/instruction.h
@@ -1,8 +1,8 @@
#ifndef INSTRUCTION_H
#define INSTRUCTION_H
-#include <vector>
-#include <string>
+#include <qstring.h>
+#include <qvector.h>
#include "registers.h"
#include "memory.h"
@@ -14,14 +14,14 @@ public:
//virtual void memory(Memory *mem) = 0; // Read or write to memory
//virtual void write_back(Registers *regs) = 0; // Write results to registers
- virtual std::vector<std::string> to_strs() = 0; // Returns all fields of instructions in string
+ virtual QVector<QString> to_strs() = 0; // Returns all fields of instructions in string
};
class InstructionR : public Instruction {
public:
InstructionR(std::uint8_t rs, std::uint8_t rd, std::uint8_t rt, std::uint8_t sa);
- std::vector<std::string> to_strs();
+ QVector<QString> to_strs();
protected:
std::uint8_t rs, rd, rt, sa;
};
@@ -30,7 +30,7 @@ class InstructionI : public Instruction {
public:
InstructionI(std::uint8_t rs, std::uint8_t rt, std::uint16_t immediate);
- std::vector<std::string> to_strs();
+ QVector<QString> to_strs();
protected:
std::uint8_t rs, rt;
std::uint16_t immediate;
@@ -40,7 +40,7 @@ class InstructionJ : public Instruction {
public:
InstructionJ(std::uint32_t address);
- std::vector<std::string> to_strs();
+ QVector<QString> to_strs();
protected:
std::uint32_t address;
};
diff --git a/qtmips_machine/instructions/arithmetic.cpp b/qtmips_machine/instructions/arithmetic.cpp
index 8c8f40e..a620436 100644
--- a/qtmips_machine/instructions/arithmetic.cpp
+++ b/qtmips_machine/instructions/arithmetic.cpp
@@ -1,13 +1,12 @@
#include "instructions/arithmetic.h"
-#include <iostream>
InstructionArithmetic::InstructionArithmetic(enum InstructionArithmeticT type, std::uint8_t rs, std::uint8_t rd, std::uint8_t rt)
: InstructionR(rs, rd, rt, 0) {
this->type = type;
}
-std::vector<std::string> InstructionArithmetic::to_strs() {
- std::vector<std::string> str = this->InstructionR::to_strs();
+QVector<QString> InstructionArithmetic::to_strs() {
+ QVector<QString> str = this->InstructionR::to_strs();
str.erase(str.begin() + 4); // Drop sa field
switch (this->type) {
case IAT_ADD:
@@ -52,8 +51,8 @@ InstructionArithmeticImmediate::InstructionArithmeticImmediate(enum InstructionA
this->type = type;
}
-std::vector<std::string> InstructionArithmeticImmediate::to_strs() {
- std::vector<std::string> str = this->InstructionI::to_strs();
+QVector<QString> InstructionArithmeticImmediate::to_strs() {
+ QVector<QString> str = this->InstructionI::to_strs();
switch (this->type) {
case IAT_ADDI:
str[0] = "addi";
diff --git a/qtmips_machine/instructions/arithmetic.h b/qtmips_machine/instructions/arithmetic.h
index 29f89d2..19f1df5 100644
--- a/qtmips_machine/instructions/arithmetic.h
+++ b/qtmips_machine/instructions/arithmetic.h
@@ -19,7 +19,7 @@ enum InstructionArithmeticT {
class InstructionArithmetic : public InstructionR {
public:
InstructionArithmetic(enum InstructionArithmeticT type, std::uint8_t rs, std::uint8_t rd, std::uint8_t rt);
- std::vector<std::string> to_strs();
+ QVector<QString> to_strs();
private:
enum InstructionArithmeticT type;
};
@@ -38,7 +38,7 @@ enum InstructionArithmeticImmediateT {
class InstructionArithmeticImmediate : public InstructionI {
public:
InstructionArithmeticImmediate(enum InstructionArithmeticImmediateT type, std::uint8_t rs, std::uint8_t rt, std::uint16_t value);
- std::vector<std::string> to_strs();
+ QVector<QString> to_strs();
private:
enum InstructionArithmeticImmediateT type;
};
diff --git a/qtmips_machine/instructions/jumpbranch.cpp b/qtmips_machine/instructions/jumpbranch.cpp
index 2ede399..6579c2b 100644
--- a/qtmips_machine/instructions/jumpbranch.cpp
+++ b/qtmips_machine/instructions/jumpbranch.cpp
@@ -5,8 +5,8 @@ InstructionJump::InstructionJump(bool link, std::uint32_t address)
this->link = link;
}
-std::vector<std::string> InstructionJump::to_strs() {
- std::vector<std::string> str = this->InstructionJ::to_strs();
+QVector<QString> InstructionJump::to_strs() {
+ QVector<QString> str = this->InstructionJ::to_strs();
if (link)
str[0] = "j";
else
@@ -19,8 +19,8 @@ InstructionJumpRegister::InstructionJumpRegister(bool link, std::uint8_t rs)
this->link = link;
}
-std::vector<std::string> InstructionJumpRegister::to_strs() {
- std::vector<std::string> str = this->InstructionR::to_strs();
+QVector<QString> InstructionJumpRegister::to_strs() {
+ QVector<QString> str = this->InstructionR::to_strs();
str.erase(str.begin() + 2, str.end()); // Drop every field after rs
if (link)
str[0] = "j";
diff --git a/qtmips_machine/instructions/jumpbranch.h b/qtmips_machine/instructions/jumpbranch.h
index b8dee5c..762ad95 100644
--- a/qtmips_machine/instructions/jumpbranch.h
+++ b/qtmips_machine/instructions/jumpbranch.h
@@ -6,7 +6,7 @@
class InstructionJump : InstructionJ {
public:
InstructionJump(bool link, std::uint32_t address);
- std::vector<std::string> to_strs();
+ QVector<QString> to_strs();
private:
bool link;
};
@@ -14,7 +14,7 @@ private:
class InstructionJumpRegister : InstructionR {
public:
InstructionJumpRegister(bool link, std::uint8_t rs);
- std::vector<std::string> to_strs();
+ QVector<QString> to_strs();
private:
bool link;
};
@@ -26,7 +26,7 @@ enum InstructionBranchT {
class InstructionBranch : InstructionI {
public:
InstructionBranch();
- std::vector<std::string> to_strs();
+ QVector<QString> to_strs();
private:
// TODO
};
diff --git a/qtmips_machine/instructions/loadstore.cpp b/qtmips_machine/instructions/loadstore.cpp
index c83eae4..27c6402 100644
--- a/qtmips_machine/instructions/loadstore.cpp
+++ b/qtmips_machine/instructions/loadstore.cpp
@@ -5,8 +5,8 @@ InstructionLoad::InstructionLoad(enum InstructionLoadStoreT type, std::uint8_t r
this->type = type;
}
-std::vector<std::string> InstructionLoad::to_strs() {
- std::vector<std::string> str = this->InstructionI::to_strs();
+QVector<QString> InstructionLoad::to_strs() {
+ QVector<QString> str = this->InstructionI::to_strs();
switch (this->type) {
case ILST_B:
str[0] = "lb";
@@ -41,8 +41,8 @@ InstructionStore::InstructionStore(enum InstructionLoadStoreT type, std::uint8_t
this->type = type;
}
-std::vector<std::string> InstructionStore::to_strs() {
- std::vector<std::string> str = this->InstructionI::to_strs();
+QVector<QString> InstructionStore::to_strs() {
+ QVector<QString> str = this->InstructionI::to_strs();
switch (this->type) {
case ILST_B:
str[0] = "sb";
diff --git a/qtmips_machine/instructions/loadstore.h b/qtmips_machine/instructions/loadstore.h
index 9741bd7..6f028fd 100644
--- a/qtmips_machine/instructions/loadstore.h
+++ b/qtmips_machine/instructions/loadstore.h
@@ -16,7 +16,7 @@ enum InstructionLoadStoreT {
class InstructionLoad : public InstructionI {
public:
InstructionLoad(enum InstructionLoadStoreT type, std::uint8_t rs, std::uint8_t rt, std::uint16_t offset);
- std::vector<std::string> to_strs();
+ QVector<QString> to_strs();
private:
enum InstructionLoadStoreT type;
};
@@ -24,7 +24,7 @@ private:
class InstructionStore : public InstructionI {
public:
InstructionStore(enum InstructionLoadStoreT type, std::uint8_t rs, std::uint8_t rt, std::uint16_t offset);
- std::vector<std::string> to_strs();
+ QVector<QString> to_strs();
private:
enum InstructionLoadStoreT type;
};
diff --git a/qtmips_machine/instructions/nop.cpp b/qtmips_machine/instructions/nop.cpp
index 5fd5e47..7623dff 100644
--- a/qtmips_machine/instructions/nop.cpp
+++ b/qtmips_machine/instructions/nop.cpp
@@ -1,7 +1,7 @@
#include "nop.h"
-std::vector<std::string> InstructionNop::to_strs() {
- std::vector<std::string> str;
- str.push_back("nop");
+QVector<QString> InstructionNop::to_strs() {
+ QVector<QString> str;
+ str << QString("nop");
return str;
}
diff --git a/qtmips_machine/instructions/nop.h b/qtmips_machine/instructions/nop.h
index b098b11..5c019fd 100644
--- a/qtmips_machine/instructions/nop.h
+++ b/qtmips_machine/instructions/nop.h
@@ -5,7 +5,7 @@
class InstructionNop : public Instruction {
public:
- std::vector<std::string> to_strs();
+ QVector<QString> to_strs();
};
#endif // NOP_H
diff --git a/qtmips_machine/instructions/shift.cpp b/qtmips_machine/instructions/shift.cpp
index a8c6e41..34bc1c9 100644
--- a/qtmips_machine/instructions/shift.cpp
+++ b/qtmips_machine/instructions/shift.cpp
@@ -5,8 +5,8 @@ InstructionShift::InstructionShift(enum InstructionShiftT type, std::uint8_t rt,
this->type = type;
}
-std::vector<std::string> InstructionShift::to_strs() {
- std::vector<std::string> str = this->InstructionR::to_strs();
+QVector<QString> InstructionShift::to_strs() {
+ QVector<QString> str = this->InstructionR::to_strs();
str.erase(str.begin() + 1); // Drop rs field
switch (this->type) {
case IST_LL:
@@ -30,8 +30,8 @@ InstructionShiftVariable::InstructionShiftVariable(enum InstructionShiftT type,
this->type = type;
}
-std::vector<std::string> InstructionShiftVariable::to_strs() {
- std::vector<std::string> str = this->InstructionR::to_strs();
+QVector<QString> InstructionShiftVariable::to_strs() {
+ QVector<QString> str = this->InstructionR::to_strs();
str.erase(str.begin() + 4); // Drop sa field
switch (this->type) {
case IST_LL:
diff --git a/qtmips_machine/instructions/shift.h b/qtmips_machine/instructions/shift.h
index 9ce29e0..69e2e1e 100644
--- a/qtmips_machine/instructions/shift.h
+++ b/qtmips_machine/instructions/shift.h
@@ -12,7 +12,7 @@ enum InstructionShiftT {
class InstructionShift : public InstructionR {
public:
InstructionShift(enum InstructionShiftT type, std::uint8_t rt, std::uint8_t rd, std::uint8_t sa);
- std::vector<std::string> to_strs();
+ QVector<QString> to_strs();
private:
enum InstructionShiftT type;
};
@@ -20,7 +20,7 @@ private:
class InstructionShiftVariable : public InstructionR {
public:
InstructionShiftVariable(enum InstructionShiftT type, std::uint8_t rs, std::uint8_t rt, std::uint8_t rd);
- std::vector<std::string> to_strs();
+ QVector<QString> to_strs();
private:
enum InstructionShiftT type;
};
diff --git a/qtmips_machine/memory.cpp b/qtmips_machine/memory.cpp
index 2540035..21ca03f 100644
--- a/qtmips_machine/memory.cpp
+++ b/qtmips_machine/memory.cpp
@@ -68,13 +68,13 @@ using namespace std;
void MemorySection::write_byte(std::uint32_t offset, std::uint8_t value) {
if (offset >= this->length)
- throw QTMIPS_EXCEPTION(OutOfMemoryAccess, "Trying to write outside of the memory section", std::string("Accessing using offset: ") + std::to_string(offset));
+ throw QTMIPS_EXCEPTION(OutOfMemoryAccess, "Trying to write outside of the memory section", QString("Accessing using offset: ") + QString(offset));
this->dt[offset] = value;
}
std::uint8_t MemorySection::read_byte(std::uint32_t offset) {
if (offset >= this->length)
- throw QTMIPS_EXCEPTION(OutOfMemoryAccess, "Trying to read outside of the memory section", std::string("Accessing using offset: ") + std::to_string(offset));
+ throw QTMIPS_EXCEPTION(OutOfMemoryAccess, "Trying to read outside of the memory section", QString("Accessing using offset: ") + QString(offset));
return this->dt[offset];
}
diff --git a/qtmips_machine/programloader.cpp b/qtmips_machine/programloader.cpp
index 0b0e294..6eefa6c 100644
--- a/qtmips_machine/programloader.cpp
+++ b/qtmips_machine/programloader.cpp
@@ -69,13 +69,13 @@ std::uint32_t ProgramLoader::get_address(size_t sec) {
return this->phdrs[this->map[sec]].p_vaddr;
}
-std::vector<std::uint8_t> ProgramLoader::get_data(size_t sec) {
+QVector<std::uint8_t> ProgramLoader::get_data(size_t sec) {
SANITY_ASSERT(sec > this->get_nsec(), "Requesting too big section");
- std::vector<std::uint8_t> d;
+ QVector<std::uint8_t> d;
char *f = elf_rawfile(this->elf, NULL);
size_t phdrs_i = this->map[sec];
for (unsigned i = 0; i < this->phdrs[phdrs_i].p_filesz; i++) {
- d.push_back((std::uint8_t) f[this->phdrs[phdrs_i].p_offset + i]);
+ d << (std::uint8_t) f[this->phdrs[phdrs_i].p_offset + i];
}
return d;
}
diff --git a/qtmips_machine/programloader.h b/qtmips_machine/programloader.h
index 058e5c1..4d722d2 100644
--- a/qtmips_machine/programloader.h
+++ b/qtmips_machine/programloader.h
@@ -5,7 +5,7 @@
#include <libelf.h>
#include <gelf.h>
#include <cstdint>
-#include <vector>
+#include <qvector.h>
class ProgramLoader {
@@ -15,14 +15,14 @@ public:
size_t get_nsec(); // Returns number of loadable sections
std::uint32_t get_address(size_t sec); // Get target address for given section
- std::vector<std::uint8_t> get_data(size_t sec); // Returns bytes of given section
+ QVector<std::uint8_t> get_data(size_t sec); // Returns bytes of given section
private:
int fd;
Elf *elf;
GElf_Ehdr hdr; // elf file header
size_t n_secs; // number of sections in elf program header
Elf32_Phdr *phdrs; // program section headers
- std::vector<size_t> map; // external index to phdrs index
+ QVector<size_t> map; // external index to phdrs index
};
#endif // PROGRAM_H
diff --git a/qtmips_machine/programmemory.cpp b/qtmips_machine/programmemory.cpp
index 1ff3bca..74b16a8 100644
--- a/qtmips_machine/programmemory.cpp
+++ b/qtmips_machine/programmemory.cpp
@@ -5,14 +5,16 @@
#include "instructions/loadstore.h"
#include "instructions/nop.h"
#include "instructions/shift.h"
-#include "utils.h"
-ProgramMemory::ProgramMemory(ProgramLoader *loader, MemoryAccess *memory) {
+ProgramMemory::ProgramMemory(MemoryAccess *memory) {
this->memory = memory;
+}
+
+void ProgramMemory::load(ProgramLoader *loader) {
// Load program to memory (just dump it byte by byte, decode is done on demand)
for (int i = 0; i < loader->get_nsec(); i++) {
std::uint32_t base_address = loader->get_address(i);
- std::vector<std::uint8_t> data = loader->get_data(i);
+ QVector<std::uint8_t> data = loader->get_data(i);
for (auto it = data.begin(); it < data.end(); it++) {
memory->write_byte(base_address + i, *it);
}
@@ -40,7 +42,7 @@ Instruction *ProgramMemory::at(std::uint32_t address) {
}
}
-#define I_UNKNOWN(DATA) throw QTMIPS_EXCEPTION(UnsupportedInstruction, "Unknown instruction, can't decode", to_string_hex(DATA))
+#define I_UNKNOWN(DATA) throw QTMIPS_EXCEPTION(UnsupportedInstruction, "Unknown instruction, can't decode", QString::number(DATA, 16))
#define I_UNSUPPORTED(INST) throw QTMIPS_EXCEPTION(UnsupportedInstruction, "Decoded unsupported unstruction", #INST)
Instruction *ProgramMemory::decode_r(std::uint32_t dt) {
diff --git a/qtmips_machine/programmemory.h b/qtmips_machine/programmemory.h
index 84b2f31..158adb1 100644
--- a/qtmips_machine/programmemory.h
+++ b/qtmips_machine/programmemory.h
@@ -8,8 +8,9 @@
class ProgramMemory {
public:
- ProgramMemory(ProgramLoader *loader, MemoryAccess *memory);
+ ProgramMemory(MemoryAccess *memory);
+ void load(ProgramLoader *l);
Instruction *at(std::uint32_t address); // return instruction isntance for given address
private:
diff --git a/qtmips_machine/qtmips_machine.pro b/qtmips_machine/qtmips_machine.pro
index 66cb37c..213727d 100644
--- a/qtmips_machine/qtmips_machine.pro
+++ b/qtmips_machine/qtmips_machine.pro
@@ -25,7 +25,6 @@ SOURCES += \
instructions/shift.cpp \
instructions/nop.cpp \
instructions/jumpbranch.cpp \
- utils.cpp \
cache.cpp
HEADERS += \
@@ -42,5 +41,4 @@ HEADERS += \
instructions/shift.h \
instructions/nop.h \
instructions/jumpbranch.h \
- utils.h \
cache.h
diff --git a/qtmips_machine/qtmipsexception.cpp b/qtmips_machine/qtmipsexception.cpp
index 2ce7ae5..4cec6b0 100644
--- a/qtmips_machine/qtmipsexception.cpp
+++ b/qtmips_machine/qtmipsexception.cpp
@@ -10,19 +10,19 @@ QtMipsException::QtMipsException(QTMIPS_ARGS_COMMON) {
}
const char *QtMipsException::what() const throw() {
- std::string message = this->msg(true);
+ QString message = this->msg(true);
char * cstr = new char [message.length()+1];
- std::strcpy (cstr, message.c_str());
+ std::strcpy (cstr, message.toStdString().c_str());
return cstr;
}
-std::string QtMipsException::msg(bool pos) const {
- std::string message;
+QString QtMipsException::msg(bool pos) const {
+ QString message;
if (pos)
- message += std::string("(") + std::string(this->file) + std::string(":") + std::to_string(this->line) + std::string(") ");
+ message += QString("(") + QString(this->file) + QString(":") + QString(this->line) + QString(") ");
message += this->reason;
- if (!this->ext.empty()) {
- message += std::string(": ");
+ if (!this->ext.isEmpty()) {
+ message += QString(": ");
message += this->ext;
}
return message;
diff --git a/qtmips_machine/qtmipsexception.h b/qtmips_machine/qtmipsexception.h
index bfaa788..81895d6 100644
--- a/qtmips_machine/qtmipsexception.h
+++ b/qtmips_machine/qtmipsexception.h
@@ -2,19 +2,19 @@
#define QTMIPSEXCEPTION_H
#include <exception>
-#include <string>
+#include <qstring.h>
-#define QTMIPS_EXCEPTION(TYPE, REASON, EXT) (QtMipsException ## TYPE (std::string(REASON), std::string(EXT), std::string(__FILE__), __LINE__))
-#define QTMIPS_ARGS_COMMON std::string reason, std::string ext, std::string file, int line
+#define QTMIPS_EXCEPTION(TYPE, REASON, EXT) (QtMipsException ## TYPE (QString(REASON), QString(EXT), QString(__FILE__), __LINE__))
+#define QTMIPS_ARGS_COMMON QString reason, QString ext, QString file, int line
// Base exception for all machine ones
class QtMipsException : public std::exception {
public:
QtMipsException(QTMIPS_ARGS_COMMON);
const char *what() const throw();
- std::string msg(bool pos) const;
+ QString msg(bool pos) const;
protected:
- std::string reason, ext, file;
+ QString reason, ext, file;
int line;
};
diff --git a/qtmips_machine/registers.cpp b/qtmips_machine/registers.cpp
index 514987a..837f403 100644
--- a/qtmips_machine/registers.cpp
+++ b/qtmips_machine/registers.cpp
@@ -1,6 +1,5 @@
#include "registers.h"
#include "qtmipsexception.h"
-#include "utils.h"
// TODO should this be configurable?
//////////////////////////////////////////////////////////////////////////////
@@ -26,26 +25,26 @@ std::uint32_t Registers::pc_inc() {
std::uint32_t Registers::pc_jmp(std::int32_t offset) {
if (offset % 4)
- throw QTMIPS_EXCEPTION(UnalignedJump, "Trying to jump by unaligned offset", to_string_hex(offset));
+ throw QTMIPS_EXCEPTION(UnalignedJump, "Trying to jump by unaligned offset", QString::number(offset, 16));
this->pc += offset;
return this->pc;
}
void Registers::pc_abs_jmp(std::uint32_t address) {
if (address % 4)
- throw QTMIPS_EXCEPTION(UnalignedJump, "Trying to jump to unaligned address", to_string_hex(address));
+ throw QTMIPS_EXCEPTION(UnalignedJump, "Trying to jump to unaligned address", QString::number(address, 16));
this->pc = address;
}
std::uint32_t Registers::read_gp(std::uint8_t i) {
- SANITY_ASSERT(i < 32, std::string("Trying to read from register ") + std::to_string(i));
+ SANITY_ASSERT(i < 32, QString("Trying to read from register ") + QString(i));
if (!i) // $0 always reads as 0
return 0;
return this->gp[i - 1];
}
void Registers::write_gp(std::uint8_t i, std::uint32_t value) {
- SANITY_ASSERT(i < 32, std::string("Trying to write to register ") + std::to_string(i));
+ SANITY_ASSERT(i < 32, QString("Trying to write to register ") + QString(i));
if (i == 0) // Skip write to $0
return;
this->gp[i - 1] = value;
diff --git a/qtmips_machine/tests/testprogrammemory.cpp b/qtmips_machine/tests/testprogrammemory.cpp
new file mode 100644
index 0000000..a08bbab
--- /dev/null
+++ b/qtmips_machine/tests/testprogrammemory.cpp
@@ -0,0 +1,41 @@
+#include "tst_machine.h"
+#include "programmemory.h"
+#include <qstring.h>
+#include <qvector.h>
+
+QVector<QString> str_inst_r(const char *inst, const char *rs, const char *rd, const char *rt, const char *sa) {
+ QVector<QString> ret;
+ ret << inst;
+ if (rs)
+ ret << rs;
+ if (rd)
+ ret << rd;
+ if (rt)
+ ret << rt;
+ if (sa)
+ ret << sa;
+ return ret;
+}
+
+#define I(II) ((std::uint32_t) II)
+
+void MachineTests::program_memory_data() {
+ QTest::addColumn<std::uint32_t>("bin");
+ QTest::addColumn<QVector<QString>>("str");
+
+ // TODO correct instruction
+ QTest::newRow("NOP") << I(0x000000) << str_inst_r("nop", nullptr, nullptr, nullptr, nullptr);
+ //QTest::newRow("SLL") << I(0x000000) << str_inst_r("sll", "", "", nullptr, nullptr);
+ // TODO other instructions
+}
+
+void MachineTests::program_memory() {
+ Memory m;
+ ProgramMemory pm(&m);
+
+ QFETCH(std::uint32_t, bin);
+ QFETCH(QVector<QString>, str);
+
+ m.write_word(0x00, bin);
+ QCOMPARE(pm.at(0x00)->to_strs(), str);
+}
diff --git a/qtmips_machine/tests/tests.pro b/qtmips_machine/tests/tests.pro
index 9477e08..b9f1497 100644
--- a/qtmips_machine/tests/tests.pro
+++ b/qtmips_machine/tests/tests.pro
@@ -17,7 +17,8 @@ DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += tst_machine.cpp \
testmemory.cpp \
- testregisters.cpp
+ testregisters.cpp \
+ testprogrammemory.cpp
HEADERS += tst_machine.h
diff --git a/qtmips_machine/tests/tst_machine.h b/qtmips_machine/tests/tst_machine.h
index e5137c4..50915f5 100644
--- a/qtmips_machine/tests/tst_machine.h
+++ b/qtmips_machine/tests/tst_machine.h
@@ -17,6 +17,9 @@ private Q_SLOTS:
void memory_section();
void memory_section_data();
void memory_endian();
+ // ProgramMemory
+ void program_memory();
+ void program_memory_data();
};
#endif // TST_MACHINE_H
diff --git a/qtmips_machine/utils.cpp b/qtmips_machine/utils.cpp
deleted file mode 100644
index dfe8c2a..0000000
--- a/qtmips_machine/utils.cpp
+++ /dev/null
@@ -1,30 +0,0 @@
-#include "utils.h"
-#include <sstream>
-
-#define TO_STR_HEX do { std::stringstream ss; ss << std::hex << v; return std::string(ss.str()); } while (false)
-
-std::string to_string_hex(int v) {
- TO_STR_HEX;
-}
-
-std::string to_string_hex(unsigned v) {
- TO_STR_HEX;
-}
-
-std::string to_string_hex(long v) {
- TO_STR_HEX;
-}
-
-std::string to_string_hex(unsigned long v) {
- TO_STR_HEX;
-}
-
-std::string to_string_hex(long long v) {
- TO_STR_HEX;
-}
-
-std::string to_string_hex(unsigned long long v) {
- TO_STR_HEX;
-}
-
-#undef TO_STR_HEX
diff --git a/qtmips_machine/utils.h b/qtmips_machine/utils.h
deleted file mode 100644
index 151e1d0..0000000
--- a/qtmips_machine/utils.h
+++ /dev/null
@@ -1,13 +0,0 @@
-#ifndef UTILS_H
-#define UTILS_H
-
-#include <string>
-
-std::string to_string_hex(int);
-std::string to_string_hex(unsigned);
-std::string to_string_hex(long);
-std::string to_string_hex(unsigned long);
-std::string to_string_hex(long long);
-std::string to_string_hex(unsigned long long);
-
-#endif // UTILS_H