summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGünther Neuwirth <e0626638@student.tuwien.ac.at>2009-05-12 19:53:07 +0200
committerGünther Neuwirth <e0626638@student.tuwien.ac.at>2009-05-12 19:53:07 +0200
commite41c92320aef4a54b7f3b323f7302b180019b6d7 (patch)
treeda2fba968ad9e2a997b8497c32929c885eb95558
parent34483e07a0548d32651cda4ca4282f3cf8cae870 (diff)
downloadooprog-e41c92320aef4a54b7f3b323f7302b180019b6d7.tar.gz
ooprog-e41c92320aef4a54b7f3b323f7302b180019b6d7.tar.bz2
ooprog-e41c92320aef4a54b7f3b323f7302b180019b6d7.zip
some changes
-rw-r--r--ue3/mycpu/ccpu.cpp93
-rw-r--r--ue3/mycpu/ccpu.h5
-rw-r--r--ue3/mycpu/cinstruction.cpp22
-rw-r--r--ue3/mycpu/cinstruction.h85
-rw-r--r--ue3/mycpu/cmem.cpp5
-rw-r--r--ue3/mycpu/cmem.h3
-rw-r--r--ue3/mycpu/test/test_prog2
7 files changed, 104 insertions, 111 deletions
diff --git a/ue3/mycpu/ccpu.cpp b/ue3/mycpu/ccpu.cpp
index 72ad96c..978f1f7 100644
--- a/ue3/mycpu/ccpu.cpp
+++ b/ue3/mycpu/ccpu.cpp
@@ -21,77 +21,56 @@ using namespace boost;
21CCPU::CCPU(const std::string& progfile, const std::string& memfile) 21CCPU::CCPU(const std::string& progfile, const std::string& memfile)
22 : m_program(progfile), m_memory(memfile) 22 : m_program(progfile), m_memory(memfile)
23{ 23{
24 m_instrtype["inc"] = new CInc(); 24 f_zero = false;
25 m_instrtype["dec"] = new CDec(); 25 f_sign = false;
26 m_instrtype["add"] = new CAdd(); 26 m_instrHandler["inc"] = new CInc();
27 m_instrtype["sub"] = new CSub(); 27 m_instrHandler["dec"] = new CDec();
28 m_instrtype["mul"] = new CMul(); 28 m_instrHandler["add"] = new CAdd();
29 m_instrtype["div"] = new CDiv(); 29 m_instrHandler["sub"] = new CSub();
30 m_instrtype["load"] = new CLoad(); 30 m_instrHandler["mul"] = new CMul();
31 m_instrtype["store"] = new CStore(); 31 m_instrHandler["div"] = new CDiv();
32 m_instrtype["test"] = new CTest(); 32 m_instrHandler["load"] = new CLoad();
33 m_instrtype["label"] = new CLabel(); 33 m_instrHandler["store"] = new CStore();
34 m_instrtype["jumpa"] = new CJumpa(m_program.getJumpAddrs()); 34 m_instrHandler["test"] = new CTest(f_zero, f_sign);
35 m_instrtype["jumpz"] = new CJumpz(m_program.getJumpAddrs()); 35 m_instrHandler["label"] = new CLabel();
36 m_instrtype["jumps"] = new CJumps(m_program.getJumpAddrs()); 36 m_instrHandler["jumpa"] = new CJumpa(m_program.getJumpAddrs());
37 m_instrtype["write"] = new CWrite(); 37 m_instrHandler["jumpz"] = new CJumpz(f_zero, f_sign, m_program.getJumpAddrs());
38 m_instrHandler["jumps"] = new CJumps(f_zero, f_sign, m_program.getJumpAddrs());
39 m_instrHandler["write"] = new CWrite();
38} 40}
39 41
40 42CCPU::~CCPU()
43{
44 std::map<std::string, CInstruction *>::iterator it;
45 for (it = m_instrHandler.begin(); it != m_instrHandler.end(); it++)
46 delete (*it).second ;
47}
41void CCPU::proceed() 48void CCPU::proceed()
42{ 49{
43 50
44 while (m_memory.getRegister("R0") < m_program.getMaxProgramCount()) 51 while (m_memory.getRegister("R0") < m_program.getMaxProgramCount())
45 { 52 {
46 std::vector<std::string>& i_list = m_program.getInstruction( 53 std::vector<std::string>& i_list = m_program.getInstruction(
47 m_memory.getRegister("R0") 54 m_memory.getRegister("R0")
48 ); 55 );
49 56
50 /* switch (m_instrtype[i_list[0]])
51 {
52 case INC:
53 cout << "fick mich"<< endl<<endl;
54 break;
55 case DEC:
56 break;
57 case ADD:
58 break;
59 case SUB:
60 cout << "sub"<< endl<<endl;
61 break;
62 case MUL:
63 break;
64 case DIV:
65 break;
66 case LOAD:
67 cout << "load"<< endl<<endl;
68 break;
69 case STORE:
70 break;
71 case TEST:
72 break;
73 case LABEL:
74 break;
75 case JUMPA:
76 break;
77 case JUMPZ:
78 break;
79 case JUMPS:
80 break;
81 default:
82 break;
83 } */
84 57
85 for(int i = 0; i < (int)i_list.size(); i++) 58
59 m_instrHandler[i_list[0]]->exec(m_memory, i_list);
60/* for(int i = 0; i < (int)i_list.size(); i++)
86 cout << i_list[i] << " "; 61 cout << i_list[i] << " ";
87 m_memory.getRegister("R0")++; 62 if (i_list[0] == "load")
88 cout << m_memory.getRegister("R0")<< endl<<endl; 63 {
64
65 cout << m_memory.getRegister(i_list[1])<<" "<<m_memory.getRegister("R255")<<endl;
66 break;
67 }*/
68 m_memory.getRegister("R0")++;
69// cout << m_memory.getRegister("R0")<<endl;
70 // cout<<endl;
89 } 71 }
72
90} 73}
91 74
92void CCPU::execInstruction(CInstruction& instr, vector<string>& i_list)
93{
94
95 75
96}
97 76
diff --git a/ue3/mycpu/ccpu.h b/ue3/mycpu/ccpu.h
index 6b16ba3..68a4b8d 100644
--- a/ue3/mycpu/ccpu.h
+++ b/ue3/mycpu/ccpu.h
@@ -52,8 +52,7 @@ class CCPU
52 * @exception none 52 * @exception none
53 * @conditions none 53 * @conditions none
54 */ 54 */
55 ~CCPU() 55 ~CCPU();
56 {}
57 56
58 57
59 void proceed(); 58 void proceed();
@@ -66,7 +65,7 @@ class CCPU
66 65
67 CProgram m_program; 66 CProgram m_program;
68 CMem m_memory; 67 CMem m_memory;
69 std::map<std::string, CInstruction *> m_instrtype; 68 std::map<std::string, CInstruction *> m_instrHandler;
70 bool f_zero, f_sign; 69 bool f_zero, f_sign;
71 // std::string m_memfiler; 70 // std::string m_memfiler;
72 71
diff --git a/ue3/mycpu/cinstruction.cpp b/ue3/mycpu/cinstruction.cpp
index 2c8dedf..3cbb033 100644
--- a/ue3/mycpu/cinstruction.cpp
+++ b/ue3/mycpu/cinstruction.cpp
@@ -1,4 +1,5 @@
1#include <iostream> 1#include <iostream>
2#include <sstream>
2#include <vector> 3#include <vector>
3#include <map> 4#include <map>
4#include "cinstruction.h" 5#include "cinstruction.h"
@@ -28,6 +29,7 @@ void CSub::exec(CMem& mem, vector<string>& instr)
28{ 29{
29 mem.getRegister(instr[1]) = mem.getRegister(instr[2]) - 30 mem.getRegister(instr[1]) = mem.getRegister(instr[2]) -
30 mem.getRegister(instr[3]); 31 mem.getRegister(instr[3]);
32
31} 33}
32 34
33 35
@@ -47,7 +49,9 @@ void CDiv::exec(CMem& mem, vector<string>& instr)
47 49
48void CLoad::exec(CMem& mem, vector<string>& instr) 50void CLoad::exec(CMem& mem, vector<string>& instr)
49{ 51{
50 mem.getRegister(instr[1]) = mem.getMem(instr[2]); 52 istringstream stmp ("22");
53 stmp >> mem.getRegister(instr[1]);
54 cout <<mem.getRegister(instr[1])<<" "<< mem.getMem(instr[2])<<endl;
51} 55}
52 56
53 57
@@ -57,17 +61,18 @@ void CStore::exec(CMem& mem, vector<string>& instr)
57} 61}
58 62
59 63
60void CTest::test(CMem& mem, vector<string>& instr, bool& f_zero, bool& f_sign) 64void CTest::exec(CMem& mem, vector<string>& instr)
61{ 65{
62 if(mem.getRegister(instr[1]) == 0) 66 if(mem.getRegister(instr[1]) == 0)
63 f_zero = true; 67 f_zero = true;
64 else 68 else
65 f_zero = false; 69 f_zero = false;
66 70
67 if(mem.getRegister(instr[1]) <= 0) 71 if(mem.getRegister(instr[1]) < 0)
68 f_sign = true; 72 f_sign = true;
69 else 73 else
70 f_sign = false; 74 f_sign = false;
75
71} 76}
72 77
73 78
@@ -76,26 +81,27 @@ void CTest::test(CMem& mem, vector<string>& instr, bool& f_zero, bool& f_sign)
76 81
77void CJumpa::exec(CMem& mem, vector<string>& instr) 82void CJumpa::exec(CMem& mem, vector<string>& instr)
78{ 83{
79 mem.getRegister(instr[0]) = (int) m_jumpaddr[instr[1]]; 84 mem.getRegister("R0") = (int) m_jumpaddr[instr[1]];
80} 85}
81 86
82 87
83void CJumpz::exec(CMem& mem, vector<string>& instr, bool& f_zero) 88void CJumpz::exec(CMem& mem, vector<string>& instr)
84{ 89{
85 if(f_zero) 90 if(f_zero)
86 mem.getRegister(instr[0]) = (int) m_jumpaddr[instr[1]]; 91 mem.getRegister("R0") = (int) m_jumpaddr[instr[1]];
87} 92}
88 93
89 94
90void CJumps::exec(CMem& mem, vector<string>& instr, bool& f_sign) 95void CJumps::exec(CMem& mem, vector<string>& instr)
91{ 96{
92 if(f_sign) 97 if(f_sign)
93 mem.getRegister(instr[0]) = (int) m_jumpaddr[instr[1]]; 98 mem.getRegister("R0") = (int) m_jumpaddr[instr[1]];
94} 99}
95 100
96 101
97void CWrite::exec(CMem& mem, vector<string>& instr) 102void CWrite::exec(CMem& mem, vector<string>& instr)
98{ 103{
104
99 if(instr[1] == "WDEZ") 105 if(instr[1] == "WDEZ")
100 cout << mem.getRegister(instr[2]) << endl; 106 cout << mem.getRegister(instr[2]) << endl;
101 else if (instr[1] == "WHEX") 107 else if (instr[1] == "WHEX")
diff --git a/ue3/mycpu/cinstruction.h b/ue3/mycpu/cinstruction.h
index d393e09..7d929a5 100644
--- a/ue3/mycpu/cinstruction.h
+++ b/ue3/mycpu/cinstruction.h
@@ -30,8 +30,11 @@ class CInstruction
30{ 30{
31 public: 31 public:
32 32
33 33 CInstruction()
34 34 {}
35 CInstruction(std::map<std::string, unsigned int>& jumpaddr)
36 : m_jumpaddr(jumpaddr)
37 {}
35 38
36 /** 39 /**
37 * @method ~CInstruction 40 * @method ~CInstruction
@@ -48,7 +51,8 @@ class CInstruction
48 51
49 virtual void exec(CMem& mem, std::vector<std::string>& instr) = 0; 52 virtual void exec(CMem& mem, std::vector<std::string>& instr) = 0;
50 53
51 54 protected:
55 std::map<std::string, unsigned int> m_jumpaddr;
52 56
53 57
54}; 58};
@@ -59,12 +63,18 @@ class CInstruction
59class CFlagInstruction : public CInstruction 63class CFlagInstruction : public CInstruction
60{ 64{
61 public: 65 public:
66 public:
67 CFlagInstruction(bool& zero, bool& sign)
68 : f_zero(zero), f_sign(sign)
69 {}
70 CFlagInstruction(bool& zero, bool& sign, std::map<std::string, unsigned int>& jumpaddr)
71 : CInstruction::CInstruction(jumpaddr), f_zero(zero), f_sign(sign)
72 {}
73 virtual void exec(CMem& mem, std::vector<std::string>& instr) = 0;
62 74
63 void exec(CMem& mem, std::vector<std::string>& instr) 75 protected:
64 {} 76 bool &f_zero, &f_sign;
65 virtual void exec(CMem& mem, std::vector<std::string>& instr, bool& flag) = 0; 77
66
67
68}; 78};
69 79
70 80
@@ -72,14 +82,14 @@ class CFlagInstruction : public CInstruction
72class CInc : public CInstruction 82class CInc : public CInstruction
73{ 83{
74 public: 84 public:
75 virtual void exec(CMem& mem, std::vector<std::string>& instr); 85 void exec(CMem& mem, std::vector<std::string>& instr);
76}; 86};
77 87
78 88
79class CDec : public CInstruction 89class CDec : public CInstruction
80{ 90{
81 public: 91 public:
82 virtual void exec(CMem& mem, std::vector<std::string>& instr); 92 void exec(CMem& mem, std::vector<std::string>& instr);
83}; 93};
84 94
85 95
@@ -87,7 +97,7 @@ class CDec : public CInstruction
87class CAdd : public CInstruction 97class CAdd : public CInstruction
88{ 98{
89 public: 99 public:
90 virtual void exec(CMem& mem, std::vector<std::string>& instr); 100 void exec(CMem& mem, std::vector<std::string>& instr);
91}; 101};
92 102
93 103
@@ -95,7 +105,7 @@ class CAdd : public CInstruction
95class CSub : public CInstruction 105class CSub : public CInstruction
96{ 106{
97 public: 107 public:
98 virtual void exec(CMem& mem, std::vector<std::string>& instr); 108 void exec(CMem& mem, std::vector<std::string>& instr);
99}; 109};
100 110
101 111
@@ -103,7 +113,7 @@ class CSub : public CInstruction
103class CMul : public CInstruction 113class CMul : public CInstruction
104{ 114{
105 public: 115 public:
106 virtual void exec(CMem& mem, std::vector<std::string>& instr); 116 void exec(CMem& mem, std::vector<std::string>& instr);
107}; 117};
108 118
109 119
@@ -111,7 +121,7 @@ class CMul : public CInstruction
111class CDiv : public CInstruction 121class CDiv : public CInstruction
112{ 122{
113 public: 123 public:
114 virtual void exec(CMem& mem, std::vector<std::string>& instr); 124 void exec(CMem& mem, std::vector<std::string>& instr);
115}; 125};
116 126
117 127
@@ -119,7 +129,7 @@ class CDiv : public CInstruction
119class CLoad : public CInstruction 129class CLoad : public CInstruction
120{ 130{
121 public: 131 public:
122 virtual void exec(CMem& mem, std::vector<std::string>& instr); 132 void exec(CMem& mem, std::vector<std::string>& instr);
123}; 133};
124 134
125 135
@@ -127,16 +137,17 @@ class CLoad : public CInstruction
127class CStore : public CInstruction 137class CStore : public CInstruction
128{ 138{
129 public: 139 public:
130 virtual void exec(CMem& mem, std::vector<std::string>& instr); 140 void exec(CMem& mem, std::vector<std::string>& instr);
131}; 141};
132 142
133class CTest : public CFlagInstruction 143class CTest : public CFlagInstruction
134{ 144{
135 public: 145 public:
136 void exec(CMem& mem, std::vector<std::string>& instr, bool& flag) 146 CTest(bool& zero, bool& sign)
147 : CFlagInstruction::CFlagInstruction(zero, sign)
137 {} 148 {}
138 virtual void test(CMem& mem, std::vector<std::string>& instr, 149
139 bool& f_zero, bool& f_sign); 150 void exec(CMem& mem, std::vector<std::string>& instr);
140}; 151};
141 152
142 153
@@ -152,36 +163,32 @@ class CLabel : public CInstruction
152class CJumpa : public CInstruction 163class CJumpa : public CInstruction
153{ 164{
154 public: 165 public:
155 CJumpa(std::map<std::string, unsigned int>& jumpaddr) 166 CJumpa(std::map<std::string, unsigned int>& jumpaddr)
156 : m_jumpaddr(jumpaddr) 167 : CInstruction::CInstruction(jumpaddr)
157 {} 168 {}
158 virtual void exec(CMem& mem, std::vector<std::string>& instr); 169 void exec(CMem& mem, std::vector<std::string>& instr);
159 protected: 170
160 std::map<std::string, unsigned int> m_jumpaddr;
161}; 171};
162 172
163 173
164class CJumpz : public CFlagInstruction 174class CJumpz : public CFlagInstruction
165{ 175{
176
166 public: 177 public:
167 CJumpz(std::map<std::string, unsigned int>& jumpaddr) 178 CJumpz(bool& zero, bool& sign, std::map<std::string, unsigned int>& jumpaddr)
168 : m_jumpaddr(jumpaddr) 179 : CFlagInstruction::CFlagInstruction(zero, sign, jumpaddr)
169 {} 180 {}
170 virtual void exec(CMem& mem, std::vector<std::string>& instr, bool& f_zero); 181 void exec(CMem& mem, std::vector<std::string>& instr);
171 protected:
172 std::map<std::string, unsigned int> m_jumpaddr;
173}; 182};
174 183
175 184
176class CJumps : public CFlagInstruction 185class CJumps : public CFlagInstruction
177{ 186{
178 public: 187 public:
179 CJumps(std::map<std::string, unsigned int>& jumpaddr) 188 CJumps(bool& zero ,bool& sign, std::map<std::string, unsigned int>& jumpaddr)
180 : m_jumpaddr(jumpaddr) 189 : CFlagInstruction::CFlagInstruction(zero, sign, jumpaddr)
181 {} 190 {}
182 virtual void exec(CMem& mem, std::vector<std::string>& instr, bool& f_sign); 191 void exec(CMem& mem, std::vector<std::string>& instr);
183 protected:
184 std::map<std::string, unsigned int> m_jumpaddr;
185}; 192};
186 193
187 194
@@ -189,7 +196,7 @@ class CJumps : public CFlagInstruction
189class CWrite : public CInstruction 196class CWrite : public CInstruction
190{ 197{
191 public: 198 public:
192 virtual void exec(CMem& mem, std::vector<std::string>& instr); 199 void exec(CMem& mem, std::vector<std::string>& instr);
193}; 200};
194 201
195#endif 202#endif
diff --git a/ue3/mycpu/cmem.cpp b/ue3/mycpu/cmem.cpp
index d27f74e..ec60b56 100644
--- a/ue3/mycpu/cmem.cpp
+++ b/ue3/mycpu/cmem.cpp
@@ -46,9 +46,10 @@ CDat& CMem::getRegister(const string reg)
46 46
47 // if (regnr >= MAX_REGISTER ) 47 // if (regnr >= MAX_REGISTER )
48 48
49 if (regnr == m_registers.size()) 49 if (regnr >= m_registers.size())
50 { 50 {
51 m_registers.push_back(CDat((int)0)); 51 for ( int i = m_registers.size(); i <= (int)regnr; i++)
52 m_registers.push_back(CDat((int)0));
52 return m_registers[m_registers.size() - 1]; 53 return m_registers[m_registers.size() - 1];
53 } 54 }
54 55
diff --git a/ue3/mycpu/cmem.h b/ue3/mycpu/cmem.h
index 923ed01..06fa876 100644
--- a/ue3/mycpu/cmem.h
+++ b/ue3/mycpu/cmem.h
@@ -62,7 +62,8 @@ class CMem
62 62
63 CDat& getMem(const std::string addr) 63 CDat& getMem(const std::string addr)
64 { 64 {
65 return getRegister("R0"); 65 getRegister("R255") = 10;
66 return getRegister("R255");
66 } 67 }
67 void setMem(const std::string addr, const CDat& value) 68 void setMem(const std::string addr, const CDat& value)
68 {} 69 {}
diff --git a/ue3/mycpu/test/test_prog b/ue3/mycpu/test/test_prog
index 7c5f929..74dac21 100644
--- a/ue3/mycpu/test/test_prog
+++ b/ue3/mycpu/test/test_prog
@@ -1,5 +1,5 @@
1# set R2 = 10 1# set R2 = 10
2load R1, R2 2load R2, R1
3 3
4# start of loop 4# start of loop
5label Loop: 5label Loop: