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