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@31
|
49 |
* Reads data on given 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@31
|
64 |
* Writes data to current position in memory and increments the index (position).
|
franta-hg@31
|
65 |
* @param value value to be written at given position
|
franta-hg@31
|
66 |
*/
|
franta-hg@31
|
67 |
template<typename T> T write(const T value) {
|
franta-hg@31
|
68 |
if (index + sizeof (T) <= MEMORY_SIZE) {
|
franta-hg@31
|
69 |
T * m = reinterpret_cast<T*> (memory + index);
|
franta-hg@31
|
70 |
*m = value;
|
franta-hg@31
|
71 |
index += sizeof (value);
|
franta-hg@31
|
72 |
} else {
|
franta-hg@31
|
73 |
return logMemoryError<T>(index);
|
franta-hg@31
|
74 |
}
|
franta-hg@31
|
75 |
}
|
franta-hg@31
|
76 |
|
franta-hg@31
|
77 |
void setIndex(address_t &index) {
|
franta-hg@31
|
78 |
this->index = index;
|
franta-hg@31
|
79 |
}
|
franta-hg@31
|
80 |
|
franta-hg@31
|
81 |
address_t getIndex() {
|
franta-hg@31
|
82 |
return index;
|
franta-hg@31
|
83 |
}
|
franta-hg@31
|
84 |
|
franta-hg@31
|
85 |
/**
|
franta-hg@31
|
86 |
* FIXME: rename
|
franta-hg@31
|
87 |
* @return
|
franta-hg@31
|
88 |
*/
|
franta-hg@31
|
89 |
bool isInside() {
|
franta-hg@31
|
90 |
return index < MEMORY_SIZE;
|
franta-hg@31
|
91 |
}
|
franta-hg@31
|
92 |
|
franta-hg@31
|
93 |
/**
|
franta-hg@31
|
94 |
* FIXME: rename, refactor
|
franta-hg@31
|
95 |
*/
|
franta-hg@31
|
96 |
void start() {
|
franta-hg@31
|
97 |
index = 0;
|
franta-hg@31
|
98 |
}
|
franta-hg@31
|
99 |
|
franta-hg@31
|
100 |
/**
|
franta-hg@31
|
101 |
* FIXME: rename, refactor
|
franta-hg@31
|
102 |
*/
|
franta-hg@31
|
103 |
void finish() {
|
franta-hg@31
|
104 |
index = MEMORY_SIZE;
|
franta-hg@31
|
105 |
}
|
franta-hg@31
|
106 |
};
|