# HG changeset patch # User František Kučera # Date 1514161664 -3600 # Node ID ff150572e8c096deab73320aa9fc7c5753b2fe13 # Parent 78c4d6b5349988e9094f3906edc998ac2afd15ae memory: rename, refactor diff -r 78c4d6b53499 -r ff150572e8c0 c++/rgb-assembler/Memory.h --- a/c++/rgb-assembler/Memory.h Mon Dec 25 00:34:27 2017 +0100 +++ b/c++/rgb-assembler/Memory.h Mon Dec 25 01:27:44 2017 +0100 @@ -46,7 +46,7 @@ } /** - * Reads data on given position in memory and increments the index (position). + * Reads data on current position in memory and increments the index (position). * @return value found at current position */ template T read() { @@ -61,8 +61,21 @@ } /** + * Reads data on given position in memory (without affecting the current position). + * @return value found at given position + */ + template T read(const address_t &address) { + // TODO: map higher memory to static hardcoded areas or peripherals + if (address + sizeof (T) <= MEMORY_SIZE) { + return *(reinterpret_cast (memory + address)); + } else { + return logMemoryError(address); + } + } + + /** * Writes data to current position in memory and increments the index (position). - * @param value value to be written at given position + * @param value value to be written at current position */ template T write(const T value) { if (index + sizeof (T) <= MEMORY_SIZE) { @@ -74,33 +87,52 @@ } } - void setIndex(address_t &index) { + /** + * Writes data to given position in memory (without affecting the current position). + * @param value value to be written at given position + */ + template T write(const address_t &address, const T value) { + if (address + sizeof (T) <= MEMORY_SIZE) { + T * m = reinterpret_cast (memory + address); + *m = value; + } else { + return logMemoryError(address); + } + } + + /** + * Set current addres to given position. + * @param index + */ + void setAddress(address_t &index) { this->index = index; } - address_t getIndex() { + /** + * @return Current position in the memory. + */ + address_t getAddress() { return index; } /** - * FIXME: rename - * @return + * @return whether the current position is inside the memory boundaries = still processing. */ - bool isInside() { + bool isNotOver() { return index < MEMORY_SIZE; } - + /** - * FIXME: rename, refactor + * Set current position to the start of the memory = position 0. */ - void start() { + void setAddressToBeginning() { index = 0; } /** - * FIXME: rename, refactor + * Set current position behind the end of the memory = stop processing. */ - void finish() { + void setAddressToEnd() { index = MEMORY_SIZE; } }; diff -r 78c4d6b53499 -r ff150572e8c0 c++/rgb-assembler/commands/End.h --- a/c++/rgb-assembler/commands/End.h Mon Dec 25 00:34:27 2017 +0100 +++ b/c++/rgb-assembler/commands/End.h Mon Dec 25 01:27:44 2017 +0100 @@ -34,7 +34,7 @@ void process(Memory &memory) override { wprintf(L"END\n"); - memory.finish(); + memory.setAddressToEnd(); } }; diff -r 78c4d6b53499 -r ff150572e8c0 c++/rgb-assembler/commands/Goto.h --- a/c++/rgb-assembler/commands/Goto.h Mon Dec 25 00:34:27 2017 +0100 +++ b/c++/rgb-assembler/commands/Goto.h Mon Dec 25 01:27:44 2017 +0100 @@ -29,7 +29,7 @@ void process(Memory &memory) override { address_t index = memory.read(); - memory.setIndex(index); + memory.setAddress(index); wprintf(L"GOTO %*d\n", 5, index); } }; diff -r 78c4d6b53499 -r ff150572e8c0 c++/rgb-assembler/commands/GotoCompare.h --- a/c++/rgb-assembler/commands/GotoCompare.h Mon Dec 25 00:34:27 2017 +0100 +++ b/c++/rgb-assembler/commands/GotoCompare.h Mon Dec 25 01:27:44 2017 +0100 @@ -34,13 +34,8 @@ address_t gt = memory.read(); address_t lt = memory.read(); - 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); + octet_t a = memory.read(aa); + octet_t b = memory.read(ab); address_t index; @@ -48,7 +43,7 @@ else if (a > b) index = gt; else index = lt; - memory.setIndex(index); + memory.setAddress(index); wprintf(L"GOTO COMPARE a = %02X, b = %02X, eq = %d, gt = %d, lt = %d → %d\n", a, b, eq, gt, lt, index); } diff -r 78c4d6b53499 -r ff150572e8c0 c++/rgb-assembler/commands/IncrementDecrement.h --- a/c++/rgb-assembler/commands/IncrementDecrement.h Mon Dec 25 00:34:27 2017 +0100 +++ b/c++/rgb-assembler/commands/IncrementDecrement.h Mon Dec 25 01:27:44 2017 +0100 @@ -39,14 +39,9 @@ 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(); + octet_t value = memory.read(address); value = increment ? value + change : value - change; - memory.setIndex(address); - memory.write(value); - memory.setIndex(originalAddress); + memory.write(address, value); wprintf(L"%sCREMENT %*d → %02X\n", (increment ? "IN" : "DE"), 5, address, value); } private: diff -r 78c4d6b53499 -r ff150572e8c0 c++/rgb-assembler/rgb-assembler.cpp --- a/c++/rgb-assembler/rgb-assembler.cpp Mon Dec 25 00:34:27 2017 +0100 +++ b/c++/rgb-assembler/rgb-assembler.cpp Mon Dec 25 01:27:44 2017 +0100 @@ -52,7 +52,7 @@ memory.write(CMD_SLEEP); memory.write(255); memory.write(CMD_GOTO); - memory.write(memory.getIndex() + sizeof (address_t) + 2 * sizeof (command_t)); + memory.write(memory.getAddress() + sizeof (address_t) + 2 * sizeof (command_t)); memory.write(CMD_INVALID); memory.write(CMD_INVALID); memory.write(CMD_SLEEP); @@ -69,11 +69,11 @@ 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(memory.getAddress() - 3 * sizeof (address_t) - 2 * sizeof (command_t)); memory.write(0); - memory.write(memory.getIndex() + sizeof (address_t)); + memory.write(memory.getAddress() + sizeof (address_t)); memory.write(CMD_END); - memory.start(); + memory.setAddressToBeginning(); } // Supported commands @@ -90,8 +90,8 @@ // Main loop / interpreter: - while (memory.isInside()) { - wprintf(L"command %*d = ", 4, memory.getIndex()); + while (memory.isNotOver()) { + wprintf(L"command %*d = ", 4, memory.getAddress()); command_t commandCode = memory.read(); wprintf(L"%02X ", commandCode);