# HG changeset patch # User František Kučera # Date 1513889355 -3600 # Node ID d4b2ec9ef8bcc32d9f4c47023aae1887fa95b895 # Parent 379c490a9831cde2ff53e382dc25b09abbc4d89f template / generic read() function diff -r 379c490a9831 -r d4b2ec9ef8bc c++/rgb-assembler/rgb-assembler.cpp --- a/c++/rgb-assembler/rgb-assembler.cpp Thu Dec 21 18:13:35 2017 +0100 +++ b/c++/rgb-assembler/rgb-assembler.cpp Thu Dec 21 21:49:15 2017 +0100 @@ -20,24 +20,12 @@ const command_t CMD_END = 0xED; /** - * Reads the command on given position in memory and increments the index (position). + * Reads data on given position in memory and increments the index (position). */ -command_t readCommand(command_t * memory, address_t &index) { - // TODO: map higher memory to static hardcoded areas or peripherals - command_t * value = reinterpret_cast (memory + index); - index += sizeof (*value) / sizeof (command_t); - return *value; -} - -address_t readAddress(command_t * memory, address_t &index) { - address_t * value = reinterpret_cast (memory + index); - index += sizeof (*value) / sizeof (command_t); - return *value; -} - -sleep_t readSleep(command_t * memory, address_t &index) { - // TODO: map higher memory to static hardcoded areas or peripherals - sleep_t * value = reinterpret_cast (memory + index); +template T read(command_t * memory, address_t &index) { + // TODO: for addresses: map higher memory to static hardcoded areas or peripherals + // TODO: sizeof (command_t) != 1 ? + T * value = reinterpret_cast (memory + index); index += sizeof (*value) / sizeof (command_t); return *value; } @@ -90,19 +78,19 @@ for (address_t i = 0; i < MEMORY_SIZE;) { wprintf(L"command %d = ", i); - command_t ch = readCommand(memory, i); + command_t ch = read(memory, i); wprintf(L"%X\n", ch); string command; switch (ch) { case CMD_GOTO: - i = readAddress(memory, i); + i = read(memory, i); command.append("GOTO "); break; case CMD_SLEEP: command.append("SLEEP"); - this_thread::sleep_for(chrono::milliseconds(readSleep(memory, i))); + this_thread::sleep_for(chrono::milliseconds(read(memory, i))); break; case CMD_END: command.append("END");