gpu.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #include "common/common_types.h"
  5. #include "common/log.h"
  6. #include "core/core.h"
  7. #include "core/mem_map.h"
  8. #include "core/hle/hle.h"
  9. #include "core/hle/kernel/thread.h"
  10. #include "core/hle/service/gsp.h"
  11. #include "core/hw/gpu.h"
  12. #include "video_core/command_processor.h"
  13. #include "video_core/video_core.h"
  14. namespace GPU {
  15. Regs g_regs;
  16. u32 g_cur_line = 0; ///< Current vertical screen line
  17. u64 g_last_line_ticks = 0; ///< CPU tick count from last vertical screen line
  18. template <typename T>
  19. inline void Read(T &var, const u32 raw_addr) {
  20. u32 addr = raw_addr - 0x1EF00000;
  21. int index = addr / 4;
  22. // Reads other than u32 are untested, so I'd rather have them abort than silently fail
  23. if (index >= Regs::NumIds() || !std::is_same<T,u32>::value) {
  24. ERROR_LOG(GPU, "unknown Read%d @ 0x%08X", sizeof(var) * 8, addr);
  25. return;
  26. }
  27. var = g_regs[addr / 4];
  28. }
  29. template <typename T>
  30. inline void Write(u32 addr, const T data) {
  31. addr -= 0x1EF00000;
  32. int index = addr / 4;
  33. // Writes other than u32 are untested, so I'd rather have them abort than silently fail
  34. if (index >= Regs::NumIds() || !std::is_same<T,u32>::value) {
  35. ERROR_LOG(GPU, "unknown Write%d 0x%08X @ 0x%08X", sizeof(data) * 8, data, addr);
  36. return;
  37. }
  38. g_regs[index] = data;
  39. switch (index) {
  40. // Memory fills are triggered once the fill value is written.
  41. // NOTE: This is not verified.
  42. case GPU_REG_INDEX_WORKAROUND(memory_fill_config[0].value, 0x00004 + 0x3):
  43. case GPU_REG_INDEX_WORKAROUND(memory_fill_config[1].value, 0x00008 + 0x3):
  44. {
  45. const bool is_second_filler = (index != GPU_REG_INDEX(memory_fill_config[0].value));
  46. const auto& config = g_regs.memory_fill_config[is_second_filler];
  47. // TODO: Not sure if this check should be done at GSP level instead
  48. if (config.address_start) {
  49. // TODO: Not sure if this algorithm is correct, particularly because it doesn't use the size member at all
  50. u32* start = (u32*)Memory::GetPointer(Memory::PhysicalToVirtualAddress(config.GetStartAddress()));
  51. u32* end = (u32*)Memory::GetPointer(Memory::PhysicalToVirtualAddress(config.GetEndAddress()));
  52. for (u32* ptr = start; ptr < end; ++ptr)
  53. *ptr = bswap32(config.value); // TODO: This is just a workaround to missing framebuffer format emulation
  54. DEBUG_LOG(GPU, "MemoryFill from 0x%08x to 0x%08x", config.GetStartAddress(), config.GetEndAddress());
  55. }
  56. break;
  57. }
  58. case GPU_REG_INDEX(display_transfer_config.trigger):
  59. {
  60. const auto& config = g_regs.display_transfer_config;
  61. if (config.trigger & 1) {
  62. u8* source_pointer = Memory::GetPointer(Memory::PhysicalToVirtualAddress(config.GetPhysicalInputAddress()));
  63. u8* dest_pointer = Memory::GetPointer(Memory::PhysicalToVirtualAddress(config.GetPhysicalOutputAddress()));
  64. for (int y = 0; y < config.output_height; ++y) {
  65. // TODO: Why does the register seem to hold twice the framebuffer width?
  66. for (int x = 0; x < config.output_width; ++x) {
  67. struct {
  68. int r, g, b, a;
  69. } source_color = { 0, 0, 0, 0 };
  70. switch (config.input_format) {
  71. case Regs::FramebufferFormat::RGBA8:
  72. {
  73. // TODO: Most likely got the component order messed up.
  74. u8* srcptr = source_pointer + x * 4 + y * config.input_width * 4;
  75. source_color.r = srcptr[0]; // blue
  76. source_color.g = srcptr[1]; // green
  77. source_color.b = srcptr[2]; // red
  78. source_color.a = srcptr[3]; // alpha
  79. break;
  80. }
  81. default:
  82. ERROR_LOG(GPU, "Unknown source framebuffer format %x", config.input_format.Value());
  83. break;
  84. }
  85. switch (config.output_format) {
  86. /*case Regs::FramebufferFormat::RGBA8:
  87. {
  88. // TODO: Untested
  89. u8* dstptr = (u32*)(dest_pointer + x * 4 + y * config.output_width * 4);
  90. dstptr[0] = source_color.r;
  91. dstptr[1] = source_color.g;
  92. dstptr[2] = source_color.b;
  93. dstptr[3] = source_color.a;
  94. break;
  95. }*/
  96. case Regs::FramebufferFormat::RGB8:
  97. {
  98. // TODO: Most likely got the component order messed up.
  99. u8* dstptr = dest_pointer + x * 3 + y * config.output_width * 3;
  100. dstptr[0] = source_color.r; // blue
  101. dstptr[1] = source_color.g; // green
  102. dstptr[2] = source_color.b; // red
  103. break;
  104. }
  105. default:
  106. ERROR_LOG(GPU, "Unknown destination framebuffer format %x", config.output_format.Value());
  107. break;
  108. }
  109. }
  110. }
  111. DEBUG_LOG(GPU, "DisplayTriggerTransfer: 0x%08x bytes from 0x%08x(%dx%d)-> 0x%08x(%dx%d), dst format %x",
  112. config.output_height * config.output_width * 4,
  113. config.GetPhysicalInputAddress(), (int)config.input_width, (int)config.input_height,
  114. config.GetPhysicalOutputAddress(), (int)config.output_width, (int)config.output_height,
  115. config.output_format.Value());
  116. }
  117. break;
  118. }
  119. // Seems like writing to this register triggers processing
  120. case GPU_REG_INDEX(command_processor_config.trigger):
  121. {
  122. const auto& config = g_regs.command_processor_config;
  123. if (config.trigger & 1)
  124. {
  125. u32* buffer = (u32*)Memory::GetPointer(Memory::PhysicalToVirtualAddress(config.GetPhysicalAddress()));
  126. u32 size = config.size << 3;
  127. Pica::CommandProcessor::ProcessCommandList(buffer, size);
  128. }
  129. break;
  130. }
  131. default:
  132. break;
  133. }
  134. }
  135. // Explicitly instantiate template functions because we aren't defining this in the header:
  136. template void Read<u64>(u64 &var, const u32 addr);
  137. template void Read<u32>(u32 &var, const u32 addr);
  138. template void Read<u16>(u16 &var, const u32 addr);
  139. template void Read<u8>(u8 &var, const u32 addr);
  140. template void Write<u64>(u32 addr, const u64 data);
  141. template void Write<u32>(u32 addr, const u32 data);
  142. template void Write<u16>(u32 addr, const u16 data);
  143. template void Write<u8>(u32 addr, const u8 data);
  144. /// Update hardware
  145. void Update() {
  146. auto& framebuffer_top = g_regs.framebuffer_config[0];
  147. u64 current_ticks = Core::g_app_core->GetTicks();
  148. // Synchronize line...
  149. if ((current_ticks - g_last_line_ticks) >= GPU::kFrameTicks / framebuffer_top.height) {
  150. GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PDC0);
  151. g_cur_line++;
  152. g_last_line_ticks = current_ticks;
  153. }
  154. // Synchronize frame...
  155. if (g_cur_line >= framebuffer_top.height) {
  156. g_cur_line = 0;
  157. GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PDC1);
  158. VideoCore::g_renderer->SwapBuffers();
  159. Kernel::WaitCurrentThread(WAITTYPE_VBLANK);
  160. HLE::Reschedule(__func__);
  161. }
  162. }
  163. /// Initialize hardware
  164. void Init() {
  165. g_cur_line = 0;
  166. g_last_line_ticks = Core::g_app_core->GetTicks();
  167. auto& framebuffer_top = g_regs.framebuffer_config[0];
  168. auto& framebuffer_sub = g_regs.framebuffer_config[1];
  169. // Setup default framebuffer addresses (located in VRAM)
  170. // .. or at least these are the ones used by system applets.
  171. // There's probably a smarter way to come up with addresses
  172. // like this which does not require hardcoding.
  173. framebuffer_top.address_left1 = 0x181E6000;
  174. framebuffer_top.address_left2 = 0x1822C800;
  175. framebuffer_top.address_right1 = 0x18273000;
  176. framebuffer_top.address_right2 = 0x182B9800;
  177. framebuffer_sub.address_left1 = 0x1848F000;
  178. //framebuffer_sub.address_left2 = unknown;
  179. framebuffer_sub.address_right1 = 0x184C7800;
  180. //framebuffer_sub.address_right2 = unknown;
  181. framebuffer_top.width = 240;
  182. framebuffer_top.height = 400;
  183. framebuffer_top.stride = 3 * 240;
  184. framebuffer_top.color_format = Regs::FramebufferFormat::RGB8;
  185. framebuffer_top.active_fb = 0;
  186. framebuffer_sub.width = 240;
  187. framebuffer_sub.height = 320;
  188. framebuffer_sub.stride = 3 * 240;
  189. framebuffer_sub.color_format = Regs::FramebufferFormat::RGB8;
  190. framebuffer_sub.active_fb = 0;
  191. NOTICE_LOG(GPU, "initialized OK");
  192. }
  193. /// Shutdown hardware
  194. void Shutdown() {
  195. NOTICE_LOG(GPU, "shutdown OK");
  196. }
  197. } // namespace