gpu.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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/video_core.h"
  13. namespace GPU {
  14. RegisterSet<u32, Regs> g_regs;
  15. u32 g_cur_line = 0; ///< Current vertical screen line
  16. u64 g_last_line_ticks = 0; ///< CPU tick count from last vertical screen line
  17. /**
  18. * Sets whether the framebuffers are in the GSP heap (FCRAM) or VRAM
  19. * @param
  20. */
  21. void SetFramebufferLocation(const FramebufferLocation mode) {
  22. switch (mode) {
  23. case FRAMEBUFFER_LOCATION_FCRAM:
  24. {
  25. auto& framebuffer_top = g_regs.Get<Regs::FramebufferTop>();
  26. auto& framebuffer_sub = g_regs.Get<Regs::FramebufferBottom>();
  27. framebuffer_top.address_left1 = PADDR_TOP_LEFT_FRAME1;
  28. framebuffer_top.address_left2 = PADDR_TOP_LEFT_FRAME2;
  29. framebuffer_top.address_right1 = PADDR_TOP_RIGHT_FRAME1;
  30. framebuffer_top.address_right2 = PADDR_TOP_RIGHT_FRAME2;
  31. framebuffer_sub.address_left1 = PADDR_SUB_FRAME1;
  32. //framebuffer_sub.address_left2 = unknown;
  33. framebuffer_sub.address_right1 = PADDR_SUB_FRAME2;
  34. //framebuffer_sub.address_right2 = unknown;
  35. break;
  36. }
  37. case FRAMEBUFFER_LOCATION_VRAM:
  38. {
  39. auto& framebuffer_top = g_regs.Get<Regs::FramebufferTop>();
  40. auto& framebuffer_sub = g_regs.Get<Regs::FramebufferBottom>();
  41. framebuffer_top.address_left1 = PADDR_VRAM_TOP_LEFT_FRAME1;
  42. framebuffer_top.address_left2 = PADDR_VRAM_TOP_LEFT_FRAME2;
  43. framebuffer_top.address_right1 = PADDR_VRAM_TOP_RIGHT_FRAME1;
  44. framebuffer_top.address_right2 = PADDR_VRAM_TOP_RIGHT_FRAME2;
  45. framebuffer_sub.address_left1 = PADDR_VRAM_SUB_FRAME1;
  46. //framebuffer_sub.address_left2 = unknown;
  47. framebuffer_sub.address_right1 = PADDR_VRAM_SUB_FRAME2;
  48. //framebuffer_sub.address_right2 = unknown;
  49. break;
  50. }
  51. }
  52. }
  53. /**
  54. * Gets the location of the framebuffers
  55. * @return Location of framebuffers as FramebufferLocation enum
  56. */
  57. FramebufferLocation GetFramebufferLocation(u32 address) {
  58. if ((address & ~Memory::VRAM_MASK) == Memory::VRAM_PADDR) {
  59. return FRAMEBUFFER_LOCATION_VRAM;
  60. } else if ((address & ~Memory::FCRAM_MASK) == Memory::FCRAM_PADDR) {
  61. return FRAMEBUFFER_LOCATION_FCRAM;
  62. } else {
  63. ERROR_LOG(GPU, "unknown framebuffer location!");
  64. }
  65. return FRAMEBUFFER_LOCATION_UNKNOWN;
  66. }
  67. u32 GetFramebufferAddr(const u32 address) {
  68. switch (GetFramebufferLocation(address)) {
  69. case FRAMEBUFFER_LOCATION_FCRAM:
  70. return Memory::VirtualAddressFromPhysical_FCRAM(address);
  71. case FRAMEBUFFER_LOCATION_VRAM:
  72. return Memory::VirtualAddressFromPhysical_VRAM(address);
  73. default:
  74. ERROR_LOG(GPU, "unknown framebuffer location");
  75. }
  76. return 0;
  77. }
  78. /**
  79. * Gets a read-only pointer to a framebuffer in memory
  80. * @param address Physical address of framebuffer
  81. * @return Returns const pointer to raw framebuffer
  82. */
  83. const u8* GetFramebufferPointer(const u32 address) {
  84. u32 addr = GetFramebufferAddr(address);
  85. return (addr != 0) ? Memory::GetPointer(addr) : nullptr;
  86. }
  87. template <typename T>
  88. inline void Read(T &var, const u32 raw_addr) {
  89. u32 addr = raw_addr - 0x1EF00000;
  90. int index = addr / 4;
  91. // Reads other than u32 are untested, so I'd rather have them abort than silently fail
  92. if (index >= Regs::NumIds || !std::is_same<T,u32>::value)
  93. {
  94. ERROR_LOG(GPU, "unknown Read%d @ 0x%08X", sizeof(var) * 8, addr);
  95. return;
  96. }
  97. var = g_regs[static_cast<Regs::Id>(addr / 4)];
  98. }
  99. template <typename T>
  100. inline void Write(u32 addr, const T data) {
  101. addr -= 0x1EF00000;
  102. int index = addr / 4;
  103. // Writes other than u32 are untested, so I'd rather have them abort than silently fail
  104. if (index >= Regs::NumIds || !std::is_same<T,u32>::value)
  105. {
  106. ERROR_LOG(GPU, "unknown Write%d 0x%08X @ 0x%08X", sizeof(data) * 8, data, addr);
  107. return;
  108. }
  109. g_regs[static_cast<Regs::Id>(index)] = data;
  110. switch (static_cast<Regs::Id>(index)) {
  111. // Memory fills are triggered once the fill value is written.
  112. // NOTE: This is not verified.
  113. case Regs::MemoryFill + 3:
  114. case Regs::MemoryFill + 7:
  115. {
  116. const auto& config = g_regs.Get<Regs::MemoryFill>(static_cast<Regs::Id>(index - 3));
  117. // TODO: Not sure if this check should be done at GSP level instead
  118. if (config.address_start) {
  119. // TODO: Not sure if this algorithm is correct, particularly because it doesn't use the size member at all
  120. u32* start = (u32*)Memory::GetPointer(config.GetStartAddress());
  121. u32* end = (u32*)Memory::GetPointer(config.GetEndAddress());
  122. for (u32* ptr = start; ptr < end; ++ptr)
  123. *ptr = bswap32(config.value); // TODO: This is just a workaround to missing framebuffer format emulation
  124. DEBUG_LOG(GPU, "MemoryFill from 0x%08x to 0x%08x", config.GetStartAddress(), config.GetEndAddress());
  125. }
  126. break;
  127. }
  128. case Regs::DisplayTransfer + 6:
  129. {
  130. const auto& config = g_regs.Get<Regs::DisplayTransfer>();
  131. if (config.trigger & 1) {
  132. u8* source_pointer = Memory::GetPointer(config.GetPhysicalInputAddress());
  133. u8* dest_pointer = Memory::GetPointer(config.GetPhysicalOutputAddress());
  134. for (int y = 0; y < config.output_height; ++y) {
  135. // TODO: Why does the register seem to hold twice the framebuffer width?
  136. for (int x = 0; x < config.output_width / 2; ++x) {
  137. struct {
  138. int r, g, b, a;
  139. } source_color = { 0, 0, 0, 0 };
  140. switch (config.input_format) {
  141. case Regs::FramebufferFormat::RGBA8:
  142. {
  143. // TODO: Most likely got the component order messed up.
  144. u8* srcptr = source_pointer + x * 4 + y * config.input_width * 4 / 2;
  145. source_color.r = srcptr[0]; // blue
  146. source_color.g = srcptr[1]; // green
  147. source_color.b = srcptr[2]; // red
  148. source_color.a = srcptr[3]; // alpha
  149. break;
  150. }
  151. default:
  152. ERROR_LOG(GPU, "Unknown source framebuffer format %x", config.input_format.Value());
  153. break;
  154. }
  155. switch (config.output_format) {
  156. /*case Regs::FramebufferFormat::RGBA8:
  157. {
  158. // TODO: Untested
  159. u8* dstptr = (u32*)(dest_pointer + x * 4 + y * config.output_width * 4);
  160. dstptr[0] = source_color.r;
  161. dstptr[1] = source_color.g;
  162. dstptr[2] = source_color.b;
  163. dstptr[3] = source_color.a;
  164. break;
  165. }*/
  166. case Regs::FramebufferFormat::RGB8:
  167. {
  168. // TODO: Most likely got the component order messed up.
  169. u8* dstptr = dest_pointer + x * 3 + y * config.output_width * 3 / 2;
  170. dstptr[0] = source_color.r; // blue
  171. dstptr[1] = source_color.g; // green
  172. dstptr[2] = source_color.b; // red
  173. break;
  174. }
  175. default:
  176. ERROR_LOG(GPU, "Unknown destination framebuffer format %x", config.output_format.Value());
  177. break;
  178. }
  179. }
  180. }
  181. DEBUG_LOG(GPU, "DisplayTriggerTransfer: 0x%08x bytes from 0x%08x(%dx%d)-> 0x%08x(%dx%d), dst format %x",
  182. config.output_height * config.output_width * 4,
  183. config.GetPhysicalInputAddress(), (int)config.input_width, (int)config.input_height,
  184. config.GetPhysicalOutputAddress(), (int)config.output_width, (int)config.output_height,
  185. config.output_format.Value());
  186. }
  187. break;
  188. }
  189. case Regs::CommandProcessor + 4:
  190. {
  191. const auto& config = g_regs.Get<Regs::CommandProcessor>();
  192. if (config.trigger & 1)
  193. {
  194. // u32* buffer = (u32*)Memory::GetPointer(config.address << 3);
  195. ERROR_LOG(GPU, "Beginning 0x%08x bytes of commands from address 0x%08x", config.size, config.address << 3);
  196. // TODO: Process command list!
  197. }
  198. break;
  199. }
  200. default:
  201. break;
  202. }
  203. }
  204. // Explicitly instantiate template functions because we aren't defining this in the header:
  205. template void Read<u64>(u64 &var, const u32 addr);
  206. template void Read<u32>(u32 &var, const u32 addr);
  207. template void Read<u16>(u16 &var, const u32 addr);
  208. template void Read<u8>(u8 &var, const u32 addr);
  209. template void Write<u64>(u32 addr, const u64 data);
  210. template void Write<u32>(u32 addr, const u32 data);
  211. template void Write<u16>(u32 addr, const u16 data);
  212. template void Write<u8>(u32 addr, const u8 data);
  213. /// Update hardware
  214. void Update() {
  215. auto& framebuffer_top = g_regs.Get<Regs::FramebufferTop>();
  216. u64 current_ticks = Core::g_app_core->GetTicks();
  217. // Synchronize line...
  218. if ((current_ticks - g_last_line_ticks) >= GPU::kFrameTicks / framebuffer_top.height) {
  219. GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PDC0);
  220. g_cur_line++;
  221. g_last_line_ticks = current_ticks;
  222. }
  223. // Synchronize frame...
  224. if (g_cur_line >= framebuffer_top.height) {
  225. g_cur_line = 0;
  226. GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PDC1);
  227. VideoCore::g_renderer->SwapBuffers();
  228. Kernel::WaitCurrentThread(WAITTYPE_VBLANK);
  229. HLE::Reschedule(__func__);
  230. }
  231. }
  232. /// Initialize hardware
  233. void Init() {
  234. g_cur_line = 0;
  235. g_last_line_ticks = Core::g_app_core->GetTicks();
  236. // SetFramebufferLocation(FRAMEBUFFER_LOCATION_FCRAM);
  237. SetFramebufferLocation(FRAMEBUFFER_LOCATION_VRAM);
  238. auto& framebuffer_top = g_regs.Get<Regs::FramebufferTop>();
  239. auto& framebuffer_sub = g_regs.Get<Regs::FramebufferBottom>();
  240. // TODO: Width should be 240 instead?
  241. framebuffer_top.width = 480;
  242. framebuffer_top.height = 400;
  243. framebuffer_top.stride = 480*3;
  244. framebuffer_top.color_format = Regs::FramebufferFormat::RGB8;
  245. framebuffer_top.active_fb = 0;
  246. framebuffer_sub.width = 480;
  247. framebuffer_sub.height = 400;
  248. framebuffer_sub.stride = 480*3;
  249. framebuffer_sub.color_format = Regs::FramebufferFormat::RGB8;
  250. framebuffer_sub.active_fb = 0;
  251. NOTICE_LOG(GPU, "initialized OK");
  252. }
  253. /// Shutdown hardware
  254. void Shutdown() {
  255. NOTICE_LOG(GPU, "shutdown OK");
  256. }
  257. } // namespace