2018-11-04 16:38:57 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
#include "Memory.h"
|
|
|
|
|
|
|
|
namespace EightBit {
|
|
|
|
// ROM is a basic implementation of the Memory interface.
|
|
|
|
// Nothing over and above the interface is exposed to users
|
|
|
|
// of the ROM class.
|
|
|
|
class Rom : public Memory {
|
|
|
|
private:
|
|
|
|
std::vector<uint8_t> m_bytes;
|
|
|
|
|
|
|
|
protected:
|
2021-08-23 09:44:48 +01:00
|
|
|
[[nodiscard]] constexpr const auto& BYTES() const noexcept { return m_bytes; }
|
|
|
|
[[nodiscard]] constexpr auto& BYTES() noexcept { return m_bytes; }
|
2018-11-04 16:38:57 +00:00
|
|
|
|
2021-07-18 14:28:40 +01:00
|
|
|
void poke(uint16_t address, uint8_t value) noexcept override;
|
2018-11-04 16:38:57 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
static int load(std::ifstream& file, std::vector<uint8_t>& output, int writeOffset = 0, int readOffset = 0, int limit = -1, int maximumSize = -1);
|
2020-11-07 09:41:12 +00:00
|
|
|
static int load(std::string path, std::vector<uint8_t>& output, int writeOffset = 0, int readOffset = 0, int limit = -1, int maximumSize = -1);
|
2018-11-04 16:38:57 +00:00
|
|
|
|
2021-07-18 14:28:40 +01:00
|
|
|
Rom(size_t size = 0) noexcept;
|
2021-08-23 09:45:40 +01:00
|
|
|
Rom(const Rom& rhs);
|
2024-01-13 10:24:21 +00:00
|
|
|
virtual ~Rom() = default;
|
2021-08-23 09:45:40 +01:00
|
|
|
Rom& operator=(const Rom& rhs);
|
2021-12-27 14:24:38 +00:00
|
|
|
bool operator==(const Rom& rhs) const;
|
2018-11-04 16:38:57 +00:00
|
|
|
|
2022-01-03 12:29:32 +00:00
|
|
|
[[nodiscard]] uint16_t size() const noexcept final;
|
2018-11-04 16:38:57 +00:00
|
|
|
|
2018-11-27 22:36:54 +00:00
|
|
|
int load(std::ifstream& file, int writeOffset = 0, int readOffset = 0, int limit = -1) final;
|
2020-11-07 09:41:12 +00:00
|
|
|
int load(std::string path, int writeOffset = 0, int readOffset = 0, int limit = -1) final;
|
2018-11-27 22:36:54 +00:00
|
|
|
int load(const std::vector<uint8_t>& bytes, int writeOffset = 0, int readOffset = 0, int limit = -1) final;
|
2018-11-04 16:38:57 +00:00
|
|
|
|
2021-12-26 22:01:12 +00:00
|
|
|
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)
|
2024-01-13 10:24:21 +00:00
|
|
|
limit = static_cast<int>(size) - readOffset;
|
2021-12-26 22:01:12 +00:00
|
|
|
|
2024-01-11 09:15:52 +00:00
|
|
|
const size_t extent = static_cast<size_t>(limit) + writeOffset;
|
2021-12-26 22:01:12 +00:00
|
|
|
if (m_bytes.size() < extent)
|
|
|
|
m_bytes.resize(extent);
|
|
|
|
|
|
|
|
std::copy(start + readOffset, start + readOffset + limit, m_bytes.begin() + writeOffset);
|
|
|
|
|
|
|
|
return limit;
|
|
|
|
}
|
|
|
|
|
2021-07-18 14:28:40 +01:00
|
|
|
[[nodiscard]] uint8_t peek(uint16_t address) const noexcept final;
|
2018-11-04 16:38:57 +00:00
|
|
|
};
|
|
|
|
}
|