1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/c++/rgb-assembler/Memory.h Mon Dec 25 00:34:27 2017 +0100
1.3 @@ -0,0 +1,106 @@
1.4 +/**
1.5 + * RGB assembler
1.6 + * Copyright © 2017 František Kučera (frantovo.cz)
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +
1.22 +#pragma once
1.23 +
1.24 +#include "types.h"
1.25 +
1.26 +const address_t MEMORY_SIZE = 1024;
1.27 +
1.28 +class Memory {
1.29 +private:
1.30 +
1.31 + template<typename T> T logMemoryError(const address_t &index) {
1.32 + wprintf(L"memory error: index = %d, sizeof(T) = %d, MEMORY_SIZE = %d\n", index, sizeof (T), MEMORY_SIZE);
1.33 + // TODO: return error value or throw exception
1.34 + return T();
1.35 + }
1.36 + octet_t * memory;
1.37 + address_t index;
1.38 +
1.39 +public:
1.40 +
1.41 + Memory() {
1.42 + memory = (octet_t*) malloc(MEMORY_SIZE);
1.43 + index = 0;
1.44 + }
1.45 +
1.46 + virtual ~Memory() {
1.47 + free(memory);
1.48 + memory = nullptr;
1.49 + }
1.50 +
1.51 + /**
1.52 + * Reads data on given position in memory and increments the index (position).
1.53 + * @return value found at current position
1.54 + */
1.55 + template<typename T> T read() {
1.56 + // TODO: map higher memory to static hardcoded areas or peripherals
1.57 + if (index + sizeof (T) <= MEMORY_SIZE) {
1.58 + T * value = reinterpret_cast<T*> (memory + index);
1.59 + index += sizeof (T);
1.60 + return *value;
1.61 + } else {
1.62 + return logMemoryError<T>(index);
1.63 + }
1.64 + }
1.65 +
1.66 + /**
1.67 + * Writes data to current position in memory and increments the index (position).
1.68 + * @param value value to be written at given position
1.69 + */
1.70 + template<typename T> T write(const T value) {
1.71 + if (index + sizeof (T) <= MEMORY_SIZE) {
1.72 + T * m = reinterpret_cast<T*> (memory + index);
1.73 + *m = value;
1.74 + index += sizeof (value);
1.75 + } else {
1.76 + return logMemoryError<T>(index);
1.77 + }
1.78 + }
1.79 +
1.80 + void setIndex(address_t &index) {
1.81 + this->index = index;
1.82 + }
1.83 +
1.84 + address_t getIndex() {
1.85 + return index;
1.86 + }
1.87 +
1.88 + /**
1.89 + * FIXME: rename
1.90 + * @return
1.91 + */
1.92 + bool isInside() {
1.93 + return index < MEMORY_SIZE;
1.94 + }
1.95 +
1.96 + /**
1.97 + * FIXME: rename, refactor
1.98 + */
1.99 + void start() {
1.100 + index = 0;
1.101 + }
1.102 +
1.103 + /**
1.104 + * FIXME: rename, refactor
1.105 + */
1.106 + void finish() {
1.107 + index = MEMORY_SIZE;
1.108 + }
1.109 +};