diff options
| -rw-r--r-- | ue4/mycpu/Makefile | 3 | ||||
| -rw-r--r-- | ue4/mycpu/ccpu.h | 14 | ||||
| -rw-r--r-- | ue4/mycpu/cdatset.h | 49 | ||||
| -rw-r--r-- | ue4/mycpu/cinstruction.h | 8 | ||||
| -rw-r--r-- | ue4/mycpu/cmem.h | 11 | ||||
| -rw-r--r-- | ue4/mycpu/cprogram.h | 14 | ||||
| -rw-r--r-- | ue4/mycpu/instructions.h | 80 | ||||
| -rw-r--r-- | ue4/mycpu/mycpu.cpp | 37 |
8 files changed, 148 insertions, 68 deletions
diff --git a/ue4/mycpu/Makefile b/ue4/mycpu/Makefile index 8022614..9e6db7a 100644 --- a/ue4/mycpu/Makefile +++ b/ue4/mycpu/Makefile | |||
| @@ -12,7 +12,8 @@ LIBS= -L/usr/local/lib -lboost_program_options | |||
| 12 | 12 | ||
| 13 | BIN= mycpu | 13 | BIN= mycpu |
| 14 | OBJS= mycpu.o | 14 | OBJS= mycpu.o |
| 15 | HEADERS= cdat.h cmem.h cinstruction.h instructions.h cprogram.h cdisplay.h displays.h ccpu.h | 15 | HEADERS= cdat.h cdatn.h cdatset.h cmem.h cinstruction.h instructions.h \ |
| 16 | cprogram.h cdisplay.h displays.h ccpu.h | ||
| 16 | 17 | ||
| 17 | .SUFFIXES: .cpp .o | 18 | .SUFFIXES: .cpp .o |
| 18 | 19 | ||
diff --git a/ue4/mycpu/ccpu.h b/ue4/mycpu/ccpu.h index 519cee9..545b870 100644 --- a/ue4/mycpu/ccpu.h +++ b/ue4/mycpu/ccpu.h | |||
| @@ -22,7 +22,7 @@ | |||
| 22 | #include "cprogram.h" | 22 | #include "cprogram.h" |
| 23 | 23 | ||
| 24 | /* forward declare CProgram */ | 24 | /* forward declare CProgram */ |
| 25 | template <class T> | 25 | template <class T=CDat<int>, int width=0> |
| 26 | class CProgram; | 26 | class CProgram; |
| 27 | 27 | ||
| 28 | /** | 28 | /** |
| @@ -31,7 +31,7 @@ class CProgram; | |||
| 31 | * CPU implementation. Used as a container for memory and instructions. | 31 | * CPU implementation. Used as a container for memory and instructions. |
| 32 | * Implements a run method to execute the program (= the instructions). | 32 | * Implements a run method to execute the program (= the instructions). |
| 33 | */ | 33 | */ |
| 34 | template <class T> | 34 | template<class T=CDat<int>, int width=0> |
| 35 | class CCPU | 35 | class CCPU |
| 36 | { | 36 | { |
| 37 | typedef typename std::set<CDisplay<T> *>::iterator displayiterator; | 37 | typedef typename std::set<CDisplay<T> *>::iterator displayiterator; |
| @@ -250,8 +250,8 @@ class CCPU | |||
| 250 | 250 | ||
| 251 | /*----------------------------------------------------------------------------*/ | 251 | /*----------------------------------------------------------------------------*/ |
| 252 | 252 | ||
| 253 | template <class T> | 253 | template<class T, int width> |
| 254 | CCPU<T>::CCPU(const unsigned cnt) | 254 | CCPU<T, width>::CCPU(const unsigned cnt) |
| 255 | : m_regcnt(cnt), m_memory(NULL), m_program(NULL), m_flagzero(false), m_flagsign(false) | 255 | : m_regcnt(cnt), m_memory(NULL), m_program(NULL), m_flagzero(false), m_flagsign(false) |
| 256 | { | 256 | { |
| 257 | /* create registers */ | 257 | /* create registers */ |
| @@ -266,7 +266,7 @@ CCPU<T>::CCPU(const unsigned cnt) | |||
| 266 | 266 | ||
| 267 | /*----------------------------------------------------------------------------*/ | 267 | /*----------------------------------------------------------------------------*/ |
| 268 | 268 | ||
| 269 | template <class T> | 269 | template<class T, int width> |
| 270 | CCPU<T>::~CCPU() | 270 | CCPU<T>::~CCPU() |
| 271 | { | 271 | { |
| 272 | /* delete registers */ | 272 | /* delete registers */ |
| @@ -280,7 +280,7 @@ CCPU<T>::~CCPU() | |||
| 280 | 280 | ||
| 281 | /*----------------------------------------------------------------------------*/ | 281 | /*----------------------------------------------------------------------------*/ |
| 282 | 282 | ||
| 283 | template <class T> | 283 | template<class T, int width> |
| 284 | void CCPU<T>::run() | 284 | void CCPU<T>::run() |
| 285 | { | 285 | { |
| 286 | if (m_memory == NULL) | 286 | if (m_memory == NULL) |
| @@ -312,7 +312,7 @@ void CCPU<T>::run() | |||
| 312 | /*----------------------------------------------------------------------------*/ | 312 | /*----------------------------------------------------------------------------*/ |
| 313 | 313 | ||
| 314 | #if DEBUG | 314 | #if DEBUG |
| 315 | template <class T> | 315 | template<class T, int width> |
| 316 | void CCPU<T>::dumpRegisters(std::ostream& out) | 316 | void CCPU<T>::dumpRegisters(std::ostream& out) |
| 317 | { | 317 | { |
| 318 | out << "[REGISTER DUMP]" << std::endl; | 318 | out << "[REGISTER DUMP]" << std::endl; |
diff --git a/ue4/mycpu/cdatset.h b/ue4/mycpu/cdatset.h new file mode 100644 index 0000000..03e71f4 --- /dev/null +++ b/ue4/mycpu/cdatset.h | |||
| @@ -0,0 +1,49 @@ | |||
| 1 | /** | ||
| 2 | * @module cdat | ||
| 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) | ||
| 4 | * @brief Datatype template and datatype definition for CCPU and CMem | ||
| 5 | * @date 26.05.2009 | ||
| 6 | */ | ||
| 7 | |||
| 8 | #ifndef CDATSET_H | ||
| 9 | #define CDATSET_H 1 | ||
| 10 | |||
| 11 | #include <boost/operators.hpp> | ||
| 12 | #include <iostream> | ||
| 13 | |||
| 14 | /** | ||
| 15 | * @class CDat | ||
| 16 | * | ||
| 17 | * Datatype template for CCPU and CMem. | ||
| 18 | */ | ||
| 19 | template <class T> | ||
| 20 | class CDatSet | ||
| 21 | : public CDat<T> | ||
| 22 | { | ||
| 23 | public: | ||
| 24 | /** | ||
| 25 | * @method operator>> | ||
| 26 | * @brief Shift/read operator for inputstream | ||
| 27 | * @param stream reference to inputstream | ||
| 28 | * @param cdat reference to object which will be read from stream | ||
| 29 | * @return reference to inputstream | ||
| 30 | * @globalvars none | ||
| 31 | * @exception none | ||
| 32 | * @conditions none | ||
| 33 | */ | ||
| 34 | friend std::istream& operator>>(std::istream & stream, CDatSet& cdat) | ||
| 35 | { | ||
| 36 | std::string s; | ||
| 37 | stream >> s; | ||
| 38 | cdat.m_value = s.size(); | ||
| 39 | return stream; | ||
| 40 | } | ||
| 41 | |||
| 42 | private: | ||
| 43 | /* members */ | ||
| 44 | T m_value; | ||
| 45 | }; | ||
| 46 | |||
| 47 | #endif | ||
| 48 | |||
| 49 | /* vim: set et sw=2 ts=2: */ | ||
diff --git a/ue4/mycpu/cinstruction.h b/ue4/mycpu/cinstruction.h index 8c35d2c..a2e3743 100644 --- a/ue4/mycpu/cinstruction.h +++ b/ue4/mycpu/cinstruction.h | |||
| @@ -17,7 +17,7 @@ | |||
| 17 | #include "ccpu.h" | 17 | #include "ccpu.h" |
| 18 | 18 | ||
| 19 | /* forward declare CCPU */ | 19 | /* forward declare CCPU */ |
| 20 | template <class T> | 20 | template<class T, int > |
| 21 | class CCPU; | 21 | class CCPU; |
| 22 | 22 | ||
| 23 | /** | 23 | /** |
| @@ -25,7 +25,7 @@ class CCPU; | |||
| 25 | * | 25 | * |
| 26 | * Abstract class for displays | 26 | * Abstract class for displays |
| 27 | */ | 27 | */ |
| 28 | template <class T> | 28 | template<class T=CDat<int>, int width=0> |
| 29 | class CInstruction | 29 | class CInstruction |
| 30 | { | 30 | { |
| 31 | public: | 31 | public: |
| @@ -193,7 +193,7 @@ class CInstruction | |||
| 193 | 193 | ||
| 194 | /*----------------------------------------------------------------------------*/ | 194 | /*----------------------------------------------------------------------------*/ |
| 195 | 195 | ||
| 196 | template<class T> | 196 | template<class T=CDat<int>, int width=0> |
| 197 | const unsigned CInstruction<T>::parseRegister(const std::string& str) | 197 | const unsigned CInstruction<T>::parseRegister(const std::string& str) |
| 198 | { | 198 | { |
| 199 | unsigned reg; | 199 | unsigned reg; |
| @@ -214,7 +214,7 @@ const unsigned CInstruction<T>::parseRegister(const std::string& str) | |||
| 214 | 214 | ||
| 215 | /*----------------------------------------------------------------------------*/ | 215 | /*----------------------------------------------------------------------------*/ |
| 216 | 216 | ||
| 217 | template<class T> | 217 | template<class T=CDat<int>, int width=0> |
| 218 | inline void CInstruction<T>::checkRegister(CCPU<T> *cpu, const unsigned regidx) | 218 | inline void CInstruction<T>::checkRegister(CCPU<T> *cpu, const unsigned regidx) |
| 219 | { | 219 | { |
| 220 | assert(cpu != NULL); | 220 | assert(cpu != NULL); |
diff --git a/ue4/mycpu/cmem.h b/ue4/mycpu/cmem.h index 6b23111..c6b8735 100644 --- a/ue4/mycpu/cmem.h +++ b/ue4/mycpu/cmem.h | |||
| @@ -13,6 +13,7 @@ | |||
| 13 | #include <sstream> | 13 | #include <sstream> |
| 14 | #include <stdexcept> | 14 | #include <stdexcept> |
| 15 | #include <boost/lexical_cast.hpp> | 15 | #include <boost/lexical_cast.hpp> |
| 16 | #include <boost/algorithm/string.hpp> | ||
| 16 | #ifdef DEBUG | 17 | #ifdef DEBUG |
| 17 | # include <iostream> | 18 | # include <iostream> |
| 18 | # include <iomanip> | 19 | # include <iomanip> |
| @@ -23,7 +24,7 @@ | |||
| 23 | * | 24 | * |
| 24 | * Extends std::vector template for use as memory for CCPU. | 25 | * Extends std::vector template for use as memory for CCPU. |
| 25 | */ | 26 | */ |
| 26 | template <class T> | 27 | template<class T=CDat<int>, int width=0> |
| 27 | class CMem | 28 | class CMem |
| 28 | : public std::vector<T> | 29 | : public std::vector<T> |
| 29 | { | 30 | { |
| @@ -55,7 +56,8 @@ class CMem | |||
| 55 | { | 56 | { |
| 56 | ++i; | 57 | ++i; |
| 57 | std::getline(in, line); | 58 | std::getline(in, line); |
| 58 | 59 | boost::algorithm::trim(line); | |
| 60 | |||
| 59 | /* skip last line if it's empty */ | 61 | /* skip last line if it's empty */ |
| 60 | if (line.empty() && in.eof()) | 62 | if (line.empty() && in.eof()) |
| 61 | break; | 63 | break; |
| @@ -63,8 +65,9 @@ class CMem | |||
| 63 | T value; | 65 | T value; |
| 64 | try | 66 | try |
| 65 | { | 67 | { |
| 66 | if (!line.empty()) | 68 | if (!line.empty()) |
| 67 | value = boost::lexical_cast<T>(line); | 69 | { |
| 70 | value = boost::lexical_cast<T>(line); | ||
| 68 | } | 71 | } |
| 69 | catch(boost::bad_lexical_cast& ex) | 72 | catch(boost::bad_lexical_cast& ex) |
| 70 | { | 73 | { |
diff --git a/ue4/mycpu/cprogram.h b/ue4/mycpu/cprogram.h index 43193ac..d8f7d5e 100644 --- a/ue4/mycpu/cprogram.h +++ b/ue4/mycpu/cprogram.h | |||
| @@ -21,7 +21,7 @@ | |||
| 21 | #include "instructions.h" | 21 | #include "instructions.h" |
| 22 | 22 | ||
| 23 | /* forward declare CInstruction */ | 23 | /* forward declare CInstruction */ |
| 24 | template <class T> | 24 | template<class T=CDat<int>, int width=0> |
| 25 | class CInstruction; | 25 | class CInstruction; |
| 26 | 26 | ||
| 27 | /** | 27 | /** |
| @@ -30,7 +30,7 @@ class CInstruction; | |||
| 30 | * CProgram extends std::vector and adds a method for parsing | 30 | * CProgram extends std::vector and adds a method for parsing |
| 31 | * programfile. This adds instances of CInstruction to CProgram itself. | 31 | * programfile. This adds instances of CInstruction to CProgram itself. |
| 32 | */ | 32 | */ |
| 33 | template <class T> | 33 | template<class T=CDat<int>, int width=0> |
| 34 | class CProgram | 34 | class CProgram |
| 35 | : public std::vector<CInstruction<T> *> | 35 | : public std::vector<CInstruction<T> *> |
| 36 | { | 36 | { |
| @@ -122,7 +122,7 @@ class CProgram | |||
| 122 | 122 | ||
| 123 | /*----------------------------------------------------------------------------*/ | 123 | /*----------------------------------------------------------------------------*/ |
| 124 | 124 | ||
| 125 | template <class T> | 125 | template<class T=CDat<int>, int width=0> |
| 126 | CProgram<T>::CProgram() | 126 | CProgram<T>::CProgram() |
| 127 | { | 127 | { |
| 128 | m_instrset.insert(new CInstructionInc<T>); | 128 | m_instrset.insert(new CInstructionInc<T>); |
| @@ -143,7 +143,7 @@ CProgram<T>::CProgram() | |||
| 143 | 143 | ||
| 144 | /*----------------------------------------------------------------------------*/ | 144 | /*----------------------------------------------------------------------------*/ |
| 145 | 145 | ||
| 146 | template <class T> | 146 | template<class T=CDat<int>, int width=0> |
| 147 | CProgram<T>::~CProgram() | 147 | CProgram<T>::~CProgram() |
| 148 | { | 148 | { |
| 149 | /* free instruction set */ | 149 | /* free instruction set */ |
| @@ -157,7 +157,7 @@ CProgram<T>::~CProgram() | |||
| 157 | 157 | ||
| 158 | /*----------------------------------------------------------------------------*/ | 158 | /*----------------------------------------------------------------------------*/ |
| 159 | 159 | ||
| 160 | template <class T> | 160 | template<class T=CDat<int>, int width=0> |
| 161 | void CProgram<T>::compile(std::istream& in) | 161 | void CProgram<T>::compile(std::istream& in) |
| 162 | { | 162 | { |
| 163 | if (!in.good()) | 163 | if (!in.good()) |
| @@ -241,7 +241,7 @@ void CProgram<T>::compile(std::istream& in) | |||
| 241 | 241 | ||
| 242 | /*----------------------------------------------------------------------------*/ | 242 | /*----------------------------------------------------------------------------*/ |
| 243 | 243 | ||
| 244 | template <class T> | 244 | template<class T=CDat<int>, int width=0> |
| 245 | unsigned CProgram<T>::findLabel(const std::string& label) const | 245 | unsigned CProgram<T>::findLabel(const std::string& label) const |
| 246 | { | 246 | { |
| 247 | std::map<std::string, unsigned>::const_iterator it; | 247 | std::map<std::string, unsigned>::const_iterator it; |
| @@ -254,7 +254,7 @@ unsigned CProgram<T>::findLabel(const std::string& label) const | |||
| 254 | /*----------------------------------------------------------------------------*/ | 254 | /*----------------------------------------------------------------------------*/ |
| 255 | 255 | ||
| 256 | #if DEBUG | 256 | #if DEBUG |
| 257 | template <class T> | 257 | template<class T=CDat<int>, int width=0> |
| 258 | void CProgram<T>::dump(std::ostream& out) | 258 | void CProgram<T>::dump(std::ostream& out) |
| 259 | { | 259 | { |
| 260 | out << "[PROGRAM DUMP]" << std::endl; | 260 | out << "[PROGRAM DUMP]" << std::endl; |
diff --git a/ue4/mycpu/instructions.h b/ue4/mycpu/instructions.h index fcff3e7..3f2fd51 100644 --- a/ue4/mycpu/instructions.h +++ b/ue4/mycpu/instructions.h | |||
| @@ -19,7 +19,7 @@ | |||
| 19 | * Syntax: inc R1 | 19 | * Syntax: inc R1 |
| 20 | * (R1++) | 20 | * (R1++) |
| 21 | */ | 21 | */ |
| 22 | template <class T> | 22 | template<class T=CDat<int>, int width=0> |
| 23 | class CInstructionInc | 23 | class CInstructionInc |
| 24 | : public CInstruction<T> | 24 | : public CInstruction<T> |
| 25 | { | 25 | { |
| @@ -45,7 +45,7 @@ class CInstructionInc | |||
| 45 | 45 | ||
| 46 | /*----------------------------------------------------------------------------*/ | 46 | /*----------------------------------------------------------------------------*/ |
| 47 | 47 | ||
| 48 | template <class T> | 48 | template<class T=CDat<int>, int width=0> |
| 49 | void CInstructionInc<T>::compile(std::list<std::string>& params) | 49 | void CInstructionInc<T>::compile(std::list<std::string>& params) |
| 50 | { | 50 | { |
| 51 | if (params.size() != 1) | 51 | if (params.size() != 1) |
| @@ -56,7 +56,7 @@ void CInstructionInc<T>::compile(std::list<std::string>& params) | |||
| 56 | 56 | ||
| 57 | /*----------------------------------------------------------------------------*/ | 57 | /*----------------------------------------------------------------------------*/ |
| 58 | 58 | ||
| 59 | template <class T> | 59 | template<class T=CDat<int>, int width=0> |
| 60 | void CInstructionInc<T>::execute(CCPU<T> *cpu) | 60 | void CInstructionInc<T>::execute(CCPU<T> *cpu) |
| 61 | { | 61 | { |
| 62 | assert(cpu != NULL); | 62 | assert(cpu != NULL); |
| @@ -74,7 +74,7 @@ void CInstructionInc<T>::execute(CCPU<T> *cpu) | |||
| 74 | * Syntax: dec R1 | 74 | * Syntax: dec R1 |
| 75 | * (R1--) | 75 | * (R1--) |
| 76 | */ | 76 | */ |
| 77 | template <class T> | 77 | template<class T=CDat<int>, int width=0> |
| 78 | class CInstructionDec | 78 | class CInstructionDec |
| 79 | : public CInstruction<T> | 79 | : public CInstruction<T> |
| 80 | { | 80 | { |
| @@ -100,7 +100,7 @@ class CInstructionDec | |||
| 100 | 100 | ||
| 101 | /*----------------------------------------------------------------------------*/ | 101 | /*----------------------------------------------------------------------------*/ |
| 102 | 102 | ||
| 103 | template <class T> | 103 | template<class T=CDat<int>, int width=0> |
| 104 | void CInstructionDec<T>::compile(std::list<std::string>& params) | 104 | void CInstructionDec<T>::compile(std::list<std::string>& params) |
| 105 | { | 105 | { |
| 106 | if (params.size() != 1) | 106 | if (params.size() != 1) |
| @@ -111,7 +111,7 @@ void CInstructionDec<T>::compile(std::list<std::string>& params) | |||
| 111 | 111 | ||
| 112 | /*----------------------------------------------------------------------------*/ | 112 | /*----------------------------------------------------------------------------*/ |
| 113 | 113 | ||
| 114 | template <class T> | 114 | template<class T=CDat<int>, int width=0> |
| 115 | void CInstructionDec<T>::execute(CCPU<T> *cpu) | 115 | void CInstructionDec<T>::execute(CCPU<T> *cpu) |
| 116 | { | 116 | { |
| 117 | assert(cpu != NULL); | 117 | assert(cpu != NULL); |
| @@ -129,7 +129,7 @@ void CInstructionDec<T>::execute(CCPU<T> *cpu) | |||
| 129 | * Syntax: add R1, R2, R3 | 129 | * Syntax: add R1, R2, R3 |
| 130 | * (R1 = R2 + R3) | 130 | * (R1 = R2 + R3) |
| 131 | */ | 131 | */ |
| 132 | template <class T> | 132 | template<class T=CDat<int>, int width=0> |
| 133 | class CInstructionAdd | 133 | class CInstructionAdd |
| 134 | : public CInstruction<T> | 134 | : public CInstruction<T> |
| 135 | { | 135 | { |
| @@ -159,7 +159,7 @@ class CInstructionAdd | |||
| 159 | 159 | ||
| 160 | /*----------------------------------------------------------------------------*/ | 160 | /*----------------------------------------------------------------------------*/ |
| 161 | 161 | ||
| 162 | template <class T> | 162 | template<class T=CDat<int>, int width=0> |
| 163 | void CInstructionAdd<T>::compile(std::list<std::string>& params) | 163 | void CInstructionAdd<T>::compile(std::list<std::string>& params) |
| 164 | { | 164 | { |
| 165 | if (params.size() != 3) | 165 | if (params.size() != 3) |
| @@ -174,7 +174,7 @@ void CInstructionAdd<T>::compile(std::list<std::string>& params) | |||
| 174 | 174 | ||
| 175 | /*----------------------------------------------------------------------------*/ | 175 | /*----------------------------------------------------------------------------*/ |
| 176 | 176 | ||
| 177 | template <class T> | 177 | template<class T=CDat<int>, int width=0> |
| 178 | void CInstructionAdd<T>::execute(CCPU<T> *cpu) | 178 | void CInstructionAdd<T>::execute(CCPU<T> *cpu) |
| 179 | { | 179 | { |
| 180 | assert(cpu != NULL); | 180 | assert(cpu != NULL); |
| @@ -195,7 +195,7 @@ void CInstructionAdd<T>::execute(CCPU<T> *cpu) | |||
| 195 | * Syntax: sub R1, R2, R3 | 195 | * Syntax: sub R1, R2, R3 |
| 196 | * (R1 = R2 - R3) | 196 | * (R1 = R2 - R3) |
| 197 | */ | 197 | */ |
| 198 | template <class T> | 198 | template<class T=CDat<int>, int width=0> |
| 199 | class CInstructionSub | 199 | class CInstructionSub |
| 200 | : public CInstruction<T> | 200 | : public CInstruction<T> |
| 201 | { | 201 | { |
| @@ -225,7 +225,7 @@ class CInstructionSub | |||
| 225 | 225 | ||
| 226 | /*----------------------------------------------------------------------------*/ | 226 | /*----------------------------------------------------------------------------*/ |
| 227 | 227 | ||
| 228 | template <class T> | 228 | template<class T=CDat<int>, int width=0> |
| 229 | void CInstructionSub<T>::compile(std::list<std::string>& params) | 229 | void CInstructionSub<T>::compile(std::list<std::string>& params) |
| 230 | { | 230 | { |
| 231 | if (params.size() != 3) | 231 | if (params.size() != 3) |
| @@ -240,7 +240,7 @@ void CInstructionSub<T>::compile(std::list<std::string>& params) | |||
| 240 | 240 | ||
| 241 | /*----------------------------------------------------------------------------*/ | 241 | /*----------------------------------------------------------------------------*/ |
| 242 | 242 | ||
| 243 | template <class T> | 243 | template<class T=CDat<int>, int width=0> |
| 244 | void CInstructionSub<T>::execute(CCPU<T> *cpu) | 244 | void CInstructionSub<T>::execute(CCPU<T> *cpu) |
| 245 | { | 245 | { |
| 246 | assert(cpu != NULL); | 246 | assert(cpu != NULL); |
| @@ -261,7 +261,7 @@ void CInstructionSub<T>::execute(CCPU<T> *cpu) | |||
| 261 | * Syntax: mul R1, R2, R3 | 261 | * Syntax: mul R1, R2, R3 |
| 262 | * (R1 = R2 * R3) | 262 | * (R1 = R2 * R3) |
| 263 | */ | 263 | */ |
| 264 | template <class T> | 264 | template<class T=CDat<int>, int width=0> |
| 265 | class CInstructionMul | 265 | class CInstructionMul |
| 266 | : public CInstruction<T> | 266 | : public CInstruction<T> |
| 267 | { | 267 | { |
| @@ -291,7 +291,7 @@ class CInstructionMul | |||
| 291 | 291 | ||
| 292 | /*----------------------------------------------------------------------------*/ | 292 | /*----------------------------------------------------------------------------*/ |
| 293 | 293 | ||
| 294 | template <class T> | 294 | template<class T=CDat<int>, int width=0> |
| 295 | void CInstructionMul<T>::compile(std::list<std::string>& params) | 295 | void CInstructionMul<T>::compile(std::list<std::string>& params) |
| 296 | { | 296 | { |
| 297 | if (params.size() != 3) | 297 | if (params.size() != 3) |
| @@ -306,7 +306,7 @@ void CInstructionMul<T>::compile(std::list<std::string>& params) | |||
| 306 | 306 | ||
| 307 | /*----------------------------------------------------------------------------*/ | 307 | /*----------------------------------------------------------------------------*/ |
| 308 | 308 | ||
| 309 | template <class T> | 309 | template<class T=CDat<int>, int width=0> |
| 310 | void CInstructionMul<T>::execute(CCPU<T> *cpu) | 310 | void CInstructionMul<T>::execute(CCPU<T> *cpu) |
| 311 | { | 311 | { |
| 312 | super::checkRegister(cpu, m_regidx1); | 312 | super::checkRegister(cpu, m_regidx1); |
| @@ -325,7 +325,7 @@ void CInstructionMul<T>::execute(CCPU<T> *cpu) | |||
| 325 | * Syntax: div R1, R2, R3 | 325 | * Syntax: div R1, R2, R3 |
| 326 | * (R1 = R2 / R3) | 326 | * (R1 = R2 / R3) |
| 327 | */ | 327 | */ |
| 328 | template <class T> | 328 | template<class T=CDat<int>, int width=0> |
| 329 | class CInstructionDiv | 329 | class CInstructionDiv |
| 330 | : public CInstruction<T> | 330 | : public CInstruction<T> |
| 331 | { | 331 | { |
| @@ -355,7 +355,7 @@ class CInstructionDiv | |||
| 355 | 355 | ||
| 356 | /*----------------------------------------------------------------------------*/ | 356 | /*----------------------------------------------------------------------------*/ |
| 357 | 357 | ||
| 358 | template <class T> | 358 | template<class T=CDat<int>, int width=0> |
| 359 | void CInstructionDiv<T>::compile(std::list<std::string>& params) | 359 | void CInstructionDiv<T>::compile(std::list<std::string>& params) |
| 360 | { | 360 | { |
| 361 | if (params.size() != 3) | 361 | if (params.size() != 3) |
| @@ -370,7 +370,7 @@ void CInstructionDiv<T>::compile(std::list<std::string>& params) | |||
| 370 | 370 | ||
| 371 | /*----------------------------------------------------------------------------*/ | 371 | /*----------------------------------------------------------------------------*/ |
| 372 | 372 | ||
| 373 | template <class T> | 373 | template<class T=CDat<int>, int width=0> |
| 374 | void CInstructionDiv<T>::execute(CCPU<T> *cpu) | 374 | void CInstructionDiv<T>::execute(CCPU<T> *cpu) |
| 375 | { | 375 | { |
| 376 | assert(cpu != NULL); | 376 | assert(cpu != NULL); |
| @@ -391,7 +391,7 @@ void CInstructionDiv<T>::execute(CCPU<T> *cpu) | |||
| 391 | * Syntax: load R1, R2 | 391 | * Syntax: load R1, R2 |
| 392 | * (R1 = memory[R2]) | 392 | * (R1 = memory[R2]) |
| 393 | */ | 393 | */ |
| 394 | template <class T> | 394 | template<class T=CDat<int>, int width=0> |
| 395 | class CInstructionLoad | 395 | class CInstructionLoad |
| 396 | : public CInstruction<T> | 396 | : public CInstruction<T> |
| 397 | { | 397 | { |
| @@ -419,7 +419,7 @@ class CInstructionLoad | |||
| 419 | 419 | ||
| 420 | /*----------------------------------------------------------------------------*/ | 420 | /*----------------------------------------------------------------------------*/ |
| 421 | 421 | ||
| 422 | template <class T> | 422 | template<class T=CDat<int>, int width=0> |
| 423 | void CInstructionLoad<T>::compile(std::list<std::string>& params) | 423 | void CInstructionLoad<T>::compile(std::list<std::string>& params) |
| 424 | { | 424 | { |
| 425 | if (params.size() != 2) | 425 | if (params.size() != 2) |
| @@ -432,7 +432,7 @@ void CInstructionLoad<T>::compile(std::list<std::string>& params) | |||
| 432 | 432 | ||
| 433 | /*----------------------------------------------------------------------------*/ | 433 | /*----------------------------------------------------------------------------*/ |
| 434 | 434 | ||
| 435 | template <class T> | 435 | template<class T=CDat<int>, int width=0> |
| 436 | void CInstructionLoad<T>::execute(CCPU<T> *cpu) | 436 | void CInstructionLoad<T>::execute(CCPU<T> *cpu) |
| 437 | { | 437 | { |
| 438 | assert(cpu != NULL); | 438 | assert(cpu != NULL); |
| @@ -453,7 +453,7 @@ void CInstructionLoad<T>::execute(CCPU<T> *cpu) | |||
| 453 | * Syntax: store R1, R2 | 453 | * Syntax: store R1, R2 |
| 454 | * (memory[R2] = R1) | 454 | * (memory[R2] = R1) |
| 455 | */ | 455 | */ |
| 456 | template <class T> | 456 | template<class T=CDat<int>, int width=0> |
| 457 | class CInstructionStore | 457 | class CInstructionStore |
| 458 | : public CInstruction<T> | 458 | : public CInstruction<T> |
| 459 | { | 459 | { |
| @@ -481,7 +481,7 @@ class CInstructionStore | |||
| 481 | 481 | ||
| 482 | /*----------------------------------------------------------------------------*/ | 482 | /*----------------------------------------------------------------------------*/ |
| 483 | 483 | ||
| 484 | template <class T> | 484 | template<class T=CDat<int>, int width=0> |
| 485 | void CInstructionStore<T>::compile(std::list<std::string>& params) | 485 | void CInstructionStore<T>::compile(std::list<std::string>& params) |
| 486 | { | 486 | { |
| 487 | if (params.size() != 2) | 487 | if (params.size() != 2) |
| @@ -494,7 +494,7 @@ void CInstructionStore<T>::compile(std::list<std::string>& params) | |||
| 494 | 494 | ||
| 495 | /*----------------------------------------------------------------------------*/ | 495 | /*----------------------------------------------------------------------------*/ |
| 496 | 496 | ||
| 497 | template <class T> | 497 | template<class T=CDat<int>, int width=0> |
| 498 | void CInstructionStore<T>::execute(CCPU<T> *cpu) | 498 | void CInstructionStore<T>::execute(CCPU<T> *cpu) |
| 499 | { | 499 | { |
| 500 | assert(cpu != NULL); | 500 | assert(cpu != NULL); |
| @@ -515,7 +515,7 @@ void CInstructionStore<T>::execute(CCPU<T> *cpu) | |||
| 515 | * Syntax: test R1 | 515 | * Syntax: test R1 |
| 516 | * (R1 == 0: zeroflag: true, R1 < 0: signflag: true) | 516 | * (R1 == 0: zeroflag: true, R1 < 0: signflag: true) |
| 517 | */ | 517 | */ |
| 518 | template <class T> | 518 | template<class T=CDat<int>, int width=0> |
| 519 | class CInstructionTest | 519 | class CInstructionTest |
| 520 | : public CInstruction<T> | 520 | : public CInstruction<T> |
| 521 | { | 521 | { |
| @@ -541,7 +541,7 @@ class CInstructionTest | |||
| 541 | 541 | ||
| 542 | /*----------------------------------------------------------------------------*/ | 542 | /*----------------------------------------------------------------------------*/ |
| 543 | 543 | ||
| 544 | template <class T> | 544 | template<class T=CDat<int>, int width=0> |
| 545 | void CInstructionTest<T>::compile(std::list<std::string>& params) | 545 | void CInstructionTest<T>::compile(std::list<std::string>& params) |
| 546 | { | 546 | { |
| 547 | if (params.size() != 1) | 547 | if (params.size() != 1) |
| @@ -552,7 +552,7 @@ void CInstructionTest<T>::compile(std::list<std::string>& params) | |||
| 552 | 552 | ||
| 553 | /*----------------------------------------------------------------------------*/ | 553 | /*----------------------------------------------------------------------------*/ |
| 554 | 554 | ||
| 555 | template <class T> | 555 | template<class T=CDat<int>, int width=0> |
| 556 | void CInstructionTest<T>::execute(CCPU<T> *cpu) | 556 | void CInstructionTest<T>::execute(CCPU<T> *cpu) |
| 557 | { | 557 | { |
| 558 | assert(cpu != NULL); | 558 | assert(cpu != NULL); |
| @@ -572,7 +572,7 @@ void CInstructionTest<T>::execute(CCPU<T> *cpu) | |||
| 572 | * Implementation of assembler command "label" | 572 | * Implementation of assembler command "label" |
| 573 | * Syntax: label name: | 573 | * Syntax: label name: |
| 574 | */ | 574 | */ |
| 575 | template <class T> | 575 | template<class T=CDat<int>, int width=0> |
| 576 | class CInstructionLabel | 576 | class CInstructionLabel |
| 577 | : public CInstruction<T> | 577 | : public CInstruction<T> |
| 578 | { | 578 | { |
| @@ -604,7 +604,7 @@ class CInstructionLabel | |||
| 604 | * Syntax: jumpa labelname | 604 | * Syntax: jumpa labelname |
| 605 | * (jump to labelname) | 605 | * (jump to labelname) |
| 606 | */ | 606 | */ |
| 607 | template <class T> | 607 | template<class T=CDat<int>, int width=0> |
| 608 | class CInstructionJumpA | 608 | class CInstructionJumpA |
| 609 | : public CInstruction<T> | 609 | : public CInstruction<T> |
| 610 | { | 610 | { |
| @@ -630,7 +630,7 @@ class CInstructionJumpA | |||
| 630 | 630 | ||
| 631 | /*----------------------------------------------------------------------------*/ | 631 | /*----------------------------------------------------------------------------*/ |
| 632 | 632 | ||
| 633 | template <class T> | 633 | template<class T=CDat<int>, int width=0> |
| 634 | void CInstructionJumpA<T>::compile(std::list<std::string>& params) | 634 | void CInstructionJumpA<T>::compile(std::list<std::string>& params) |
| 635 | { | 635 | { |
| 636 | if (params.size() != 1) | 636 | if (params.size() != 1) |
| @@ -641,7 +641,7 @@ void CInstructionJumpA<T>::compile(std::list<std::string>& params) | |||
| 641 | 641 | ||
| 642 | /*----------------------------------------------------------------------------*/ | 642 | /*----------------------------------------------------------------------------*/ |
| 643 | 643 | ||
| 644 | template <class T> | 644 | template<class T=CDat<int>, int width=0> |
| 645 | void CInstructionJumpA<T>::execute(CCPU<T> *cpu) | 645 | void CInstructionJumpA<T>::execute(CCPU<T> *cpu) |
| 646 | { | 646 | { |
| 647 | assert(cpu != NULL); | 647 | assert(cpu != NULL); |
| @@ -661,7 +661,7 @@ void CInstructionJumpA<T>::execute(CCPU<T> *cpu) | |||
| 661 | * Syntax: jumpz labelname | 661 | * Syntax: jumpz labelname |
| 662 | * (jump to labelname if zeroflag) | 662 | * (jump to labelname if zeroflag) |
| 663 | */ | 663 | */ |
| 664 | template <class T> | 664 | template<class T=CDat<int>, int width=0> |
| 665 | class CInstructionJumpZ | 665 | class CInstructionJumpZ |
| 666 | : public CInstruction<T> | 666 | : public CInstruction<T> |
| 667 | { | 667 | { |
| @@ -687,7 +687,7 @@ class CInstructionJumpZ | |||
| 687 | 687 | ||
| 688 | /*----------------------------------------------------------------------------*/ | 688 | /*----------------------------------------------------------------------------*/ |
| 689 | 689 | ||
| 690 | template <class T> | 690 | template<class T=CDat<int>, int width=0> |
| 691 | void CInstructionJumpZ<T>::compile(std::list<std::string>& params) | 691 | void CInstructionJumpZ<T>::compile(std::list<std::string>& params) |
| 692 | { | 692 | { |
| 693 | if (params.size() != 1) | 693 | if (params.size() != 1) |
| @@ -698,7 +698,7 @@ void CInstructionJumpZ<T>::compile(std::list<std::string>& params) | |||
| 698 | 698 | ||
| 699 | /*----------------------------------------------------------------------------*/ | 699 | /*----------------------------------------------------------------------------*/ |
| 700 | 700 | ||
| 701 | template <class T> | 701 | template<class T=CDat<int>, int width=0> |
| 702 | void CInstructionJumpZ<T>::execute(CCPU<T> *cpu) | 702 | void CInstructionJumpZ<T>::execute(CCPU<T> *cpu) |
| 703 | { | 703 | { |
| 704 | assert(cpu != NULL); | 704 | assert(cpu != NULL); |
| @@ -720,7 +720,7 @@ void CInstructionJumpZ<T>::execute(CCPU<T> *cpu) | |||
| 720 | * Syntax: jumps labelname | 720 | * Syntax: jumps labelname |
| 721 | * (jump to labelname if signflag) | 721 | * (jump to labelname if signflag) |
| 722 | */ | 722 | */ |
| 723 | template <class T> | 723 | template<class T=CDat<int>, int width=0> |
| 724 | class CInstructionJumpS | 724 | class CInstructionJumpS |
| 725 | : public CInstruction<T> | 725 | : public CInstruction<T> |
| 726 | { | 726 | { |
| @@ -746,7 +746,7 @@ class CInstructionJumpS | |||
| 746 | 746 | ||
| 747 | /*----------------------------------------------------------------------------*/ | 747 | /*----------------------------------------------------------------------------*/ |
| 748 | 748 | ||
| 749 | template <class T> | 749 | template<class T=CDat<int>, int width=0> |
| 750 | void CInstructionJumpS<T>::compile(std::list<std::string>& params) | 750 | void CInstructionJumpS<T>::compile(std::list<std::string>& params) |
| 751 | { | 751 | { |
| 752 | if (params.size() != 1) | 752 | if (params.size() != 1) |
| @@ -757,7 +757,7 @@ void CInstructionJumpS<T>::compile(std::list<std::string>& params) | |||
| 757 | 757 | ||
| 758 | /*----------------------------------------------------------------------------*/ | 758 | /*----------------------------------------------------------------------------*/ |
| 759 | 759 | ||
| 760 | template <class T> | 760 | template<class T=CDat<int>, int width=0> |
| 761 | void CInstructionJumpS<T>::execute(CCPU<T> *cpu) | 761 | void CInstructionJumpS<T>::execute(CCPU<T> *cpu) |
| 762 | { | 762 | { |
| 763 | assert(cpu != NULL); | 763 | assert(cpu != NULL); |
| @@ -779,7 +779,7 @@ void CInstructionJumpS<T>::execute(CCPU<T> *cpu) | |||
| 779 | * Syntax: write DEV, R1 | 779 | * Syntax: write DEV, R1 |
| 780 | * (write R1 to DEV, which is a name of a display) | 780 | * (write R1 to DEV, which is a name of a display) |
| 781 | */ | 781 | */ |
| 782 | template <class T> | 782 | template<class T=CDat<int>, int width=0> |
| 783 | class CInstructionWrite | 783 | class CInstructionWrite |
| 784 | : public CInstruction<T> | 784 | : public CInstruction<T> |
| 785 | { | 785 | { |
| @@ -808,7 +808,7 @@ class CInstructionWrite | |||
| 808 | 808 | ||
| 809 | /*----------------------------------------------------------------------------*/ | 809 | /*----------------------------------------------------------------------------*/ |
| 810 | 810 | ||
| 811 | template <class T> | 811 | template<class T=CDat<int>, int width=0> |
| 812 | void CInstructionWrite<T>::compile(std::list<std::string>& params) | 812 | void CInstructionWrite<T>::compile(std::list<std::string>& params) |
| 813 | { | 813 | { |
| 814 | if (params.size() != 2) | 814 | if (params.size() != 2) |
| @@ -821,7 +821,7 @@ void CInstructionWrite<T>::compile(std::list<std::string>& params) | |||
| 821 | 821 | ||
| 822 | /*----------------------------------------------------------------------------*/ | 822 | /*----------------------------------------------------------------------------*/ |
| 823 | 823 | ||
| 824 | template <class T> | 824 | template<class T=CDat<int>, int width=0> |
| 825 | void CInstructionWrite<T>::execute(CCPU<T> *cpu) | 825 | void CInstructionWrite<T>::execute(CCPU<T> *cpu) |
| 826 | { | 826 | { |
| 827 | assert(cpu != NULL); | 827 | assert(cpu != NULL); |
diff --git a/ue4/mycpu/mycpu.cpp b/ue4/mycpu/mycpu.cpp index 6c9f71a..2d73177 100644 --- a/ue4/mycpu/mycpu.cpp +++ b/ue4/mycpu/mycpu.cpp | |||
| @@ -17,6 +17,8 @@ | |||
| 17 | #include <stdexcept> | 17 | #include <stdexcept> |
| 18 | #include <stdlib.h> | 18 | #include <stdlib.h> |
| 19 | #include "cdat.h" | 19 | #include "cdat.h" |
| 20 | #include "cdatn.h" | ||
| 21 | #include "cdatset.h" | ||
| 20 | #include "cmem.h" | 22 | #include "cmem.h" |
| 21 | #include "cprogram.h" | 23 | #include "cprogram.h" |
| 22 | #include "ccpu.h" | 24 | #include "ccpu.h" |
| @@ -73,7 +75,32 @@ int main(int argc, char* argv[]) | |||
| 73 | } | 75 | } |
| 74 | 76 | ||
| 75 | /* create memory and optionally initialize memory from file */ | 77 | /* create memory and optionally initialize memory from file */ |
| 76 | CMem<CDat<int> > memory; | 78 | |
| 79 | CMem *memory = NULL; | ||
| 80 | if (vm.count("format")) | ||
| 81 | { | ||
| 82 | string format(vm["format"].as<string>()); | ||
| 83 | if(format == "s") | ||
| 84 | memory = new CMem<CDatSet<int> >(); | ||
| 85 | else | ||
| 86 | { | ||
| 87 | try | ||
| 88 | { | ||
| 89 | int bc = boost::lexical_cast<int>(format); | ||
| 90 | if (bc > 1 && bc < 33) | ||
| 91 | memory = new CMem<CDatN<int> >(); | ||
| 92 | } | ||
| 93 | catch(boost::bad_lexical_cast& ex) | ||
| 94 | { | ||
| 95 | std::stringstream sstr; | ||
| 96 | sstr << "Illegal format: (" << format << "): " << ex.what(); | ||
| 97 | throw std::runtime_error(sstr.str()); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | else | ||
| 102 | |||
| 103 | |||
| 77 | if (vm.count("memory")) | 104 | if (vm.count("memory")) |
| 78 | { | 105 | { |
| 79 | string memoryfile(vm["memory"].as<string>()); | 106 | string memoryfile(vm["memory"].as<string>()); |
| @@ -86,7 +113,7 @@ int main(int argc, char* argv[]) | |||
| 86 | 113 | ||
| 87 | try | 114 | try |
| 88 | { | 115 | { |
| 89 | memory.initialize(file); | 116 | memory->initialize(file); |
| 90 | file.close(); | 117 | file.close(); |
| 91 | } | 118 | } |
| 92 | catch(runtime_error& ex) | 119 | catch(runtime_error& ex) |
| @@ -98,7 +125,7 @@ int main(int argc, char* argv[]) | |||
| 98 | } | 125 | } |
| 99 | 126 | ||
| 100 | #if DEBUG | 127 | #if DEBUG |
| 101 | memory.dump(cerr); | 128 | memory->dump(cerr); |
| 102 | #endif | 129 | #endif |
| 103 | } | 130 | } |
| 104 | 131 | ||
| @@ -134,7 +161,7 @@ int main(int argc, char* argv[]) | |||
| 134 | try | 161 | try |
| 135 | { | 162 | { |
| 136 | CCPU<CDat<int> > cpu(256); | 163 | CCPU<CDat<int> > cpu(256); |
| 137 | cpu.setMemory(&memory); | 164 | cpu.setMemory(memory); |
| 138 | cpu.setProgram(&program); | 165 | cpu.setProgram(&program); |
| 139 | cpu.run(); | 166 | cpu.run(); |
| 140 | #if DEBUG | 167 | #if DEBUG |
| @@ -146,7 +173,7 @@ int main(int argc, char* argv[]) | |||
| 146 | cerr << me << ": Error while executing program:" << endl | 173 | cerr << me << ": Error while executing program:" << endl |
| 147 | << " " << ex.what() << endl; | 174 | << " " << ex.what() << endl; |
| 148 | #if DEBUG | 175 | #if DEBUG |
| 149 | memory.dump(cerr); | 176 | memory->dump(cerr); |
| 150 | #endif | 177 | #endif |
| 151 | return 1; | 178 | return 1; |
| 152 | } | 179 | } |
