diff options
| author | Günther Neuwirth <e0626638@student.tuwien.ac.at> | 2009-05-12 23:00:03 +0200 |
|---|---|---|
| committer | Günther Neuwirth <e0626638@student.tuwien.ac.at> | 2009-05-12 23:00:03 +0200 |
| commit | ddf63e2765a6b225d18c59321595e69e1a126e0c (patch) | |
| tree | 36336d5b3a1f0af0f9c2782351e30efe31bcf159 | |
| parent | e41c92320aef4a54b7f3b323f7302b180019b6d7 (diff) | |
| download | ooprog-ddf63e2765a6b225d18c59321595e69e1a126e0c.tar.gz ooprog-ddf63e2765a6b225d18c59321595e69e1a126e0c.tar.bz2 ooprog-ddf63e2765a6b225d18c59321595e69e1a126e0c.zip | |
adding tha rest
| -rw-r--r-- | ue3/mycpu/cinstruction.cpp | 8 | ||||
| -rw-r--r-- | ue3/mycpu/cmem.cpp | 70 | ||||
| -rw-r--r-- | ue3/mycpu/cmem.h | 19 | ||||
| -rw-r--r-- | ue3/mycpu/test/test_prog | 2 | ||||
| -rw-r--r-- | ue3/mycpu/test/test_prog_mem | 7 |
5 files changed, 84 insertions, 22 deletions
diff --git a/ue3/mycpu/cinstruction.cpp b/ue3/mycpu/cinstruction.cpp index 3cbb033..12c5c74 100644 --- a/ue3/mycpu/cinstruction.cpp +++ b/ue3/mycpu/cinstruction.cpp | |||
| @@ -49,15 +49,15 @@ void CDiv::exec(CMem& mem, vector<string>& instr) | |||
| 49 | 49 | ||
| 50 | void CLoad::exec(CMem& mem, vector<string>& instr) | 50 | void CLoad::exec(CMem& mem, vector<string>& instr) |
| 51 | { | 51 | { |
| 52 | istringstream stmp ("22"); | 52 | istringstream stmp (mem.getMemAt(instr[2])); |
| 53 | stmp >> mem.getRegister(instr[1]); | 53 | stmp >> mem.getRegister(instr[1]); |
| 54 | cout <<mem.getRegister(instr[1])<<" "<< mem.getMem(instr[2])<<endl; | 54 | |
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | 57 | ||
| 58 | void CStore::exec(CMem& mem, vector<string>& instr) | 58 | void CStore::exec(CMem& mem, vector<string>& instr) |
| 59 | { | 59 | { |
| 60 | mem.setMem(instr[2], mem.getRegister(instr[1])); | 60 | mem.setMemAt(instr[2], mem.getRegister(instr[1])); |
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | 63 | ||
diff --git a/ue3/mycpu/cmem.cpp b/ue3/mycpu/cmem.cpp index ec60b56..165747b 100644 --- a/ue3/mycpu/cmem.cpp +++ b/ue3/mycpu/cmem.cpp | |||
| @@ -37,13 +37,9 @@ CMem::~CMem() | |||
| 37 | 37 | ||
| 38 | CDat& CMem::getRegister(const string reg) | 38 | CDat& CMem::getRegister(const string reg) |
| 39 | { | 39 | { |
| 40 | istringstream stmp ( | ||
| 41 | reg.substr(reg.find_first_of("R") + 1, reg.size()) | ||
| 42 | ); | ||
| 43 | 40 | ||
| 44 | unsigned int regnr; | 41 | unsigned int regnr = getRegNr(reg); |
| 45 | stmp >> regnr; | 42 | |
| 46 | |||
| 47 | // if (regnr >= MAX_REGISTER ) | 43 | // if (regnr >= MAX_REGISTER ) |
| 48 | 44 | ||
| 49 | if (regnr >= m_registers.size()) | 45 | if (regnr >= m_registers.size()) |
| @@ -55,6 +51,68 @@ CDat& CMem::getRegister(const string reg) | |||
| 55 | 51 | ||
| 56 | return m_registers[regnr]; | 52 | return m_registers[regnr]; |
| 57 | } | 53 | } |
| 54 | |||
| 55 | /*----------------------------------------------------------------------------*/ | ||
| 56 | |||
| 57 | string CMem::getMemAt(const std::string addr) | ||
| 58 | { | ||
| 59 | int pos = getRegister(addr).getTypeValue(); | ||
| 60 | /* open and read mem */ | ||
| 61 | ifstream file(m_memfile.c_str(), ios::in); | ||
| 62 | string cur_line; | ||
| 63 | for (int i = 0; i <= pos; i++) | ||
| 64 | getline(file, cur_line); | ||
| 65 | |||
| 66 | file.close(); | ||
| 67 | if (cur_line.empty()) | ||
| 68 | return ""; | ||
| 69 | trim(cur_line); | ||
| 70 | return cur_line; | ||
| 71 | } | ||
| 72 | void CMem::setMemAt(const std::string addr, const CDat& value) | ||
| 73 | { | ||
| 74 | unsigned int pos = getRegister(addr).getTypeValue(); | ||
| 75 | /* open and read mem */ | ||
| 76 | ifstream ifile(m_memfile.c_str() ); | ||
| 77 | vector<string> tmp; | ||
| 78 | unsigned int i = 0; | ||
| 79 | while (ifile.good()) | ||
| 80 | { | ||
| 81 | string cur_line; | ||
| 82 | getline(ifile, cur_line); | ||
| 83 | if(!cur_line.empty()) | ||
| 84 | tmp.push_back(cur_line); | ||
| 85 | i++; | ||
| 86 | } | ||
| 87 | ifile.close(); | ||
| 88 | cout << tmp.size()<<"sasa"<<pos<<" "<<endl; | ||
| 89 | |||
| 90 | ofstream ofile(m_memfile.c_str(), fstream::trunc); | ||
| 91 | i = 0; | ||
| 92 | while (ofile.good() && i < tmp.size()) | ||
| 93 | { | ||
| 94 | if (i != pos) | ||
| 95 | ofile << tmp[i] << endl; | ||
| 96 | else | ||
| 97 | ofile << value << endl; | ||
| 98 | cout << tmp[i]<< endl; | ||
| 99 | i++; | ||
| 100 | } | ||
| 101 | ofile.close(); | ||
| 102 | } | ||
| 103 | /*----------------------------------------------------------------------------*/ | ||
| 104 | |||
| 105 | unsigned int CMem::getRegNr(const std::string reg) | ||
| 106 | { | ||
| 107 | istringstream stmp ( | ||
| 108 | reg.substr(reg.find_first_of("R") + 1, reg.size()) | ||
| 109 | ); | ||
| 110 | |||
| 111 | unsigned int regnr; | ||
| 112 | stmp >> regnr; | ||
| 113 | return regnr; | ||
| 114 | } | ||
| 115 | |||
| 58 | /*----------------------------------------------------------------------------*/ | 116 | /*----------------------------------------------------------------------------*/ |
| 59 | 117 | ||
| 60 | #ifdef DEBUG | 118 | #ifdef DEBUG |
diff --git a/ue3/mycpu/cmem.h b/ue3/mycpu/cmem.h index 06fa876..356c9c0 100644 --- a/ue3/mycpu/cmem.h +++ b/ue3/mycpu/cmem.h | |||
| @@ -60,13 +60,9 @@ class CMem | |||
| 60 | 60 | ||
| 61 | CDat& getRegister(const std::string reg); | 61 | CDat& getRegister(const std::string reg); |
| 62 | 62 | ||
| 63 | CDat& getMem(const std::string addr) | 63 | std::string getMemAt(const std::string addr); |
| 64 | { | 64 | |
| 65 | getRegister("R255") = 10; | 65 | void setMemAt(const std::string addr, const CDat& value); |
| 66 | return getRegister("R255"); | ||
| 67 | } | ||
| 68 | void setMem(const std::string addr, const CDat& value) | ||
| 69 | {} | ||
| 70 | 66 | ||
| 71 | #ifdef DEBUG | 67 | #ifdef DEBUG |
| 72 | /** | 68 | /** |
| @@ -83,11 +79,12 @@ class CMem | |||
| 83 | 79 | ||
| 84 | 80 | ||
| 85 | private: | 81 | private: |
| 86 | /* members */ | ||
| 87 | std::string m_memfile; | ||
| 88 | 82 | ||
| 89 | 83 | unsigned int getRegNr(const std::string reg); | |
| 90 | std::vector<CDat> m_registers; | 84 | |
| 85 | /* members */ | ||
| 86 | std::string m_memfile; | ||
| 87 | std::vector<CDat> m_registers; | ||
| 91 | }; | 88 | }; |
| 92 | 89 | ||
| 93 | #endif | 90 | #endif |
diff --git a/ue3/mycpu/test/test_prog b/ue3/mycpu/test/test_prog index 74dac21..b50575b 100644 --- a/ue3/mycpu/test/test_prog +++ b/ue3/mycpu/test/test_prog | |||
| @@ -1,4 +1,5 @@ | |||
| 1 | # set R2 = 10 | 1 | # set R2 = 10 |
| 2 | |||
| 2 | load R2, R1 | 3 | load R2, R1 |
| 3 | 4 | ||
| 4 | # start of loop | 5 | # start of loop |
| @@ -8,6 +9,7 @@ sub R4, R3, R2 | |||
| 8 | test R4 | 9 | test R4 |
| 9 | jumpz EndLoop | 10 | jumpz EndLoop |
| 10 | write WDEZ, R3 | 11 | write WDEZ, R3 |
| 12 | store R3, R1 | ||
| 11 | jumpa Loop | 13 | jumpa Loop |
| 12 | 14 | ||
| 13 | label EndLoop: | 15 | label EndLoop: |
diff --git a/ue3/mycpu/test/test_prog_mem b/ue3/mycpu/test/test_prog_mem index f599e28..8c3e80f 100644 --- a/ue3/mycpu/test/test_prog_mem +++ b/ue3/mycpu/test/test_prog_mem | |||
| @@ -1 +1,6 @@ | |||
| 1 | 10 | 1 | 9 |
| 2 | 3 | ||
| 3 | 4 | ||
| 4 | 5 | ||
| 5 | 4 | ||
| 6 | 3 | ||
