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/>.
23 const address_t MEMORY_SIZE = 1024;
28 template<typename T> T logMemoryError(const address_t &index) {
29 wprintf(L"memory error: index = %d, sizeof(T) = %d, MEMORY_SIZE = %d\n", index, sizeof (T), MEMORY_SIZE);
30 // TODO: return error value or throw exception
39 memory = (octet_t*) malloc(MEMORY_SIZE);
49 * Reads data on given 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) <= MEMORY_SIZE) {
55 T * value = reinterpret_cast<T*> (memory + index);
59 return logMemoryError<T>(index);
64 * Writes data to current position in memory and increments the index (position).
65 * @param value value to be written at given position
67 template<typename T> T write(const T value) {
68 if (index + sizeof (T) <= MEMORY_SIZE) {
69 T * m = reinterpret_cast<T*> (memory + index);
71 index += sizeof (value);
73 return logMemoryError<T>(index);
77 void setIndex(address_t &index) {
81 address_t getIndex() {
90 return index < MEMORY_SIZE;
94 * FIXME: rename, refactor
101 * FIXME: rename, refactor