gpu.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <cstddef>
  6. #include "common/common_types.h"
  7. #include "common/bit_field.h"
  8. namespace GPU {
  9. // Returns index corresponding to the Regs member labeled by field_name
  10. // TODO: Due to Visual studio bug 209229, offsetof does not return constant expressions
  11. // when used with array elements (e.g. GPU_REG_INDEX(memory_fill_config[0])).
  12. // For details cf. https://connect.microsoft.com/VisualStudio/feedback/details/209229/offsetof-does-not-produce-a-constant-expression-for-array-members
  13. // Hopefully, this will be fixed sometime in the future.
  14. // For lack of better alternatives, we currently hardcode the offsets when constant
  15. // expressions are needed via GPU_REG_INDEX_WORKAROUND (on sane compilers, static_asserts
  16. // will then make sure the offsets indeed match the automatically calculated ones).
  17. #define GPU_REG_INDEX(field_name) (offsetof(GPU::Regs, field_name) / sizeof(u32))
  18. #if defined(_MSC_VER)
  19. #define GPU_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) (backup_workaround_index)
  20. #else
  21. // NOTE: Yeah, hacking in a static_assert here just to workaround the lacking MSVC compiler
  22. // really is this annoying. This macro just forwards its first argument to GPU_REG_INDEX
  23. // and then performs a (no-op) cast to size_t iff the second argument matches the expected
  24. // field offset. Otherwise, the compiler will fail to compile this code.
  25. #define GPU_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) \
  26. ((typename std::enable_if<backup_workaround_index == GPU_REG_INDEX(field_name), size_t>::type)GPU_REG_INDEX(field_name))
  27. #endif
  28. // MMIO region 0x1EFxxxxx
  29. struct Regs {
  30. // helper macro to properly align structure members.
  31. // Calling INSERT_PADDING_WORDS will add a new member variable with a name like "pad121",
  32. // depending on the current source line to make sure variable names are unique.
  33. #define INSERT_PADDING_WORDS_HELPER1(x, y) x ## y
  34. #define INSERT_PADDING_WORDS_HELPER2(x, y) INSERT_PADDING_WORDS_HELPER1(x, y)
  35. #define INSERT_PADDING_WORDS(num_words) u32 INSERT_PADDING_WORDS_HELPER2(pad, __LINE__)[(num_words)]
  36. // helper macro to make sure the defined structures are of the expected size.
  37. #if defined(_MSC_VER)
  38. // TODO: MSVC does not support using sizeof() on non-static data members even though this
  39. // is technically allowed since C++11. This macro should be enabled once MSVC adds
  40. // support for that.
  41. #define ASSERT_MEMBER_SIZE(name, size_in_bytes)
  42. #else
  43. #define ASSERT_MEMBER_SIZE(name, size_in_bytes) \
  44. static_assert(sizeof(name) == size_in_bytes, \
  45. "Structure size and register block length don't match")
  46. #endif
  47. enum class PixelFormat : u32 {
  48. RGBA8 = 0,
  49. RGB8 = 1,
  50. RGB565 = 2,
  51. RGB5A1 = 3,
  52. RGBA4 = 4,
  53. };
  54. INSERT_PADDING_WORDS(0x4);
  55. struct {
  56. u32 address_start;
  57. u32 address_end; // ?
  58. u32 size;
  59. u32 value; // ?
  60. inline u32 GetStartAddress() const {
  61. return DecodeAddressRegister(address_start);
  62. }
  63. inline u32 GetEndAddress() const {
  64. return DecodeAddressRegister(address_end);
  65. }
  66. } memory_fill_config[2];
  67. ASSERT_MEMBER_SIZE(memory_fill_config[0], 0x10);
  68. INSERT_PADDING_WORDS(0x10b);
  69. struct FramebufferConfig {
  70. union {
  71. u32 size;
  72. BitField< 0, 16, u32> width;
  73. BitField<16, 16, u32> height;
  74. };
  75. INSERT_PADDING_WORDS(0x2);
  76. u32 address_left1;
  77. u32 address_left2;
  78. union {
  79. u32 format;
  80. BitField< 0, 3, PixelFormat> color_format;
  81. };
  82. INSERT_PADDING_WORDS(0x1);
  83. union {
  84. u32 active_fb;
  85. // 0: Use parameters ending with "1"
  86. // 1: Use parameters ending with "2"
  87. BitField<0, 1, u32> second_fb_active;
  88. };
  89. INSERT_PADDING_WORDS(0x5);
  90. // Distance between two pixel rows, in bytes
  91. u32 stride;
  92. u32 address_right1;
  93. u32 address_right2;
  94. INSERT_PADDING_WORDS(0x30);
  95. } framebuffer_config[2];
  96. ASSERT_MEMBER_SIZE(framebuffer_config[0], 0x100);
  97. INSERT_PADDING_WORDS(0x169);
  98. struct {
  99. u32 input_address;
  100. u32 output_address;
  101. inline u32 GetPhysicalInputAddress() const {
  102. return DecodeAddressRegister(input_address);
  103. }
  104. inline u32 GetPhysicalOutputAddress() const {
  105. return DecodeAddressRegister(output_address);
  106. }
  107. union {
  108. u32 output_size;
  109. BitField< 0, 16, u32> output_width;
  110. BitField<16, 16, u32> output_height;
  111. };
  112. union {
  113. u32 input_size;
  114. BitField< 0, 16, u32> input_width;
  115. BitField<16, 16, u32> input_height;
  116. };
  117. union {
  118. u32 flags;
  119. BitField< 0, 1, u32> flip_data; // flips input data horizontally (TODO) if true
  120. BitField< 8, 3, PixelFormat> input_format;
  121. BitField<12, 3, PixelFormat> output_format;
  122. BitField<16, 1, u32> output_tiled; // stores output in a tiled format
  123. // TODO: Not really sure if this actually scales, or even resizes at all.
  124. BitField<24, 1, u32> scale_horizontally;
  125. };
  126. INSERT_PADDING_WORDS(0x1);
  127. // it seems that writing to this field triggers the display transfer
  128. u32 trigger;
  129. } display_transfer_config;
  130. ASSERT_MEMBER_SIZE(display_transfer_config, 0x1c);
  131. INSERT_PADDING_WORDS(0x331);
  132. struct {
  133. // command list size (in bytes)
  134. u32 size;
  135. INSERT_PADDING_WORDS(0x1);
  136. // command list address
  137. u32 address;
  138. INSERT_PADDING_WORDS(0x1);
  139. // it seems that writing to this field triggers command list processing
  140. u32 trigger;
  141. inline u32 GetPhysicalAddress() const {
  142. return DecodeAddressRegister(address);
  143. }
  144. } command_processor_config;
  145. ASSERT_MEMBER_SIZE(command_processor_config, 0x14);
  146. INSERT_PADDING_WORDS(0x9c3);
  147. #undef INSERT_PADDING_WORDS_HELPER1
  148. #undef INSERT_PADDING_WORDS_HELPER2
  149. #undef INSERT_PADDING_WORDS
  150. static inline size_t NumIds() {
  151. return sizeof(Regs) / sizeof(u32);
  152. }
  153. u32& operator [] (int index) const {
  154. u32* content = (u32*)this;
  155. return content[index];
  156. }
  157. u32& operator [] (int index) {
  158. u32* content = (u32*)this;
  159. return content[index];
  160. }
  161. private:
  162. /*
  163. * Most physical addresses which GPU registers refer to are 8-byte aligned.
  164. * This function should be used to get the address from a raw register value.
  165. */
  166. static inline u32 DecodeAddressRegister(u32 register_value) {
  167. return register_value * 8;
  168. }
  169. };
  170. static_assert(std::is_standard_layout<Regs>::value, "Structure does not use standard layout");
  171. // TODO: MSVC does not support using offsetof() on non-static data members even though this
  172. // is technically allowed since C++11. This macro should be enabled once MSVC adds
  173. // support for that.
  174. #ifndef _MSC_VER
  175. #define ASSERT_REG_POSITION(field_name, position) \
  176. static_assert(offsetof(Regs, field_name) == position * 4, \
  177. "Field "#field_name" has invalid position")
  178. ASSERT_REG_POSITION(memory_fill_config[0], 0x00004);
  179. ASSERT_REG_POSITION(memory_fill_config[1], 0x00008);
  180. ASSERT_REG_POSITION(framebuffer_config[0], 0x00117);
  181. ASSERT_REG_POSITION(framebuffer_config[1], 0x00157);
  182. ASSERT_REG_POSITION(display_transfer_config, 0x00300);
  183. ASSERT_REG_POSITION(command_processor_config, 0x00638);
  184. #undef ASSERT_REG_POSITION
  185. #endif // !defined(_MSC_VER)
  186. // The total number of registers is chosen arbitrarily, but let's make sure it's not some odd value anyway.
  187. static_assert(sizeof(Regs) == 0x1000 * sizeof(u32), "Invalid total size of register set");
  188. extern Regs g_regs;
  189. extern bool g_skip_frame;
  190. template <typename T>
  191. void Read(T &var, const u32 addr);
  192. template <typename T>
  193. void Write(u32 addr, const T data);
  194. /// Update hardware
  195. void Update();
  196. /// Initialize hardware
  197. void Init();
  198. /// Shutdown hardware
  199. void Shutdown();
  200. } // namespace