c++/rgb-assembler/rgb-assembler.cpp
author František Kučera <franta-hg@frantovo.cz>
Fri, 22 Dec 2017 00:00:04 +0100
changeset 10 efed38f454a0
parent 9 157bc062efa7
child 11 1d1cc6552542
permissions -rw-r--r--
COLOR
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@0
     6
franta-hg@6
     7
#include <chrono>
franta-hg@6
     8
#include <thread>
franta-hg@6
     9
franta-hg@0
    10
using namespace std;
franta-hg@0
    11
franta-hg@0
    12
typedef uint16_t address_t;
franta-hg@0
    13
typedef uint8_t command_t;
franta-hg@6
    14
typedef uint8_t sleep_t;
franta-hg@10
    15
typedef uint8_t color_t;
franta-hg@0
    16
franta-hg@0
    17
const address_t MEMORY_SIZE = 1024;
franta-hg@0
    18
franta-hg@5
    19
const command_t CMD_GOTO = 0x70;
franta-hg@5
    20
const command_t CMD_SLEEP = 0xFF;
franta-hg@10
    21
const command_t CMD_COLOR = 0xAA;
franta-hg@5
    22
const command_t CMD_END = 0xED;
franta-hg@0
    23
franta-hg@1
    24
/**
franta-hg@8
    25
 * Reads data on given position in memory and increments the index (position).
franta-hg@1
    26
 */
franta-hg@8
    27
template<typename T> T read(command_t * memory, address_t &index) {
franta-hg@8
    28
	// TODO: for addresses: map higher memory to static hardcoded areas or peripherals
franta-hg@8
    29
	// TODO: sizeof (command_t) != 1 ?
franta-hg@8
    30
	T * value = reinterpret_cast<T*> (memory + index);
franta-hg@6
    31
	index += sizeof (*value) / sizeof (command_t);
franta-hg@6
    32
	return *value;
franta-hg@0
    33
}
franta-hg@0
    34
franta-hg@9
    35
/**
franta-hg@9
    36
 * Writes data to given position in memory and increments the index (position).
franta-hg@9
    37
 */
franta-hg@9
    38
template<typename T> void write(command_t * memory, address_t &index, const T value) {
franta-hg@9
    39
	// TODO: sizeof (command_t) != 1 ?
franta-hg@9
    40
	// T * m = (T*) (memory + index);
franta-hg@9
    41
	T * m = reinterpret_cast<T*> (memory + index);
franta-hg@6
    42
	*m = value;
franta-hg@6
    43
	index += sizeof (value) / sizeof (command_t);
franta-hg@6
    44
}
franta-hg@6
    45
franta-hg@0
    46
int main(int argc, char* argv[]) {
franta-hg@0
    47
franta-hg@0
    48
	setlocale(LC_ALL, "");
franta-hg@0
    49
franta-hg@2
    50
	command_t * memory = (command_t*) malloc(MEMORY_SIZE);
franta-hg@0
    51
franta-hg@2
    52
	{
franta-hg@2
    53
		address_t a = 0;
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_SLEEP);
franta-hg@9
    57
		write<sleep_t>(memory, a, 10);
franta-hg@9
    58
		write<command_t>(memory, a, CMD_SLEEP);
franta-hg@9
    59
		write<sleep_t>(memory, a, 255);
franta-hg@9
    60
		write<command_t>(memory, a, CMD_GOTO);
franta-hg@9
    61
		write<address_t>(memory, a, a + 4);
franta-hg@9
    62
		write<command_t>(memory, a, 1);
franta-hg@9
    63
		write<command_t>(memory, a, 1);
franta-hg@9
    64
		write<command_t>(memory, a, CMD_SLEEP);
franta-hg@9
    65
		write<sleep_t>(memory, a, 255);
franta-hg@10
    66
		write<command_t>(memory, a, CMD_COLOR);
franta-hg@10
    67
		write<color_t>(memory, a, 50);
franta-hg@10
    68
		write<color_t>(memory, a, 100);
franta-hg@10
    69
		write<color_t>(memory, a, 150);
franta-hg@9
    70
		write<command_t>(memory, a, CMD_END);
franta-hg@2
    71
	}
franta-hg@0
    72
franta-hg@2
    73
	for (address_t i = 0; i < MEMORY_SIZE;) {
franta-hg@3
    74
		wprintf(L"command %d = ", i);
franta-hg@8
    75
		command_t ch = read<command_t>(memory, i);
franta-hg@5
    76
		wprintf(L"%X\n", ch);
franta-hg@0
    77
franta-hg@0
    78
		string command;
franta-hg@10
    79
		
franta-hg@10
    80
		color_t r;
franta-hg@10
    81
		color_t g;
franta-hg@10
    82
		color_t b;
franta-hg@0
    83
franta-hg@0
    84
		switch (ch) {
franta-hg@0
    85
			case CMD_GOTO:
franta-hg@8
    86
				i = read<address_t>(memory, i);
franta-hg@2
    87
				command.append("GOTO ");
franta-hg@0
    88
				break;
franta-hg@0
    89
			case CMD_SLEEP:
franta-hg@0
    90
				command.append("SLEEP");
franta-hg@8
    91
				this_thread::sleep_for(chrono::milliseconds(read<sleep_t>(memory, i)));
franta-hg@0
    92
				break;
franta-hg@10
    93
			case CMD_COLOR:
franta-hg@10
    94
				command.append("COLOR");
franta-hg@10
    95
				r = read<color_t>(memory, i);
franta-hg@10
    96
				g = read<color_t>(memory, i);
franta-hg@10
    97
				b = read<color_t>(memory, i);
franta-hg@10
    98
				break;
franta-hg@0
    99
			case CMD_END:
franta-hg@0
   100
				command.append("END");
franta-hg@2
   101
				i = MEMORY_SIZE;
franta-hg@0
   102
				break;
franta-hg@0
   103
franta-hg@0
   104
		}
franta-hg@0
   105
franta-hg@0
   106
		if (!command.empty()) {
franta-hg@0
   107
			wprintf(L"\t%s\n", command.c_str());
franta-hg@0
   108
		}
franta-hg@0
   109
	}
franta-hg@0
   110
franta-hg@7
   111
	free(memory);
franta-hg@7
   112
	memory = nullptr;
franta-hg@0
   113
	wprintf(L"all done\n");
franta-hg@0
   114
	return 0;
franta-hg@0
   115
}
franta-hg@0
   116