# HG changeset patch # User František Kučera # Date 1513959315 -3600 # Node ID 2ab3d728224939e046005869b5616dfa3fdd8481 # Parent 2a4b8b3abe14415f87f3fe161007c485a3cf625f INCREMENT, DECREMENT diff -r 2a4b8b3abe14 -r 2ab3d7282249 c++/rgb-assembler/rgb-assembler.cpp --- a/c++/rgb-assembler/rgb-assembler.cpp Fri Dec 22 16:21:10 2017 +0100 +++ b/c++/rgb-assembler/rgb-assembler.cpp Fri Dec 22 17:15:15 2017 +0100 @@ -10,6 +10,7 @@ using namespace std; typedef uint16_t address_t; +typedef uint8_t byte_t; typedef uint8_t command_t; typedef uint8_t sleep_t; typedef uint8_t color_t; @@ -46,6 +47,18 @@ const command_t CMD_END = 0xED; /** + * Increase value at given address + * parameter: address_t + */ +const command_t CMD_INCREMENT = 0x11; + +/** + * Decrease value at given address + * parameter: address_t + */ +const command_t CMD_DECREMENT = 0x12; + +/** * Placeholder for unsupported command. * Just for testing. */ @@ -54,30 +67,30 @@ /** * Reads data on given position in memory and increments the index (position). */ -template T read(command_t * memory, address_t &index) { +template T read(byte_t * memory, address_t &index) { // TODO: for addresses: map higher memory to static hardcoded areas or peripherals - // TODO: sizeof (command_t) != 1 ? + // TODO: sizeof (byte_t) != 1 ? T * value = reinterpret_cast (memory + index); - index += sizeof (*value) / sizeof (command_t); + index += sizeof (*value) / sizeof (byte_t); return *value; } /** * Writes data to given position in memory and increments the index (position). */ -template void write(command_t * memory, address_t &index, const T value) { - // TODO: sizeof (command_t) != 1 ? +template void write(byte_t * memory, address_t &index, const T value) { + // TODO: sizeof (byte_t) != 1 ? // T * m = (T*) (memory + index); T * m = reinterpret_cast (memory + index); *m = value; - index += sizeof (value) / sizeof (command_t); + index += sizeof (value) / sizeof (byte_t); } int main(int argc, char* argv[]) { setlocale(LC_ALL, ""); - command_t * memory = (command_t*) malloc(MEMORY_SIZE); + byte_t * memory = (byte_t*) malloc(MEMORY_SIZE); { address_t a = 0; @@ -98,6 +111,10 @@ 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_END); } @@ -129,6 +146,18 @@ wprintf(L"COLOR %02X %02X %02X → %d\n", r, g, b, led); break; } + case CMD_INCREMENT: + case CMD_DECREMENT: + { + address_t address = read(memory, i); + address_t address_r = address; + address_t address_w = address_r; + byte_t value = read(memory, address_r); + value = ch == CMD_INCREMENT ? value + 1 : value - 1; + write(memory, address_w, value); + wprintf(L"%sCREMENT %*d → %02X\n", (ch == CMD_INCREMENT ? "IN" : "DE"), 5, address, value); + break; + } case CMD_END: { wprintf(L"END\n");