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"
19 // TODO: strong typedefs http://www.boost.org/doc/libs/1_61_0/libs/serialization/doc/strong_typedef.html ?
22 * Skip to the given address.
23 * parameter: address_t
25 const command_t CMD_GOTO = 0x70;
28 * Compare values on two addresses and go to one of given three addresses.
29 * parameter: address_t a
30 * parameter: address_t b
31 * parameter: address_t GOTO target when a == b
32 * parameter: address_t GOTO target when a > b
33 * parameter: address_t GOTO target when a < b
35 const command_t CMD_GOTO_COMPARE = 0x80;
38 * Wait given time in ms.
41 const command_t CMD_SLEEP = 0xFF;
45 * parameter: led_t LED number
46 * parameter: color_t red
47 * parameter: color_t green
48 * parameter: color_t blue
50 const command_t CMD_COLOR = 0xAA;
55 const command_t CMD_END = 0xED;
58 * Increase value at given address
59 * parameter: address_t
61 const command_t CMD_INCREMENT = 0x11;
64 * Decrease value at given address
65 * parameter: address_t
67 const command_t CMD_DECREMENT = 0x12;
70 * Placeholder for unsupported command.
73 const command_t CMD_INVALID = 0x1;
75 // TODO: more commands, better numbers
77 int main(int argc, char* argv[]) {
79 setlocale(LC_ALL, "");
81 octet_t * memory = (octet_t*) malloc(MEMORY_SIZE);
83 // Sample program / data:
84 // TODO: load bytes from file, stdin, serial port, network…
87 write<command_t>(memory, a, CMD_SLEEP);
88 write<sleep_t>(memory, a, 255);
89 write<command_t>(memory, a, CMD_SLEEP);
90 write<sleep_t>(memory, a, 10);
91 write<command_t>(memory, a, CMD_SLEEP);
92 write<sleep_t>(memory, a, 255);
93 write<command_t>(memory, a, CMD_GOTO);
94 write<address_t>(memory, a, a + sizeof (address_t) + 2 * sizeof (command_t));
95 write<command_t>(memory, a, CMD_INVALID);
96 write<command_t>(memory, a, CMD_INVALID);
97 write<command_t>(memory, a, CMD_SLEEP);
98 write<sleep_t>(memory, a, 255);
99 write<command_t>(memory, a, CMD_COLOR);
100 write<led_t>(memory, a, 23);
101 write<color_t>(memory, a, 0);
102 write<color_t>(memory, a, 200);
103 write<color_t>(memory, a, 255);
104 write<command_t>(memory, a, CMD_INCREMENT);
105 write<address_t>(memory, a, 0);
106 write<command_t>(memory, a, CMD_DECREMENT);
107 write<address_t>(memory, a, 0);
108 write<command_t>(memory, a, CMD_GOTO_COMPARE);
109 write<address_t>(memory, a, 0);
110 write<address_t>(memory, a, 0 + sizeof (command_t) + sizeof (sleep_t));
111 write<address_t>(memory, a, a - 3 * sizeof (address_t) - 2 * sizeof (command_t));
112 write<address_t>(memory, a, 0);
113 write<address_t>(memory, a, a + sizeof (address_t));
114 write<command_t>(memory, a, CMD_END);
117 unordered_map<command_t, shared_ptr < Command>> commands = {
118 {CMD_GOTO, make_shared<commands::Goto>()},
119 {CMD_GOTO_COMPARE, make_shared<commands::GotoCompare>()},
120 {CMD_SLEEP, make_shared<commands::Sleep>()},
121 {CMD_END, make_shared<commands::End>()},
122 {CMD_COLOR, make_shared<commands::Color>()},
125 for (address_t i = 0; i < MEMORY_SIZE;) {
126 wprintf(L"command %*d = ", 4, i);
127 command_t command = read<command_t>(memory, i);
128 wprintf(L"%02X ", command);
130 shared_ptr<Command> cx = commands[command];
133 cx->process(memory, i);
136 // TODO: wprintf(L"invalid command\n");
143 address_t address = read<address_t>(memory, i);
144 address_t address_r = address;
145 address_t address_w = address_r;
146 octet_t value = read<octet_t>(memory, address_r);
147 value = command == CMD_INCREMENT ? value + 1 : value - 1;
148 write<octet_t>(memory, address_w, value);
149 wprintf(L"%sCREMENT %*d → %02X\n", (command == CMD_INCREMENT ? "IN" : "DE"), 5, address, value);
154 wprintf(L"invalid command\n");
162 wprintf(L"all done\n");