template / generic read() function
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 21 Dec 2017 21:49:15 +0100
changeset 8d4b2ec9ef8bc
parent 7 379c490a9831
child 9 157bc062efa7
template / generic read() function
c++/rgb-assembler/rgb-assembler.cpp
     1.1 --- a/c++/rgb-assembler/rgb-assembler.cpp	Thu Dec 21 18:13:35 2017 +0100
     1.2 +++ b/c++/rgb-assembler/rgb-assembler.cpp	Thu Dec 21 21:49:15 2017 +0100
     1.3 @@ -20,24 +20,12 @@
     1.4  const command_t CMD_END = 0xED;
     1.5  
     1.6  /**
     1.7 - * Reads the command on given position in memory and increments the index (position).
     1.8 + * Reads data on given position in memory and increments the index (position).
     1.9   */
    1.10 -command_t readCommand(command_t * memory, address_t &index) {
    1.11 -	// TODO: map higher memory to static hardcoded areas or peripherals
    1.12 -	command_t * value = reinterpret_cast<command_t*> (memory + index);
    1.13 -	index += sizeof (*value) / sizeof (command_t);
    1.14 -	return *value;
    1.15 -}
    1.16 -
    1.17 -address_t readAddress(command_t * memory, address_t &index) {
    1.18 -	address_t * value = reinterpret_cast<address_t*> (memory + index);
    1.19 -	index += sizeof (*value) / sizeof (command_t);
    1.20 -	return *value;
    1.21 -}
    1.22 -
    1.23 -sleep_t readSleep(command_t * memory, address_t &index) {
    1.24 -	// TODO: map higher memory to static hardcoded areas or peripherals
    1.25 -	sleep_t * value = reinterpret_cast<sleep_t*> (memory + index);
    1.26 +template<typename T> T read(command_t * memory, address_t &index) {
    1.27 +	// TODO: for addresses: map higher memory to static hardcoded areas or peripherals
    1.28 +	// TODO: sizeof (command_t) != 1 ?
    1.29 +	T * value = reinterpret_cast<T*> (memory + index);
    1.30  	index += sizeof (*value) / sizeof (command_t);
    1.31  	return *value;
    1.32  }
    1.33 @@ -90,19 +78,19 @@
    1.34  
    1.35  	for (address_t i = 0; i < MEMORY_SIZE;) {
    1.36  		wprintf(L"command %d = ", i);
    1.37 -		command_t ch = readCommand(memory, i);
    1.38 +		command_t ch = read<command_t>(memory, i);
    1.39  		wprintf(L"%X\n", ch);
    1.40  
    1.41  		string command;
    1.42  
    1.43  		switch (ch) {
    1.44  			case CMD_GOTO:
    1.45 -				i = readAddress(memory, i);
    1.46 +				i = read<address_t>(memory, i);
    1.47  				command.append("GOTO ");
    1.48  				break;
    1.49  			case CMD_SLEEP:
    1.50  				command.append("SLEEP");
    1.51 -				this_thread::sleep_for(chrono::milliseconds(readSleep(memory, i)));
    1.52 +				this_thread::sleep_for(chrono::milliseconds(read<sleep_t>(memory, i)));
    1.53  				break;
    1.54  			case CMD_END:
    1.55  				command.append("END");