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