From 0665de5951d9b1ea45d36a34c99da6c389e73c4e Mon Sep 17 00:00:00 2001 From: "Adrian.Conlon" Date: Thu, 7 Sep 2017 00:53:22 +0100 Subject: [PATCH] Make the base BUS architecture a little easier to work with. Signed-off-by: Adrian.Conlon --- inc/Memory.h | 15 +++++++++++---- inc/Ram.h | 2 +- inc/Rom.h | 2 +- src/Memory.cpp | 22 ++++++++++++++++------ 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/inc/Memory.h b/inc/Memory.h index dbf0079..c53a609 100644 --- a/inc/Memory.h +++ b/inc/Memory.h @@ -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& output, int offset = 0, int maximumSize = -1); + static int loadBinary( + const std::string& path, + std::vector& output, + int writeOffset, + int readOffset, + int limit, + int maximumSize); }; } \ No newline at end of file diff --git a/inc/Ram.h b/inc/Ram.h index 0a01af0..45526a5 100644 --- a/inc/Ram.h +++ b/inc/Ram.h @@ -6,7 +6,7 @@ namespace EightBit { class Ram : public Memory { public: - Ram(size_t size) + Ram(size_t size = 0) : Memory(size) { } diff --git a/inc/Rom.h b/inc/Rom.h index e6235f9..5557de1 100644 --- a/inc/Rom.h +++ b/inc/Rom.h @@ -6,7 +6,7 @@ namespace EightBit { class Rom : public Memory { public: - Rom(size_t size) + Rom(size_t size = 0) : Memory(size) { } diff --git a/src/Memory.cpp b/src/Memory.cpp index 3542922..3057297 100644 --- a/src/Memory.cpp +++ b/src/Memory.cpp @@ -4,22 +4,32 @@ #include #include -int EightBit::Memory::loadBinary(const std::string& path, std::vector& output, int offset, int maximumSize) { +int EightBit::Memory::loadBinary( + const std::string& path, + std::vector& 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;