franta-hg@0: #include franta-hg@0: #include franta-hg@0: #include franta-hg@0: #include franta-hg@0: #include franta-hg@0: franta-hg@6: #include franta-hg@6: #include franta-hg@6: franta-hg@0: using namespace std; franta-hg@0: franta-hg@0: typedef uint16_t address_t; franta-hg@0: typedef uint8_t command_t; franta-hg@6: typedef uint8_t sleep_t; franta-hg@10: typedef uint8_t color_t; franta-hg@0: franta-hg@12: // TODO: strong typedefs http://www.boost.org/doc/libs/1_61_0/libs/serialization/doc/strong_typedef.html ? franta-hg@12: franta-hg@0: const address_t MEMORY_SIZE = 1024; franta-hg@0: franta-hg@5: const command_t CMD_GOTO = 0x70; franta-hg@5: const command_t CMD_SLEEP = 0xFF; franta-hg@10: const command_t CMD_COLOR = 0xAA; franta-hg@5: const command_t CMD_END = 0xED; franta-hg@11: const command_t CMD_INVALID = 0x1; // placeholder for unsupported command franta-hg@0: franta-hg@1: /** franta-hg@8: * Reads data on given position in memory and increments the index (position). franta-hg@1: */ franta-hg@8: template T read(command_t * memory, address_t &index) { franta-hg@8: // TODO: for addresses: map higher memory to static hardcoded areas or peripherals franta-hg@8: // TODO: sizeof (command_t) != 1 ? franta-hg@8: T * value = reinterpret_cast (memory + index); franta-hg@6: index += sizeof (*value) / sizeof (command_t); franta-hg@6: return *value; franta-hg@0: } franta-hg@0: franta-hg@9: /** franta-hg@9: * Writes data to given position in memory and increments the index (position). franta-hg@9: */ franta-hg@9: template void write(command_t * memory, address_t &index, const T value) { franta-hg@9: // TODO: sizeof (command_t) != 1 ? franta-hg@9: // T * m = (T*) (memory + index); franta-hg@9: T * m = reinterpret_cast (memory + index); franta-hg@6: *m = value; franta-hg@6: index += sizeof (value) / sizeof (command_t); franta-hg@6: } franta-hg@6: franta-hg@0: int main(int argc, char* argv[]) { franta-hg@0: franta-hg@0: setlocale(LC_ALL, ""); franta-hg@0: franta-hg@2: command_t * memory = (command_t*) malloc(MEMORY_SIZE); franta-hg@0: franta-hg@2: { franta-hg@2: address_t a = 0; franta-hg@9: write(memory, a, CMD_SLEEP); franta-hg@9: write(memory, a, 255); franta-hg@9: write(memory, a, CMD_SLEEP); franta-hg@9: write(memory, a, 10); franta-hg@9: write(memory, a, CMD_SLEEP); franta-hg@9: write(memory, a, 255); franta-hg@9: write(memory, a, CMD_GOTO); franta-hg@9: write(memory, a, a + 4); franta-hg@11: write(memory, a, CMD_INVALID); franta-hg@11: write(memory, a, CMD_INVALID); franta-hg@9: write(memory, a, CMD_SLEEP); franta-hg@9: write(memory, a, 255); franta-hg@10: write(memory, a, CMD_COLOR); franta-hg@11: write(memory, a, 0); franta-hg@11: write(memory, a, 200); franta-hg@11: write(memory, a, 255); franta-hg@9: write(memory, a, CMD_END); franta-hg@2: } franta-hg@0: franta-hg@2: for (address_t i = 0; i < MEMORY_SIZE;) { franta-hg@11: wprintf(L"command %*d = ", 4, i); franta-hg@8: command_t ch = read(memory, i); franta-hg@11: wprintf(L"%02X ", ch); franta-hg@0: franta-hg@0: switch (ch) { franta-hg@0: case CMD_GOTO: franta-hg@12: { franta-hg@8: i = read(memory, i); franta-hg@11: wprintf(L"GOTO %*d\n", 5, i); franta-hg@0: break; franta-hg@12: } franta-hg@0: case CMD_SLEEP: franta-hg@12: { franta-hg@12: sleep_t delay = read(memory, i); franta-hg@11: wprintf(L"SLEEP %*d ms\n", 4, delay); franta-hg@11: this_thread::sleep_for(chrono::milliseconds(delay)); franta-hg@0: break; franta-hg@12: } franta-hg@10: case CMD_COLOR: franta-hg@12: { franta-hg@12: color_t r = read(memory, i); franta-hg@12: color_t g = read(memory, i); franta-hg@12: color_t b = read(memory, i); franta-hg@11: wprintf(L"COLOR %02X %02X %02X\n", r, g, b); franta-hg@10: break; franta-hg@12: } franta-hg@0: case CMD_END: franta-hg@12: { franta-hg@11: wprintf(L"END\n"); franta-hg@2: i = MEMORY_SIZE; franta-hg@0: break; franta-hg@12: } franta-hg@11: default: franta-hg@12: { franta-hg@11: wprintf(L"invalid command\n"); franta-hg@12: } franta-hg@0: } franta-hg@0: franta-hg@0: } franta-hg@0: franta-hg@7: free(memory); franta-hg@7: memory = nullptr; franta-hg@0: wprintf(L"all done\n"); franta-hg@0: return 0; franta-hg@0: } franta-hg@0: