c++/rgb-assembler/rgb-assembler.cpp
author František Kučera <franta-hg@frantovo.cz>
Fri, 22 Dec 2017 18:09:58 +0100
changeset 15 4b4fb847d01a
parent 14 2ab3d7282249
child 16 5c142e9c00e5
permissions -rw-r--r--
GOTO_COMPARE
     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 byte_t;
    14 typedef uint8_t command_t;
    15 typedef uint8_t sleep_t;
    16 typedef uint8_t color_t;
    17 typedef uint8_t led_t;
    18 
    19 // TODO: strong typedefs http://www.boost.org/doc/libs/1_61_0/libs/serialization/doc/strong_typedef.html ?
    20 
    21 const address_t MEMORY_SIZE = 1024;
    22 
    23 /**
    24  * Skip to the given address.
    25  * parameter: address_t
    26  */
    27 const command_t CMD_GOTO = 0x70;
    28 
    29 /**
    30  * Compare values on two addresses and go to one of given three addresses.
    31  * parameter: address_t a
    32  * parameter: address_t b
    33  * parameter: address_t GOTO target when a == b
    34  * parameter: address_t GOTO target when a > b
    35  * parameter: address_t GOTO target when a < b
    36  */
    37 const command_t CMD_GOTO_COMPARE = 0x80;
    38 
    39 /**
    40  * Wait given time in ms.
    41  * parameter: sleep_t
    42  */
    43 const command_t CMD_SLEEP = 0xFF;
    44 
    45 /**
    46  * Set RGB LED color.
    47  * parameter: led_t LED number
    48  * parameter: color_t red
    49  * parameter: color_t green
    50  * parameter: color_t blue
    51  */
    52 const command_t CMD_COLOR = 0xAA;
    53 
    54 /**
    55  * Stop program.
    56  */
    57 const command_t CMD_END = 0xED;
    58 
    59 /**
    60  * Increase value at given address
    61  * parameter: address_t
    62  */
    63 const command_t CMD_INCREMENT = 0x11;
    64 
    65 /**
    66  * Decrease value at given address
    67  * parameter: address_t
    68  */
    69 const command_t CMD_DECREMENT = 0x12;
    70 
    71 /**
    72  * Placeholder for unsupported command.
    73  * Just for testing.
    74  */
    75 const command_t CMD_INVALID = 0x1;
    76 
    77 // TODO: more commands, better numbers
    78 
    79 /**
    80  * Reads data on given position in memory and increments the index (position).
    81  */
    82 template<typename T> T read(byte_t * memory, address_t &index) {
    83 	// TODO: for addresses: map higher memory to static hardcoded areas or peripherals
    84 	// TODO: sizeof (byte_t) != 1 ?
    85 	T * value = reinterpret_cast<T*> (memory + index);
    86 	index += sizeof (*value) / sizeof (byte_t);
    87 	return *value;
    88 }
    89 
    90 /**
    91  * Writes data to given position in memory and increments the index (position).
    92  */
    93 template<typename T> void write(byte_t * memory, address_t &index, const T value) {
    94 	// TODO: sizeof (byte_t) != 1 ?
    95 	// T * m = (T*) (memory + index);
    96 	T * m = reinterpret_cast<T*> (memory + index);
    97 	*m = value;
    98 	index += sizeof (value) / sizeof (byte_t);
    99 }
   100 
   101 int main(int argc, char* argv[]) {
   102 
   103 	setlocale(LC_ALL, "");
   104 
   105 	byte_t * memory = (byte_t*) malloc(MEMORY_SIZE);
   106 
   107 	{
   108 		address_t a = 0;
   109 		write<command_t>(memory, a, CMD_SLEEP);
   110 		write<sleep_t>(memory, a, 255);
   111 		write<command_t>(memory, a, CMD_SLEEP);
   112 		write<sleep_t>(memory, a, 10);
   113 		write<command_t>(memory, a, CMD_SLEEP);
   114 		write<sleep_t>(memory, a, 255);
   115 		write<command_t>(memory, a, CMD_GOTO);
   116 		write<address_t>(memory, a, a + sizeof (address_t) + 2 * sizeof (command_t));
   117 		write<command_t>(memory, a, CMD_INVALID);
   118 		write<command_t>(memory, a, CMD_INVALID);
   119 		write<command_t>(memory, a, CMD_SLEEP);
   120 		write<sleep_t>(memory, a, 255);
   121 		write<command_t>(memory, a, CMD_COLOR);
   122 		write<led_t>(memory, a, 23);
   123 		write<color_t>(memory, a, 0);
   124 		write<color_t>(memory, a, 200);
   125 		write<color_t>(memory, a, 255);
   126 		write<command_t>(memory, a, CMD_INCREMENT);
   127 		write<address_t>(memory, a, 0);
   128 		write<command_t>(memory, a, CMD_DECREMENT);
   129 		write<address_t>(memory, a, 0);
   130 		write<command_t>(memory, a, CMD_GOTO_COMPARE);
   131 		write<address_t>(memory, a, 0);
   132 		write<address_t>(memory, a, 0 + sizeof (command_t) + sizeof (sleep_t));
   133 		write<address_t>(memory, a, a - 3 * sizeof (address_t) - 2 * sizeof (command_t));
   134 		write<address_t>(memory, a, 0);
   135 		write<address_t>(memory, a, a + sizeof (address_t));
   136 		write<command_t>(memory, a, CMD_END);
   137 	}
   138 
   139 	for (address_t i = 0; i < MEMORY_SIZE;) {
   140 		wprintf(L"command %*d = ", 4, i);
   141 		command_t ch = read<command_t>(memory, i);
   142 		wprintf(L"%02X  ", ch);
   143 
   144 		switch (ch) {
   145 			case CMD_GOTO:
   146 			{
   147 				i = read<address_t>(memory, i);
   148 				wprintf(L"GOTO %*d\n", 5, i);
   149 				break;
   150 			}
   151 			case CMD_SLEEP:
   152 			{
   153 				sleep_t delay = read<sleep_t>(memory, i);
   154 				wprintf(L"SLEEP %*d ms\n", 4, delay);
   155 				this_thread::sleep_for(chrono::milliseconds(delay));
   156 				break;
   157 			}
   158 			case CMD_COLOR:
   159 			{
   160 				led_t led = read<led_t>(memory, i);
   161 				color_t r = read<color_t>(memory, i);
   162 				color_t g = read<color_t>(memory, i);
   163 				color_t b = read<color_t>(memory, i);
   164 				wprintf(L"COLOR  %02X %02X %02X → %d\n", r, g, b, led);
   165 				break;
   166 			}
   167 			case CMD_INCREMENT:
   168 			case CMD_DECREMENT:
   169 			{
   170 				address_t address = read<address_t>(memory, i);
   171 				address_t address_r = address;
   172 				address_t address_w = address_r;
   173 				byte_t value = read<byte_t>(memory, address_r);
   174 				value = ch == CMD_INCREMENT ? value + 1 : value - 1;
   175 				write<byte_t>(memory, address_w, value);
   176 				wprintf(L"%sCREMENT %*d → %02X\n", (ch == CMD_INCREMENT ? "IN" : "DE"), 5, address, value);
   177 				break;
   178 			}
   179 			case CMD_GOTO_COMPARE:
   180 			{
   181 				address_t aa = read<address_t>(memory, i);
   182 				address_t ab = read<address_t>(memory, i);
   183 				address_t eq = read<address_t>(memory, i);
   184 				address_t gt = read<address_t>(memory, i);
   185 				address_t lt = read<address_t>(memory, i);
   186 
   187 				byte_t a = read<byte_t>(memory, aa);
   188 				byte_t b = read<byte_t>(memory, ab);
   189 
   190 				if (a == b) {
   191 					i = eq;
   192 				} else if (a > b) {
   193 					i = gt;
   194 				} else {
   195 					i = lt;
   196 				}
   197 				wprintf(L"GOTO COMPARE  a = %02X, b = %02X, eq = %d, gt = %d, lt = %d → %d\n", a, b, eq, gt, lt, i);
   198 				break;
   199 			}
   200 			case CMD_END:
   201 			{
   202 				wprintf(L"END\n");
   203 				i = MEMORY_SIZE;
   204 				break;
   205 			}
   206 			default:
   207 			{
   208 				wprintf(L"invalid command\n");
   209 			}
   210 		}
   211 
   212 	}
   213 
   214 	free(memory);
   215 	memory = nullptr;
   216 	wprintf(L"all done\n");
   217 	return 0;
   218 }
   219