diff options
Diffstat (limited to 'ue4/mycpu/cinstruction.h')
| -rw-r--r-- | ue4/mycpu/cinstruction.h | 50 |
1 files changed, 46 insertions, 4 deletions
diff --git a/ue4/mycpu/cinstruction.h b/ue4/mycpu/cinstruction.h index 4cc69de..8c35d2c 100644 --- a/ue4/mycpu/cinstruction.h +++ b/ue4/mycpu/cinstruction.h | |||
| @@ -2,7 +2,7 @@ | |||
| 2 | * @module cinstruction | 2 | * @module cinstruction |
| 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) | 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) |
| 4 | * @brief Abstract class for displays | 4 | * @brief Abstract class for displays |
| 5 | * @date 13.05.2009 | 5 | * @date 26.05.2009 |
| 6 | */ | 6 | */ |
| 7 | 7 | ||
| 8 | #ifndef CINSTRUCTION_H | 8 | #ifndef CINSTRUCTION_H |
| @@ -10,8 +10,14 @@ | |||
| 10 | 10 | ||
| 11 | #include <iostream> | 11 | #include <iostream> |
| 12 | #include <list> | 12 | #include <list> |
| 13 | #include <sstream> | ||
| 14 | #include <stdexcept> | ||
| 15 | #include <boost/lexical_cast.hpp> | ||
| 16 | #include <assert.h> | ||
| 17 | #include "ccpu.h" | ||
| 13 | 18 | ||
| 14 | /* forward declare CCPU */ | 19 | /* forward declare CCPU */ |
| 20 | template <class T> | ||
| 15 | class CCPU; | 21 | class CCPU; |
| 16 | 22 | ||
| 17 | /** | 23 | /** |
| @@ -19,6 +25,7 @@ class CCPU; | |||
| 19 | * | 25 | * |
| 20 | * Abstract class for displays | 26 | * Abstract class for displays |
| 21 | */ | 27 | */ |
| 28 | template <class T> | ||
| 22 | class CInstruction | 29 | class CInstruction |
| 23 | { | 30 | { |
| 24 | public: | 31 | public: |
| @@ -70,7 +77,7 @@ class CInstruction | |||
| 70 | * @exception std::runtime_error | 77 | * @exception std::runtime_error |
| 71 | * @conditions none | 78 | * @conditions none |
| 72 | */ | 79 | */ |
| 73 | virtual CInstruction& operator()(CCPU *cpu) | 80 | virtual CInstruction& operator()(CCPU<T> *cpu) |
| 74 | { | 81 | { |
| 75 | execute(cpu); | 82 | execute(cpu); |
| 76 | return *this; | 83 | return *this; |
| @@ -142,7 +149,7 @@ class CInstruction | |||
| 142 | * @exception std::runtime_error | 149 | * @exception std::runtime_error |
| 143 | * @conditions none | 150 | * @conditions none |
| 144 | */ | 151 | */ |
| 145 | virtual void checkRegister(CCPU *cpu, const unsigned regidx); | 152 | virtual void checkRegister(CCPU<T> *cpu, const unsigned regidx); |
| 146 | 153 | ||
| 147 | /** | 154 | /** |
| 148 | * @method factory | 155 | * @method factory |
| @@ -176,7 +183,7 @@ class CInstruction | |||
| 176 | * @exception std::runtime_error | 183 | * @exception std::runtime_error |
| 177 | * @conditions none | 184 | * @conditions none |
| 178 | */ | 185 | */ |
| 179 | virtual void execute(CCPU *cpu) = 0; | 186 | virtual void execute(CCPU<T> *cpu) = 0; |
| 180 | 187 | ||
| 181 | protected: | 188 | protected: |
| 182 | /* members */ | 189 | /* members */ |
| @@ -184,6 +191,41 @@ class CInstruction | |||
| 184 | std::string m_name; | 191 | std::string m_name; |
| 185 | }; | 192 | }; |
| 186 | 193 | ||
| 194 | /*----------------------------------------------------------------------------*/ | ||
| 195 | |||
| 196 | template<class T> | ||
| 197 | const unsigned CInstruction<T>::parseRegister(const std::string& str) | ||
| 198 | { | ||
| 199 | unsigned reg; | ||
| 200 | if (str.length() < 2 || str[0] != 'r') | ||
| 201 | throw std::runtime_error("Invalid syntax of register"); | ||
| 202 | |||
| 203 | try | ||
| 204 | { | ||
| 205 | reg = boost::lexical_cast<unsigned>(str.substr(1)); | ||
| 206 | } | ||
| 207 | catch(boost::bad_lexical_cast& ex) | ||
| 208 | { | ||
| 209 | throw std::runtime_error("Invalid syntax of register"); | ||
| 210 | } | ||
| 211 | |||
| 212 | return reg; | ||
| 213 | } | ||
| 214 | |||
| 215 | /*----------------------------------------------------------------------------*/ | ||
| 216 | |||
| 217 | template<class T> | ||
| 218 | inline void CInstruction<T>::checkRegister(CCPU<T> *cpu, const unsigned regidx) | ||
| 219 | { | ||
| 220 | assert(cpu != NULL); | ||
| 221 | if (regidx >= cpu->getRegisterCount()) | ||
| 222 | { | ||
| 223 | std::stringstream sstr; | ||
| 224 | sstr << "Register R" << regidx << " doesn't exist (out of bound)"; | ||
| 225 | throw std::runtime_error(sstr.str()); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 187 | #endif | 229 | #endif |
| 188 | 230 | ||
| 189 | /* vim: set et sw=2 ts=2: */ | 231 | /* vim: set et sw=2 ts=2: */ |
