c++/rgb-assembler/rgb-assembler.cpp
author František Kučera <franta-hg@frantovo.cz>
Thu, 21 Dec 2017 21:55:26 +0100
changeset 9 157bc062efa7
parent 8 d4b2ec9ef8bc
child 10 efed38f454a0
permissions -rw-r--r--
template / generic write() 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 /**
    34  * Writes data to given position in memory and increments the index (position).
    35  */
    36 template<typename T> void write(command_t * memory, address_t &index, const T value) {
    37 	// TODO: sizeof (command_t) != 1 ?
    38 	// T * m = (T*) (memory + index);
    39 	T * m = reinterpret_cast<T*> (memory + index);
    40 	*m = value;
    41 	index += sizeof (value) / sizeof (command_t);
    42 }
    43 
    44 int main(int argc, char* argv[]) {
    45 
    46 	setlocale(LC_ALL, "");
    47 
    48 	command_t * memory = (command_t*) malloc(MEMORY_SIZE);
    49 
    50 	{
    51 		address_t a = 0;
    52 		write<command_t>(memory, a, CMD_SLEEP);
    53 		write<sleep_t>(memory, a, 255);
    54 		write<command_t>(memory, a, CMD_SLEEP);
    55 		write<sleep_t>(memory, a, 10);
    56 		write<command_t>(memory, a, CMD_SLEEP);
    57 		write<sleep_t>(memory, a, 255);
    58 		write<command_t>(memory, a, CMD_GOTO);
    59 		write<address_t>(memory, a, a + 4);
    60 		write<command_t>(memory, a, 1);
    61 		write<command_t>(memory, a, 1);
    62 		write<command_t>(memory, a, CMD_SLEEP);
    63 		write<sleep_t>(memory, a, 255);
    64 		write<command_t>(memory, a, CMD_END);
    65 	}
    66 
    67 	for (address_t i = 0; i < MEMORY_SIZE;) {
    68 		wprintf(L"command %d = ", i);
    69 		command_t ch = read<command_t>(memory, i);
    70 		wprintf(L"%X\n", ch);
    71 
    72 		string command;
    73 
    74 		switch (ch) {
    75 			case CMD_GOTO:
    76 				i = read<address_t>(memory, i);
    77 				command.append("GOTO ");
    78 				break;
    79 			case CMD_SLEEP:
    80 				command.append("SLEEP");
    81 				this_thread::sleep_for(chrono::milliseconds(read<sleep_t>(memory, i)));
    82 				break;
    83 			case CMD_END:
    84 				command.append("END");
    85 				i = MEMORY_SIZE;
    86 				break;
    87 
    88 		}
    89 
    90 		if (!command.empty()) {
    91 			wprintf(L"\t%s\n", command.c_str());
    92 		}
    93 	}
    94 
    95 	free(memory);
    96 	memory = nullptr;
    97 	wprintf(L"all done\n");
    98 	return 0;
    99 }
   100