# HG changeset patch # User František Kučera # Date 1514158467 -3600 # Node ID 78c4d6b5349988e9094f3906edc998ac2afd15ae # Parent b997cbf9e30b81846afeecae18988c1d1f9d6873 rename memory.h to Memory.h diff -r b997cbf9e30b -r 78c4d6b53499 c++/rgb-assembler/Command.h --- a/c++/rgb-assembler/Command.h Mon Dec 25 00:24:07 2017 +0100 +++ b/c++/rgb-assembler/Command.h Mon Dec 25 00:34:27 2017 +0100 @@ -19,7 +19,7 @@ #pragma once #include "types.h" -#include "memory.h" +#include "Memory.h" class Command { public: diff -r b997cbf9e30b -r 78c4d6b53499 c++/rgb-assembler/Memory.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/c++/rgb-assembler/Memory.h Mon Dec 25 00:34:27 2017 +0100 @@ -0,0 +1,106 @@ +/** + * RGB assembler + * Copyright © 2017 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#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); + // TODO: return error value or throw exception + return T(); + } + octet_t * memory; + address_t 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; + } +}; diff -r b997cbf9e30b -r 78c4d6b53499 c++/rgb-assembler/commands/End.h --- a/c++/rgb-assembler/commands/End.h Mon Dec 25 00:24:07 2017 +0100 +++ b/c++/rgb-assembler/commands/End.h Mon Dec 25 00:34:27 2017 +0100 @@ -23,7 +23,7 @@ #include #include "../Command.h" -#include "../memory.h" +#include "../Memory.h" using namespace std; diff -r b997cbf9e30b -r 78c4d6b53499 c++/rgb-assembler/commands/IncrementDecrement.h --- a/c++/rgb-assembler/commands/IncrementDecrement.h Mon Dec 25 00:24:07 2017 +0100 +++ b/c++/rgb-assembler/commands/IncrementDecrement.h Mon Dec 25 00:34:27 2017 +0100 @@ -23,7 +23,7 @@ #include #include "../Command.h" -#include "../memory.h" +#include "../Memory.h" using namespace std; diff -r b997cbf9e30b -r 78c4d6b53499 c++/rgb-assembler/memory.h --- a/c++/rgb-assembler/memory.h Mon Dec 25 00:24:07 2017 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,106 +0,0 @@ -/** - * RGB assembler - * Copyright © 2017 František Kučera (frantovo.cz) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#pragma once - -#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); - // TODO: return error value or throw exception - return T(); - } - octet_t * memory; - address_t 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; - } -}; diff -r b997cbf9e30b -r 78c4d6b53499 c++/rgb-assembler/nbproject/configurations.xml --- a/c++/rgb-assembler/nbproject/configurations.xml Mon Dec 25 00:24:07 2017 +0100 +++ b/c++/rgb-assembler/nbproject/configurations.xml Mon Dec 25 00:34:27 2017 +0100 @@ -12,7 +12,7 @@ commands/IncrementDecrement.h commands/Sleep.h commands.h - memory.h + Memory.h types.h - + @@ -110,7 +110,7 @@ - + diff -r b997cbf9e30b -r 78c4d6b53499 c++/rgb-assembler/rgb-assembler.cpp --- a/c++/rgb-assembler/rgb-assembler.cpp Mon Dec 25 00:24:07 2017 +0100 +++ b/c++/rgb-assembler/rgb-assembler.cpp Mon Dec 25 00:34:27 2017 +0100 @@ -24,7 +24,7 @@ #include #include "types.h" -#include "memory.h" +#include "Memory.h" #include "Command.h" #include "commands.h" #include "commands/Goto.h"