From 40f123b7c0eaf1507d51f6b87192ec2f956e5d5e Mon Sep 17 00:00:00 2001
From: Tony Wasserka <NeoBrainX@gmail.com>
Date: Mon, 15 Dec 2014 21:28:45 +0100
Subject: [PATCH] Pica: Unify ugly address translation hacks.

---
 src/citra_qt/debugger/graphics_cmdlists.cpp   |  8 +++---
 .../debugger/graphics_framebuffer.cpp         |  8 +++---
 src/video_core/command_processor.cpp          |  4 +--
 src/video_core/debug_utils/debug_utils.cpp    |  2 +-
 src/video_core/debug_utils/debug_utils.h      |  2 +-
 src/video_core/pica.h                         | 25 +++++++++++++------
 src/video_core/rasterizer.cpp                 |  8 +++---
 7 files changed, 33 insertions(+), 24 deletions(-)

diff --git a/src/citra_qt/debugger/graphics_cmdlists.cpp b/src/citra_qt/debugger/graphics_cmdlists.cpp
index 01ff31d44..bf35f035f 100644
--- a/src/citra_qt/debugger/graphics_cmdlists.cpp
+++ b/src/citra_qt/debugger/graphics_cmdlists.cpp
@@ -47,7 +47,7 @@ public:
 };
 
 TextureInfoDockWidget::TextureInfoDockWidget(const Pica::DebugUtils::TextureInfo& info, QWidget* parent)
-    : QDockWidget(tr("Texture 0x%1").arg(info.address, 8, 16, QLatin1Char('0'))),
+    : QDockWidget(tr("Texture 0x%1").arg(info.physical_address, 8, 16, QLatin1Char('0'))),
       info(info) {
 
     QWidget* main_widget = new QWidget;
@@ -60,7 +60,7 @@ TextureInfoDockWidget::TextureInfoDockWidget(const Pica::DebugUtils::TextureInfo
     phys_address_spinbox->SetBase(16);
     phys_address_spinbox->SetRange(0, 0xFFFFFFFF);
     phys_address_spinbox->SetPrefix("0x");
-    phys_address_spinbox->SetValue(info.address);
+    phys_address_spinbox->SetValue(info.physical_address);
     connect(phys_address_spinbox, SIGNAL(ValueChanged(qint64)), this, SLOT(OnAddressChanged(qint64)));
 
     QComboBox* format_choice = new QComboBox;
@@ -125,7 +125,7 @@ TextureInfoDockWidget::TextureInfoDockWidget(const Pica::DebugUtils::TextureInfo
 }
 
 void TextureInfoDockWidget::OnAddressChanged(qint64 value) {
-    info.address = value;
+    info.physical_address = value;
     emit UpdatePixmap(ReloadPixmap());
 }
 
@@ -150,7 +150,7 @@ void TextureInfoDockWidget::OnStrideChanged(int value) {
 }
 
 QPixmap TextureInfoDockWidget::ReloadPixmap() const {
-    u8* src = Memory::GetPointer(info.address);
+    u8* src = Memory::GetPointer(Pica::PAddrToVAddr(info.physical_address));
     return QPixmap::fromImage(LoadTexture(src, info));
 }
 
diff --git a/src/citra_qt/debugger/graphics_framebuffer.cpp b/src/citra_qt/debugger/graphics_framebuffer.cpp
index c055299a4..484be1db5 100644
--- a/src/citra_qt/debugger/graphics_framebuffer.cpp
+++ b/src/citra_qt/debugger/graphics_framebuffer.cpp
@@ -199,7 +199,7 @@ void GraphicsFramebufferWidget::OnUpdate()
         auto framebuffer = Pica::registers.framebuffer;
         using Framebuffer = decltype(framebuffer);
 
-        framebuffer_address = framebuffer.GetColorBufferAddress();
+        framebuffer_address = framebuffer.GetColorBufferPhysicalAddress();
         framebuffer_width = framebuffer.GetWidth();
         framebuffer_height = framebuffer.GetHeight();
         framebuffer_format = static_cast<Format>(framebuffer.color_format);
@@ -224,7 +224,7 @@ void GraphicsFramebufferWidget::OnUpdate()
     case Format::RGBA8:
     {
         QImage decoded_image(framebuffer_width, framebuffer_height, QImage::Format_ARGB32);
-        u32* color_buffer = (u32*)Memory::GetPointer(framebuffer_address);
+        u32* color_buffer = (u32*)Memory::GetPointer(Pica::PAddrToVAddr(framebuffer_address));
         for (unsigned y = 0; y < framebuffer_height; ++y) {
             for (unsigned x = 0; x < framebuffer_width; ++x) {
                 u32 value = *(color_buffer + x + y * framebuffer_width);
@@ -239,7 +239,7 @@ void GraphicsFramebufferWidget::OnUpdate()
     case Format::RGB8:
     {
         QImage decoded_image(framebuffer_width, framebuffer_height, QImage::Format_ARGB32);
-        u8* color_buffer = Memory::GetPointer(framebuffer_address);
+        u8* color_buffer = Memory::GetPointer(Pica::PAddrToVAddr(framebuffer_address));
         for (unsigned y = 0; y < framebuffer_height; ++y) {
             for (unsigned x = 0; x < framebuffer_width; ++x) {
                 u8* pixel_pointer = color_buffer + x * 3 + y * 3 * framebuffer_width;
@@ -254,7 +254,7 @@ void GraphicsFramebufferWidget::OnUpdate()
     case Format::RGBA5551:
     {
         QImage decoded_image(framebuffer_width, framebuffer_height, QImage::Format_ARGB32);
-        u32* color_buffer = (u32*)Memory::GetPointer(framebuffer_address);
+        u32* color_buffer = (u32*)Memory::GetPointer(Pica::PAddrToVAddr(framebuffer_address));
         for (unsigned y = 0; y < framebuffer_height; ++y) {
             for (unsigned x = 0; x < framebuffer_width; ++x) {
                 u16 value = *(u16*)(((u8*)color_buffer) + x * 2 + y * framebuffer_width * 2);
diff --git a/src/video_core/command_processor.cpp b/src/video_core/command_processor.cpp
index b74cd3261..3d06ac7e6 100644
--- a/src/video_core/command_processor.cpp
+++ b/src/video_core/command_processor.cpp
@@ -56,7 +56,7 @@ static inline void WritePicaReg(u32 id, u32 value, u32 mask) {
                 g_debug_context->OnEvent(DebugContext::Event::IncomingPrimitiveBatch, nullptr);
 
             const auto& attribute_config = registers.vertex_attributes;
-            const u8* const base_address = Memory::GetPointer(attribute_config.GetBaseAddress());
+            const u8* const base_address = Memory::GetPointer(PAddrToVAddr(attribute_config.GetPhysicalBaseAddress()));
 
             // Information about internal vertex attributes
             const u8* vertex_attribute_sources[16];
@@ -116,7 +116,7 @@ static inline void WritePicaReg(u32 id, u32 value, u32 mask) {
                         input.attr[i][comp] = float24::FromFloat32(srcval);
                         LOG_TRACE(HW_GPU, "Loaded component %x of attribute %x for vertex %x (index %x) from 0x%08x + 0x%08lx + 0x%04lx: %f",
                                   comp, i, vertex, index,
-                                  attribute_config.GetBaseAddress(),
+                                  PAddrToVAddr(attribute_config.GetPhysicalBaseAddress()),
                                   vertex_attribute_sources[i] - base_address,
                                   srcdata - vertex_attribute_sources[i],
                                   input.attr[i][comp].ToFloat32());
diff --git a/src/video_core/debug_utils/debug_utils.cpp b/src/video_core/debug_utils/debug_utils.cpp
index 3cc22f436..08ecd4ccb 100644
--- a/src/video_core/debug_utils/debug_utils.cpp
+++ b/src/video_core/debug_utils/debug_utils.cpp
@@ -455,7 +455,7 @@ TextureInfo TextureInfo::FromPicaRegister(const Regs::TextureConfig& config,
                                           const Regs::TextureFormat& format)
 {
     TextureInfo info;
-    info.address = config.GetPhysicalAddress();
+    info.physical_address = config.GetPhysicalAddress();
     info.width = config.width;
     info.height = config.height;
     info.format = format;
diff --git a/src/video_core/debug_utils/debug_utils.h b/src/video_core/debug_utils/debug_utils.h
index f950356f3..2a764e121 100644
--- a/src/video_core/debug_utils/debug_utils.h
+++ b/src/video_core/debug_utils/debug_utils.h
@@ -192,7 +192,7 @@ void OnPicaRegWrite(u32 id, u32 value);
 std::unique_ptr<PicaTrace> FinishPicaTracing();
 
 struct TextureInfo {
-    unsigned int address;
+    PAddr physical_address;
     int width;
     int height;
     int stride;
diff --git a/src/video_core/pica.h b/src/video_core/pica.h
index 5712439e1..7d82d733d 100644
--- a/src/video_core/pica.h
+++ b/src/video_core/pica.h
@@ -127,7 +127,7 @@ struct Regs {
         u32 address;
 
         u32 GetPhysicalAddress() const {
-            return DecodeAddressRegister(address) - Memory::FCRAM_PADDR + Memory::HEAP_LINEAR_VADDR;
+            return DecodeAddressRegister(address);
         }
 
         // texture1 and texture2 store the texture format directly after the address
@@ -317,11 +317,11 @@ struct Regs {
 
         INSERT_PADDING_WORDS(0x1);
 
-        inline u32 GetColorBufferAddress() const {
-            return Memory::PhysicalToVirtualAddress(DecodeAddressRegister(color_buffer_address));
+        inline u32 GetColorBufferPhysicalAddress() const {
+            return DecodeAddressRegister(color_buffer_address);
         }
-        inline u32 GetDepthBufferAddress() const {
-            return Memory::PhysicalToVirtualAddress(DecodeAddressRegister(depth_buffer_address));
+        inline u32 GetDepthBufferPhysicalAddress() const {
+            return DecodeAddressRegister(depth_buffer_address);
         }
 
         inline u32 GetWidth() const {
@@ -345,9 +345,8 @@ struct Regs {
 
         BitField<0, 29, u32> base_address;
 
-        inline u32 GetBaseAddress() const {
-            // TODO: Ugly, should fix PhysicalToVirtualAddress instead
-            return DecodeAddressRegister(base_address) - Memory::FCRAM_PADDR + Memory::HEAP_LINEAR_VADDR;
+        u32 GetPhysicalBaseAddress() const {
+            return DecodeAddressRegister(base_address);
         }
 
         // Descriptor for internal vertex attributes
@@ -779,5 +778,15 @@ 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::HEAP_LINEAR_VADDR;
+    } else {
+        return 0;
+    }
+}
 
 } // namespace
diff --git a/src/video_core/rasterizer.cpp b/src/video_core/rasterizer.cpp
index 25efd49c8..bd79e4413 100644
--- a/src/video_core/rasterizer.cpp
+++ b/src/video_core/rasterizer.cpp
@@ -18,7 +18,7 @@ namespace Pica {
 namespace Rasterizer {
 
 static void DrawPixel(int x, int y, const Math::Vec4<u8>& color) {
-    u32* color_buffer = (u32*)Memory::GetPointer(registers.framebuffer.GetColorBufferAddress());
+    u32* color_buffer = (u32*)Memory::GetPointer(PAddrToVAddr(registers.framebuffer.GetColorBufferPhysicalAddress()));
     u32 value = (color.a() << 24) | (color.r() << 16) | (color.g() << 8) | color.b();
 
     // Assuming RGBA8 format until actual framebuffer format handling is implemented
@@ -26,14 +26,14 @@ static void DrawPixel(int x, int y, const Math::Vec4<u8>& color) {
 }
 
 static u32 GetDepth(int x, int y) {
-    u16* depth_buffer = (u16*)Memory::GetPointer(registers.framebuffer.GetDepthBufferAddress());
+    u16* depth_buffer = (u16*)Memory::GetPointer(PAddrToVAddr(registers.framebuffer.GetDepthBufferPhysicalAddress()));
 
     // Assuming 16-bit depth buffer format until actual format handling is implemented
     return *(depth_buffer + x + y * registers.framebuffer.GetWidth());
 }
 
 static void SetDepth(int x, int y, u16 value) {
-    u16* depth_buffer = (u16*)Memory::GetPointer(registers.framebuffer.GetDepthBufferAddress());
+    u16* depth_buffer = (u16*)Memory::GetPointer(PAddrToVAddr(registers.framebuffer.GetDepthBufferPhysicalAddress()));
 
     // Assuming 16-bit depth buffer format until actual format handling is implemented
     *(depth_buffer + x + y * registers.framebuffer.GetWidth()) = value;
@@ -204,7 +204,7 @@ void ProcessTriangle(const VertexShader::OutputVertex& v0,
                 s = GetWrappedTexCoord(registers.texture0.wrap_s, s, registers.texture0.width);
                 t = GetWrappedTexCoord(registers.texture0.wrap_t, t, registers.texture0.height);
 
-                u8* texture_data = Memory::GetPointer(texture.config.GetPhysicalAddress());
+                u8* texture_data = Memory::GetPointer(PAddrToVAddr(texture.config.GetPhysicalAddress()));
                 auto info = DebugUtils::TextureInfo::FromPicaRegister(texture.config, texture.format);
 
                 texture_color[i] = DebugUtils::LookupTexture(texture_data, s, t, info);