c++/rgb-assembler/rgb-assembler.cpp
author František Kučera <franta-hg@frantovo.cz>
Thu, 21 Dec 2017 21:49:15 +0100
changeset 8 d4b2ec9ef8bc
parent 7 379c490a9831
child 9 157bc062efa7
permissions -rw-r--r--
template / generic read() function
     1 #include <cstdlib>
     2 #include <iostream>
     3 #include <wchar.h>
     4 #include <locale.h>
     5 #include <cstring>
     6 
     7 #include <chrono>
     8 #include <thread>
     9 
    10 using namespace std;
    11 
    12 typedef uint16_t address_t;
    13 typedef uint8_t command_t;
    14 typedef uint8_t sleep_t;
    15 
    16 const address_t MEMORY_SIZE = 1024;
    17 
    18 const command_t CMD_GOTO = 0x70;
    19 const command_t CMD_SLEEP = 0xFF;
    20 const command_t CMD_END = 0xED;
    21 
    22 /**
    23  * Reads data on given position in memory and increments the index (position).
    24  */
    25 template<typename T> T read(command_t * memory, address_t &index) {
    26 	// TODO: for addresses: map higher memory to static hardcoded areas or peripherals
    27 	// TODO: sizeof (command_t) != 1 ?
    28 	T * value = reinterpret_cast<T*> (memory + index);
    29 	index += sizeof (*value) / sizeof (command_t);
    30 	return *value;
    31 }
    32 
    33 void writeCommand(command_t * memory, address_t &index, const command_t value) {
    34 	// command_t * m = (command_t*) (memory + index);
    35 	command_t * m = reinterpret_cast<command_t*> (memory + index);
    36 	*m = value;
    37 	index += sizeof (value) / sizeof (command_t);
    38 }
    39 
    40 void writeAddress(command_t * memory, address_t &index, const address_t value) {
    41 	// command_t * m = (command_t*) (memory + index);
    42 	address_t * m = reinterpret_cast<address_t*> (memory + index);
    43 	*m = value;
    44 	index += sizeof (value) / sizeof (command_t);
    45 }
    46 
    47 void writeSleep(command_t * memory, address_t &index, const sleep_t value) {
    48 	// command_t * m = (command_t*) (memory + index);
    49 	sleep_t * m = reinterpret_cast<sleep_t*> (memory + index);
    50 	*m = value;
    51 	index += sizeof (value) / sizeof (command_t);
    52 }
    53 
    54 int main(int argc, char* argv[]) {
    55 
    56 	setlocale(LC_ALL, "");
    57 
    58 	command_t * memory = (command_t*) malloc(MEMORY_SIZE);
    59 
    60 	{
    61 		address_t a = 0;
    62 		writeCommand(memory, a, CMD_SLEEP);
    63 		writeSleep(memory, a, 255);
    64 		writeCommand(memory, a, CMD_SLEEP);
    65 		writeSleep(memory, a, 10);
    66 		writeCommand(memory, a, CMD_SLEEP);
    67 		writeSleep(memory, a, 255);
    68 		writeCommand(memory, a, CMD_GOTO);
    69 		writeAddress(memory, a, a + 4);
    70 		writeCommand(memory, a, 1);
    71 		writeCommand(memory, a, 1);
    72 		writeCommand(memory, a, CMD_SLEEP);
    73 		writeSleep(memory, a, 255);
    74 		writeCommand(memory, a, CMD_END);
    75 	}
    76 
    77 
    78 
    79 	for (address_t i = 0; i < MEMORY_SIZE;) {
    80 		wprintf(L"command %d = ", i);
    81 		command_t ch = read<command_t>(memory, i);
    82 		wprintf(L"%X\n", ch);
    83 
    84 		string command;
    85 
    86 		switch (ch) {
    87 			case CMD_GOTO:
    88 				i = read<address_t>(memory, i);
    89 				command.append("GOTO ");
    90 				break;
    91 			case CMD_SLEEP:
    92 				command.append("SLEEP");
    93 				this_thread::sleep_for(chrono::milliseconds(read<sleep_t>(memory, i)));
    94 				break;
    95 			case CMD_END:
    96 				command.append("END");
    97 				i = MEMORY_SIZE;
    98 				break;
    99 
   100 		}
   101 
   102 		if (!command.empty()) {
   103 			wprintf(L"\t%s\n", command.c_str());
   104 		}
   105 	}
   106 
   107 
   108 
   109 	free(memory);
   110 	memory = nullptr;
   111 	wprintf(L"all done\n");
   112 	return 0;
   113 }
   114