c++/rgb-assembler/rgb-assembler.cpp
author František Kučera <franta-hg@frantovo.cz>
Sun, 24 Dec 2017 00:30:54 +0100
changeset 29 10d6964e7b4a
parent 28 2b2f8a17d63a
child 30 f049c3d3244d
permissions -rw-r--r--
license: GNU GPLv3+
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@20
    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@0
    39
int main(int argc, char* argv[]) {
franta-hg@0
    40
franta-hg@0
    41
	setlocale(LC_ALL, "");
franta-hg@0
    42
franta-hg@17
    43
	octet_t * memory = (octet_t*) malloc(MEMORY_SIZE);
franta-hg@0
    44
franta-hg@16
    45
	// Sample program / data:
franta-hg@16
    46
	// TODO: load bytes from file, stdin, serial port, network…
franta-hg@2
    47
	{
franta-hg@2
    48
		address_t a = 0;
franta-hg@9
    49
		write<command_t>(memory, a, CMD_SLEEP);
franta-hg@9
    50
		write<sleep_t>(memory, a, 255);
franta-hg@9
    51
		write<command_t>(memory, a, CMD_SLEEP);
franta-hg@9
    52
		write<sleep_t>(memory, a, 10);
franta-hg@9
    53
		write<command_t>(memory, a, CMD_SLEEP);
franta-hg@9
    54
		write<sleep_t>(memory, a, 255);
franta-hg@9
    55
		write<command_t>(memory, a, CMD_GOTO);
franta-hg@15
    56
		write<address_t>(memory, a, a + sizeof (address_t) + 2 * sizeof (command_t));
franta-hg@11
    57
		write<command_t>(memory, a, CMD_INVALID);
franta-hg@11
    58
		write<command_t>(memory, a, CMD_INVALID);
franta-hg@9
    59
		write<command_t>(memory, a, CMD_SLEEP);
franta-hg@9
    60
		write<sleep_t>(memory, a, 255);
franta-hg@10
    61
		write<command_t>(memory, a, CMD_COLOR);
franta-hg@13
    62
		write<led_t>(memory, a, 23);
franta-hg@11
    63
		write<color_t>(memory, a, 0);
franta-hg@11
    64
		write<color_t>(memory, a, 200);
franta-hg@11
    65
		write<color_t>(memory, a, 255);
franta-hg@14
    66
		write<command_t>(memory, a, CMD_INCREMENT);
franta-hg@14
    67
		write<address_t>(memory, a, 0);
franta-hg@14
    68
		write<command_t>(memory, a, CMD_DECREMENT);
franta-hg@14
    69
		write<address_t>(memory, a, 0);
franta-hg@15
    70
		write<command_t>(memory, a, CMD_GOTO_COMPARE);
franta-hg@15
    71
		write<address_t>(memory, a, 0);
franta-hg@15
    72
		write<address_t>(memory, a, 0 + sizeof (command_t) + sizeof (sleep_t));
franta-hg@15
    73
		write<address_t>(memory, a, a - 3 * sizeof (address_t) - 2 * sizeof (command_t));
franta-hg@15
    74
		write<address_t>(memory, a, 0);
franta-hg@15
    75
		write<address_t>(memory, a, a + sizeof (address_t));
franta-hg@9
    76
		write<command_t>(memory, a, CMD_END);
franta-hg@2
    77
	}
franta-hg@0
    78
franta-hg@21
    79
	unordered_map<command_t, shared_ptr < Command>> commands = {
franta-hg@21
    80
		{CMD_GOTO, make_shared<commands::Goto>()},
franta-hg@25
    81
		{CMD_GOTO_COMPARE, make_shared<commands::GotoCompare>()},
franta-hg@22
    82
		{CMD_SLEEP, make_shared<commands::Sleep>()},
franta-hg@23
    83
		{CMD_END, make_shared<commands::End>()},
franta-hg@24
    84
		{CMD_COLOR, make_shared<commands::Color>()},
franta-hg@27
    85
		{CMD_INCREMENT, make_shared < commands::IncrementDecrement>(true, 1)},
franta-hg@27
    86
		{CMD_DECREMENT, make_shared < commands::IncrementDecrement>(false, 1)},
franta-hg@21
    87
	};
franta-hg@27
    88
franta-hg@21
    89
franta-hg@2
    90
	for (address_t i = 0; i < MEMORY_SIZE;) {
franta-hg@11
    91
		wprintf(L"command %*d = ", 4, i);
franta-hg@26
    92
		command_t commandCode = read<command_t>(memory, i);
franta-hg@26
    93
		wprintf(L"%02X  ", commandCode);
franta-hg@0
    94
franta-hg@26
    95
		shared_ptr<Command> command = commands[commandCode];
franta-hg@21
    96
franta-hg@26
    97
		if (command) {
franta-hg@26
    98
			command->process(memory, i);
franta-hg@21
    99
		} else {
franta-hg@26
   100
			wprintf(L"invalid command\n");
franta-hg@21
   101
		}
franta-hg@0
   102
	}
franta-hg@0
   103
franta-hg@7
   104
	free(memory);
franta-hg@7
   105
	memory = nullptr;
franta-hg@0
   106
	wprintf(L"all done\n");
franta-hg@0
   107
	return 0;
franta-hg@0
   108
}