From 375607d9969896c894fa2642ce4a0b8b519a2adb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Sat, 2 Sep 2017 15:34:07 +0200 Subject: Use QString and QVector instead of std ones and more --- README.md | 2 +- qtmips_machine/instruction.cpp | 44 ++++++++++++------------------ qtmips_machine/instruction.h | 12 ++++---- qtmips_machine/instructions/arithmetic.cpp | 9 +++--- qtmips_machine/instructions/arithmetic.h | 4 +-- qtmips_machine/instructions/jumpbranch.cpp | 8 +++--- qtmips_machine/instructions/jumpbranch.h | 6 ++-- qtmips_machine/instructions/loadstore.cpp | 8 +++--- qtmips_machine/instructions/loadstore.h | 4 +-- qtmips_machine/instructions/nop.cpp | 6 ++-- qtmips_machine/instructions/nop.h | 2 +- qtmips_machine/instructions/shift.cpp | 8 +++--- qtmips_machine/instructions/shift.h | 4 +-- qtmips_machine/memory.cpp | 4 +-- qtmips_machine/programloader.cpp | 6 ++-- qtmips_machine/programloader.h | 6 ++-- qtmips_machine/programmemory.cpp | 10 ++++--- qtmips_machine/programmemory.h | 3 +- qtmips_machine/qtmips_machine.pro | 2 -- qtmips_machine/qtmipsexception.cpp | 14 +++++----- qtmips_machine/qtmipsexception.h | 10 +++---- qtmips_machine/registers.cpp | 9 +++--- qtmips_machine/tests/testprogrammemory.cpp | 41 ++++++++++++++++++++++++++++ qtmips_machine/tests/tests.pro | 3 +- qtmips_machine/tests/tst_machine.h | 3 ++ qtmips_machine/utils.cpp | 30 -------------------- qtmips_machine/utils.h | 13 --------- 27 files changed, 131 insertions(+), 140 deletions(-) create mode 100644 qtmips_machine/tests/testprogrammemory.cpp delete mode 100644 qtmips_machine/utils.cpp delete mode 100644 qtmips_machine/utils.h diff --git a/README.md b/README.md index bb60441..ea96aa5 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ MIPS CPU simulator for education purposes. Dependencies ------------ * Qt 5 (version 4 is not tested but it might work) -* libelf +* elfutils (libelf works too but you might have problems with it) Compilation ----------- 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 -#include -#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 InstructionR::to_strs() { - std::vector str; +QVector InstructionR::to_strs() { + QVector 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 InstructionI::to_strs() { - std::vector str; +QVector InstructionI::to_strs() { + QVector 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 InstructionJ::to_strs() { - std::vector str; +QVector InstructionJ::to_strs() { + QVector 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 -#include +#include +#include #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 to_strs() = 0; // Returns all fields of instructions in string + virtual QVector 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 to_strs(); + QVector 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 to_strs(); + QVector 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 to_strs(); + QVector 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 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 InstructionArithmetic::to_strs() { - std::vector str = this->InstructionR::to_strs(); +QVector InstructionArithmetic::to_strs() { + QVector 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 InstructionArithmeticImmediate::to_strs() { - std::vector str = this->InstructionI::to_strs(); +QVector InstructionArithmeticImmediate::to_strs() { + QVector 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 to_strs(); + QVector 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 to_strs(); + QVector 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 InstructionJump::to_strs() { - std::vector str = this->InstructionJ::to_strs(); +QVector InstructionJump::to_strs() { + QVector 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 InstructionJumpRegister::to_strs() { - std::vector str = this->InstructionR::to_strs(); +QVector InstructionJumpRegister::to_strs() { + QVector 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 to_strs(); + QVector to_strs(); private: bool link; }; @@ -14,7 +14,7 @@ private: class InstructionJumpRegister : InstructionR { public: InstructionJumpRegister(bool link, std::uint8_t rs); - std::vector to_strs(); + QVector to_strs(); private: bool link; }; @@ -26,7 +26,7 @@ enum InstructionBranchT { class InstructionBranch : InstructionI { public: InstructionBranch(); - std::vector to_strs(); + QVector 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 InstructionLoad::to_strs() { - std::vector str = this->InstructionI::to_strs(); +QVector InstructionLoad::to_strs() { + QVector 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 InstructionStore::to_strs() { - std::vector str = this->InstructionI::to_strs(); +QVector InstructionStore::to_strs() { + QVector 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 to_strs(); + QVector 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 to_strs(); + QVector 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 InstructionNop::to_strs() { - std::vector str; - str.push_back("nop"); +QVector InstructionNop::to_strs() { + QVector 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 to_strs(); + QVector 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 InstructionShift::to_strs() { - std::vector str = this->InstructionR::to_strs(); +QVector InstructionShift::to_strs() { + QVector 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 InstructionShiftVariable::to_strs() { - std::vector str = this->InstructionR::to_strs(); +QVector InstructionShiftVariable::to_strs() { + QVector 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 to_strs(); + QVector 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 to_strs(); + QVector 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 ProgramLoader::get_data(size_t sec) { +QVector ProgramLoader::get_data(size_t sec) { SANITY_ASSERT(sec > this->get_nsec(), "Requesting too big section"); - std::vector d; + QVector 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 #include #include -#include +#include 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 get_data(size_t sec); // Returns bytes of given section + QVector 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 map; // external index to phdrs index + QVector 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 data = loader->get_data(i); + QVector 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 -#include +#include -#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 +#include + +QVector str_inst_r(const char *inst, const char *rs, const char *rd, const char *rt, const char *sa) { + QVector 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("bin"); + QTest::addColumn>("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, 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 - -#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 - -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 -- cgit v1.2.3