diff -r ff150572e8c0 -r d7eed4d972de c++/rgb-assembler/Memory.h --- a/c++/rgb-assembler/Memory.h Mon Dec 25 01:27:44 2017 +0100 +++ b/c++/rgb-assembler/Memory.h Mon Dec 25 01:34:06 2017 +0100 @@ -20,24 +20,25 @@ #include "types.h" -const address_t MEMORY_SIZE = 1024; - class Memory { private: 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); + wprintf(L"memory error: index = %d, sizeof(T) = %d, MEMORY_SIZE = %d\n", index, sizeof (T), memorySize); // TODO: return error value or throw exception return T(); } + octet_t * memory; + address_t memorySize; address_t index; public: - Memory() { - memory = (octet_t*) malloc(MEMORY_SIZE); - index = 0; + Memory(const address_t memorySize) { + this->memory = (octet_t*) malloc(memorySize); + this->memorySize = memorySize; + this->index = 0; } virtual ~Memory() { @@ -51,7 +52,7 @@ */ template T read() { // TODO: map higher memory to static hardcoded areas or peripherals - if (index + sizeof (T) <= MEMORY_SIZE) { + if (index + sizeof (T) <= memorySize) { T * value = reinterpret_cast (memory + index); index += sizeof (T); return *value; @@ -66,7 +67,7 @@ */ template T read(const address_t &address) { // TODO: map higher memory to static hardcoded areas or peripherals - if (address + sizeof (T) <= MEMORY_SIZE) { + if (address + sizeof (T) <= memorySize) { return *(reinterpret_cast (memory + address)); } else { return logMemoryError(address); @@ -78,7 +79,7 @@ * @param value value to be written at current position */ template T write(const T value) { - if (index + sizeof (T) <= MEMORY_SIZE) { + if (index + sizeof (T) <= memorySize) { T * m = reinterpret_cast (memory + index); *m = value; index += sizeof (value); @@ -92,7 +93,7 @@ * @param value value to be written at given position */ template T write(const address_t &address, const T value) { - if (address + sizeof (T) <= MEMORY_SIZE) { + if (address + sizeof (T) <= memorySize) { T * m = reinterpret_cast (memory + address); *m = value; } else { @@ -119,7 +120,7 @@ * @return whether the current position is inside the memory boundaries = still processing. */ bool isNotOver() { - return index < MEMORY_SIZE; + return index < memorySize; } /** @@ -133,6 +134,6 @@ * Set current position behind the end of the memory = stop processing. */ void setAddressToEnd() { - index = MEMORY_SIZE; + index = memorySize; } };