Mesen-SX/Core/MemoryManager.cpp

425 lines
10 KiB
C++
Raw Normal View History

2019-02-26 22:27:09 -05:00
#include "stdafx.h"
#include "MemoryManager.h"
#include "Console.h"
#include "BaseCartridge.h"
#include "Cpu.h"
#include "Ppu.h"
#include "DmaController.h"
2019-02-26 22:27:09 -05:00
#include "RegisterHandlerA.h"
#include "RegisterHandlerB.h"
#include "RamHandler.h"
#include "MessageManager.h"
#include "DebugTypes.h"
#include "EmuSettings.h"
2019-07-30 22:34:52 -04:00
#include "Sa1.h"
#include "Gsu.h"
2019-08-03 23:43:51 -04:00
#include "Cx4.h"
#include "BaseCoprocessor.h"
2019-10-12 22:40:25 -04:00
#include "CheatManager.h"
2019-03-12 09:15:57 -04:00
#include "../Utilities/Serializer.h"
2019-02-26 22:27:09 -05:00
#include "../Utilities/HexUtilities.h"
void MemoryManager::Initialize(Console *console)
2019-02-26 22:27:09 -05:00
{
_masterClock = 0;
2019-03-09 00:31:54 -05:00
_openBus = 0;
_cpuSpeed = 8;
2019-02-26 22:27:09 -05:00
_console = console;
_regs = console->GetInternalRegisters().get();
_cpu = console->GetCpu().get();
_ppu = console->GetPpu().get();
_cart = console->GetCartridge().get();
2019-10-12 22:40:25 -04:00
_cheatManager = console->GetCheatManager().get();
2019-02-26 22:27:09 -05:00
_workRam = new uint8_t[MemoryManager::WorkRamSize];
_console->GetSettings()->InitializeRam(_workRam, MemoryManager::WorkRamSize);
2019-02-26 22:27:09 -05:00
_registerHandlerA.reset(new RegisterHandlerA(
console->GetDmaController().get(),
console->GetInternalRegisters().get(),
console->GetControlManager().get()
));
2019-02-26 22:27:09 -05:00
_registerHandlerB.reset(new RegisterHandlerB(
_console,
_ppu,
2019-02-26 22:27:09 -05:00
console->GetSpc().get(),
_workRam
));
memset(_hasEvent, 0, sizeof(_hasEvent));
_hasEvent[276 * 4] = true;
_hasEvent[285 * 4] = true;
_hasEvent[1360] = true;
_hasEvent[1364] = true;
_hasEvent[1368] = true;
2019-02-26 22:27:09 -05:00
for(uint32_t i = 0; i < 128 * 1024; i += 0x1000) {
_workRamHandlers.push_back(unique_ptr<RamHandler>(new RamHandler(_workRam, i, MemoryManager::WorkRamSize, SnesMemoryType::WorkRam)));
2019-02-26 22:27:09 -05:00
}
_mappings.RegisterHandler(0x7E, 0x7F, 0x0000, 0xFFFF, _workRamHandlers);
2019-02-26 22:27:09 -05:00
_mappings.RegisterHandler(0x00, 0x3F, 0x2000, 0x2FFF, _registerHandlerB.get());
_mappings.RegisterHandler(0x80, 0xBF, 0x2000, 0x2FFF, _registerHandlerB.get());
2019-02-26 22:27:09 -05:00
_mappings.RegisterHandler(0x00, 0x3F, 0x4000, 0x4FFF, _registerHandlerA.get());
_mappings.RegisterHandler(0x80, 0xBF, 0x4000, 0x4FFF, _registerHandlerA.get());
2019-02-26 22:27:09 -05:00
_mappings.RegisterHandler(0x00, 0x3F, 0x0000, 0x0FFF, _workRamHandlers[0].get());
_mappings.RegisterHandler(0x80, 0xBF, 0x0000, 0x0FFF, _workRamHandlers[0].get());
_mappings.RegisterHandler(0x00, 0x3F, 0x1000, 0x1FFF, _workRamHandlers[1].get());
_mappings.RegisterHandler(0x80, 0xBF, 0x1000, 0x1FFF, _workRamHandlers[1].get());
2019-02-26 22:27:09 -05:00
_cart->Init(_mappings);
2019-02-26 22:27:09 -05:00
GenerateMasterClockTable();
UpdateEvents();
2019-02-26 22:27:09 -05:00
}
MemoryManager::~MemoryManager()
{
delete[] _workRam;
}
void MemoryManager::Reset()
{
_masterClock = 0;
_hClock = 0;
UpdateEvents();
}
2019-02-26 22:27:09 -05:00
void MemoryManager::GenerateMasterClockTable()
{
for(int j = 0; j < 2; j++) {
for(int i = 0; i < 0x10000; i++) {
uint8_t bank = (i & 0xFF00) >> 8;
if(bank >= 0x40 && bank <= 0x7F) {
//Slow
_masterClockTable[j][i] = 8;
2019-03-08 10:26:54 -05:00
} else if(bank >= 0xC0) {
//Banks $C0-$FF
2019-02-26 22:27:09 -05:00
//Slow or fast (depending on register)
_masterClockTable[j][i] = j == 1 ? 6 : 8;
} else {
2019-03-08 10:26:54 -05:00
//Banks $00-$3F and $80-$BF
2019-02-26 22:27:09 -05:00
uint8_t page = (i & 0xFF);
if(page <= 0x1F) {
//Slow
_masterClockTable[j][i] = 8;
2019-02-26 22:27:09 -05:00
} else if(page >= 0x20 && page <= 0x3F) {
//Fast
_masterClockTable[j][i] = 6;
} else if(page == 0x40 || page == 0x41) {
//Extra slow
_masterClockTable[j][i] = 12;
} else if(page >= 0x42 && page <= 0x5F) {
//Fast
_masterClockTable[j][i] = 6;
} else if(page >= 0x60 && page <= 0x7F) {
//Slow
_masterClockTable[j][i] = 8;
2019-03-08 10:26:54 -05:00
} else if(bank <= 0x3F) {
//Slow
_masterClockTable[j][i] = 8;
2019-02-26 22:27:09 -05:00
} else {
//page >= $80
//Slow or fast (depending on register)
_masterClockTable[j][i] = j == 1 ? 6 : 8;
}
}
}
}
}
void MemoryManager::IncMasterClock4()
2019-02-26 22:27:09 -05:00
{
Exec();
Exec();
}
void MemoryManager::IncMasterClock6()
{
Exec();
Exec();
Exec();
}
void MemoryManager::IncMasterClock8()
{
Exec();
Exec();
Exec();
Exec();
}
void MemoryManager::IncMasterClock40()
{
Exec(); Exec(); Exec(); Exec(); Exec();
Exec(); Exec(); Exec(); Exec(); Exec();
Exec(); Exec(); Exec(); Exec(); Exec();
Exec(); Exec(); Exec(); Exec(); Exec();
}
void MemoryManager::IncMasterClockStartup()
{
for(int i = 0; i < 182 / 2; i++) {
Exec();
}
2019-02-26 22:27:09 -05:00
}
2019-03-08 10:26:54 -05:00
void MemoryManager::IncrementMasterClockValue(uint16_t cyclesToRun)
2019-02-26 22:27:09 -05:00
{
2019-03-08 10:26:54 -05:00
switch(cyclesToRun) {
2019-07-12 23:53:47 -04:00
case 12: Exec();
case 10: Exec();
case 8: Exec();
case 6: Exec();
case 4: Exec();
case 2: Exec();
2019-03-08 10:26:54 -05:00
}
}
void MemoryManager::UpdateEvents()
{
_hasEvent[_hdmaInitPosition] = false;
_hasEvent[_dramRefreshPosition] = false;
if(_ppu->GetScanline() == 0) {
_hdmaInitPosition = 12 + (_masterClock & 0x07);
_hasEvent[_hdmaInitPosition] = true;
}
_dramRefreshPosition = 538 - (_masterClock & 0x07);
_hasEvent[_dramRefreshPosition] = true;
}
2019-07-30 22:34:52 -04:00
void MemoryManager::SyncCoprocessors()
{
if(_cart->GetCoprocessor()) {
if(_cart->GetGsu()) {
_cart->GetGsu()->Run();
} else if(_cart->GetSa1()) {
_cart->GetSa1()->Run();
2019-08-03 23:43:51 -04:00
} else if(_cart->GetCx4()) {
_cart->GetCx4()->Run();
2019-07-30 22:34:52 -04:00
}
}
}
2019-03-08 10:26:54 -05:00
void MemoryManager::Exec()
{
_masterClock += 2;
_hClock += 2;
if(_hasEvent[_hClock]) {
2019-07-08 09:18:38 -04:00
if(_hClock >= 1360 && _ppu->ProcessEndOfScanline(_hClock)) {
_hClock = 0;
UpdateEvents();
}
if((_hClock & 0x03) == 0) {
_console->ProcessPpuCycle(_ppu->GetScanline(), _hClock);
_regs->ProcessIrqCounters();
if(_hClock == 276 * 4 && _ppu->GetScanline() < _ppu->GetVblankStart()) {
_console->GetDmaController()->BeginHdmaTransfer();
}
}
if(_hClock == _hdmaInitPosition && _ppu->GetScanline() == 0) {
_console->GetDmaController()->BeginHdmaInit();
}
if(_hClock == _dramRefreshPosition) {
IncMasterClock40();
_cpu->IncreaseCycleCount<5>();
}
} else if((_hClock & 0x03) == 0) {
_console->ProcessPpuCycle(_ppu->GetScanline(), _hClock);
_regs->ProcessIrqCounters();
}
2019-07-30 22:34:52 -04:00
SyncCoprocessors();
2019-02-26 22:27:09 -05:00
}
uint8_t MemoryManager::Read(uint32_t addr, MemoryOperationType type)
{
IncrementMasterClockValue(_cpuSpeed - 4);
2019-02-26 22:27:09 -05:00
uint8_t value;
IMemoryHandler *handler = _mappings.GetHandler(addr);
if(handler) {
value = handler->Read(addr);
_memTypeBusA = handler->GetMemoryType();
2019-03-09 00:31:54 -05:00
_openBus = value;
2019-02-26 22:27:09 -05:00
} else {
//open bus
2019-03-09 00:31:54 -05:00
value = _openBus;
LogDebug("[Debug] Read - missing handler: $" + HexUtilities::ToHex(addr));
2019-02-26 22:27:09 -05:00
}
2019-10-12 22:40:25 -04:00
_cheatManager->ApplyCheat(addr, value);
_console->ProcessMemoryRead<CpuType::Cpu>(addr, value, type);
IncMasterClock4();
2019-02-26 22:27:09 -05:00
return value;
}
uint8_t MemoryManager::ReadDma(uint32_t addr, bool forBusA)
2019-02-26 22:27:09 -05:00
{
_cpu->DetectNmiSignalEdge();
IncMasterClock4();
2019-02-26 22:27:09 -05:00
uint8_t value;
IMemoryHandler* handler = _mappings.GetHandler(addr);
if(handler) {
if(forBusA && handler == _registerHandlerB.get() && (addr & 0xFF00) == 0x2100) {
//Trying to read from bus B using bus A returns open bus
value = _openBus;
} else if(handler == _registerHandlerA.get()) {
uint16_t regAddr = addr & 0xFFFF;
if(regAddr == 0x420B || regAddr == 0x420C || (regAddr >= 0x4300 && regAddr <= 0x437F)) {
//Trying to read the DMA controller with DMA returns open bus
value = _openBus;
} else {
value = handler->Read(addr);
}
} else {
value = handler->Read(addr);
if(handler != _registerHandlerB.get()) {
_memTypeBusA = handler->GetMemoryType();
}
}
2019-03-09 00:31:54 -05:00
_openBus = value;
2019-02-26 22:27:09 -05:00
} else {
//open bus
2019-03-09 00:31:54 -05:00
value = _openBus;
LogDebug("[Debug] Read - missing handler: $" + HexUtilities::ToHex(addr));
2019-02-26 22:27:09 -05:00
}
2019-10-12 22:40:25 -04:00
_cheatManager->ApplyCheat(addr, value);
_console->ProcessMemoryRead<CpuType::Cpu>(addr, value, MemoryOperationType::DmaRead);
2019-02-26 22:27:09 -05:00
return value;
}
uint8_t MemoryManager::Peek(uint32_t addr)
{
return _mappings.Peek(addr);
2019-02-26 22:27:09 -05:00
}
uint16_t MemoryManager::PeekWord(uint32_t addr)
{
return _mappings.PeekWord(addr);
}
void MemoryManager::PeekBlock(uint32_t addr, uint8_t *dest)
{
_mappings.PeekBlock(addr, dest);
}
2019-02-26 22:27:09 -05:00
void MemoryManager::Write(uint32_t addr, uint8_t value, MemoryOperationType type)
{
IncrementMasterClockValue(_cpuSpeed);
2019-02-26 22:27:09 -05:00
_console->ProcessMemoryWrite<CpuType::Cpu>(addr, value, type);
IMemoryHandler* handler = _mappings.GetHandler(addr);
if(handler) {
handler->Write(addr, value);
_memTypeBusA = handler->GetMemoryType();
2019-02-26 22:27:09 -05:00
} else {
LogDebug("[Debug] Write - missing handler: $" + HexUtilities::ToHex(addr) + " = " + HexUtilities::ToHex(value));
2019-02-26 22:27:09 -05:00
}
}
void MemoryManager::WriteDma(uint32_t addr, uint8_t value, bool forBusA)
2019-02-26 22:27:09 -05:00
{
_cpu->DetectNmiSignalEdge();
IncMasterClock4();
_console->ProcessMemoryWrite<CpuType::Cpu>(addr, value, MemoryOperationType::DmaWrite);
IMemoryHandler* handler = _mappings.GetHandler(addr);
if(handler) {
if(forBusA && handler == _registerHandlerB.get() && (addr & 0xFF00) == 0x2100) {
//Trying to write to bus B using bus A does nothing
} else if(handler == _registerHandlerA.get()) {
uint16_t regAddr = addr & 0xFFFF;
if(regAddr == 0x420B || regAddr == 0x420C || (regAddr >= 0x4300 && regAddr <= 0x437F)) {
//Trying to write to the DMA controller with DMA does nothing
} else {
handler->Write(addr, value);
}
} else {
handler->Write(addr, value);
if(handler != _registerHandlerB.get()) {
_memTypeBusA = handler->GetMemoryType();
}
}
2019-02-26 22:27:09 -05:00
} else {
LogDebug("[Debug] Write - missing handler: $" + HexUtilities::ToHex(addr) + " = " + HexUtilities::ToHex(value));
2019-02-26 22:27:09 -05:00
}
}
2019-03-09 00:31:54 -05:00
uint8_t MemoryManager::GetOpenBus()
{
return _openBus;
}
2019-02-26 22:27:09 -05:00
uint64_t MemoryManager::GetMasterClock()
{
return _masterClock;
}
uint16_t MemoryManager::GetHClock()
{
return _hClock;
}
2019-02-26 22:27:09 -05:00
uint8_t * MemoryManager::DebugGetWorkRam()
{
return _workRam;
}
MemoryMappings* MemoryManager::GetMemoryMappings()
{
return &_mappings;
}
uint8_t MemoryManager::GetCpuSpeed(uint32_t addr)
{
return _masterClockTable[(uint8_t)_regs->IsFastRomEnabled()][addr >> 8];
}
uint8_t MemoryManager::GetCpuSpeed()
{
return _cpuSpeed;
}
void MemoryManager::SetCpuSpeed(uint8_t speed)
{
_cpuSpeed = speed;
}
SnesMemoryType MemoryManager::GetMemoryTypeBusA()
2019-03-07 20:12:32 -05:00
{
return _memTypeBusA;
2019-03-07 20:12:32 -05:00
}
bool MemoryManager::IsRegister(uint32_t cpuAddress)
{
IMemoryHandler* handler = _mappings.GetHandler(cpuAddress);
return handler == _registerHandlerA.get() || handler == _registerHandlerB.get();
}
bool MemoryManager::IsWorkRam(uint32_t cpuAddress)
2019-02-26 22:27:09 -05:00
{
IMemoryHandler* handler = _mappings.GetHandler(cpuAddress);
return handler && handler->GetMemoryType() == SnesMemoryType::WorkRam;
2019-02-26 22:27:09 -05:00
}
2019-03-12 09:15:57 -04:00
void MemoryManager::Serialize(Serializer &s)
{
s.Stream(_masterClock, _openBus, _cpuSpeed, _hClock, _dramRefreshPosition, _hdmaInitPosition);
2019-03-12 09:15:57 -04:00
s.StreamArray(_workRam, MemoryManager::WorkRamSize);
s.StreamArray(_hasEvent, sizeof(_hasEvent));
s.Stream(_registerHandlerB.get());
2019-03-12 09:15:57 -04:00
}