# HG changeset patch # User František Kučera # Date 1514157847 -3600 # Node ID b997cbf9e30b81846afeecae18988c1d1f9d6873 # Parent f049c3d3244d20f0176164ef6d1e11ae9d72f2d1 wrap memory in a class diff -r f049c3d3244d -r b997cbf9e30b c++/rgb-assembler/Command.h --- a/c++/rgb-assembler/Command.h Sun Dec 24 00:47:34 2017 +0100 +++ b/c++/rgb-assembler/Command.h Mon Dec 25 00:24:07 2017 +0100 @@ -24,11 +24,8 @@ class Command { public: /** - * Process command at given address, read parameters if any. And shift address to new one (after last parameter) + * Process command at given address, read parameters if any. * @param memory - * @param index address of the command in the memory */ - virtual void process(octet_t * memory, address_t &index) = 0; -private: - + virtual void process(Memory &memory) = 0; }; diff -r f049c3d3244d -r b997cbf9e30b c++/rgb-assembler/commands/Color.h --- a/c++/rgb-assembler/commands/Color.h Sun Dec 24 00:47:34 2017 +0100 +++ b/c++/rgb-assembler/commands/Color.h Mon Dec 25 00:24:07 2017 +0100 @@ -27,15 +27,13 @@ class Color : public Command { public: - void process(octet_t* memory, address_t& index) override { - led_t led = read(memory, index); - color_t r = read(memory, index); - color_t g = read(memory, index); - color_t b = read(memory, index); + void process(Memory &memory) override { + led_t led = memory.read(); + color_t r = memory.read(); + color_t g = memory.read(); + color_t b = memory.read(); wprintf(L"COLOR %02X %02X %02X → %d\n", r, g, b, led); } -private: - }; } diff -r f049c3d3244d -r b997cbf9e30b c++/rgb-assembler/commands/End.h --- a/c++/rgb-assembler/commands/End.h Sun Dec 24 00:47:34 2017 +0100 +++ b/c++/rgb-assembler/commands/End.h Mon Dec 25 00:24:07 2017 +0100 @@ -32,12 +32,10 @@ class End : public Command { public: - void process(octet_t* memory, address_t& index) override { + void process(Memory &memory) override { wprintf(L"END\n"); - index = MEMORY_SIZE; + memory.finish(); } -private: - }; } diff -r f049c3d3244d -r b997cbf9e30b c++/rgb-assembler/commands/Goto.h --- a/c++/rgb-assembler/commands/Goto.h Sun Dec 24 00:47:34 2017 +0100 +++ b/c++/rgb-assembler/commands/Goto.h Mon Dec 25 00:24:07 2017 +0100 @@ -27,12 +27,11 @@ class Goto : public Command { public: - void process(octet_t* memory, address_t& index) override { - index = read(memory, index); + void process(Memory &memory) override { + address_t index = memory.read(); + memory.setIndex(index); wprintf(L"GOTO %*d\n", 5, index); } -private: - }; } diff -r f049c3d3244d -r b997cbf9e30b c++/rgb-assembler/commands/GotoCompare.h --- a/c++/rgb-assembler/commands/GotoCompare.h Sun Dec 24 00:47:34 2017 +0100 +++ b/c++/rgb-assembler/commands/GotoCompare.h Mon Dec 25 00:24:07 2017 +0100 @@ -27,24 +27,31 @@ class GotoCompare : public Command { public: - void process(octet_t* memory, address_t& index) override { - address_t aa = read(memory, index); - address_t ab = read(memory, index); - address_t eq = read(memory, index); - address_t gt = read(memory, index); - address_t lt = read(memory, index); + void process(Memory &memory) override { + address_t aa = memory.read(); + address_t ab = memory.read(); + address_t eq = memory.read(); + address_t gt = memory.read(); + address_t lt = memory.read(); - octet_t a = read(memory, aa); - octet_t b = read(memory, ab); + address_t originalAddress = memory.getIndex(); + // TODO: add to Memory methods read(address_t) and write(address_t, T) + memory.setIndex(aa); + octet_t a = memory.read(); + memory.setIndex(ab); + octet_t b = memory.read(); + memory.setIndex(originalAddress); + + address_t index; if (a == b) index = eq; else if (a > b) index = gt; else index = lt; + memory.setIndex(index); + wprintf(L"GOTO COMPARE a = %02X, b = %02X, eq = %d, gt = %d, lt = %d → %d\n", a, b, eq, gt, lt, index); } -private: - }; } diff -r f049c3d3244d -r b997cbf9e30b c++/rgb-assembler/commands/IncrementDecrement.h --- a/c++/rgb-assembler/commands/IncrementDecrement.h Sun Dec 24 00:47:34 2017 +0100 +++ b/c++/rgb-assembler/commands/IncrementDecrement.h Mon Dec 25 00:24:07 2017 +0100 @@ -37,13 +37,16 @@ this->change = change; } - void process(octet_t* memory, address_t& index) override { - address_t address = read(memory, index); - address_t address_r = address; - address_t address_w = address_r; - octet_t value = read(memory, address_r); + void process(Memory &memory) override { + address_t address = memory.read(); + address_t originalAddress = memory.getIndex(); + // TODO: add to Memory methods read(address_t) and write(address_t, T) + memory.setIndex(address); + octet_t value = memory.read(); value = increment ? value + change : value - change; - write(memory, address_w, value); + memory.setIndex(address); + memory.write(value); + memory.setIndex(originalAddress); wprintf(L"%sCREMENT %*d → %02X\n", (increment ? "IN" : "DE"), 5, address, value); } private: diff -r f049c3d3244d -r b997cbf9e30b c++/rgb-assembler/commands/Sleep.h --- a/c++/rgb-assembler/commands/Sleep.h Sun Dec 24 00:47:34 2017 +0100 +++ b/c++/rgb-assembler/commands/Sleep.h Mon Dec 25 00:24:07 2017 +0100 @@ -31,13 +31,11 @@ class Sleep : public Command { public: - void process(octet_t* memory, address_t& index) override { - sleep_t delay = read(memory, index); + void process(Memory &memory) override { + sleep_t delay = memory.read(); wprintf(L"SLEEP %*d ms\n", 4, delay); this_thread::sleep_for(chrono::milliseconds(delay)); } -private: - }; } diff -r f049c3d3244d -r b997cbf9e30b c++/rgb-assembler/memory.h --- a/c++/rgb-assembler/memory.h Sun Dec 24 00:47:34 2017 +0100 +++ b/c++/rgb-assembler/memory.h Mon Dec 25 00:24:07 2017 +0100 @@ -22,42 +22,85 @@ 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(); -} +class Memory { +private: -/** - * 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); + 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; -/** - * 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); +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 f049c3d3244d -r b997cbf9e30b c++/rgb-assembler/rgb-assembler.cpp --- a/c++/rgb-assembler/rgb-assembler.cpp Sun Dec 24 00:47:34 2017 +0100 +++ b/c++/rgb-assembler/rgb-assembler.cpp Mon Dec 25 00:24:07 2017 +0100 @@ -40,41 +40,40 @@ setlocale(LC_ALL, ""); - // TODO: wrap in a class with read(), write() and setAddress() methods - octet_t * memory = (octet_t*) malloc(MEMORY_SIZE); - + Memory memory; + // Sample program / data: // TODO: load bytes from file, stdin, serial port, network… { - address_t a = 0; - write(memory, a, CMD_SLEEP); - write(memory, a, 255); - write(memory, a, CMD_SLEEP); - write(memory, a, 10); - write(memory, a, CMD_SLEEP); - write(memory, a, 255); - write(memory, a, CMD_GOTO); - write(memory, a, a + sizeof (address_t) + 2 * sizeof (command_t)); - write(memory, a, CMD_INVALID); - write(memory, a, CMD_INVALID); - write(memory, a, CMD_SLEEP); - write(memory, a, 255); - write(memory, a, CMD_COLOR); - write(memory, a, 23); - write(memory, a, 0); - write(memory, a, 200); - write(memory, a, 255); - write(memory, a, CMD_INCREMENT); - write(memory, a, 0); - write(memory, a, CMD_DECREMENT); - write(memory, a, 0); - write(memory, a, CMD_GOTO_COMPARE); - write(memory, a, 0); - write(memory, a, 0 + sizeof (command_t) + sizeof (sleep_t)); - write(memory, a, a - 3 * sizeof (address_t) - 2 * sizeof (command_t)); - write(memory, a, 0); - write(memory, a, a + sizeof (address_t)); - write(memory, a, CMD_END); + memory.write(CMD_SLEEP); + memory.write(255); + memory.write(CMD_SLEEP); + memory.write(10); + memory.write(CMD_SLEEP); + memory.write(255); + memory.write(CMD_GOTO); + memory.write(memory.getIndex() + sizeof (address_t) + 2 * sizeof (command_t)); + memory.write(CMD_INVALID); + memory.write(CMD_INVALID); + memory.write(CMD_SLEEP); + memory.write(255); + memory.write(CMD_COLOR); + memory.write(23); + memory.write(0); + memory.write(200); + memory.write(255); + memory.write(CMD_INCREMENT); + memory.write(0); + memory.write(CMD_DECREMENT); + memory.write(0); + memory.write(CMD_GOTO_COMPARE); + memory.write(0); + memory.write(0 + sizeof (command_t) + sizeof (sleep_t)); + memory.write(memory.getIndex() - 3 * sizeof (address_t) - 2 * sizeof (command_t)); + memory.write(0); + memory.write(memory.getIndex() + sizeof (address_t)); + memory.write(CMD_END); + memory.start(); } // Supported commands @@ -91,22 +90,20 @@ // Main loop / interpreter: - for (address_t i = 0; i < MEMORY_SIZE;) { - wprintf(L"command %*d = ", 4, i); - command_t commandCode = read(memory, i); + while (memory.isInside()) { + wprintf(L"command %*d = ", 4, memory.getIndex()); + command_t commandCode = memory.read(); wprintf(L"%02X ", commandCode); shared_ptr command = commands[commandCode]; if (command) { - command->process(memory, i); + command->process(memory); } else { wprintf(L"invalid command\n"); } } - free(memory); - memory = nullptr; wprintf(L"all done\n"); return 0; }