INCREMENT and DECREMENT in class
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 24 Dec 2017 00:08:55 +0100
changeset 2647ee91579133
parent 25 5ce09de7f9b7
child 27 835bd4559e5c
INCREMENT and DECREMENT in class
c++/rgb-assembler/commands/IncrementDecrement.h
c++/rgb-assembler/nbproject/configurations.xml
c++/rgb-assembler/rgb-assembler.cpp
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/c++/rgb-assembler/commands/IncrementDecrement.h	Sun Dec 24 00:08:55 2017 +0100
     1.3 @@ -0,0 +1,35 @@
     1.4 +#pragma once
     1.5 +
     1.6 +#include <wchar.h>
     1.7 +#include <chrono>
     1.8 +#include <thread>
     1.9 +
    1.10 +#include "../Command.h"
    1.11 +#include "../memory.h"
    1.12 +
    1.13 +using namespace std;
    1.14 +
    1.15 +namespace commands {
    1.16 +
    1.17 +class IncrementDecrement : public Command {
    1.18 +public:
    1.19 +
    1.20 +	IncrementDecrement(const bool increment) {
    1.21 +		this->increment = increment;
    1.22 +	}
    1.23 +
    1.24 +	void process(octet_t* memory, address_t& index) override {
    1.25 +		address_t address = read<address_t>(memory, index);
    1.26 +		address_t address_r = address;
    1.27 +		address_t address_w = address_r;
    1.28 +		octet_t value = read<octet_t>(memory, address_r);
    1.29 +		value = increment ? value + 1 : value - 1;
    1.30 +		write<octet_t>(memory, address_w, value);
    1.31 +		wprintf(L"%sCREMENT %*d → %02X\n", (increment ? "IN" : "DE"), 5, address, value);
    1.32 +	}
    1.33 +private:
    1.34 +	bool increment;
    1.35 +
    1.36 +};
    1.37 +
    1.38 +}
     2.1 --- a/c++/rgb-assembler/nbproject/configurations.xml	Sat Dec 23 23:50:44 2017 +0100
     2.2 +++ b/c++/rgb-assembler/nbproject/configurations.xml	Sun Dec 24 00:08:55 2017 +0100
     2.3 @@ -9,6 +9,7 @@
     2.4        <itemPath>commands/End.h</itemPath>
     2.5        <itemPath>commands/Goto.h</itemPath>
     2.6        <itemPath>commands/GotoCompare.h</itemPath>
     2.7 +      <itemPath>commands/IncrementDecrement.h</itemPath>
     2.8        <itemPath>commands/Sleep.h</itemPath>
     2.9        <itemPath>memory.h</itemPath>
    2.10        <itemPath>types.h</itemPath>
    2.11 @@ -58,6 +59,8 @@
    2.12        </item>
    2.13        <item path="commands/GotoCompare.h" ex="false" tool="3" flavor2="0">
    2.14        </item>
    2.15 +      <item path="commands/IncrementDecrement.h" ex="false" tool="3" flavor2="0">
    2.16 +      </item>
    2.17        <item path="commands/Sleep.h" ex="false" tool="3" flavor2="0">
    2.18        </item>
    2.19        <item path="memory.h" ex="false" tool="3" flavor2="0">
    2.20 @@ -98,6 +101,8 @@
    2.21        </item>
    2.22        <item path="commands/GotoCompare.h" ex="false" tool="3" flavor2="0">
    2.23        </item>
    2.24 +      <item path="commands/IncrementDecrement.h" ex="false" tool="3" flavor2="0">
    2.25 +      </item>
    2.26        <item path="commands/Sleep.h" ex="false" tool="3" flavor2="0">
    2.27        </item>
    2.28        <item path="memory.h" ex="false" tool="3" flavor2="0">
     3.1 --- a/c++/rgb-assembler/rgb-assembler.cpp	Sat Dec 23 23:50:44 2017 +0100
     3.2 +++ b/c++/rgb-assembler/rgb-assembler.cpp	Sun Dec 24 00:08:55 2017 +0100
     3.3 @@ -13,6 +13,7 @@
     3.4  #include "commands/Sleep.h"
     3.5  #include "commands/End.h"
     3.6  #include "commands/Color.h"
     3.7 +#include "commands/IncrementDecrement.h"
     3.8  
     3.9  using namespace std;
    3.10  
    3.11 @@ -120,41 +121,23 @@
    3.12  		{CMD_SLEEP, make_shared<commands::Sleep>()},
    3.13  		{CMD_END, make_shared<commands::End>()},
    3.14  		{CMD_COLOR, make_shared<commands::Color>()},
    3.15 +		{CMD_INCREMENT, make_shared < commands::IncrementDecrement>(true)},
    3.16 +		{CMD_DECREMENT, make_shared < commands::IncrementDecrement>(false)},
    3.17  	};
    3.18 +	
    3.19  
    3.20  	for (address_t i = 0; i < MEMORY_SIZE;) {
    3.21  		wprintf(L"command %*d = ", 4, i);
    3.22 -		command_t command = read<command_t>(memory, i);
    3.23 -		wprintf(L"%02X  ", command);
    3.24 +		command_t commandCode = read<command_t>(memory, i);
    3.25 +		wprintf(L"%02X  ", commandCode);
    3.26  
    3.27 -		shared_ptr<Command> cx = commands[command];
    3.28 +		shared_ptr<Command> command = commands[commandCode];
    3.29  
    3.30 -		if (cx) {
    3.31 -			cx->process(memory, i);
    3.32 -			continue;
    3.33 +		if (command) {
    3.34 +			command->process(memory, i);
    3.35  		} else {
    3.36 -			// TODO: wprintf(L"invalid command\n");
    3.37 +			wprintf(L"invalid command\n");
    3.38  		}
    3.39 -
    3.40 -		switch (command) {
    3.41 -			case CMD_INCREMENT:
    3.42 -			case CMD_DECREMENT:
    3.43 -			{
    3.44 -				address_t address = read<address_t>(memory, i);
    3.45 -				address_t address_r = address;
    3.46 -				address_t address_w = address_r;
    3.47 -				octet_t value = read<octet_t>(memory, address_r);
    3.48 -				value = command == CMD_INCREMENT ? value + 1 : value - 1;
    3.49 -				write<octet_t>(memory, address_w, value);
    3.50 -				wprintf(L"%sCREMENT %*d → %02X\n", (command == CMD_INCREMENT ? "IN" : "DE"), 5, address, value);
    3.51 -				break;
    3.52 -			}
    3.53 -			default:
    3.54 -			{
    3.55 -				wprintf(L"invalid command\n");
    3.56 -			}
    3.57 -		}
    3.58 -
    3.59  	}
    3.60  
    3.61  	free(memory);
    3.62 @@ -162,4 +145,3 @@
    3.63  	wprintf(L"all done\n");
    3.64  	return 0;
    3.65  }
    3.66 -