diff options
| author | Günther Neuwirth <e0626638@student.tuwien.ac.at> | 2009-05-12 15:27:58 +0200 |
|---|---|---|
| committer | Günther Neuwirth <e0626638@student.tuwien.ac.at> | 2009-05-12 15:27:58 +0200 |
| commit | 34483e07a0548d32651cda4ca4282f3cf8cae870 (patch) | |
| tree | ef499b9850e60cc45ca4de1605c962a5cc040a8f | |
| parent | fe1ef6b47f59899e8687bb1dcc92eba1d103a08f (diff) | |
| download | ooprog-34483e07a0548d32651cda4ca4282f3cf8cae870.tar.gz ooprog-34483e07a0548d32651cda4ca4282f3cf8cae870.tar.bz2 ooprog-34483e07a0548d32651cda4ca4282f3cf8cae870.zip | |
adding rest of files
| -rw-r--r-- | ue3/mycpu/Makefile | 6 | ||||
| -rw-r--r-- | ue3/mycpu/ccpu.cpp | 97 | ||||
| -rw-r--r-- | ue3/mycpu/ccpu.h | 77 | ||||
| -rw-r--r-- | ue3/mycpu/cdat.h | 14 | ||||
| -rw-r--r-- | ue3/mycpu/cinstruction.cpp | 104 | ||||
| -rw-r--r-- | ue3/mycpu/cinstruction.h | 196 | ||||
| -rw-r--r-- | ue3/mycpu/cmem.cpp | 65 | ||||
| -rw-r--r-- | ue3/mycpu/cmem.h | 94 | ||||
| -rw-r--r-- | ue3/mycpu/cprogram.cpp | 109 | ||||
| -rw-r--r-- | ue3/mycpu/cprogram.h | 106 | ||||
| -rw-r--r-- | ue3/mycpu/mycpu.cpp | 94 | ||||
| -rw-r--r-- | ue3/mycpu/test/test_prog | 13 | ||||
| -rw-r--r-- | ue3/mycpu/test/test_prog_mem | 1 |
13 files changed, 973 insertions, 3 deletions
diff --git a/ue3/mycpu/Makefile b/ue3/mycpu/Makefile index 6f57741..76b6244 100644 --- a/ue3/mycpu/Makefile +++ b/ue3/mycpu/Makefile | |||
| @@ -8,11 +8,11 @@ DEBUGFLAGS= -DNDEBUG | |||
| 8 | INCLUDE_PATH= -I/usr/local/include | 8 | INCLUDE_PATH= -I/usr/local/include |
| 9 | CXXFLAGS= -O -ansi -pedantic-errors -Wall $(INCLUDE_PATH) $(DEBUGFLAGS) | 9 | CXXFLAGS= -O -ansi -pedantic-errors -Wall $(INCLUDE_PATH) $(DEBUGFLAGS) |
| 10 | LDFLAGS= | 10 | LDFLAGS= |
| 11 | LIBS= -L/usr/local/lib | 11 | LIBS= -L/usr/local/lib -lboost_program_options |
| 12 | 12 | ||
| 13 | BIN= mycpu | 13 | BIN= mycpu |
| 14 | OBJS= mycpu.o cdat.o | 14 | OBJS= mycpu.o ccpu.o cprogram.o cmem.o cinstruction.o |
| 15 | HEADERS= cdat.h | 15 | HEADERS= cdat.h ccpu.h cprogram.h cmem.h cinstruction.h |
| 16 | 16 | ||
| 17 | .SUFFIXES: .cpp .o | 17 | .SUFFIXES: .cpp .o |
| 18 | 18 | ||
diff --git a/ue3/mycpu/ccpu.cpp b/ue3/mycpu/ccpu.cpp new file mode 100644 index 0000000..72ad96c --- /dev/null +++ b/ue3/mycpu/ccpu.cpp | |||
| @@ -0,0 +1,97 @@ | |||
| 1 | /** | ||
| 2 | * @module ccpu | ||
| 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) | ||
| 4 | * @brief class for processing a program | ||
| 5 | * @date 11.05.2009 | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include <fstream> | ||
| 9 | #include <boost/tokenizer.hpp> | ||
| 10 | #include <boost/algorithm/string.hpp> | ||
| 11 | |||
| 12 | #include "ccpu.h" | ||
| 13 | #include "cinstruction.h" | ||
| 14 | #include "cprogram.h" | ||
| 15 | #include "cmem.h" | ||
| 16 | |||
| 17 | |||
| 18 | using namespace std; | ||
| 19 | using namespace boost; | ||
| 20 | |||
| 21 | CCPU::CCPU(const std::string& progfile, const std::string& memfile) | ||
| 22 | : m_program(progfile), m_memory(memfile) | ||
| 23 | { | ||
| 24 | m_instrtype["inc"] = new CInc(); | ||
| 25 | m_instrtype["dec"] = new CDec(); | ||
| 26 | m_instrtype["add"] = new CAdd(); | ||
| 27 | m_instrtype["sub"] = new CSub(); | ||
| 28 | m_instrtype["mul"] = new CMul(); | ||
| 29 | m_instrtype["div"] = new CDiv(); | ||
| 30 | m_instrtype["load"] = new CLoad(); | ||
| 31 | m_instrtype["store"] = new CStore(); | ||
| 32 | m_instrtype["test"] = new CTest(); | ||
| 33 | m_instrtype["label"] = new CLabel(); | ||
| 34 | m_instrtype["jumpa"] = new CJumpa(m_program.getJumpAddrs()); | ||
| 35 | m_instrtype["jumpz"] = new CJumpz(m_program.getJumpAddrs()); | ||
| 36 | m_instrtype["jumps"] = new CJumps(m_program.getJumpAddrs()); | ||
| 37 | m_instrtype["write"] = new CWrite(); | ||
| 38 | } | ||
| 39 | |||
| 40 | |||
| 41 | void CCPU::proceed() | ||
| 42 | { | ||
| 43 | |||
| 44 | while (m_memory.getRegister("R0") < m_program.getMaxProgramCount()) | ||
| 45 | { | ||
| 46 | std::vector<std::string>& i_list = m_program.getInstruction( | ||
| 47 | m_memory.getRegister("R0") | ||
| 48 | ); | ||
| 49 | |||
| 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 | |||
| 85 | for(int i = 0; i < (int)i_list.size(); i++) | ||
| 86 | cout << i_list[i] << " "; | ||
| 87 | m_memory.getRegister("R0")++; | ||
| 88 | cout << m_memory.getRegister("R0")<< endl<<endl; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | void CCPU::execInstruction(CInstruction& instr, vector<string>& i_list) | ||
| 93 | { | ||
| 94 | |||
| 95 | |||
| 96 | } | ||
| 97 | |||
diff --git a/ue3/mycpu/ccpu.h b/ue3/mycpu/ccpu.h new file mode 100644 index 0000000..6b16ba3 --- /dev/null +++ b/ue3/mycpu/ccpu.h | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | /** | ||
| 2 | * @module ccpu | ||
| 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) | ||
| 4 | * @brief class for parsing simple scriptfiles | ||
| 5 | * @date 17.04.2009 | ||
| 6 | */ | ||
| 7 | |||
| 8 | #ifndef CCPU_H | ||
| 9 | #define CCPU_H | ||
| 10 | |||
| 11 | #include <stdexcept> | ||
| 12 | #include <string> | ||
| 13 | #include <vector> | ||
| 14 | |||
| 15 | #include "cprogram.h" | ||
| 16 | #include "cmem.h" | ||
| 17 | #include "cinstruction.h" | ||
| 18 | /** | ||
| 19 | * @class CCPU | ||
| 20 | * | ||
| 21 | * Parses a simple line based scriptfile with some limitations: | ||
| 22 | * first function (starting a block) must be a read-command, | ||
| 23 | * last must be a write-command (ending this block). | ||
| 24 | * | ||
| 25 | * read- and write-commands have hard coded parameters, number#1 being a filetype. | ||
| 26 | * Classes handling certain filetypes must be of type CFile. | ||
| 27 | * Custom functions will be passed to CFile::callFunc(). | ||
| 28 | * | ||
| 29 | * On error ParserError will be thrown. | ||
| 30 | */ | ||
| 31 | class CCPU | ||
| 32 | { | ||
| 33 | public: | ||
| 34 | |||
| 35 | |||
| 36 | /** | ||
| 37 | * @method CScriptparser | ||
| 38 | * @brief Default ctor | ||
| 39 | * @param scriptfile filename of script to parse | ||
| 40 | * @return - | ||
| 41 | * @globalvars none | ||
| 42 | * @exception bad_alloc | ||
| 43 | * @conditions none | ||
| 44 | */ | ||
| 45 | CCPU(const std::string& progfile, const std::string& memfile); | ||
| 46 | /** | ||
| 47 | * @method ~CScriptparser | ||
| 48 | * @brief Default dtor | ||
| 49 | * @param - | ||
| 50 | * @return - | ||
| 51 | * @globalvars none | ||
| 52 | * @exception none | ||
| 53 | * @conditions none | ||
| 54 | */ | ||
| 55 | ~CCPU() | ||
| 56 | {} | ||
| 57 | |||
| 58 | |||
| 59 | void proceed(); | ||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | void execInstruction( CInstruction& instr, std::vector<std::string>& i_list); | ||
| 64 | private: | ||
| 65 | /* members */ | ||
| 66 | |||
| 67 | CProgram m_program; | ||
| 68 | CMem m_memory; | ||
| 69 | std::map<std::string, CInstruction *> m_instrtype; | ||
| 70 | bool f_zero, f_sign; | ||
| 71 | // std::string m_memfiler; | ||
| 72 | |||
| 73 | }; | ||
| 74 | |||
| 75 | #endif | ||
| 76 | |||
| 77 | /* vim: set et sw=2 ts=2: */ | ||
diff --git a/ue3/mycpu/cdat.h b/ue3/mycpu/cdat.h index dc8a07c..3fda572 100644 --- a/ue3/mycpu/cdat.h +++ b/ue3/mycpu/cdat.h | |||
| @@ -295,6 +295,20 @@ class CDatT | |||
| 295 | return stream; | 295 | return stream; |
| 296 | } | 296 | } |
| 297 | 297 | ||
| 298 | /** | ||
| 299 | * @method getTypeValue | ||
| 300 | * @brief returns value as type T | ||
| 301 | * @param - | ||
| 302 | * @return m_value | ||
| 303 | * @globalvars none | ||
| 304 | * @exception none | ||
| 305 | * @conditions none | ||
| 306 | */ | ||
| 307 | T& getTypeValue() | ||
| 308 | { | ||
| 309 | return m_value; | ||
| 310 | } | ||
| 311 | |||
| 298 | private: | 312 | private: |
| 299 | /* members */ | 313 | /* members */ |
| 300 | T m_value; | 314 | T m_value; |
diff --git a/ue3/mycpu/cinstruction.cpp b/ue3/mycpu/cinstruction.cpp new file mode 100644 index 0000000..2c8dedf --- /dev/null +++ b/ue3/mycpu/cinstruction.cpp | |||
| @@ -0,0 +1,104 @@ | |||
| 1 | #include <iostream> | ||
| 2 | #include <vector> | ||
| 3 | #include <map> | ||
| 4 | #include "cinstruction.h" | ||
| 5 | #include "cmem.h" | ||
| 6 | using namespace std; | ||
| 7 | |||
| 8 | void CInc::exec(CMem& mem, vector<string>& instr) | ||
| 9 | { | ||
| 10 | mem.getRegister(instr[1])++; | ||
| 11 | } | ||
| 12 | |||
| 13 | |||
| 14 | void CDec::exec(CMem& mem, vector<string>& instr) | ||
| 15 | { | ||
| 16 | mem.getRegister(instr[1])--; | ||
| 17 | } | ||
| 18 | |||
| 19 | |||
| 20 | void CAdd::exec(CMem& mem, vector<string>& instr) | ||
| 21 | { | ||
| 22 | mem.getRegister(instr[1]) = mem.getRegister(instr[2]) + | ||
| 23 | mem.getRegister(instr[3]); | ||
| 24 | } | ||
| 25 | |||
| 26 | |||
| 27 | void CSub::exec(CMem& mem, vector<string>& instr) | ||
| 28 | { | ||
| 29 | mem.getRegister(instr[1]) = mem.getRegister(instr[2]) - | ||
| 30 | mem.getRegister(instr[3]); | ||
| 31 | } | ||
| 32 | |||
| 33 | |||
| 34 | void CMul::exec(CMem& mem, vector<string>& instr) | ||
| 35 | { | ||
| 36 | mem.getRegister(instr[1]) = mem.getRegister(instr[2]) * | ||
| 37 | mem.getRegister(instr[3]); | ||
| 38 | } | ||
| 39 | |||
| 40 | |||
| 41 | void CDiv::exec(CMem& mem, vector<string>& instr) | ||
| 42 | { | ||
| 43 | mem.getRegister(instr[1]) = mem.getRegister(instr[2]) / | ||
| 44 | mem.getRegister(instr[3]); | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | void CLoad::exec(CMem& mem, vector<string>& instr) | ||
| 49 | { | ||
| 50 | mem.getRegister(instr[1]) = mem.getMem(instr[2]); | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | void CStore::exec(CMem& mem, vector<string>& instr) | ||
| 55 | { | ||
| 56 | mem.setMem(instr[2], mem.getRegister(instr[1])); | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | void CTest::test(CMem& mem, vector<string>& instr, bool& f_zero, bool& f_sign) | ||
| 61 | { | ||
| 62 | if(mem.getRegister(instr[1]) == 0) | ||
| 63 | f_zero = true; | ||
| 64 | else | ||
| 65 | f_zero = false; | ||
| 66 | |||
| 67 | if(mem.getRegister(instr[1]) <= 0) | ||
| 68 | f_sign = true; | ||
| 69 | else | ||
| 70 | f_sign = false; | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | |||
| 75 | |||
| 76 | |||
| 77 | void CJumpa::exec(CMem& mem, vector<string>& instr) | ||
| 78 | { | ||
| 79 | mem.getRegister(instr[0]) = (int) m_jumpaddr[instr[1]]; | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | void CJumpz::exec(CMem& mem, vector<string>& instr, bool& f_zero) | ||
| 84 | { | ||
| 85 | if(f_zero) | ||
| 86 | mem.getRegister(instr[0]) = (int) m_jumpaddr[instr[1]]; | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | void CJumps::exec(CMem& mem, vector<string>& instr, bool& f_sign) | ||
| 91 | { | ||
| 92 | if(f_sign) | ||
| 93 | mem.getRegister(instr[0]) = (int) m_jumpaddr[instr[1]]; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | void CWrite::exec(CMem& mem, vector<string>& instr) | ||
| 98 | { | ||
| 99 | if(instr[1] == "WDEZ") | ||
| 100 | cout << mem.getRegister(instr[2]) << endl; | ||
| 101 | else if (instr[1] == "WHEX") | ||
| 102 | cout << hex << mem.getRegister(instr[2]) << endl; | ||
| 103 | } | ||
| 104 | |||
diff --git a/ue3/mycpu/cinstruction.h b/ue3/mycpu/cinstruction.h new file mode 100644 index 0000000..d393e09 --- /dev/null +++ b/ue3/mycpu/cinstruction.h | |||
| @@ -0,0 +1,196 @@ | |||
| 1 | /** | ||
| 2 | * @module cinstruction | ||
| 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) | ||
| 4 | * @brief abstract class cpu instructions | ||
| 5 | * @date 17.04.2009 | ||
| 6 | */ | ||
| 7 | |||
| 8 | #ifndef CINSTRUCTION_H | ||
| 9 | #define CINSTRUCTION_H | ||
| 10 | #include <map> | ||
| 11 | #include <vector> | ||
| 12 | #include "cmem.h" | ||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | /** | ||
| 17 | * @class CInstruction | ||
| 18 | * | ||
| 19 | * Parses a simple line based scriptfile with some limitations: | ||
| 20 | * first function (starting a block) must be a read-command, | ||
| 21 | * last must be a write-command (ending this block). | ||
| 22 | * | ||
| 23 | * read- and write-commands have hard coded parameters, number#1 being a filetype. | ||
| 24 | * Classes handling certain filetypes must be of type CFile. | ||
| 25 | * Custom functions will be passed to CFile::callFunc(). | ||
| 26 | * | ||
| 27 | * On error ParserError will be thrown. | ||
| 28 | */ | ||
| 29 | class CInstruction | ||
| 30 | { | ||
| 31 | public: | ||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | |||
| 36 | /** | ||
| 37 | * @method ~CInstruction | ||
| 38 | * @brief Default dtor | ||
| 39 | * @param - | ||
| 40 | * @return - | ||
| 41 | * @globalvars none | ||
| 42 | * @exception none | ||
| 43 | * @conditions none | ||
| 44 | */ | ||
| 45 | virtual ~CInstruction() | ||
| 46 | {} | ||
| 47 | |||
| 48 | |||
| 49 | virtual void exec(CMem& mem, std::vector<std::string>& instr) = 0; | ||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | }; | ||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | |||
| 59 | class CFlagInstruction : public CInstruction | ||
| 60 | { | ||
| 61 | public: | ||
| 62 | |||
| 63 | void exec(CMem& mem, std::vector<std::string>& instr) | ||
| 64 | {} | ||
| 65 | virtual void exec(CMem& mem, std::vector<std::string>& instr, bool& flag) = 0; | ||
| 66 | |||
| 67 | |||
| 68 | }; | ||
| 69 | |||
| 70 | |||
| 71 | |||
| 72 | class CInc : public CInstruction | ||
| 73 | { | ||
| 74 | public: | ||
| 75 | virtual void exec(CMem& mem, std::vector<std::string>& instr); | ||
| 76 | }; | ||
| 77 | |||
| 78 | |||
| 79 | class CDec : public CInstruction | ||
| 80 | { | ||
| 81 | public: | ||
| 82 | virtual void exec(CMem& mem, std::vector<std::string>& instr); | ||
| 83 | }; | ||
| 84 | |||
| 85 | |||
| 86 | |||
| 87 | class CAdd : public CInstruction | ||
| 88 | { | ||
| 89 | public: | ||
| 90 | virtual void exec(CMem& mem, std::vector<std::string>& instr); | ||
| 91 | }; | ||
| 92 | |||
| 93 | |||
| 94 | |||
| 95 | class CSub : public CInstruction | ||
| 96 | { | ||
| 97 | public: | ||
| 98 | virtual void exec(CMem& mem, std::vector<std::string>& instr); | ||
| 99 | }; | ||
| 100 | |||
| 101 | |||
| 102 | |||
| 103 | class CMul : public CInstruction | ||
| 104 | { | ||
| 105 | public: | ||
| 106 | virtual void exec(CMem& mem, std::vector<std::string>& instr); | ||
| 107 | }; | ||
| 108 | |||
| 109 | |||
| 110 | |||
| 111 | class CDiv : public CInstruction | ||
| 112 | { | ||
| 113 | public: | ||
| 114 | virtual void exec(CMem& mem, std::vector<std::string>& instr); | ||
| 115 | }; | ||
| 116 | |||
| 117 | |||
| 118 | |||
| 119 | class CLoad : public CInstruction | ||
| 120 | { | ||
| 121 | public: | ||
| 122 | virtual void exec(CMem& mem, std::vector<std::string>& instr); | ||
| 123 | }; | ||
| 124 | |||
| 125 | |||
| 126 | |||
| 127 | class CStore : public CInstruction | ||
| 128 | { | ||
| 129 | public: | ||
| 130 | virtual void exec(CMem& mem, std::vector<std::string>& instr); | ||
| 131 | }; | ||
| 132 | |||
| 133 | class CTest : public CFlagInstruction | ||
| 134 | { | ||
| 135 | public: | ||
| 136 | void exec(CMem& mem, std::vector<std::string>& instr, bool& flag) | ||
| 137 | {} | ||
| 138 | virtual void test(CMem& mem, std::vector<std::string>& instr, | ||
| 139 | bool& f_zero, bool& f_sign); | ||
| 140 | }; | ||
| 141 | |||
| 142 | |||
| 143 | class CLabel : public CInstruction | ||
| 144 | { | ||
| 145 | public: | ||
| 146 | void exec(CMem& mem, std::vector<std::string>& instr) | ||
| 147 | {} | ||
| 148 | |||
| 149 | }; | ||
| 150 | |||
| 151 | |||
| 152 | class CJumpa : public CInstruction | ||
| 153 | { | ||
| 154 | public: | ||
| 155 | CJumpa(std::map<std::string, unsigned int>& jumpaddr) | ||
| 156 | : m_jumpaddr(jumpaddr) | ||
| 157 | {} | ||
| 158 | virtual void exec(CMem& mem, std::vector<std::string>& instr); | ||
| 159 | protected: | ||
| 160 | std::map<std::string, unsigned int> m_jumpaddr; | ||
| 161 | }; | ||
| 162 | |||
| 163 | |||
| 164 | class CJumpz : public CFlagInstruction | ||
| 165 | { | ||
| 166 | public: | ||
| 167 | CJumpz(std::map<std::string, unsigned int>& jumpaddr) | ||
| 168 | : m_jumpaddr(jumpaddr) | ||
| 169 | {} | ||
| 170 | virtual void exec(CMem& mem, std::vector<std::string>& instr, bool& f_zero); | ||
| 171 | protected: | ||
| 172 | std::map<std::string, unsigned int> m_jumpaddr; | ||
| 173 | }; | ||
| 174 | |||
| 175 | |||
| 176 | class CJumps : public CFlagInstruction | ||
| 177 | { | ||
| 178 | public: | ||
| 179 | CJumps(std::map<std::string, unsigned int>& jumpaddr) | ||
| 180 | : m_jumpaddr(jumpaddr) | ||
| 181 | {} | ||
| 182 | virtual void exec(CMem& mem, std::vector<std::string>& instr, bool& f_sign); | ||
| 183 | protected: | ||
| 184 | std::map<std::string, unsigned int> m_jumpaddr; | ||
| 185 | }; | ||
| 186 | |||
| 187 | |||
| 188 | |||
| 189 | class CWrite : public CInstruction | ||
| 190 | { | ||
| 191 | public: | ||
| 192 | virtual void exec(CMem& mem, std::vector<std::string>& instr); | ||
| 193 | }; | ||
| 194 | |||
| 195 | #endif | ||
| 196 | /* vim: set et sw=2 ts=2: */ | ||
diff --git a/ue3/mycpu/cmem.cpp b/ue3/mycpu/cmem.cpp new file mode 100644 index 0000000..d27f74e --- /dev/null +++ b/ue3/mycpu/cmem.cpp | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | /** | ||
| 2 | * @module cprogram | ||
| 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) | ||
| 4 | * @brief class for parsing and saving a program | ||
| 5 | * @date 11.05.2009 | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include <fstream> | ||
| 9 | #include <sstream> | ||
| 10 | #include <string> | ||
| 11 | #include <boost/tokenizer.hpp> | ||
| 12 | #include <boost/algorithm/string.hpp> | ||
| 13 | #include "cdat.h" | ||
| 14 | #include "cmem.h" | ||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | using namespace std; | ||
| 19 | using namespace boost; | ||
| 20 | |||
| 21 | CMem::CMem(const std::string& memfile) : | ||
| 22 | m_memfile(memfile) | ||
| 23 | { | ||
| 24 | |||
| 25 | dump(std::cout); | ||
| 26 | |||
| 27 | } | ||
| 28 | |||
| 29 | /*----------------------------------------------------------------------------*/ | ||
| 30 | |||
| 31 | CMem::~CMem() | ||
| 32 | { | ||
| 33 | |||
| 34 | } | ||
| 35 | |||
| 36 | /*----------------------------------------------------------------------------*/ | ||
| 37 | |||
| 38 | CDat& CMem::getRegister(const string reg) | ||
| 39 | { | ||
| 40 | istringstream stmp ( | ||
| 41 | reg.substr(reg.find_first_of("R") + 1, reg.size()) | ||
| 42 | ); | ||
| 43 | |||
| 44 | unsigned int regnr; | ||
| 45 | stmp >> regnr; | ||
| 46 | |||
| 47 | // if (regnr >= MAX_REGISTER ) | ||
| 48 | |||
| 49 | if (regnr == m_registers.size()) | ||
| 50 | { | ||
| 51 | m_registers.push_back(CDat((int)0)); | ||
| 52 | return m_registers[m_registers.size() - 1]; | ||
| 53 | } | ||
| 54 | |||
| 55 | return m_registers[regnr]; | ||
| 56 | } | ||
| 57 | /*----------------------------------------------------------------------------*/ | ||
| 58 | |||
| 59 | #ifdef DEBUG | ||
| 60 | void CMem::dump(std::ostream& out) | ||
| 61 | { | ||
| 62 | out << endl << "Memory file:" << endl << m_memfile << endl; | ||
| 63 | out << endl << "Memory file content:" << endl; | ||
| 64 | } | ||
| 65 | #endif | ||
diff --git a/ue3/mycpu/cmem.h b/ue3/mycpu/cmem.h new file mode 100644 index 0000000..923ed01 --- /dev/null +++ b/ue3/mycpu/cmem.h | |||
| @@ -0,0 +1,94 @@ | |||
| 1 | /** | ||
| 2 | * @module cmem | ||
| 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) | ||
| 4 | * @brief class for providing 256 registers. | ||
| 5 | * @date 11.05.2009 | ||
| 6 | */ | ||
| 7 | |||
| 8 | #ifndef CMEM_H | ||
| 9 | #define CMEM_H | ||
| 10 | |||
| 11 | #include <stdexcept> | ||
| 12 | #include <string> | ||
| 13 | |||
| 14 | #include <set> | ||
| 15 | #ifdef DEBUG | ||
| 16 | # include <iostream> | ||
| 17 | #endif | ||
| 18 | #include "cdat.h" | ||
| 19 | |||
| 20 | |||
| 21 | #define MAX_REGISTER 256; | ||
| 22 | /** | ||
| 23 | * @class CMem | ||
| 24 | * | ||
| 25 | * Parses a simple line based scriptfile with some limitations: | ||
| 26 | * first function (starting a block) must be a read-command, | ||
| 27 | * last must be a write-command (ending this block). | ||
| 28 | * | ||
| 29 | * read- and write-commands have hard coded parameters, number#1 being a filetype. | ||
| 30 | * Classes handling certain filetypes must be of type CFile. | ||
| 31 | * Custom functions will be passed to CFile::callFunc(). | ||
| 32 | * | ||
| 33 | * On error ParserError will be thrown. | ||
| 34 | */ | ||
| 35 | class CMem | ||
| 36 | { | ||
| 37 | public: | ||
| 38 | |||
| 39 | /** | ||
| 40 | * @method CMem | ||
| 41 | * @brief Default ctor | ||
| 42 | * @param memoryfile filename | ||
| 43 | * @return - | ||
| 44 | * @globalvars none | ||
| 45 | * @exception bad_alloc | ||
| 46 | * @conditions none | ||
| 47 | */ | ||
| 48 | CMem(const std::string& memfile); | ||
| 49 | |||
| 50 | /** | ||
| 51 | * @method ~CMem | ||
| 52 | * @brief Default dtor | ||
| 53 | * @param - | ||
| 54 | * @return - | ||
| 55 | * @globalvars none | ||
| 56 | * @exception none | ||
| 57 | * @conditions none | ||
| 58 | */ | ||
| 59 | ~CMem(); | ||
| 60 | |||
| 61 | CDat& getRegister(const std::string reg); | ||
| 62 | |||
| 63 | CDat& getMem(const std::string addr) | ||
| 64 | { | ||
| 65 | return getRegister("R0"); | ||
| 66 | } | ||
| 67 | void setMem(const std::string addr, const CDat& value) | ||
| 68 | {} | ||
| 69 | |||
| 70 | #ifdef DEBUG | ||
| 71 | /** | ||
| 72 | * @method dump | ||
| 73 | * @brief Dumps the resgister contnent to ostream | ||
| 74 | * @param out output stream | ||
| 75 | * @return - | ||
| 76 | * @globalvars | ||
| 77 | * @exception | ||
| 78 | * @conditions | ||
| 79 | */ | ||
| 80 | void dump(std::ostream& out); | ||
| 81 | #endif | ||
| 82 | |||
| 83 | |||
| 84 | private: | ||
| 85 | /* members */ | ||
| 86 | std::string m_memfile; | ||
| 87 | |||
| 88 | |||
| 89 | std::vector<CDat> m_registers; | ||
| 90 | }; | ||
| 91 | |||
| 92 | #endif | ||
| 93 | |||
| 94 | /* vim: set et sw=2 ts=2: */ | ||
diff --git a/ue3/mycpu/cprogram.cpp b/ue3/mycpu/cprogram.cpp new file mode 100644 index 0000000..9297b6e --- /dev/null +++ b/ue3/mycpu/cprogram.cpp | |||
| @@ -0,0 +1,109 @@ | |||
| 1 | /** | ||
| 2 | * @module cprogram | ||
| 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) | ||
| 4 | * @brief class for parsing and saving a program | ||
| 5 | * @date 11.05.2009 | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include <fstream> | ||
| 9 | #include <vector> | ||
| 10 | #include <string> | ||
| 11 | #include <boost/tokenizer.hpp> | ||
| 12 | #include <boost/algorithm/string.hpp> | ||
| 13 | #include <boost/algorithm/string/split.hpp> | ||
| 14 | #include "cprogram.h" | ||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | using namespace std; | ||
| 19 | using namespace boost; | ||
| 20 | |||
| 21 | CProgram::CProgram(const std::string& progfile) : | ||
| 22 | m_programfile(progfile) | ||
| 23 | { | ||
| 24 | parse(); | ||
| 25 | dump(std::cout); | ||
| 26 | |||
| 27 | } | ||
| 28 | |||
| 29 | /*----------------------------------------------------------------------------*/ | ||
| 30 | |||
| 31 | CProgram::~CProgram() | ||
| 32 | { | ||
| 33 | |||
| 34 | } | ||
| 35 | |||
| 36 | |||
| 37 | |||
| 38 | /*----------------------------------------------------------------------------*/ | ||
| 39 | |||
| 40 | void CProgram::parse() | ||
| 41 | { | ||
| 42 | /* open and read file */ | ||
| 43 | ifstream file(m_programfile.c_str(), ios::in); | ||
| 44 | // if (!file) | ||
| 45 | // throw ParserError("Unable to open scriptfile '" + m_scriptfile + "'."); | ||
| 46 | |||
| 47 | int cur_line_nr = 0; | ||
| 48 | |||
| 49 | while (!file.eof() && file.good()) | ||
| 50 | { | ||
| 51 | string cur_line; | ||
| 52 | |||
| 53 | /* read file per line */ | ||
| 54 | getline(file, cur_line); | ||
| 55 | if (cur_line.empty()) | ||
| 56 | continue; | ||
| 57 | |||
| 58 | trim(cur_line); | ||
| 59 | |||
| 60 | /* ignore comments */ | ||
| 61 | if (cur_line.find_first_of('#') == 0) | ||
| 62 | continue; | ||
| 63 | |||
| 64 | /*remove commas from current line */ | ||
| 65 | unsigned int pos; | ||
| 66 | while (( pos = cur_line.find_first_of(",") ) != string::npos) | ||
| 67 | cur_line.erase(pos, 1); | ||
| 68 | |||
| 69 | /* add source line*/ | ||
| 70 | m_progsource.push_back(vector<string>()); | ||
| 71 | algorithm::split( m_progsource.back(), cur_line, is_any_of(" \t")); | ||
| 72 | |||
| 73 | /* jump addr as line number */ | ||
| 74 | if(m_progsource[cur_line_nr][0] == "label") | ||
| 75 | { | ||
| 76 | int size = m_progsource[cur_line_nr][01].size() - 1; | ||
| 77 | string label(m_progsource[cur_line_nr][01].substr(0, size)); | ||
| 78 | m_jumpaddr[label] = cur_line_nr; | ||
| 79 | } | ||
| 80 | |||
| 81 | cur_line_nr++; | ||
| 82 | } | ||
| 83 | file.close(); | ||
| 84 | |||
| 85 | } | ||
| 86 | |||
| 87 | /*----------------------------------------------------------------------------*/ | ||
| 88 | |||
| 89 | #ifdef DEBUG | ||
| 90 | void CProgram::dump(std::ostream& out) | ||
| 91 | { | ||
| 92 | |||
| 93 | out << endl << "Program file:" << endl << m_programfile << endl; | ||
| 94 | |||
| 95 | out << endl << "Program source:" << endl; | ||
| 96 | for (int i = 0; i < (int) m_progsource.size();i++) | ||
| 97 | { | ||
| 98 | for(int x = 0; x < (int) m_progsource[i].size();x++) | ||
| 99 | out << m_progsource[i][x] << " "; | ||
| 100 | out << std::endl; | ||
| 101 | } | ||
| 102 | |||
| 103 | out << endl << "Jump addresses:" << endl; | ||
| 104 | map<string, unsigned int>::iterator it; | ||
| 105 | for (it = m_jumpaddr.begin(); it != m_jumpaddr.end(); it++) | ||
| 106 | out << (*it).first << " " << (*it).second << endl; | ||
| 107 | |||
| 108 | } | ||
| 109 | #endif | ||
diff --git a/ue3/mycpu/cprogram.h b/ue3/mycpu/cprogram.h new file mode 100644 index 0000000..25d0ce2 --- /dev/null +++ b/ue3/mycpu/cprogram.h | |||
| @@ -0,0 +1,106 @@ | |||
| 1 | /** | ||
| 2 | * @module cprogram | ||
| 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) | ||
| 4 | * @brief class for parsing and saving the program instuctions. | ||
| 5 | * @date 17.04.2009 | ||
| 6 | */ | ||
| 7 | |||
| 8 | #ifndef CPROGRAM_H | ||
| 9 | #define CPROGRAM_H | ||
| 10 | |||
| 11 | #include <stdexcept> | ||
| 12 | #include <string> | ||
| 13 | #include <vector> | ||
| 14 | #include <map> | ||
| 15 | #ifdef DEBUG | ||
| 16 | # include <iostream> | ||
| 17 | #endif | ||
| 18 | #include "cdat.h" | ||
| 19 | /** | ||
| 20 | * @class CProgram | ||
| 21 | * | ||
| 22 | * Parses a simple line based scriptfile with some limitations: | ||
| 23 | * first function (starting a block) must be a read-command, | ||
| 24 | * last must be a write-command (ending this block). | ||
| 25 | * | ||
| 26 | * read- and write-commands have hard coded parameters, number#1 being a filetype. | ||
| 27 | * Classes handling certain filetypes must be of type CFile. | ||
| 28 | * Custom functions will be passed to CFile::callFunc(). | ||
| 29 | * | ||
| 30 | * On error ParserError will be thrown. | ||
| 31 | */ | ||
| 32 | class CProgram | ||
| 33 | { | ||
| 34 | public: | ||
| 35 | |||
| 36 | /** | ||
| 37 | * @method CProgram | ||
| 38 | * @brief Default ctor | ||
| 39 | * @param programfile filename | ||
| 40 | * @return - | ||
| 41 | * @globalvars none | ||
| 42 | * @exception bad_alloc | ||
| 43 | * @conditions none | ||
| 44 | */ | ||
| 45 | CProgram(const std::string& progfile); | ||
| 46 | |||
| 47 | /** | ||
| 48 | * @method ~CScriptparser | ||
| 49 | * @brief Default dtor | ||
| 50 | * @param - | ||
| 51 | * @return - | ||
| 52 | * @globalvars none | ||
| 53 | * @exception none | ||
| 54 | * @conditions none | ||
| 55 | */ | ||
| 56 | ~CProgram(); | ||
| 57 | |||
| 58 | CDat getMaxProgramCount() | ||
| 59 | { | ||
| 60 | return CDat(m_progsource.size()); | ||
| 61 | } | ||
| 62 | |||
| 63 | std::vector<std::string>& getInstruction(CDat linenr) | ||
| 64 | { | ||
| 65 | return m_progsource[linenr.getTypeValue()]; | ||
| 66 | } | ||
| 67 | |||
| 68 | std::map<std::string, unsigned int>& getJumpAddrs() | ||
| 69 | { | ||
| 70 | return m_jumpaddr; | ||
| 71 | } | ||
| 72 | #ifdef DEBUG | ||
| 73 | /** | ||
| 74 | * @method dump | ||
| 75 | * @brief Dumps the program source to ostream | ||
| 76 | * @param out output stream | ||
| 77 | * @return - | ||
| 78 | * @globalvars | ||
| 79 | * @exception | ||
| 80 | * @conditions | ||
| 81 | */ | ||
| 82 | void dump(std::ostream& out); | ||
| 83 | #endif | ||
| 84 | |||
| 85 | protected: | ||
| 86 | /** | ||
| 87 | * @method parse | ||
| 88 | * @brief parse the program file | ||
| 89 | * @param - | ||
| 90 | * @return - | ||
| 91 | * @globalvars none | ||
| 92 | * @exception ParserError | ||
| 93 | * @conditions none | ||
| 94 | */ | ||
| 95 | void parse(); | ||
| 96 | |||
| 97 | private: | ||
| 98 | /* members */ | ||
| 99 | std::string m_programfile; | ||
| 100 | std::vector<std::vector<std::string> > m_progsource; | ||
| 101 | std::map<std::string, unsigned int> m_jumpaddr; | ||
| 102 | }; | ||
| 103 | |||
| 104 | #endif | ||
| 105 | |||
| 106 | /* vim: set et sw=2 ts=2: */ | ||
diff --git a/ue3/mycpu/mycpu.cpp b/ue3/mycpu/mycpu.cpp new file mode 100644 index 0000000..af6fe08 --- /dev/null +++ b/ue3/mycpu/mycpu.cpp | |||
| @@ -0,0 +1,94 @@ | |||
| 1 | /** | ||
| 2 | * @module mycpu | ||
| 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) | ||
| 4 | * @brief mycpu reads a program file and optional a memory file given as | ||
| 5 | * commandline option. | ||
| 6 | * On error (e.g. unknown function) the program will terminate | ||
| 7 | * @date 11.05.2009 | ||
| 8 | * @par Exercise | ||
| 9 | * 3 | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include <iostream> | ||
| 13 | #include <boost/program_options.hpp> | ||
| 14 | #include "ccpu.h" | ||
| 15 | |||
| 16 | using namespace std; | ||
| 17 | namespace po = boost::program_options; | ||
| 18 | |||
| 19 | /** | ||
| 20 | * @func main | ||
| 21 | * @brief program entry point | ||
| 22 | * @param argc standard parameter of main | ||
| 23 | * @param argv standard parameter of main | ||
| 24 | * @return 0 on success, not 0 otherwise | ||
| 25 | * @globalvars none | ||
| 26 | * @exception none | ||
| 27 | * @conditions none | ||
| 28 | * | ||
| 29 | * setup commandline options, parse them and pass scriptfile to scriptparser | ||
| 30 | * instance. On error print error message to stderr. | ||
| 31 | * Unknown commandline options will print a usage message. | ||
| 32 | */ | ||
| 33 | int main(int argc, char* argv[]) | ||
| 34 | { | ||
| 35 | string me(argv[0]); | ||
| 36 | |||
| 37 | /* define commandline options */ | ||
| 38 | po::options_description desc("Allowed options"); | ||
| 39 | desc.add_options() | ||
| 40 | ("help,h", "this help message") | ||
| 41 | ("progfile,c", po::value<string>(), "program file") | ||
| 42 | ("memfile,m", po::value<string>(), "memory file"); | ||
| 43 | |||
| 44 | /* parse commandline options */ | ||
| 45 | po::variables_map vm; | ||
| 46 | try | ||
| 47 | { | ||
| 48 | po::store(po::parse_command_line(argc, argv, desc), vm); | ||
| 49 | po::notify(vm); | ||
| 50 | } | ||
| 51 | catch(po::error& ex) | ||
| 52 | { | ||
| 53 | cerr << "Error: " << ex.what() << endl; | ||
| 54 | } | ||
| 55 | |||
| 56 | /* print usage upon request or missing params */ | ||
| 57 | if (vm.count("help") || !vm.count("progfile")) | ||
| 58 | { | ||
| 59 | cout << "Usage: " << me << " -c <programfile> [ -m <memoryfile>]" << endl; | ||
| 60 | cout << desc << endl; | ||
| 61 | if ( vm.count("help")) | ||
| 62 | return 0; | ||
| 63 | return 1; | ||
| 64 | } | ||
| 65 | |||
| 66 | string memfile(""); | ||
| 67 | if (vm.count("memfile")) | ||
| 68 | memfile = vm["memfile"].as<string>(); | ||
| 69 | |||
| 70 | CCPU cpu(vm["progfile"].as<string>(), memfile); | ||
| 71 | cpu.proceed(); | ||
| 72 | |||
| 73 | /* CScriptparser parser(vm["c"].as<string>()); | ||
| 74 | try | ||
| 75 | { | ||
| 76 | parser.parse(); | ||
| 77 | } | ||
| 78 | catch(CScriptparser::ParserError& ex) | ||
| 79 | { | ||
| 80 | cerr << me << ": Error while processing scriptfile: " << ex.what() << endl; | ||
| 81 | if (!ex.getLine().empty()) | ||
| 82 | cerr << "Scriptline: '" << ex.getLine() << "'" << endl; | ||
| 83 | return 1; | ||
| 84 | } | ||
| 85 | catch(exception& ex) | ||
| 86 | { | ||
| 87 | cerr << me << ": Unexpected exception: " << ex.what() << endl; | ||
| 88 | return 1; | ||
| 89 | }*/ | ||
| 90 | |||
| 91 | return 0; | ||
| 92 | } | ||
| 93 | |||
| 94 | /* vim: set et sw=2 ts=2: */ | ||
diff --git a/ue3/mycpu/test/test_prog b/ue3/mycpu/test/test_prog new file mode 100644 index 0000000..7c5f929 --- /dev/null +++ b/ue3/mycpu/test/test_prog | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | # set R2 = 10 | ||
| 2 | load R1, R2 | ||
| 3 | |||
| 4 | # start of loop | ||
| 5 | label Loop: | ||
| 6 | inc R3 | ||
| 7 | sub R4, R3, R2 | ||
| 8 | test R4 | ||
| 9 | jumpz EndLoop | ||
| 10 | write WDEZ, R3 | ||
| 11 | jumpa Loop | ||
| 12 | |||
| 13 | label EndLoop: | ||
diff --git a/ue3/mycpu/test/test_prog_mem b/ue3/mycpu/test/test_prog_mem new file mode 100644 index 0000000..f599e28 --- /dev/null +++ b/ue3/mycpu/test/test_prog_mem | |||
| @@ -0,0 +1 @@ | |||
| 10 | |||
