12 typedef uint16_t address_t;
13 typedef uint8_t command_t;
14 typedef uint8_t sleep_t;
16 const address_t MEMORY_SIZE = 1024;
18 const command_t CMD_GOTO = 0x70;
19 const command_t CMD_SLEEP = 0xFF;
20 const command_t CMD_END = 0xED;
23 * Reads data on given position in memory and increments the index (position).
25 template<typename T> T read(command_t * memory, address_t &index) {
26 // TODO: for addresses: map higher memory to static hardcoded areas or peripherals
27 // TODO: sizeof (command_t) != 1 ?
28 T * value = reinterpret_cast<T*> (memory + index);
29 index += sizeof (*value) / sizeof (command_t);
33 void writeCommand(command_t * memory, address_t &index, const command_t value) {
34 // command_t * m = (command_t*) (memory + index);
35 command_t * m = reinterpret_cast<command_t*> (memory + index);
37 index += sizeof (value) / sizeof (command_t);
40 void writeAddress(command_t * memory, address_t &index, const address_t value) {
41 // command_t * m = (command_t*) (memory + index);
42 address_t * m = reinterpret_cast<address_t*> (memory + index);
44 index += sizeof (value) / sizeof (command_t);
47 void writeSleep(command_t * memory, address_t &index, const sleep_t value) {
48 // command_t * m = (command_t*) (memory + index);
49 sleep_t * m = reinterpret_cast<sleep_t*> (memory + index);
51 index += sizeof (value) / sizeof (command_t);
54 int main(int argc, char* argv[]) {
56 setlocale(LC_ALL, "");
58 command_t * memory = (command_t*) malloc(MEMORY_SIZE);
62 writeCommand(memory, a, CMD_SLEEP);
63 writeSleep(memory, a, 255);
64 writeCommand(memory, a, CMD_SLEEP);
65 writeSleep(memory, a, 10);
66 writeCommand(memory, a, CMD_SLEEP);
67 writeSleep(memory, a, 255);
68 writeCommand(memory, a, CMD_GOTO);
69 writeAddress(memory, a, a + 4);
70 writeCommand(memory, a, 1);
71 writeCommand(memory, a, 1);
72 writeCommand(memory, a, CMD_SLEEP);
73 writeSleep(memory, a, 255);
74 writeCommand(memory, a, CMD_END);
79 for (address_t i = 0; i < MEMORY_SIZE;) {
80 wprintf(L"command %d = ", i);
81 command_t ch = read<command_t>(memory, i);
88 i = read<address_t>(memory, i);
89 command.append("GOTO ");
92 command.append("SLEEP");
93 this_thread::sleep_for(chrono::milliseconds(read<sleep_t>(memory, i)));
96 command.append("END");
102 if (!command.empty()) {
103 wprintf(L"\t%s\n", command.c_str());
111 wprintf(L"all done\n");