pica.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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(0x3);
  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 DepthFunc : u32 {
  245. Always = 1,
  246. LessThan = 4,
  247. GreaterThan = 6,
  248. };
  249. union {
  250. // If false, logic blending is used
  251. BitField<8, 1, u32> alphablend_enable;
  252. };
  253. union {
  254. enum BlendEquation : u32 {
  255. Add = 0,
  256. };
  257. enum BlendFactor : u32 {
  258. Zero = 0,
  259. One = 1,
  260. SourceAlpha = 6,
  261. OneMinusSourceAlpha = 7,
  262. };
  263. BitField< 0, 8, BlendEquation> blend_equation_rgb;
  264. BitField< 8, 8, BlendEquation> blend_equation_a;
  265. BitField<16, 4, BlendFactor> factor_source_rgb;
  266. BitField<20, 4, BlendFactor> factor_dest_rgb;
  267. BitField<24, 4, BlendFactor> factor_source_a;
  268. BitField<28, 4, BlendFactor> factor_dest_a;
  269. } alpha_blending;
  270. union {
  271. enum Op {
  272. Set = 4,
  273. };
  274. BitField<0, 4, Op> op;
  275. } logic_op;
  276. INSERT_PADDING_WORDS(0x4);
  277. union {
  278. BitField< 0, 1, u32> depth_test_enable;
  279. BitField< 4, 3, DepthFunc> depth_test_func;
  280. BitField<12, 1, u32> depth_write_enable;
  281. };
  282. INSERT_PADDING_WORDS(0x8);
  283. } output_merger;
  284. struct {
  285. enum ColorFormat : u32 {
  286. RGBA8 = 0,
  287. RGB8 = 1,
  288. RGBA5551 = 2,
  289. RGB565 = 3,
  290. RGBA4 = 4,
  291. };
  292. INSERT_PADDING_WORDS(0x6);
  293. u32 depth_format;
  294. u32 color_format;
  295. INSERT_PADDING_WORDS(0x4);
  296. u32 depth_buffer_address;
  297. u32 color_buffer_address;
  298. union {
  299. // Apparently, the framebuffer width is stored as expected,
  300. // while the height is stored as the actual height minus one.
  301. // Hence, don't access these fields directly but use the accessors
  302. // GetWidth() and GetHeight() instead.
  303. BitField< 0, 11, u32> width;
  304. BitField<12, 10, u32> height;
  305. };
  306. INSERT_PADDING_WORDS(0x1);
  307. inline u32 GetColorBufferPhysicalAddress() const {
  308. return DecodeAddressRegister(color_buffer_address);
  309. }
  310. inline u32 GetDepthBufferPhysicalAddress() const {
  311. return DecodeAddressRegister(depth_buffer_address);
  312. }
  313. inline u32 GetWidth() const {
  314. return width;
  315. }
  316. inline u32 GetHeight() const {
  317. return height + 1;
  318. }
  319. } framebuffer;
  320. INSERT_PADDING_WORDS(0xe0);
  321. struct {
  322. enum class Format : u64 {
  323. BYTE = 0,
  324. UBYTE = 1,
  325. SHORT = 2,
  326. FLOAT = 3,
  327. };
  328. BitField<0, 29, u32> base_address;
  329. u32 GetPhysicalBaseAddress() const {
  330. return DecodeAddressRegister(base_address);
  331. }
  332. // Descriptor for internal vertex attributes
  333. union {
  334. BitField< 0, 2, Format> format0; // size of one element
  335. BitField< 2, 2, u64> size0; // number of elements minus 1
  336. BitField< 4, 2, Format> format1;
  337. BitField< 6, 2, u64> size1;
  338. BitField< 8, 2, Format> format2;
  339. BitField<10, 2, u64> size2;
  340. BitField<12, 2, Format> format3;
  341. BitField<14, 2, u64> size3;
  342. BitField<16, 2, Format> format4;
  343. BitField<18, 2, u64> size4;
  344. BitField<20, 2, Format> format5;
  345. BitField<22, 2, u64> size5;
  346. BitField<24, 2, Format> format6;
  347. BitField<26, 2, u64> size6;
  348. BitField<28, 2, Format> format7;
  349. BitField<30, 2, u64> size7;
  350. BitField<32, 2, Format> format8;
  351. BitField<34, 2, u64> size8;
  352. BitField<36, 2, Format> format9;
  353. BitField<38, 2, u64> size9;
  354. BitField<40, 2, Format> format10;
  355. BitField<42, 2, u64> size10;
  356. BitField<44, 2, Format> format11;
  357. BitField<46, 2, u64> size11;
  358. BitField<48, 12, u64> attribute_mask;
  359. // number of total attributes minus 1
  360. BitField<60, 4, u64> num_extra_attributes;
  361. };
  362. inline Format GetFormat(int n) const {
  363. Format formats[] = {
  364. format0, format1, format2, format3,
  365. format4, format5, format6, format7,
  366. format8, format9, format10, format11
  367. };
  368. return formats[n];
  369. }
  370. inline int GetNumElements(int n) const {
  371. u64 sizes[] = {
  372. size0, size1, size2, size3,
  373. size4, size5, size6, size7,
  374. size8, size9, size10, size11
  375. };
  376. return (int)sizes[n]+1;
  377. }
  378. inline int GetElementSizeInBytes(int n) const {
  379. return (GetFormat(n) == Format::FLOAT) ? 4 :
  380. (GetFormat(n) == Format::SHORT) ? 2 : 1;
  381. }
  382. inline int GetStride(int n) const {
  383. return GetNumElements(n) * GetElementSizeInBytes(n);
  384. }
  385. inline int GetNumTotalAttributes() const {
  386. return (int)num_extra_attributes+1;
  387. }
  388. // Attribute loaders map the source vertex data to input attributes
  389. // This e.g. allows to load different attributes from different memory locations
  390. struct {
  391. // Source attribute data offset from the base address
  392. u32 data_offset;
  393. union {
  394. BitField< 0, 4, u64> comp0;
  395. BitField< 4, 4, u64> comp1;
  396. BitField< 8, 4, u64> comp2;
  397. BitField<12, 4, u64> comp3;
  398. BitField<16, 4, u64> comp4;
  399. BitField<20, 4, u64> comp5;
  400. BitField<24, 4, u64> comp6;
  401. BitField<28, 4, u64> comp7;
  402. BitField<32, 4, u64> comp8;
  403. BitField<36, 4, u64> comp9;
  404. BitField<40, 4, u64> comp10;
  405. BitField<44, 4, u64> comp11;
  406. // bytes for a single vertex in this loader
  407. BitField<48, 8, u64> byte_count;
  408. BitField<60, 4, u64> component_count;
  409. };
  410. inline int GetComponent(int n) const {
  411. u64 components[] = {
  412. comp0, comp1, comp2, comp3,
  413. comp4, comp5, comp6, comp7,
  414. comp8, comp9, comp10, comp11
  415. };
  416. return (int)components[n];
  417. }
  418. } attribute_loaders[12];
  419. } vertex_attributes;
  420. struct {
  421. enum IndexFormat : u32 {
  422. BYTE = 0,
  423. SHORT = 1,
  424. };
  425. union {
  426. BitField<0, 31, u32> offset; // relative to base attribute address
  427. BitField<31, 1, IndexFormat> format;
  428. };
  429. } index_array;
  430. // Number of vertices to render
  431. u32 num_vertices;
  432. INSERT_PADDING_WORDS(0x5);
  433. // These two trigger rendering of triangles
  434. u32 trigger_draw;
  435. u32 trigger_draw_indexed;
  436. INSERT_PADDING_WORDS(0x2e);
  437. enum class TriangleTopology : u32 {
  438. List = 0,
  439. Strip = 1,
  440. Fan = 2,
  441. ListIndexed = 3, // TODO: No idea if this is correct
  442. };
  443. BitField<8, 2, TriangleTopology> triangle_topology;
  444. INSERT_PADDING_WORDS(0x51);
  445. BitField<0, 16, u32> vs_bool_uniforms;
  446. union {
  447. BitField< 0, 8, u32> x;
  448. BitField< 8, 8, u32> y;
  449. BitField<16, 8, u32> z;
  450. BitField<24, 8, u32> w;
  451. } vs_int_uniforms[4];
  452. INSERT_PADDING_WORDS(0x5);
  453. // Offset to shader program entry point (in words)
  454. BitField<0, 16, u32> vs_main_offset;
  455. union {
  456. BitField< 0, 4, u64> attribute0_register;
  457. BitField< 4, 4, u64> attribute1_register;
  458. BitField< 8, 4, u64> attribute2_register;
  459. BitField<12, 4, u64> attribute3_register;
  460. BitField<16, 4, u64> attribute4_register;
  461. BitField<20, 4, u64> attribute5_register;
  462. BitField<24, 4, u64> attribute6_register;
  463. BitField<28, 4, u64> attribute7_register;
  464. BitField<32, 4, u64> attribute8_register;
  465. BitField<36, 4, u64> attribute9_register;
  466. BitField<40, 4, u64> attribute10_register;
  467. BitField<44, 4, u64> attribute11_register;
  468. BitField<48, 4, u64> attribute12_register;
  469. BitField<52, 4, u64> attribute13_register;
  470. BitField<56, 4, u64> attribute14_register;
  471. BitField<60, 4, u64> attribute15_register;
  472. int GetRegisterForAttribute(int attribute_index) {
  473. u64 fields[] = {
  474. attribute0_register, attribute1_register, attribute2_register, attribute3_register,
  475. attribute4_register, attribute5_register, attribute6_register, attribute7_register,
  476. attribute8_register, attribute9_register, attribute10_register, attribute11_register,
  477. attribute12_register, attribute13_register, attribute14_register, attribute15_register,
  478. };
  479. return (int)fields[attribute_index];
  480. }
  481. } vs_input_register_map;
  482. INSERT_PADDING_WORDS(0x3);
  483. struct {
  484. enum Format : u32
  485. {
  486. FLOAT24 = 0,
  487. FLOAT32 = 1
  488. };
  489. bool IsFloat32() const {
  490. return format == FLOAT32;
  491. }
  492. union {
  493. // Index of the next uniform to write to
  494. // TODO: ctrulib uses 8 bits for this, however that seems to yield lots of invalid indices
  495. BitField<0, 7, u32> index;
  496. BitField<31, 1, Format> format;
  497. };
  498. // Writing to these registers sets the "current" uniform.
  499. // TODO: It's not clear how the hardware stores what the "current" uniform is.
  500. u32 set_value[8];
  501. } vs_uniform_setup;
  502. INSERT_PADDING_WORDS(0x2);
  503. struct {
  504. u32 begin_load;
  505. // Writing to these registers sets the "current" word in the shader program.
  506. // TODO: It's not clear how the hardware stores what the "current" word is.
  507. u32 set_word[8];
  508. } vs_program;
  509. INSERT_PADDING_WORDS(0x1);
  510. // This register group is used to load an internal table of swizzling patterns,
  511. // which are indexed by each shader instruction to specify vector component swizzling.
  512. struct {
  513. u32 begin_load;
  514. // Writing to these registers sets the "current" swizzle pattern in the table.
  515. // TODO: It's not clear how the hardware stores what the "current" swizzle pattern is.
  516. u32 set_word[8];
  517. } vs_swizzle_patterns;
  518. INSERT_PADDING_WORDS(0x22);
  519. #undef INSERT_PADDING_WORDS_HELPER1
  520. #undef INSERT_PADDING_WORDS_HELPER2
  521. #undef INSERT_PADDING_WORDS
  522. // Map register indices to names readable by humans
  523. // Used for debugging purposes, so performance is not an issue here
  524. static std::string GetCommandName(int index) {
  525. std::map<u32, std::string> map;
  526. #define ADD_FIELD(name) \
  527. do { \
  528. map.insert({PICA_REG_INDEX(name), #name}); \
  529. /* TODO: change to Regs::name when VS2015 and other compilers support it */ \
  530. for (u32 i = PICA_REG_INDEX(name) + 1; i < PICA_REG_INDEX(name) + sizeof(Regs().name) / 4; ++i) \
  531. map.insert({i, #name + std::string("+") + std::to_string(i-PICA_REG_INDEX(name))}); \
  532. } while(false)
  533. ADD_FIELD(trigger_irq);
  534. ADD_FIELD(viewport_size_x);
  535. ADD_FIELD(viewport_size_y);
  536. ADD_FIELD(viewport_depth_range);
  537. ADD_FIELD(viewport_depth_far_plane);
  538. ADD_FIELD(viewport_corner);
  539. ADD_FIELD(texture0_enable);
  540. ADD_FIELD(texture0);
  541. ADD_FIELD(texture0_format);
  542. ADD_FIELD(texture1);
  543. ADD_FIELD(texture1_format);
  544. ADD_FIELD(texture2);
  545. ADD_FIELD(texture2_format);
  546. ADD_FIELD(tev_stage0);
  547. ADD_FIELD(tev_stage1);
  548. ADD_FIELD(tev_stage2);
  549. ADD_FIELD(tev_stage3);
  550. ADD_FIELD(tev_stage4);
  551. ADD_FIELD(tev_stage5);
  552. ADD_FIELD(output_merger);
  553. ADD_FIELD(framebuffer);
  554. ADD_FIELD(vertex_attributes);
  555. ADD_FIELD(index_array);
  556. ADD_FIELD(num_vertices);
  557. ADD_FIELD(trigger_draw);
  558. ADD_FIELD(trigger_draw_indexed);
  559. ADD_FIELD(triangle_topology);
  560. ADD_FIELD(vs_bool_uniforms);
  561. ADD_FIELD(vs_int_uniforms);
  562. ADD_FIELD(vs_main_offset);
  563. ADD_FIELD(vs_input_register_map);
  564. ADD_FIELD(vs_uniform_setup);
  565. ADD_FIELD(vs_program);
  566. ADD_FIELD(vs_swizzle_patterns);
  567. #undef ADD_FIELD
  568. // Return empty string if no match is found
  569. return map[index];
  570. }
  571. static inline size_t NumIds() {
  572. return sizeof(Regs) / sizeof(u32);
  573. }
  574. u32& operator [] (int index) const {
  575. u32* content = (u32*)this;
  576. return content[index];
  577. }
  578. u32& operator [] (int index) {
  579. u32* content = (u32*)this;
  580. return content[index];
  581. }
  582. private:
  583. /*
  584. * Most physical addresses which Pica registers refer to are 8-byte aligned.
  585. * This function should be used to get the address from a raw register value.
  586. */
  587. static inline u32 DecodeAddressRegister(u32 register_value) {
  588. return register_value * 8;
  589. }
  590. };
  591. // TODO: MSVC does not support using offsetof() on non-static data members even though this
  592. // is technically allowed since C++11. This macro should be enabled once MSVC adds
  593. // support for that.
  594. #ifndef _MSC_VER
  595. #define ASSERT_REG_POSITION(field_name, position) static_assert(offsetof(Regs, field_name) == position * 4, "Field "#field_name" has invalid position")
  596. ASSERT_REG_POSITION(trigger_irq, 0x10);
  597. ASSERT_REG_POSITION(viewport_size_x, 0x41);
  598. ASSERT_REG_POSITION(viewport_size_y, 0x43);
  599. ASSERT_REG_POSITION(viewport_depth_range, 0x4d);
  600. ASSERT_REG_POSITION(viewport_depth_far_plane, 0x4e);
  601. ASSERT_REG_POSITION(vs_output_attributes[0], 0x50);
  602. ASSERT_REG_POSITION(vs_output_attributes[1], 0x51);
  603. ASSERT_REG_POSITION(viewport_corner, 0x68);
  604. ASSERT_REG_POSITION(texture0_enable, 0x80);
  605. ASSERT_REG_POSITION(texture0, 0x81);
  606. ASSERT_REG_POSITION(texture0_format, 0x8e);
  607. ASSERT_REG_POSITION(texture1, 0x91);
  608. ASSERT_REG_POSITION(texture1_format, 0x96);
  609. ASSERT_REG_POSITION(texture2, 0x99);
  610. ASSERT_REG_POSITION(texture2_format, 0x9e);
  611. ASSERT_REG_POSITION(tev_stage0, 0xc0);
  612. ASSERT_REG_POSITION(tev_stage1, 0xc8);
  613. ASSERT_REG_POSITION(tev_stage2, 0xd0);
  614. ASSERT_REG_POSITION(tev_stage3, 0xd8);
  615. ASSERT_REG_POSITION(tev_stage4, 0xf0);
  616. ASSERT_REG_POSITION(tev_stage5, 0xf8);
  617. ASSERT_REG_POSITION(output_merger, 0x100);
  618. ASSERT_REG_POSITION(framebuffer, 0x110);
  619. ASSERT_REG_POSITION(vertex_attributes, 0x200);
  620. ASSERT_REG_POSITION(index_array, 0x227);
  621. ASSERT_REG_POSITION(num_vertices, 0x228);
  622. ASSERT_REG_POSITION(trigger_draw, 0x22e);
  623. ASSERT_REG_POSITION(trigger_draw_indexed, 0x22f);
  624. ASSERT_REG_POSITION(triangle_topology, 0x25e);
  625. ASSERT_REG_POSITION(vs_bool_uniforms, 0x2b0);
  626. ASSERT_REG_POSITION(vs_int_uniforms, 0x2b1);
  627. ASSERT_REG_POSITION(vs_main_offset, 0x2ba);
  628. ASSERT_REG_POSITION(vs_input_register_map, 0x2bb);
  629. ASSERT_REG_POSITION(vs_uniform_setup, 0x2c0);
  630. ASSERT_REG_POSITION(vs_program, 0x2cb);
  631. ASSERT_REG_POSITION(vs_swizzle_patterns, 0x2d5);
  632. #undef ASSERT_REG_POSITION
  633. #endif // !defined(_MSC_VER)
  634. // The total number of registers is chosen arbitrarily, but let's make sure it's not some odd value anyway.
  635. static_assert(sizeof(Regs) <= 0x300 * sizeof(u32), "Register set structure larger than it should be");
  636. static_assert(sizeof(Regs) >= 0x300 * sizeof(u32), "Register set structure smaller than it should be");
  637. extern Regs registers; // TODO: Not sure if we want to have one global instance for this
  638. struct float24 {
  639. static float24 FromFloat32(float val) {
  640. float24 ret;
  641. ret.value = val;
  642. return ret;
  643. }
  644. // 16 bit mantissa, 7 bit exponent, 1 bit sign
  645. // TODO: No idea if this works as intended
  646. static float24 FromRawFloat24(u32 hex) {
  647. float24 ret;
  648. if ((hex & 0xFFFFFF) == 0) {
  649. ret.value = 0;
  650. } else {
  651. u32 mantissa = hex & 0xFFFF;
  652. u32 exponent = (hex >> 16) & 0x7F;
  653. u32 sign = hex >> 23;
  654. ret.value = powf(2.0f, (float)exponent-63.0f) * (1.0f + mantissa * powf(2.0f, -16.f));
  655. if (sign)
  656. ret.value = -ret.value;
  657. }
  658. return ret;
  659. }
  660. // Not recommended for anything but logging
  661. float ToFloat32() const {
  662. return value;
  663. }
  664. float24 operator * (const float24& flt) const {
  665. return float24::FromFloat32(ToFloat32() * flt.ToFloat32());
  666. }
  667. float24 operator / (const float24& flt) const {
  668. return float24::FromFloat32(ToFloat32() / flt.ToFloat32());
  669. }
  670. float24 operator + (const float24& flt) const {
  671. return float24::FromFloat32(ToFloat32() + flt.ToFloat32());
  672. }
  673. float24 operator - (const float24& flt) const {
  674. return float24::FromFloat32(ToFloat32() - flt.ToFloat32());
  675. }
  676. float24& operator *= (const float24& flt) {
  677. value *= flt.ToFloat32();
  678. return *this;
  679. }
  680. float24& operator /= (const float24& flt) {
  681. value /= flt.ToFloat32();
  682. return *this;
  683. }
  684. float24& operator += (const float24& flt) {
  685. value += flt.ToFloat32();
  686. return *this;
  687. }
  688. float24& operator -= (const float24& flt) {
  689. value -= flt.ToFloat32();
  690. return *this;
  691. }
  692. float24 operator - () const {
  693. return float24::FromFloat32(-ToFloat32());
  694. }
  695. bool operator < (const float24& flt) const {
  696. return ToFloat32() < flt.ToFloat32();
  697. }
  698. bool operator > (const float24& flt) const {
  699. return ToFloat32() > flt.ToFloat32();
  700. }
  701. bool operator >= (const float24& flt) const {
  702. return ToFloat32() >= flt.ToFloat32();
  703. }
  704. bool operator <= (const float24& flt) const {
  705. return ToFloat32() <= flt.ToFloat32();
  706. }
  707. bool operator == (const float24& flt) const {
  708. return ToFloat32() == flt.ToFloat32();
  709. }
  710. bool operator != (const float24& flt) const {
  711. return ToFloat32() != flt.ToFloat32();
  712. }
  713. private:
  714. // Stored as a regular float, merely for convenience
  715. // TODO: Perform proper arithmetic on this!
  716. float value;
  717. };
  718. union CommandHeader {
  719. CommandHeader(u32 h) : hex(h) {}
  720. u32 hex;
  721. BitField< 0, 16, u32> cmd_id;
  722. BitField<16, 4, u32> parameter_mask;
  723. BitField<20, 11, u32> extra_data_length;
  724. BitField<31, 1, u32> group_commands;
  725. };
  726. // TODO: Ugly, should fix PhysicalToVirtualAddress instead
  727. inline static u32 PAddrToVAddr(u32 addr) {
  728. if (addr >= Memory::VRAM_PADDR && addr < Memory::VRAM_PADDR + Memory::VRAM_SIZE) {
  729. return addr - Memory::VRAM_PADDR + Memory::VRAM_VADDR;
  730. } else if (addr >= Memory::FCRAM_PADDR && addr < Memory::FCRAM_PADDR + Memory::FCRAM_SIZE) {
  731. return addr - Memory::FCRAM_PADDR + Memory::HEAP_LINEAR_VADDR;
  732. } else {
  733. return 0;
  734. }
  735. }
  736. } // namespace