pica.h 29 KB

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