use malloc instead of an array (the memory contains sequence of various types so typed array makes no sense)
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 21 Dec 2017 16:18:23 +0100
changeset 2b5cf9c841efc
parent 1 ac1c16f0ebcd
child 3 1dd99de3b178
use malloc instead of an array (the memory contains sequence of various types so typed array makes no sense)
c++/rgb-assembler/rgb-assembler.cpp
     1.1 --- a/c++/rgb-assembler/rgb-assembler.cpp	Thu Dec 21 15:17:37 2017 +0100
     1.2 +++ b/c++/rgb-assembler/rgb-assembler.cpp	Thu Dec 21 16:18:23 2017 +0100
     1.3 @@ -18,34 +18,47 @@
     1.4  /**
     1.5   * Reads the command on given position in memory and increments the index (position).
     1.6   */
     1.7 -command_t readCommand(command_t(&memory)[MEMORY_SIZE], address_t &index) {
     1.8 +command_t readCommand(command_t * memory, address_t &index) {
     1.9  	// TODO: map higher memory to static hardcoded areas or peripherals
    1.10 -	return memory[index++];
    1.11 +	command_t value = memory[index];
    1.12 +	index += sizeof (value);
    1.13 +	return value;
    1.14 +}
    1.15 +
    1.16 +address_t readAddress(command_t * memory, address_t &index) {
    1.17 +	address_t value = memory[index];
    1.18 +	index += sizeof (value);
    1.19 +	return value;
    1.20  }
    1.21  
    1.22  void writeMemoryChar(command_t(&memory)[MEMORY_SIZE], address_t &index, const int value) {
    1.23  	memory[index] = value;
    1.24  }
    1.25  
    1.26 -void writeMemoryChar(command_t* memory[], address_t &index, const command_t value) {
    1.27 -	*memory[index] = value;
    1.28 +void writeCommand(command_t * memory, address_t &index, const command_t value) {
    1.29 +	// command_t * m = (command_t*) (memory + index);
    1.30 +	command_t * m = reinterpret_cast<command_t*> (memory + index);
    1.31 +	*m = value;
    1.32 +	index += sizeof (value);
    1.33  }
    1.34  
    1.35  int main(int argc, char* argv[]) {
    1.36  
    1.37  	setlocale(LC_ALL, "");
    1.38  
    1.39 +	command_t * memory = (command_t*) malloc(MEMORY_SIZE);
    1.40  
    1.41 -	command_t memory[MEMORY_SIZE] = {
    1.42 -		CMD_SLEEP,
    1.43 -		CMD_SLEEP,
    1.44 -		CMD_SLEEP,
    1.45 -		CMD_END,
    1.46 -	};
    1.47 -	address_t memorySize = sizeof (memory) / sizeof (*memory);
    1.48 +	{
    1.49 +		address_t a = 0;
    1.50 +		writeCommand(memory, a, CMD_SLEEP);
    1.51 +		writeCommand(memory, a, CMD_SLEEP);
    1.52 +		writeCommand(memory, a, CMD_SLEEP);
    1.53 +		writeCommand(memory, a, CMD_END);
    1.54 +	}
    1.55  
    1.56  
    1.57 -	for (address_t i = 0; i < memorySize;) {
    1.58 +
    1.59 +	for (address_t i = 0; i < MEMORY_SIZE;) {
    1.60  		command_t ch = readCommand(memory, i);
    1.61  		wprintf(L"command %d = %d\n", i, ch);
    1.62  
    1.63 @@ -53,14 +66,15 @@
    1.64  
    1.65  		switch (ch) {
    1.66  			case CMD_GOTO:
    1.67 -				command.append("GOTO");
    1.68 +				i = readAddress(memory, i);
    1.69 +				command.append("GOTO ");
    1.70  				break;
    1.71  			case CMD_SLEEP:
    1.72  				command.append("SLEEP");
    1.73  				break;
    1.74  			case CMD_END:
    1.75  				command.append("END");
    1.76 -				i = memorySize;
    1.77 +				i = MEMORY_SIZE;
    1.78  				break;
    1.79  
    1.80  		}