Cleaning up

This commit is contained in:
Zoe Roux
2021-07-04 02:56:44 +02:00
57 changed files with 5597 additions and 5173 deletions

View File

@@ -4,43 +4,41 @@
#include <algorithm>
#include <iostream>
#include "MemoryBus.hpp"
#include "../SNES.hpp"
#include "MemoryShadow.hpp"
#include "RectangleShadow.hpp"
#include "../Exceptions/InvalidAddress.hpp"
#include "SNES.hpp"
#include "Memory/MemoryBus.hpp"
#include "Memory/MemoryShadow.hpp"
#include "Exceptions/InvalidAddress.hpp"
namespace ComSquare::Memory
{
std::shared_ptr<IMemory> MemoryBus::getAccessor(uint24_t addr)
IMemory *MemoryBus::getAccessor(uint24_t addr)
{
auto it = std::find_if(this->_memoryAccessors.begin(), this->_memoryAccessors.end(), [addr](std::shared_ptr<IMemory> &accessor)
auto it = std::find_if(this->_memoryAccessors.begin(), this->_memoryAccessors.end(), [addr](IMemory &accessor)
{
return accessor->hasMemoryAt(addr);
return accessor.hasMemoryAt(addr);
});
if (it == this->_memoryAccessors.end())
return nullptr;
return *it;
return &it->get();
}
uint8_t MemoryBus::read(uint24_t addr)
{
std::shared_ptr<IMemory> handler = this->getAccessor(addr);
IMemory *handler = this->getAccessor(addr);
if (!handler) {
std::cout << "Unknown memory accessor for address $" << std::hex << addr << ". Using open bus." << std::endl;
return this->_openBus;
}
uint8_t data = handler->read(handler->getRelativeAddress(addr));
this->_openBus = data;
return data;
}
uint8_t MemoryBus::read(uint24_t addr, bool silence)
uint8_t MemoryBus::peek(uint24_t addr)
{
if (!silence)
return this->read(addr);
std::shared_ptr<IMemory> handler = this->getAccessor(addr);
IMemory *handler = this->getAccessor(addr);
if (!handler)
return this->_openBus;
@@ -53,7 +51,7 @@ namespace ComSquare::Memory
void MemoryBus::write(uint24_t addr, uint8_t data)
{
std::shared_ptr<IMemory> handler = this->getAccessor(addr);
IMemory *handler = this->getAccessor(addr);
if (!handler) {
std::cout << "Unknown memory accessor for address " << std::hex << addr << ". Warning, it was a write." << std::endl;
@@ -64,28 +62,30 @@ namespace ComSquare::Memory
void MemoryBus::_mirrorComponents(SNES &console, unsigned i)
{
this->_memoryAccessors.emplace_back(new Memory::RectangleShadow(console.wram, i, i, 0x0000, 0x1FFF));
this->_memoryAccessors.emplace_back(new Memory::MemoryShadow(console.ppu, (i << 16u) + 0x2100, (i << 16u) + 0x213F));
this->_memoryAccessors.emplace_back(new Memory::MemoryShadow(console.apu, (i << 16u) + 0x2140, (i << 16u) + 0x2143));
this->_memoryAccessors.emplace_back(new Memory::MemoryShadow(console.cpu, (i << 16u) + 0x4200, (i << 16u) + 0x421F));
this->_rectangleShadows.emplace_back(console.wram, i, i, 0x0000, 0x1FFF);
this->_shadows.emplace_back(console.ppu, (i << 16u) + 0x2100, (i << 16u) + 0x213F);
this->_shadows.emplace_back(console.apu, (i << 16u) + 0x2140, (i << 16u) + 0x2143);
this->_shadows.emplace_back(console.cpu, (i << 16u) + 0x4200, (i << 16u) + 0x421F);
}
void MemoryBus::mapComponents(SNES &console)
{
this->_memoryAccessors.clear();
this->_shadows.clear();
this->_rectangleShadows.clear();
// The WRam and PU registers are always mapped at the same address no matter the mapping mode.
console.wram->setMemoryRegion(0x7E, 0x7F, 0x0000, 0xFFFF);
this->_memoryAccessors.push_back(console.wram);
console.wram.setMemoryRegion(0x7E, 0x7F, 0x0000, 0xFFFF);
this->_memoryAccessors.emplace_back(console.wram);
console.ppu->setMemoryRegion(0x2100, 0x213F);
this->_memoryAccessors.push_back(console.ppu);
console.ppu.setMemoryRegion(0x2100, 0x213F);
this->_memoryAccessors.emplace_back(console.ppu);
console.apu->setMemoryRegion(0x2140, 0x2143);
this->_memoryAccessors.push_back(console.apu);
console.apu.setMemoryRegion(0x2140, 0x2143);
this->_memoryAccessors.emplace_back(console.apu);
console.cpu->setMemoryRegion(0x4200, 0x44FF);
this->_memoryAccessors.push_back(console.cpu);
console.cpu.setMemoryRegion(0x4200, 0x44FF);
this->_memoryAccessors.emplace_back(console.cpu);
// TODO implement Joys.
@@ -96,27 +96,27 @@ namespace ComSquare::Memory
for (uint8_t i = 0x80; i < 0xC0; i += 0x01)
this->_mirrorComponents(console, i);
if (console.cartridge->header.mappingMode & Cartridge::LoRom) {
console.cartridge->setMemoryRegion(0x80, 0xFF, 0x8000, 0xFFFF);
this->_memoryAccessors.push_back(console.cartridge);
if (console.cartridge.header.mappingMode & Cartridge::LoRom) {
console.cartridge.setMemoryRegion(0x80, 0xFF, 0x8000, 0xFFFF);
this->_memoryAccessors.emplace_back(console.cartridge);
// Mirror on the Q1 and Q2.
this->_memoryAccessors.emplace_back(new Memory::RectangleShadow(console.cartridge, 0x00, 0x7D, 0x8000, 0xFFFF));
this->_rectangleShadows.emplace_back(console.cartridge, 0x00, 0x7D, 0x8000, 0xFFFF);
// Mirror on the lower half of the Q2.
this->_memoryAccessors.emplace_back((new Memory::RectangleShadow(console.cartridge, 0x40, 0x6F, 0x0000, 0x7FFF))->setBankOffset(0x40));
this->_rectangleShadows.emplace_back(console.cartridge, 0x40, 0x6F, 0x0000, 0x7FFF).setBankOffset(0x40);
// Mirror on the lower half of the Q4
this->_memoryAccessors.emplace_back((new Memory::RectangleShadow(console.cartridge, 0xC0, 0xEF, 0x0000, 0x7FFF))->setBankOffset(0x40));
this->_rectangleShadows.emplace_back(console.cartridge, 0xC0, 0xEF, 0x0000, 0x7FFF).setBankOffset(0x40);
console.sram->setMemoryRegion(0xF0, 0xFD, 0x0000, 0x7FFF);
this->_memoryAccessors.push_back(console.sram);
this->_memoryAccessors.emplace_back(new Memory::RectangleShadow(console.sram, 0x70, 0x7D, 0x0000, 0x7FFF));
console.sram.setMemoryRegion(0xF0, 0xFD, 0x0000, 0x7FFF);
this->_memoryAccessors.emplace_back(console.sram);
this->_rectangleShadows.emplace_back(console.sram, 0x70, 0x7D, 0x0000, 0x7FFF);
// TODO implement the SRam accessor for the FE/FF
}
// TODO should implement HiRom.
}
bool MemoryBus::isDebugger()
{
return false;
for (auto &shadow : this->_shadows)
this->_memoryAccessors.emplace_back(shadow);
for (auto &shadow : this->_rectangleShadows)
this->_memoryAccessors.emplace_back(shadow);
}
}