gpu.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include "common/common_types.h"
  6. #include "common/bit_field.h"
  7. #include "common/register_set.h"
  8. namespace GPU {
  9. static const u32 kFrameCycles = 268123480 / 60; ///< 268MHz / 60 frames per second
  10. static const u32 kFrameTicks = kFrameCycles / 3; ///< Approximate number of instructions/frame
  11. // MMIO region 0x1EFxxxxx
  12. struct Regs {
  13. enum Id : u32 {
  14. MemoryFill = 0x00004, // + 5,6,7; second block at 8-11
  15. FramebufferTop = 0x00117, // + 11a,11b,11c,11d(?),11e...126
  16. FramebufferBottom = 0x00157, // + 15a,15b,15c,15d(?),15e...166
  17. DisplayTransfer = 0x00300, // + 301,302,303,304,305,306
  18. CommandProcessor = 0x00638, // + 63a,63c
  19. NumIds = 0x01000
  20. };
  21. template<Id id>
  22. struct Struct;
  23. enum class FramebufferFormat : u32 {
  24. RGBA8 = 0,
  25. RGB8 = 1,
  26. RGB565 = 2,
  27. RGB5A1 = 3,
  28. RGBA4 = 4,
  29. };
  30. };
  31. template<>
  32. struct Regs::Struct<Regs::MemoryFill> {
  33. u32 address_start;
  34. u32 address_end; // ?
  35. u32 size;
  36. u32 value; // ?
  37. inline u32 GetStartAddress() const {
  38. return address_start * 8;
  39. }
  40. inline u32 GetEndAddress() const {
  41. return address_end * 8;
  42. }
  43. };
  44. static_assert(sizeof(Regs::Struct<Regs::MemoryFill>) == 0x10, "Structure size and register block length don't match");
  45. template<>
  46. struct Regs::Struct<Regs::FramebufferTop> {
  47. using Format = Regs::FramebufferFormat;
  48. union {
  49. u32 size;
  50. BitField< 0, 16, u32> width;
  51. BitField<16, 16, u32> height;
  52. };
  53. u32 pad0[2];
  54. u32 address_left1;
  55. u32 address_left2;
  56. union {
  57. u32 format;
  58. BitField< 0, 3, Format> color_format;
  59. };
  60. u32 pad1;
  61. union {
  62. u32 active_fb;
  63. // 0: Use parameters ending with "1"
  64. // 1: Use parameters ending with "2"
  65. BitField<0, 1, u32> second_fb_active;
  66. };
  67. u32 pad2[5];
  68. // Distance between two pixel rows, in bytes
  69. u32 stride;
  70. u32 address_right1;
  71. u32 address_right2;
  72. };
  73. template<>
  74. struct Regs::Struct<Regs::FramebufferBottom> : public Regs::Struct<Regs::FramebufferTop> {
  75. };
  76. static_assert(sizeof(Regs::Struct<Regs::FramebufferTop>) == 0x40, "Structure size and register block length don't match");
  77. template<>
  78. struct Regs::Struct<Regs::DisplayTransfer> {
  79. using Format = Regs::FramebufferFormat;
  80. u32 input_address;
  81. u32 output_address;
  82. inline u32 GetPhysicalInputAddress() const {
  83. return input_address * 8;
  84. }
  85. inline u32 GetPhysicalOutputAddress() const {
  86. return output_address * 8;
  87. }
  88. union {
  89. u32 output_size;
  90. BitField< 0, 16, u32> output_width;
  91. BitField<16, 16, u32> output_height;
  92. };
  93. union {
  94. u32 input_size;
  95. BitField< 0, 16, u32> input_width;
  96. BitField<16, 16, u32> input_height;
  97. };
  98. union {
  99. u32 flags;
  100. BitField< 0, 1, u32> flip_data; // flips input data horizontally (TODO) if true
  101. BitField< 8, 3, Format> input_format;
  102. BitField<12, 3, Format> output_format;
  103. BitField<16, 1, u32> output_tiled; // stores output in a tiled format
  104. };
  105. u32 unknown;
  106. // it seems that writing to this field triggers the display transfer
  107. u32 trigger;
  108. };
  109. static_assert(sizeof(Regs::Struct<Regs::DisplayTransfer>) == 0x1C, "Structure size and register block length don't match");
  110. template<>
  111. struct Regs::Struct<Regs::CommandProcessor> {
  112. // command list size
  113. u32 size;
  114. u32 pad0;
  115. // command list address
  116. u32 address;
  117. u32 pad1;
  118. // it seems that writing to this field triggers command list processing
  119. u32 trigger;
  120. };
  121. static_assert(sizeof(Regs::Struct<Regs::CommandProcessor>) == 0x14, "Structure size and register block length don't match");
  122. extern RegisterSet<u32, Regs> g_regs;
  123. enum {
  124. TOP_ASPECT_X = 0x5,
  125. TOP_ASPECT_Y = 0x3,
  126. TOP_HEIGHT = 240,
  127. TOP_WIDTH = 400,
  128. BOTTOM_WIDTH = 320,
  129. // Physical addresses in FCRAM (chosen arbitrarily)
  130. PADDR_TOP_LEFT_FRAME1 = 0x201D4C00,
  131. PADDR_TOP_LEFT_FRAME2 = 0x202D4C00,
  132. PADDR_TOP_RIGHT_FRAME1 = 0x203D4C00,
  133. PADDR_TOP_RIGHT_FRAME2 = 0x204D4C00,
  134. PADDR_SUB_FRAME1 = 0x205D4C00,
  135. PADDR_SUB_FRAME2 = 0x206D4C00,
  136. // Physical addresses in FCRAM used by ARM9 applications
  137. /* PADDR_TOP_LEFT_FRAME1 = 0x20184E60,
  138. PADDR_TOP_LEFT_FRAME2 = 0x201CB370,
  139. PADDR_TOP_RIGHT_FRAME1 = 0x20282160,
  140. PADDR_TOP_RIGHT_FRAME2 = 0x202C8670,
  141. PADDR_SUB_FRAME1 = 0x202118E0,
  142. PADDR_SUB_FRAME2 = 0x20249CF0,*/
  143. // Physical addresses in VRAM
  144. // TODO: These should just be deduced from the ones above
  145. PADDR_VRAM_TOP_LEFT_FRAME1 = 0x181D4C00,
  146. PADDR_VRAM_TOP_LEFT_FRAME2 = 0x182D4C00,
  147. PADDR_VRAM_TOP_RIGHT_FRAME1 = 0x183D4C00,
  148. PADDR_VRAM_TOP_RIGHT_FRAME2 = 0x184D4C00,
  149. PADDR_VRAM_SUB_FRAME1 = 0x185D4C00,
  150. PADDR_VRAM_SUB_FRAME2 = 0x186D4C00,
  151. // Physical addresses in VRAM used by ARM9 applications
  152. /* PADDR_VRAM_TOP_LEFT_FRAME2 = 0x181CB370,
  153. PADDR_VRAM_TOP_RIGHT_FRAME1 = 0x18282160,
  154. PADDR_VRAM_TOP_RIGHT_FRAME2 = 0x182C8670,
  155. PADDR_VRAM_SUB_FRAME1 = 0x182118E0,
  156. PADDR_VRAM_SUB_FRAME2 = 0x18249CF0,*/
  157. };
  158. /// Framebuffer location
  159. enum FramebufferLocation {
  160. FRAMEBUFFER_LOCATION_UNKNOWN, ///< Framebuffer location is unknown
  161. FRAMEBUFFER_LOCATION_FCRAM, ///< Framebuffer is in the GSP heap
  162. FRAMEBUFFER_LOCATION_VRAM, ///< Framebuffer is in VRAM
  163. };
  164. /**
  165. * Sets whether the framebuffers are in the GSP heap (FCRAM) or VRAM
  166. * @param
  167. */
  168. void SetFramebufferLocation(const FramebufferLocation mode);
  169. /**
  170. * Gets a read-only pointer to a framebuffer in memory
  171. * @param address Physical address of framebuffer
  172. * @return Returns const pointer to raw framebuffer
  173. */
  174. const u8* GetFramebufferPointer(const u32 address);
  175. u32 GetFramebufferAddr(const u32 address);
  176. /**
  177. * Gets the location of the framebuffers
  178. */
  179. FramebufferLocation GetFramebufferLocation(u32 address);
  180. template <typename T>
  181. void Read(T &var, const u32 addr);
  182. template <typename T>
  183. void Write(u32 addr, const T data);
  184. /// Update hardware
  185. void Update();
  186. /// Initialize hardware
  187. void Init();
  188. /// Shutdown hardware
  189. void Shutdown();
  190. } // namespace