summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ue4/mycpu/Makefile3
-rw-r--r--ue4/mycpu/ccpu.h14
-rw-r--r--ue4/mycpu/cdatset.h49
-rw-r--r--ue4/mycpu/cinstruction.h8
-rw-r--r--ue4/mycpu/cmem.h11
-rw-r--r--ue4/mycpu/cprogram.h14
-rw-r--r--ue4/mycpu/instructions.h80
-rw-r--r--ue4/mycpu/mycpu.cpp37
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
13BIN= mycpu 13BIN= mycpu
14OBJS= mycpu.o 14OBJS= mycpu.o
15HEADERS= cdat.h cmem.h cinstruction.h instructions.h cprogram.h cdisplay.h displays.h ccpu.h 15HEADERS= 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 */
25template <class T> 25template <class T=CDat<int>, int width=0>
26class CProgram; 26class 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 */
34template <class T> 34template<class T=CDat<int>, int width=0>
35class CCPU 35class 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
253template <class T> 253template<class T, int width>
254CCPU<T>::CCPU(const unsigned cnt) 254CCPU<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
269template <class T> 269template<class T, int width>
270CCPU<T>::~CCPU() 270CCPU<T>::~CCPU()
271{ 271{
272 /* delete registers */ 272 /* delete registers */
@@ -280,7 +280,7 @@ CCPU<T>::~CCPU()
280 280
281/*----------------------------------------------------------------------------*/ 281/*----------------------------------------------------------------------------*/
282 282
283template <class T> 283template<class T, int width>
284void CCPU<T>::run() 284void 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
315template <class T> 315template<class T, int width>
316void CCPU<T>::dumpRegisters(std::ostream& out) 316void 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 */
19template <class T>
20class 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 */
20template <class T> 20template<class T, int >
21class CCPU; 21class 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 */
28template <class T> 28template<class T=CDat<int>, int width=0>
29class CInstruction 29class CInstruction
30{ 30{
31 public: 31 public:
@@ -193,7 +193,7 @@ class CInstruction
193 193
194/*----------------------------------------------------------------------------*/ 194/*----------------------------------------------------------------------------*/
195 195
196template<class T> 196template<class T=CDat<int>, int width=0>
197const unsigned CInstruction<T>::parseRegister(const std::string& str) 197const 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
217template<class T> 217template<class T=CDat<int>, int width=0>
218inline void CInstruction<T>::checkRegister(CCPU<T> *cpu, const unsigned regidx) 218inline 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 */
26template <class T> 27template<class T=CDat<int>, int width=0>
27class CMem 28class 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 */
24template <class T> 24template<class T=CDat<int>, int width=0>
25class CInstruction; 25class 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 */
33template <class T> 33template<class T=CDat<int>, int width=0>
34class CProgram 34class 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
125template <class T> 125template<class T=CDat<int>, int width=0>
126CProgram<T>::CProgram() 126CProgram<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
146template <class T> 146template<class T=CDat<int>, int width=0>
147CProgram<T>::~CProgram() 147CProgram<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
160template <class T> 160template<class T=CDat<int>, int width=0>
161void CProgram<T>::compile(std::istream& in) 161void 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
244template <class T> 244template<class T=CDat<int>, int width=0>
245unsigned CProgram<T>::findLabel(const std::string& label) const 245unsigned 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
257template <class T> 257template<class T=CDat<int>, int width=0>
258void CProgram<T>::dump(std::ostream& out) 258void 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 */
22template <class T> 22template<class T=CDat<int>, int width=0>
23class CInstructionInc 23class CInstructionInc
24 : public CInstruction<T> 24 : public CInstruction<T>
25{ 25{
@@ -45,7 +45,7 @@ class CInstructionInc
45 45
46/*----------------------------------------------------------------------------*/ 46/*----------------------------------------------------------------------------*/
47 47
48template <class T> 48template<class T=CDat<int>, int width=0>
49void CInstructionInc<T>::compile(std::list<std::string>& params) 49void 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
59template <class T> 59template<class T=CDat<int>, int width=0>
60void CInstructionInc<T>::execute(CCPU<T> *cpu) 60void 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 */
77template <class T> 77template<class T=CDat<int>, int width=0>
78class CInstructionDec 78class CInstructionDec
79 : public CInstruction<T> 79 : public CInstruction<T>
80{ 80{
@@ -100,7 +100,7 @@ class CInstructionDec
100 100
101/*----------------------------------------------------------------------------*/ 101/*----------------------------------------------------------------------------*/
102 102
103template <class T> 103template<class T=CDat<int>, int width=0>
104void CInstructionDec<T>::compile(std::list<std::string>& params) 104void 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
114template <class T> 114template<class T=CDat<int>, int width=0>
115void CInstructionDec<T>::execute(CCPU<T> *cpu) 115void 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 */
132template <class T> 132template<class T=CDat<int>, int width=0>
133class CInstructionAdd 133class CInstructionAdd
134 : public CInstruction<T> 134 : public CInstruction<T>
135{ 135{
@@ -159,7 +159,7 @@ class CInstructionAdd
159 159
160/*----------------------------------------------------------------------------*/ 160/*----------------------------------------------------------------------------*/
161 161
162template <class T> 162template<class T=CDat<int>, int width=0>
163void CInstructionAdd<T>::compile(std::list<std::string>& params) 163void 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
177template <class T> 177template<class T=CDat<int>, int width=0>
178void CInstructionAdd<T>::execute(CCPU<T> *cpu) 178void 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 */
198template <class T> 198template<class T=CDat<int>, int width=0>
199class CInstructionSub 199class CInstructionSub
200 : public CInstruction<T> 200 : public CInstruction<T>
201{ 201{
@@ -225,7 +225,7 @@ class CInstructionSub
225 225
226/*----------------------------------------------------------------------------*/ 226/*----------------------------------------------------------------------------*/
227 227
228template <class T> 228template<class T=CDat<int>, int width=0>
229void CInstructionSub<T>::compile(std::list<std::string>& params) 229void 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
243template <class T> 243template<class T=CDat<int>, int width=0>
244void CInstructionSub<T>::execute(CCPU<T> *cpu) 244void 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 */
264template <class T> 264template<class T=CDat<int>, int width=0>
265class CInstructionMul 265class CInstructionMul
266 : public CInstruction<T> 266 : public CInstruction<T>
267{ 267{
@@ -291,7 +291,7 @@ class CInstructionMul
291 291
292/*----------------------------------------------------------------------------*/ 292/*----------------------------------------------------------------------------*/
293 293
294template <class T> 294template<class T=CDat<int>, int width=0>
295void CInstructionMul<T>::compile(std::list<std::string>& params) 295void 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
309template <class T> 309template<class T=CDat<int>, int width=0>
310void CInstructionMul<T>::execute(CCPU<T> *cpu) 310void 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 */
328template <class T> 328template<class T=CDat<int>, int width=0>
329class CInstructionDiv 329class CInstructionDiv
330 : public CInstruction<T> 330 : public CInstruction<T>
331{ 331{
@@ -355,7 +355,7 @@ class CInstructionDiv
355 355
356/*----------------------------------------------------------------------------*/ 356/*----------------------------------------------------------------------------*/
357 357
358template <class T> 358template<class T=CDat<int>, int width=0>
359void CInstructionDiv<T>::compile(std::list<std::string>& params) 359void 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
373template <class T> 373template<class T=CDat<int>, int width=0>
374void CInstructionDiv<T>::execute(CCPU<T> *cpu) 374void 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 */
394template <class T> 394template<class T=CDat<int>, int width=0>
395class CInstructionLoad 395class CInstructionLoad
396 : public CInstruction<T> 396 : public CInstruction<T>
397{ 397{
@@ -419,7 +419,7 @@ class CInstructionLoad
419 419
420/*----------------------------------------------------------------------------*/ 420/*----------------------------------------------------------------------------*/
421 421
422template <class T> 422template<class T=CDat<int>, int width=0>
423void CInstructionLoad<T>::compile(std::list<std::string>& params) 423void 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
435template <class T> 435template<class T=CDat<int>, int width=0>
436void CInstructionLoad<T>::execute(CCPU<T> *cpu) 436void 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 */
456template <class T> 456template<class T=CDat<int>, int width=0>
457class CInstructionStore 457class CInstructionStore
458 : public CInstruction<T> 458 : public CInstruction<T>
459{ 459{
@@ -481,7 +481,7 @@ class CInstructionStore
481 481
482/*----------------------------------------------------------------------------*/ 482/*----------------------------------------------------------------------------*/
483 483
484template <class T> 484template<class T=CDat<int>, int width=0>
485void CInstructionStore<T>::compile(std::list<std::string>& params) 485void 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
497template <class T> 497template<class T=CDat<int>, int width=0>
498void CInstructionStore<T>::execute(CCPU<T> *cpu) 498void 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 */
518template <class T> 518template<class T=CDat<int>, int width=0>
519class CInstructionTest 519class CInstructionTest
520 : public CInstruction<T> 520 : public CInstruction<T>
521{ 521{
@@ -541,7 +541,7 @@ class CInstructionTest
541 541
542/*----------------------------------------------------------------------------*/ 542/*----------------------------------------------------------------------------*/
543 543
544template <class T> 544template<class T=CDat<int>, int width=0>
545void CInstructionTest<T>::compile(std::list<std::string>& params) 545void 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
555template <class T> 555template<class T=CDat<int>, int width=0>
556void CInstructionTest<T>::execute(CCPU<T> *cpu) 556void 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 */
575template <class T> 575template<class T=CDat<int>, int width=0>
576class CInstructionLabel 576class 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 */
607template <class T> 607template<class T=CDat<int>, int width=0>
608class CInstructionJumpA 608class CInstructionJumpA
609 : public CInstruction<T> 609 : public CInstruction<T>
610{ 610{
@@ -630,7 +630,7 @@ class CInstructionJumpA
630 630
631/*----------------------------------------------------------------------------*/ 631/*----------------------------------------------------------------------------*/
632 632
633template <class T> 633template<class T=CDat<int>, int width=0>
634void CInstructionJumpA<T>::compile(std::list<std::string>& params) 634void 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
644template <class T> 644template<class T=CDat<int>, int width=0>
645void CInstructionJumpA<T>::execute(CCPU<T> *cpu) 645void 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 */
664template <class T> 664template<class T=CDat<int>, int width=0>
665class CInstructionJumpZ 665class CInstructionJumpZ
666 : public CInstruction<T> 666 : public CInstruction<T>
667{ 667{
@@ -687,7 +687,7 @@ class CInstructionJumpZ
687 687
688/*----------------------------------------------------------------------------*/ 688/*----------------------------------------------------------------------------*/
689 689
690template <class T> 690template<class T=CDat<int>, int width=0>
691void CInstructionJumpZ<T>::compile(std::list<std::string>& params) 691void 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
701template <class T> 701template<class T=CDat<int>, int width=0>
702void CInstructionJumpZ<T>::execute(CCPU<T> *cpu) 702void 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 */
723template <class T> 723template<class T=CDat<int>, int width=0>
724class CInstructionJumpS 724class CInstructionJumpS
725 : public CInstruction<T> 725 : public CInstruction<T>
726{ 726{
@@ -746,7 +746,7 @@ class CInstructionJumpS
746 746
747/*----------------------------------------------------------------------------*/ 747/*----------------------------------------------------------------------------*/
748 748
749template <class T> 749template<class T=CDat<int>, int width=0>
750void CInstructionJumpS<T>::compile(std::list<std::string>& params) 750void 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
760template <class T> 760template<class T=CDat<int>, int width=0>
761void CInstructionJumpS<T>::execute(CCPU<T> *cpu) 761void 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 */
782template <class T> 782template<class T=CDat<int>, int width=0>
783class CInstructionWrite 783class CInstructionWrite
784 : public CInstruction<T> 784 : public CInstruction<T>
785{ 785{
@@ -808,7 +808,7 @@ class CInstructionWrite
808 808
809/*----------------------------------------------------------------------------*/ 809/*----------------------------------------------------------------------------*/
810 810
811template <class T> 811template<class T=CDat<int>, int width=0>
812void CInstructionWrite<T>::compile(std::list<std::string>& params) 812void 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
824template <class T> 824template<class T=CDat<int>, int width=0>
825void CInstructionWrite<T>::execute(CCPU<T> *cpu) 825void 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 }