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@0: 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@5: const command_t CMD_END = 0xED; franta-hg@0: franta-hg@1: /** franta-hg@1: * Reads the command on given position in memory and increments the index (position). franta-hg@1: */ franta-hg@2: command_t readCommand(command_t * memory, address_t &index) { franta-hg@0: // TODO: map higher memory to static hardcoded areas or peripherals franta-hg@3: command_t * value = reinterpret_cast (memory + index); franta-hg@4: index += sizeof (*value) / sizeof (command_t); franta-hg@3: return *value; franta-hg@2: } franta-hg@2: franta-hg@2: address_t readAddress(command_t * memory, address_t &index) { franta-hg@3: address_t * value = reinterpret_cast (memory + index); franta-hg@4: index += sizeof (*value) / sizeof (command_t); franta-hg@3: return *value; franta-hg@0: } franta-hg@0: franta-hg@6: sleep_t readSleep(command_t * memory, address_t &index) { franta-hg@6: // TODO: map higher memory to static hardcoded areas or peripherals franta-hg@6: sleep_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@2: void writeCommand(command_t * memory, address_t &index, const command_t value) { franta-hg@2: // command_t * m = (command_t*) (memory + index); franta-hg@2: command_t * m = reinterpret_cast (memory + index); franta-hg@2: *m = value; franta-hg@4: index += sizeof (value) / sizeof (command_t); franta-hg@4: } franta-hg@4: franta-hg@4: void writeAddress(command_t * memory, address_t &index, const address_t value) { franta-hg@4: // command_t * m = (command_t*) (memory + index); franta-hg@4: address_t * m = reinterpret_cast (memory + index); franta-hg@4: *m = value; franta-hg@4: index += sizeof (value) / sizeof (command_t); franta-hg@0: } franta-hg@0: franta-hg@6: void writeSleep(command_t * memory, address_t &index, const sleep_t value) { franta-hg@6: // command_t * m = (command_t*) (memory + index); franta-hg@6: sleep_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@2: writeCommand(memory, a, CMD_SLEEP); franta-hg@6: writeSleep(memory, a, 255); franta-hg@2: writeCommand(memory, a, CMD_SLEEP); franta-hg@6: writeSleep(memory, a, 10); franta-hg@2: writeCommand(memory, a, CMD_SLEEP); franta-hg@6: writeSleep(memory, a, 255); franta-hg@3: writeCommand(memory, a, CMD_GOTO); franta-hg@6: writeAddress(memory, a, a+4); franta-hg@3: writeCommand(memory, a, 1); franta-hg@3: writeCommand(memory, a, 1); franta-hg@3: writeCommand(memory, a, CMD_SLEEP); franta-hg@6: writeSleep(memory, a, 255); franta-hg@2: writeCommand(memory, a, CMD_END); franta-hg@2: } franta-hg@0: franta-hg@0: franta-hg@2: franta-hg@2: for (address_t i = 0; i < MEMORY_SIZE;) { franta-hg@3: wprintf(L"command %d = ", i); franta-hg@0: command_t ch = readCommand(memory, i); franta-hg@5: wprintf(L"%X\n", ch); franta-hg@0: franta-hg@0: string command; franta-hg@0: franta-hg@0: switch (ch) { franta-hg@0: case CMD_GOTO: franta-hg@2: i = readAddress(memory, i); franta-hg@2: command.append("GOTO "); franta-hg@0: break; franta-hg@0: case CMD_SLEEP: franta-hg@0: command.append("SLEEP"); franta-hg@6: this_thread::sleep_for(chrono::milliseconds(readSleep(memory, i))); franta-hg@0: break; franta-hg@0: case CMD_END: franta-hg@0: command.append("END"); franta-hg@2: i = MEMORY_SIZE; franta-hg@0: break; franta-hg@0: franta-hg@0: } franta-hg@0: franta-hg@0: if (!command.empty()) { franta-hg@0: wprintf(L"\t%s\n", command.c_str()); franta-hg@0: } franta-hg@0: } franta-hg@0: franta-hg@0: franta-hg@0: franta-hg@0: wprintf(L"all done\n"); franta-hg@0: return 0; franta-hg@0: } franta-hg@0: