6 #include <unordered_map>
11 #include "commands/Goto.h"
12 #include "commands/GotoCompare.h"
13 #include "commands/Sleep.h"
14 #include "commands/End.h"
15 #include "commands/Color.h"
16 #include "commands/IncrementDecrement.h"
20 // TODO: strong typedefs http://www.boost.org/doc/libs/1_61_0/libs/serialization/doc/strong_typedef.html ?
23 * Skip to the given address.
24 * parameter: address_t
26 const command_t CMD_GOTO = 0x70;
29 * Compare values on two addresses and go to one of given three addresses.
30 * parameter: address_t a
31 * parameter: address_t b
32 * parameter: address_t GOTO target when a == b
33 * parameter: address_t GOTO target when a > b
34 * parameter: address_t GOTO target when a < b
36 const command_t CMD_GOTO_COMPARE = 0x80;
39 * Wait given time in ms.
42 const command_t CMD_SLEEP = 0xFF;
46 * parameter: led_t LED number
47 * parameter: color_t red
48 * parameter: color_t green
49 * parameter: color_t blue
51 const command_t CMD_COLOR = 0xAA;
56 const command_t CMD_END = 0xED;
59 * Increase value at given address
60 * parameter: address_t
62 const command_t CMD_INCREMENT = 0x11;
65 * Decrease value at given address
66 * parameter: address_t
68 const command_t CMD_DECREMENT = 0x12;
71 * Placeholder for unsupported command.
74 const command_t CMD_INVALID = 0x1;
76 // TODO: more commands, better numbers
78 int main(int argc, char* argv[]) {
80 setlocale(LC_ALL, "");
82 octet_t * memory = (octet_t*) malloc(MEMORY_SIZE);
84 // Sample program / data:
85 // TODO: load bytes from file, stdin, serial port, network…
88 write<command_t>(memory, a, CMD_SLEEP);
89 write<sleep_t>(memory, a, 255);
90 write<command_t>(memory, a, CMD_SLEEP);
91 write<sleep_t>(memory, a, 10);
92 write<command_t>(memory, a, CMD_SLEEP);
93 write<sleep_t>(memory, a, 255);
94 write<command_t>(memory, a, CMD_GOTO);
95 write<address_t>(memory, a, a + sizeof (address_t) + 2 * sizeof (command_t));
96 write<command_t>(memory, a, CMD_INVALID);
97 write<command_t>(memory, a, CMD_INVALID);
98 write<command_t>(memory, a, CMD_SLEEP);
99 write<sleep_t>(memory, a, 255);
100 write<command_t>(memory, a, CMD_COLOR);
101 write<led_t>(memory, a, 23);
102 write<color_t>(memory, a, 0);
103 write<color_t>(memory, a, 200);
104 write<color_t>(memory, a, 255);
105 write<command_t>(memory, a, CMD_INCREMENT);
106 write<address_t>(memory, a, 0);
107 write<command_t>(memory, a, CMD_DECREMENT);
108 write<address_t>(memory, a, 0);
109 write<command_t>(memory, a, CMD_GOTO_COMPARE);
110 write<address_t>(memory, a, 0);
111 write<address_t>(memory, a, 0 + sizeof (command_t) + sizeof (sleep_t));
112 write<address_t>(memory, a, a - 3 * sizeof (address_t) - 2 * sizeof (command_t));
113 write<address_t>(memory, a, 0);
114 write<address_t>(memory, a, a + sizeof (address_t));
115 write<command_t>(memory, a, CMD_END);
118 unordered_map<command_t, shared_ptr < Command>> commands = {
119 {CMD_GOTO, make_shared<commands::Goto>()},
120 {CMD_GOTO_COMPARE, make_shared<commands::GotoCompare>()},
121 {CMD_SLEEP, make_shared<commands::Sleep>()},
122 {CMD_END, make_shared<commands::End>()},
123 {CMD_COLOR, make_shared<commands::Color>()},
124 {CMD_INCREMENT, make_shared < commands::IncrementDecrement>(true)},
125 {CMD_DECREMENT, make_shared < commands::IncrementDecrement>(false)},
129 for (address_t i = 0; i < MEMORY_SIZE;) {
130 wprintf(L"command %*d = ", 4, i);
131 command_t commandCode = read<command_t>(memory, i);
132 wprintf(L"%02X ", commandCode);
134 shared_ptr<Command> command = commands[commandCode];
137 command->process(memory, i);
139 wprintf(L"invalid command\n");
145 wprintf(L"all done\n");