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