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