c++/rgb-assembler/rgb-assembler.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 23 Dec 2017 17:52:32 +0100
changeset 18 4975c24cc361
parent 17 8f0a5552db78
child 19 17785b69430d
permissions -rw-r--r--
check memory bounds in read() and write()
     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 octet_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 template<typename T> T logMemoryError(const address_t &index) {
    80 	wprintf(L"memory error: index = %d, sizeof(T) = %d, MEMORY_SIZE = %d\n", index, sizeof (T), MEMORY_SIZE);
    81 	// TODO: return error value or throw exception
    82 	return T();
    83 }
    84 
    85 /**
    86  * Reads data on given position in memory and increments the index (position).
    87  * 
    88  * @param memory array of bytes / octets
    89  * @param index offset in same units as memory type
    90  * @return value found at given position
    91  */
    92 template<typename T> T read(octet_t * memory, address_t &index) {
    93 	// TODO: map higher memory to static hardcoded areas or peripherals
    94 	if (index + sizeof (T) <= MEMORY_SIZE) {
    95 		T * value = reinterpret_cast<T*> (memory + index);
    96 		index += sizeof (T);
    97 		return *value;
    98 	} else {
    99 		return logMemoryError<T>(index);
   100 	}
   101 }
   102 
   103 /**
   104  * Writes data to given position in memory and increments the index (position).
   105  * @param memory array of bytes / octets
   106  * @param index offset in same units as memory type
   107  * @param value value to be written at given position
   108  */
   109 template<typename T> T write(octet_t * memory, address_t &index, const T value) {
   110 	if (index + sizeof (T) <= MEMORY_SIZE) {
   111 		T * m = reinterpret_cast<T*> (memory + index);
   112 		*m = value;
   113 		index += sizeof (value);
   114 	} else {
   115 		return logMemoryError<T>(index);
   116 	}
   117 }
   118 
   119 int main(int argc, char* argv[]) {
   120 
   121 	setlocale(LC_ALL, "");
   122 
   123 	octet_t * memory = (octet_t*) malloc(MEMORY_SIZE);
   124 
   125 	// Sample program / data:
   126 	// TODO: load bytes from file, stdin, serial port, network…
   127 	{
   128 		address_t a = 0;
   129 		write<command_t>(memory, a, CMD_SLEEP);
   130 		write<sleep_t>(memory, a, 255);
   131 		write<command_t>(memory, a, CMD_SLEEP);
   132 		write<sleep_t>(memory, a, 10);
   133 		write<command_t>(memory, a, CMD_SLEEP);
   134 		write<sleep_t>(memory, a, 255);
   135 		write<command_t>(memory, a, CMD_GOTO);
   136 		write<address_t>(memory, a, a + sizeof (address_t) + 2 * sizeof (command_t));
   137 		write<command_t>(memory, a, CMD_INVALID);
   138 		write<command_t>(memory, a, CMD_INVALID);
   139 		write<command_t>(memory, a, CMD_SLEEP);
   140 		write<sleep_t>(memory, a, 255);
   141 		write<command_t>(memory, a, CMD_COLOR);
   142 		write<led_t>(memory, a, 23);
   143 		write<color_t>(memory, a, 0);
   144 		write<color_t>(memory, a, 200);
   145 		write<color_t>(memory, a, 255);
   146 		write<command_t>(memory, a, CMD_INCREMENT);
   147 		write<address_t>(memory, a, 0);
   148 		write<command_t>(memory, a, CMD_DECREMENT);
   149 		write<address_t>(memory, a, 0);
   150 		write<command_t>(memory, a, CMD_GOTO_COMPARE);
   151 		write<address_t>(memory, a, 0);
   152 		write<address_t>(memory, a, 0 + sizeof (command_t) + sizeof (sleep_t));
   153 		write<address_t>(memory, a, a - 3 * sizeof (address_t) - 2 * sizeof (command_t));
   154 		write<address_t>(memory, a, 0);
   155 		write<address_t>(memory, a, a + sizeof (address_t));
   156 		write<command_t>(memory, a, CMD_END);
   157 	}
   158 
   159 	for (address_t i = 0; i < MEMORY_SIZE;) {
   160 		wprintf(L"command %*d = ", 4, i);
   161 		command_t command = read<command_t>(memory, i);
   162 		wprintf(L"%02X  ", command);
   163 
   164 		switch (command) {
   165 			case CMD_GOTO:
   166 			{
   167 				i = read<address_t>(memory, i);
   168 				wprintf(L"GOTO %*d\n", 5, i);
   169 				break;
   170 			}
   171 			case CMD_SLEEP:
   172 			{
   173 				sleep_t delay = read<sleep_t>(memory, i);
   174 				wprintf(L"SLEEP %*d ms\n", 4, delay);
   175 				this_thread::sleep_for(chrono::milliseconds(delay));
   176 				break;
   177 			}
   178 			case CMD_COLOR:
   179 			{
   180 				led_t led = read<led_t>(memory, i);
   181 				color_t r = read<color_t>(memory, i);
   182 				color_t g = read<color_t>(memory, i);
   183 				color_t b = read<color_t>(memory, i);
   184 				wprintf(L"COLOR  %02X %02X %02X → %d\n", r, g, b, led);
   185 				break;
   186 			}
   187 			case CMD_INCREMENT:
   188 			case CMD_DECREMENT:
   189 			{
   190 				address_t address = read<address_t>(memory, i);
   191 				address_t address_r = address;
   192 				address_t address_w = address_r;
   193 				octet_t value = read<octet_t>(memory, address_r);
   194 				value = command == CMD_INCREMENT ? value + 1 : value - 1;
   195 				write<octet_t>(memory, address_w, value);
   196 				wprintf(L"%sCREMENT %*d → %02X\n", (command == CMD_INCREMENT ? "IN" : "DE"), 5, address, value);
   197 				break;
   198 			}
   199 			case CMD_GOTO_COMPARE:
   200 			{
   201 				address_t aa = read<address_t>(memory, i);
   202 				address_t ab = read<address_t>(memory, i);
   203 				address_t eq = read<address_t>(memory, i);
   204 				address_t gt = read<address_t>(memory, i);
   205 				address_t lt = read<address_t>(memory, i);
   206 
   207 				octet_t a = read<octet_t>(memory, aa);
   208 				octet_t b = read<octet_t>(memory, ab);
   209 
   210 				if (a == b) i = eq;
   211 				else if (a > b) i = gt;
   212 				else i = lt;
   213 
   214 				wprintf(L"GOTO COMPARE  a = %02X, b = %02X, eq = %d, gt = %d, lt = %d → %d\n", a, b, eq, gt, lt, i);
   215 				break;
   216 			}
   217 			case CMD_END:
   218 			{
   219 				wprintf(L"END\n");
   220 				i = MEMORY_SIZE;
   221 				break;
   222 			}
   223 			default:
   224 			{
   225 				wprintf(L"invalid command\n");
   226 			}
   227 		}
   228 
   229 	}
   230 
   231 	free(memory);
   232 	memory = nullptr;
   233 	wprintf(L"all done\n");
   234 	return 0;
   235 }
   236