c++/rgb-assembler/rgb-assembler.cpp
author František Kučera <franta-hg@frantovo.cz>
Thu, 21 Dec 2017 17:22:55 +0100
changeset 5 e0a48e475a77
parent 4 b589459129a9
child 6 d55ac7dbc2e6
permissions -rw-r--r--
commands in hexadecimal
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@5
    14
const command_t CMD_GOTO = 0x70;
franta-hg@5
    15
const command_t CMD_SLEEP = 0xFF;
franta-hg@5
    16
const command_t CMD_END = 0xED;
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@4
    24
	index += sizeof (*value) / sizeof (command_t);
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@4
    30
	index += sizeof (*value) / sizeof (command_t);
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@4
    42
	index += sizeof (value) / sizeof (command_t);
franta-hg@4
    43
}
franta-hg@4
    44
franta-hg@4
    45
void writeAddress(command_t * memory, address_t &index, const address_t value) {
franta-hg@4
    46
	// command_t * m = (command_t*) (memory + index);
franta-hg@4
    47
	address_t * m = reinterpret_cast<address_t*> (memory + index);
franta-hg@4
    48
	*m = value;
franta-hg@4
    49
	index += sizeof (value) / sizeof (command_t);
franta-hg@0
    50
}
franta-hg@0
    51
franta-hg@0
    52
int main(int argc, char* argv[]) {
franta-hg@0
    53
franta-hg@0
    54
	setlocale(LC_ALL, "");
franta-hg@0
    55
franta-hg@2
    56
	command_t * memory = (command_t*) malloc(MEMORY_SIZE);
franta-hg@0
    57
franta-hg@2
    58
	{
franta-hg@2
    59
		address_t a = 0;
franta-hg@2
    60
		writeCommand(memory, a, CMD_SLEEP);
franta-hg@2
    61
		writeCommand(memory, a, CMD_SLEEP);
franta-hg@2
    62
		writeCommand(memory, a, CMD_SLEEP);
franta-hg@3
    63
		writeCommand(memory, a, CMD_GOTO);
franta-hg@4
    64
		writeAddress(memory, a, 8);
franta-hg@3
    65
		writeCommand(memory, a, 1);
franta-hg@3
    66
		writeCommand(memory, a, 1);
franta-hg@3
    67
		writeCommand(memory, a, CMD_SLEEP);
franta-hg@2
    68
		writeCommand(memory, a, CMD_END);
franta-hg@2
    69
	}
franta-hg@0
    70
franta-hg@0
    71
franta-hg@2
    72
franta-hg@2
    73
	for (address_t i = 0; i < MEMORY_SIZE;) {
franta-hg@3
    74
		wprintf(L"command %d = ", i);
franta-hg@0
    75
		command_t ch = readCommand(memory, i);
franta-hg@5
    76
		wprintf(L"%X\n", ch);
franta-hg@0
    77
franta-hg@0
    78
		string command;
franta-hg@0
    79
franta-hg@0
    80
		switch (ch) {
franta-hg@0
    81
			case CMD_GOTO:
franta-hg@2
    82
				i = readAddress(memory, i);
franta-hg@2
    83
				command.append("GOTO ");
franta-hg@0
    84
				break;
franta-hg@0
    85
			case CMD_SLEEP:
franta-hg@0
    86
				command.append("SLEEP");
franta-hg@0
    87
				break;
franta-hg@0
    88
			case CMD_END:
franta-hg@0
    89
				command.append("END");
franta-hg@2
    90
				i = MEMORY_SIZE;
franta-hg@0
    91
				break;
franta-hg@0
    92
franta-hg@0
    93
		}
franta-hg@0
    94
franta-hg@0
    95
		if (!command.empty()) {
franta-hg@0
    96
			wprintf(L"\t%s\n", command.c_str());
franta-hg@0
    97
		}
franta-hg@0
    98
	}
franta-hg@0
    99
franta-hg@0
   100
franta-hg@0
   101
franta-hg@0
   102
	wprintf(L"all done\n");
franta-hg@0
   103
	return 0;
franta-hg@0
   104
}
franta-hg@0
   105