Mesen/Core/MemoryManager.cpp

180 lines
4.7 KiB
C++
Raw Normal View History

2014-06-14 15:27:55 +00:00
#include "stdafx.h"
#include "MemoryManager.h"
#include "BaseMapper.h"
#include "Debugger.h"
#include "CheatManager.h"
#include "Console.h"
MemoryManager::MemoryManager(shared_ptr<Console> console)
{
_console = console;
_internalRAM = new uint8_t[InternalRAMSize];
_internalRamHandler.SetInternalRam(_internalRAM);
for(int i = 0; i < 2; i++) {
2014-07-02 02:56:06 +00:00
_nametableRAM[i] = new uint8_t[NameTableScreenSize];
_console->GetMapper()->InitializeRam(_nametableRAM[i], NameTableScreenSize);
2014-07-02 02:56:06 +00:00
}
_ramReadHandlers = new IMemoryHandler*[RAMSize];
_ramWriteHandlers = new IMemoryHandler*[RAMSize];
for(int i = 0; i < RAMSize; i++) {
_ramReadHandlers[i] = &_openBusHandler;
_ramWriteHandlers[i] = &_openBusHandler;
}
RegisterIODevice(&_internalRamHandler);
}
MemoryManager::~MemoryManager()
{
delete[] _internalRAM;
for(int i = 0; i < 2; i++) {
2014-07-02 02:56:06 +00:00
delete[] _nametableRAM[i];
}
delete[] _ramReadHandlers;
delete[] _ramWriteHandlers;
}
2014-06-14 15:27:55 +00:00
void MemoryManager::SetMapper(shared_ptr<BaseMapper> mapper)
{
_mapper = mapper;
_mapper->SetDefaultNametables(_nametableRAM[0], _nametableRAM[1]);
}
2015-07-06 00:30:39 +00:00
void MemoryManager::Reset(bool softReset)
{
if(!softReset) {
_mapper->InitializeRam(_internalRAM, InternalRAMSize);
2015-07-06 00:30:39 +00:00
}
2016-01-19 02:46:53 +00:00
_mapper->Reset(softReset);
2015-07-06 00:30:39 +00:00
}
2016-06-26 00:46:54 +00:00
void MemoryManager::InitializeMemoryHandlers(IMemoryHandler** memoryHandlers, IMemoryHandler* handler, vector<uint16_t> *addresses, bool allowOverride)
2014-06-14 15:27:55 +00:00
{
for(uint16_t address : *addresses) {
if(!allowOverride && memoryHandlers[address] != &_openBusHandler && memoryHandlers[address] != handler) {
throw std::runtime_error("Not supported");
2014-06-16 01:45:36 +00:00
}
memoryHandlers[address] = handler;
2014-06-16 01:45:36 +00:00
}
}
2014-06-16 01:45:36 +00:00
void MemoryManager::RegisterIODevice(IMemoryHandler *handler)
{
MemoryRanges ranges;
handler->GetMemoryRanges(ranges);
2016-06-26 00:46:54 +00:00
InitializeMemoryHandlers(_ramReadHandlers, handler, ranges.GetRAMReadAddresses(), ranges.GetAllowOverride());
InitializeMemoryHandlers(_ramWriteHandlers, handler, ranges.GetRAMWriteAddresses(), ranges.GetAllowOverride());
2014-06-14 15:27:55 +00:00
}
void MemoryManager::UnregisterIODevice(IMemoryHandler *handler)
{
MemoryRanges ranges;
handler->GetMemoryRanges(ranges);
for(uint16_t address : *ranges.GetRAMReadAddresses()) {
_ramReadHandlers[address] = &_openBusHandler;
}
for(uint16_t address : *ranges.GetRAMWriteAddresses()) {
_ramWriteHandlers[address] = &_openBusHandler;
}
}
uint8_t* MemoryManager::GetInternalRAM()
{
return _internalRAM;
}
2017-08-30 22:31:27 +00:00
uint8_t MemoryManager::DebugRead(uint16_t addr, bool disableSideEffects)
2014-06-14 15:27:55 +00:00
{
uint8_t value = 0x00;
if(addr <= 0x1FFF) {
value = _ramReadHandlers[addr]->ReadRAM(addr);
2017-08-30 22:31:27 +00:00
} else {
IMemoryHandler* handler = _ramReadHandlers[addr];
if(handler) {
if(disableSideEffects) {
if(handler == _mapper.get()) {
//Only allow reads from prg/chr ram/rom (e.g not ppu, apu, mapper registers, etc.)
value = ((BaseMapper*)handler)->DebugReadRAM(addr);
}
2017-08-30 22:31:27 +00:00
} else {
value = handler->ReadRAM(addr);
}
} else {
//Fake open bus
value = addr >> 8;
2017-08-30 22:31:27 +00:00
}
}
_console->GetCheatManager()->ApplyRamCodes(addr, value);
return value;
}
uint16_t MemoryManager::DebugReadWord(uint16_t addr)
{
return DebugRead(addr) | (DebugRead(addr + 1) << 8);
}
uint8_t MemoryManager::Read(uint16_t addr, MemoryOperationType operationType)
{
uint8_t value = _ramReadHandlers[addr]->ReadRAM(addr);
_console->GetCheatManager()->ApplyRamCodes(addr, value);
_console->DebugProcessRamOperation(operationType, addr, value);
_openBusHandler.SetOpenBus(value);
return value;
2014-06-14 15:27:55 +00:00
}
void MemoryManager::Write(uint16_t addr, uint8_t value)
{
if(_console->DebugProcessRamOperation(MemoryOperationType::Write, addr, value)) {
_ramWriteHandlers[addr]->WriteRAM(addr, value);
2014-06-14 15:27:55 +00:00
}
}
2017-08-30 22:31:27 +00:00
void MemoryManager::DebugWrite(uint16_t addr, uint8_t value, bool disableSideEffects)
{
if(addr <= 0x1FFF) {
_ramWriteHandlers[addr]->WriteRAM(addr, value);
} else {
2017-08-30 22:31:27 +00:00
IMemoryHandler* handler = _ramReadHandlers[addr];
if(handler) {
if(disableSideEffects) {
if(handler == _mapper.get()) {
//Only allow writes to prg/chr ram/rom (e.g not ppu, apu, mapper registers, etc.)
((BaseMapper*)handler)->DebugWriteRAM(addr, value);
}
2017-08-30 22:31:27 +00:00
} else {
handler->WriteRAM(addr, value);
}
}
}
}
uint32_t MemoryManager::ToAbsolutePrgAddress(uint16_t ramAddr)
{
return _mapper->ToAbsoluteAddress(ramAddr);
}
2014-06-26 01:52:37 +00:00
void MemoryManager::StreamState(bool saving)
{
ArrayInfo<uint8_t> internalRam = { _internalRAM, MemoryManager::InternalRAMSize };
ArrayInfo<uint8_t> nameTable0Ram = { _nametableRAM[0], MemoryManager::NameTableScreenSize };
ArrayInfo<uint8_t> nameTable1Ram = { _nametableRAM[1], MemoryManager::NameTableScreenSize };
Stream(internalRam, nameTable0Ram, nameTable1Ram);
}
uint8_t MemoryManager::GetOpenBus(uint8_t mask)
{
return _openBusHandler.GetOpenBus() & mask;
}