1.1 --- a/c++/rgb-assembler/rgb-assembler.cpp Fri Dec 22 18:14:40 2017 +0100
1.2 +++ b/c++/rgb-assembler/rgb-assembler.cpp Sat Dec 23 17:33:49 2017 +0100
1.3 @@ -10,7 +10,7 @@
1.4 using namespace std;
1.5
1.6 typedef uint16_t address_t;
1.7 -typedef uint8_t byte_t;
1.8 +typedef uint8_t octet_t;
1.9 typedef uint8_t command_t;
1.10 typedef uint8_t sleep_t;
1.11 typedef uint8_t color_t;
1.12 @@ -78,31 +78,35 @@
1.13
1.14 /**
1.15 * Reads data on given position in memory and increments the index (position).
1.16 + *
1.17 + * @param memory array of bytes / octets
1.18 + * @param index offset in same units as memory type
1.19 + * @return value found at given position
1.20 */
1.21 -template<typename T> T read(byte_t * memory, address_t &index) {
1.22 +template<typename T> T read(octet_t * memory, address_t &index) {
1.23 // TODO: for addresses: map higher memory to static hardcoded areas or peripherals
1.24 - // TODO: sizeof (byte_t) != 1 ?
1.25 T * value = reinterpret_cast<T*> (memory + index);
1.26 - index += sizeof (*value) / sizeof (byte_t);
1.27 + index += sizeof (T);
1.28 return *value;
1.29 }
1.30
1.31 /**
1.32 * Writes data to given position in memory and increments the index (position).
1.33 + * @param memory array of bytes / octets
1.34 + * @param index offset in same units as memory type
1.35 + * @param value value to be written at given position
1.36 */
1.37 -template<typename T> void write(byte_t * memory, address_t &index, const T value) {
1.38 - // TODO: sizeof (byte_t) != 1 ?
1.39 - // T * m = (T*) (memory + index);
1.40 +template<typename T> void write(octet_t * memory, address_t &index, const T value) {
1.41 T * m = reinterpret_cast<T*> (memory + index);
1.42 *m = value;
1.43 - index += sizeof (value) / sizeof (byte_t);
1.44 + index += sizeof (value);
1.45 }
1.46
1.47 int main(int argc, char* argv[]) {
1.48
1.49 setlocale(LC_ALL, "");
1.50
1.51 - byte_t * memory = (byte_t*) malloc(MEMORY_SIZE);
1.52 + octet_t * memory = (octet_t*) malloc(MEMORY_SIZE);
1.53
1.54 // Sample program / data:
1.55 // TODO: load bytes from file, stdin, serial port, network…
1.56 @@ -172,9 +176,9 @@
1.57 address_t address = read<address_t>(memory, i);
1.58 address_t address_r = address;
1.59 address_t address_w = address_r;
1.60 - byte_t value = read<byte_t>(memory, address_r);
1.61 + octet_t value = read<octet_t>(memory, address_r);
1.62 value = command == CMD_INCREMENT ? value + 1 : value - 1;
1.63 - write<byte_t>(memory, address_w, value);
1.64 + write<octet_t>(memory, address_w, value);
1.65 wprintf(L"%sCREMENT %*d → %02X\n", (command == CMD_INCREMENT ? "IN" : "DE"), 5, address, value);
1.66 break;
1.67 }
1.68 @@ -186,8 +190,8 @@
1.69 address_t gt = read<address_t>(memory, i);
1.70 address_t lt = read<address_t>(memory, i);
1.71
1.72 - byte_t a = read<byte_t>(memory, aa);
1.73 - byte_t b = read<byte_t>(memory, ab);
1.74 + octet_t a = read<octet_t>(memory, aa);
1.75 + octet_t b = read<octet_t>(memory, ab);
1.76
1.77 if (a == b) i = eq;
1.78 else if (a > b) i = gt;