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@0
|
15 |
|
franta-hg@0
|
16 |
const address_t MEMORY_SIZE = 1024;
|
franta-hg@0
|
17 |
|
franta-hg@5
|
18 |
const command_t CMD_GOTO = 0x70;
|
franta-hg@5
|
19 |
const command_t CMD_SLEEP = 0xFF;
|
franta-hg@5
|
20 |
const command_t CMD_END = 0xED;
|
franta-hg@0
|
21 |
|
franta-hg@1
|
22 |
/**
|
franta-hg@1
|
23 |
* Reads the command on given position in memory and increments the index (position).
|
franta-hg@1
|
24 |
*/
|
franta-hg@2
|
25 |
command_t readCommand(command_t * memory, address_t &index) {
|
franta-hg@0
|
26 |
// TODO: map higher memory to static hardcoded areas or peripherals
|
franta-hg@3
|
27 |
command_t * value = reinterpret_cast<command_t*> (memory + index);
|
franta-hg@4
|
28 |
index += sizeof (*value) / sizeof (command_t);
|
franta-hg@3
|
29 |
return *value;
|
franta-hg@2
|
30 |
}
|
franta-hg@2
|
31 |
|
franta-hg@2
|
32 |
address_t readAddress(command_t * memory, address_t &index) {
|
franta-hg@3
|
33 |
address_t * value = reinterpret_cast<address_t*> (memory + index);
|
franta-hg@4
|
34 |
index += sizeof (*value) / sizeof (command_t);
|
franta-hg@3
|
35 |
return *value;
|
franta-hg@0
|
36 |
}
|
franta-hg@0
|
37 |
|
franta-hg@6
|
38 |
sleep_t readSleep(command_t * memory, address_t &index) {
|
franta-hg@6
|
39 |
// TODO: map higher memory to static hardcoded areas or peripherals
|
franta-hg@6
|
40 |
sleep_t * value = reinterpret_cast<sleep_t*> (memory + index);
|
franta-hg@6
|
41 |
index += sizeof (*value) / sizeof (command_t);
|
franta-hg@6
|
42 |
return *value;
|
franta-hg@0
|
43 |
}
|
franta-hg@0
|
44 |
|
franta-hg@2
|
45 |
void writeCommand(command_t * memory, address_t &index, const command_t value) {
|
franta-hg@2
|
46 |
// command_t * m = (command_t*) (memory + index);
|
franta-hg@2
|
47 |
command_t * m = reinterpret_cast<command_t*> (memory + index);
|
franta-hg@2
|
48 |
*m = value;
|
franta-hg@4
|
49 |
index += sizeof (value) / sizeof (command_t);
|
franta-hg@4
|
50 |
}
|
franta-hg@4
|
51 |
|
franta-hg@4
|
52 |
void writeAddress(command_t * memory, address_t &index, const address_t value) {
|
franta-hg@4
|
53 |
// command_t * m = (command_t*) (memory + index);
|
franta-hg@4
|
54 |
address_t * m = reinterpret_cast<address_t*> (memory + index);
|
franta-hg@4
|
55 |
*m = value;
|
franta-hg@4
|
56 |
index += sizeof (value) / sizeof (command_t);
|
franta-hg@0
|
57 |
}
|
franta-hg@0
|
58 |
|
franta-hg@6
|
59 |
void writeSleep(command_t * memory, address_t &index, const sleep_t value) {
|
franta-hg@6
|
60 |
// command_t * m = (command_t*) (memory + index);
|
franta-hg@6
|
61 |
sleep_t * m = reinterpret_cast<sleep_t*> (memory + index);
|
franta-hg@6
|
62 |
*m = value;
|
franta-hg@6
|
63 |
index += sizeof (value) / sizeof (command_t);
|
franta-hg@6
|
64 |
}
|
franta-hg@6
|
65 |
|
franta-hg@0
|
66 |
int main(int argc, char* argv[]) {
|
franta-hg@0
|
67 |
|
franta-hg@0
|
68 |
setlocale(LC_ALL, "");
|
franta-hg@0
|
69 |
|
franta-hg@2
|
70 |
command_t * memory = (command_t*) malloc(MEMORY_SIZE);
|
franta-hg@0
|
71 |
|
franta-hg@2
|
72 |
{
|
franta-hg@2
|
73 |
address_t a = 0;
|
franta-hg@2
|
74 |
writeCommand(memory, a, CMD_SLEEP);
|
franta-hg@6
|
75 |
writeSleep(memory, a, 255);
|
franta-hg@2
|
76 |
writeCommand(memory, a, CMD_SLEEP);
|
franta-hg@6
|
77 |
writeSleep(memory, a, 10);
|
franta-hg@2
|
78 |
writeCommand(memory, a, CMD_SLEEP);
|
franta-hg@6
|
79 |
writeSleep(memory, a, 255);
|
franta-hg@3
|
80 |
writeCommand(memory, a, CMD_GOTO);
|
franta-hg@6
|
81 |
writeAddress(memory, a, a+4);
|
franta-hg@3
|
82 |
writeCommand(memory, a, 1);
|
franta-hg@3
|
83 |
writeCommand(memory, a, 1);
|
franta-hg@3
|
84 |
writeCommand(memory, a, CMD_SLEEP);
|
franta-hg@6
|
85 |
writeSleep(memory, a, 255);
|
franta-hg@2
|
86 |
writeCommand(memory, a, CMD_END);
|
franta-hg@2
|
87 |
}
|
franta-hg@0
|
88 |
|
franta-hg@0
|
89 |
|
franta-hg@2
|
90 |
|
franta-hg@2
|
91 |
for (address_t i = 0; i < MEMORY_SIZE;) {
|
franta-hg@3
|
92 |
wprintf(L"command %d = ", i);
|
franta-hg@0
|
93 |
command_t ch = readCommand(memory, i);
|
franta-hg@5
|
94 |
wprintf(L"%X\n", ch);
|
franta-hg@0
|
95 |
|
franta-hg@0
|
96 |
string command;
|
franta-hg@0
|
97 |
|
franta-hg@0
|
98 |
switch (ch) {
|
franta-hg@0
|
99 |
case CMD_GOTO:
|
franta-hg@2
|
100 |
i = readAddress(memory, i);
|
franta-hg@2
|
101 |
command.append("GOTO ");
|
franta-hg@0
|
102 |
break;
|
franta-hg@0
|
103 |
case CMD_SLEEP:
|
franta-hg@0
|
104 |
command.append("SLEEP");
|
franta-hg@6
|
105 |
this_thread::sleep_for(chrono::milliseconds(readSleep(memory, i)));
|
franta-hg@0
|
106 |
break;
|
franta-hg@0
|
107 |
case CMD_END:
|
franta-hg@0
|
108 |
command.append("END");
|
franta-hg@2
|
109 |
i = MEMORY_SIZE;
|
franta-hg@0
|
110 |
break;
|
franta-hg@0
|
111 |
|
franta-hg@0
|
112 |
}
|
franta-hg@0
|
113 |
|
franta-hg@0
|
114 |
if (!command.empty()) {
|
franta-hg@0
|
115 |
wprintf(L"\t%s\n", command.c_str());
|
franta-hg@0
|
116 |
}
|
franta-hg@0
|
117 |
}
|
franta-hg@0
|
118 |
|
franta-hg@0
|
119 |
|
franta-hg@0
|
120 |
|
franta-hg@0
|
121 |
wprintf(L"all done\n");
|
franta-hg@0
|
122 |
return 0;
|
franta-hg@0
|
123 |
}
|
franta-hg@0
|
124 |
|