2019-09-09 07:01:26 +00:00
|
|
|
#pragma once
|
2020-01-10 03:31:12 +00:00
|
|
|
#include "common/align.h"
|
2019-09-09 07:01:26 +00:00
|
|
|
#include "bus.h"
|
|
|
|
#include "cpu_core.h"
|
|
|
|
|
|
|
|
namespace CPU {
|
|
|
|
|
2019-09-24 14:36:24 +00:00
|
|
|
template<MemoryAccessType type, MemoryAccessSize size>
|
2019-10-04 10:23:47 +00:00
|
|
|
TickCount Core::DoMemoryAccess(VirtualMemoryAddress address, u32& value)
|
2019-09-09 07:01:26 +00:00
|
|
|
{
|
|
|
|
switch (address >> 29)
|
|
|
|
{
|
|
|
|
case 0x00: // KUSEG 0M-512M
|
|
|
|
{
|
|
|
|
if constexpr (type == MemoryAccessType::Write)
|
|
|
|
{
|
|
|
|
if (m_cop0_regs.sr.Isc)
|
2019-11-26 14:01:47 +00:00
|
|
|
return 0;
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2019-09-14 03:52:57 +00:00
|
|
|
const PhysicalMemoryAddress phys_addr = address & UINT32_C(0x1FFFFFFF);
|
|
|
|
if ((phys_addr & DCACHE_LOCATION_MASK) == DCACHE_LOCATION)
|
|
|
|
{
|
|
|
|
DoScratchpadAccess<type, size>(phys_addr, value);
|
2019-11-26 14:01:47 +00:00
|
|
|
return 0;
|
2019-09-14 03:52:57 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
return m_bus->DispatchAccess<type, size>(phys_addr, value);
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case 0x01: // KUSEG 512M-1024M
|
|
|
|
case 0x02: // KUSEG 1024M-1536M
|
|
|
|
case 0x03: // KUSEG 1536M-2048M
|
|
|
|
{
|
|
|
|
// Above 512mb raises an exception.
|
2019-10-04 10:23:47 +00:00
|
|
|
return -1;
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case 0x04: // KSEG0 - physical memory cached
|
|
|
|
{
|
|
|
|
if constexpr (type == MemoryAccessType::Write)
|
|
|
|
{
|
|
|
|
if (m_cop0_regs.sr.Isc)
|
2019-11-26 14:01:47 +00:00
|
|
|
return 0;
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2019-09-14 03:52:57 +00:00
|
|
|
const PhysicalMemoryAddress phys_addr = address & UINT32_C(0x1FFFFFFF);
|
|
|
|
if ((phys_addr & DCACHE_LOCATION_MASK) == DCACHE_LOCATION)
|
|
|
|
{
|
|
|
|
DoScratchpadAccess<type, size>(phys_addr, value);
|
2019-11-26 14:01:47 +00:00
|
|
|
return 0;
|
2019-09-14 03:52:57 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
return m_bus->DispatchAccess<type, size>(phys_addr, value);
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x05: // KSEG1 - physical memory uncached
|
|
|
|
{
|
2019-09-14 03:52:57 +00:00
|
|
|
const PhysicalMemoryAddress phys_addr = address & UINT32_C(0x1FFFFFFF);
|
2019-10-04 10:23:47 +00:00
|
|
|
return m_bus->DispatchAccess<type, size>(phys_addr, value);
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x06: // KSEG2
|
|
|
|
case 0x07: // KSEG2
|
|
|
|
{
|
|
|
|
if (address == 0xFFFE0130)
|
|
|
|
{
|
|
|
|
if constexpr (type == MemoryAccessType::Read)
|
|
|
|
value = m_cache_control;
|
|
|
|
else
|
|
|
|
WriteCacheControl(value);
|
2019-09-14 03:22:05 +00:00
|
|
|
|
2019-11-26 14:01:47 +00:00
|
|
|
return 0;
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-10-04 10:23:47 +00:00
|
|
|
return -1;
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
UnreachableCode();
|
2019-09-14 03:22:05 +00:00
|
|
|
return false;
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 03:52:57 +00:00
|
|
|
template<MemoryAccessType type, MemoryAccessSize size>
|
2019-09-14 04:29:23 +00:00
|
|
|
bool CPU::Core::DoAlignmentCheck(VirtualMemoryAddress address)
|
|
|
|
{
|
|
|
|
if constexpr (size == MemoryAccessSize::HalfWord)
|
|
|
|
{
|
2019-10-03 16:26:37 +00:00
|
|
|
if (Common::IsAlignedPow2(address, 2))
|
2019-09-14 04:29:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if constexpr (size == MemoryAccessSize::Word)
|
|
|
|
{
|
2019-10-03 16:26:37 +00:00
|
|
|
if (Common::IsAlignedPow2(address, 4))
|
2019-09-14 04:29:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_cop0_regs.BadVaddr = address;
|
|
|
|
RaiseException(type == MemoryAccessType::Read ? Exception::AdEL : Exception::AdES);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<MemoryAccessType type, MemoryAccessSize size>
|
2019-09-14 03:52:57 +00:00
|
|
|
void CPU::Core::DoScratchpadAccess(PhysicalMemoryAddress address, u32& value)
|
|
|
|
{
|
|
|
|
const PhysicalMemoryAddress cache_offset = address & DCACHE_OFFSET_MASK;
|
|
|
|
if constexpr (size == MemoryAccessSize::Byte)
|
|
|
|
{
|
|
|
|
if constexpr (type == MemoryAccessType::Read)
|
|
|
|
value = ZeroExtend32(m_dcache[cache_offset]);
|
|
|
|
else
|
|
|
|
m_dcache[cache_offset] = Truncate8(value);
|
|
|
|
}
|
|
|
|
else if constexpr (size == MemoryAccessSize::HalfWord)
|
|
|
|
{
|
|
|
|
if constexpr (type == MemoryAccessType::Read)
|
|
|
|
{
|
|
|
|
u16 temp;
|
|
|
|
std::memcpy(&temp, &m_dcache[cache_offset], sizeof(temp));
|
|
|
|
value = ZeroExtend32(temp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
u16 temp = Truncate16(value);
|
|
|
|
std::memcpy(&m_dcache[cache_offset], &temp, sizeof(temp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if constexpr (size == MemoryAccessSize::Word)
|
|
|
|
{
|
|
|
|
if constexpr (type == MemoryAccessType::Read)
|
|
|
|
std::memcpy(&value, &m_dcache[cache_offset], sizeof(value));
|
|
|
|
else
|
|
|
|
std::memcpy(&m_dcache[cache_offset], &value, sizeof(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 07:01:26 +00:00
|
|
|
} // namespace CPU
|