INCREMENT, DECREMENT
authorFrantišek Kučera <franta-hg@frantovo.cz>
Fri, 22 Dec 2017 17:15:15 +0100
changeset 142ab3d7282249
parent 13 2a4b8b3abe14
child 15 4b4fb847d01a
INCREMENT, DECREMENT
c++/rgb-assembler/rgb-assembler.cpp
     1.1 --- a/c++/rgb-assembler/rgb-assembler.cpp	Fri Dec 22 16:21:10 2017 +0100
     1.2 +++ b/c++/rgb-assembler/rgb-assembler.cpp	Fri Dec 22 17:15:15 2017 +0100
     1.3 @@ -10,6 +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 command_t;
     1.9  typedef uint8_t sleep_t;
    1.10  typedef uint8_t color_t;
    1.11 @@ -46,6 +47,18 @@
    1.12  const command_t CMD_END = 0xED;
    1.13  
    1.14  /**
    1.15 + * Increase value at given address
    1.16 + * parameter: address_t
    1.17 + */
    1.18 +const command_t CMD_INCREMENT = 0x11;
    1.19 +
    1.20 +/**
    1.21 + * Decrease value at given address
    1.22 + * parameter: address_t
    1.23 + */
    1.24 +const command_t CMD_DECREMENT = 0x12;
    1.25 +
    1.26 +/**
    1.27   * Placeholder for unsupported command.
    1.28   * Just for testing.
    1.29   */
    1.30 @@ -54,30 +67,30 @@
    1.31  /**
    1.32   * Reads data on given position in memory and increments the index (position).
    1.33   */
    1.34 -template<typename T> T read(command_t * memory, address_t &index) {
    1.35 +template<typename T> T read(byte_t * memory, address_t &index) {
    1.36  	// TODO: for addresses: map higher memory to static hardcoded areas or peripherals
    1.37 -	// TODO: sizeof (command_t) != 1 ?
    1.38 +	// TODO: sizeof (byte_t) != 1 ?
    1.39  	T * value = reinterpret_cast<T*> (memory + index);
    1.40 -	index += sizeof (*value) / sizeof (command_t);
    1.41 +	index += sizeof (*value) / sizeof (byte_t);
    1.42  	return *value;
    1.43  }
    1.44  
    1.45  /**
    1.46   * Writes data to given position in memory and increments the index (position).
    1.47   */
    1.48 -template<typename T> void write(command_t * memory, address_t &index, const T value) {
    1.49 -	// TODO: sizeof (command_t) != 1 ?
    1.50 +template<typename T> void write(byte_t * memory, address_t &index, const T value) {
    1.51 +	// TODO: sizeof (byte_t) != 1 ?
    1.52  	// T * m = (T*) (memory + index);
    1.53  	T * m = reinterpret_cast<T*> (memory + index);
    1.54  	*m = value;
    1.55 -	index += sizeof (value) / sizeof (command_t);
    1.56 +	index += sizeof (value) / sizeof (byte_t);
    1.57  }
    1.58  
    1.59  int main(int argc, char* argv[]) {
    1.60  
    1.61  	setlocale(LC_ALL, "");
    1.62  
    1.63 -	command_t * memory = (command_t*) malloc(MEMORY_SIZE);
    1.64 +	byte_t * memory = (byte_t*) malloc(MEMORY_SIZE);
    1.65  
    1.66  	{
    1.67  		address_t a = 0;
    1.68 @@ -98,6 +111,10 @@
    1.69  		write<color_t>(memory, a, 0);
    1.70  		write<color_t>(memory, a, 200);
    1.71  		write<color_t>(memory, a, 255);
    1.72 +		write<command_t>(memory, a, CMD_INCREMENT);
    1.73 +		write<address_t>(memory, a, 0);
    1.74 +		write<command_t>(memory, a, CMD_DECREMENT);
    1.75 +		write<address_t>(memory, a, 0);
    1.76  		write<command_t>(memory, a, CMD_END);
    1.77  	}
    1.78  
    1.79 @@ -129,6 +146,18 @@
    1.80  				wprintf(L"COLOR  %02X %02X %02X → %d\n", r, g, b, led);
    1.81  				break;
    1.82  			}
    1.83 +			case CMD_INCREMENT:
    1.84 +			case CMD_DECREMENT:
    1.85 +			{
    1.86 +				address_t address = read<address_t>(memory, i);
    1.87 +				address_t address_r = address;
    1.88 +				address_t address_w = address_r;
    1.89 +				byte_t value = read<byte_t>(memory, address_r);
    1.90 +				value = ch == CMD_INCREMENT ? value + 1 : value - 1;
    1.91 +				write<byte_t>(memory, address_w, value);
    1.92 +				wprintf(L"%sCREMENT %*d → %02X\n", (ch == CMD_INCREMENT ? "IN" : "DE"), 5, address, value);
    1.93 +				break;
    1.94 +			}
    1.95  			case CMD_END:
    1.96  			{
    1.97  				wprintf(L"END\n");