EightBit/inc/Memory.h
Adrian Conlon 9524cf4c6b Adjust memory loaders, to allow easier usage (default arguments)
Signed-off-by: Adrian Conlon <Adrian.conlon@gmail.com>
2018-01-25 22:24:08 +00:00

52 lines
1.5 KiB
C++

#pragma once
#include <cstdint>
#include <vector>
#include <string>
#include <fstream>
namespace EightBit {
class Memory {
public:
static int load(std::ifstream& file, std::vector<uint8_t>& output, int writeOffset = 0, int readOffset = 0, int limit = -1, int maximumSize = -1);
static int load(const std::string& path, std::vector<uint8_t>& output, int writeOffset = 0, int readOffset = 0, int limit = -1, int maximumSize = -1);
Memory(size_t size = 0)
: m_bytes(size) {
}
size_t size() const { return m_bytes.size(); }
int load(std::ifstream& file, int writeOffset = 0, int readOffset = 0, int limit = -1) {
const auto maximumSize = (int)m_bytes.size() - writeOffset;
return load(file, m_bytes, writeOffset, readOffset, limit, maximumSize);
}
int load(const std::string& path, int writeOffset = 0, int readOffset = 0, int limit = -1) {
const auto maximumSize = (int)m_bytes.size() - writeOffset;
return load(path, m_bytes, writeOffset, readOffset, limit, maximumSize);
}
int load(const std::vector<uint8_t>& bytes, int writeOffset = 0, int readOffset = 0, int limit = -1) {
if (limit < 0)
limit = (int)bytes.size() - readOffset;
std::copy(bytes.cbegin() + readOffset, bytes.cbegin() + limit, m_bytes.begin() + writeOffset);
return limit;
}
protected:
std::vector<uint8_t>& BYTES() { return m_bytes; }
uint8_t read(uint16_t address) const {
return m_bytes[address];
}
void write(uint16_t address, uint8_t value) {
m_bytes[address] = value;
}
private:
std::vector<uint8_t> m_bytes;
};
}