c++/rgb-assembler/rgb-assembler.cpp
author František Kučera <franta-hg@frantovo.cz>
Mon, 25 Dec 2017 01:27:44 +0100
changeset 33 ff150572e8c0
parent 32 78c4d6b53499
child 34 d7eed4d972de
permissions -rw-r--r--
memory: rename, refactor
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@32
    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@31
    43
	Memory memory;
franta-hg@31
    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@31
    48
		memory.write<command_t>(CMD_SLEEP);
franta-hg@31
    49
		memory.write<sleep_t>(255);
franta-hg@31
    50
		memory.write<command_t>(CMD_SLEEP);
franta-hg@31
    51
		memory.write<sleep_t>(10);
franta-hg@31
    52
		memory.write<command_t>(CMD_SLEEP);
franta-hg@31
    53
		memory.write<sleep_t>(255);
franta-hg@31
    54
		memory.write<command_t>(CMD_GOTO);
franta-hg@33
    55
		memory.write<address_t>(memory.getAddress() + sizeof (address_t) + 2 * sizeof (command_t));
franta-hg@31
    56
		memory.write<command_t>(CMD_INVALID);
franta-hg@31
    57
		memory.write<command_t>(CMD_INVALID);
franta-hg@31
    58
		memory.write<command_t>(CMD_SLEEP);
franta-hg@31
    59
		memory.write<sleep_t>(255);
franta-hg@31
    60
		memory.write<command_t>(CMD_COLOR);
franta-hg@31
    61
		memory.write<led_t>(23);
franta-hg@31
    62
		memory.write<color_t>(0);
franta-hg@31
    63
		memory.write<color_t>(200);
franta-hg@31
    64
		memory.write<color_t>(255);
franta-hg@31
    65
		memory.write<command_t>(CMD_INCREMENT);
franta-hg@31
    66
		memory.write<address_t>(0);
franta-hg@31
    67
		memory.write<command_t>(CMD_DECREMENT);
franta-hg@31
    68
		memory.write<address_t>(0);
franta-hg@31
    69
		memory.write<command_t>(CMD_GOTO_COMPARE);
franta-hg@31
    70
		memory.write<address_t>(0);
franta-hg@31
    71
		memory.write<address_t>(0 + sizeof (command_t) + sizeof (sleep_t));
franta-hg@33
    72
		memory.write<address_t>(memory.getAddress() - 3 * sizeof (address_t) - 2 * sizeof (command_t));
franta-hg@31
    73
		memory.write<address_t>(0);
franta-hg@33
    74
		memory.write<address_t>(memory.getAddress() + sizeof (address_t));
franta-hg@31
    75
		memory.write<command_t>(CMD_END);
franta-hg@33
    76
		memory.setAddressToBeginning();
franta-hg@2
    77
	}
franta-hg@0
    78
franta-hg@30
    79
	// Supported commands
franta-hg@30
    80
	// TODO: dynamic reconfiguration
franta-hg@21
    81
	unordered_map<command_t, shared_ptr < Command>> commands = {
franta-hg@21
    82
		{CMD_GOTO, make_shared<commands::Goto>()},
franta-hg@25
    83
		{CMD_GOTO_COMPARE, make_shared<commands::GotoCompare>()},
franta-hg@22
    84
		{CMD_SLEEP, make_shared<commands::Sleep>()},
franta-hg@23
    85
		{CMD_END, make_shared<commands::End>()},
franta-hg@24
    86
		{CMD_COLOR, make_shared<commands::Color>()},
franta-hg@30
    87
		{CMD_INCREMENT, make_shared <commands::IncrementDecrement>(true, 1)},
franta-hg@30
    88
		{CMD_DECREMENT, make_shared <commands::IncrementDecrement>(false, 1)},
franta-hg@21
    89
	};
franta-hg@27
    90
franta-hg@21
    91
franta-hg@30
    92
	// Main loop / interpreter:
franta-hg@33
    93
	while (memory.isNotOver()) {
franta-hg@33
    94
		wprintf(L"command %*d = ", 4, memory.getAddress());
franta-hg@31
    95
		command_t commandCode = memory.read<command_t>();
franta-hg@26
    96
		wprintf(L"%02X  ", commandCode);
franta-hg@0
    97
franta-hg@26
    98
		shared_ptr<Command> command = commands[commandCode];
franta-hg@21
    99
franta-hg@26
   100
		if (command) {
franta-hg@31
   101
			command->process(memory);
franta-hg@21
   102
		} else {
franta-hg@26
   103
			wprintf(L"invalid command\n");
franta-hg@21
   104
		}
franta-hg@0
   105
	}
franta-hg@0
   106
franta-hg@0
   107
	wprintf(L"all done\n");
franta-hg@0
   108
	return 0;
franta-hg@0
   109
}