diff -r f049c3d3244d -r b997cbf9e30b c++/rgb-assembler/memory.h --- a/c++/rgb-assembler/memory.h Sun Dec 24 00:47:34 2017 +0100 +++ b/c++/rgb-assembler/memory.h Mon Dec 25 00:24:07 2017 +0100 @@ -22,42 +22,85 @@ const address_t MEMORY_SIZE = 1024; -template T logMemoryError(const address_t &index) { - wprintf(L"memory error: index = %d, sizeof(T) = %d, MEMORY_SIZE = %d\n", index, sizeof (T), MEMORY_SIZE); - // TODO: return error value or throw exception - return T(); -} +class Memory { +private: -/** - * Reads data on given position in memory and increments the index (position). - * - * @param memory array of bytes / octets - * @param index offset in same units as memory type - * @return value found at given position - */ -template T read(octet_t * memory, address_t &index) { - // TODO: map higher memory to static hardcoded areas or peripherals - if (index + sizeof (T) <= MEMORY_SIZE) { - T * value = reinterpret_cast (memory + index); - index += sizeof (T); - return *value; - } else { - return logMemoryError(index); + template T logMemoryError(const address_t &index) { + wprintf(L"memory error: index = %d, sizeof(T) = %d, MEMORY_SIZE = %d\n", index, sizeof (T), MEMORY_SIZE); + // TODO: return error value or throw exception + return T(); } -} + octet_t * memory; + address_t index; -/** - * Writes data to given position in memory and increments the index (position). - * @param memory array of bytes / octets - * @param index offset in same units as memory type - * @param value value to be written at given position - */ -template T write(octet_t * memory, address_t &index, const T value) { - if (index + sizeof (T) <= MEMORY_SIZE) { - T * m = reinterpret_cast (memory + index); - *m = value; - index += sizeof (value); - } else { - return logMemoryError(index); +public: + + Memory() { + memory = (octet_t*) malloc(MEMORY_SIZE); + index = 0; } -} + + virtual ~Memory() { + free(memory); + memory = nullptr; + } + + /** + * Reads data on given position in memory and increments the index (position). + * @return value found at current position + */ + template T read() { + // TODO: map higher memory to static hardcoded areas or peripherals + if (index + sizeof (T) <= MEMORY_SIZE) { + T * value = reinterpret_cast (memory + index); + index += sizeof (T); + return *value; + } else { + return logMemoryError(index); + } + } + + /** + * Writes data to current position in memory and increments the index (position). + * @param value value to be written at given position + */ + template T write(const T value) { + if (index + sizeof (T) <= MEMORY_SIZE) { + T * m = reinterpret_cast (memory + index); + *m = value; + index += sizeof (value); + } else { + return logMemoryError(index); + } + } + + void setIndex(address_t &index) { + this->index = index; + } + + address_t getIndex() { + return index; + } + + /** + * FIXME: rename + * @return + */ + bool isInside() { + return index < MEMORY_SIZE; + } + + /** + * FIXME: rename, refactor + */ + void start() { + index = 0; + } + + /** + * FIXME: rename, refactor + */ + void finish() { + index = MEMORY_SIZE; + } +};