c++/rgb-assembler/rgb-assembler.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 23 Dec 2017 23:34:21 +0100
changeset 21 ae4a9092e1a8
parent 20 b9ceffdcaf14
child 22 cb2055cdc2f0
permissions -rw-r--r--
GOTO in class
     1 #include <cstdlib>
     2 #include <iostream>
     3 #include <wchar.h>
     4 #include <locale.h>
     5 #include <cstring>
     6 #include <unordered_map>
     7 #include <chrono>
     8 #include <thread>
     9 
    10 #include "types.h"
    11 #include "memory.h"
    12 #include "Command.h"
    13 #include "commands/Goto.h"
    14 
    15 using namespace std;
    16 
    17 // TODO: strong typedefs http://www.boost.org/doc/libs/1_61_0/libs/serialization/doc/strong_typedef.html ?
    18 
    19 /**
    20  * Skip to the given address.
    21  * parameter: address_t
    22  */
    23 const command_t CMD_GOTO = 0x70;
    24 
    25 /**
    26  * Compare values on two addresses and go to one of given three addresses.
    27  * parameter: address_t a
    28  * parameter: address_t b
    29  * parameter: address_t GOTO target when a == b
    30  * parameter: address_t GOTO target when a > b
    31  * parameter: address_t GOTO target when a < b
    32  */
    33 const command_t CMD_GOTO_COMPARE = 0x80;
    34 
    35 /**
    36  * Wait given time in ms.
    37  * parameter: sleep_t
    38  */
    39 const command_t CMD_SLEEP = 0xFF;
    40 
    41 /**
    42  * Set RGB LED color.
    43  * parameter: led_t LED number
    44  * parameter: color_t red
    45  * parameter: color_t green
    46  * parameter: color_t blue
    47  */
    48 const command_t CMD_COLOR = 0xAA;
    49 
    50 /**
    51  * Stop program.
    52  */
    53 const command_t CMD_END = 0xED;
    54 
    55 /**
    56  * Increase value at given address
    57  * parameter: address_t
    58  */
    59 const command_t CMD_INCREMENT = 0x11;
    60 
    61 /**
    62  * Decrease value at given address
    63  * parameter: address_t
    64  */
    65 const command_t CMD_DECREMENT = 0x12;
    66 
    67 /**
    68  * Placeholder for unsupported command.
    69  * Just for testing.
    70  */
    71 const command_t CMD_INVALID = 0x1;
    72 
    73 // TODO: more commands, better numbers
    74 
    75 int main(int argc, char* argv[]) {
    76 
    77 	setlocale(LC_ALL, "");
    78 
    79 	octet_t * memory = (octet_t*) malloc(MEMORY_SIZE);
    80 
    81 	// Sample program / data:
    82 	// TODO: load bytes from file, stdin, serial port, network…
    83 	{
    84 		address_t a = 0;
    85 		write<command_t>(memory, a, CMD_SLEEP);
    86 		write<sleep_t>(memory, a, 255);
    87 		write<command_t>(memory, a, CMD_SLEEP);
    88 		write<sleep_t>(memory, a, 10);
    89 		write<command_t>(memory, a, CMD_SLEEP);
    90 		write<sleep_t>(memory, a, 255);
    91 		write<command_t>(memory, a, CMD_GOTO);
    92 		write<address_t>(memory, a, a + sizeof (address_t) + 2 * sizeof (command_t));
    93 		write<command_t>(memory, a, CMD_INVALID);
    94 		write<command_t>(memory, a, CMD_INVALID);
    95 		write<command_t>(memory, a, CMD_SLEEP);
    96 		write<sleep_t>(memory, a, 255);
    97 		write<command_t>(memory, a, CMD_COLOR);
    98 		write<led_t>(memory, a, 23);
    99 		write<color_t>(memory, a, 0);
   100 		write<color_t>(memory, a, 200);
   101 		write<color_t>(memory, a, 255);
   102 		write<command_t>(memory, a, CMD_INCREMENT);
   103 		write<address_t>(memory, a, 0);
   104 		write<command_t>(memory, a, CMD_DECREMENT);
   105 		write<address_t>(memory, a, 0);
   106 		write<command_t>(memory, a, CMD_GOTO_COMPARE);
   107 		write<address_t>(memory, a, 0);
   108 		write<address_t>(memory, a, 0 + sizeof (command_t) + sizeof (sleep_t));
   109 		write<address_t>(memory, a, a - 3 * sizeof (address_t) - 2 * sizeof (command_t));
   110 		write<address_t>(memory, a, 0);
   111 		write<address_t>(memory, a, a + sizeof (address_t));
   112 		write<command_t>(memory, a, CMD_END);
   113 	}
   114 
   115 	unordered_map<command_t, shared_ptr < Command>> commands = {
   116 		{CMD_GOTO, make_shared<commands::Goto>()},
   117 	};
   118 
   119 	for (address_t i = 0; i < MEMORY_SIZE;) {
   120 		wprintf(L"command %*d = ", 4, i);
   121 		command_t command = read<command_t>(memory, i);
   122 		wprintf(L"%02X  ", command);
   123 
   124 		shared_ptr<Command> cx = commands[command];
   125 
   126 		if (cx) {
   127 			cx->process(memory, i);
   128 			continue;
   129 		} else {
   130 			// TODO: wprintf(L"invalid command\n");
   131 		}
   132 
   133 		switch (command) {
   134 			case CMD_SLEEP:
   135 			{
   136 				sleep_t delay = read<sleep_t>(memory, i);
   137 				wprintf(L"SLEEP %*d ms\n", 4, delay);
   138 				this_thread::sleep_for(chrono::milliseconds(delay));
   139 				break;
   140 			}
   141 			case CMD_COLOR:
   142 			{
   143 				led_t led = read<led_t>(memory, i);
   144 				color_t r = read<color_t>(memory, i);
   145 				color_t g = read<color_t>(memory, i);
   146 				color_t b = read<color_t>(memory, i);
   147 				wprintf(L"COLOR  %02X %02X %02X → %d\n", r, g, b, led);
   148 				break;
   149 			}
   150 			case CMD_INCREMENT:
   151 			case CMD_DECREMENT:
   152 			{
   153 				address_t address = read<address_t>(memory, i);
   154 				address_t address_r = address;
   155 				address_t address_w = address_r;
   156 				octet_t value = read<octet_t>(memory, address_r);
   157 				value = command == CMD_INCREMENT ? value + 1 : value - 1;
   158 				write<octet_t>(memory, address_w, value);
   159 				wprintf(L"%sCREMENT %*d → %02X\n", (command == CMD_INCREMENT ? "IN" : "DE"), 5, address, value);
   160 				break;
   161 			}
   162 			case CMD_GOTO_COMPARE:
   163 			{
   164 				address_t aa = read<address_t>(memory, i);
   165 				address_t ab = read<address_t>(memory, i);
   166 				address_t eq = read<address_t>(memory, i);
   167 				address_t gt = read<address_t>(memory, i);
   168 				address_t lt = read<address_t>(memory, i);
   169 
   170 				octet_t a = read<octet_t>(memory, aa);
   171 				octet_t b = read<octet_t>(memory, ab);
   172 
   173 				if (a == b) i = eq;
   174 				else if (a > b) i = gt;
   175 				else i = lt;
   176 
   177 				wprintf(L"GOTO COMPARE  a = %02X, b = %02X, eq = %d, gt = %d, lt = %d → %d\n", a, b, eq, gt, lt, i);
   178 				break;
   179 			}
   180 			case CMD_END:
   181 			{
   182 				wprintf(L"END\n");
   183 				i = MEMORY_SIZE;
   184 				break;
   185 			}
   186 			default:
   187 			{
   188 				wprintf(L"invalid command\n");
   189 			}
   190 		}
   191 
   192 	}
   193 
   194 	free(memory);
   195 	memory = nullptr;
   196 	wprintf(L"all done\n");
   197 	return 0;
   198 }
   199