c++/rgb-assembler/rgb-assembler.cpp
changeset 26 47ee91579133
parent 25 5ce09de7f9b7
child 27 835bd4559e5c
     1.1 --- a/c++/rgb-assembler/rgb-assembler.cpp	Sat Dec 23 23:50:44 2017 +0100
     1.2 +++ b/c++/rgb-assembler/rgb-assembler.cpp	Sun Dec 24 00:08:55 2017 +0100
     1.3 @@ -13,6 +13,7 @@
     1.4  #include "commands/Sleep.h"
     1.5  #include "commands/End.h"
     1.6  #include "commands/Color.h"
     1.7 +#include "commands/IncrementDecrement.h"
     1.8  
     1.9  using namespace std;
    1.10  
    1.11 @@ -120,41 +121,23 @@
    1.12  		{CMD_SLEEP, make_shared<commands::Sleep>()},
    1.13  		{CMD_END, make_shared<commands::End>()},
    1.14  		{CMD_COLOR, make_shared<commands::Color>()},
    1.15 +		{CMD_INCREMENT, make_shared < commands::IncrementDecrement>(true)},
    1.16 +		{CMD_DECREMENT, make_shared < commands::IncrementDecrement>(false)},
    1.17  	};
    1.18 +	
    1.19  
    1.20  	for (address_t i = 0; i < MEMORY_SIZE;) {
    1.21  		wprintf(L"command %*d = ", 4, i);
    1.22 -		command_t command = read<command_t>(memory, i);
    1.23 -		wprintf(L"%02X  ", command);
    1.24 +		command_t commandCode = read<command_t>(memory, i);
    1.25 +		wprintf(L"%02X  ", commandCode);
    1.26  
    1.27 -		shared_ptr<Command> cx = commands[command];
    1.28 +		shared_ptr<Command> command = commands[commandCode];
    1.29  
    1.30 -		if (cx) {
    1.31 -			cx->process(memory, i);
    1.32 -			continue;
    1.33 +		if (command) {
    1.34 +			command->process(memory, i);
    1.35  		} else {
    1.36 -			// TODO: wprintf(L"invalid command\n");
    1.37 +			wprintf(L"invalid command\n");
    1.38  		}
    1.39 -
    1.40 -		switch (command) {
    1.41 -			case CMD_INCREMENT:
    1.42 -			case CMD_DECREMENT:
    1.43 -			{
    1.44 -				address_t address = read<address_t>(memory, i);
    1.45 -				address_t address_r = address;
    1.46 -				address_t address_w = address_r;
    1.47 -				octet_t value = read<octet_t>(memory, address_r);
    1.48 -				value = command == CMD_INCREMENT ? value + 1 : value - 1;
    1.49 -				write<octet_t>(memory, address_w, value);
    1.50 -				wprintf(L"%sCREMENT %*d → %02X\n", (command == CMD_INCREMENT ? "IN" : "DE"), 5, address, value);
    1.51 -				break;
    1.52 -			}
    1.53 -			default:
    1.54 -			{
    1.55 -				wprintf(L"invalid command\n");
    1.56 -			}
    1.57 -		}
    1.58 -
    1.59  	}
    1.60  
    1.61  	free(memory);
    1.62 @@ -162,4 +145,3 @@
    1.63  	wprintf(L"all done\n");
    1.64  	return 0;
    1.65  }
    1.66 -