1.1 --- a/c++/rgb-assembler/memory.h Sun Dec 24 00:47:34 2017 +0100
1.2 +++ b/c++/rgb-assembler/memory.h Mon Dec 25 00:24:07 2017 +0100
1.3 @@ -22,42 +22,85 @@
1.4
1.5 const address_t MEMORY_SIZE = 1024;
1.6
1.7 -template<typename T> T logMemoryError(const address_t &index) {
1.8 - wprintf(L"memory error: index = %d, sizeof(T) = %d, MEMORY_SIZE = %d\n", index, sizeof (T), MEMORY_SIZE);
1.9 - // TODO: return error value or throw exception
1.10 - return T();
1.11 -}
1.12 +class Memory {
1.13 +private:
1.14
1.15 -/**
1.16 - * Reads data on given position in memory and increments the index (position).
1.17 - *
1.18 - * @param memory array of bytes / octets
1.19 - * @param index offset in same units as memory type
1.20 - * @return value found at given position
1.21 - */
1.22 -template<typename T> T read(octet_t * memory, address_t &index) {
1.23 - // TODO: map higher memory to static hardcoded areas or peripherals
1.24 - if (index + sizeof (T) <= MEMORY_SIZE) {
1.25 - T * value = reinterpret_cast<T*> (memory + index);
1.26 - index += sizeof (T);
1.27 - return *value;
1.28 - } else {
1.29 - return logMemoryError<T>(index);
1.30 + template<typename T> T logMemoryError(const address_t &index) {
1.31 + wprintf(L"memory error: index = %d, sizeof(T) = %d, MEMORY_SIZE = %d\n", index, sizeof (T), MEMORY_SIZE);
1.32 + // TODO: return error value or throw exception
1.33 + return T();
1.34 }
1.35 -}
1.36 + octet_t * memory;
1.37 + address_t index;
1.38
1.39 -/**
1.40 - * Writes data to given position in memory and increments the index (position).
1.41 - * @param memory array of bytes / octets
1.42 - * @param index offset in same units as memory type
1.43 - * @param value value to be written at given position
1.44 - */
1.45 -template<typename T> T write(octet_t * memory, address_t &index, const T value) {
1.46 - if (index + sizeof (T) <= MEMORY_SIZE) {
1.47 - T * m = reinterpret_cast<T*> (memory + index);
1.48 - *m = value;
1.49 - index += sizeof (value);
1.50 - } else {
1.51 - return logMemoryError<T>(index);
1.52 +public:
1.53 +
1.54 + Memory() {
1.55 + memory = (octet_t*) malloc(MEMORY_SIZE);
1.56 + index = 0;
1.57 }
1.58 -}
1.59 +
1.60 + virtual ~Memory() {
1.61 + free(memory);
1.62 + memory = nullptr;
1.63 + }
1.64 +
1.65 + /**
1.66 + * Reads data on given position in memory and increments the index (position).
1.67 + * @return value found at current position
1.68 + */
1.69 + template<typename T> T read() {
1.70 + // TODO: map higher memory to static hardcoded areas or peripherals
1.71 + if (index + sizeof (T) <= MEMORY_SIZE) {
1.72 + T * value = reinterpret_cast<T*> (memory + index);
1.73 + index += sizeof (T);
1.74 + return *value;
1.75 + } else {
1.76 + return logMemoryError<T>(index);
1.77 + }
1.78 + }
1.79 +
1.80 + /**
1.81 + * Writes data to current position in memory and increments the index (position).
1.82 + * @param value value to be written at given position
1.83 + */
1.84 + template<typename T> T write(const T value) {
1.85 + if (index + sizeof (T) <= MEMORY_SIZE) {
1.86 + T * m = reinterpret_cast<T*> (memory + index);
1.87 + *m = value;
1.88 + index += sizeof (value);
1.89 + } else {
1.90 + return logMemoryError<T>(index);
1.91 + }
1.92 + }
1.93 +
1.94 + void setIndex(address_t &index) {
1.95 + this->index = index;
1.96 + }
1.97 +
1.98 + address_t getIndex() {
1.99 + return index;
1.100 + }
1.101 +
1.102 + /**
1.103 + * FIXME: rename
1.104 + * @return
1.105 + */
1.106 + bool isInside() {
1.107 + return index < MEMORY_SIZE;
1.108 + }
1.109 +
1.110 + /**
1.111 + * FIXME: rename, refactor
1.112 + */
1.113 + void start() {
1.114 + index = 0;
1.115 + }
1.116 +
1.117 + /**
1.118 + * FIXME: rename, refactor
1.119 + */
1.120 + void finish() {
1.121 + index = MEMORY_SIZE;
1.122 + }
1.123 +};