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