summaryrefslogtreecommitdiffstats
path: root/ue3/mycpu/cinstruction.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ue3/mycpu/cinstruction.cpp')
-rw-r--r--ue3/mycpu/cinstruction.cpp132
1 files changed, 35 insertions, 97 deletions
diff --git a/ue3/mycpu/cinstruction.cpp b/ue3/mycpu/cinstruction.cpp
index 12c5c74..a766015 100644
--- a/ue3/mycpu/cinstruction.cpp
+++ b/ue3/mycpu/cinstruction.cpp
@@ -1,110 +1,48 @@
1#include <iostream> 1/**
2 * @module cinstruction
3 * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348)
4 * @brief Abstract class for displays
5 * @date 13.05.2009
6 */
7
2#include <sstream> 8#include <sstream>
3#include <vector> 9#include <stdexcept>
4#include <map> 10#include <boost/lexical_cast.hpp>
11#include <assert.h>
5#include "cinstruction.h" 12#include "cinstruction.h"
6#include "cmem.h" 13#include "ccpu.h"
7using namespace std;
8
9void CInc::exec(CMem& mem, vector<string>& instr)
10{
11 mem.getRegister(instr[1])++;
12}
13
14
15void CDec::exec(CMem& mem, vector<string>& instr)
16{
17 mem.getRegister(instr[1])--;
18}
19
20
21void CAdd::exec(CMem& mem, vector<string>& instr)
22{
23 mem.getRegister(instr[1]) = mem.getRegister(instr[2]) +
24 mem.getRegister(instr[3]);
25}
26
27
28void CSub::exec(CMem& mem, vector<string>& instr)
29{
30 mem.getRegister(instr[1]) = mem.getRegister(instr[2]) -
31 mem.getRegister(instr[3]);
32
33}
34
35
36void CMul::exec(CMem& mem, vector<string>& instr)
37{
38 mem.getRegister(instr[1]) = mem.getRegister(instr[2]) *
39 mem.getRegister(instr[3]);
40}
41
42
43void CDiv::exec(CMem& mem, vector<string>& instr)
44{
45 mem.getRegister(instr[1]) = mem.getRegister(instr[2]) /
46 mem.getRegister(instr[3]);
47}
48
49
50void CLoad::exec(CMem& mem, vector<string>& instr)
51{
52 istringstream stmp (mem.getMemAt(instr[2]));
53 stmp >> mem.getRegister(instr[1]);
54
55}
56
57
58void CStore::exec(CMem& mem, vector<string>& instr)
59{
60 mem.setMemAt(instr[2], mem.getRegister(instr[1]));
61}
62
63
64void CTest::exec(CMem& mem, vector<string>& instr)
65{
66 if(mem.getRegister(instr[1]) == 0)
67 f_zero = true;
68 else
69 f_zero = false;
70
71 if(mem.getRegister(instr[1]) < 0)
72 f_sign = true;
73 else
74 f_sign = false;
75
76}
77
78
79
80
81
82void CJumpa::exec(CMem& mem, vector<string>& instr)
83{
84 mem.getRegister("R0") = (int) m_jumpaddr[instr[1]];
85}
86 14
15using namespace std;
87 16
88void CJumpz::exec(CMem& mem, vector<string>& instr) 17const unsigned CInstruction::parseRegister(const std::string& str)
89{ 18{
90 if(f_zero) 19 unsigned reg;
91 mem.getRegister("R0") = (int) m_jumpaddr[instr[1]]; 20 if (str.length() < 2 || str[0] != 'r')
92} 21 throw runtime_error("Invalid syntax of register");
93 22
23 try
24 {
25 reg = boost::lexical_cast<unsigned>(str.substr(1));
26 }
27 catch(boost::bad_lexical_cast& ex)
28 {
29 throw runtime_error("Invalid syntax of register");
30 }
94 31
95void CJumps::exec(CMem& mem, vector<string>& instr) 32 return reg;
96{
97 if(f_sign)
98 mem.getRegister("R0") = (int) m_jumpaddr[instr[1]];
99} 33}
100 34
35/*----------------------------------------------------------------------------*/
101 36
102void CWrite::exec(CMem& mem, vector<string>& instr) 37inline void CInstruction::checkRegister(CCPU *cpu, const unsigned regidx)
103{ 38{
104 39 assert(cpu != NULL);
105 if(instr[1] == "WDEZ") 40 if (regidx >= cpu->getRegisterCount())
106 cout << mem.getRegister(instr[2]) << endl; 41 {
107 else if (instr[1] == "WHEX") 42 stringstream sstr;
108 cout << hex << mem.getRegister(instr[2]) << endl; 43 sstr << "Register R" << regidx << " doesn't exist (out of bound)";
44 throw runtime_error(sstr.str());
45 }
109} 46}
110 47
48/* vim: set et sw=2 ts=2: */