c++/rgb-assembler/rgb-assembler.cpp
changeset 31 b997cbf9e30b
parent 30 f049c3d3244d
child 32 78c4d6b53499
     1.1 --- a/c++/rgb-assembler/rgb-assembler.cpp	Sun Dec 24 00:47:34 2017 +0100
     1.2 +++ b/c++/rgb-assembler/rgb-assembler.cpp	Mon Dec 25 00:24:07 2017 +0100
     1.3 @@ -40,41 +40,40 @@
     1.4  
     1.5  	setlocale(LC_ALL, "");
     1.6  
     1.7 -	// TODO: wrap in a class with read(), write() and setAddress() methods
     1.8 -	octet_t * memory = (octet_t*) malloc(MEMORY_SIZE);
     1.9 -
    1.10 +	Memory memory;
    1.11 +	
    1.12  	// Sample program / data:
    1.13  	// TODO: load bytes from file, stdin, serial port, network…
    1.14  	{
    1.15 -		address_t a = 0;
    1.16 -		write<command_t>(memory, a, CMD_SLEEP);
    1.17 -		write<sleep_t>(memory, a, 255);
    1.18 -		write<command_t>(memory, a, CMD_SLEEP);
    1.19 -		write<sleep_t>(memory, a, 10);
    1.20 -		write<command_t>(memory, a, CMD_SLEEP);
    1.21 -		write<sleep_t>(memory, a, 255);
    1.22 -		write<command_t>(memory, a, CMD_GOTO);
    1.23 -		write<address_t>(memory, a, a + sizeof (address_t) + 2 * sizeof (command_t));
    1.24 -		write<command_t>(memory, a, CMD_INVALID);
    1.25 -		write<command_t>(memory, a, CMD_INVALID);
    1.26 -		write<command_t>(memory, a, CMD_SLEEP);
    1.27 -		write<sleep_t>(memory, a, 255);
    1.28 -		write<command_t>(memory, a, CMD_COLOR);
    1.29 -		write<led_t>(memory, a, 23);
    1.30 -		write<color_t>(memory, a, 0);
    1.31 -		write<color_t>(memory, a, 200);
    1.32 -		write<color_t>(memory, a, 255);
    1.33 -		write<command_t>(memory, a, CMD_INCREMENT);
    1.34 -		write<address_t>(memory, a, 0);
    1.35 -		write<command_t>(memory, a, CMD_DECREMENT);
    1.36 -		write<address_t>(memory, a, 0);
    1.37 -		write<command_t>(memory, a, CMD_GOTO_COMPARE);
    1.38 -		write<address_t>(memory, a, 0);
    1.39 -		write<address_t>(memory, a, 0 + sizeof (command_t) + sizeof (sleep_t));
    1.40 -		write<address_t>(memory, a, a - 3 * sizeof (address_t) - 2 * sizeof (command_t));
    1.41 -		write<address_t>(memory, a, 0);
    1.42 -		write<address_t>(memory, a, a + sizeof (address_t));
    1.43 -		write<command_t>(memory, a, CMD_END);
    1.44 +		memory.write<command_t>(CMD_SLEEP);
    1.45 +		memory.write<sleep_t>(255);
    1.46 +		memory.write<command_t>(CMD_SLEEP);
    1.47 +		memory.write<sleep_t>(10);
    1.48 +		memory.write<command_t>(CMD_SLEEP);
    1.49 +		memory.write<sleep_t>(255);
    1.50 +		memory.write<command_t>(CMD_GOTO);
    1.51 +		memory.write<address_t>(memory.getIndex() + sizeof (address_t) + 2 * sizeof (command_t));
    1.52 +		memory.write<command_t>(CMD_INVALID);
    1.53 +		memory.write<command_t>(CMD_INVALID);
    1.54 +		memory.write<command_t>(CMD_SLEEP);
    1.55 +		memory.write<sleep_t>(255);
    1.56 +		memory.write<command_t>(CMD_COLOR);
    1.57 +		memory.write<led_t>(23);
    1.58 +		memory.write<color_t>(0);
    1.59 +		memory.write<color_t>(200);
    1.60 +		memory.write<color_t>(255);
    1.61 +		memory.write<command_t>(CMD_INCREMENT);
    1.62 +		memory.write<address_t>(0);
    1.63 +		memory.write<command_t>(CMD_DECREMENT);
    1.64 +		memory.write<address_t>(0);
    1.65 +		memory.write<command_t>(CMD_GOTO_COMPARE);
    1.66 +		memory.write<address_t>(0);
    1.67 +		memory.write<address_t>(0 + sizeof (command_t) + sizeof (sleep_t));
    1.68 +		memory.write<address_t>(memory.getIndex() - 3 * sizeof (address_t) - 2 * sizeof (command_t));
    1.69 +		memory.write<address_t>(0);
    1.70 +		memory.write<address_t>(memory.getIndex() + sizeof (address_t));
    1.71 +		memory.write<command_t>(CMD_END);
    1.72 +		memory.start();
    1.73  	}
    1.74  
    1.75  	// Supported commands
    1.76 @@ -91,22 +90,20 @@
    1.77  
    1.78  
    1.79  	// Main loop / interpreter:
    1.80 -	for (address_t i = 0; i < MEMORY_SIZE;) {
    1.81 -		wprintf(L"command %*d = ", 4, i);
    1.82 -		command_t commandCode = read<command_t>(memory, i);
    1.83 +	while (memory.isInside()) {
    1.84 +		wprintf(L"command %*d = ", 4, memory.getIndex());
    1.85 +		command_t commandCode = memory.read<command_t>();
    1.86  		wprintf(L"%02X  ", commandCode);
    1.87  
    1.88  		shared_ptr<Command> command = commands[commandCode];
    1.89  
    1.90  		if (command) {
    1.91 -			command->process(memory, i);
    1.92 +			command->process(memory);
    1.93  		} else {
    1.94  			wprintf(L"invalid command\n");
    1.95  		}
    1.96  	}
    1.97  
    1.98 -	free(memory);
    1.99 -	memory = nullptr;
   1.100  	wprintf(L"all done\n");
   1.101  	return 0;
   1.102  }