c++/rgb-assembler/memory.h
author František Kučera <franta-hg@frantovo.cz>
Mon, 25 Dec 2017 00:24:07 +0100
changeset 31 b997cbf9e30b
parent 29 10d6964e7b4a
permissions -rw-r--r--
wrap memory in a class
     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 const address_t MEMORY_SIZE = 1024;
    24 
    25 class Memory {
    26 private:
    27 
    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
    31 		return T();
    32 	}
    33 	octet_t * memory;
    34 	address_t index;
    35 
    36 public:
    37 
    38 	Memory() {
    39 		memory = (octet_t*) malloc(MEMORY_SIZE);
    40 		index = 0;
    41 	}
    42 
    43 	virtual ~Memory() {
    44 		free(memory);
    45 		memory = nullptr;
    46 	}
    47 
    48 	/**
    49 	 * Reads data on given 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) <= MEMORY_SIZE) {
    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 	 * Writes data to current position in memory and increments the index (position).
    65 	 * @param value value to be written at given position
    66 	 */
    67 	template<typename T> T write(const T value) {
    68 		if (index + sizeof (T) <= MEMORY_SIZE) {
    69 			T * m = reinterpret_cast<T*> (memory + index);
    70 			*m = value;
    71 			index += sizeof (value);
    72 		} else {
    73 			return logMemoryError<T>(index);
    74 		}
    75 	}
    76 
    77 	void setIndex(address_t &index) {
    78 		this->index = index;
    79 	}
    80 
    81 	address_t getIndex() {
    82 		return index;
    83 	}
    84 
    85 	/**
    86 	 * FIXME: rename
    87 	 * @return 
    88 	 */
    89 	bool isInside() {
    90 		return index < MEMORY_SIZE;
    91 	}
    92 	
    93 	/**
    94 	 * FIXME: rename, refactor
    95 	 */
    96 	void start() {
    97 		index = 0;
    98 	}
    99 
   100 	/**
   101 	 * FIXME: rename, refactor
   102 	 */
   103 	void finish() {
   104 		index = MEMORY_SIZE;
   105 	}
   106 };