diff options
| author | manuel <manuel@nc8430.lan> | 2009-05-14 18:58:48 +0200 |
|---|---|---|
| committer | manuel <manuel@nc8430.lan> | 2009-05-14 18:58:48 +0200 |
| commit | 53b68ac658569dea3f5a26745dacf2a9aca0b266 (patch) | |
| tree | 269af83394e84e2a7142dde87fb15dce413aa5c9 /ue3/mycpu/cmem.h | |
| parent | ddf63e2765a6b225d18c59321595e69e1a126e0c (diff) | |
| parent | 3563c6dfd0f5f102cb748ecc6ad318601990515e (diff) | |
| download | ooprog-53b68ac658569dea3f5a26745dacf2a9aca0b266.tar.gz ooprog-53b68ac658569dea3f5a26745dacf2a9aca0b266.tar.bz2 ooprog-53b68ac658569dea3f5a26745dacf2a9aca0b266.zip | |
trying merging cpu-mm with default branch
Diffstat (limited to 'ue3/mycpu/cmem.h')
| -rw-r--r-- | ue3/mycpu/cmem.h | 133 |
1 files changed, 75 insertions, 58 deletions
diff --git a/ue3/mycpu/cmem.h b/ue3/mycpu/cmem.h index 356c9c0..5045c34 100644 --- a/ue3/mycpu/cmem.h +++ b/ue3/mycpu/cmem.h | |||
| @@ -1,92 +1,109 @@ | |||
| 1 | /** | 1 | /** |
| 2 | * @module cmem | 2 | * @module cmem |
| 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) | 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) |
| 4 | * @brief class for providing 256 registers. | 4 | * @brief Memory template and memory definition for CCPU |
| 5 | * @date 11.05.2009 | 5 | * @date 10.05.2009 |
| 6 | */ | 6 | */ |
| 7 | 7 | ||
| 8 | #ifndef CMEM_H | 8 | #ifndef CMEM_H |
| 9 | #define CMEM_H | 9 | #define CMEM_H 1 |
| 10 | 10 | ||
| 11 | #include <vector> | ||
| 12 | #include <istream> | ||
| 13 | #include <sstream> | ||
| 11 | #include <stdexcept> | 14 | #include <stdexcept> |
| 12 | #include <string> | 15 | #include <boost/lexical_cast.hpp> |
| 13 | |||
| 14 | #include <set> | ||
| 15 | #ifdef DEBUG | 16 | #ifdef DEBUG |
| 16 | # include <iostream> | 17 | # include <iostream> |
| 18 | # include <iomanip> | ||
| 17 | #endif | 19 | #endif |
| 18 | #include "cdat.h" | 20 | #include "cdat.h" |
| 19 | |||
| 20 | 21 | ||
| 21 | #define MAX_REGISTER 256; | ||
| 22 | /** | 22 | /** |
| 23 | * @class CMem | 23 | * @class CVectorMem |
| 24 | * | 24 | * |
| 25 | * Parses a simple line based scriptfile with some limitations: | 25 | * Extends std::vector template for use as memory for CCPU. |
| 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 | */ | 26 | */ |
| 35 | class CMem | 27 | template <class T, class Allocator=std::allocator<T> > |
| 28 | class CVectorMem | ||
| 29 | : public std::vector<T, Allocator> | ||
| 36 | { | 30 | { |
| 37 | public: | 31 | public: |
| 32 | using std::vector<T, Allocator>::size; | ||
| 33 | using std::vector<T, Allocator>::at; | ||
| 38 | 34 | ||
| 39 | /** | 35 | /** |
| 40 | * @method CMem | 36 | * @method initialize |
| 41 | * @brief Default ctor | 37 | * @brief initialize the vector with the content of istream. istream is |
| 42 | * @param memoryfile filename | 38 | * read per line. empty lines will add unitialized elements. |
| 43 | * @return - | 39 | * @param in inputstream to read from |
| 40 | * @return void | ||
| 44 | * @globalvars none | 41 | * @globalvars none |
| 45 | * @exception bad_alloc | 42 | * @exception std::runtime_error |
| 46 | * @conditions none | 43 | * @conditions none |
| 47 | */ | 44 | */ |
| 48 | CMem(const std::string& memfile); | 45 | void initialize(std::istream& in) |
| 46 | { | ||
| 47 | if (!in.good()) | ||
| 48 | return; | ||
| 49 | |||
| 50 | std::string line; | ||
| 51 | unsigned i = 0; | ||
| 52 | while (!in.eof() && in.good()) | ||
| 53 | { | ||
| 54 | ++i; | ||
| 55 | std::getline(in, line); | ||
| 49 | 56 | ||
| 57 | /* skip last line if it's empty */ | ||
| 58 | if (line.empty() && in.eof()) | ||
| 59 | break; | ||
| 60 | |||
| 61 | T value; | ||
| 62 | try | ||
| 63 | { | ||
| 64 | if (!line.empty()) | ||
| 65 | value = boost::lexical_cast<T>(line); | ||
| 66 | } | ||
| 67 | catch(boost::bad_lexical_cast& ex) | ||
| 68 | { | ||
| 69 | std::stringstream sstr; | ||
| 70 | sstr << "Unable to convert input (line " << i << "): " << ex.what(); | ||
| 71 | throw std::runtime_error(sstr.str()); | ||
| 72 | } | ||
| 73 | |||
| 74 | push_back(value); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | #if DEBUG | ||
| 50 | /** | 79 | /** |
| 51 | * @method ~CMem | 80 | * @method dump |
| 52 | * @brief Default dtor | 81 | * @brief dumps contents of vector to outputstream |
| 53 | * @param - | 82 | * @param out outputstream to write to |
| 54 | * @return - | 83 | * @return void |
| 55 | * @globalvars none | 84 | * @globalvars none |
| 56 | * @exception none | 85 | * @exception none |
| 57 | * @conditions none | 86 | * @conditions none |
| 58 | */ | 87 | */ |
| 59 | ~CMem(); | 88 | void dump(std::ostream& out) |
| 60 | 89 | { | |
| 61 | CDat& getRegister(const std::string reg); | 90 | out << "[MEMORY DUMP]" << std::endl; |
| 62 | 91 | for(unsigned i = 0; i < size(); ++i) | |
| 63 | std::string getMemAt(const std::string addr); | 92 | { |
| 64 | 93 | out << "[" << std::setw(4) << std::setfill('0') << i << "] " | |
| 65 | void setMemAt(const std::string addr, const CDat& value); | 94 | << at(i) << std::endl; |
| 66 | 95 | } | |
| 67 | #ifdef DEBUG | 96 | } |
| 68 | /** | ||
| 69 | * @method dump | ||
| 70 | * @brief Dumps the resgister contnent to ostream | ||
| 71 | * @param out output stream | ||
| 72 | * @return - | ||
| 73 | * @globalvars | ||
| 74 | * @exception | ||
| 75 | * @conditions | ||
| 76 | */ | ||
| 77 | void dump(std::ostream& out); | ||
| 78 | #endif | 97 | #endif |
| 79 | |||
| 80 | |||
| 81 | private: | ||
| 82 | |||
| 83 | unsigned int getRegNr(const std::string reg); | ||
| 84 | |||
| 85 | /* members */ | ||
| 86 | std::string m_memfile; | ||
| 87 | std::vector<CDat> m_registers; | ||
| 88 | }; | 98 | }; |
| 89 | 99 | ||
| 100 | /** | ||
| 101 | * @class CMem | ||
| 102 | * | ||
| 103 | * Memory definition for CCPU | ||
| 104 | */ | ||
| 105 | typedef CVectorMem<CDat> CMem; | ||
| 106 | |||
| 90 | #endif | 107 | #endif |
| 91 | 108 | ||
| 92 | /* vim: set et sw=2 ts=2: */ | 109 | /* vim: set et sw=2 ts=2: */ |
