# HG changeset patch # User František Kučera # Date 1513869503 -3600 # Node ID b5cf9c841efc24095a5dd0472133e2bbcf8e0725 # Parent ac1c16f0ebcd210666f239ab57680d1a6fa16a75 use malloc instead of an array (the memory contains sequence of various types so typed array makes no sense) diff -r ac1c16f0ebcd -r b5cf9c841efc c++/rgb-assembler/rgb-assembler.cpp --- a/c++/rgb-assembler/rgb-assembler.cpp Thu Dec 21 15:17:37 2017 +0100 +++ b/c++/rgb-assembler/rgb-assembler.cpp Thu Dec 21 16:18:23 2017 +0100 @@ -18,34 +18,47 @@ /** * Reads the command on given position in memory and increments the index (position). */ -command_t readCommand(command_t(&memory)[MEMORY_SIZE], address_t &index) { +command_t readCommand(command_t * memory, address_t &index) { // TODO: map higher memory to static hardcoded areas or peripherals - return memory[index++]; + command_t value = memory[index]; + index += sizeof (value); + return value; +} + +address_t readAddress(command_t * memory, address_t &index) { + address_t value = memory[index]; + index += sizeof (value); + return value; } void writeMemoryChar(command_t(&memory)[MEMORY_SIZE], address_t &index, const int value) { memory[index] = value; } -void writeMemoryChar(command_t* memory[], address_t &index, const command_t value) { - *memory[index] = value; +void writeCommand(command_t * memory, address_t &index, const command_t value) { + // command_t * m = (command_t*) (memory + index); + command_t * m = reinterpret_cast (memory + index); + *m = value; + index += sizeof (value); } int main(int argc, char* argv[]) { setlocale(LC_ALL, ""); + command_t * memory = (command_t*) malloc(MEMORY_SIZE); - command_t memory[MEMORY_SIZE] = { - CMD_SLEEP, - CMD_SLEEP, - CMD_SLEEP, - CMD_END, - }; - address_t memorySize = sizeof (memory) / sizeof (*memory); + { + address_t a = 0; + writeCommand(memory, a, CMD_SLEEP); + writeCommand(memory, a, CMD_SLEEP); + writeCommand(memory, a, CMD_SLEEP); + writeCommand(memory, a, CMD_END); + } - for (address_t i = 0; i < memorySize;) { + + for (address_t i = 0; i < MEMORY_SIZE;) { command_t ch = readCommand(memory, i); wprintf(L"command %d = %d\n", i, ch); @@ -53,14 +66,15 @@ switch (ch) { case CMD_GOTO: - command.append("GOTO"); + i = readAddress(memory, i); + command.append("GOTO "); break; case CMD_SLEEP: command.append("SLEEP"); break; case CMD_END: command.append("END"); - i = memorySize; + i = MEMORY_SIZE; break; }