franta-hg@29
|
1 |
/**
|
franta-hg@29
|
2 |
* RGB assembler
|
franta-hg@29
|
3 |
* Copyright © 2017 František Kučera (frantovo.cz)
|
franta-hg@29
|
4 |
*
|
franta-hg@29
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@29
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@29
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
franta-hg@29
|
8 |
* (at your option) any later version.
|
franta-hg@29
|
9 |
*
|
franta-hg@29
|
10 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@29
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@29
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@29
|
13 |
* GNU General Public License for more details.
|
franta-hg@29
|
14 |
*
|
franta-hg@29
|
15 |
* You should have received a copy of the GNU General Public License
|
franta-hg@29
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@29
|
17 |
*/
|
franta-hg@29
|
18 |
|
franta-hg@0
|
19 |
#include <cstdlib>
|
franta-hg@0
|
20 |
#include <iostream>
|
franta-hg@0
|
21 |
#include <wchar.h>
|
franta-hg@0
|
22 |
#include <locale.h>
|
franta-hg@0
|
23 |
#include <cstring>
|
franta-hg@20
|
24 |
#include <unordered_map>
|
franta-hg@6
|
25 |
|
franta-hg@19
|
26 |
#include "types.h"
|
franta-hg@32
|
27 |
#include "Memory.h"
|
franta-hg@20
|
28 |
#include "Command.h"
|
franta-hg@28
|
29 |
#include "commands.h"
|
franta-hg@20
|
30 |
#include "commands/Goto.h"
|
franta-hg@25
|
31 |
#include "commands/GotoCompare.h"
|
franta-hg@22
|
32 |
#include "commands/Sleep.h"
|
franta-hg@23
|
33 |
#include "commands/End.h"
|
franta-hg@24
|
34 |
#include "commands/Color.h"
|
franta-hg@26
|
35 |
#include "commands/IncrementDecrement.h"
|
franta-hg@19
|
36 |
|
franta-hg@0
|
37 |
using namespace std;
|
franta-hg@0
|
38 |
|
franta-hg@34
|
39 |
const address_t MEMORY_SIZE = 1024;
|
franta-hg@34
|
40 |
|
franta-hg@0
|
41 |
int main(int argc, char* argv[]) {
|
franta-hg@0
|
42 |
|
franta-hg@0
|
43 |
setlocale(LC_ALL, "");
|
franta-hg@0
|
44 |
|
franta-hg@34
|
45 |
Memory memory(MEMORY_SIZE);
|
franta-hg@34
|
46 |
|
franta-hg@16
|
47 |
// Sample program / data:
|
franta-hg@16
|
48 |
// TODO: load bytes from file, stdin, serial port, network…
|
franta-hg@2
|
49 |
{
|
franta-hg@31
|
50 |
memory.write<command_t>(CMD_SLEEP);
|
franta-hg@31
|
51 |
memory.write<sleep_t>(255);
|
franta-hg@31
|
52 |
memory.write<command_t>(CMD_SLEEP);
|
franta-hg@31
|
53 |
memory.write<sleep_t>(10);
|
franta-hg@31
|
54 |
memory.write<command_t>(CMD_SLEEP);
|
franta-hg@31
|
55 |
memory.write<sleep_t>(255);
|
franta-hg@31
|
56 |
memory.write<command_t>(CMD_GOTO);
|
franta-hg@33
|
57 |
memory.write<address_t>(memory.getAddress() + sizeof (address_t) + 2 * sizeof (command_t));
|
franta-hg@31
|
58 |
memory.write<command_t>(CMD_INVALID);
|
franta-hg@31
|
59 |
memory.write<command_t>(CMD_INVALID);
|
franta-hg@31
|
60 |
memory.write<command_t>(CMD_SLEEP);
|
franta-hg@31
|
61 |
memory.write<sleep_t>(255);
|
franta-hg@31
|
62 |
memory.write<command_t>(CMD_COLOR);
|
franta-hg@31
|
63 |
memory.write<led_t>(23);
|
franta-hg@31
|
64 |
memory.write<color_t>(0);
|
franta-hg@31
|
65 |
memory.write<color_t>(200);
|
franta-hg@31
|
66 |
memory.write<color_t>(255);
|
franta-hg@31
|
67 |
memory.write<command_t>(CMD_INCREMENT);
|
franta-hg@31
|
68 |
memory.write<address_t>(0);
|
franta-hg@31
|
69 |
memory.write<command_t>(CMD_DECREMENT);
|
franta-hg@31
|
70 |
memory.write<address_t>(0);
|
franta-hg@31
|
71 |
memory.write<command_t>(CMD_GOTO_COMPARE);
|
franta-hg@31
|
72 |
memory.write<address_t>(0);
|
franta-hg@31
|
73 |
memory.write<address_t>(0 + sizeof (command_t) + sizeof (sleep_t));
|
franta-hg@33
|
74 |
memory.write<address_t>(memory.getAddress() - 3 * sizeof (address_t) - 2 * sizeof (command_t));
|
franta-hg@31
|
75 |
memory.write<address_t>(0);
|
franta-hg@33
|
76 |
memory.write<address_t>(memory.getAddress() + sizeof (address_t));
|
franta-hg@31
|
77 |
memory.write<command_t>(CMD_END);
|
franta-hg@33
|
78 |
memory.setAddressToBeginning();
|
franta-hg@2
|
79 |
}
|
franta-hg@0
|
80 |
|
franta-hg@30
|
81 |
// Supported commands
|
franta-hg@30
|
82 |
// TODO: dynamic reconfiguration
|
franta-hg@21
|
83 |
unordered_map<command_t, shared_ptr < Command>> commands = {
|
franta-hg@21
|
84 |
{CMD_GOTO, make_shared<commands::Goto>()},
|
franta-hg@25
|
85 |
{CMD_GOTO_COMPARE, make_shared<commands::GotoCompare>()},
|
franta-hg@22
|
86 |
{CMD_SLEEP, make_shared<commands::Sleep>()},
|
franta-hg@23
|
87 |
{CMD_END, make_shared<commands::End>()},
|
franta-hg@24
|
88 |
{CMD_COLOR, make_shared<commands::Color>()},
|
franta-hg@30
|
89 |
{CMD_INCREMENT, make_shared <commands::IncrementDecrement>(true, 1)},
|
franta-hg@30
|
90 |
{CMD_DECREMENT, make_shared <commands::IncrementDecrement>(false, 1)},
|
franta-hg@21
|
91 |
};
|
franta-hg@27
|
92 |
|
franta-hg@21
|
93 |
|
franta-hg@30
|
94 |
// Main loop / interpreter:
|
franta-hg@33
|
95 |
while (memory.isNotOver()) {
|
franta-hg@33
|
96 |
wprintf(L"command %*d = ", 4, memory.getAddress());
|
franta-hg@31
|
97 |
command_t commandCode = memory.read<command_t>();
|
franta-hg@26
|
98 |
wprintf(L"%02X ", commandCode);
|
franta-hg@0
|
99 |
|
franta-hg@26
|
100 |
shared_ptr<Command> command = commands[commandCode];
|
franta-hg@21
|
101 |
|
franta-hg@26
|
102 |
if (command) {
|
franta-hg@31
|
103 |
command->process(memory);
|
franta-hg@21
|
104 |
} else {
|
franta-hg@26
|
105 |
wprintf(L"invalid command\n");
|
franta-hg@21
|
106 |
}
|
franta-hg@0
|
107 |
}
|
franta-hg@0
|
108 |
|
franta-hg@0
|
109 |
wprintf(L"all done\n");
|
franta-hg@0
|
110 |
return 0;
|
franta-hg@0
|
111 |
}
|