c++/rgb-assembler/rgb-assembler.cpp
author František Kučera <franta-hg@frantovo.cz>
Thu, 21 Dec 2017 17:52:30 +0100
changeset 6 d55ac7dbc2e6
parent 5 e0a48e475a77
child 7 379c490a9831
permissions -rw-r--r--
SLEEP
     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 the command on given position in memory and increments the index (position).
    24  */
    25 command_t readCommand(command_t * memory, address_t &index) {
    26 	// TODO: map higher memory to static hardcoded areas or peripherals
    27 	command_t * value = reinterpret_cast<command_t*> (memory + index);
    28 	index += sizeof (*value) / sizeof (command_t);
    29 	return *value;
    30 }
    31 
    32 address_t readAddress(command_t * memory, address_t &index) {
    33 	address_t * value = reinterpret_cast<address_t*> (memory + index);
    34 	index += sizeof (*value) / sizeof (command_t);
    35 	return *value;
    36 }
    37 
    38 sleep_t readSleep(command_t * memory, address_t &index) {
    39 	// TODO: map higher memory to static hardcoded areas or peripherals
    40 	sleep_t * value = reinterpret_cast<sleep_t*> (memory + index);
    41 	index += sizeof (*value) / sizeof (command_t);
    42 	return *value;
    43 }
    44 
    45 void writeCommand(command_t * memory, address_t &index, const command_t value) {
    46 	// command_t * m = (command_t*) (memory + index);
    47 	command_t * m = reinterpret_cast<command_t*> (memory + index);
    48 	*m = value;
    49 	index += sizeof (value) / sizeof (command_t);
    50 }
    51 
    52 void writeAddress(command_t * memory, address_t &index, const address_t value) {
    53 	// command_t * m = (command_t*) (memory + index);
    54 	address_t * m = reinterpret_cast<address_t*> (memory + index);
    55 	*m = value;
    56 	index += sizeof (value) / sizeof (command_t);
    57 }
    58 
    59 void writeSleep(command_t * memory, address_t &index, const sleep_t value) {
    60 	// command_t * m = (command_t*) (memory + index);
    61 	sleep_t * m = reinterpret_cast<sleep_t*> (memory + index);
    62 	*m = value;
    63 	index += sizeof (value) / sizeof (command_t);
    64 }
    65 
    66 int main(int argc, char* argv[]) {
    67 
    68 	setlocale(LC_ALL, "");
    69 
    70 	command_t * memory = (command_t*) malloc(MEMORY_SIZE);
    71 
    72 	{
    73 		address_t a = 0;
    74 		writeCommand(memory, a, CMD_SLEEP);
    75 		writeSleep(memory, a, 255);
    76 		writeCommand(memory, a, CMD_SLEEP);
    77 		writeSleep(memory, a, 10);
    78 		writeCommand(memory, a, CMD_SLEEP);
    79 		writeSleep(memory, a, 255);
    80 		writeCommand(memory, a, CMD_GOTO);
    81 		writeAddress(memory, a, a+4);
    82 		writeCommand(memory, a, 1);
    83 		writeCommand(memory, a, 1);
    84 		writeCommand(memory, a, CMD_SLEEP);
    85 		writeSleep(memory, a, 255);
    86 		writeCommand(memory, a, CMD_END);
    87 	}
    88 
    89 
    90 
    91 	for (address_t i = 0; i < MEMORY_SIZE;) {
    92 		wprintf(L"command %d = ", i);
    93 		command_t ch = readCommand(memory, i);
    94 		wprintf(L"%X\n", ch);
    95 
    96 		string command;
    97 
    98 		switch (ch) {
    99 			case CMD_GOTO:
   100 				i = readAddress(memory, i);
   101 				command.append("GOTO ");
   102 				break;
   103 			case CMD_SLEEP:
   104 				command.append("SLEEP");
   105 				this_thread::sleep_for(chrono::milliseconds(readSleep(memory, i)));
   106 				break;
   107 			case CMD_END:
   108 				command.append("END");
   109 				i = MEMORY_SIZE;
   110 				break;
   111 
   112 		}
   113 
   114 		if (!command.empty()) {
   115 			wprintf(L"\t%s\n", command.c_str());
   116 		}
   117 	}
   118 
   119 
   120 
   121 	wprintf(L"all done\n");
   122 	return 0;
   123 }
   124