memory: rename, refactor
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 25 Dec 2017 01:27:44 +0100
changeset 33ff150572e8c0
parent 32 78c4d6b53499
child 34 d7eed4d972de
memory: rename, refactor
c++/rgb-assembler/Memory.h
c++/rgb-assembler/commands/End.h
c++/rgb-assembler/commands/Goto.h
c++/rgb-assembler/commands/GotoCompare.h
c++/rgb-assembler/commands/IncrementDecrement.h
c++/rgb-assembler/rgb-assembler.cpp
     1.1 --- a/c++/rgb-assembler/Memory.h	Mon Dec 25 00:34:27 2017 +0100
     1.2 +++ b/c++/rgb-assembler/Memory.h	Mon Dec 25 01:27:44 2017 +0100
     1.3 @@ -46,7 +46,7 @@
     1.4  	}
     1.5  
     1.6  	/**
     1.7 -	 * Reads data on given position in memory and increments the index (position).
     1.8 +	 * Reads data on current position in memory and increments the index (position).
     1.9  	 * @return value found at current position
    1.10  	 */
    1.11  	template<typename T> T read() {
    1.12 @@ -61,8 +61,21 @@
    1.13  	}
    1.14  
    1.15  	/**
    1.16 +	 * Reads data on given position in memory (without affecting the current position).
    1.17 +	 * @return value found at given position
    1.18 +	 */
    1.19 +	template<typename T> T read(const address_t &address) {
    1.20 +		// TODO: map higher memory to static hardcoded areas or peripherals
    1.21 +		if (address + sizeof (T) <= MEMORY_SIZE) {
    1.22 +			return *(reinterpret_cast<T*> (memory + address));
    1.23 +		} else {
    1.24 +			return logMemoryError<T>(address);
    1.25 +		}
    1.26 +	}
    1.27 +
    1.28 +	/**
    1.29  	 * Writes data to current position in memory and increments the index (position).
    1.30 -	 * @param value value to be written at given position
    1.31 +	 * @param value value to be written at current position
    1.32  	 */
    1.33  	template<typename T> T write(const T value) {
    1.34  		if (index + sizeof (T) <= MEMORY_SIZE) {
    1.35 @@ -74,33 +87,52 @@
    1.36  		}
    1.37  	}
    1.38  
    1.39 -	void setIndex(address_t &index) {
    1.40 +	/**
    1.41 +	 * Writes data to given position in memory (without affecting the current position).
    1.42 +	 * @param value value to be written at given position
    1.43 +	 */
    1.44 +	template<typename T> T write(const address_t &address, const T value) {
    1.45 +		if (address + sizeof (T) <= MEMORY_SIZE) {
    1.46 +			T * m = reinterpret_cast<T*> (memory + address);
    1.47 +			*m = value;
    1.48 +		} else {
    1.49 +			return logMemoryError<T>(address);
    1.50 +		}
    1.51 +	}
    1.52 +
    1.53 +	/**
    1.54 +	 * Set current addres to given position.
    1.55 +	 * @param index
    1.56 +	 */
    1.57 +	void setAddress(address_t &index) {
    1.58  		this->index = index;
    1.59  	}
    1.60  
    1.61 -	address_t getIndex() {
    1.62 +	/**
    1.63 +	 * @return Current position in the memory.
    1.64 +	 */
    1.65 +	address_t getAddress() {
    1.66  		return index;
    1.67  	}
    1.68  
    1.69  	/**
    1.70 -	 * FIXME: rename
    1.71 -	 * @return 
    1.72 +	 * @return whether the current position is inside the memory boundaries = still processing.
    1.73  	 */
    1.74 -	bool isInside() {
    1.75 +	bool isNotOver() {
    1.76  		return index < MEMORY_SIZE;
    1.77  	}
    1.78 -	
    1.79 +
    1.80  	/**
    1.81 -	 * FIXME: rename, refactor
    1.82 +	 * Set current position to the start of the memory = position 0.
    1.83  	 */
    1.84 -	void start() {
    1.85 +	void setAddressToBeginning() {
    1.86  		index = 0;
    1.87  	}
    1.88  
    1.89  	/**
    1.90 -	 * FIXME: rename, refactor
    1.91 +	 * Set current position behind the end of the memory = stop processing.
    1.92  	 */
    1.93 -	void finish() {
    1.94 +	void setAddressToEnd() {
    1.95  		index = MEMORY_SIZE;
    1.96  	}
    1.97  };
     2.1 --- a/c++/rgb-assembler/commands/End.h	Mon Dec 25 00:34:27 2017 +0100
     2.2 +++ b/c++/rgb-assembler/commands/End.h	Mon Dec 25 01:27:44 2017 +0100
     2.3 @@ -34,7 +34,7 @@
     2.4  
     2.5  	void process(Memory &memory) override {
     2.6  		wprintf(L"END\n");
     2.7 -		memory.finish();
     2.8 +		memory.setAddressToEnd();
     2.9  	}
    2.10  };
    2.11  
     3.1 --- a/c++/rgb-assembler/commands/Goto.h	Mon Dec 25 00:34:27 2017 +0100
     3.2 +++ b/c++/rgb-assembler/commands/Goto.h	Mon Dec 25 01:27:44 2017 +0100
     3.3 @@ -29,7 +29,7 @@
     3.4  
     3.5  	void process(Memory &memory) override {
     3.6  		address_t index = memory.read<address_t>();
     3.7 -		memory.setIndex(index);
     3.8 +		memory.setAddress(index);
     3.9  		wprintf(L"GOTO %*d\n", 5, index);
    3.10  	}
    3.11  };
     4.1 --- a/c++/rgb-assembler/commands/GotoCompare.h	Mon Dec 25 00:34:27 2017 +0100
     4.2 +++ b/c++/rgb-assembler/commands/GotoCompare.h	Mon Dec 25 01:27:44 2017 +0100
     4.3 @@ -34,13 +34,8 @@
     4.4  		address_t gt = memory.read<address_t>();
     4.5  		address_t lt = memory.read<address_t>();
     4.6  
     4.7 -		address_t originalAddress = memory.getIndex();
     4.8 -		// TODO: add to Memory methods read(address_t) and write(address_t, T)
     4.9 -		memory.setIndex(aa);
    4.10 -		octet_t a = memory.read<octet_t>();
    4.11 -		memory.setIndex(ab);
    4.12 -		octet_t b = memory.read<octet_t>();
    4.13 -		memory.setIndex(originalAddress);
    4.14 +		octet_t a = memory.read<octet_t>(aa);
    4.15 +		octet_t b = memory.read<octet_t>(ab);
    4.16  
    4.17  		address_t index;
    4.18  
    4.19 @@ -48,7 +43,7 @@
    4.20  		else if (a > b) index = gt;
    4.21  		else index = lt;
    4.22  
    4.23 -		memory.setIndex(index);
    4.24 +		memory.setAddress(index);
    4.25  
    4.26  		wprintf(L"GOTO COMPARE  a = %02X, b = %02X, eq = %d, gt = %d, lt = %d → %d\n", a, b, eq, gt, lt, index);
    4.27  	}
     5.1 --- a/c++/rgb-assembler/commands/IncrementDecrement.h	Mon Dec 25 00:34:27 2017 +0100
     5.2 +++ b/c++/rgb-assembler/commands/IncrementDecrement.h	Mon Dec 25 01:27:44 2017 +0100
     5.3 @@ -39,14 +39,9 @@
     5.4  
     5.5  	void process(Memory &memory) override {
     5.6  		address_t address = memory.read<address_t>();
     5.7 -		address_t originalAddress = memory.getIndex();
     5.8 -		// TODO: add to Memory methods read(address_t) and write(address_t, T)
     5.9 -		memory.setIndex(address);
    5.10 -		octet_t value = memory.read<octet_t>();
    5.11 +		octet_t value = memory.read<octet_t>(address);
    5.12  		value = increment ? value + change : value - change;
    5.13 -		memory.setIndex(address);
    5.14 -		memory.write<octet_t>(value);
    5.15 -		memory.setIndex(originalAddress);
    5.16 +		memory.write<octet_t>(address, value);
    5.17  		wprintf(L"%sCREMENT %*d → %02X\n", (increment ? "IN" : "DE"), 5, address, value);
    5.18  	}
    5.19  private:
     6.1 --- a/c++/rgb-assembler/rgb-assembler.cpp	Mon Dec 25 00:34:27 2017 +0100
     6.2 +++ b/c++/rgb-assembler/rgb-assembler.cpp	Mon Dec 25 01:27:44 2017 +0100
     6.3 @@ -52,7 +52,7 @@
     6.4  		memory.write<command_t>(CMD_SLEEP);
     6.5  		memory.write<sleep_t>(255);
     6.6  		memory.write<command_t>(CMD_GOTO);
     6.7 -		memory.write<address_t>(memory.getIndex() + sizeof (address_t) + 2 * sizeof (command_t));
     6.8 +		memory.write<address_t>(memory.getAddress() + sizeof (address_t) + 2 * sizeof (command_t));
     6.9  		memory.write<command_t>(CMD_INVALID);
    6.10  		memory.write<command_t>(CMD_INVALID);
    6.11  		memory.write<command_t>(CMD_SLEEP);
    6.12 @@ -69,11 +69,11 @@
    6.13  		memory.write<command_t>(CMD_GOTO_COMPARE);
    6.14  		memory.write<address_t>(0);
    6.15  		memory.write<address_t>(0 + sizeof (command_t) + sizeof (sleep_t));
    6.16 -		memory.write<address_t>(memory.getIndex() - 3 * sizeof (address_t) - 2 * sizeof (command_t));
    6.17 +		memory.write<address_t>(memory.getAddress() - 3 * sizeof (address_t) - 2 * sizeof (command_t));
    6.18  		memory.write<address_t>(0);
    6.19 -		memory.write<address_t>(memory.getIndex() + sizeof (address_t));
    6.20 +		memory.write<address_t>(memory.getAddress() + sizeof (address_t));
    6.21  		memory.write<command_t>(CMD_END);
    6.22 -		memory.start();
    6.23 +		memory.setAddressToBeginning();
    6.24  	}
    6.25  
    6.26  	// Supported commands
    6.27 @@ -90,8 +90,8 @@
    6.28  
    6.29  
    6.30  	// Main loop / interpreter:
    6.31 -	while (memory.isInside()) {
    6.32 -		wprintf(L"command %*d = ", 4, memory.getIndex());
    6.33 +	while (memory.isNotOver()) {
    6.34 +		wprintf(L"command %*d = ", 4, memory.getAddress());
    6.35  		command_t commandCode = memory.read<command_t>();
    6.36  		wprintf(L"%02X  ", commandCode);
    6.37