2019-09-09 07:01:26 +00:00
|
|
|
#include "bus.h"
|
2019-09-17 09:22:39 +00:00
|
|
|
#include "cdrom.h"
|
2020-01-10 03:31:12 +00:00
|
|
|
#include "common/align.h"
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/log.h"
|
2019-09-14 10:28:47 +00:00
|
|
|
#include "common/state_wrapper.h"
|
2019-11-19 10:30:04 +00:00
|
|
|
#include "cpu_code_cache.h"
|
2019-09-17 06:26:00 +00:00
|
|
|
#include "cpu_core.h"
|
2019-09-14 03:22:05 +00:00
|
|
|
#include "cpu_disasm.h"
|
2019-09-09 07:01:26 +00:00
|
|
|
#include "dma.h"
|
2019-09-11 04:01:19 +00:00
|
|
|
#include "gpu.h"
|
2019-09-17 06:26:00 +00:00
|
|
|
#include "interrupt_controller.h"
|
2019-09-29 02:51:34 +00:00
|
|
|
#include "mdec.h"
|
2019-09-20 06:47:41 +00:00
|
|
|
#include "pad.h"
|
2019-12-07 11:09:02 +00:00
|
|
|
#include "sio.h"
|
2019-09-24 13:44:38 +00:00
|
|
|
#include "spu.h"
|
2019-09-20 13:40:19 +00:00
|
|
|
#include "timers.h"
|
2019-09-09 07:01:26 +00:00
|
|
|
#include <cstdio>
|
|
|
|
Log_SetChannel(Bus);
|
|
|
|
|
2019-10-05 06:29:14 +00:00
|
|
|
#define FIXUP_WORD_READ_OFFSET(offset) ((offset) & ~u32(3))
|
2019-10-13 06:48:11 +00:00
|
|
|
#define FIXUP_WORD_READ_VALUE(offset, value) ((value) >> (((offset)&u32(3)) * 8))
|
2019-10-05 06:29:14 +00:00
|
|
|
|
2019-09-17 06:26:00 +00:00
|
|
|
// Offset and value remapping for (w32) registers from nocash docs.
|
|
|
|
void FixupUnalignedWordAccessW32(u32& offset, u32& value)
|
|
|
|
{
|
|
|
|
const u32 byte_offset = offset & u32(3);
|
|
|
|
offset &= ~u32(3);
|
|
|
|
value <<= byte_offset * 8;
|
|
|
|
}
|
|
|
|
|
2019-09-09 07:01:26 +00:00
|
|
|
Bus::Bus() = default;
|
|
|
|
|
|
|
|
Bus::~Bus() = default;
|
|
|
|
|
2019-11-19 10:30:04 +00:00
|
|
|
void Bus::Initialize(CPU::Core* cpu, CPU::CodeCache* cpu_code_cache, DMA* dma,
|
|
|
|
InterruptController* interrupt_controller, GPU* gpu, CDROM* cdrom, Pad* pad, Timers* timers,
|
2019-12-07 11:09:02 +00:00
|
|
|
SPU* spu, MDEC* mdec, SIO* sio)
|
2019-09-09 07:01:26 +00:00
|
|
|
{
|
2019-09-17 06:26:00 +00:00
|
|
|
m_cpu = cpu;
|
2019-11-19 10:30:04 +00:00
|
|
|
m_cpu_code_cache = cpu_code_cache;
|
2019-09-09 07:01:26 +00:00
|
|
|
m_dma = dma;
|
2019-09-17 06:26:00 +00:00
|
|
|
m_interrupt_controller = interrupt_controller;
|
2019-09-09 07:01:26 +00:00
|
|
|
m_gpu = gpu;
|
2019-09-17 09:22:39 +00:00
|
|
|
m_cdrom = cdrom;
|
2019-09-20 06:47:41 +00:00
|
|
|
m_pad = pad;
|
2019-09-20 13:40:19 +00:00
|
|
|
m_timers = timers;
|
2019-09-24 13:44:38 +00:00
|
|
|
m_spu = spu;
|
2019-09-29 02:51:34 +00:00
|
|
|
m_mdec = mdec;
|
2019-12-07 11:09:02 +00:00
|
|
|
m_sio = sio;
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Bus::Reset()
|
|
|
|
{
|
|
|
|
m_ram.fill(static_cast<u8>(0));
|
2019-09-24 14:36:24 +00:00
|
|
|
m_MEMCTRL.exp1_base = 0x1F000000;
|
|
|
|
m_MEMCTRL.exp2_base = 0x1F802000;
|
2019-10-04 09:41:18 +00:00
|
|
|
m_MEMCTRL.exp1_delay_size.bits = 0x0013243F;
|
|
|
|
m_MEMCTRL.exp3_delay_size.bits = 0x00003022;
|
|
|
|
m_MEMCTRL.bios_delay_size.bits = 0x0013243F;
|
|
|
|
m_MEMCTRL.spu_delay_size.bits = 0x200931E1;
|
|
|
|
m_MEMCTRL.cdrom_delay_size.bits = 0x00020843;
|
|
|
|
m_MEMCTRL.exp2_delay_size.bits = 0x00070777;
|
|
|
|
m_MEMCTRL.common_delay.bits = 0x00031125;
|
2019-09-24 14:36:24 +00:00
|
|
|
m_ram_size_reg = UINT32_C(0x00000B88);
|
2019-10-04 09:41:18 +00:00
|
|
|
RecalculateMemoryTimings();
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Bus::DoState(StateWrapper& sw)
|
|
|
|
{
|
2019-10-04 10:23:47 +00:00
|
|
|
sw.Do(&m_exp1_access_time);
|
|
|
|
sw.Do(&m_exp2_access_time);
|
|
|
|
sw.Do(&m_bios_access_time);
|
|
|
|
sw.Do(&m_cdrom_access_time);
|
|
|
|
sw.Do(&m_spu_access_time);
|
2019-09-14 10:28:47 +00:00
|
|
|
sw.DoBytes(m_ram.data(), m_ram.size());
|
|
|
|
sw.DoBytes(m_bios.data(), m_bios.size());
|
2019-09-24 14:36:24 +00:00
|
|
|
sw.DoArray(m_MEMCTRL.regs, countof(m_MEMCTRL.regs));
|
|
|
|
sw.Do(&m_ram_size_reg);
|
2019-09-14 10:28:47 +00:00
|
|
|
sw.Do(&m_tty_line_buffer);
|
|
|
|
return !sw.HasError();
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 14:36:24 +00:00
|
|
|
bool Bus::ReadByte(PhysicalMemoryAddress address, u8* value)
|
2019-09-09 07:01:26 +00:00
|
|
|
{
|
|
|
|
u32 temp = 0;
|
2019-09-24 14:36:24 +00:00
|
|
|
const bool result = DispatchAccess<MemoryAccessType::Read, MemoryAccessSize::Byte>(address, temp);
|
2019-09-09 07:01:26 +00:00
|
|
|
*value = Truncate8(temp);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-09-24 14:36:24 +00:00
|
|
|
bool Bus::ReadHalfWord(PhysicalMemoryAddress address, u16* value)
|
2019-09-09 07:01:26 +00:00
|
|
|
{
|
|
|
|
u32 temp = 0;
|
2019-09-29 02:51:34 +00:00
|
|
|
const bool result = DispatchAccess<MemoryAccessType::Read, MemoryAccessSize::HalfWord>(address, temp);
|
2019-09-09 07:01:26 +00:00
|
|
|
*value = Truncate16(temp);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-09-24 14:36:24 +00:00
|
|
|
bool Bus::ReadWord(PhysicalMemoryAddress address, u32* value)
|
2019-09-09 07:01:26 +00:00
|
|
|
{
|
2019-09-24 14:36:24 +00:00
|
|
|
return DispatchAccess<MemoryAccessType::Read, MemoryAccessSize::Word>(address, *value);
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 14:36:24 +00:00
|
|
|
bool Bus::WriteByte(PhysicalMemoryAddress address, u8 value)
|
2019-09-09 07:01:26 +00:00
|
|
|
{
|
|
|
|
u32 temp = ZeroExtend32(value);
|
2019-09-24 14:36:24 +00:00
|
|
|
return DispatchAccess<MemoryAccessType::Read, MemoryAccessSize::Byte>(address, temp);
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 14:36:24 +00:00
|
|
|
bool Bus::WriteHalfWord(PhysicalMemoryAddress address, u16 value)
|
2019-09-09 07:01:26 +00:00
|
|
|
{
|
|
|
|
u32 temp = ZeroExtend32(value);
|
2019-09-24 14:36:24 +00:00
|
|
|
return DispatchAccess<MemoryAccessType::Read, MemoryAccessSize::HalfWord>(address, temp);
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 14:36:24 +00:00
|
|
|
bool Bus::WriteWord(PhysicalMemoryAddress address, u32 value)
|
2019-09-09 07:01:26 +00:00
|
|
|
{
|
2019-09-24 14:36:24 +00:00
|
|
|
return DispatchAccess<MemoryAccessType::Write, MemoryAccessSize::Word>(address, value);
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 06:48:11 +00:00
|
|
|
TickCount Bus::ReadWords(PhysicalMemoryAddress address, u32* words, u32 word_count)
|
|
|
|
{
|
|
|
|
if (address + (word_count * sizeof(u32)) > (RAM_BASE + RAM_SIZE))
|
|
|
|
{
|
|
|
|
// Not RAM, or RAM mirrors.
|
|
|
|
TickCount total_ticks = 0;
|
|
|
|
for (u32 i = 0; i < word_count; i++)
|
|
|
|
{
|
|
|
|
const TickCount ticks = DispatchAccess<MemoryAccessType::Read, MemoryAccessSize::Word>(address, words[i]);
|
|
|
|
if (ticks < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
total_ticks += ticks;
|
|
|
|
address += sizeof(u32);
|
|
|
|
}
|
|
|
|
|
|
|
|
return total_ticks;
|
|
|
|
}
|
|
|
|
|
|
|
|
// DMA is using DRAM Hyper Page mode, allowing it to access DRAM rows at 1 clock cycle per word (effectively around 17
|
|
|
|
// clks per 16 words, due to required row address loading, probably plus some further minimal overload due to refresh
|
|
|
|
// cycles). This is making DMA much faster than CPU memory accesses (CPU DRAM access takes 1 opcode cycle plus 6
|
|
|
|
// waitstates, ie. 7 cycles in total).
|
|
|
|
std::memcpy(words, &m_ram[address], sizeof(u32) * word_count);
|
|
|
|
return static_cast<TickCount>(word_count + ((word_count + 15) / 16));
|
|
|
|
}
|
|
|
|
|
|
|
|
TickCount Bus::WriteWords(PhysicalMemoryAddress address, const u32* words, u32 word_count)
|
|
|
|
{
|
|
|
|
if (address + (word_count * sizeof(u32)) > (RAM_BASE + RAM_SIZE))
|
|
|
|
{
|
|
|
|
// Not RAM, or RAM mirrors.
|
|
|
|
TickCount total_ticks = 0;
|
|
|
|
for (u32 i = 0; i < word_count; i++)
|
|
|
|
{
|
|
|
|
u32 value = words[i];
|
|
|
|
const TickCount ticks = DispatchAccess<MemoryAccessType::Write, MemoryAccessSize::Word>(address, value);
|
|
|
|
if (ticks < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
total_ticks += ticks;
|
|
|
|
address += sizeof(u32);
|
|
|
|
}
|
|
|
|
|
|
|
|
return total_ticks;
|
|
|
|
}
|
|
|
|
|
2019-11-26 09:45:36 +00:00
|
|
|
const u32 start_page = address / CPU_CODE_CACHE_PAGE_SIZE;
|
|
|
|
const u32 end_page = (address + word_count * sizeof(u32)) / CPU_CODE_CACHE_PAGE_SIZE;
|
|
|
|
for (u32 page = start_page; page <= end_page; page++)
|
|
|
|
{
|
|
|
|
if (m_ram_code_bits[page])
|
|
|
|
DoInvalidateCodeCache(page);
|
|
|
|
}
|
|
|
|
|
2019-10-13 06:48:11 +00:00
|
|
|
std::memcpy(&m_ram[address], words, sizeof(u32) * word_count);
|
|
|
|
return static_cast<TickCount>(word_count + ((word_count + 15) / 16));
|
|
|
|
}
|
|
|
|
|
2019-09-22 15:28:00 +00:00
|
|
|
void Bus::SetExpansionROM(std::vector<u8> data)
|
|
|
|
{
|
|
|
|
m_exp1_rom = std::move(data);
|
|
|
|
}
|
|
|
|
|
2019-11-16 05:27:57 +00:00
|
|
|
void Bus::SetBIOS(const std::vector<u8>& image)
|
2019-09-09 07:01:26 +00:00
|
|
|
{
|
2019-11-16 05:27:57 +00:00
|
|
|
if (image.size() != static_cast<u32>(BIOS_SIZE))
|
2019-09-09 07:01:26 +00:00
|
|
|
{
|
2019-11-16 05:27:57 +00:00
|
|
|
Panic("Incorrect BIOS image size");
|
|
|
|
return;
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2019-11-16 05:27:57 +00:00
|
|
|
std::copy(image.cbegin(), image.cend(), m_bios.begin());
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 09:41:18 +00:00
|
|
|
std::tuple<TickCount, TickCount, TickCount> Bus::CalculateMemoryTiming(MEMDELAY mem_delay, COMDELAY common_delay)
|
|
|
|
{
|
|
|
|
// from nocash spec
|
|
|
|
s32 first = 0, seq = 0, min = 0;
|
|
|
|
if (mem_delay.use_com0_time)
|
|
|
|
{
|
|
|
|
first += s32(common_delay.com0) - 1;
|
|
|
|
seq += s32(common_delay.com0) - 1;
|
|
|
|
}
|
|
|
|
if (mem_delay.use_com2_time)
|
|
|
|
{
|
|
|
|
first += s32(common_delay.com2);
|
|
|
|
seq += s32(common_delay.com2);
|
|
|
|
}
|
|
|
|
if (mem_delay.use_com3_time)
|
|
|
|
{
|
|
|
|
min = s32(common_delay.com3);
|
|
|
|
}
|
|
|
|
if (first < 6)
|
|
|
|
first++;
|
|
|
|
|
|
|
|
first = first + s32(mem_delay.access_time) + 2;
|
|
|
|
seq = seq + s32(mem_delay.access_time) + 2;
|
|
|
|
|
|
|
|
if (first < (min + 6))
|
|
|
|
first = min + 6;
|
|
|
|
if (seq < (min + 2))
|
|
|
|
seq = min + 2;
|
|
|
|
|
|
|
|
const TickCount byte_access_time = first;
|
|
|
|
const TickCount halfword_access_time = mem_delay.data_bus_16bit ? first : (first + seq);
|
|
|
|
const TickCount word_access_time = mem_delay.data_bus_16bit ? (first + seq) : (first + seq + seq + seq);
|
2019-11-26 14:01:47 +00:00
|
|
|
return std::tie(std::max(byte_access_time - 1, 0), std::max(halfword_access_time - 1, 0),
|
|
|
|
std::max(word_access_time - 1, 0));
|
2019-10-04 09:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Bus::RecalculateMemoryTimings()
|
|
|
|
{
|
|
|
|
std::tie(m_bios_access_time[0], m_bios_access_time[1], m_bios_access_time[2]) =
|
|
|
|
CalculateMemoryTiming(m_MEMCTRL.bios_delay_size, m_MEMCTRL.common_delay);
|
|
|
|
std::tie(m_cdrom_access_time[0], m_cdrom_access_time[1], m_cdrom_access_time[2]) =
|
|
|
|
CalculateMemoryTiming(m_MEMCTRL.cdrom_delay_size, m_MEMCTRL.common_delay);
|
|
|
|
std::tie(m_spu_access_time[0], m_spu_access_time[1], m_spu_access_time[2]) =
|
|
|
|
CalculateMemoryTiming(m_MEMCTRL.spu_delay_size, m_MEMCTRL.common_delay);
|
|
|
|
|
2019-10-18 13:10:41 +00:00
|
|
|
Log_TracePrintf("BIOS Memory Timing: %u bit bus, byte=%d, halfword=%d, word=%d",
|
2019-11-26 14:01:47 +00:00
|
|
|
m_MEMCTRL.bios_delay_size.data_bus_16bit ? 16 : 8, m_bios_access_time[0] + 1,
|
|
|
|
m_bios_access_time[1] + 1, m_bios_access_time[2] + 1);
|
2019-10-18 13:10:41 +00:00
|
|
|
Log_TracePrintf("CDROM Memory Timing: %u bit bus, byte=%d, halfword=%d, word=%d",
|
2019-11-26 14:01:47 +00:00
|
|
|
m_MEMCTRL.cdrom_delay_size.data_bus_16bit ? 16 : 8, m_cdrom_access_time[0] + 1,
|
|
|
|
m_cdrom_access_time[1] + 1, m_cdrom_access_time[2] + 1);
|
2019-10-18 13:10:41 +00:00
|
|
|
Log_TracePrintf("SPU Memory Timing: %u bit bus, byte=%d, halfword=%d, word=%d",
|
2019-11-26 14:01:47 +00:00
|
|
|
m_MEMCTRL.spu_delay_size.data_bus_16bit ? 16 : 8, m_spu_access_time[0] + 1, m_spu_access_time[1] + 1,
|
|
|
|
m_spu_access_time[2] + 1);
|
2019-10-04 09:41:18 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
TickCount Bus::DoInvalidAccess(MemoryAccessType type, MemoryAccessSize size, PhysicalMemoryAddress address, u32& value)
|
2019-09-09 07:01:26 +00:00
|
|
|
{
|
|
|
|
SmallString str;
|
|
|
|
str.AppendString("Invalid bus ");
|
|
|
|
if (size == MemoryAccessSize::Byte)
|
|
|
|
str.AppendString("byte");
|
|
|
|
if (size == MemoryAccessSize::HalfWord)
|
|
|
|
str.AppendString("word");
|
|
|
|
if (size == MemoryAccessSize::Word)
|
|
|
|
str.AppendString("dword");
|
|
|
|
str.AppendCharacter(' ');
|
|
|
|
if (type == MemoryAccessType::Read)
|
|
|
|
str.AppendString("read");
|
|
|
|
else
|
|
|
|
str.AppendString("write");
|
|
|
|
|
2019-09-24 14:36:24 +00:00
|
|
|
str.AppendFormattedString(" at address 0x%08X", address);
|
2019-09-09 07:01:26 +00:00
|
|
|
if (type == MemoryAccessType::Write)
|
|
|
|
str.AppendFormattedString(" (value 0x%08X)", value);
|
|
|
|
|
|
|
|
Log_ErrorPrint(str);
|
|
|
|
if (type == MemoryAccessType::Read)
|
|
|
|
value = UINT32_C(0xFFFFFFFF);
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
return 1;
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
u32 Bus::DoReadEXP1(MemoryAccessSize size, u32 offset)
|
2019-09-22 15:28:00 +00:00
|
|
|
{
|
|
|
|
if (m_exp1_rom.empty())
|
2019-10-04 10:23:47 +00:00
|
|
|
{
|
|
|
|
// EXP1 not present.
|
|
|
|
return UINT32_C(0xFFFFFFFF);
|
|
|
|
}
|
2019-09-22 15:28:00 +00:00
|
|
|
|
|
|
|
if (offset == 0x20018)
|
|
|
|
{
|
|
|
|
// Bit 0 - Action Replay On/Off
|
|
|
|
return UINT32_C(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const u32 transfer_size = u32(1) << static_cast<u32>(size);
|
|
|
|
if ((offset + transfer_size) > m_exp1_rom.size())
|
|
|
|
{
|
2019-10-04 10:23:47 +00:00
|
|
|
return UINT32_C(0);
|
2019-09-22 15:28:00 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
u32 value;
|
2019-09-22 15:28:00 +00:00
|
|
|
if (size == MemoryAccessSize::Byte)
|
|
|
|
{
|
|
|
|
value = ZeroExtend32(m_exp1_rom[offset]);
|
|
|
|
}
|
|
|
|
else if (size == MemoryAccessSize::HalfWord)
|
|
|
|
{
|
|
|
|
u16 halfword;
|
|
|
|
std::memcpy(&halfword, &m_exp1_rom[offset], sizeof(halfword));
|
|
|
|
value = ZeroExtend32(halfword);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::memcpy(&value, &m_exp1_rom[offset], sizeof(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log_DevPrintf("EXP1 read: 0x%08X -> 0x%08X", EXP1_BASE | offset, value);
|
2019-10-04 10:23:47 +00:00
|
|
|
return value;
|
2019-09-22 15:28:00 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
void Bus::DoWriteEXP1(MemoryAccessSize size, u32 offset, u32 value)
|
2019-09-22 15:28:00 +00:00
|
|
|
{
|
2019-10-04 10:23:47 +00:00
|
|
|
Log_WarningPrintf("EXP1 write: 0x%08X <- 0x%08X", EXP1_BASE | offset, value);
|
2019-09-22 15:28:00 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
u32 Bus::DoReadEXP2(MemoryAccessSize size, u32 offset)
|
2019-09-09 07:01:26 +00:00
|
|
|
{
|
|
|
|
// rx/tx buffer empty
|
|
|
|
if (offset == 0x21)
|
|
|
|
{
|
2019-10-04 10:23:47 +00:00
|
|
|
return 0x04 | 0x08;
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
Log_WarningPrintf("EXP2 read: 0x%08X", EXP2_BASE | offset);
|
|
|
|
return UINT32_C(0xFFFFFFFF);
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
void Bus::DoWriteEXP2(MemoryAccessSize size, u32 offset, u32 value)
|
2019-09-09 07:01:26 +00:00
|
|
|
{
|
|
|
|
if (offset == 0x23)
|
|
|
|
{
|
|
|
|
if (value == '\r')
|
2019-10-04 10:23:47 +00:00
|
|
|
return;
|
2019-09-09 07:01:26 +00:00
|
|
|
|
|
|
|
if (value == '\n')
|
|
|
|
{
|
2020-01-10 03:31:12 +00:00
|
|
|
if (!m_tty_line_buffer.empty())
|
2019-11-03 14:55:07 +00:00
|
|
|
{
|
2020-01-10 03:31:12 +00:00
|
|
|
Log_InfoPrintf("TTY: %s", m_tty_line_buffer.c_str());
|
2019-11-03 14:55:07 +00:00
|
|
|
#ifdef _DEBUG
|
|
|
|
if (CPU::LOG_EXECUTION)
|
2020-01-10 03:31:12 +00:00
|
|
|
CPU::WriteToExecutionLog("TTY: %s\n", m_tty_line_buffer.c_str());
|
2019-11-03 14:55:07 +00:00
|
|
|
#endif
|
|
|
|
}
|
2020-01-10 03:31:12 +00:00
|
|
|
m_tty_line_buffer.clear();
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-10 03:31:12 +00:00
|
|
|
m_tty_line_buffer += static_cast<char>(Truncate8(value));
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
return;
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (offset == 0x41)
|
|
|
|
{
|
|
|
|
Log_WarningPrintf("BIOS POST status: %02X", value & UINT32_C(0x0F));
|
2019-10-04 10:23:47 +00:00
|
|
|
return;
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
Log_WarningPrintf("EXP2 write: 0x%08X <- 0x%08X", EXP2_BASE | offset, value);
|
2019-09-24 14:36:24 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
u32 Bus::DoReadMemoryControl(MemoryAccessSize size, u32 offset)
|
2019-09-24 14:36:24 +00:00
|
|
|
{
|
2019-10-04 10:23:47 +00:00
|
|
|
u32 value = m_MEMCTRL.regs[offset / 4];
|
2019-09-24 14:36:24 +00:00
|
|
|
FixupUnalignedWordAccessW32(offset, value);
|
2019-10-04 10:23:47 +00:00
|
|
|
return value;
|
2019-09-24 14:36:24 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
void Bus::DoWriteMemoryControl(MemoryAccessSize size, u32 offset, u32 value)
|
2019-09-24 14:36:24 +00:00
|
|
|
{
|
|
|
|
FixupUnalignedWordAccessW32(offset, value);
|
2019-10-04 09:41:18 +00:00
|
|
|
|
|
|
|
const u32 index = offset / 4;
|
|
|
|
const u32 write_mask = (index == 8) ? COMDELAY::WRITE_MASK : MEMDELAY::WRITE_MASK;
|
|
|
|
const u32 new_value = (m_MEMCTRL.regs[index] & ~write_mask) | (value & write_mask);
|
|
|
|
if (m_MEMCTRL.regs[index] != new_value)
|
|
|
|
{
|
|
|
|
m_MEMCTRL.regs[index] = new_value;
|
|
|
|
RecalculateMemoryTimings();
|
|
|
|
}
|
2019-09-24 14:36:24 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
u32 Bus::DoReadMemoryControl2(MemoryAccessSize size, u32 offset)
|
2019-09-24 14:36:24 +00:00
|
|
|
{
|
|
|
|
if (offset == 0x00)
|
2019-10-04 10:23:47 +00:00
|
|
|
return m_ram_size_reg;
|
2019-09-24 14:36:24 +00:00
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
u32 value = 0;
|
|
|
|
DoInvalidAccess(MemoryAccessType::Read, size, MEMCTRL2_BASE | offset, value);
|
|
|
|
return value;
|
2019-09-24 14:36:24 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
void Bus::DoWriteMemoryControl2(MemoryAccessSize size, u32 offset, u32 value)
|
2019-09-24 14:36:24 +00:00
|
|
|
{
|
|
|
|
if (offset == 0x00)
|
|
|
|
{
|
|
|
|
m_ram_size_reg = value;
|
2019-10-04 10:23:47 +00:00
|
|
|
return;
|
2019-09-24 14:36:24 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
DoInvalidAccess(MemoryAccessType::Write, size, MEMCTRL2_BASE | offset, value);
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
u32 Bus::DoReadPad(MemoryAccessSize size, u32 offset)
|
2019-09-20 06:47:41 +00:00
|
|
|
{
|
2019-10-04 10:23:47 +00:00
|
|
|
return m_pad->ReadRegister(offset);
|
2019-09-20 06:47:41 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
void Bus::DoWritePad(MemoryAccessSize size, u32 offset, u32 value)
|
2019-09-20 06:47:41 +00:00
|
|
|
{
|
|
|
|
m_pad->WriteRegister(offset, value);
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
u32 Bus::DoReadSIO(MemoryAccessSize size, u32 offset)
|
2019-09-22 15:28:00 +00:00
|
|
|
{
|
2019-12-07 11:09:02 +00:00
|
|
|
return m_sio->ReadRegister(offset);
|
2019-09-22 15:28:00 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
void Bus::DoWriteSIO(MemoryAccessSize size, u32 offset, u32 value)
|
2019-09-22 15:28:00 +00:00
|
|
|
{
|
2019-12-07 11:09:02 +00:00
|
|
|
m_sio->WriteRegister(offset, value);
|
2019-09-22 15:28:00 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
u32 Bus::DoReadCDROM(MemoryAccessSize size, u32 offset)
|
2019-09-17 09:22:39 +00:00
|
|
|
{
|
|
|
|
// TODO: Splitting of half/word reads.
|
|
|
|
Assert(size == MemoryAccessSize::Byte);
|
2019-10-04 10:23:47 +00:00
|
|
|
return ZeroExtend32(m_cdrom->ReadRegister(offset));
|
2019-09-17 09:22:39 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
void Bus::DoWriteCDROM(MemoryAccessSize size, u32 offset, u32 value)
|
2019-09-17 09:22:39 +00:00
|
|
|
{
|
|
|
|
// TODO: Splitting of half/word reads.
|
|
|
|
Assert(size == MemoryAccessSize::Byte);
|
|
|
|
m_cdrom->WriteRegister(offset, Truncate8(value));
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
u32 Bus::DoReadGPU(MemoryAccessSize size, u32 offset)
|
2019-09-11 04:01:19 +00:00
|
|
|
{
|
|
|
|
Assert(size == MemoryAccessSize::Word);
|
2019-10-04 10:23:47 +00:00
|
|
|
return m_gpu->ReadRegister(offset);
|
2019-09-11 04:01:19 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
void Bus::DoWriteGPU(MemoryAccessSize size, u32 offset, u32 value)
|
2019-09-11 04:01:19 +00:00
|
|
|
{
|
|
|
|
Assert(size == MemoryAccessSize::Word);
|
|
|
|
m_gpu->WriteRegister(offset, value);
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
u32 Bus::DoReadMDEC(MemoryAccessSize size, u32 offset)
|
2019-09-29 02:51:34 +00:00
|
|
|
{
|
|
|
|
Assert(size == MemoryAccessSize::Word);
|
2019-10-04 10:23:47 +00:00
|
|
|
return m_mdec->ReadRegister(offset);
|
2019-09-29 02:51:34 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
void Bus::DoWriteMDEC(MemoryAccessSize size, u32 offset, u32 value)
|
2019-09-29 02:51:34 +00:00
|
|
|
{
|
|
|
|
Assert(size == MemoryAccessSize::Word);
|
|
|
|
m_mdec->WriteRegister(offset, value);
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
u32 Bus::DoReadInterruptController(MemoryAccessSize size, u32 offset)
|
2019-09-17 06:26:00 +00:00
|
|
|
{
|
2019-10-04 10:23:47 +00:00
|
|
|
u32 value = m_interrupt_controller->ReadRegister(offset);
|
2019-09-17 06:26:00 +00:00
|
|
|
FixupUnalignedWordAccessW32(offset, value);
|
2019-10-04 10:23:47 +00:00
|
|
|
return value;
|
2019-09-17 06:26:00 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
void Bus::DoWriteInterruptController(MemoryAccessSize size, u32 offset, u32 value)
|
2019-09-17 06:26:00 +00:00
|
|
|
{
|
|
|
|
FixupUnalignedWordAccessW32(offset, value);
|
|
|
|
m_interrupt_controller->WriteRegister(offset, value);
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
u32 Bus::DoReadTimers(MemoryAccessSize size, u32 offset)
|
2019-09-20 13:40:19 +00:00
|
|
|
{
|
2019-10-04 10:23:47 +00:00
|
|
|
u32 value = m_timers->ReadRegister(offset);
|
2019-09-20 13:40:19 +00:00
|
|
|
FixupUnalignedWordAccessW32(offset, value);
|
2019-10-04 10:23:47 +00:00
|
|
|
return value;
|
2019-09-20 13:40:19 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
void Bus::DoWriteTimers(MemoryAccessSize size, u32 offset, u32 value)
|
2019-09-20 13:40:19 +00:00
|
|
|
{
|
|
|
|
FixupUnalignedWordAccessW32(offset, value);
|
|
|
|
m_timers->WriteRegister(offset, value);
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
u32 Bus::DoReadSPU(MemoryAccessSize size, u32 offset)
|
2019-09-09 07:01:26 +00:00
|
|
|
{
|
2019-10-04 10:23:47 +00:00
|
|
|
// 32-bit reads are read as two 16-bit accesses.
|
2019-09-24 13:44:38 +00:00
|
|
|
if (size == MemoryAccessSize::Word)
|
2019-09-09 07:01:26 +00:00
|
|
|
{
|
2019-09-24 13:44:38 +00:00
|
|
|
const u16 lsb = m_spu->ReadRegister(offset);
|
|
|
|
const u16 msb = m_spu->ReadRegister(offset + 2);
|
2019-10-04 10:23:47 +00:00
|
|
|
return ZeroExtend32(lsb) | (ZeroExtend32(msb) << 16);
|
2019-09-24 13:44:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-10-04 10:23:47 +00:00
|
|
|
return ZeroExtend32(m_spu->ReadRegister(offset));
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
void Bus::DoWriteSPU(MemoryAccessSize size, u32 offset, u32 value)
|
2019-09-09 07:01:26 +00:00
|
|
|
{
|
2019-09-24 13:44:38 +00:00
|
|
|
// 32-bit writes are written as two 16-bit writes.
|
2019-10-04 10:23:47 +00:00
|
|
|
// TODO: Ignore if address is not aligned.
|
2019-09-24 13:44:38 +00:00
|
|
|
if (size == MemoryAccessSize::Word)
|
|
|
|
{
|
2019-10-04 10:23:47 +00:00
|
|
|
Assert(Common::IsAlignedPow2(offset, 2));
|
2019-09-24 13:44:38 +00:00
|
|
|
m_spu->WriteRegister(offset, Truncate16(value));
|
|
|
|
m_spu->WriteRegister(offset + 2, Truncate16(value >> 16));
|
2019-10-04 10:23:47 +00:00
|
|
|
return;
|
2019-09-24 13:44:38 +00:00
|
|
|
}
|
2019-09-09 07:01:26 +00:00
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
Assert(Common::IsAlignedPow2(offset, 2));
|
2019-09-24 13:44:38 +00:00
|
|
|
m_spu->WriteRegister(offset, Truncate16(value));
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 10:30:04 +00:00
|
|
|
void Bus::DoInvalidateCodeCache(u32 page_index)
|
|
|
|
{
|
2019-11-21 14:32:40 +00:00
|
|
|
m_cpu_code_cache->InvalidateBlocksWithPageIndex(page_index);
|
2019-11-19 10:30:04 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
u32 Bus::DoReadDMA(MemoryAccessSize size, u32 offset)
|
2019-09-09 07:01:26 +00:00
|
|
|
{
|
2019-10-05 06:29:14 +00:00
|
|
|
return FIXUP_WORD_READ_VALUE(offset, m_dma->ReadRegister(FIXUP_WORD_READ_OFFSET(offset)));
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:23:47 +00:00
|
|
|
void Bus::DoWriteDMA(MemoryAccessSize size, u32 offset, u32 value)
|
2019-09-09 07:01:26 +00:00
|
|
|
{
|
2019-09-30 07:21:57 +00:00
|
|
|
switch (size)
|
|
|
|
{
|
|
|
|
case MemoryAccessSize::Byte:
|
|
|
|
case MemoryAccessSize::HalfWord:
|
|
|
|
{
|
|
|
|
// zero extend length register
|
|
|
|
if ((offset & u32(0xF0)) < 7 && (offset & u32(0x0F)) == 0x4)
|
|
|
|
value = ZeroExtend32(value);
|
|
|
|
else
|
|
|
|
FixupUnalignedWordAccessW32(offset, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-09-09 07:01:26 +00:00
|
|
|
m_dma->WriteRegister(offset, value);
|
|
|
|
}
|