c++/rgb-assembler/rgb-assembler.cpp
author František Kučera <franta-hg@frantovo.cz>
Mon, 25 Dec 2017 00:24:07 +0100
changeset 31 b997cbf9e30b
parent 30 f049c3d3244d
child 32 78c4d6b53499
permissions -rw-r--r--
wrap memory in a class
     1 /**
     2  * RGB assembler
     3  * Copyright © 2017 František Kučera (frantovo.cz)
     4  *
     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.
     9  *
    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.
    14  *
    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/>.
    17  */
    18 
    19 #include <cstdlib>
    20 #include <iostream>
    21 #include <wchar.h>
    22 #include <locale.h>
    23 #include <cstring>
    24 #include <unordered_map>
    25 
    26 #include "types.h"
    27 #include "memory.h"
    28 #include "Command.h"
    29 #include "commands.h"
    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"
    36 
    37 using namespace std;
    38 
    39 int main(int argc, char* argv[]) {
    40 
    41 	setlocale(LC_ALL, "");
    42 
    43 	Memory memory;
    44 	
    45 	// Sample program / data:
    46 	// TODO: load bytes from file, stdin, serial port, network…
    47 	{
    48 		memory.write<command_t>(CMD_SLEEP);
    49 		memory.write<sleep_t>(255);
    50 		memory.write<command_t>(CMD_SLEEP);
    51 		memory.write<sleep_t>(10);
    52 		memory.write<command_t>(CMD_SLEEP);
    53 		memory.write<sleep_t>(255);
    54 		memory.write<command_t>(CMD_GOTO);
    55 		memory.write<address_t>(memory.getIndex() + sizeof (address_t) + 2 * sizeof (command_t));
    56 		memory.write<command_t>(CMD_INVALID);
    57 		memory.write<command_t>(CMD_INVALID);
    58 		memory.write<command_t>(CMD_SLEEP);
    59 		memory.write<sleep_t>(255);
    60 		memory.write<command_t>(CMD_COLOR);
    61 		memory.write<led_t>(23);
    62 		memory.write<color_t>(0);
    63 		memory.write<color_t>(200);
    64 		memory.write<color_t>(255);
    65 		memory.write<command_t>(CMD_INCREMENT);
    66 		memory.write<address_t>(0);
    67 		memory.write<command_t>(CMD_DECREMENT);
    68 		memory.write<address_t>(0);
    69 		memory.write<command_t>(CMD_GOTO_COMPARE);
    70 		memory.write<address_t>(0);
    71 		memory.write<address_t>(0 + sizeof (command_t) + sizeof (sleep_t));
    72 		memory.write<address_t>(memory.getIndex() - 3 * sizeof (address_t) - 2 * sizeof (command_t));
    73 		memory.write<address_t>(0);
    74 		memory.write<address_t>(memory.getIndex() + sizeof (address_t));
    75 		memory.write<command_t>(CMD_END);
    76 		memory.start();
    77 	}
    78 
    79 	// Supported commands
    80 	// TODO: dynamic reconfiguration
    81 	unordered_map<command_t, shared_ptr < Command>> commands = {
    82 		{CMD_GOTO, make_shared<commands::Goto>()},
    83 		{CMD_GOTO_COMPARE, make_shared<commands::GotoCompare>()},
    84 		{CMD_SLEEP, make_shared<commands::Sleep>()},
    85 		{CMD_END, make_shared<commands::End>()},
    86 		{CMD_COLOR, make_shared<commands::Color>()},
    87 		{CMD_INCREMENT, make_shared <commands::IncrementDecrement>(true, 1)},
    88 		{CMD_DECREMENT, make_shared <commands::IncrementDecrement>(false, 1)},
    89 	};
    90 
    91 
    92 	// Main loop / interpreter:
    93 	while (memory.isInside()) {
    94 		wprintf(L"command %*d = ", 4, memory.getIndex());
    95 		command_t commandCode = memory.read<command_t>();
    96 		wprintf(L"%02X  ", commandCode);
    97 
    98 		shared_ptr<Command> command = commands[commandCode];
    99 
   100 		if (command) {
   101 			command->process(memory);
   102 		} else {
   103 			wprintf(L"invalid command\n");
   104 		}
   105 	}
   106 
   107 	wprintf(L"all done\n");
   108 	return 0;
   109 }