Browse Source
* Bumped required C++ version up to C++17. * Memory mapper has barebones functionality. * Base memory virtual class added. No implementations have been written yet.master
Ian Burgmyer
4 years ago
4 changed files with 117 additions and 1 deletions
@ -0,0 +1,24 @@ |
|||||||
|
/* PlipMemory.h
|
||||||
|
* |
||||||
|
* An interface representing some type of memory. |
||||||
|
*/ |
||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <cstdint> |
||||||
|
|
||||||
|
namespace Plip { |
||||||
|
class PlipMemory { |
||||||
|
public: |
||||||
|
uint8_t operator[] (uint32_t offset) { |
||||||
|
return GetByte(offset); |
||||||
|
}; |
||||||
|
|
||||||
|
virtual uint8_t GetByte(uint32_t address) = 0; |
||||||
|
virtual uint32_t GetLength() = 0; |
||||||
|
virtual void SetByte(uint32_t address, uint8_t value) = 0; |
||||||
|
|
||||||
|
protected: |
||||||
|
PlipMemory() = default; |
||||||
|
}; |
||||||
|
} |
@ -0,0 +1,58 @@ |
|||||||
|
/* PlipMemoryMap.cpp
|
||||||
|
* |
||||||
|
* A flexible memory mapper. |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <tuple> |
||||||
|
|
||||||
|
#include "PlipMemoryMap.h" |
||||||
|
|
||||||
|
namespace Plip { |
||||||
|
void PlipMemoryMap::AddBlock(PlipMemory *memory, uint32_t offset, uint32_t length) { |
||||||
|
uint32_t start = 0; |
||||||
|
|
||||||
|
if(m_range.begin() != m_range.end()) { |
||||||
|
auto last = m_range.back(); |
||||||
|
start = last.startAddress + last.length; |
||||||
|
} |
||||||
|
|
||||||
|
m_range.push_back({ |
||||||
|
.startAddress = start, |
||||||
|
.memory = memory, |
||||||
|
.offset = offset, |
||||||
|
.length = length |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
std::tuple<PlipMemory*, uint32_t> PlipMemoryMap::FindAddress(uint32_t address) { |
||||||
|
for(auto const &memory : m_range) { |
||||||
|
if(address < memory.startAddress || address > memory.startAddress + memory.length) |
||||||
|
continue; |
||||||
|
|
||||||
|
return { memory.memory, address - memory.startAddress }; |
||||||
|
} |
||||||
|
|
||||||
|
return { nullptr, 0 }; |
||||||
|
} |
||||||
|
|
||||||
|
uint8_t PlipMemoryMap::GetByte(uint32_t address) { |
||||||
|
auto [ memory, offset ] = FindAddress(address); |
||||||
|
|
||||||
|
if(memory == nullptr) return 0; |
||||||
|
return memory->GetByte(offset); |
||||||
|
} |
||||||
|
|
||||||
|
uint32_t PlipMemoryMap::GetLength() { |
||||||
|
if(m_range.begin() == m_range.end()) return 0; |
||||||
|
|
||||||
|
auto last = m_range.back(); |
||||||
|
return last.startAddress + last.length; |
||||||
|
} |
||||||
|
|
||||||
|
void PlipMemoryMap::SetByte(uint32_t address, uint8_t value) { |
||||||
|
auto [ memory, offset ] = FindAddress(address); |
||||||
|
|
||||||
|
if(memory == nullptr) return; |
||||||
|
memory->SetByte(offset, value); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,33 @@ |
|||||||
|
/* PlipMemoryMap.h
|
||||||
|
* |
||||||
|
* A flexible memory mapper. |
||||||
|
*/ |
||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <cstdint> |
||||||
|
#include <list> |
||||||
|
|
||||||
|
#include "PlipMemory.h" |
||||||
|
|
||||||
|
namespace Plip { |
||||||
|
struct PlipMemoryMapRange { |
||||||
|
uint32_t startAddress; |
||||||
|
PlipMemory *memory; |
||||||
|
uint32_t offset; |
||||||
|
uint32_t length; |
||||||
|
}; |
||||||
|
|
||||||
|
class PlipMemoryMap { |
||||||
|
public: |
||||||
|
void AddBlock(PlipMemory *memory, uint32_t offset, uint32_t length); |
||||||
|
uint8_t GetByte(uint32_t address); |
||||||
|
uint32_t GetLength(); |
||||||
|
void SetByte(uint32_t address, uint8_t value); |
||||||
|
|
||||||
|
private: |
||||||
|
std::tuple<PlipMemory*, uint32_t> FindAddress(uint32_t address); |
||||||
|
|
||||||
|
std::list<PlipMemoryMapRange> m_range; |
||||||
|
}; |
||||||
|
} |
Loading…
Reference in new issue