c++/rgb-assembler/rgb-assembler.cpp
author František Kučera <franta-hg@frantovo.cz>
Sun, 24 Dec 2017 00:17:12 +0100
changeset 28 2b2f8a17d63a
parent 27 835bd4559e5c
child 29 10d6964e7b4a
permissions -rw-r--r--
move standard commands (codes) to a header file
     1 #include <cstdlib>
     2 #include <iostream>
     3 #include <wchar.h>
     4 #include <locale.h>
     5 #include <cstring>
     6 #include <unordered_map>
     7 
     8 #include "types.h"
     9 #include "memory.h"
    10 #include "Command.h"
    11 #include "commands.h"
    12 #include "commands/Goto.h"
    13 #include "commands/GotoCompare.h"
    14 #include "commands/Sleep.h"
    15 #include "commands/End.h"
    16 #include "commands/Color.h"
    17 #include "commands/IncrementDecrement.h"
    18 
    19 using namespace std;
    20 
    21 int main(int argc, char* argv[]) {
    22 
    23 	setlocale(LC_ALL, "");
    24 
    25 	octet_t * memory = (octet_t*) malloc(MEMORY_SIZE);
    26 
    27 	// Sample program / data:
    28 	// TODO: load bytes from file, stdin, serial port, network…
    29 	{
    30 		address_t a = 0;
    31 		write<command_t>(memory, a, CMD_SLEEP);
    32 		write<sleep_t>(memory, a, 255);
    33 		write<command_t>(memory, a, CMD_SLEEP);
    34 		write<sleep_t>(memory, a, 10);
    35 		write<command_t>(memory, a, CMD_SLEEP);
    36 		write<sleep_t>(memory, a, 255);
    37 		write<command_t>(memory, a, CMD_GOTO);
    38 		write<address_t>(memory, a, a + sizeof (address_t) + 2 * sizeof (command_t));
    39 		write<command_t>(memory, a, CMD_INVALID);
    40 		write<command_t>(memory, a, CMD_INVALID);
    41 		write<command_t>(memory, a, CMD_SLEEP);
    42 		write<sleep_t>(memory, a, 255);
    43 		write<command_t>(memory, a, CMD_COLOR);
    44 		write<led_t>(memory, a, 23);
    45 		write<color_t>(memory, a, 0);
    46 		write<color_t>(memory, a, 200);
    47 		write<color_t>(memory, a, 255);
    48 		write<command_t>(memory, a, CMD_INCREMENT);
    49 		write<address_t>(memory, a, 0);
    50 		write<command_t>(memory, a, CMD_DECREMENT);
    51 		write<address_t>(memory, a, 0);
    52 		write<command_t>(memory, a, CMD_GOTO_COMPARE);
    53 		write<address_t>(memory, a, 0);
    54 		write<address_t>(memory, a, 0 + sizeof (command_t) + sizeof (sleep_t));
    55 		write<address_t>(memory, a, a - 3 * sizeof (address_t) - 2 * sizeof (command_t));
    56 		write<address_t>(memory, a, 0);
    57 		write<address_t>(memory, a, a + sizeof (address_t));
    58 		write<command_t>(memory, a, CMD_END);
    59 	}
    60 
    61 	unordered_map<command_t, shared_ptr < Command>> commands = {
    62 		{CMD_GOTO, make_shared<commands::Goto>()},
    63 		{CMD_GOTO_COMPARE, make_shared<commands::GotoCompare>()},
    64 		{CMD_SLEEP, make_shared<commands::Sleep>()},
    65 		{CMD_END, make_shared<commands::End>()},
    66 		{CMD_COLOR, make_shared<commands::Color>()},
    67 		{CMD_INCREMENT, make_shared < commands::IncrementDecrement>(true, 1)},
    68 		{CMD_DECREMENT, make_shared < commands::IncrementDecrement>(false, 1)},
    69 	};
    70 
    71 
    72 	for (address_t i = 0; i < MEMORY_SIZE;) {
    73 		wprintf(L"command %*d = ", 4, i);
    74 		command_t commandCode = read<command_t>(memory, i);
    75 		wprintf(L"%02X  ", commandCode);
    76 
    77 		shared_ptr<Command> command = commands[commandCode];
    78 
    79 		if (command) {
    80 			command->process(memory, i);
    81 		} else {
    82 			wprintf(L"invalid command\n");
    83 		}
    84 	}
    85 
    86 	free(memory);
    87 	memory = nullptr;
    88 	wprintf(L"all done\n");
    89 	return 0;
    90 }