c++/rgb-assembler/rgb-assembler.cpp
changeset 9 157bc062efa7
parent 8 d4b2ec9ef8bc
child 10 efed38f454a0
     1.1 --- a/c++/rgb-assembler/rgb-assembler.cpp	Thu Dec 21 21:49:15 2017 +0100
     1.2 +++ b/c++/rgb-assembler/rgb-assembler.cpp	Thu Dec 21 21:55:26 2017 +0100
     1.3 @@ -30,23 +30,13 @@
     1.4  	return *value;
     1.5  }
     1.6  
     1.7 -void writeCommand(command_t * memory, address_t &index, const command_t value) {
     1.8 -	// command_t * m = (command_t*) (memory + index);
     1.9 -	command_t * m = reinterpret_cast<command_t*> (memory + index);
    1.10 -	*m = value;
    1.11 -	index += sizeof (value) / sizeof (command_t);
    1.12 -}
    1.13 -
    1.14 -void writeAddress(command_t * memory, address_t &index, const address_t value) {
    1.15 -	// command_t * m = (command_t*) (memory + index);
    1.16 -	address_t * m = reinterpret_cast<address_t*> (memory + index);
    1.17 -	*m = value;
    1.18 -	index += sizeof (value) / sizeof (command_t);
    1.19 -}
    1.20 -
    1.21 -void writeSleep(command_t * memory, address_t &index, const sleep_t value) {
    1.22 -	// command_t * m = (command_t*) (memory + index);
    1.23 -	sleep_t * m = reinterpret_cast<sleep_t*> (memory + index);
    1.24 +/**
    1.25 + * Writes data to given position in memory and increments the index (position).
    1.26 + */
    1.27 +template<typename T> void write(command_t * memory, address_t &index, const T value) {
    1.28 +	// TODO: sizeof (command_t) != 1 ?
    1.29 +	// T * m = (T*) (memory + index);
    1.30 +	T * m = reinterpret_cast<T*> (memory + index);
    1.31  	*m = value;
    1.32  	index += sizeof (value) / sizeof (command_t);
    1.33  }
    1.34 @@ -59,23 +49,21 @@
    1.35  
    1.36  	{
    1.37  		address_t a = 0;
    1.38 -		writeCommand(memory, a, CMD_SLEEP);
    1.39 -		writeSleep(memory, a, 255);
    1.40 -		writeCommand(memory, a, CMD_SLEEP);
    1.41 -		writeSleep(memory, a, 10);
    1.42 -		writeCommand(memory, a, CMD_SLEEP);
    1.43 -		writeSleep(memory, a, 255);
    1.44 -		writeCommand(memory, a, CMD_GOTO);
    1.45 -		writeAddress(memory, a, a + 4);
    1.46 -		writeCommand(memory, a, 1);
    1.47 -		writeCommand(memory, a, 1);
    1.48 -		writeCommand(memory, a, CMD_SLEEP);
    1.49 -		writeSleep(memory, a, 255);
    1.50 -		writeCommand(memory, a, CMD_END);
    1.51 +		write<command_t>(memory, a, CMD_SLEEP);
    1.52 +		write<sleep_t>(memory, a, 255);
    1.53 +		write<command_t>(memory, a, CMD_SLEEP);
    1.54 +		write<sleep_t>(memory, a, 10);
    1.55 +		write<command_t>(memory, a, CMD_SLEEP);
    1.56 +		write<sleep_t>(memory, a, 255);
    1.57 +		write<command_t>(memory, a, CMD_GOTO);
    1.58 +		write<address_t>(memory, a, a + 4);
    1.59 +		write<command_t>(memory, a, 1);
    1.60 +		write<command_t>(memory, a, 1);
    1.61 +		write<command_t>(memory, a, CMD_SLEEP);
    1.62 +		write<sleep_t>(memory, a, 255);
    1.63 +		write<command_t>(memory, a, CMD_END);
    1.64  	}
    1.65  
    1.66 -
    1.67 -
    1.68  	for (address_t i = 0; i < MEMORY_SIZE;) {
    1.69  		wprintf(L"command %d = ", i);
    1.70  		command_t ch = read<command_t>(memory, i);
    1.71 @@ -104,8 +92,6 @@
    1.72  		}
    1.73  	}
    1.74  
    1.75 -
    1.76 -
    1.77  	free(memory);
    1.78  	memory = nullptr;
    1.79  	wprintf(L"all done\n");