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