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@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@0: franta-hg@0: const address_t MEMORY_SIZE = 1024; franta-hg@0: franta-hg@0: const command_t CMD_GOTO = 100; franta-hg@0: const command_t CMD_SLEEP = 101; franta-hg@0: const command_t CMD_END = 102; 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@3: index += sizeof (*value); 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@3: index += sizeof (*value); franta-hg@3: return *value; franta-hg@0: } franta-hg@0: franta-hg@1: void writeMemoryChar(command_t(&memory)[MEMORY_SIZE], address_t &index, const int value) { franta-hg@0: memory[index] = 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@2: index += sizeof (value); franta-hg@0: } franta-hg@0: 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@2: writeCommand(memory, a, CMD_SLEEP); franta-hg@2: writeCommand(memory, a, CMD_SLEEP); franta-hg@3: writeCommand(memory, a, CMD_GOTO); franta-hg@3: writeCommand(memory, a, 8); franta-hg@3: writeCommand(memory, a, 0); franta-hg@3: writeCommand(memory, a, 1); franta-hg@3: writeCommand(memory, a, 1); franta-hg@3: writeCommand(memory, a, CMD_SLEEP); 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@3: wprintf(L"%d\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@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: