From 28a9e4c1d55c66e99b5cf16fda00dcb75ab27fde Mon Sep 17 00:00:00 2001
From: Yuri Kunde Schlesner <yuriks@yuriks.net>
Date: Sat, 9 May 2015 03:08:11 -0300
Subject: [PATCH] Memory: Support more regions in the VAddr-PAddr translation
 functions

Also adds better documentation and removes the one-off reimplementation
of the function in pica.h.
---
 src/citra_qt/debugger/graphics_cmdlists.cpp   |  4 +-
 .../debugger/graphics_framebuffer.cpp         |  2 +-
 src/core/mem_map.h                            | 13 +++--
 src/core/mem_map_funcs.cpp                    | 52 +++++++++----------
 src/video_core/command_processor.cpp          |  4 +-
 src/video_core/pica.h                         | 11 ----
 src/video_core/rasterizer.cpp                 | 10 ++--
 7 files changed, 45 insertions(+), 51 deletions(-)

diff --git a/src/citra_qt/debugger/graphics_cmdlists.cpp b/src/citra_qt/debugger/graphics_cmdlists.cpp
index 9bcd25821..6727acf18 100644
--- a/src/citra_qt/debugger/graphics_cmdlists.cpp
+++ b/src/citra_qt/debugger/graphics_cmdlists.cpp
@@ -159,7 +159,7 @@ void TextureInfoDockWidget::OnStrideChanged(int value) {
 }
 
 QPixmap TextureInfoDockWidget::ReloadPixmap() const {
-    u8* src = Memory::GetPointer(Pica::PAddrToVAddr(info.physical_address));
+    u8* src = Memory::GetPointer(Memory::PhysicalToVirtualAddress(info.physical_address));
     return QPixmap::fromImage(LoadTexture(src, info));
 }
 
@@ -274,7 +274,7 @@ void GPUCommandListWidget::SetCommandInfo(const QModelIndex& index) {
         auto format = Pica::registers.GetTextures()[index].format;
 
         auto info = Pica::DebugUtils::TextureInfo::FromPicaRegister(config, format);
-        u8* src = Memory::GetPointer(Pica::PAddrToVAddr(config.GetPhysicalAddress()));
+        u8* src = Memory::GetPointer(Memory::PhysicalToVirtualAddress(config.GetPhysicalAddress()));
         new_info_widget = new TextureInfoWidget(src, info);
     } else {
         new_info_widget = new QWidget;
diff --git a/src/citra_qt/debugger/graphics_framebuffer.cpp b/src/citra_qt/debugger/graphics_framebuffer.cpp
index d621d7204..fa101a6a2 100644
--- a/src/citra_qt/debugger/graphics_framebuffer.cpp
+++ b/src/citra_qt/debugger/graphics_framebuffer.cpp
@@ -215,7 +215,7 @@ void GraphicsFramebufferWidget::OnUpdate()
     u32 bytes_per_pixel = GraphicsFramebufferWidget::BytesPerPixel(framebuffer_format);
 
     QImage decoded_image(framebuffer_width, framebuffer_height, QImage::Format_ARGB32);
-    u8* buffer = Memory::GetPointer(Pica::PAddrToVAddr(framebuffer_address));
+    u8* buffer = Memory::GetPointer(Memory::PhysicalToVirtualAddress(framebuffer_address));
 
     for (unsigned int y = 0; y < framebuffer_height; ++y) {
         for (unsigned int x = 0; x < framebuffer_width; ++x) {
diff --git a/src/core/mem_map.h b/src/core/mem_map.h
index 1591fc0a9..5a08cc105 100644
--- a/src/core/mem_map.h
+++ b/src/core/mem_map.h
@@ -181,10 +181,15 @@ inline const char* GetCharPointer(const VAddr address) {
     return (const char *)GetPointer(address);
 }
 
-/// Converts a physical address to virtual address
-VAddr PhysicalToVirtualAddress(PAddr addr);
-
-/// Converts a virtual address to physical address
+/**
+ * Converts a virtual address inside a region with 1:1 mapping to physical memory to a physical
+ * address. This should be used by services to translate addresses for use by the hardware.
+ */
 PAddr VirtualToPhysicalAddress(VAddr addr);
 
+/**
+ * Undoes a mapping performed by VirtualToPhysicalAddress().
+ */
+VAddr PhysicalToVirtualAddress(PAddr addr);
+
 } // namespace
diff --git a/src/core/mem_map_funcs.cpp b/src/core/mem_map_funcs.cpp
index f96ae6e9e..a8e0fed07 100644
--- a/src/core/mem_map_funcs.cpp
+++ b/src/core/mem_map_funcs.cpp
@@ -18,40 +18,40 @@ namespace Memory {
 static std::map<u32, MemoryBlock> heap_map;
 static std::map<u32, MemoryBlock> heap_linear_map;
 
-/// Convert a physical address to virtual address
-VAddr PhysicalToVirtualAddress(const PAddr addr) {
-    // Our memory interface read/write functions assume virtual addresses. Put any physical address
-    // to virtual address translations here. This is quite hacky, but necessary until we implement
-    // proper MMU emulation.
-    // TODO: Screw it, I'll let bunnei figure out how to do this properly.
-    if (addr == 0) {
-        return 0;
-    } else if ((addr >= VRAM_PADDR) && (addr < VRAM_PADDR_END)) {
-        return addr - VRAM_PADDR + VRAM_VADDR;
-    } else if ((addr >= FCRAM_PADDR) && (addr < FCRAM_PADDR_END)) {
-        return addr - FCRAM_PADDR + LINEAR_HEAP_VADDR;
-    }
-
-    LOG_ERROR(HW_Memory, "Unknown physical address @ 0x%08x", addr);
-    return addr;
-}
-
-/// Convert a physical address to virtual address
 PAddr VirtualToPhysicalAddress(const VAddr addr) {
-    // Our memory interface read/write functions assume virtual addresses. Put any physical address
-    // to virtual address translations here. This is quite hacky, but necessary until we implement
-    // proper MMU emulation.
-    // TODO: Screw it, I'll let bunnei figure out how to do this properly.
     if (addr == 0) {
         return 0;
-    } else if ((addr >= VRAM_VADDR) && (addr < VRAM_VADDR_END)) {
+    } else if (addr >= VRAM_VADDR && addr < VRAM_VADDR_END) {
         return addr - VRAM_VADDR + VRAM_PADDR;
-    } else if ((addr >= LINEAR_HEAP_VADDR) && (addr < LINEAR_HEAP_VADDR_END)) {
+    } else if (addr >= LINEAR_HEAP_VADDR && addr < LINEAR_HEAP_VADDR_END) {
         return addr - LINEAR_HEAP_VADDR + FCRAM_PADDR;
+    } else if (addr >= DSP_RAM_VADDR && addr < DSP_RAM_VADDR_END) {
+        return addr - DSP_RAM_VADDR + DSP_RAM_PADDR;
+    } else if (addr >= IO_AREA_VADDR && addr < IO_AREA_VADDR_END) {
+        return addr - IO_AREA_VADDR + IO_AREA_PADDR;
     }
 
     LOG_ERROR(HW_Memory, "Unknown virtual address @ 0x%08x", addr);
-    return addr;
+    // To help with debugging, set bit on address so that it's obviously invalid.
+    return addr | 0x80000000;
+}
+
+VAddr PhysicalToVirtualAddress(const PAddr addr) {
+    if (addr == 0) {
+        return 0;
+    } else if (addr >= VRAM_PADDR && addr < VRAM_PADDR_END) {
+        return addr - VRAM_PADDR + VRAM_VADDR;
+    } else if (addr >= FCRAM_PADDR && addr < FCRAM_PADDR_END) {
+        return addr - FCRAM_PADDR + LINEAR_HEAP_VADDR;
+    } else if (addr >= DSP_RAM_PADDR && addr < DSP_RAM_PADDR_END) {
+        return addr - DSP_RAM_PADDR + DSP_RAM_VADDR;
+    } else if (addr >= IO_AREA_PADDR && addr < IO_AREA_PADDR_END) {
+        return addr - IO_AREA_PADDR + IO_AREA_VADDR;
+    }
+
+    LOG_ERROR(HW_Memory, "Unknown physical address @ 0x%08x", addr);
+    // To help with debugging, set bit on address so that it's obviously invalid.
+    return addr | 0x80000000;
 }
 
 template <typename T>
diff --git a/src/video_core/command_processor.cpp b/src/video_core/command_processor.cpp
index c4cdf672b..7c4047e33 100644
--- a/src/video_core/command_processor.cpp
+++ b/src/video_core/command_processor.cpp
@@ -102,7 +102,7 @@ static inline void WritePicaReg(u32 id, u32 value, u32 mask) {
             bool is_indexed = (id == PICA_REG_INDEX(trigger_draw_indexed));
 
             const auto& index_info = registers.index_array;
-            const u8* index_address_8 = Memory::GetPointer(PAddrToVAddr(base_address + index_info.offset));
+            const u8* index_address_8 = Memory::GetPointer(Memory::PhysicalToVirtualAddress(base_address + index_info.offset));
             const u16* index_address_16 = (u16*)index_address_8;
             bool index_u16 = index_info.format != 0;
 
@@ -135,7 +135,7 @@ static inline void WritePicaReg(u32 id, u32 value, u32 mask) {
                                   input.attr[i][2].ToFloat32(), input.attr[i][3].ToFloat32());
                     } else {
                         for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
-                            const u8* srcdata = Memory::GetPointer(PAddrToVAddr(vertex_attribute_sources[i] + vertex_attribute_strides[i] * vertex + comp * vertex_attribute_element_size[i]));
+                            const u8* srcdata = Memory::GetPointer(Memory::PhysicalToVirtualAddress(vertex_attribute_sources[i] + vertex_attribute_strides[i] * vertex + comp * vertex_attribute_element_size[i]));
 
                             const float srcval = (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::BYTE) ? *(s8*)srcdata :
                                 (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::UBYTE) ? *(u8*)srcdata :
diff --git a/src/video_core/pica.h b/src/video_core/pica.h
index 4c48b6bf7..e4a91058c 100644
--- a/src/video_core/pica.h
+++ b/src/video_core/pica.h
@@ -998,15 +998,4 @@ union CommandHeader {
     BitField<31,  1, u32> group_commands;
 };
 
-// TODO: Ugly, should fix PhysicalToVirtualAddress instead
-inline static u32 PAddrToVAddr(u32 addr) {
-    if (addr >= Memory::VRAM_PADDR && addr < Memory::VRAM_PADDR + Memory::VRAM_SIZE) {
-        return addr - Memory::VRAM_PADDR + Memory::VRAM_VADDR;
-    } else if (addr >= Memory::FCRAM_PADDR && addr < Memory::FCRAM_PADDR + Memory::FCRAM_SIZE) {
-        return addr - Memory::FCRAM_PADDR + Memory::LINEAR_HEAP_VADDR;
-    } else {
-        return 0;
-    }
-}
-
 } // namespace
diff --git a/src/video_core/rasterizer.cpp b/src/video_core/rasterizer.cpp
index 6ec253601..ab7776929 100644
--- a/src/video_core/rasterizer.cpp
+++ b/src/video_core/rasterizer.cpp
@@ -30,7 +30,7 @@ static void DrawPixel(int x, int y, const Math::Vec4<u8>& color) {
     const u32 coarse_y = y & ~7;
     u32 bytes_per_pixel = GPU::Regs::BytesPerPixel(GPU::Regs::PixelFormat(registers.framebuffer.color_format.Value()));
     u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * registers.framebuffer.width * bytes_per_pixel;
-    u8* dst_pixel = Memory::GetPointer(PAddrToVAddr(addr)) + dst_offset;
+    u8* dst_pixel = Memory::GetPointer(Memory::PhysicalToVirtualAddress(addr)) + dst_offset;
 
     switch (registers.framebuffer.color_format) {
     case registers.framebuffer.RGBA8:
@@ -67,7 +67,7 @@ static const Math::Vec4<u8> GetPixel(int x, int y) {
     const u32 coarse_y = y & ~7;
     u32 bytes_per_pixel = GPU::Regs::BytesPerPixel(GPU::Regs::PixelFormat(registers.framebuffer.color_format.Value()));
     u32 src_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * registers.framebuffer.width * bytes_per_pixel;
-    u8* src_pixel = Memory::GetPointer(PAddrToVAddr(addr)) + src_offset;
+    u8* src_pixel = Memory::GetPointer(Memory::PhysicalToVirtualAddress(addr)) + src_offset;
 
     switch (registers.framebuffer.color_format) {
     case registers.framebuffer.RGBA8:
@@ -95,7 +95,7 @@ static const Math::Vec4<u8> GetPixel(int x, int y) {
 
 static u32 GetDepth(int x, int y) {
     const PAddr addr = registers.framebuffer.GetDepthBufferPhysicalAddress();
-    u8* depth_buffer = Memory::GetPointer(PAddrToVAddr(addr));
+    u8* depth_buffer = Memory::GetPointer(Memory::PhysicalToVirtualAddress(addr));
 
     y = (registers.framebuffer.height - y);
     
@@ -122,7 +122,7 @@ static u32 GetDepth(int x, int y) {
 
 static void SetDepth(int x, int y, u32 value) {
     const PAddr addr = registers.framebuffer.GetDepthBufferPhysicalAddress();
-    u8* depth_buffer = Memory::GetPointer(PAddrToVAddr(addr));
+    u8* depth_buffer = Memory::GetPointer(Memory::PhysicalToVirtualAddress(addr));
 
     y = (registers.framebuffer.height - y);
 
@@ -361,7 +361,7 @@ static void ProcessTriangleInternal(const VertexShader::OutputVertex& v0,
                 s = GetWrappedTexCoord(texture.config.wrap_s, s, texture.config.width);
                 t = texture.config.height - 1 - GetWrappedTexCoord(texture.config.wrap_t, t, texture.config.height);
 
-                u8* texture_data = Memory::GetPointer(PAddrToVAddr(texture.config.GetPhysicalAddress()));
+                u8* texture_data = Memory::GetPointer(Memory::PhysicalToVirtualAddress(texture.config.GetPhysicalAddress()));
                 auto info = DebugUtils::TextureInfo::FromPicaRegister(texture.config, texture.format);
 
                 texture_color[i] = DebugUtils::LookupTexture(texture_data, s, t, info);