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