c++/rgb-assembler/rgb-assembler.cpp
author František Kučera <franta-hg@frantovo.cz>
Thu, 21 Dec 2017 16:18:23 +0100
changeset 2 b5cf9c841efc
parent 1 ac1c16f0ebcd
child 3 1dd99de3b178
permissions -rw-r--r--
use malloc instead of an array (the memory contains sequence of various types so typed array makes no sense)
     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 = 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 = 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_END);
    57 	}
    58 
    59 
    60 
    61 	for (address_t i = 0; i < MEMORY_SIZE;) {
    62 		command_t ch = readCommand(memory, i);
    63 		wprintf(L"command %d = %d\n", i, ch);
    64 
    65 		string command;
    66 
    67 		switch (ch) {
    68 			case CMD_GOTO:
    69 				i = readAddress(memory, i);
    70 				command.append("GOTO ");
    71 				break;
    72 			case CMD_SLEEP:
    73 				command.append("SLEEP");
    74 				break;
    75 			case CMD_END:
    76 				command.append("END");
    77 				i = MEMORY_SIZE;
    78 				break;
    79 
    80 		}
    81 
    82 		if (!command.empty()) {
    83 			wprintf(L"\t%s\n", command.c_str());
    84 		}
    85 	}
    86 
    87 
    88 
    89 	wprintf(L"all done\n");
    90 	return 0;
    91 }
    92