2017-06-04 21:38:34 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdint>
|
2017-09-06 13:22:23 +01:00
|
|
|
#include <vector>
|
2017-10-22 21:24:28 +01:00
|
|
|
#include <string>
|
2017-12-26 22:46:16 +00:00
|
|
|
#include <fstream>
|
2017-06-04 23:37:25 +01:00
|
|
|
|
2017-06-04 21:38:34 +01:00
|
|
|
namespace EightBit {
|
|
|
|
class Memory {
|
|
|
|
public:
|
2018-01-25 22:24:08 +00:00
|
|
|
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);
|
2017-12-26 22:46:16 +00:00
|
|
|
|
2018-08-07 23:06:15 +01:00
|
|
|
Memory(const size_t size = 0) noexcept
|
2017-09-06 13:22:23 +01:00
|
|
|
: m_bytes(size) {
|
2017-07-06 21:32:52 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 15:00:57 +01:00
|
|
|
size_t size() const { return m_bytes.size(); }
|
|
|
|
|
2018-04-14 09:39:06 +01:00
|
|
|
int load(std::ifstream& file, const int writeOffset = 0, const int readOffset = 0, const int limit = -1) {
|
2017-12-26 22:46:16 +00:00
|
|
|
const auto maximumSize = (int)m_bytes.size() - writeOffset;
|
|
|
|
return load(file, m_bytes, writeOffset, readOffset, limit, maximumSize);
|
|
|
|
}
|
|
|
|
|
2018-04-14 09:39:06 +01:00
|
|
|
int load(const std::string& path, const int writeOffset = 0, const int readOffset = 0, const int limit = -1) {
|
2017-09-07 00:53:22 +01:00
|
|
|
const auto maximumSize = (int)m_bytes.size() - writeOffset;
|
2017-12-26 22:46:16 +00:00
|
|
|
return load(path, m_bytes, writeOffset, readOffset, limit, maximumSize);
|
|
|
|
}
|
|
|
|
|
2018-04-14 09:39:06 +01:00
|
|
|
int load(const std::vector<uint8_t>& bytes, const int writeOffset = 0, const int readOffset = 0, int limit = -1) {
|
2017-12-26 22:46:16 +00:00
|
|
|
if (limit < 0)
|
|
|
|
limit = (int)bytes.size() - readOffset;
|
|
|
|
std::copy(bytes.cbegin() + readOffset, bytes.cbegin() + limit, m_bytes.begin() + writeOffset);
|
|
|
|
return limit;
|
2017-07-18 21:40:29 +01:00
|
|
|
}
|
|
|
|
|
2018-06-24 20:58:20 +01:00
|
|
|
uint8_t peek(const uint16_t address) {
|
2018-06-10 00:40:56 +01:00
|
|
|
return BYTES()[address];
|
|
|
|
}
|
|
|
|
|
2017-06-04 21:38:34 +01:00
|
|
|
protected:
|
2017-11-03 22:05:01 +00:00
|
|
|
std::vector<uint8_t>& BYTES() { return m_bytes; }
|
2017-06-04 21:38:34 +01:00
|
|
|
|
2018-06-10 00:40:56 +01:00
|
|
|
void poke(const uint16_t address, const uint8_t value) {
|
|
|
|
BYTES()[address] = value;
|
2017-08-06 17:06:48 +01:00
|
|
|
}
|
|
|
|
|
2017-09-06 13:22:23 +01:00
|
|
|
private:
|
2017-11-03 22:05:01 +00:00
|
|
|
std::vector<uint8_t> m_bytes;
|
2017-06-04 21:38:34 +01:00
|
|
|
};
|
2018-06-24 22:33:02 +01:00
|
|
|
|
|
|
|
typedef Memory Rom;
|
2017-11-30 23:19:17 +00:00
|
|
|
}
|