summaryrefslogtreecommitdiffstats
path: root/ue4/mycpu/cinstruction.h
diff options
context:
space:
mode:
Diffstat (limited to 'ue4/mycpu/cinstruction.h')
-rw-r--r--ue4/mycpu/cinstruction.h91
1 files changed, 64 insertions, 27 deletions
diff --git a/ue4/mycpu/cinstruction.h b/ue4/mycpu/cinstruction.h
index a2e3743..85122b4 100644
--- a/ue4/mycpu/cinstruction.h
+++ b/ue4/mycpu/cinstruction.h
@@ -1,7 +1,7 @@
1/** 1/**
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 instructions
5 * @date 26.05.2009 5 * @date 26.05.2009
6 */ 6 */
7 7
@@ -11,21 +11,46 @@
11#include <iostream> 11#include <iostream>
12#include <list> 12#include <list>
13#include <sstream> 13#include <sstream>
14#include <stdexcept>
15#include <boost/lexical_cast.hpp> 14#include <boost/lexical_cast.hpp>
16#include <assert.h> 15#include <assert.h>
16#include <stdexcept>
17
18/**
19 * @class CInstructionError
20 *
21 * Exception thrown by implemententations of CInstruction
22 */
23class CInstructionError
24 : public std::invalid_argument
25{
26 public:
27 /**
28 * @method CInstructionError
29 * @brief Default exception ctor
30 * @param what message to pass along
31 * @return -
32 * @globalvars none
33 * @exception none
34 * @pre none
35 * @post none
36 */
37 CInstructionError(const std::string& what)
38 : std::invalid_argument(what)
39 {}
40};
41
17#include "ccpu.h" 42#include "ccpu.h"
18 43
19/* forward declare CCPU */ 44/* forward declare CCPU */
20template<class T, int > 45template <class T>
21class CCPU; 46class CCPU;
22 47
23/** 48/**
24 * @class CInstruction 49 * @class CInstruction
25 * 50 *
26 * Abstract class for displays 51 * Abstract class for instructions
27 */ 52 */
28template<class T=CDat<int>, int width=0> 53template <class T>
29class CInstruction 54class CInstruction
30{ 55{
31 public: 56 public:
@@ -36,7 +61,8 @@ class CInstruction
36 * @return - 61 * @return -
37 * @globalvars none 62 * @globalvars none
38 * @exception none 63 * @exception none
39 * @conditions none 64 * @pre none
65 * @post none
40 */ 66 */
41 CInstruction(std::string name) 67 CInstruction(std::string name)
42 : m_name(name) 68 : m_name(name)
@@ -49,7 +75,8 @@ class CInstruction
49 * @return - 75 * @return -
50 * @globalvars none 76 * @globalvars none
51 * @exception none 77 * @exception none
52 * @conditions none 78 * @pre none
79 * @post none
53 */ 80 */
54 virtual ~CInstruction() 81 virtual ~CInstruction()
55 {} 82 {}
@@ -61,7 +88,8 @@ class CInstruction
61 * @return true if instructionname is name 88 * @return true if instructionname is name
62 * @globalvars none 89 * @globalvars none
63 * @exception none 90 * @exception none
64 * @conditions none 91 * @pre none
92 * @post none
65 */ 93 */
66 virtual bool operator==(std::string& name) 94 virtual bool operator==(std::string& name)
67 { 95 {
@@ -74,8 +102,9 @@ class CInstruction
74 * @param cpu pointer to cpu 102 * @param cpu pointer to cpu
75 * @return - 103 * @return -
76 * @globalvars none 104 * @globalvars none
77 * @exception std::runtime_error 105 * @exception CInstructionError
78 * @conditions none 106 * @pre none
107 * @post none
79 */ 108 */
80 virtual CInstruction& operator()(CCPU<T> *cpu) 109 virtual CInstruction& operator()(CCPU<T> *cpu)
81 { 110 {
@@ -90,7 +119,8 @@ class CInstruction
90 * @return name of instruction 119 * @return name of instruction
91 * @globalvars none 120 * @globalvars none
92 * @exception none 121 * @exception none
93 * @conditions none 122 * @pre none
123 * @post none
94 */ 124 */
95 virtual const std::string& getName() 125 virtual const std::string& getName()
96 { 126 {
@@ -104,7 +134,8 @@ class CInstruction
104 * @return reference to outputstream 134 * @return reference to outputstream
105 * @globalvars none 135 * @globalvars none
106 * @exception none 136 * @exception none
107 * @conditions none 137 * @pre none
138 * @post none
108 */ 139 */
109 virtual std::ostream& dump(std::ostream& stream) 140 virtual std::ostream& dump(std::ostream& stream)
110 { 141 {
@@ -120,7 +151,8 @@ class CInstruction
120 * @return reference to outputstream 151 * @return reference to outputstream
121 * @globalvars none 152 * @globalvars none
122 * @exception none 153 * @exception none
123 * @conditions none 154 * @pre none
155 * @post none
124 */ 156 */
125 friend std::ostream& operator<<(std::ostream& stream, CInstruction& instr) 157 friend std::ostream& operator<<(std::ostream& stream, CInstruction& instr)
126 { 158 {
@@ -133,8 +165,9 @@ class CInstruction
133 * @param str register in assembler syntax 165 * @param str register in assembler syntax
134 * @return registernumber 166 * @return registernumber
135 * @globalvars none 167 * @globalvars none
136 * @exception std::runtime_error 168 * @exception CInstructionError
137 * @conditions none 169 * @pre none
170 * @post none
138 */ 171 */
139 virtual const unsigned parseRegister(const std::string& str); 172 virtual const unsigned parseRegister(const std::string& str);
140 173
@@ -146,8 +179,9 @@ class CInstruction
146 * @param regidx registernumber 179 * @param regidx registernumber
147 * @return - 180 * @return -
148 * @globalvars none 181 * @globalvars none
149 * @exception std::runtime_error 182 * @exception CInstructionError
150 * @conditions none 183 * @pre none
184 * @post none
151 */ 185 */
152 virtual void checkRegister(CCPU<T> *cpu, const unsigned regidx); 186 virtual void checkRegister(CCPU<T> *cpu, const unsigned regidx);
153 187
@@ -158,7 +192,8 @@ class CInstruction
158 * @return new instruction instance 192 * @return new instruction instance
159 * @globalvars none 193 * @globalvars none
160 * @exception none 194 * @exception none
161 * @conditions none 195 * @pre none
196 * @post none
162 */ 197 */
163 virtual CInstruction *factory() = 0; 198 virtual CInstruction *factory() = 0;
164 199
@@ -169,8 +204,9 @@ class CInstruction
169 * @param params list of parameters of this instruction 204 * @param params list of parameters of this instruction
170 * @return - 205 * @return -
171 * @globalvars none 206 * @globalvars none
172 * @exception std::runtime_error 207 * @exception CInstructionError
173 * @conditions none 208 * @pre none
209 * @post none
174 */ 210 */
175 virtual void compile(std::list<std::string>& params) = 0; 211 virtual void compile(std::list<std::string>& params) = 0;
176 212
@@ -180,8 +216,9 @@ class CInstruction
180 * @param cpu pointer to cpu 216 * @param cpu pointer to cpu
181 * @return - 217 * @return -
182 * @globalvars none 218 * @globalvars none
183 * @exception std::runtime_error 219 * @exception CInstructionError
184 * @conditions none 220 * @pre none
221 * @post none
185 */ 222 */
186 virtual void execute(CCPU<T> *cpu) = 0; 223 virtual void execute(CCPU<T> *cpu) = 0;
187 224
@@ -193,12 +230,12 @@ class CInstruction
193 230
194/*----------------------------------------------------------------------------*/ 231/*----------------------------------------------------------------------------*/
195 232
196template<class T=CDat<int>, int width=0> 233template<class T>
197const unsigned CInstruction<T>::parseRegister(const std::string& str) 234const unsigned CInstruction<T>::parseRegister(const std::string& str)
198{ 235{
199 unsigned reg; 236 unsigned reg;
200 if (str.length() < 2 || str[0] != 'r') 237 if (str.length() < 2 || str[0] != 'r')
201 throw std::runtime_error("Invalid syntax of register"); 238 throw CInstructionError("Invalid syntax of register");
202 239
203 try 240 try
204 { 241 {
@@ -206,7 +243,7 @@ const unsigned CInstruction<T>::parseRegister(const std::string& str)
206 } 243 }
207 catch(boost::bad_lexical_cast& ex) 244 catch(boost::bad_lexical_cast& ex)
208 { 245 {
209 throw std::runtime_error("Invalid syntax of register"); 246 throw CInstructionError("Invalid syntax of register");
210 } 247 }
211 248
212 return reg; 249 return reg;
@@ -214,7 +251,7 @@ const unsigned CInstruction<T>::parseRegister(const std::string& str)
214 251
215/*----------------------------------------------------------------------------*/ 252/*----------------------------------------------------------------------------*/
216 253
217template<class T=CDat<int>, int width=0> 254template<class T>
218inline void CInstruction<T>::checkRegister(CCPU<T> *cpu, const unsigned regidx) 255inline void CInstruction<T>::checkRegister(CCPU<T> *cpu, const unsigned regidx)
219{ 256{
220 assert(cpu != NULL); 257 assert(cpu != NULL);
@@ -222,7 +259,7 @@ inline void CInstruction<T>::checkRegister(CCPU<T> *cpu, const unsigned regidx)
222 { 259 {
223 std::stringstream sstr; 260 std::stringstream sstr;
224 sstr << "Register R" << regidx << " doesn't exist (out of bound)"; 261 sstr << "Register R" << regidx << " doesn't exist (out of bound)";
225 throw std::runtime_error(sstr.str()); 262 throw CInstructionError(sstr.str());
226 } 263 }
227} 264}
228 265