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
     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 = 0x70;
    15 const command_t CMD_SLEEP = 0xFF;
    16 const command_t CMD_END = 0xED;
    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) / sizeof (command_t);
    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) / sizeof (command_t);
    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) / sizeof (command_t);
    43 }
    44 
    45 void writeAddress(command_t * memory, address_t &index, const address_t value) {
    46 	// command_t * m = (command_t*) (memory + index);
    47 	address_t * m = reinterpret_cast<address_t*> (memory + index);
    48 	*m = value;
    49 	index += sizeof (value) / sizeof (command_t);
    50 }
    51 
    52 int main(int argc, char* argv[]) {
    53 
    54 	setlocale(LC_ALL, "");
    55 
    56 	command_t * memory = (command_t*) malloc(MEMORY_SIZE);
    57 
    58 	{
    59 		address_t a = 0;
    60 		writeCommand(memory, a, CMD_SLEEP);
    61 		writeCommand(memory, a, CMD_SLEEP);
    62 		writeCommand(memory, a, CMD_SLEEP);
    63 		writeCommand(memory, a, CMD_GOTO);
    64 		writeAddress(memory, a, 8);
    65 		writeCommand(memory, a, 1);
    66 		writeCommand(memory, a, 1);
    67 		writeCommand(memory, a, CMD_SLEEP);
    68 		writeCommand(memory, a, CMD_END);
    69 	}
    70 
    71 
    72 
    73 	for (address_t i = 0; i < MEMORY_SIZE;) {
    74 		wprintf(L"command %d = ", i);
    75 		command_t ch = readCommand(memory, i);
    76 		wprintf(L"%X\n", ch);
    77 
    78 		string command;
    79 
    80 		switch (ch) {
    81 			case CMD_GOTO:
    82 				i = readAddress(memory, i);
    83 				command.append("GOTO ");
    84 				break;
    85 			case CMD_SLEEP:
    86 				command.append("SLEEP");
    87 				break;
    88 			case CMD_END:
    89 				command.append("END");
    90 				i = MEMORY_SIZE;
    91 				break;
    92 
    93 		}
    94 
    95 		if (!command.empty()) {
    96 			wprintf(L"\t%s\n", command.c_str());
    97 		}
    98 	}
    99 
   100 
   101 
   102 	wprintf(L"all done\n");
   103 	return 0;
   104 }
   105