2023-01-13 04:01:11 +00:00
|
|
|
//
|
|
|
|
// MemorySlotHandler.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 12/01/2023.
|
|
|
|
// Copyright © 2023 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "MemorySlotHandler.hpp"
|
|
|
|
|
2023-01-13 04:08:01 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
2023-01-13 04:01:11 +00:00
|
|
|
using namespace MSX;
|
|
|
|
|
2023-01-14 02:54:59 +00:00
|
|
|
MemorySlot::MemorySlot(MemorySlotChangeHandler &handler) : handler_(handler) {
|
2023-01-13 04:01:11 +00:00
|
|
|
for(int subslot = 0; subslot < 4; subslot++) {
|
|
|
|
for(int region = 0; region < 8; region++) {
|
|
|
|
read_pointers_[subslot][region] = unmapped.data();
|
|
|
|
write_pointers_[subslot][region] = scratch.data();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemorySlot::set_secondary_paging(uint8_t value) {
|
|
|
|
secondary_paging_ = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t MemorySlot::secondary_paging() const {
|
|
|
|
return secondary_paging_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t *MemorySlot::read_pointer(int segment) const {
|
|
|
|
const int subslot = (secondary_paging_ >> (segment & ~1)) & 3;
|
|
|
|
return read_pointers_[subslot][segment];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *MemorySlot::write_pointer(int segment) const {
|
|
|
|
const int subslot = (secondary_paging_ >> (segment & ~1)) & 3;
|
|
|
|
return write_pointers_[subslot][segment];
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemorySlot::set_source(const std::vector<uint8_t> &source) {
|
|
|
|
source_ = source;
|
|
|
|
}
|
|
|
|
|
2023-01-13 19:07:54 +00:00
|
|
|
void MemorySlot::resize_source(std::size_t size) {
|
|
|
|
source_.resize(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint8_t> &MemorySlot::source() {
|
|
|
|
return source_;
|
|
|
|
}
|
|
|
|
|
2023-01-13 04:01:11 +00:00
|
|
|
const std::vector<uint8_t> &MemorySlot::source() const {
|
|
|
|
return source_;
|
|
|
|
}
|
|
|
|
|
2023-01-13 14:31:56 +00:00
|
|
|
template <MSX::MemorySlot::AccessType type>
|
2023-01-13 04:01:11 +00:00
|
|
|
void MemorySlot::map(int subslot, std::size_t source_address, uint16_t destination_address, std::size_t length) {
|
|
|
|
assert(!(destination_address & 8191));
|
|
|
|
assert(!(length & 8191));
|
|
|
|
assert(size_t(destination_address) + length <= 65536);
|
|
|
|
|
|
|
|
for(std::size_t c = 0; c < (length >> 13); ++c) {
|
|
|
|
source_address %= source_.size();
|
2023-01-13 14:31:56 +00:00
|
|
|
|
|
|
|
const int bank = int((destination_address >> 13) + c);
|
|
|
|
read_pointers_[subslot][bank] = &source_[source_address];
|
|
|
|
if constexpr (type == AccessType::ReadWrite) {
|
|
|
|
write_pointers_[subslot][bank] = read_pointers_[subslot][bank];
|
|
|
|
}
|
|
|
|
|
2023-01-13 04:01:11 +00:00
|
|
|
source_address += 8192;
|
|
|
|
}
|
|
|
|
|
2023-01-14 02:54:59 +00:00
|
|
|
handler_.did_page();
|
2023-01-13 04:01:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MemorySlot::unmap(int subslot, uint16_t destination_address, std::size_t length) {
|
|
|
|
assert(!(destination_address & 8191));
|
|
|
|
assert(!(length & 8191));
|
|
|
|
assert(size_t(destination_address) + length <= 65536);
|
|
|
|
|
|
|
|
for(std::size_t c = 0; c < (length >> 13); ++c) {
|
|
|
|
read_pointers_[subslot][(destination_address >> 13) + c] = nullptr;
|
|
|
|
}
|
|
|
|
|
2023-01-14 02:54:59 +00:00
|
|
|
handler_.did_page();
|
2023-01-13 04:01:11 +00:00
|
|
|
}
|
2023-01-13 14:31:56 +00:00
|
|
|
|
|
|
|
template void MemorySlot::map<MSX::MemorySlot::AccessType::Read>(int subslot, std::size_t source_address, uint16_t destination_address, std::size_t length);
|
|
|
|
template void MemorySlot::map<MSX::MemorySlot::AccessType::ReadWrite>(int subslot, std::size_t source_address, uint16_t destination_address, std::size_t length);
|