franta-hg@0
|
1 |
#include <cstdlib>
|
franta-hg@0
|
2 |
#include <iostream>
|
franta-hg@0
|
3 |
#include <wchar.h>
|
franta-hg@0
|
4 |
#include <locale.h>
|
franta-hg@0
|
5 |
#include <cstring>
|
franta-hg@0
|
6 |
|
franta-hg@6
|
7 |
#include <chrono>
|
franta-hg@6
|
8 |
#include <thread>
|
franta-hg@6
|
9 |
|
franta-hg@0
|
10 |
using namespace std;
|
franta-hg@0
|
11 |
|
franta-hg@0
|
12 |
typedef uint16_t address_t;
|
franta-hg@14
|
13 |
typedef uint8_t byte_t;
|
franta-hg@0
|
14 |
typedef uint8_t command_t;
|
franta-hg@6
|
15 |
typedef uint8_t sleep_t;
|
franta-hg@10
|
16 |
typedef uint8_t color_t;
|
franta-hg@13
|
17 |
typedef uint8_t led_t;
|
franta-hg@0
|
18 |
|
franta-hg@12
|
19 |
// TODO: strong typedefs http://www.boost.org/doc/libs/1_61_0/libs/serialization/doc/strong_typedef.html ?
|
franta-hg@12
|
20 |
|
franta-hg@0
|
21 |
const address_t MEMORY_SIZE = 1024;
|
franta-hg@0
|
22 |
|
franta-hg@13
|
23 |
/**
|
franta-hg@13
|
24 |
* Skip to the given address.
|
franta-hg@13
|
25 |
* parameter: address_t
|
franta-hg@13
|
26 |
*/
|
franta-hg@5
|
27 |
const command_t CMD_GOTO = 0x70;
|
franta-hg@13
|
28 |
|
franta-hg@13
|
29 |
/**
|
franta-hg@15
|
30 |
* Compare values on two addresses and go to one of given three addresses.
|
franta-hg@15
|
31 |
* parameter: address_t a
|
franta-hg@15
|
32 |
* parameter: address_t b
|
franta-hg@15
|
33 |
* parameter: address_t GOTO target when a == b
|
franta-hg@15
|
34 |
* parameter: address_t GOTO target when a > b
|
franta-hg@15
|
35 |
* parameter: address_t GOTO target when a < b
|
franta-hg@15
|
36 |
*/
|
franta-hg@15
|
37 |
const command_t CMD_GOTO_COMPARE = 0x80;
|
franta-hg@15
|
38 |
|
franta-hg@15
|
39 |
/**
|
franta-hg@13
|
40 |
* Wait given time in ms.
|
franta-hg@13
|
41 |
* parameter: sleep_t
|
franta-hg@13
|
42 |
*/
|
franta-hg@5
|
43 |
const command_t CMD_SLEEP = 0xFF;
|
franta-hg@13
|
44 |
|
franta-hg@13
|
45 |
/**
|
franta-hg@13
|
46 |
* Set RGB LED color.
|
franta-hg@13
|
47 |
* parameter: led_t LED number
|
franta-hg@13
|
48 |
* parameter: color_t red
|
franta-hg@13
|
49 |
* parameter: color_t green
|
franta-hg@13
|
50 |
* parameter: color_t blue
|
franta-hg@13
|
51 |
*/
|
franta-hg@10
|
52 |
const command_t CMD_COLOR = 0xAA;
|
franta-hg@13
|
53 |
|
franta-hg@13
|
54 |
/**
|
franta-hg@13
|
55 |
* Stop program.
|
franta-hg@13
|
56 |
*/
|
franta-hg@5
|
57 |
const command_t CMD_END = 0xED;
|
franta-hg@13
|
58 |
|
franta-hg@13
|
59 |
/**
|
franta-hg@14
|
60 |
* Increase value at given address
|
franta-hg@14
|
61 |
* parameter: address_t
|
franta-hg@14
|
62 |
*/
|
franta-hg@14
|
63 |
const command_t CMD_INCREMENT = 0x11;
|
franta-hg@14
|
64 |
|
franta-hg@14
|
65 |
/**
|
franta-hg@14
|
66 |
* Decrease value at given address
|
franta-hg@14
|
67 |
* parameter: address_t
|
franta-hg@14
|
68 |
*/
|
franta-hg@14
|
69 |
const command_t CMD_DECREMENT = 0x12;
|
franta-hg@14
|
70 |
|
franta-hg@14
|
71 |
/**
|
franta-hg@13
|
72 |
* Placeholder for unsupported command.
|
franta-hg@13
|
73 |
* Just for testing.
|
franta-hg@13
|
74 |
*/
|
franta-hg@13
|
75 |
const command_t CMD_INVALID = 0x1;
|
franta-hg@0
|
76 |
|
franta-hg@15
|
77 |
// TODO: more commands, better numbers
|
franta-hg@15
|
78 |
|
franta-hg@1
|
79 |
/**
|
franta-hg@8
|
80 |
* Reads data on given position in memory and increments the index (position).
|
franta-hg@1
|
81 |
*/
|
franta-hg@14
|
82 |
template<typename T> T read(byte_t * memory, address_t &index) {
|
franta-hg@8
|
83 |
// TODO: for addresses: map higher memory to static hardcoded areas or peripherals
|
franta-hg@14
|
84 |
// TODO: sizeof (byte_t) != 1 ?
|
franta-hg@8
|
85 |
T * value = reinterpret_cast<T*> (memory + index);
|
franta-hg@14
|
86 |
index += sizeof (*value) / sizeof (byte_t);
|
franta-hg@6
|
87 |
return *value;
|
franta-hg@0
|
88 |
}
|
franta-hg@0
|
89 |
|
franta-hg@9
|
90 |
/**
|
franta-hg@9
|
91 |
* Writes data to given position in memory and increments the index (position).
|
franta-hg@9
|
92 |
*/
|
franta-hg@14
|
93 |
template<typename T> void write(byte_t * memory, address_t &index, const T value) {
|
franta-hg@14
|
94 |
// TODO: sizeof (byte_t) != 1 ?
|
franta-hg@9
|
95 |
// T * m = (T*) (memory + index);
|
franta-hg@9
|
96 |
T * m = reinterpret_cast<T*> (memory + index);
|
franta-hg@6
|
97 |
*m = value;
|
franta-hg@14
|
98 |
index += sizeof (value) / sizeof (byte_t);
|
franta-hg@6
|
99 |
}
|
franta-hg@6
|
100 |
|
franta-hg@0
|
101 |
int main(int argc, char* argv[]) {
|
franta-hg@0
|
102 |
|
franta-hg@0
|
103 |
setlocale(LC_ALL, "");
|
franta-hg@0
|
104 |
|
franta-hg@14
|
105 |
byte_t * memory = (byte_t*) malloc(MEMORY_SIZE);
|
franta-hg@0
|
106 |
|
franta-hg@16
|
107 |
// Sample program / data:
|
franta-hg@16
|
108 |
// TODO: load bytes from file, stdin, serial port, network…
|
franta-hg@2
|
109 |
{
|
franta-hg@2
|
110 |
address_t a = 0;
|
franta-hg@9
|
111 |
write<command_t>(memory, a, CMD_SLEEP);
|
franta-hg@9
|
112 |
write<sleep_t>(memory, a, 255);
|
franta-hg@9
|
113 |
write<command_t>(memory, a, CMD_SLEEP);
|
franta-hg@9
|
114 |
write<sleep_t>(memory, a, 10);
|
franta-hg@9
|
115 |
write<command_t>(memory, a, CMD_SLEEP);
|
franta-hg@9
|
116 |
write<sleep_t>(memory, a, 255);
|
franta-hg@9
|
117 |
write<command_t>(memory, a, CMD_GOTO);
|
franta-hg@15
|
118 |
write<address_t>(memory, a, a + sizeof (address_t) + 2 * sizeof (command_t));
|
franta-hg@11
|
119 |
write<command_t>(memory, a, CMD_INVALID);
|
franta-hg@11
|
120 |
write<command_t>(memory, a, CMD_INVALID);
|
franta-hg@9
|
121 |
write<command_t>(memory, a, CMD_SLEEP);
|
franta-hg@9
|
122 |
write<sleep_t>(memory, a, 255);
|
franta-hg@10
|
123 |
write<command_t>(memory, a, CMD_COLOR);
|
franta-hg@13
|
124 |
write<led_t>(memory, a, 23);
|
franta-hg@11
|
125 |
write<color_t>(memory, a, 0);
|
franta-hg@11
|
126 |
write<color_t>(memory, a, 200);
|
franta-hg@11
|
127 |
write<color_t>(memory, a, 255);
|
franta-hg@14
|
128 |
write<command_t>(memory, a, CMD_INCREMENT);
|
franta-hg@14
|
129 |
write<address_t>(memory, a, 0);
|
franta-hg@14
|
130 |
write<command_t>(memory, a, CMD_DECREMENT);
|
franta-hg@14
|
131 |
write<address_t>(memory, a, 0);
|
franta-hg@15
|
132 |
write<command_t>(memory, a, CMD_GOTO_COMPARE);
|
franta-hg@15
|
133 |
write<address_t>(memory, a, 0);
|
franta-hg@15
|
134 |
write<address_t>(memory, a, 0 + sizeof (command_t) + sizeof (sleep_t));
|
franta-hg@15
|
135 |
write<address_t>(memory, a, a - 3 * sizeof (address_t) - 2 * sizeof (command_t));
|
franta-hg@15
|
136 |
write<address_t>(memory, a, 0);
|
franta-hg@15
|
137 |
write<address_t>(memory, a, a + sizeof (address_t));
|
franta-hg@9
|
138 |
write<command_t>(memory, a, CMD_END);
|
franta-hg@2
|
139 |
}
|
franta-hg@0
|
140 |
|
franta-hg@2
|
141 |
for (address_t i = 0; i < MEMORY_SIZE;) {
|
franta-hg@11
|
142 |
wprintf(L"command %*d = ", 4, i);
|
franta-hg@16
|
143 |
command_t command = read<command_t>(memory, i);
|
franta-hg@16
|
144 |
wprintf(L"%02X ", command);
|
franta-hg@0
|
145 |
|
franta-hg@16
|
146 |
switch (command) {
|
franta-hg@0
|
147 |
case CMD_GOTO:
|
franta-hg@12
|
148 |
{
|
franta-hg@8
|
149 |
i = read<address_t>(memory, i);
|
franta-hg@11
|
150 |
wprintf(L"GOTO %*d\n", 5, i);
|
franta-hg@0
|
151 |
break;
|
franta-hg@12
|
152 |
}
|
franta-hg@0
|
153 |
case CMD_SLEEP:
|
franta-hg@12
|
154 |
{
|
franta-hg@12
|
155 |
sleep_t delay = read<sleep_t>(memory, i);
|
franta-hg@11
|
156 |
wprintf(L"SLEEP %*d ms\n", 4, delay);
|
franta-hg@11
|
157 |
this_thread::sleep_for(chrono::milliseconds(delay));
|
franta-hg@0
|
158 |
break;
|
franta-hg@12
|
159 |
}
|
franta-hg@10
|
160 |
case CMD_COLOR:
|
franta-hg@12
|
161 |
{
|
franta-hg@13
|
162 |
led_t led = read<led_t>(memory, i);
|
franta-hg@12
|
163 |
color_t r = read<color_t>(memory, i);
|
franta-hg@12
|
164 |
color_t g = read<color_t>(memory, i);
|
franta-hg@12
|
165 |
color_t b = read<color_t>(memory, i);
|
franta-hg@13
|
166 |
wprintf(L"COLOR %02X %02X %02X → %d\n", r, g, b, led);
|
franta-hg@10
|
167 |
break;
|
franta-hg@12
|
168 |
}
|
franta-hg@14
|
169 |
case CMD_INCREMENT:
|
franta-hg@14
|
170 |
case CMD_DECREMENT:
|
franta-hg@14
|
171 |
{
|
franta-hg@14
|
172 |
address_t address = read<address_t>(memory, i);
|
franta-hg@14
|
173 |
address_t address_r = address;
|
franta-hg@14
|
174 |
address_t address_w = address_r;
|
franta-hg@14
|
175 |
byte_t value = read<byte_t>(memory, address_r);
|
franta-hg@16
|
176 |
value = command == CMD_INCREMENT ? value + 1 : value - 1;
|
franta-hg@14
|
177 |
write<byte_t>(memory, address_w, value);
|
franta-hg@16
|
178 |
wprintf(L"%sCREMENT %*d → %02X\n", (command == CMD_INCREMENT ? "IN" : "DE"), 5, address, value);
|
franta-hg@14
|
179 |
break;
|
franta-hg@14
|
180 |
}
|
franta-hg@15
|
181 |
case CMD_GOTO_COMPARE:
|
franta-hg@15
|
182 |
{
|
franta-hg@15
|
183 |
address_t aa = read<address_t>(memory, i);
|
franta-hg@15
|
184 |
address_t ab = read<address_t>(memory, i);
|
franta-hg@15
|
185 |
address_t eq = read<address_t>(memory, i);
|
franta-hg@15
|
186 |
address_t gt = read<address_t>(memory, i);
|
franta-hg@15
|
187 |
address_t lt = read<address_t>(memory, i);
|
franta-hg@15
|
188 |
|
franta-hg@15
|
189 |
byte_t a = read<byte_t>(memory, aa);
|
franta-hg@15
|
190 |
byte_t b = read<byte_t>(memory, ab);
|
franta-hg@15
|
191 |
|
franta-hg@16
|
192 |
if (a == b) i = eq;
|
franta-hg@16
|
193 |
else if (a > b) i = gt;
|
franta-hg@16
|
194 |
else i = lt;
|
franta-hg@16
|
195 |
|
franta-hg@15
|
196 |
wprintf(L"GOTO COMPARE a = %02X, b = %02X, eq = %d, gt = %d, lt = %d → %d\n", a, b, eq, gt, lt, i);
|
franta-hg@15
|
197 |
break;
|
franta-hg@15
|
198 |
}
|
franta-hg@0
|
199 |
case CMD_END:
|
franta-hg@12
|
200 |
{
|
franta-hg@11
|
201 |
wprintf(L"END\n");
|
franta-hg@2
|
202 |
i = MEMORY_SIZE;
|
franta-hg@0
|
203 |
break;
|
franta-hg@12
|
204 |
}
|
franta-hg@11
|
205 |
default:
|
franta-hg@12
|
206 |
{
|
franta-hg@11
|
207 |
wprintf(L"invalid command\n");
|
franta-hg@12
|
208 |
}
|
franta-hg@0
|
209 |
}
|
franta-hg@0
|
210 |
|
franta-hg@0
|
211 |
}
|
franta-hg@0
|
212 |
|
franta-hg@7
|
213 |
free(memory);
|
franta-hg@7
|
214 |
memory = nullptr;
|
franta-hg@0
|
215 |
wprintf(L"all done\n");
|
franta-hg@0
|
216 |
return 0;
|
franta-hg@0
|
217 |
}
|
franta-hg@0
|
218 |
|