12 typedef uint16_t address_t;
13 typedef uint8_t octet_t;
14 typedef uint8_t command_t;
15 typedef uint8_t sleep_t;
16 typedef uint8_t color_t;
17 typedef uint8_t led_t;
19 // TODO: strong typedefs http://www.boost.org/doc/libs/1_61_0/libs/serialization/doc/strong_typedef.html ?
21 const address_t MEMORY_SIZE = 1024;
24 * Skip to the given address.
25 * parameter: address_t
27 const command_t CMD_GOTO = 0x70;
30 * Compare values on two addresses and go to one of given three addresses.
31 * parameter: address_t a
32 * parameter: address_t b
33 * parameter: address_t GOTO target when a == b
34 * parameter: address_t GOTO target when a > b
35 * parameter: address_t GOTO target when a < b
37 const command_t CMD_GOTO_COMPARE = 0x80;
40 * Wait given time in ms.
43 const command_t CMD_SLEEP = 0xFF;
47 * parameter: led_t LED number
48 * parameter: color_t red
49 * parameter: color_t green
50 * parameter: color_t blue
52 const command_t CMD_COLOR = 0xAA;
57 const command_t CMD_END = 0xED;
60 * Increase value at given address
61 * parameter: address_t
63 const command_t CMD_INCREMENT = 0x11;
66 * Decrease value at given address
67 * parameter: address_t
69 const command_t CMD_DECREMENT = 0x12;
72 * Placeholder for unsupported command.
75 const command_t CMD_INVALID = 0x1;
77 // TODO: more commands, better numbers
80 * Reads data on given position in memory and increments the index (position).
82 * @param memory array of bytes / octets
83 * @param index offset in same units as memory type
84 * @return value found at given position
86 template<typename T> T read(octet_t * memory, address_t &index) {
87 // TODO: for addresses: map higher memory to static hardcoded areas or peripherals
88 T * value = reinterpret_cast<T*> (memory + index);
94 * Writes data to given position in memory and increments the index (position).
95 * @param memory array of bytes / octets
96 * @param index offset in same units as memory type
97 * @param value value to be written at given position
99 template<typename T> void write(octet_t * memory, address_t &index, const T value) {
100 T * m = reinterpret_cast<T*> (memory + index);
102 index += sizeof (value);
105 int main(int argc, char* argv[]) {
107 setlocale(LC_ALL, "");
109 octet_t * memory = (octet_t*) malloc(MEMORY_SIZE);
111 // Sample program / data:
112 // TODO: load bytes from file, stdin, serial port, network…
115 write<command_t>(memory, a, CMD_SLEEP);
116 write<sleep_t>(memory, a, 255);
117 write<command_t>(memory, a, CMD_SLEEP);
118 write<sleep_t>(memory, a, 10);
119 write<command_t>(memory, a, CMD_SLEEP);
120 write<sleep_t>(memory, a, 255);
121 write<command_t>(memory, a, CMD_GOTO);
122 write<address_t>(memory, a, a + sizeof (address_t) + 2 * sizeof (command_t));
123 write<command_t>(memory, a, CMD_INVALID);
124 write<command_t>(memory, a, CMD_INVALID);
125 write<command_t>(memory, a, CMD_SLEEP);
126 write<sleep_t>(memory, a, 255);
127 write<command_t>(memory, a, CMD_COLOR);
128 write<led_t>(memory, a, 23);
129 write<color_t>(memory, a, 0);
130 write<color_t>(memory, a, 200);
131 write<color_t>(memory, a, 255);
132 write<command_t>(memory, a, CMD_INCREMENT);
133 write<address_t>(memory, a, 0);
134 write<command_t>(memory, a, CMD_DECREMENT);
135 write<address_t>(memory, a, 0);
136 write<command_t>(memory, a, CMD_GOTO_COMPARE);
137 write<address_t>(memory, a, 0);
138 write<address_t>(memory, a, 0 + sizeof (command_t) + sizeof (sleep_t));
139 write<address_t>(memory, a, a - 3 * sizeof (address_t) - 2 * sizeof (command_t));
140 write<address_t>(memory, a, 0);
141 write<address_t>(memory, a, a + sizeof (address_t));
142 write<command_t>(memory, a, CMD_END);
145 for (address_t i = 0; i < MEMORY_SIZE;) {
146 wprintf(L"command %*d = ", 4, i);
147 command_t command = read<command_t>(memory, i);
148 wprintf(L"%02X ", command);
153 i = read<address_t>(memory, i);
154 wprintf(L"GOTO %*d\n", 5, i);
159 sleep_t delay = read<sleep_t>(memory, i);
160 wprintf(L"SLEEP %*d ms\n", 4, delay);
161 this_thread::sleep_for(chrono::milliseconds(delay));
166 led_t led = read<led_t>(memory, i);
167 color_t r = read<color_t>(memory, i);
168 color_t g = read<color_t>(memory, i);
169 color_t b = read<color_t>(memory, i);
170 wprintf(L"COLOR %02X %02X %02X → %d\n", r, g, b, led);
176 address_t address = read<address_t>(memory, i);
177 address_t address_r = address;
178 address_t address_w = address_r;
179 octet_t value = read<octet_t>(memory, address_r);
180 value = command == CMD_INCREMENT ? value + 1 : value - 1;
181 write<octet_t>(memory, address_w, value);
182 wprintf(L"%sCREMENT %*d → %02X\n", (command == CMD_INCREMENT ? "IN" : "DE"), 5, address, value);
185 case CMD_GOTO_COMPARE:
187 address_t aa = read<address_t>(memory, i);
188 address_t ab = read<address_t>(memory, i);
189 address_t eq = read<address_t>(memory, i);
190 address_t gt = read<address_t>(memory, i);
191 address_t lt = read<address_t>(memory, i);
193 octet_t a = read<octet_t>(memory, aa);
194 octet_t b = read<octet_t>(memory, ab);
197 else if (a > b) i = gt;
200 wprintf(L"GOTO COMPARE a = %02X, b = %02X, eq = %d, gt = %d, lt = %d → %d\n", a, b, eq, gt, lt, i);
211 wprintf(L"invalid command\n");
219 wprintf(L"all done\n");