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