From 9cf92379d5fcf0076c25dae0935daab446c992cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Wed, 30 Aug 2017 21:37:53 +0200 Subject: Initial commit Adding work done so far. --- qtmips_machine/instructions/arithmetic.cpp | 87 ++++++++++++++++++++++++++++++ qtmips_machine/instructions/arithmetic.h | 46 ++++++++++++++++ qtmips_machine/instructions/jumpbranch.cpp | 30 +++++++++++ qtmips_machine/instructions/jumpbranch.h | 34 ++++++++++++ qtmips_machine/instructions/loadstore.cpp | 67 +++++++++++++++++++++++ qtmips_machine/instructions/loadstore.h | 32 +++++++++++ qtmips_machine/instructions/nop.cpp | 7 +++ qtmips_machine/instructions/nop.h | 11 ++++ qtmips_machine/instructions/shift.cpp | 51 ++++++++++++++++++ qtmips_machine/instructions/shift.h | 28 ++++++++++ 10 files changed, 393 insertions(+) create mode 100644 qtmips_machine/instructions/arithmetic.cpp create mode 100644 qtmips_machine/instructions/arithmetic.h create mode 100644 qtmips_machine/instructions/jumpbranch.cpp create mode 100644 qtmips_machine/instructions/jumpbranch.h create mode 100644 qtmips_machine/instructions/loadstore.cpp create mode 100644 qtmips_machine/instructions/loadstore.h create mode 100644 qtmips_machine/instructions/nop.cpp create mode 100644 qtmips_machine/instructions/nop.h create mode 100644 qtmips_machine/instructions/shift.cpp create mode 100644 qtmips_machine/instructions/shift.h (limited to 'qtmips_machine/instructions') diff --git a/qtmips_machine/instructions/arithmetic.cpp b/qtmips_machine/instructions/arithmetic.cpp new file mode 100644 index 0000000..8c8f40e --- /dev/null +++ b/qtmips_machine/instructions/arithmetic.cpp @@ -0,0 +1,87 @@ +#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(); + str.erase(str.begin() + 4); // Drop sa field + switch (this->type) { + case IAT_ADD: + str[0] = "add"; + break; + case IAT_ADDU: + str[0] = "addu"; + break; + case IAT_SUB: + str[0] = "sub"; + break; + case IAT_SUBU: + str[0] = "subu"; + break; + case IAT_AND: + str[0] = "and"; + break; + case IAT_OR: + str[0] = "or"; + break; + case IAT_XOR: + str[0] = "xor"; + break; + case IAT_NOR: + str[0] = "nor"; + break; + case IAT_SLT: + str[0] = "slt"; + break; + case IAT_SLTU: + str[0] = "sltu"; + break; + default: + // TODO different exception + throw std::exception(); + } + return str; +} + +InstructionArithmeticImmediate::InstructionArithmeticImmediate(enum InstructionArithmeticImmediateT type, std::uint8_t rs, std::uint8_t rt, std::uint16_t value) + : InstructionI(rs, rt, value) { + this->type = type; +} + +std::vector InstructionArithmeticImmediate::to_strs() { + std::vector str = this->InstructionI::to_strs(); + switch (this->type) { + case IAT_ADDI: + str[0] = "addi"; + break; + case IAT_ADDIU: + str[0] = "addiu"; + break; + case IAT_ANDI: + str[0] = "andi"; + break; + case IAT_ORI: + str[0] = "ori"; + break; + case IAT_XORI: + str[0] = "xori"; + break; + case IAT_SLTI: + str[0] = "slti"; + break; + case IAT_SLTIU: + str[0] = "sltiu"; + break; + case IAT_LUI: + str[0] = "lu"; + break; + default: + // TODO different exception + throw std::exception(); + } + return str; +} diff --git a/qtmips_machine/instructions/arithmetic.h b/qtmips_machine/instructions/arithmetic.h new file mode 100644 index 0000000..29f89d2 --- /dev/null +++ b/qtmips_machine/instructions/arithmetic.h @@ -0,0 +1,46 @@ +#ifndef ARITHMETIC_H +#define ARITHMETIC_H + +#include "instruction.h" + +enum InstructionArithmeticT { + IAT_ADD, // Add + IAT_ADDU, // Add unsigned + IAT_SUB, // Subtract + IAT_SUBU, // Subtract unsigned + IAT_AND, + IAT_OR, + IAT_XOR, + IAT_NOR, + IAT_SLT, // set on less than + IAT_SLTU, // set on less than unsigned +}; + +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(); +private: + enum InstructionArithmeticT type; +}; + +enum InstructionArithmeticImmediateT { + IAT_ADDI, + IAT_ADDIU, + IAT_ANDI, + IAT_ORI, + IAT_XORI, + IAT_SLTI, + IAT_SLTIU, + IAT_LUI +}; + +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(); +private: + enum InstructionArithmeticImmediateT type; +}; + +#endif // ARITHMETIC_H diff --git a/qtmips_machine/instructions/jumpbranch.cpp b/qtmips_machine/instructions/jumpbranch.cpp new file mode 100644 index 0000000..2ede399 --- /dev/null +++ b/qtmips_machine/instructions/jumpbranch.cpp @@ -0,0 +1,30 @@ +#include "jumpbranch.h" + +InstructionJump::InstructionJump(bool link, std::uint32_t address) + : InstructionJ(address) { + this->link = link; +} + +std::vector InstructionJump::to_strs() { + std::vector str = this->InstructionJ::to_strs(); + if (link) + str[0] = "j"; + else + str[0] = "jal"; + return str; +} + +InstructionJumpRegister::InstructionJumpRegister(bool link, std::uint8_t rs) + : InstructionR(rs, 0, 0, 0) { + this->link = link; +} + +std::vector InstructionJumpRegister::to_strs() { + std::vector str = this->InstructionR::to_strs(); + str.erase(str.begin() + 2, str.end()); // Drop every field after rs + if (link) + str[0] = "j"; + else + str[0] = "jal"; + return str; +} diff --git a/qtmips_machine/instructions/jumpbranch.h b/qtmips_machine/instructions/jumpbranch.h new file mode 100644 index 0000000..b8dee5c --- /dev/null +++ b/qtmips_machine/instructions/jumpbranch.h @@ -0,0 +1,34 @@ +#ifndef JUMPBRANCH_H +#define JUMPBRANCH_H + +#include "instruction.h" + +class InstructionJump : InstructionJ { +public: + InstructionJump(bool link, std::uint32_t address); + std::vector to_strs(); +private: + bool link; +}; + +class InstructionJumpRegister : InstructionR { +public: + InstructionJumpRegister(bool link, std::uint8_t rs); + std::vector to_strs(); +private: + bool link; +}; + +enum InstructionBranchT { + +}; + +class InstructionBranch : InstructionI { +public: + InstructionBranch(); + std::vector to_strs(); +private: + // TODO +}; + +#endif // JUMPBRANCH_H diff --git a/qtmips_machine/instructions/loadstore.cpp b/qtmips_machine/instructions/loadstore.cpp new file mode 100644 index 0000000..c83eae4 --- /dev/null +++ b/qtmips_machine/instructions/loadstore.cpp @@ -0,0 +1,67 @@ +#include "loadstore.h" + +InstructionLoad::InstructionLoad(enum InstructionLoadStoreT type, std::uint8_t rs, std::uint8_t rt, std::uint16_t offset) + : InstructionI(rs, rt, offset) { + this->type = type; +} + +std::vector InstructionLoad::to_strs() { + std::vector str = this->InstructionI::to_strs(); + switch (this->type) { + case ILST_B: + str[0] = "lb"; + break; + case ILST_HW: + str[0] = "lh"; + break; + case ILST_WL: + str[0] = "lwl"; + break; + case ILST_W: + str[0] = "lw"; + break; + case ILST_BU: + str[0] = "lbu"; + break; + case ILST_HU: + str[0] = "lhu"; + break; + case ILST_WR: + str[0] = "lwr"; + break; + default: + // TODO different exception + throw std::exception(); + } + return str; +} + +InstructionStore::InstructionStore(enum InstructionLoadStoreT type, std::uint8_t rs, std::uint8_t rt, std::uint16_t offset) + : InstructionI(rs, rt, offset) { + this->type = type; +} + +std::vector InstructionStore::to_strs() { + std::vector str = this->InstructionI::to_strs(); + switch (this->type) { + case ILST_B: + str[0] = "sb"; + break; + case ILST_HW: + str[0] = "sh"; + break; + case ILST_WL: + str[0] = "swl"; + break; + case ILST_W: + str[0] = "sw"; + break; + case ILST_WR: + str[0] = "swr"; + break; + default: + // TODO different exception + throw std::exception(); + } + return str; +} diff --git a/qtmips_machine/instructions/loadstore.h b/qtmips_machine/instructions/loadstore.h new file mode 100644 index 0000000..9741bd7 --- /dev/null +++ b/qtmips_machine/instructions/loadstore.h @@ -0,0 +1,32 @@ +#ifndef LOADSTORE_H +#define LOADSTORE_H + +#include "instruction.h" + +enum InstructionLoadStoreT { + ILST_B, // Byte + ILST_HW, // Half word + ILST_WL, // Word left + ILST_W, // Word + ILST_BU, // Byte unsigned + ILST_HU, // Half word unsigned + ILST_WR // Word right +}; + +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(); +private: + enum InstructionLoadStoreT type; +}; + +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(); +private: + enum InstructionLoadStoreT type; +}; + +#endif // LOADSTORE_H diff --git a/qtmips_machine/instructions/nop.cpp b/qtmips_machine/instructions/nop.cpp new file mode 100644 index 0000000..5fd5e47 --- /dev/null +++ b/qtmips_machine/instructions/nop.cpp @@ -0,0 +1,7 @@ +#include "nop.h" + +std::vector InstructionNop::to_strs() { + std::vector str; + str.push_back("nop"); + return str; +} diff --git a/qtmips_machine/instructions/nop.h b/qtmips_machine/instructions/nop.h new file mode 100644 index 0000000..b098b11 --- /dev/null +++ b/qtmips_machine/instructions/nop.h @@ -0,0 +1,11 @@ +#ifndef NOP_H +#define NOP_H + +#include "instruction.h" + +class InstructionNop : public Instruction { +public: + std::vector to_strs(); +}; + +#endif // NOP_H diff --git a/qtmips_machine/instructions/shift.cpp b/qtmips_machine/instructions/shift.cpp new file mode 100644 index 0000000..a8c6e41 --- /dev/null +++ b/qtmips_machine/instructions/shift.cpp @@ -0,0 +1,51 @@ +#include "shift.h" + +InstructionShift::InstructionShift(enum InstructionShiftT type, std::uint8_t rt, std::uint8_t rd, std::uint8_t sa) + : InstructionR(0, rt, rd, sa) { + this->type = type; +} + +std::vector InstructionShift::to_strs() { + std::vector str = this->InstructionR::to_strs(); + str.erase(str.begin() + 1); // Drop rs field + switch (this->type) { + case IST_LL: + str[0] = "sll"; + break; + case IST_RL: + str[0] = "srl"; + break; + case IST_RA: + str[0] = "sra"; + break; + default: + // TODO different exception + throw std::exception(); + } + return str; +} + +InstructionShiftVariable::InstructionShiftVariable(enum InstructionShiftT type, std::uint8_t rs, std::uint8_t rt, std::uint8_t rd) + : InstructionR(rs, rt, rd, 0) { + this->type = type; +} + +std::vector InstructionShiftVariable::to_strs() { + std::vector str = this->InstructionR::to_strs(); + str.erase(str.begin() + 4); // Drop sa field + switch (this->type) { + case IST_LL: + str[0] = "sllv"; + break; + case IST_RL: + str[0] = "srlv"; + break; + case IST_RA: + str[0] = "srav"; + break; + default: + // TODO different exception + throw std::exception(); + } + return str; +} diff --git a/qtmips_machine/instructions/shift.h b/qtmips_machine/instructions/shift.h new file mode 100644 index 0000000..9ce29e0 --- /dev/null +++ b/qtmips_machine/instructions/shift.h @@ -0,0 +1,28 @@ +#ifndef SHIFT_H +#define SHIFT_H + +#include "instruction.h" + +enum InstructionShiftT { + IST_LL, // Left logical + IST_RL, // Right logical + IST_RA // Right arithmetic +}; + +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(); +private: + enum InstructionShiftT type; +}; + +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(); +private: + enum InstructionShiftT type; +}; + +#endif // SHIFT_H -- cgit v1.2.3