6 #include <unordered_map>
11 #include "commands/Goto.h"
12 #include "commands/Sleep.h"
16 // TODO: strong typedefs http://www.boost.org/doc/libs/1_61_0/libs/serialization/doc/strong_typedef.html ?
19 * Skip to the given address.
20 * parameter: address_t
22 const command_t CMD_GOTO = 0x70;
25 * Compare values on two addresses and go to one of given three addresses.
26 * parameter: address_t a
27 * parameter: address_t b
28 * parameter: address_t GOTO target when a == b
29 * parameter: address_t GOTO target when a > b
30 * parameter: address_t GOTO target when a < b
32 const command_t CMD_GOTO_COMPARE = 0x80;
35 * Wait given time in ms.
38 const command_t CMD_SLEEP = 0xFF;
42 * parameter: led_t LED number
43 * parameter: color_t red
44 * parameter: color_t green
45 * parameter: color_t blue
47 const command_t CMD_COLOR = 0xAA;
52 const command_t CMD_END = 0xED;
55 * Increase value at given address
56 * parameter: address_t
58 const command_t CMD_INCREMENT = 0x11;
61 * Decrease value at given address
62 * parameter: address_t
64 const command_t CMD_DECREMENT = 0x12;
67 * Placeholder for unsupported command.
70 const command_t CMD_INVALID = 0x1;
72 // TODO: more commands, better numbers
74 int main(int argc, char* argv[]) {
76 setlocale(LC_ALL, "");
78 octet_t * memory = (octet_t*) malloc(MEMORY_SIZE);
80 // Sample program / data:
81 // TODO: load bytes from file, stdin, serial port, network…
84 write<command_t>(memory, a, CMD_SLEEP);
85 write<sleep_t>(memory, a, 255);
86 write<command_t>(memory, a, CMD_SLEEP);
87 write<sleep_t>(memory, a, 10);
88 write<command_t>(memory, a, CMD_SLEEP);
89 write<sleep_t>(memory, a, 255);
90 write<command_t>(memory, a, CMD_GOTO);
91 write<address_t>(memory, a, a + sizeof (address_t) + 2 * sizeof (command_t));
92 write<command_t>(memory, a, CMD_INVALID);
93 write<command_t>(memory, a, CMD_INVALID);
94 write<command_t>(memory, a, CMD_SLEEP);
95 write<sleep_t>(memory, a, 255);
96 write<command_t>(memory, a, CMD_COLOR);
97 write<led_t>(memory, a, 23);
98 write<color_t>(memory, a, 0);
99 write<color_t>(memory, a, 200);
100 write<color_t>(memory, a, 255);
101 write<command_t>(memory, a, CMD_INCREMENT);
102 write<address_t>(memory, a, 0);
103 write<command_t>(memory, a, CMD_DECREMENT);
104 write<address_t>(memory, a, 0);
105 write<command_t>(memory, a, CMD_GOTO_COMPARE);
106 write<address_t>(memory, a, 0);
107 write<address_t>(memory, a, 0 + sizeof (command_t) + sizeof (sleep_t));
108 write<address_t>(memory, a, a - 3 * sizeof (address_t) - 2 * sizeof (command_t));
109 write<address_t>(memory, a, 0);
110 write<address_t>(memory, a, a + sizeof (address_t));
111 write<command_t>(memory, a, CMD_END);
114 unordered_map<command_t, shared_ptr < Command>> commands = {
115 {CMD_GOTO, make_shared<commands::Goto>()},
116 {CMD_SLEEP, make_shared<commands::Sleep>()},
119 for (address_t i = 0; i < MEMORY_SIZE;) {
120 wprintf(L"command %*d = ", 4, i);
121 command_t command = read<command_t>(memory, i);
122 wprintf(L"%02X ", command);
124 shared_ptr<Command> cx = commands[command];
127 cx->process(memory, i);
130 // TODO: wprintf(L"invalid command\n");
136 led_t led = read<led_t>(memory, i);
137 color_t r = read<color_t>(memory, i);
138 color_t g = read<color_t>(memory, i);
139 color_t b = read<color_t>(memory, i);
140 wprintf(L"COLOR %02X %02X %02X → %d\n", r, g, b, led);
146 address_t address = read<address_t>(memory, i);
147 address_t address_r = address;
148 address_t address_w = address_r;
149 octet_t value = read<octet_t>(memory, address_r);
150 value = command == CMD_INCREMENT ? value + 1 : value - 1;
151 write<octet_t>(memory, address_w, value);
152 wprintf(L"%sCREMENT %*d → %02X\n", (command == CMD_INCREMENT ? "IN" : "DE"), 5, address, value);
155 case CMD_GOTO_COMPARE:
157 address_t aa = read<address_t>(memory, i);
158 address_t ab = read<address_t>(memory, i);
159 address_t eq = read<address_t>(memory, i);
160 address_t gt = read<address_t>(memory, i);
161 address_t lt = read<address_t>(memory, i);
163 octet_t a = read<octet_t>(memory, aa);
164 octet_t b = read<octet_t>(memory, ab);
167 else if (a > b) i = gt;
170 wprintf(L"GOTO COMPARE a = %02X, b = %02X, eq = %d, gt = %d, lt = %d → %d\n", a, b, eq, gt, lt, i);
181 wprintf(L"invalid command\n");
189 wprintf(L"all done\n");