c++/rgb-assembler/rgb-assembler.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 23 Dec 2017 23:50:44 +0100
changeset 25 5ce09de7f9b7
parent 24 e24883b00180
child 26 47ee91579133
permissions -rw-r--r--
GOTO_COMPARE in class
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@20
    11
#include "commands/Goto.h"
franta-hg@25
    12
#include "commands/GotoCompare.h"
franta-hg@22
    13
#include "commands/Sleep.h"
franta-hg@23
    14
#include "commands/End.h"
franta-hg@24
    15
#include "commands/Color.h"
franta-hg@19
    16
franta-hg@0
    17
using namespace std;
franta-hg@0
    18
franta-hg@12
    19
// TODO: strong typedefs http://www.boost.org/doc/libs/1_61_0/libs/serialization/doc/strong_typedef.html ?
franta-hg@12
    20
franta-hg@13
    21
/**
franta-hg@13
    22
 * Skip to the given address.
franta-hg@13
    23
 * parameter: address_t
franta-hg@13
    24
 */
franta-hg@5
    25
const command_t CMD_GOTO = 0x70;
franta-hg@13
    26
franta-hg@13
    27
/**
franta-hg@15
    28
 * Compare values on two addresses and go to one of given three addresses.
franta-hg@15
    29
 * parameter: address_t a
franta-hg@15
    30
 * parameter: address_t b
franta-hg@15
    31
 * parameter: address_t GOTO target when a == b
franta-hg@15
    32
 * parameter: address_t GOTO target when a > b
franta-hg@15
    33
 * parameter: address_t GOTO target when a < b
franta-hg@15
    34
 */
franta-hg@15
    35
const command_t CMD_GOTO_COMPARE = 0x80;
franta-hg@15
    36
franta-hg@15
    37
/**
franta-hg@13
    38
 * Wait given time in ms.
franta-hg@13
    39
 * parameter: sleep_t
franta-hg@13
    40
 */
franta-hg@5
    41
const command_t CMD_SLEEP = 0xFF;
franta-hg@13
    42
franta-hg@13
    43
/**
franta-hg@13
    44
 * Set RGB LED color.
franta-hg@13
    45
 * parameter: led_t LED number
franta-hg@13
    46
 * parameter: color_t red
franta-hg@13
    47
 * parameter: color_t green
franta-hg@13
    48
 * parameter: color_t blue
franta-hg@13
    49
 */
franta-hg@10
    50
const command_t CMD_COLOR = 0xAA;
franta-hg@13
    51
franta-hg@13
    52
/**
franta-hg@13
    53
 * Stop program.
franta-hg@13
    54
 */
franta-hg@5
    55
const command_t CMD_END = 0xED;
franta-hg@13
    56
franta-hg@13
    57
/**
franta-hg@14
    58
 * Increase value at given address
franta-hg@14
    59
 * parameter: address_t
franta-hg@14
    60
 */
franta-hg@14
    61
const command_t CMD_INCREMENT = 0x11;
franta-hg@14
    62
franta-hg@14
    63
/**
franta-hg@14
    64
 * Decrease value at given address
franta-hg@14
    65
 * parameter: address_t
franta-hg@14
    66
 */
franta-hg@14
    67
const command_t CMD_DECREMENT = 0x12;
franta-hg@14
    68
franta-hg@14
    69
/**
franta-hg@13
    70
 * Placeholder for unsupported command.
franta-hg@13
    71
 * Just for testing.
franta-hg@13
    72
 */
franta-hg@13
    73
const command_t CMD_INVALID = 0x1;
franta-hg@0
    74
franta-hg@15
    75
// TODO: more commands, better numbers
franta-hg@15
    76
franta-hg@0
    77
int main(int argc, char* argv[]) {
franta-hg@0
    78
franta-hg@0
    79
	setlocale(LC_ALL, "");
franta-hg@0
    80
franta-hg@17
    81
	octet_t * memory = (octet_t*) malloc(MEMORY_SIZE);
franta-hg@0
    82
franta-hg@16
    83
	// Sample program / data:
franta-hg@16
    84
	// TODO: load bytes from file, stdin, serial port, network…
franta-hg@2
    85
	{
franta-hg@2
    86
		address_t a = 0;
franta-hg@9
    87
		write<command_t>(memory, a, CMD_SLEEP);
franta-hg@9
    88
		write<sleep_t>(memory, a, 255);
franta-hg@9
    89
		write<command_t>(memory, a, CMD_SLEEP);
franta-hg@9
    90
		write<sleep_t>(memory, a, 10);
franta-hg@9
    91
		write<command_t>(memory, a, CMD_SLEEP);
franta-hg@9
    92
		write<sleep_t>(memory, a, 255);
franta-hg@9
    93
		write<command_t>(memory, a, CMD_GOTO);
franta-hg@15
    94
		write<address_t>(memory, a, a + sizeof (address_t) + 2 * sizeof (command_t));
franta-hg@11
    95
		write<command_t>(memory, a, CMD_INVALID);
franta-hg@11
    96
		write<command_t>(memory, a, CMD_INVALID);
franta-hg@9
    97
		write<command_t>(memory, a, CMD_SLEEP);
franta-hg@9
    98
		write<sleep_t>(memory, a, 255);
franta-hg@10
    99
		write<command_t>(memory, a, CMD_COLOR);
franta-hg@13
   100
		write<led_t>(memory, a, 23);
franta-hg@11
   101
		write<color_t>(memory, a, 0);
franta-hg@11
   102
		write<color_t>(memory, a, 200);
franta-hg@11
   103
		write<color_t>(memory, a, 255);
franta-hg@14
   104
		write<command_t>(memory, a, CMD_INCREMENT);
franta-hg@14
   105
		write<address_t>(memory, a, 0);
franta-hg@14
   106
		write<command_t>(memory, a, CMD_DECREMENT);
franta-hg@14
   107
		write<address_t>(memory, a, 0);
franta-hg@15
   108
		write<command_t>(memory, a, CMD_GOTO_COMPARE);
franta-hg@15
   109
		write<address_t>(memory, a, 0);
franta-hg@15
   110
		write<address_t>(memory, a, 0 + sizeof (command_t) + sizeof (sleep_t));
franta-hg@15
   111
		write<address_t>(memory, a, a - 3 * sizeof (address_t) - 2 * sizeof (command_t));
franta-hg@15
   112
		write<address_t>(memory, a, 0);
franta-hg@15
   113
		write<address_t>(memory, a, a + sizeof (address_t));
franta-hg@9
   114
		write<command_t>(memory, a, CMD_END);
franta-hg@2
   115
	}
franta-hg@0
   116
franta-hg@21
   117
	unordered_map<command_t, shared_ptr < Command>> commands = {
franta-hg@21
   118
		{CMD_GOTO, make_shared<commands::Goto>()},
franta-hg@25
   119
		{CMD_GOTO_COMPARE, make_shared<commands::GotoCompare>()},
franta-hg@22
   120
		{CMD_SLEEP, make_shared<commands::Sleep>()},
franta-hg@23
   121
		{CMD_END, make_shared<commands::End>()},
franta-hg@24
   122
		{CMD_COLOR, make_shared<commands::Color>()},
franta-hg@21
   123
	};
franta-hg@21
   124
franta-hg@2
   125
	for (address_t i = 0; i < MEMORY_SIZE;) {
franta-hg@11
   126
		wprintf(L"command %*d = ", 4, i);
franta-hg@16
   127
		command_t command = read<command_t>(memory, i);
franta-hg@16
   128
		wprintf(L"%02X  ", command);
franta-hg@0
   129
franta-hg@21
   130
		shared_ptr<Command> cx = commands[command];
franta-hg@21
   131
franta-hg@21
   132
		if (cx) {
franta-hg@21
   133
			cx->process(memory, i);
franta-hg@21
   134
			continue;
franta-hg@21
   135
		} else {
franta-hg@21
   136
			// TODO: wprintf(L"invalid command\n");
franta-hg@21
   137
		}
franta-hg@21
   138
franta-hg@16
   139
		switch (command) {
franta-hg@14
   140
			case CMD_INCREMENT:
franta-hg@14
   141
			case CMD_DECREMENT:
franta-hg@14
   142
			{
franta-hg@14
   143
				address_t address = read<address_t>(memory, i);
franta-hg@14
   144
				address_t address_r = address;
franta-hg@14
   145
				address_t address_w = address_r;
franta-hg@17
   146
				octet_t value = read<octet_t>(memory, address_r);
franta-hg@16
   147
				value = command == CMD_INCREMENT ? value + 1 : value - 1;
franta-hg@17
   148
				write<octet_t>(memory, address_w, value);
franta-hg@16
   149
				wprintf(L"%sCREMENT %*d → %02X\n", (command == CMD_INCREMENT ? "IN" : "DE"), 5, address, value);
franta-hg@14
   150
				break;
franta-hg@14
   151
			}
franta-hg@11
   152
			default:
franta-hg@12
   153
			{
franta-hg@11
   154
				wprintf(L"invalid command\n");
franta-hg@12
   155
			}
franta-hg@0
   156
		}
franta-hg@0
   157
franta-hg@0
   158
	}
franta-hg@0
   159
franta-hg@7
   160
	free(memory);
franta-hg@7
   161
	memory = nullptr;
franta-hg@0
   162
	wprintf(L"all done\n");
franta-hg@0
   163
	return 0;
franta-hg@0
   164
}
franta-hg@0
   165