pica.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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 <array>
  6. #include <cstddef>
  7. #include <initializer_list>
  8. #include <map>
  9. #include <vector>
  10. #include "common/bit_field.h"
  11. #include "common/common_types.h"
  12. #include "core/mem_map.h"
  13. namespace Pica {
  14. // Returns index corresponding to the Regs member labeled by field_name
  15. // TODO: Due to Visual studio bug 209229, offsetof does not return constant expressions
  16. // when used with array elements (e.g. PICA_REG_INDEX(vs_uniform_setup.set_value[1])).
  17. // For details cf. https://connect.microsoft.com/VisualStudio/feedback/details/209229/offsetof-does-not-produce-a-constant-expression-for-array-members
  18. // Hopefully, this will be fixed sometime in the future.
  19. // For lack of better alternatives, we currently hardcode the offsets when constant
  20. // expressions are needed via PICA_REG_INDEX_WORKAROUND (on sane compilers, static_asserts
  21. // will then make sure the offsets indeed match the automatically calculated ones).
  22. #define PICA_REG_INDEX(field_name) (offsetof(Pica::Regs, field_name) / sizeof(u32))
  23. #if defined(_MSC_VER)
  24. #define PICA_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) (backup_workaround_index)
  25. #else
  26. // NOTE: Yeah, hacking in a static_assert here just to workaround the lacking MSVC compiler
  27. // really is this annoying. This macro just forwards its first argument to PICA_REG_INDEX
  28. // and then performs a (no-op) cast to size_t iff the second argument matches the expected
  29. // field offset. Otherwise, the compiler will fail to compile this code.
  30. #define PICA_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) \
  31. ((typename std::enable_if<backup_workaround_index == PICA_REG_INDEX(field_name), size_t>::type)PICA_REG_INDEX(field_name))
  32. #endif // _MSC_VER
  33. struct Regs {
  34. // helper macro to properly align structure members.
  35. // Calling INSERT_PADDING_WORDS will add a new member variable with a name like "pad121",
  36. // depending on the current source line to make sure variable names are unique.
  37. #define INSERT_PADDING_WORDS_HELPER1(x, y) x ## y
  38. #define INSERT_PADDING_WORDS_HELPER2(x, y) INSERT_PADDING_WORDS_HELPER1(x, y)
  39. #define INSERT_PADDING_WORDS(num_words) u32 INSERT_PADDING_WORDS_HELPER2(pad, __LINE__)[(num_words)];
  40. INSERT_PADDING_WORDS(0x10);
  41. u32 trigger_irq;
  42. INSERT_PADDING_WORDS(0x30);
  43. BitField<0, 24, u32> viewport_size_x;
  44. INSERT_PADDING_WORDS(0x1);
  45. BitField<0, 24, u32> viewport_size_y;
  46. INSERT_PADDING_WORDS(0x9);
  47. BitField<0, 24, u32> viewport_depth_range; // float24
  48. BitField<0, 24, u32> viewport_depth_far_plane; // float24
  49. INSERT_PADDING_WORDS(0x1);
  50. union VSOutputAttributes {
  51. // Maps components of output vertex attributes to semantics
  52. enum Semantic : u32
  53. {
  54. POSITION_X = 0,
  55. POSITION_Y = 1,
  56. POSITION_Z = 2,
  57. POSITION_W = 3,
  58. COLOR_R = 8,
  59. COLOR_G = 9,
  60. COLOR_B = 10,
  61. COLOR_A = 11,
  62. TEXCOORD0_U = 12,
  63. TEXCOORD0_V = 13,
  64. TEXCOORD1_U = 14,
  65. TEXCOORD1_V = 15,
  66. TEXCOORD2_U = 22,
  67. TEXCOORD2_V = 23,
  68. INVALID = 31,
  69. };
  70. BitField< 0, 5, Semantic> map_x;
  71. BitField< 8, 5, Semantic> map_y;
  72. BitField<16, 5, Semantic> map_z;
  73. BitField<24, 5, Semantic> map_w;
  74. } vs_output_attributes[7];
  75. INSERT_PADDING_WORDS(0x11);
  76. union {
  77. BitField< 0, 16, u32> x;
  78. BitField<16, 16, u32> y;
  79. } viewport_corner;
  80. INSERT_PADDING_WORDS(0x17);
  81. struct TextureConfig {
  82. enum WrapMode : u32 {
  83. ClampToEdge = 0,
  84. Repeat = 2,
  85. };
  86. INSERT_PADDING_WORDS(0x1);
  87. union {
  88. BitField< 0, 16, u32> height;
  89. BitField<16, 16, u32> width;
  90. };
  91. union {
  92. BitField< 8, 2, WrapMode> wrap_s;
  93. BitField<11, 2, WrapMode> wrap_t;
  94. };
  95. INSERT_PADDING_WORDS(0x1);
  96. u32 address;
  97. u32 GetPhysicalAddress() const {
  98. return DecodeAddressRegister(address);
  99. }
  100. // texture1 and texture2 store the texture format directly after the address
  101. // whereas texture0 inserts some additional flags inbetween.
  102. // Hence, we store the format separately so that all other parameters can be described
  103. // in a single structure.
  104. };
  105. enum class TextureFormat : u32 {
  106. RGBA8 = 0,
  107. RGB8 = 1,
  108. RGBA5551 = 2,
  109. RGB565 = 3,
  110. RGBA4 = 4,
  111. IA8 = 5,
  112. I8 = 7,
  113. A8 = 8,
  114. IA4 = 9,
  115. A4 = 11,
  116. // TODO: Support for the other formats is not implemented, yet.
  117. // Seems like they are luminance formats and compressed textures.
  118. };
  119. static unsigned NibblesPerPixel(TextureFormat format) {
  120. switch (format) {
  121. case TextureFormat::RGBA8:
  122. return 8;
  123. case TextureFormat::RGB8:
  124. return 6;
  125. case TextureFormat::RGBA5551:
  126. case TextureFormat::RGB565:
  127. case TextureFormat::RGBA4:
  128. case TextureFormat::IA8:
  129. return 4;
  130. case TextureFormat::A4:
  131. return 1;
  132. case TextureFormat::I8:
  133. case TextureFormat::A8:
  134. case TextureFormat::IA4:
  135. default: // placeholder for yet unknown formats
  136. return 2;
  137. }
  138. }
  139. union {
  140. BitField< 0, 1, u32> texture0_enable;
  141. BitField< 1, 1, u32> texture1_enable;
  142. BitField< 2, 1, u32> texture2_enable;
  143. };
  144. TextureConfig texture0;
  145. INSERT_PADDING_WORDS(0x8);
  146. BitField<0, 4, TextureFormat> texture0_format;
  147. INSERT_PADDING_WORDS(0x2);
  148. TextureConfig texture1;
  149. BitField<0, 4, TextureFormat> texture1_format;
  150. INSERT_PADDING_WORDS(0x2);
  151. TextureConfig texture2;
  152. BitField<0, 4, TextureFormat> texture2_format;
  153. INSERT_PADDING_WORDS(0x21);
  154. struct FullTextureConfig {
  155. const bool enabled;
  156. const TextureConfig config;
  157. const TextureFormat format;
  158. };
  159. const std::array<FullTextureConfig, 3> GetTextures() const {
  160. return {{
  161. { texture0_enable.ToBool(), texture0, texture0_format },
  162. { texture1_enable.ToBool(), texture1, texture1_format },
  163. { texture2_enable.ToBool(), texture2, texture2_format }
  164. }};
  165. }
  166. // 0xc0-0xff: Texture Combiner (akin to glTexEnv)
  167. struct TevStageConfig {
  168. enum class Source : u32 {
  169. PrimaryColor = 0x0,
  170. Texture0 = 0x3,
  171. Texture1 = 0x4,
  172. Texture2 = 0x5,
  173. Texture3 = 0x6,
  174. // 0x7-0xc = primary color??
  175. Constant = 0xe,
  176. Previous = 0xf,
  177. };
  178. enum class ColorModifier : u32 {
  179. SourceColor = 0,
  180. OneMinusSourceColor = 1,
  181. SourceAlpha = 2,
  182. OneMinusSourceAlpha = 3,
  183. // Other values seem to be non-standard extensions
  184. };
  185. enum class AlphaModifier : u32 {
  186. SourceAlpha = 0,
  187. OneMinusSourceAlpha = 1,
  188. // Other values seem to be non-standard extensions
  189. };
  190. enum class Operation : u32 {
  191. Replace = 0,
  192. Modulate = 1,
  193. Add = 2,
  194. AddSigned = 3,
  195. Lerp = 4,
  196. Subtract = 5,
  197. };
  198. union {
  199. BitField< 0, 4, Source> color_source1;
  200. BitField< 4, 4, Source> color_source2;
  201. BitField< 8, 4, Source> color_source3;
  202. BitField<16, 4, Source> alpha_source1;
  203. BitField<20, 4, Source> alpha_source2;
  204. BitField<24, 4, Source> alpha_source3;
  205. };
  206. union {
  207. BitField< 0, 4, ColorModifier> color_modifier1;
  208. BitField< 4, 4, ColorModifier> color_modifier2;
  209. BitField< 8, 4, ColorModifier> color_modifier3;
  210. BitField<12, 3, AlphaModifier> alpha_modifier1;
  211. BitField<16, 3, AlphaModifier> alpha_modifier2;
  212. BitField<20, 3, AlphaModifier> alpha_modifier3;
  213. };
  214. union {
  215. BitField< 0, 4, Operation> color_op;
  216. BitField<16, 4, Operation> alpha_op;
  217. };
  218. union {
  219. BitField< 0, 8, u32> const_r;
  220. BitField< 8, 8, u32> const_g;
  221. BitField<16, 8, u32> const_b;
  222. BitField<24, 8, u32> const_a;
  223. };
  224. INSERT_PADDING_WORDS(0x1);
  225. };
  226. TevStageConfig tev_stage0;
  227. INSERT_PADDING_WORDS(0x3);
  228. TevStageConfig tev_stage1;
  229. INSERT_PADDING_WORDS(0x3);
  230. TevStageConfig tev_stage2;
  231. INSERT_PADDING_WORDS(0x3);
  232. TevStageConfig tev_stage3;
  233. INSERT_PADDING_WORDS(0x13);
  234. TevStageConfig tev_stage4;
  235. INSERT_PADDING_WORDS(0x3);
  236. TevStageConfig tev_stage5;
  237. INSERT_PADDING_WORDS(0x13);
  238. const std::array<Regs::TevStageConfig,6> GetTevStages() const {
  239. return { tev_stage0, tev_stage1,
  240. tev_stage2, tev_stage3,
  241. tev_stage4, tev_stage5 };
  242. };
  243. struct {
  244. enum ColorFormat : u32 {
  245. RGBA8 = 0,
  246. RGB8 = 1,
  247. RGBA5551 = 2,
  248. RGB565 = 3,
  249. RGBA4 = 4,
  250. };
  251. INSERT_PADDING_WORDS(0x6);
  252. u32 depth_format;
  253. u32 color_format;
  254. INSERT_PADDING_WORDS(0x4);
  255. u32 depth_buffer_address;
  256. u32 color_buffer_address;
  257. union {
  258. // Apparently, the framebuffer width is stored as expected,
  259. // while the height is stored as the actual height minus one.
  260. // Hence, don't access these fields directly but use the accessors
  261. // GetWidth() and GetHeight() instead.
  262. BitField< 0, 11, u32> width;
  263. BitField<12, 10, u32> height;
  264. };
  265. INSERT_PADDING_WORDS(0x1);
  266. inline u32 GetColorBufferPhysicalAddress() const {
  267. return DecodeAddressRegister(color_buffer_address);
  268. }
  269. inline u32 GetDepthBufferPhysicalAddress() const {
  270. return DecodeAddressRegister(depth_buffer_address);
  271. }
  272. inline u32 GetWidth() const {
  273. return width;
  274. }
  275. inline u32 GetHeight() const {
  276. return height + 1;
  277. }
  278. } framebuffer;
  279. INSERT_PADDING_WORDS(0xe0);
  280. struct {
  281. enum class Format : u64 {
  282. BYTE = 0,
  283. UBYTE = 1,
  284. SHORT = 2,
  285. FLOAT = 3,
  286. };
  287. BitField<0, 29, u32> base_address;
  288. u32 GetPhysicalBaseAddress() const {
  289. return DecodeAddressRegister(base_address);
  290. }
  291. // Descriptor for internal vertex attributes
  292. union {
  293. BitField< 0, 2, Format> format0; // size of one element
  294. BitField< 2, 2, u64> size0; // number of elements minus 1
  295. BitField< 4, 2, Format> format1;
  296. BitField< 6, 2, u64> size1;
  297. BitField< 8, 2, Format> format2;
  298. BitField<10, 2, u64> size2;
  299. BitField<12, 2, Format> format3;
  300. BitField<14, 2, u64> size3;
  301. BitField<16, 2, Format> format4;
  302. BitField<18, 2, u64> size4;
  303. BitField<20, 2, Format> format5;
  304. BitField<22, 2, u64> size5;
  305. BitField<24, 2, Format> format6;
  306. BitField<26, 2, u64> size6;
  307. BitField<28, 2, Format> format7;
  308. BitField<30, 2, u64> size7;
  309. BitField<32, 2, Format> format8;
  310. BitField<34, 2, u64> size8;
  311. BitField<36, 2, Format> format9;
  312. BitField<38, 2, u64> size9;
  313. BitField<40, 2, Format> format10;
  314. BitField<42, 2, u64> size10;
  315. BitField<44, 2, Format> format11;
  316. BitField<46, 2, u64> size11;
  317. BitField<48, 12, u64> attribute_mask;
  318. // number of total attributes minus 1
  319. BitField<60, 4, u64> num_extra_attributes;
  320. };
  321. inline Format GetFormat(int n) const {
  322. Format formats[] = {
  323. format0, format1, format2, format3,
  324. format4, format5, format6, format7,
  325. format8, format9, format10, format11
  326. };
  327. return formats[n];
  328. }
  329. inline int GetNumElements(int n) const {
  330. u64 sizes[] = {
  331. size0, size1, size2, size3,
  332. size4, size5, size6, size7,
  333. size8, size9, size10, size11
  334. };
  335. return (int)sizes[n]+1;
  336. }
  337. inline int GetElementSizeInBytes(int n) const {
  338. return (GetFormat(n) == Format::FLOAT) ? 4 :
  339. (GetFormat(n) == Format::SHORT) ? 2 : 1;
  340. }
  341. inline int GetStride(int n) const {
  342. return GetNumElements(n) * GetElementSizeInBytes(n);
  343. }
  344. inline int GetNumTotalAttributes() const {
  345. return (int)num_extra_attributes+1;
  346. }
  347. // Attribute loaders map the source vertex data to input attributes
  348. // This e.g. allows to load different attributes from different memory locations
  349. struct {
  350. // Source attribute data offset from the base address
  351. u32 data_offset;
  352. union {
  353. BitField< 0, 4, u64> comp0;
  354. BitField< 4, 4, u64> comp1;
  355. BitField< 8, 4, u64> comp2;
  356. BitField<12, 4, u64> comp3;
  357. BitField<16, 4, u64> comp4;
  358. BitField<20, 4, u64> comp5;
  359. BitField<24, 4, u64> comp6;
  360. BitField<28, 4, u64> comp7;
  361. BitField<32, 4, u64> comp8;
  362. BitField<36, 4, u64> comp9;
  363. BitField<40, 4, u64> comp10;
  364. BitField<44, 4, u64> comp11;
  365. // bytes for a single vertex in this loader
  366. BitField<48, 8, u64> byte_count;
  367. BitField<60, 4, u64> component_count;
  368. };
  369. inline int GetComponent(int n) const {
  370. u64 components[] = {
  371. comp0, comp1, comp2, comp3,
  372. comp4, comp5, comp6, comp7,
  373. comp8, comp9, comp10, comp11
  374. };
  375. return (int)components[n];
  376. }
  377. } attribute_loaders[12];
  378. } vertex_attributes;
  379. struct {
  380. enum IndexFormat : u32 {
  381. BYTE = 0,
  382. SHORT = 1,
  383. };
  384. union {
  385. BitField<0, 31, u32> offset; // relative to base attribute address
  386. BitField<31, 1, IndexFormat> format;
  387. };
  388. } index_array;
  389. // Number of vertices to render
  390. u32 num_vertices;
  391. INSERT_PADDING_WORDS(0x5);
  392. // These two trigger rendering of triangles
  393. u32 trigger_draw;
  394. u32 trigger_draw_indexed;
  395. INSERT_PADDING_WORDS(0x2e);
  396. enum class TriangleTopology : u32 {
  397. List = 0,
  398. Strip = 1,
  399. Fan = 2,
  400. ListIndexed = 3, // TODO: No idea if this is correct
  401. };
  402. BitField<8, 2, TriangleTopology> triangle_topology;
  403. INSERT_PADDING_WORDS(0x51);
  404. BitField<0, 16, u32> vs_bool_uniforms;
  405. INSERT_PADDING_WORDS(0x9);
  406. // Offset to shader program entry point (in words)
  407. BitField<0, 16, u32> vs_main_offset;
  408. union {
  409. BitField< 0, 4, u64> attribute0_register;
  410. BitField< 4, 4, u64> attribute1_register;
  411. BitField< 8, 4, u64> attribute2_register;
  412. BitField<12, 4, u64> attribute3_register;
  413. BitField<16, 4, u64> attribute4_register;
  414. BitField<20, 4, u64> attribute5_register;
  415. BitField<24, 4, u64> attribute6_register;
  416. BitField<28, 4, u64> attribute7_register;
  417. BitField<32, 4, u64> attribute8_register;
  418. BitField<36, 4, u64> attribute9_register;
  419. BitField<40, 4, u64> attribute10_register;
  420. BitField<44, 4, u64> attribute11_register;
  421. BitField<48, 4, u64> attribute12_register;
  422. BitField<52, 4, u64> attribute13_register;
  423. BitField<56, 4, u64> attribute14_register;
  424. BitField<60, 4, u64> attribute15_register;
  425. int GetRegisterForAttribute(int attribute_index) {
  426. u64 fields[] = {
  427. attribute0_register, attribute1_register, attribute2_register, attribute3_register,
  428. attribute4_register, attribute5_register, attribute6_register, attribute7_register,
  429. attribute8_register, attribute9_register, attribute10_register, attribute11_register,
  430. attribute12_register, attribute13_register, attribute14_register, attribute15_register,
  431. };
  432. return (int)fields[attribute_index];
  433. }
  434. } vs_input_register_map;
  435. INSERT_PADDING_WORDS(0x3);
  436. struct {
  437. enum Format : u32
  438. {
  439. FLOAT24 = 0,
  440. FLOAT32 = 1
  441. };
  442. bool IsFloat32() const {
  443. return format == FLOAT32;
  444. }
  445. union {
  446. // Index of the next uniform to write to
  447. // TODO: ctrulib uses 8 bits for this, however that seems to yield lots of invalid indices
  448. BitField<0, 7, u32> index;
  449. BitField<31, 1, Format> format;
  450. };
  451. // Writing to these registers sets the "current" uniform.
  452. // TODO: It's not clear how the hardware stores what the "current" uniform is.
  453. u32 set_value[8];
  454. } vs_uniform_setup;
  455. INSERT_PADDING_WORDS(0x2);
  456. struct {
  457. u32 begin_load;
  458. // Writing to these registers sets the "current" word in the shader program.
  459. // TODO: It's not clear how the hardware stores what the "current" word is.
  460. u32 set_word[8];
  461. } vs_program;
  462. INSERT_PADDING_WORDS(0x1);
  463. // This register group is used to load an internal table of swizzling patterns,
  464. // which are indexed by each shader instruction to specify vector component swizzling.
  465. struct {
  466. u32 begin_load;
  467. // Writing to these registers sets the "current" swizzle pattern in the table.
  468. // TODO: It's not clear how the hardware stores what the "current" swizzle pattern is.
  469. u32 set_word[8];
  470. } vs_swizzle_patterns;
  471. INSERT_PADDING_WORDS(0x22);
  472. #undef INSERT_PADDING_WORDS_HELPER1
  473. #undef INSERT_PADDING_WORDS_HELPER2
  474. #undef INSERT_PADDING_WORDS
  475. // Map register indices to names readable by humans
  476. // Used for debugging purposes, so performance is not an issue here
  477. static std::string GetCommandName(int index) {
  478. std::map<u32, std::string> map;
  479. #define ADD_FIELD(name) \
  480. do { \
  481. map.insert({PICA_REG_INDEX(name), #name}); \
  482. /* TODO: change to Regs::name when VS2015 and other compilers support it */ \
  483. for (u32 i = PICA_REG_INDEX(name) + 1; i < PICA_REG_INDEX(name) + sizeof(Regs().name) / 4; ++i) \
  484. map.insert({i, #name + std::string("+") + std::to_string(i-PICA_REG_INDEX(name))}); \
  485. } while(false)
  486. ADD_FIELD(trigger_irq);
  487. ADD_FIELD(viewport_size_x);
  488. ADD_FIELD(viewport_size_y);
  489. ADD_FIELD(viewport_depth_range);
  490. ADD_FIELD(viewport_depth_far_plane);
  491. ADD_FIELD(viewport_corner);
  492. ADD_FIELD(texture0_enable);
  493. ADD_FIELD(texture0);
  494. ADD_FIELD(texture0_format);
  495. ADD_FIELD(texture1);
  496. ADD_FIELD(texture1_format);
  497. ADD_FIELD(texture2);
  498. ADD_FIELD(texture2_format);
  499. ADD_FIELD(tev_stage0);
  500. ADD_FIELD(tev_stage1);
  501. ADD_FIELD(tev_stage2);
  502. ADD_FIELD(tev_stage3);
  503. ADD_FIELD(tev_stage4);
  504. ADD_FIELD(tev_stage5);
  505. ADD_FIELD(framebuffer);
  506. ADD_FIELD(vertex_attributes);
  507. ADD_FIELD(index_array);
  508. ADD_FIELD(num_vertices);
  509. ADD_FIELD(trigger_draw);
  510. ADD_FIELD(trigger_draw_indexed);
  511. ADD_FIELD(triangle_topology);
  512. ADD_FIELD(vs_bool_uniforms);
  513. ADD_FIELD(vs_main_offset);
  514. ADD_FIELD(vs_input_register_map);
  515. ADD_FIELD(vs_uniform_setup);
  516. ADD_FIELD(vs_program);
  517. ADD_FIELD(vs_swizzle_patterns);
  518. #undef ADD_FIELD
  519. // Return empty string if no match is found
  520. return map[index];
  521. }
  522. static inline size_t NumIds() {
  523. return sizeof(Regs) / sizeof(u32);
  524. }
  525. u32& operator [] (int index) const {
  526. u32* content = (u32*)this;
  527. return content[index];
  528. }
  529. u32& operator [] (int index) {
  530. u32* content = (u32*)this;
  531. return content[index];
  532. }
  533. private:
  534. /*
  535. * Most physical addresses which Pica registers refer to are 8-byte aligned.
  536. * This function should be used to get the address from a raw register value.
  537. */
  538. static inline u32 DecodeAddressRegister(u32 register_value) {
  539. return register_value * 8;
  540. }
  541. };
  542. // TODO: MSVC does not support using offsetof() on non-static data members even though this
  543. // is technically allowed since C++11. This macro should be enabled once MSVC adds
  544. // support for that.
  545. #ifndef _MSC_VER
  546. #define ASSERT_REG_POSITION(field_name, position) static_assert(offsetof(Regs, field_name) == position * 4, "Field "#field_name" has invalid position")
  547. ASSERT_REG_POSITION(trigger_irq, 0x10);
  548. ASSERT_REG_POSITION(viewport_size_x, 0x41);
  549. ASSERT_REG_POSITION(viewport_size_y, 0x43);
  550. ASSERT_REG_POSITION(viewport_depth_range, 0x4d);
  551. ASSERT_REG_POSITION(viewport_depth_far_plane, 0x4e);
  552. ASSERT_REG_POSITION(vs_output_attributes[0], 0x50);
  553. ASSERT_REG_POSITION(vs_output_attributes[1], 0x51);
  554. ASSERT_REG_POSITION(viewport_corner, 0x68);
  555. ASSERT_REG_POSITION(texture0_enable, 0x80);
  556. ASSERT_REG_POSITION(texture0, 0x81);
  557. ASSERT_REG_POSITION(texture0_format, 0x8e);
  558. ASSERT_REG_POSITION(texture1, 0x91);
  559. ASSERT_REG_POSITION(texture1_format, 0x96);
  560. ASSERT_REG_POSITION(texture2, 0x99);
  561. ASSERT_REG_POSITION(texture2_format, 0x9e);
  562. ASSERT_REG_POSITION(tev_stage0, 0xc0);
  563. ASSERT_REG_POSITION(tev_stage1, 0xc8);
  564. ASSERT_REG_POSITION(tev_stage2, 0xd0);
  565. ASSERT_REG_POSITION(tev_stage3, 0xd8);
  566. ASSERT_REG_POSITION(tev_stage4, 0xf0);
  567. ASSERT_REG_POSITION(tev_stage5, 0xf8);
  568. ASSERT_REG_POSITION(framebuffer, 0x110);
  569. ASSERT_REG_POSITION(vertex_attributes, 0x200);
  570. ASSERT_REG_POSITION(index_array, 0x227);
  571. ASSERT_REG_POSITION(num_vertices, 0x228);
  572. ASSERT_REG_POSITION(trigger_draw, 0x22e);
  573. ASSERT_REG_POSITION(trigger_draw_indexed, 0x22f);
  574. ASSERT_REG_POSITION(triangle_topology, 0x25e);
  575. ASSERT_REG_POSITION(vs_bool_uniforms, 0x2b0);
  576. ASSERT_REG_POSITION(vs_main_offset, 0x2ba);
  577. ASSERT_REG_POSITION(vs_input_register_map, 0x2bb);
  578. ASSERT_REG_POSITION(vs_uniform_setup, 0x2c0);
  579. ASSERT_REG_POSITION(vs_program, 0x2cb);
  580. ASSERT_REG_POSITION(vs_swizzle_patterns, 0x2d5);
  581. #undef ASSERT_REG_POSITION
  582. #endif // !defined(_MSC_VER)
  583. // The total number of registers is chosen arbitrarily, but let's make sure it's not some odd value anyway.
  584. static_assert(sizeof(Regs) <= 0x300 * sizeof(u32), "Register set structure larger than it should be");
  585. static_assert(sizeof(Regs) >= 0x300 * sizeof(u32), "Register set structure smaller than it should be");
  586. extern Regs registers; // TODO: Not sure if we want to have one global instance for this
  587. struct float24 {
  588. static float24 FromFloat32(float val) {
  589. float24 ret;
  590. ret.value = val;
  591. return ret;
  592. }
  593. // 16 bit mantissa, 7 bit exponent, 1 bit sign
  594. // TODO: No idea if this works as intended
  595. static float24 FromRawFloat24(u32 hex) {
  596. float24 ret;
  597. if ((hex & 0xFFFFFF) == 0) {
  598. ret.value = 0;
  599. } else {
  600. u32 mantissa = hex & 0xFFFF;
  601. u32 exponent = (hex >> 16) & 0x7F;
  602. u32 sign = hex >> 23;
  603. ret.value = powf(2.0f, (float)exponent-63.0f) * (1.0f + mantissa * powf(2.0f, -16.f));
  604. if (sign)
  605. ret.value = -ret.value;
  606. }
  607. return ret;
  608. }
  609. // Not recommended for anything but logging
  610. float ToFloat32() const {
  611. return value;
  612. }
  613. float24 operator * (const float24& flt) const {
  614. return float24::FromFloat32(ToFloat32() * flt.ToFloat32());
  615. }
  616. float24 operator / (const float24& flt) const {
  617. return float24::FromFloat32(ToFloat32() / flt.ToFloat32());
  618. }
  619. float24 operator + (const float24& flt) const {
  620. return float24::FromFloat32(ToFloat32() + flt.ToFloat32());
  621. }
  622. float24 operator - (const float24& flt) const {
  623. return float24::FromFloat32(ToFloat32() - flt.ToFloat32());
  624. }
  625. float24 operator - () const {
  626. return float24::FromFloat32(-ToFloat32());
  627. }
  628. bool operator < (const float24& flt) const {
  629. return ToFloat32() < flt.ToFloat32();
  630. }
  631. bool operator > (const float24& flt) const {
  632. return ToFloat32() > flt.ToFloat32();
  633. }
  634. bool operator >= (const float24& flt) const {
  635. return ToFloat32() >= flt.ToFloat32();
  636. }
  637. bool operator <= (const float24& flt) const {
  638. return ToFloat32() <= flt.ToFloat32();
  639. }
  640. bool operator == (const float24& flt) const {
  641. return ToFloat32() == flt.ToFloat32();
  642. }
  643. bool operator != (const float24& flt) const {
  644. return ToFloat32() != flt.ToFloat32();
  645. }
  646. private:
  647. // Stored as a regular float, merely for convenience
  648. // TODO: Perform proper arithmetic on this!
  649. float value;
  650. };
  651. union CommandHeader {
  652. CommandHeader(u32 h) : hex(h) {}
  653. u32 hex;
  654. BitField< 0, 16, u32> cmd_id;
  655. BitField<16, 4, u32> parameter_mask;
  656. BitField<20, 11, u32> extra_data_length;
  657. BitField<31, 1, u32> group_commands;
  658. };
  659. // TODO: Ugly, should fix PhysicalToVirtualAddress instead
  660. inline static u32 PAddrToVAddr(u32 addr) {
  661. if (addr >= Memory::VRAM_PADDR && addr < Memory::VRAM_PADDR + Memory::VRAM_SIZE) {
  662. return addr - Memory::VRAM_PADDR + Memory::VRAM_VADDR;
  663. } else if (addr >= Memory::FCRAM_PADDR && addr < Memory::FCRAM_PADDR + Memory::FCRAM_SIZE) {
  664. return addr - Memory::FCRAM_PADDR + Memory::HEAP_LINEAR_VADDR;
  665. } else {
  666. return 0;
  667. }
  668. }
  669. } // namespace