Make the base BUS architecture a little easier to work with.

Signed-off-by: Adrian.Conlon <adrian.conlon@gmail.com>
This commit is contained in:
Adrian.Conlon 2017-09-07 00:53:22 +01:00
parent 64b7335a79
commit 0665de5951
4 changed files with 29 additions and 12 deletions

View File

@ -6,12 +6,13 @@
namespace EightBit {
class Memory {
public:
Memory(size_t size)
Memory(size_t size = 0)
: m_bytes(size) {
}
void load(const std::string& path, uint16_t offset = 0) {
loadBinary(path, m_bytes, offset, m_bytes.size() - offset);
int load(const std::string& path, int writeOffset = 0, int readOffset = 0, int limit = -1) {
const auto maximumSize = (int)m_bytes.size() - writeOffset;
return loadBinary(path, m_bytes, writeOffset, readOffset, limit, maximumSize);
}
protected:
@ -26,6 +27,12 @@ namespace EightBit {
}
private:
static int loadBinary(const std::string& path, std::vector<uint8_t>& output, int offset = 0, int maximumSize = -1);
static int loadBinary(
const std::string& path,
std::vector<uint8_t>& output,
int writeOffset,
int readOffset,
int limit,
int maximumSize);
};
}

View File

@ -6,7 +6,7 @@
namespace EightBit {
class Ram : public Memory {
public:
Ram(size_t size)
Ram(size_t size = 0)
: Memory(size) {
}

View File

@ -6,7 +6,7 @@
namespace EightBit {
class Rom : public Memory {
public:
Rom(size_t size)
Rom(size_t size = 0)
: Memory(size) {
}

View File

@ -4,22 +4,32 @@
#include <iostream>
#include <fstream>
int EightBit::Memory::loadBinary(const std::string& path, std::vector<uint8_t>& output, int offset, int maximumSize) {
int EightBit::Memory::loadBinary(
const std::string& path,
std::vector<uint8_t>& output,
int writeOffset,
int readOffset,
int limit,
int maximumSize) {
std::ifstream file;
file.exceptions(std::ios::failbit | std::ios::badbit);
file.open(path, std::ios::binary | std::ios::ate);
auto size = (int)file.tellg();
if ((maximumSize > 0) && (size > maximumSize))
const auto size = (int)file.tellg();
if ((maximumSize > 0) && ((size - readOffset) > maximumSize))
throw std::runtime_error("Binary cannot fit");
size_t extent = size + offset;
size_t extent = size + writeOffset;
if (output.size() < extent)
output.resize(extent);
file.seekg(0, std::ios::beg);
file.seekg(readOffset, std::ios::beg);
file.read((char*)&output[offset], size);
if ((limit < 0) || (limit > size))
limit = size;
file.read((char*)&output[writeOffset], limit);
file.close();
return size;