From 0aef634a54f786c49241c24b8d0c7aa3555b0fa1 Mon Sep 17 00:00:00 2001
From: Subv <subv2112@gmail.com>
Date: Sun, 15 May 2016 12:43:52 -0500
Subject: [PATCH] Memory: Handle RasterizerCachedMemory and
 RasterizerCachedSpecial page types in the memory block manipulation
 functions.

---
 src/core/hle/service/fs/archive.cpp |  1 -
 src/core/memory.cpp                 | 61 ++++++++++++++++++++++++++++-
 2 files changed, 60 insertions(+), 2 deletions(-)

diff --git a/src/core/hle/service/fs/archive.cpp b/src/core/hle/service/fs/archive.cpp
index a7ebfde254..81b9abe4cc 100644
--- a/src/core/hle/service/fs/archive.cpp
+++ b/src/core/hle/service/fs/archive.cpp
@@ -116,7 +116,6 @@ ResultVal<bool> File::SyncRequest() {
             }
             Memory::WriteBlock(address, data.data(), *read);
             cmd_buff[2] = static_cast<u32>(*read);
-            Memory::RasterizerFlushAndInvalidateRegion(Memory::VirtualToPhysicalAddress(address), length);
             break;
         }
 
diff --git a/src/core/memory.cpp b/src/core/memory.cpp
index 2ac3e7de41..8c9e5d46dd 100644
--- a/src/core/memory.cpp
+++ b/src/core/memory.cpp
@@ -392,6 +392,20 @@ void ReadBlock(const VAddr src_addr, void* dest_buffer, const size_t size) {
             GetMMIOHandler(current_vaddr)->ReadBlock(current_vaddr, dest_buffer, copy_amount);
             break;
         }
+        case PageType::RasterizerCachedMemory: {
+            RasterizerFlushRegion(VirtualToPhysicalAddress(current_vaddr), copy_amount);
+
+            std::memcpy(dest_buffer, GetPointerFromVMA(current_vaddr), copy_amount);
+            break;
+        }
+        case PageType::RasterizerCachedSpecial: {
+            DEBUG_ASSERT(GetMMIOHandler(current_vaddr));
+
+            RasterizerFlushRegion(VirtualToPhysicalAddress(current_vaddr), copy_amount);
+
+            GetMMIOHandler(current_vaddr)->ReadBlock(current_vaddr, dest_buffer, copy_amount);
+            break;
+        }
         default:
             UNREACHABLE();
         }
@@ -446,6 +460,20 @@ void WriteBlock(const VAddr dest_addr, const void* src_buffer, const size_t size
             GetMMIOHandler(current_vaddr)->WriteBlock(current_vaddr, src_buffer, copy_amount);
             break;
         }
+        case PageType::RasterizerCachedMemory: {
+            RasterizerFlushAndInvalidateRegion(VirtualToPhysicalAddress(current_vaddr), copy_amount);
+
+            std::memcpy(GetPointerFromVMA(current_vaddr), src_buffer, copy_amount);
+            break;
+        }
+        case PageType::RasterizerCachedSpecial: {
+            DEBUG_ASSERT(GetMMIOHandler(current_vaddr));
+
+            RasterizerFlushAndInvalidateRegion(VirtualToPhysicalAddress(current_vaddr), copy_amount);
+
+            GetMMIOHandler(current_vaddr)->WriteBlock(current_vaddr, src_buffer, copy_amount);
+            break;
+        }
         default:
             UNREACHABLE();
         }
@@ -462,6 +490,8 @@ void ZeroBlock(const VAddr dest_addr, const size_t size) {
     size_t page_index = dest_addr >> PAGE_BITS;
     size_t page_offset = dest_addr & PAGE_MASK;
 
+    static const std::array<u8, PAGE_SIZE> zeros = {};
+
     while (remaining_size > 0) {
         const size_t copy_amount = std::min(PAGE_SIZE - page_offset, remaining_size);
         const VAddr current_vaddr = (page_index << PAGE_BITS) + page_offset;
@@ -481,7 +511,20 @@ void ZeroBlock(const VAddr dest_addr, const size_t size) {
         case PageType::Special: {
             DEBUG_ASSERT(GetMMIOHandler(current_vaddr));
 
-            static const std::array<u8, PAGE_SIZE> zeros = {};
+            GetMMIOHandler(current_vaddr)->WriteBlock(current_vaddr, zeros.data(), copy_amount);
+            break;
+        }
+        case PageType::RasterizerCachedMemory: {
+            RasterizerFlushAndInvalidateRegion(VirtualToPhysicalAddress(current_vaddr), copy_amount);
+
+            std::memset(GetPointerFromVMA(current_vaddr), 0, copy_amount);
+            break;
+        }
+        case PageType::RasterizerCachedSpecial: {
+            DEBUG_ASSERT(GetMMIOHandler(current_vaddr));
+
+            RasterizerFlushAndInvalidateRegion(VirtualToPhysicalAddress(current_vaddr), copy_amount);
+
             GetMMIOHandler(current_vaddr)->WriteBlock(current_vaddr, zeros.data(), copy_amount);
             break;
         }
@@ -524,6 +567,22 @@ void CopyBlock(VAddr dest_addr, VAddr src_addr, const size_t size) {
             WriteBlock(dest_addr, buffer.data(), buffer.size());
             break;
         }
+        case PageType::RasterizerCachedMemory: {
+            RasterizerFlushRegion(VirtualToPhysicalAddress(current_vaddr), copy_amount);
+
+            WriteBlock(dest_addr, GetPointerFromVMA(current_vaddr), copy_amount);
+            break;
+        }
+        case PageType::RasterizerCachedSpecial: {
+            DEBUG_ASSERT(GetMMIOHandler(current_vaddr));
+
+            RasterizerFlushRegion(VirtualToPhysicalAddress(current_vaddr), copy_amount);
+
+            std::vector<u8> buffer(copy_amount);
+            GetMMIOHandler(current_vaddr)->ReadBlock(current_vaddr, buffer.data(), buffer.size());
+            WriteBlock(dest_addr, buffer.data(), buffer.size());
+            break;
+        }
         default:
             UNREACHABLE();
         }