# HG changeset patch # User František Kučera # Date 1514070535 -3600 # Node ID 47ee9157913301367c9035f4435454c9e61c84e6 # Parent 5ce09de7f9b74ef9bc292e873acc38a364ab2da0 INCREMENT and DECREMENT in class diff -r 5ce09de7f9b7 -r 47ee91579133 c++/rgb-assembler/commands/IncrementDecrement.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/c++/rgb-assembler/commands/IncrementDecrement.h Sun Dec 24 00:08:55 2017 +0100 @@ -0,0 +1,35 @@ +#pragma once + +#include +#include +#include + +#include "../Command.h" +#include "../memory.h" + +using namespace std; + +namespace commands { + +class IncrementDecrement : public Command { +public: + + IncrementDecrement(const bool increment) { + this->increment = increment; + } + + 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); + value = increment ? value + 1 : value - 1; + write(memory, address_w, value); + wprintf(L"%sCREMENT %*d → %02X\n", (increment ? "IN" : "DE"), 5, address, value); + } +private: + bool increment; + +}; + +} diff -r 5ce09de7f9b7 -r 47ee91579133 c++/rgb-assembler/nbproject/configurations.xml --- a/c++/rgb-assembler/nbproject/configurations.xml Sat Dec 23 23:50:44 2017 +0100 +++ b/c++/rgb-assembler/nbproject/configurations.xml Sun Dec 24 00:08:55 2017 +0100 @@ -9,6 +9,7 @@ commands/End.h commands/Goto.h commands/GotoCompare.h + commands/IncrementDecrement.h commands/Sleep.h memory.h types.h @@ -58,6 +59,8 @@ + + @@ -98,6 +101,8 @@ + + diff -r 5ce09de7f9b7 -r 47ee91579133 c++/rgb-assembler/rgb-assembler.cpp --- a/c++/rgb-assembler/rgb-assembler.cpp Sat Dec 23 23:50:44 2017 +0100 +++ b/c++/rgb-assembler/rgb-assembler.cpp Sun Dec 24 00:08:55 2017 +0100 @@ -13,6 +13,7 @@ #include "commands/Sleep.h" #include "commands/End.h" #include "commands/Color.h" +#include "commands/IncrementDecrement.h" using namespace std; @@ -120,41 +121,23 @@ {CMD_SLEEP, make_shared()}, {CMD_END, make_shared()}, {CMD_COLOR, make_shared()}, + {CMD_INCREMENT, make_shared < commands::IncrementDecrement>(true)}, + {CMD_DECREMENT, make_shared < commands::IncrementDecrement>(false)}, }; + for (address_t i = 0; i < MEMORY_SIZE;) { wprintf(L"command %*d = ", 4, i); - command_t command = read(memory, i); - wprintf(L"%02X ", command); + command_t commandCode = read(memory, i); + wprintf(L"%02X ", commandCode); - shared_ptr cx = commands[command]; + shared_ptr command = commands[commandCode]; - if (cx) { - cx->process(memory, i); - continue; + if (command) { + command->process(memory, i); } else { - // TODO: wprintf(L"invalid command\n"); + wprintf(L"invalid command\n"); } - - switch (command) { - case CMD_INCREMENT: - case CMD_DECREMENT: - { - address_t address = read(memory, i); - address_t address_r = address; - address_t address_w = address_r; - octet_t value = read(memory, address_r); - value = command == CMD_INCREMENT ? value + 1 : value - 1; - write(memory, address_w, value); - wprintf(L"%sCREMENT %*d → %02X\n", (command == CMD_INCREMENT ? "IN" : "DE"), 5, address, value); - break; - } - default: - { - wprintf(L"invalid command\n"); - } - } - } free(memory); @@ -162,4 +145,3 @@ wprintf(L"all done\n"); return 0; } -