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