Max OS 0.3
Loading...
Searching...
No Matches
memoryIO.cpp
Go to the documentation of this file.
1
9#include <memory/memoryIO.h>
10
11using namespace MaxOS::memory;
12
18MemIO::MemIO(uintptr_t address)
19: m_address(address)
20{
21
22}
23
24MemIO::~MemIO() = default;
25
31MemIO8Bit::MemIO8Bit(uintptr_t address)
32: MemIO(address)
33{
34}
35
36MemIO8Bit::~MemIO8Bit() = default;
37
43void MemIO8Bit::write(uint8_t data) {
44
45 (*((volatile uint8_t*) (m_address))) = (data);
46}
47
53uint8_t MemIO8Bit::read() {
54
55 return *((volatile uint8_t*) (m_address));
56}
57
63MemIO16Bit::MemIO16Bit(uintptr_t address)
64: MemIO(address)
65{
66}
67
68MemIO16Bit::~MemIO16Bit() = default;
69
75void MemIO16Bit::write(uint16_t data) {
76
77 (*((volatile uint16_t*) (m_address))) = (data);
78}
79
85uint16_t MemIO16Bit::read() {
86
87 return *((volatile uint16_t*) (m_address));
88}
89
95MemIO32Bit::MemIO32Bit(uintptr_t address)
96: MemIO(address)
97{
98}
99
100MemIO32Bit::~MemIO32Bit() = default;
101
107void MemIO32Bit::write(uint32_t data) {
108
109 (*((volatile uint32_t*) (m_address))) = (data);
110}
111
118
119 return *((volatile uint32_t*) (m_address));
120}
121
127MemIO64Bit::MemIO64Bit(uintptr_t address)
128: MemIO(address)
129{
130}
131
132MemIO64Bit::~MemIO64Bit() = default;
133
139void MemIO64Bit::write(uint64_t data) {
140
141 (*((volatile uint64_t*) (m_address))) = (data);
142}
143
150
151 return *((volatile uint64_t*) (m_address));
152}
153
154
165void* memcpy(void* destination, const void* source, uint64_t num) {
166
167 // Make sure the source and destination are not the same
168 if (destination == source)
169 return destination;
170
171 // Make sure they exist
172 if (destination == nullptr || source == nullptr)
173 return destination;
174
175 // Get the source and destination
176 auto* dst = (unsigned char*) destination;
177 const auto* src = (const unsigned char*) source;
178
179 // Copy the data
180 for (size_t i = 0; i < num; i++)
181 dst[i] = src[i];
182
183 // Usefully for easier code writing
184 return destination;
185}
186
195void* memset(void* ptr, unsigned char value, uint64_t num) {
196
197 // Make sure the pointer exists
198 if (ptr == nullptr)
199 return ptr;
200
201 auto* dst = (unsigned char*) ptr;
202 for (size_t i = 0; i < num; i++)
203 dst[i] = (unsigned char) value;
204 return ptr;
205}
206
215void* memset(void* ptr, uint32_t value, uint64_t num) {
216
217 // Make sure the pointer exists
218 if (ptr == nullptr)
219 return ptr;
220
221 auto* dst = (uint32_t*) ptr;
222 for (size_t i = 0; i < num; i++)
223 dst[i] = value;
224 return ptr;
225}
226
235void* memmove(void* destination, const void* source, uint64_t num) {
236
237 // Make sure the source and destination are not the same
238 if (destination == source)
239 return destination;
240
241 // Make sure they exist
242 if (destination == nullptr || source == nullptr)
243 return destination;
244
245 auto* dst = (unsigned char*) destination;
246 const auto* src = (const unsigned char*) source;
247 if (dst < src) {
248 for (size_t i = 0; i < num; i++)
249 dst[i] = src[i];
250 } else {
251 for (size_t i = num; i != 0; i--)
252 dst[i - 1] = src[i - 1];
253 }
254 return destination;
255}
256
265int memcmp(const void* ptr1, const void* ptr2, uint64_t num) {
266
267 // Make sure the pointers exist
268 if (ptr1 == nullptr || ptr2 == nullptr)
269 return 0;
270
271 const auto* p1 = (const unsigned char*) ptr1;
272 const auto* p2 = (const unsigned char*) ptr2;
273 for (size_t i = 0; i < num; i++) {
274 if (p1[i] < p2[i])
275 return -1;
276 if (p1[i] > p2[i])
277 return 1;
278 }
279 return 0;
280}
MemIO16Bit(uintptr_t address)
Construct a new Mem IO object for 16 bit reads/writes.
Definition memoryIO.cpp:63
virtual uint16_t read()
Reads data from the memory address.
Definition memoryIO.cpp:85
virtual void write(uint16_t data)
Writes data to the memory address.
Definition memoryIO.cpp:75
virtual uint32_t read()
Reads data from the memory address.
Definition memoryIO.cpp:117
MemIO32Bit(uintptr_t address)
Construct a new Mem IO object for 32 bit reads/writes.
Definition memoryIO.cpp:95
virtual void write(uint32_t data)
Writes data to the memory address.
Definition memoryIO.cpp:107
virtual void write(uint64_t data)
Writes data to the memory address.
Definition memoryIO.cpp:139
MemIO64Bit(uintptr_t address)
Construct a new Mem IO object for 64 bit reads/writes.
Definition memoryIO.cpp:127
virtual uint64_t read()
Reads data from the memory address.
Definition memoryIO.cpp:149
virtual uint8_t read()
Reads data from the memory address.
Definition memoryIO.cpp:53
MemIO8Bit(uintptr_t address)
Construct a new Mem IO object for 8 bit reads/writes.
Definition memoryIO.cpp:31
virtual void write(uint8_t data)
Writes data to the memory address.
Definition memoryIO.cpp:43
base class for all memory IO
Definition memoryIO.h:24
uintptr_t m_address
The memory address to read from / write to.
Definition memoryIO.h:26
MemIO(uintptr_t address)
Construct a new Mem IO object.
Definition memoryIO.cpp:18
Defines classes for memory input and output operations of various bit widths (8, 16,...
void * memmove(void *destination, const void *source, uint64_t num)
Copies a block of memory from one location to another.
Definition memoryIO.cpp:235
void * memcpy(void *destination, const void *source, uint64_t num)
Copies a block of memory from one location to another.
Definition memoryIO.cpp:165
int memcmp(const void *ptr1, const void *ptr2, uint64_t num)
Compares two blocks of memory.
Definition memoryIO.cpp:265
void * memset(void *ptr, uint32_t value, uint64_t num)
Fills a block of memory with a specified value.
Definition memoryIO.cpp:215