# HG changeset patch # User František Kučera # Date 1514046829 -3600 # Node ID 8f0a5552db789132b6b9990c620777145a74d19d # Parent 5c142e9c00e5365cbaa078fee9bdc720df2644ad index in read() and write() is in same units as memory type diff -r 5c142e9c00e5 -r 8f0a5552db78 c++/rgb-assembler/rgb-assembler.cpp --- a/c++/rgb-assembler/rgb-assembler.cpp Fri Dec 22 18:14:40 2017 +0100 +++ b/c++/rgb-assembler/rgb-assembler.cpp Sat Dec 23 17:33:49 2017 +0100 @@ -10,7 +10,7 @@ using namespace std; typedef uint16_t address_t; -typedef uint8_t byte_t; +typedef uint8_t octet_t; typedef uint8_t command_t; typedef uint8_t sleep_t; typedef uint8_t color_t; @@ -78,31 +78,35 @@ /** * Reads data on given position in memory and increments the index (position). + * + * @param memory array of bytes / octets + * @param index offset in same units as memory type + * @return value found at given position */ -template T read(byte_t * memory, address_t &index) { +template T read(octet_t * memory, address_t &index) { // TODO: for addresses: map higher memory to static hardcoded areas or peripherals - // TODO: sizeof (byte_t) != 1 ? T * value = reinterpret_cast (memory + index); - index += sizeof (*value) / sizeof (byte_t); + index += sizeof (T); return *value; } /** * Writes data to given position in memory and increments the index (position). + * @param memory array of bytes / octets + * @param index offset in same units as memory type + * @param value value to be written at given position */ -template void write(byte_t * memory, address_t &index, const T value) { - // TODO: sizeof (byte_t) != 1 ? - // T * m = (T*) (memory + index); +template void write(octet_t * memory, address_t &index, const T value) { T * m = reinterpret_cast (memory + index); *m = value; - index += sizeof (value) / sizeof (byte_t); + index += sizeof (value); } int main(int argc, char* argv[]) { setlocale(LC_ALL, ""); - byte_t * memory = (byte_t*) malloc(MEMORY_SIZE); + octet_t * memory = (octet_t*) malloc(MEMORY_SIZE); // Sample program / data: // TODO: load bytes from file, stdin, serial port, network… @@ -172,9 +176,9 @@ address_t address = read(memory, i); address_t address_r = address; address_t address_w = address_r; - byte_t value = read(memory, address_r); + octet_t value = read(memory, address_r); value = command == CMD_INCREMENT ? value + 1 : value - 1; - write(memory, address_w, value); + write(memory, address_w, value); wprintf(L"%sCREMENT %*d → %02X\n", (command == CMD_INCREMENT ? "IN" : "DE"), 5, address, value); break; } @@ -186,8 +190,8 @@ address_t gt = read(memory, i); address_t lt = read(memory, i); - byte_t a = read(memory, aa); - byte_t b = read(memory, ab); + octet_t a = read(memory, aa); + octet_t b = read(memory, ab); if (a == b) i = eq; else if (a > b) i = gt;