3 * Copyright © 2017 František Kučera (frantovo.cz)
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.
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.
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/>.
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
38 Memory(const address_t memorySize) {
39 this->memory = (octet_t*) malloc(memorySize);
40 this->memorySize = memorySize;
49 * Reads data on current position in memory and increments the index (position).
50 * @return value found at current position
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);
59 return logMemoryError<T>(index);
64 * Reads data on given position in memory (without affecting the current position).
65 * @return value found at given position
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));
72 return logMemoryError<T>(address);
77 * Writes data to current position in memory and increments the index (position).
78 * @param value value to be written at current position
80 template<typename T> void write(const T value) {
81 if (index + sizeof (T) <= memorySize) {
82 T * m = reinterpret_cast<T*> (memory + index);
84 index += sizeof (value);
86 logMemoryError<T>(index);
91 * Writes data to given position in memory (without affecting the current position).
92 * @param value value to be written at given position
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);
99 logMemoryError<T>(address);
104 * Set current addres to given position.
107 void setAddress(address_t index) {
112 * @return Current position in the memory.
114 address_t getAddress() {
119 * @return whether the current position is inside the memory boundaries = still processing.
122 return index < memorySize;
126 * Set current position to the start of the memory = position 0.
128 void setAddressToBeginning() {
133 * Set current position behind the end of the memory = stop processing.
135 void setAddressToEnd() {