c++/rgb-assembler/rgb-assembler.cpp
author František Kučera <franta-hg@frantovo.cz>
Thu, 21 Dec 2017 15:17:37 +0100
changeset 1 ac1c16f0ebcd
parent 0 ee60ce4d8af5
child 2 b5cf9c841efc
permissions -rw-r--r--
increment index in th read function
     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)[MEMORY_SIZE], address_t &index) {
    22 	// TODO: map higher memory to static hardcoded areas or peripherals
    23 	return memory[index++];
    24 }
    25 
    26 void writeMemoryChar(command_t(&memory)[MEMORY_SIZE], address_t &index, const int value) {
    27 	memory[index] = value;
    28 }
    29 
    30 void writeMemoryChar(command_t* memory[], address_t &index, const command_t value) {
    31 	*memory[index] = value;
    32 }
    33 
    34 int main(int argc, char* argv[]) {
    35 
    36 	setlocale(LC_ALL, "");
    37 
    38 
    39 	command_t memory[MEMORY_SIZE] = {
    40 		CMD_SLEEP,
    41 		CMD_SLEEP,
    42 		CMD_SLEEP,
    43 		CMD_END,
    44 	};
    45 	address_t memorySize = sizeof (memory) / sizeof (*memory);
    46 
    47 
    48 	for (address_t i = 0; i < memorySize;) {
    49 		command_t ch = readCommand(memory, i);
    50 		wprintf(L"command %d = %d\n", i, ch);
    51 
    52 		string command;
    53 
    54 		switch (ch) {
    55 			case CMD_GOTO:
    56 				command.append("GOTO");
    57 				break;
    58 			case CMD_SLEEP:
    59 				command.append("SLEEP");
    60 				break;
    61 			case CMD_END:
    62 				command.append("END");
    63 				i = memorySize;
    64 				break;
    65 
    66 		}
    67 
    68 		if (!command.empty()) {
    69 			wprintf(L"\t%s\n", command.c_str());
    70 		}
    71 	}
    72 
    73 
    74 
    75 	wprintf(L"all done\n");
    76 	return 0;
    77 }
    78