Browse Source

WIP: Memory mapper.

* 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
parent
commit
0fe2315003
  1. 3
      CMakeLists.txt
  2. 24
      libplip/PlipMemory.h
  3. 58
      libplip/PlipMemoryMap.cpp
  4. 33
      libplip/PlipMemoryMap.h

3
CMakeLists.txt

@ -4,7 +4,7 @@ set(gui_name "plip-sdl")
set(lib_name "plip")
project(${gui_name})
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)
find_package(SDL2 REQUIRED)
@ -35,6 +35,7 @@ add_library(${lib_name}
libplip/Plip.cpp
libplip/PlipInput.cpp
libplip/PlipInputDefinition.cpp
libplip/PlipMemoryMap.cpp
)
add_dependencies(${lib_name} GENERATE_LIB_VERSION_HEADER)

24
libplip/PlipMemory.h

@ -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;
};
}

58
libplip/PlipMemoryMap.cpp

@ -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);
}
}

33
libplip/PlipMemoryMap.h

@ -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…
Cancel
Save