c++/rgb-assembler/Memory.h
changeset 33 ff150572e8c0
parent 32 78c4d6b53499
child 34 d7eed4d972de
     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  };