diff options
Diffstat (limited to 'ue3/mycpu/cinstruction.cpp')
| -rw-r--r-- | ue3/mycpu/cinstruction.cpp | 132 |
1 files changed, 35 insertions, 97 deletions
diff --git a/ue3/mycpu/cinstruction.cpp b/ue3/mycpu/cinstruction.cpp index 12c5c74..a766015 100644 --- a/ue3/mycpu/cinstruction.cpp +++ b/ue3/mycpu/cinstruction.cpp | |||
| @@ -1,110 +1,48 @@ | |||
| 1 | #include <iostream> | 1 | /** |
| 2 | * @module cinstruction | ||
| 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) | ||
| 4 | * @brief Abstract class for displays | ||
| 5 | * @date 13.05.2009 | ||
| 6 | */ | ||
| 7 | |||
| 2 | #include <sstream> | 8 | #include <sstream> |
| 3 | #include <vector> | 9 | #include <stdexcept> |
| 4 | #include <map> | 10 | #include <boost/lexical_cast.hpp> |
| 11 | #include <assert.h> | ||
| 5 | #include "cinstruction.h" | 12 | #include "cinstruction.h" |
| 6 | #include "cmem.h" | 13 | #include "ccpu.h" |
| 7 | using namespace std; | ||
| 8 | |||
| 9 | void CInc::exec(CMem& mem, vector<string>& instr) | ||
| 10 | { | ||
| 11 | mem.getRegister(instr[1])++; | ||
| 12 | } | ||
| 13 | |||
| 14 | |||
| 15 | void CDec::exec(CMem& mem, vector<string>& instr) | ||
| 16 | { | ||
| 17 | mem.getRegister(instr[1])--; | ||
| 18 | } | ||
| 19 | |||
| 20 | |||
| 21 | void CAdd::exec(CMem& mem, vector<string>& instr) | ||
| 22 | { | ||
| 23 | mem.getRegister(instr[1]) = mem.getRegister(instr[2]) + | ||
| 24 | mem.getRegister(instr[3]); | ||
| 25 | } | ||
| 26 | |||
| 27 | |||
| 28 | void CSub::exec(CMem& mem, vector<string>& instr) | ||
| 29 | { | ||
| 30 | mem.getRegister(instr[1]) = mem.getRegister(instr[2]) - | ||
| 31 | mem.getRegister(instr[3]); | ||
| 32 | |||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | void CMul::exec(CMem& mem, vector<string>& instr) | ||
| 37 | { | ||
| 38 | mem.getRegister(instr[1]) = mem.getRegister(instr[2]) * | ||
| 39 | mem.getRegister(instr[3]); | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | void CDiv::exec(CMem& mem, vector<string>& instr) | ||
| 44 | { | ||
| 45 | mem.getRegister(instr[1]) = mem.getRegister(instr[2]) / | ||
| 46 | mem.getRegister(instr[3]); | ||
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | void CLoad::exec(CMem& mem, vector<string>& instr) | ||
| 51 | { | ||
| 52 | istringstream stmp (mem.getMemAt(instr[2])); | ||
| 53 | stmp >> mem.getRegister(instr[1]); | ||
| 54 | |||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 58 | void CStore::exec(CMem& mem, vector<string>& instr) | ||
| 59 | { | ||
| 60 | mem.setMemAt(instr[2], mem.getRegister(instr[1])); | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | void CTest::exec(CMem& mem, vector<string>& instr) | ||
| 65 | { | ||
| 66 | if(mem.getRegister(instr[1]) == 0) | ||
| 67 | f_zero = true; | ||
| 68 | else | ||
| 69 | f_zero = false; | ||
| 70 | |||
| 71 | if(mem.getRegister(instr[1]) < 0) | ||
| 72 | f_sign = true; | ||
| 73 | else | ||
| 74 | f_sign = false; | ||
| 75 | |||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | |||
| 80 | |||
| 81 | |||
| 82 | void CJumpa::exec(CMem& mem, vector<string>& instr) | ||
| 83 | { | ||
| 84 | mem.getRegister("R0") = (int) m_jumpaddr[instr[1]]; | ||
| 85 | } | ||
| 86 | 14 | ||
| 15 | using namespace std; | ||
| 87 | 16 | ||
| 88 | void CJumpz::exec(CMem& mem, vector<string>& instr) | 17 | const unsigned CInstruction::parseRegister(const std::string& str) |
| 89 | { | 18 | { |
| 90 | if(f_zero) | 19 | unsigned reg; |
| 91 | mem.getRegister("R0") = (int) m_jumpaddr[instr[1]]; | 20 | if (str.length() < 2 || str[0] != 'r') |
| 92 | } | 21 | throw runtime_error("Invalid syntax of register"); |
| 93 | 22 | ||
| 23 | try | ||
| 24 | { | ||
| 25 | reg = boost::lexical_cast<unsigned>(str.substr(1)); | ||
| 26 | } | ||
| 27 | catch(boost::bad_lexical_cast& ex) | ||
| 28 | { | ||
| 29 | throw runtime_error("Invalid syntax of register"); | ||
| 30 | } | ||
| 94 | 31 | ||
| 95 | void CJumps::exec(CMem& mem, vector<string>& instr) | 32 | return reg; |
| 96 | { | ||
| 97 | if(f_sign) | ||
| 98 | mem.getRegister("R0") = (int) m_jumpaddr[instr[1]]; | ||
| 99 | } | 33 | } |
| 100 | 34 | ||
| 35 | /*----------------------------------------------------------------------------*/ | ||
| 101 | 36 | ||
| 102 | void CWrite::exec(CMem& mem, vector<string>& instr) | 37 | inline void CInstruction::checkRegister(CCPU *cpu, const unsigned regidx) |
| 103 | { | 38 | { |
| 104 | 39 | assert(cpu != NULL); | |
| 105 | if(instr[1] == "WDEZ") | 40 | if (regidx >= cpu->getRegisterCount()) |
| 106 | cout << mem.getRegister(instr[2]) << endl; | 41 | { |
| 107 | else if (instr[1] == "WHEX") | 42 | stringstream sstr; |
| 108 | cout << hex << mem.getRegister(instr[2]) << endl; | 43 | sstr << "Register R" << regidx << " doesn't exist (out of bound)"; |
| 44 | throw runtime_error(sstr.str()); | ||
| 45 | } | ||
| 109 | } | 46 | } |
| 110 | 47 | ||
| 48 | /* vim: set et sw=2 ts=2: */ | ||
