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@0
|
13 |
typedef uint8_t command_t;
|
franta-hg@6
|
14 |
typedef uint8_t sleep_t;
|
franta-hg@10
|
15 |
typedef uint8_t color_t;
|
franta-hg@0
|
16 |
|
franta-hg@12
|
17 |
// TODO: strong typedefs http://www.boost.org/doc/libs/1_61_0/libs/serialization/doc/strong_typedef.html ?
|
franta-hg@12
|
18 |
|
franta-hg@0
|
19 |
const address_t MEMORY_SIZE = 1024;
|
franta-hg@0
|
20 |
|
franta-hg@5
|
21 |
const command_t CMD_GOTO = 0x70;
|
franta-hg@5
|
22 |
const command_t CMD_SLEEP = 0xFF;
|
franta-hg@10
|
23 |
const command_t CMD_COLOR = 0xAA;
|
franta-hg@5
|
24 |
const command_t CMD_END = 0xED;
|
franta-hg@11
|
25 |
const command_t CMD_INVALID = 0x1; // placeholder for unsupported command
|
franta-hg@0
|
26 |
|
franta-hg@1
|
27 |
/**
|
franta-hg@8
|
28 |
* Reads data on given position in memory and increments the index (position).
|
franta-hg@1
|
29 |
*/
|
franta-hg@8
|
30 |
template<typename T> T read(command_t * memory, address_t &index) {
|
franta-hg@8
|
31 |
// TODO: for addresses: map higher memory to static hardcoded areas or peripherals
|
franta-hg@8
|
32 |
// TODO: sizeof (command_t) != 1 ?
|
franta-hg@8
|
33 |
T * value = reinterpret_cast<T*> (memory + index);
|
franta-hg@6
|
34 |
index += sizeof (*value) / sizeof (command_t);
|
franta-hg@6
|
35 |
return *value;
|
franta-hg@0
|
36 |
}
|
franta-hg@0
|
37 |
|
franta-hg@9
|
38 |
/**
|
franta-hg@9
|
39 |
* Writes data to given position in memory and increments the index (position).
|
franta-hg@9
|
40 |
*/
|
franta-hg@9
|
41 |
template<typename T> void write(command_t * memory, address_t &index, const T value) {
|
franta-hg@9
|
42 |
// TODO: sizeof (command_t) != 1 ?
|
franta-hg@9
|
43 |
// T * m = (T*) (memory + index);
|
franta-hg@9
|
44 |
T * m = reinterpret_cast<T*> (memory + index);
|
franta-hg@6
|
45 |
*m = value;
|
franta-hg@6
|
46 |
index += sizeof (value) / sizeof (command_t);
|
franta-hg@6
|
47 |
}
|
franta-hg@6
|
48 |
|
franta-hg@0
|
49 |
int main(int argc, char* argv[]) {
|
franta-hg@0
|
50 |
|
franta-hg@0
|
51 |
setlocale(LC_ALL, "");
|
franta-hg@0
|
52 |
|
franta-hg@2
|
53 |
command_t * memory = (command_t*) malloc(MEMORY_SIZE);
|
franta-hg@0
|
54 |
|
franta-hg@2
|
55 |
{
|
franta-hg@2
|
56 |
address_t a = 0;
|
franta-hg@9
|
57 |
write<command_t>(memory, a, CMD_SLEEP);
|
franta-hg@9
|
58 |
write<sleep_t>(memory, a, 255);
|
franta-hg@9
|
59 |
write<command_t>(memory, a, CMD_SLEEP);
|
franta-hg@9
|
60 |
write<sleep_t>(memory, a, 10);
|
franta-hg@9
|
61 |
write<command_t>(memory, a, CMD_SLEEP);
|
franta-hg@9
|
62 |
write<sleep_t>(memory, a, 255);
|
franta-hg@9
|
63 |
write<command_t>(memory, a, CMD_GOTO);
|
franta-hg@9
|
64 |
write<address_t>(memory, a, a + 4);
|
franta-hg@11
|
65 |
write<command_t>(memory, a, CMD_INVALID);
|
franta-hg@11
|
66 |
write<command_t>(memory, a, CMD_INVALID);
|
franta-hg@9
|
67 |
write<command_t>(memory, a, CMD_SLEEP);
|
franta-hg@9
|
68 |
write<sleep_t>(memory, a, 255);
|
franta-hg@10
|
69 |
write<command_t>(memory, a, CMD_COLOR);
|
franta-hg@11
|
70 |
write<color_t>(memory, a, 0);
|
franta-hg@11
|
71 |
write<color_t>(memory, a, 200);
|
franta-hg@11
|
72 |
write<color_t>(memory, a, 255);
|
franta-hg@9
|
73 |
write<command_t>(memory, a, CMD_END);
|
franta-hg@2
|
74 |
}
|
franta-hg@0
|
75 |
|
franta-hg@2
|
76 |
for (address_t i = 0; i < MEMORY_SIZE;) {
|
franta-hg@11
|
77 |
wprintf(L"command %*d = ", 4, i);
|
franta-hg@8
|
78 |
command_t ch = read<command_t>(memory, i);
|
franta-hg@11
|
79 |
wprintf(L"%02X ", ch);
|
franta-hg@0
|
80 |
|
franta-hg@0
|
81 |
switch (ch) {
|
franta-hg@0
|
82 |
case CMD_GOTO:
|
franta-hg@12
|
83 |
{
|
franta-hg@8
|
84 |
i = read<address_t>(memory, i);
|
franta-hg@11
|
85 |
wprintf(L"GOTO %*d\n", 5, i);
|
franta-hg@0
|
86 |
break;
|
franta-hg@12
|
87 |
}
|
franta-hg@0
|
88 |
case CMD_SLEEP:
|
franta-hg@12
|
89 |
{
|
franta-hg@12
|
90 |
sleep_t delay = read<sleep_t>(memory, i);
|
franta-hg@11
|
91 |
wprintf(L"SLEEP %*d ms\n", 4, delay);
|
franta-hg@11
|
92 |
this_thread::sleep_for(chrono::milliseconds(delay));
|
franta-hg@0
|
93 |
break;
|
franta-hg@12
|
94 |
}
|
franta-hg@10
|
95 |
case CMD_COLOR:
|
franta-hg@12
|
96 |
{
|
franta-hg@12
|
97 |
color_t r = read<color_t>(memory, i);
|
franta-hg@12
|
98 |
color_t g = read<color_t>(memory, i);
|
franta-hg@12
|
99 |
color_t b = read<color_t>(memory, i);
|
franta-hg@11
|
100 |
wprintf(L"COLOR %02X %02X %02X\n", r, g, b);
|
franta-hg@10
|
101 |
break;
|
franta-hg@12
|
102 |
}
|
franta-hg@0
|
103 |
case CMD_END:
|
franta-hg@12
|
104 |
{
|
franta-hg@11
|
105 |
wprintf(L"END\n");
|
franta-hg@2
|
106 |
i = MEMORY_SIZE;
|
franta-hg@0
|
107 |
break;
|
franta-hg@12
|
108 |
}
|
franta-hg@11
|
109 |
default:
|
franta-hg@12
|
110 |
{
|
franta-hg@11
|
111 |
wprintf(L"invalid command\n");
|
franta-hg@12
|
112 |
}
|
franta-hg@0
|
113 |
}
|
franta-hg@0
|
114 |
|
franta-hg@0
|
115 |
}
|
franta-hg@0
|
116 |
|
franta-hg@7
|
117 |
free(memory);
|
franta-hg@7
|
118 |
memory = nullptr;
|
franta-hg@0
|
119 |
wprintf(L"all done\n");
|
franta-hg@0
|
120 |
return 0;
|
franta-hg@0
|
121 |
}
|
franta-hg@0
|
122 |
|