1.1 --- a/c++/rgb-assembler/rgb-assembler.cpp Sat Dec 23 17:33:49 2017 +0100
1.2 +++ b/c++/rgb-assembler/rgb-assembler.cpp Sat Dec 23 17:52:32 2017 +0100
1.3 @@ -76,6 +76,12 @@
1.4
1.5 // TODO: more commands, better numbers
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 +
1.13 /**
1.14 * Reads data on given position in memory and increments the index (position).
1.15 *
1.16 @@ -84,10 +90,14 @@
1.17 * @return value found at given position
1.18 */
1.19 template<typename T> T read(octet_t * memory, address_t &index) {
1.20 - // TODO: for addresses: map higher memory to static hardcoded areas or peripherals
1.21 - T * value = reinterpret_cast<T*> (memory + index);
1.22 - index += sizeof (T);
1.23 - return *value;
1.24 + // TODO: map higher memory to static hardcoded areas or peripherals
1.25 + if (index + sizeof (T) <= MEMORY_SIZE) {
1.26 + T * value = reinterpret_cast<T*> (memory + index);
1.27 + index += sizeof (T);
1.28 + return *value;
1.29 + } else {
1.30 + return logMemoryError<T>(index);
1.31 + }
1.32 }
1.33
1.34 /**
1.35 @@ -96,10 +106,14 @@
1.36 * @param index offset in same units as memory type
1.37 * @param value value to be written at given position
1.38 */
1.39 -template<typename T> void write(octet_t * memory, address_t &index, const T value) {
1.40 - T * m = reinterpret_cast<T*> (memory + index);
1.41 - *m = value;
1.42 - index += sizeof (value);
1.43 +template<typename T> T write(octet_t * memory, address_t &index, const T value) {
1.44 + if (index + sizeof (T) <= MEMORY_SIZE) {
1.45 + T * m = reinterpret_cast<T*> (memory + index);
1.46 + *m = value;
1.47 + index += sizeof (value);
1.48 + } else {
1.49 + return logMemoryError<T>(index);
1.50 + }
1.51 }
1.52
1.53 int main(int argc, char* argv[]) {