Introduce the concept of a MemoryInterface to the EightBit library.

Signed-off-by: Adrian Conlon <Adrian.conlon@gmail.com>
This commit is contained in:
Adrian Conlon 2018-11-03 22:15:17 +00:00
parent 68a785ceec
commit 984626a331
6 changed files with 23 additions and 15 deletions

View File

@ -5,7 +5,8 @@
#include <iomanip>
#include <bitset>
#include "Memory.h"
#include <Memory.h>
#include "LR35902.h"
#include "IoRegisters.h"

View File

@ -5,8 +5,10 @@
#include <string>
#include <fstream>
#include "MemoryInterface.h"
namespace EightBit {
class Memory {
class Memory : public MemoryInterface {
private:
std::vector<uint8_t> m_bytes;
@ -14,7 +16,7 @@ namespace EightBit {
const auto& BYTES() const { return m_bytes; }
auto& BYTES() { return m_bytes; }
void poke(const uint16_t address, const uint8_t value) {
virtual void poke(const uint16_t address, const uint8_t value) override {
BYTES()[address] = value;
}
@ -26,26 +28,26 @@ namespace EightBit {
: m_bytes(size) {
}
auto size() const { return m_bytes.size(); }
virtual size_t size() const final { return m_bytes.size(); }
auto load(std::ifstream& file, const int writeOffset = 0, const int readOffset = 0, const int limit = -1) {
const auto maximumSize = (int)m_bytes.size() - writeOffset;
virtual int load(std::ifstream& file, const int writeOffset = 0, const int readOffset = 0, const int limit = -1) final {
const auto maximumSize = (int)size() - writeOffset;
return load(file, m_bytes, writeOffset, readOffset, limit, maximumSize);
}
auto load(const std::string& path, const int writeOffset = 0, const int readOffset = 0, const int limit = -1) {
const auto maximumSize = (int)m_bytes.size() - writeOffset;
virtual int load(const std::string& path, const int writeOffset = 0, const int readOffset = 0, const int limit = -1) final {
const auto maximumSize = (int)size() - writeOffset;
return load(path, m_bytes, writeOffset, readOffset, limit, maximumSize);
}
auto load(const std::vector<uint8_t>& bytes, const int writeOffset = 0, const int readOffset = 0, int limit = -1) {
virtual int load(const std::vector<uint8_t>& bytes, const int writeOffset = 0, const int readOffset = 0, int limit = -1) final {
if (limit < 0)
limit = (int)bytes.size() - readOffset;
std::copy(bytes.cbegin() + readOffset, bytes.cbegin() + limit, m_bytes.begin() + writeOffset);
return limit;
}
auto peek(const uint16_t address) const {
virtual uint8_t peek(const uint16_t address) const final {
return BYTES()[address];
}
};

View File

@ -2,14 +2,15 @@
#include <cstdint>
#include "Memory.h"
namespace EightBit {
class MemoryInterface;
struct MemoryMapping {
enum AccessLevel { Unknown, ReadOnly, ReadWrite, };
Memory& memory;
MemoryInterface& memory;
uint16_t begin = 0xffff;
uint16_t mask = 0U;
AccessLevel access = Unknown;

View File

@ -10,11 +10,11 @@ namespace EightBit {
: Memory(size) {
}
auto& reference(const uint16_t address) {
virtual uint8_t& reference(const uint16_t address) final {
return BYTES()[address];
}
void poke(const uint16_t address, const uint8_t value) {
virtual void poke(const uint16_t address, const uint8_t value) final {
Memory::poke(address, value);
}
};

View File

@ -149,6 +149,7 @@
<ClInclude Include="..\inc\IntelProcessor.h" />
<ClInclude Include="..\inc\LittleEndianProcessor.h" />
<ClInclude Include="..\inc\Memory.h" />
<ClInclude Include="..\inc\MemoryInterface.h" />
<ClInclude Include="..\inc\MemoryMapping.h" />
<ClInclude Include="..\inc\Processor.h" />
<ClInclude Include="..\inc\Ram.h" />

View File

@ -59,6 +59,9 @@
<ClInclude Include="..\inc\Chip.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\inc\MemoryInterface.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">