# HG changeset patch # User František Kučera # Date 1514067891 -3600 # Node ID b9ceffdcaf148fb41d4769ec55d55a04c56fc43c # Parent 17785b69430d9da2d421d54e04b995366bc3513b move memory functions to a header file + introduce Command classes diff -r 17785b69430d -r b9ceffdcaf14 c++/rgb-assembler/Command.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/c++/rgb-assembler/Command.h Sat Dec 23 23:24:51 2017 +0100 @@ -0,0 +1,16 @@ +#pragma once + +#include "types.h" +#include "memory.h" + +class Command { +public: + /** + * Process command at given address, read parameters if any. And shift address to new one (after last parameter) + * @param memory + * @param index address of the command in the memory + */ + virtual void process(octet_t * memory, address_t &index) = 0; +private: + +}; diff -r 17785b69430d -r b9ceffdcaf14 c++/rgb-assembler/commands/Goto.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/c++/rgb-assembler/commands/Goto.h Sat Dec 23 23:24:51 2017 +0100 @@ -0,0 +1,20 @@ +#pragma once + +#include + +#include "../Command.h" + +namespace commands { + +class Goto : public Command { +public: + + void process(octet_t* memory, address_t& index) override { + index = read(memory, index); + wprintf(L"GOTO %*d\n", 5, index); + } +private: + +}; + +} diff -r 17785b69430d -r b9ceffdcaf14 c++/rgb-assembler/memory.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/c++/rgb-assembler/memory.h Sat Dec 23 23:24:51 2017 +0100 @@ -0,0 +1,45 @@ +#pragma once + +#include "types.h" + +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(); +} + +/** + * 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); + } +} + +/** + * 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); + } +} diff -r 17785b69430d -r b9ceffdcaf14 c++/rgb-assembler/nbproject/configurations.xml --- a/c++/rgb-assembler/nbproject/configurations.xml Sat Dec 23 20:13:24 2017 +0100 +++ b/c++/rgb-assembler/nbproject/configurations.xml Sat Dec 23 23:24:51 2017 +0100 @@ -4,6 +4,9 @@ + Command.h + commands/Goto.h + memory.h types.h 8 + + + + + + @@ -67,6 +76,12 @@ 5 + + + + + + diff -r 17785b69430d -r b9ceffdcaf14 c++/rgb-assembler/rgb-assembler.cpp --- a/c++/rgb-assembler/rgb-assembler.cpp Sat Dec 23 20:13:24 2017 +0100 +++ b/c++/rgb-assembler/rgb-assembler.cpp Sat Dec 23 23:24:51 2017 +0100 @@ -3,18 +3,19 @@ #include #include #include - +#include #include #include #include "types.h" +#include "memory.h" +#include "Command.h" +#include "commands/Goto.h" using namespace std; // TODO: strong typedefs http://www.boost.org/doc/libs/1_61_0/libs/serialization/doc/strong_typedef.html ? -const address_t MEMORY_SIZE = 1024; - /** * Skip to the given address. * parameter: address_t @@ -71,46 +72,6 @@ // TODO: more commands, better numbers -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(); -} - -/** - * 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); - } -} - -/** - * 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); - } -} - int main(int argc, char* argv[]) { setlocale(LC_ALL, "");