Add a span compatible ROM loader

This commit is contained in:
Adrian Conlon 2021-12-26 22:01:12 +00:00
parent 4f4d234ed5
commit af7679505c
2 changed files with 17 additions and 11 deletions

View File

@ -35,6 +35,22 @@ namespace EightBit {
int load(std::string path, int writeOffset = 0, int readOffset = 0, int limit = -1) final;
int load(const std::vector<uint8_t>& bytes, int writeOffset = 0, int readOffset = 0, int limit = -1) final;
template <typename Iter>
int load(Iter start, Iter end, int writeOffset = 0, int readOffset = 0, int limit = -1) {
const auto size = end - start;
if (limit < 0)
limit = size - readOffset;
const size_t extent = limit + writeOffset;
if (m_bytes.size() < extent)
m_bytes.resize(extent);
std::copy(start + readOffset, start + readOffset + limit, m_bytes.begin() + writeOffset);
return limit;
}
[[nodiscard]] uint8_t peek(uint16_t address) const noexcept final;
};
}

View File

@ -68,17 +68,7 @@ int EightBit::Rom::load(const std::string path, const int writeOffset, const int
}
int EightBit::Rom::load(const std::vector<uint8_t>& bytes, const int writeOffset, const int readOffset, int limit) {
if (limit < 0)
limit = bytes.size() - readOffset;
const size_t extent = limit + writeOffset;
if (m_bytes.size() < extent)
m_bytes.resize(extent);
std::copy(bytes.cbegin() + readOffset, bytes.cbegin() + readOffset + limit, m_bytes.begin() + writeOffset);
return limit;
return load(bytes.cbegin(), bytes.cend(), writeOffset, readOffset, limit);
}
uint8_t EightBit::Rom::peek(const uint16_t address) const noexcept {