aboutsummaryrefslogtreecommitdiff
path: root/qtmips_machine/instructions
diff options
context:
space:
mode:
authorKarel Kočí <cynerd@email.cz>2017-08-30 21:37:53 +0200
committerKarel Kočí <cynerd@email.cz>2017-08-30 21:42:02 +0200
commit9cf92379d5fcf0076c25dae0935daab446c992cd (patch)
treedd09a2e996db1e5a8117f01bec76f1e93eaca6e1 /qtmips_machine/instructions
downloadqtmips-9cf92379d5fcf0076c25dae0935daab446c992cd.tar.gz
qtmips-9cf92379d5fcf0076c25dae0935daab446c992cd.tar.bz2
qtmips-9cf92379d5fcf0076c25dae0935daab446c992cd.zip
Initial commit
Adding work done so far.
Diffstat (limited to 'qtmips_machine/instructions')
-rw-r--r--qtmips_machine/instructions/arithmetic.cpp87
-rw-r--r--qtmips_machine/instructions/arithmetic.h46
-rw-r--r--qtmips_machine/instructions/jumpbranch.cpp30
-rw-r--r--qtmips_machine/instructions/jumpbranch.h34
-rw-r--r--qtmips_machine/instructions/loadstore.cpp67
-rw-r--r--qtmips_machine/instructions/loadstore.h32
-rw-r--r--qtmips_machine/instructions/nop.cpp7
-rw-r--r--qtmips_machine/instructions/nop.h11
-rw-r--r--qtmips_machine/instructions/shift.cpp51
-rw-r--r--qtmips_machine/instructions/shift.h28
10 files changed, 393 insertions, 0 deletions
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 <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();
+ 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<std::string> InstructionArithmeticImmediate::to_strs() {
+ std::vector<std::string> 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<std::string> 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<std::string> 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<std::string> InstructionJump::to_strs() {
+ std::vector<std::string> 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<std::string> InstructionJumpRegister::to_strs() {
+ std::vector<std::string> 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<std::string> to_strs();
+private:
+ bool link;
+};
+
+class InstructionJumpRegister : InstructionR {
+public:
+ InstructionJumpRegister(bool link, std::uint8_t rs);
+ std::vector<std::string> to_strs();
+private:
+ bool link;
+};
+
+enum InstructionBranchT {
+
+};
+
+class InstructionBranch : InstructionI {
+public:
+ InstructionBranch();
+ std::vector<std::string> 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<std::string> InstructionLoad::to_strs() {
+ std::vector<std::string> 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<std::string> InstructionStore::to_strs() {
+ std::vector<std::string> 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<std::string> 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<std::string> 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<std::string> InstructionNop::to_strs() {
+ std::vector<std::string> 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<std::string> 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<std::string> InstructionShift::to_strs() {
+ std::vector<std::string> 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<std::string> InstructionShiftVariable::to_strs() {
+ std::vector<std::string> 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<std::string> 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<std::string> to_strs();
+private:
+ enum InstructionShiftT type;
+};
+
+#endif // SHIFT_H