c++/rgb-assembler/rgb-assembler.cpp
author František Kučera <franta-hg@frantovo.cz>
Thu, 21 Dec 2017 17:06:39 +0100
changeset 3 1dd99de3b178
parent 2 b5cf9c841efc
child 4 b589459129a9
permissions -rw-r--r--
GOTO
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@0
     7
using namespace std;
franta-hg@0
     8
franta-hg@0
     9
typedef uint16_t address_t;
franta-hg@0
    10
typedef uint8_t command_t;
franta-hg@0
    11
franta-hg@0
    12
const address_t MEMORY_SIZE = 1024;
franta-hg@0
    13
franta-hg@0
    14
const command_t CMD_GOTO = 100;
franta-hg@0
    15
const command_t CMD_SLEEP = 101;
franta-hg@0
    16
const command_t CMD_END = 102;
franta-hg@0
    17
franta-hg@1
    18
/**
franta-hg@1
    19
 * Reads the command on given position in memory and increments the index (position).
franta-hg@1
    20
 */
franta-hg@2
    21
command_t readCommand(command_t * memory, address_t &index) {
franta-hg@0
    22
	// TODO: map higher memory to static hardcoded areas or peripherals
franta-hg@3
    23
	command_t * value = reinterpret_cast<command_t*> (memory + index);
franta-hg@3
    24
	index += sizeof (*value);
franta-hg@3
    25
	return *value;
franta-hg@2
    26
}
franta-hg@2
    27
franta-hg@2
    28
address_t readAddress(command_t * memory, address_t &index) {
franta-hg@3
    29
	address_t * value = reinterpret_cast<address_t*> (memory + index);
franta-hg@3
    30
	index += sizeof (*value);
franta-hg@3
    31
	return *value;
franta-hg@0
    32
}
franta-hg@0
    33
franta-hg@1
    34
void writeMemoryChar(command_t(&memory)[MEMORY_SIZE], address_t &index, const int value) {
franta-hg@0
    35
	memory[index] = value;
franta-hg@0
    36
}
franta-hg@0
    37
franta-hg@2
    38
void writeCommand(command_t * memory, address_t &index, const command_t value) {
franta-hg@2
    39
	// command_t * m = (command_t*) (memory + index);
franta-hg@2
    40
	command_t * m = reinterpret_cast<command_t*> (memory + index);
franta-hg@2
    41
	*m = value;
franta-hg@2
    42
	index += sizeof (value);
franta-hg@0
    43
}
franta-hg@0
    44
franta-hg@0
    45
int main(int argc, char* argv[]) {
franta-hg@0
    46
franta-hg@0
    47
	setlocale(LC_ALL, "");
franta-hg@0
    48
franta-hg@2
    49
	command_t * memory = (command_t*) malloc(MEMORY_SIZE);
franta-hg@0
    50
franta-hg@2
    51
	{
franta-hg@2
    52
		address_t a = 0;
franta-hg@2
    53
		writeCommand(memory, a, CMD_SLEEP);
franta-hg@2
    54
		writeCommand(memory, a, CMD_SLEEP);
franta-hg@2
    55
		writeCommand(memory, a, CMD_SLEEP);
franta-hg@3
    56
		writeCommand(memory, a, CMD_GOTO);
franta-hg@3
    57
		writeCommand(memory, a, 8);
franta-hg@3
    58
		writeCommand(memory, a, 0);
franta-hg@3
    59
		writeCommand(memory, a, 1);
franta-hg@3
    60
		writeCommand(memory, a, 1);
franta-hg@3
    61
		writeCommand(memory, a, CMD_SLEEP);
franta-hg@2
    62
		writeCommand(memory, a, CMD_END);
franta-hg@2
    63
	}
franta-hg@0
    64
franta-hg@0
    65
franta-hg@2
    66
franta-hg@2
    67
	for (address_t i = 0; i < MEMORY_SIZE;) {
franta-hg@3
    68
		wprintf(L"command %d = ", i);
franta-hg@0
    69
		command_t ch = readCommand(memory, i);
franta-hg@3
    70
		wprintf(L"%d\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@2
    76
				i = readAddress(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@0
    81
				break;
franta-hg@0
    82
			case CMD_END:
franta-hg@0
    83
				command.append("END");
franta-hg@2
    84
				i = MEMORY_SIZE;
franta-hg@0
    85
				break;
franta-hg@0
    86
franta-hg@0
    87
		}
franta-hg@0
    88
franta-hg@0
    89
		if (!command.empty()) {
franta-hg@0
    90
			wprintf(L"\t%s\n", command.c_str());
franta-hg@0
    91
		}
franta-hg@0
    92
	}
franta-hg@0
    93
franta-hg@0
    94
franta-hg@0
    95
franta-hg@0
    96
	wprintf(L"all done\n");
franta-hg@0
    97
	return 0;
franta-hg@0
    98
}
franta-hg@0
    99