12 typedef uint16_t address_t;
13 typedef uint8_t byte_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 * Wait given time in ms.
33 const command_t CMD_SLEEP = 0xFF;
37 * parameter: led_t LED number
38 * parameter: color_t red
39 * parameter: color_t green
40 * parameter: color_t blue
42 const command_t CMD_COLOR = 0xAA;
47 const command_t CMD_END = 0xED;
50 * Increase value at given address
51 * parameter: address_t
53 const command_t CMD_INCREMENT = 0x11;
56 * Decrease value at given address
57 * parameter: address_t
59 const command_t CMD_DECREMENT = 0x12;
62 * Placeholder for unsupported command.
65 const command_t CMD_INVALID = 0x1;
68 * Reads data on given position in memory and increments the index (position).
70 template<typename T> T read(byte_t * memory, address_t &index) {
71 // TODO: for addresses: map higher memory to static hardcoded areas or peripherals
72 // TODO: sizeof (byte_t) != 1 ?
73 T * value = reinterpret_cast<T*> (memory + index);
74 index += sizeof (*value) / sizeof (byte_t);
79 * Writes data to given position in memory and increments the index (position).
81 template<typename T> void write(byte_t * memory, address_t &index, const T value) {
82 // TODO: sizeof (byte_t) != 1 ?
83 // T * m = (T*) (memory + index);
84 T * m = reinterpret_cast<T*> (memory + index);
86 index += sizeof (value) / sizeof (byte_t);
89 int main(int argc, char* argv[]) {
91 setlocale(LC_ALL, "");
93 byte_t * memory = (byte_t*) malloc(MEMORY_SIZE);
97 write<command_t>(memory, a, CMD_SLEEP);
98 write<sleep_t>(memory, a, 255);
99 write<command_t>(memory, a, CMD_SLEEP);
100 write<sleep_t>(memory, a, 10);
101 write<command_t>(memory, a, CMD_SLEEP);
102 write<sleep_t>(memory, a, 255);
103 write<command_t>(memory, a, CMD_GOTO);
104 write<address_t>(memory, a, a + 4);
105 write<command_t>(memory, a, CMD_INVALID);
106 write<command_t>(memory, a, CMD_INVALID);
107 write<command_t>(memory, a, CMD_SLEEP);
108 write<sleep_t>(memory, a, 255);
109 write<command_t>(memory, a, CMD_COLOR);
110 write<led_t>(memory, a, 23);
111 write<color_t>(memory, a, 0);
112 write<color_t>(memory, a, 200);
113 write<color_t>(memory, a, 255);
114 write<command_t>(memory, a, CMD_INCREMENT);
115 write<address_t>(memory, a, 0);
116 write<command_t>(memory, a, CMD_DECREMENT);
117 write<address_t>(memory, a, 0);
118 write<command_t>(memory, a, CMD_END);
121 for (address_t i = 0; i < MEMORY_SIZE;) {
122 wprintf(L"command %*d = ", 4, i);
123 command_t ch = read<command_t>(memory, i);
124 wprintf(L"%02X ", ch);
129 i = read<address_t>(memory, i);
130 wprintf(L"GOTO %*d\n", 5, i);
135 sleep_t delay = read<sleep_t>(memory, i);
136 wprintf(L"SLEEP %*d ms\n", 4, delay);
137 this_thread::sleep_for(chrono::milliseconds(delay));
142 led_t led = read<led_t>(memory, i);
143 color_t r = read<color_t>(memory, i);
144 color_t g = read<color_t>(memory, i);
145 color_t b = read<color_t>(memory, i);
146 wprintf(L"COLOR %02X %02X %02X → %d\n", r, g, b, led);
152 address_t address = read<address_t>(memory, i);
153 address_t address_r = address;
154 address_t address_w = address_r;
155 byte_t value = read<byte_t>(memory, address_r);
156 value = ch == CMD_INCREMENT ? value + 1 : value - 1;
157 write<byte_t>(memory, address_w, value);
158 wprintf(L"%sCREMENT %*d → %02X\n", (ch == CMD_INCREMENT ? "IN" : "DE"), 5, address, value);
169 wprintf(L"invalid command\n");
177 wprintf(L"all done\n");