From 519dbc818d86ea6311d0b29884eb2a52930bf17e Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Tue, 26 Nov 2019 19:45:36 +1000 Subject: [PATCH] CPU/CodeCache: Fix DMA writes not invalidating code blocks Fixes Crash Team Racing and Spyro in Cached Interpreter/Recompiler modes. --- src/core/bus.cpp | 8 ++++++++ src/core/cpu_code_cache.cpp | 4 ++-- src/core/cpu_types.h | 5 +---- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/core/bus.cpp b/src/core/bus.cpp index 0c57699fe..5295ef7b9 100644 --- a/src/core/bus.cpp +++ b/src/core/bus.cpp @@ -165,6 +165,14 @@ TickCount Bus::WriteWords(PhysicalMemoryAddress address, const u32* words, u32 w return total_ticks; } + 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); + } + std::memcpy(&m_ram[address], words, sizeof(u32) * word_count); return static_cast(word_count + ((word_count + 15) / 16)); } diff --git a/src/core/cpu_code_cache.cpp b/src/core/cpu_code_cache.cpp index 9a3928504..f99488ca7 100644 --- a/src/core/cpu_code_cache.cpp +++ b/src/core/cpu_code_cache.cpp @@ -357,7 +357,7 @@ void CodeCache::AddBlockToPageMap(CodeBlock* block) const u32 start_page = block->GetStartPageIndex(); const u32 end_page = block->GetEndPageIndex(); - for (u32 page = start_page; page < end_page; page++) + for (u32 page = start_page; page <= end_page; page++) { m_ram_block_map[page].push_back(block); m_bus->SetRAMCodePage(page); @@ -371,7 +371,7 @@ void CodeCache::RemoveBlockFromPageMap(CodeBlock* block) const u32 start_page = block->GetStartPageIndex(); const u32 end_page = block->GetEndPageIndex(); - for (u32 page = start_page; page < end_page; page++) + for (u32 page = start_page; page <= end_page; page++) { auto& page_blocks = m_ram_block_map[page]; auto page_block_iter = std::find(page_blocks.begin(), page_blocks.end(), block); diff --git a/src/core/cpu_types.h b/src/core/cpu_types.h index eeda716f8..0480026e3 100644 --- a/src/core/cpu_types.h +++ b/src/core/cpu_types.h @@ -435,10 +435,7 @@ struct CodeBlock const u32 GetPC() const { return key.GetPC(); } const u32 GetSizeInBytes() const { return static_cast(instructions.size()) * sizeof(Instruction); } const u32 GetStartPageIndex() const { return (key.GetPC() / CPU_CODE_CACHE_PAGE_SIZE); } - const u32 GetEndPageIndex() const - { - return ((key.GetPC() + GetSizeInBytes() + (CPU_CODE_CACHE_PAGE_SIZE - 1)) / CPU_CODE_CACHE_PAGE_SIZE); - } + const u32 GetEndPageIndex() const { return ((key.GetPC() + GetSizeInBytes()) / CPU_CODE_CACHE_PAGE_SIZE); } bool IsInRAM() const { // TODO: Constant