c++/rgb-assembler/Memory.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 27 Dec 2017 11:34:09 +0100
changeset 36 2b3a26d3b1ad
parent 35 84356a15828b
child 37 b63d62c675fe
permissions -rw-r--r--
write() don't need return value
     1 /**
     2  * RGB assembler
     3  * Copyright © 2017 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 #pragma once
    20 
    21 #include "types.h"
    22 
    23 class Memory {
    24 private:
    25 
    26 	template<typename T> T logMemoryError(const address_t &index) {
    27 		wprintf(L"memory error: index = %d, sizeof(T) = %d, MEMORY_SIZE = %d\n", index, sizeof (T), memorySize);
    28 		// TODO: return error value or throw exception
    29 		return T();
    30 	}
    31 
    32 	octet_t * memory;
    33 	address_t memorySize;
    34 	address_t index;
    35 
    36 public:
    37 
    38 	Memory(const address_t memorySize) {
    39 		this->memory = (octet_t*) malloc(memorySize);
    40 		this->memorySize = memorySize;
    41 		this->index = 0;
    42 	}
    43 
    44 	virtual ~Memory() {
    45 		free(memory);
    46 	}
    47 
    48 	/**
    49 	 * Reads data on current position in memory and increments the index (position).
    50 	 * @return value found at current position
    51 	 */
    52 	template<typename T> T read() {
    53 		// TODO: map higher memory to static hardcoded areas or peripherals
    54 		if (index + sizeof (T) <= memorySize) {
    55 			T * value = reinterpret_cast<T*> (memory + index);
    56 			index += sizeof (T);
    57 			return *value;
    58 		} else {
    59 			return logMemoryError<T>(index);
    60 		}
    61 	}
    62 
    63 	/**
    64 	 * Reads data on given position in memory (without affecting the current position).
    65 	 * @return value found at given position
    66 	 */
    67 	template<typename T> T read(const address_t &address) {
    68 		// TODO: map higher memory to static hardcoded areas or peripherals
    69 		if (address + sizeof (T) <= memorySize) {
    70 			return *(reinterpret_cast<T*> (memory + address));
    71 		} else {
    72 			return logMemoryError<T>(address);
    73 		}
    74 	}
    75 
    76 	/**
    77 	 * Writes data to current position in memory and increments the index (position).
    78 	 * @param value value to be written at current position
    79 	 */
    80 	template<typename T> void write(const T value) {
    81 		if (index + sizeof (T) <= memorySize) {
    82 			T * m = reinterpret_cast<T*> (memory + index);
    83 			*m = value;
    84 			index += sizeof (value);
    85 		} else {
    86 			logMemoryError<T>(index);
    87 		}
    88 	}
    89 
    90 	/**
    91 	 * Writes data to given position in memory (without affecting the current position).
    92 	 * @param value value to be written at given position
    93 	 */
    94 	template<typename T> void write(const address_t &address, const T value) {
    95 		if (address + sizeof (T) <= memorySize) {
    96 			T * m = reinterpret_cast<T*> (memory + address);
    97 			*m = value;
    98 		} else {
    99 			logMemoryError<T>(address);
   100 		}
   101 	}
   102 
   103 	/**
   104 	 * Set current addres to given position.
   105 	 * @param index
   106 	 */
   107 	void setAddress(address_t &index) {
   108 		this->index = index;
   109 	}
   110 
   111 	/**
   112 	 * @return Current position in the memory.
   113 	 */
   114 	address_t getAddress() {
   115 		return index;
   116 	}
   117 
   118 	/**
   119 	 * @return whether the current position is inside the memory boundaries = still processing.
   120 	 */
   121 	bool isNotOver() {
   122 		return index < memorySize;
   123 	}
   124 
   125 	/**
   126 	 * Set current position to the start of the memory = position 0.
   127 	 */
   128 	void setAddressToBeginning() {
   129 		index = 0;
   130 	}
   131 
   132 	/**
   133 	 * Set current position behind the end of the memory = stop processing.
   134 	 */
   135 	void setAddressToEnd() {
   136 		index = memorySize;
   137 	}
   138 };