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
     1 #include <cstdlib>
     2 #include <iostream>
     3 #include <wchar.h>
     4 #include <locale.h>
     5 #include <cstring>
     6 
     7 using namespace std;
     8 
     9 typedef uint16_t address_t;
    10 typedef uint8_t command_t;
    11 
    12 const address_t MEMORY_SIZE = 1024;
    13 
    14 const command_t CMD_GOTO = 100;
    15 const command_t CMD_SLEEP = 101;
    16 const command_t CMD_END = 102;
    17 
    18 /**
    19  * Reads the command on given position in memory and increments the index (position).
    20  */
    21 command_t readCommand(command_t * memory, address_t &index) {
    22 	// TODO: map higher memory to static hardcoded areas or peripherals
    23 	command_t * value = reinterpret_cast<command_t*> (memory + index);
    24 	index += sizeof (*value);
    25 	return *value;
    26 }
    27 
    28 address_t readAddress(command_t * memory, address_t &index) {
    29 	address_t * value = reinterpret_cast<address_t*> (memory + index);
    30 	index += sizeof (*value);
    31 	return *value;
    32 }
    33 
    34 void writeMemoryChar(command_t(&memory)[MEMORY_SIZE], address_t &index, const int value) {
    35 	memory[index] = value;
    36 }
    37 
    38 void writeCommand(command_t * memory, address_t &index, const command_t value) {
    39 	// command_t * m = (command_t*) (memory + index);
    40 	command_t * m = reinterpret_cast<command_t*> (memory + index);
    41 	*m = value;
    42 	index += sizeof (value);
    43 }
    44 
    45 int main(int argc, char* argv[]) {
    46 
    47 	setlocale(LC_ALL, "");
    48 
    49 	command_t * memory = (command_t*) malloc(MEMORY_SIZE);
    50 
    51 	{
    52 		address_t a = 0;
    53 		writeCommand(memory, a, CMD_SLEEP);
    54 		writeCommand(memory, a, CMD_SLEEP);
    55 		writeCommand(memory, a, CMD_SLEEP);
    56 		writeCommand(memory, a, CMD_GOTO);
    57 		writeCommand(memory, a, 8);
    58 		writeCommand(memory, a, 0);
    59 		writeCommand(memory, a, 1);
    60 		writeCommand(memory, a, 1);
    61 		writeCommand(memory, a, CMD_SLEEP);
    62 		writeCommand(memory, a, CMD_END);
    63 	}
    64 
    65 
    66 
    67 	for (address_t i = 0; i < MEMORY_SIZE;) {
    68 		wprintf(L"command %d = ", i);
    69 		command_t ch = readCommand(memory, i);
    70 		wprintf(L"%d\n", ch);
    71 
    72 		string command;
    73 
    74 		switch (ch) {
    75 			case CMD_GOTO:
    76 				i = readAddress(memory, i);
    77 				command.append("GOTO ");
    78 				break;
    79 			case CMD_SLEEP:
    80 				command.append("SLEEP");
    81 				break;
    82 			case CMD_END:
    83 				command.append("END");
    84 				i = MEMORY_SIZE;
    85 				break;
    86 
    87 		}
    88 
    89 		if (!command.empty()) {
    90 			wprintf(L"\t%s\n", command.c_str());
    91 		}
    92 	}
    93 
    94 
    95 
    96 	wprintf(L"all done\n");
    97 	return 0;
    98 }
    99