Sony PlayStation2 Study
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
9.3 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. # _____ ___ ____ ___ ____
  3. # ____| | ____| | | |____|
  4. # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
  5. #-----------------------------------------------------------------------
  6. # (c) 2020 h4570 Sandro Sobczyński <sandro.sobczynski@gmail.com>
  7. # Licenced under Academic Free License version 2.0
  8. # Review ps2sdk README & LICENSE files for further details.
  9. */
  10. /**
  11. * @file Chain mode related functions for packet2.
  12. * @defgroup packet2_chain Chain
  13. * Chain mode related functions of libpacket2.
  14. * @ingroup packet2
  15. * @{
  16. */
  17. #ifndef __PACKET2_CHAIN_H__
  18. #define __PACKET2_CHAIN_H__
  19. #include <packet2.h>
  20. #include <packet2_types.h>
  21. #include <assert.h>
  22. #define PACKET2_COUNT_QWC 1 << 16
  23. #ifdef __cplusplus
  24. extern "C"
  25. {
  26. #endif
  27. // ---
  28. // DMA tag management
  29. // ---
  30. /**
  31. * Set dma tag.
  32. * For more details, check description of dma_tag_t.
  33. * @param tag Pointer to DMA tag to set.
  34. * @param qwc Qword count.
  35. * @param pce Priority Control Enable. 0 by default.
  36. * @param id Tag ID.
  37. * @param irq Interrupt Request. False by default.
  38. * @param addr Address.
  39. * @param spr Memory/SPR Selection. False by default.
  40. */
  41. static inline void packet2_chain_set_dma_tag(dma_tag_t *tag, u32 qwc, u32 pce, u32 id, u8 irq, const u128 *addr, u8 spr)
  42. {
  43. tag->QWC = qwc;
  44. tag->PCE = pce;
  45. tag->ID = id;
  46. tag->IRQ = irq;
  47. tag->ADDR = (u64)((u32)addr);
  48. tag->SPR = spr;
  49. }
  50. /**
  51. * Add dma tag.
  52. * For more details, check description of dma_tag_t.
  53. * Will move current buffer position by:
  54. * DWORD if packet->tte == true,
  55. * QWORD if packet->tte == false
  56. * @param packet2 Pointer to packet.
  57. * @param qwc Qword count. If you want to use
  58. * packet2_chain_close_tag(), and count qwords
  59. * automatically, set value to PACKET2_COUNT_QWC.
  60. * @param pce Priority Control Enable. 0 by default.
  61. * @param id Tag ID.
  62. * @param irq Interrupt Request. False by default.
  63. * @param addr Address.
  64. * @param spr Memory/SPR Selection. False by default.
  65. */
  66. static inline void packet2_chain_add_dma_tag(packet2_t *packet2, u32 qwc, u32 pce, enum DmaTagType id, u8 irq, const u128 *addr, u8 spr)
  67. {
  68. assert(((u32)packet2->next & 0xF) == 0); // Free space in packet is aligned properly.
  69. assert(packet2->tag_opened_at == NULL); // All previous tags are closed.
  70. if (qwc == PACKET2_COUNT_QWC)
  71. {
  72. packet2_chain_set_dma_tag((dma_tag_t *)packet2->next, 0, pce, id, irq, addr, spr); // placeholder
  73. packet2->tag_opened_at = (dma_tag_t *)packet2->next;
  74. }
  75. else
  76. {
  77. packet2_chain_set_dma_tag((dma_tag_t *)packet2->next, qwc, pce, id, irq, addr, spr);
  78. packet2->tag_opened_at = (dma_tag_t *)NULL;
  79. }
  80. if (!packet2->tte)
  81. packet2_advance_next(packet2, sizeof(dma_tag_t));
  82. else
  83. packet2_advance_next(packet2, sizeof(u64));
  84. }
  85. /**
  86. * Close dma tag.
  87. * In reality, get back to pointer "tag_opened_at" and fix qwc value.
  88. * @param packet2 Pointer to packet.
  89. */
  90. static inline void packet2_chain_close_tag(packet2_t *packet2)
  91. {
  92. assert(packet2->tag_opened_at != NULL); // There is opened tag.
  93. assert(((u32)packet2->next & 0xF) == 0); // Free space in packet is aligned properly.
  94. packet2->tag_opened_at->QWC = (((u32)packet2->next - (u32)packet2->tag_opened_at) / 16 - 1);
  95. packet2->tag_opened_at = (dma_tag_t *)NULL;
  96. }
  97. // ---
  98. // DMA tags
  99. // ---
  100. /**
  101. * Add CNT tag.
  102. * @note packet2_chain_close_tag() required. Qwords
  103. * are counted automatically.
  104. * For more details, check description of dma_tag_t.
  105. * @param packet2 Pointer to packet.
  106. * @param irq Interrupt Request. False by default.
  107. * @param pce Priority Control Enable. 0 by default.
  108. * @param spr Memory/SPR Selection. False by default.
  109. */
  110. static inline void packet2_chain_open_cnt(packet2_t *packet2, u8 irq, u32 pce, u8 spr)
  111. {
  112. packet2_chain_add_dma_tag(packet2, PACKET2_COUNT_QWC, pce, P2_DMA_TAG_CNT, irq, 0, spr);
  113. }
  114. /**
  115. * Add END tag.
  116. * @note packet2_chain_close_tag() required. Qwords
  117. * are counted automatically.
  118. * For more details, check description of dma_tag_t.
  119. * @note Don't forget about close_tag()!
  120. * @param packet2 Pointer to packet.
  121. * @param irq Interrupt Request. False by default.
  122. * @param pce Priority Control Enable. 0 by default.
  123. */
  124. static inline void packet2_chain_open_end(packet2_t *packet2, u8 irq, u32 pce)
  125. {
  126. packet2_chain_add_dma_tag(packet2, PACKET2_COUNT_QWC, pce, P2_DMA_TAG_END, irq, 0, 0);
  127. }
  128. /**
  129. * Add REF tag.
  130. * @note Qwords are NOT counted automatically, so
  131. * there is NO need to use packet2_chain_close_tag();
  132. * For more details, check description of dma_tag_t.
  133. * @param packet2 Pointer to packet.
  134. * @param ref_data Pointer to data.
  135. * @param qw_length Size of data in qwords.
  136. * @param irq Interrupt Request. False by default.
  137. * @param spr Memory/SPR Selection. False by default.
  138. * @param pce Priority Control Enable. 0 by default.
  139. */
  140. static inline void packet2_chain_ref(packet2_t *packet2, const void *ref_data, u32 qw_length, u8 irq, u8 spr, u32 pce)
  141. {
  142. assert(((u32)packet2->next & 0xF) == 0); // Free space in packet is aligned properly.
  143. packet2_chain_add_dma_tag(packet2, qw_length, pce, P2_DMA_TAG_REF, irq, (const u128 *)ref_data, spr);
  144. }
  145. /**
  146. * Add NEXT tag.
  147. * @note packet2_chain_close_tag() required. Qwords
  148. * are counted automatically.
  149. * For more details, check description of dma_tag_t.
  150. * @param next_tag Pointer to next tag.
  151. * @param irq Interrupt Request. False by default.
  152. * @param spr Memory/SPR Selection. False by default.
  153. * @param pce Priority Control Enable. 0 by default.
  154. */
  155. static inline void packet2_chain_next(packet2_t *packet2, const dma_tag_t *next_tag, u8 irq, u8 spr, u32 pce)
  156. {
  157. assert(((u32)packet2->next & 0xF) == 0); // Free space in packet is aligned properly.
  158. packet2_chain_add_dma_tag(packet2, PACKET2_COUNT_QWC, pce, P2_DMA_TAG_NEXT, irq, (u128 *)next_tag, spr);
  159. }
  160. /**
  161. * Add REFS tag.
  162. * @note Qwords are NOT counted automatically, so
  163. * there is NO need to use packet2_chain_close_tag();
  164. * For more details, check description of dma_tag_t.
  165. * @param packet2 Pointer to packet.
  166. * @param ref_data Pointer to data.
  167. * @param qw_length Size of data in qwords.
  168. * @param irq Interrupt Request. False by default.
  169. * @param spr Memory/SPR Selection. False by default.
  170. * @param pce Priority Control Enable. 0 by default.
  171. */
  172. static inline void packet2_chain_refs(packet2_t *packet2, const void *ref_data, u32 qw_length, u8 irq, u8 spr, u32 pce)
  173. {
  174. assert(((u32)packet2->next & 0xF) == 0); // Free space in packet is aligned properly.
  175. packet2_chain_add_dma_tag(packet2, qw_length, pce, P2_DMA_TAG_REFS, irq, (const u128 *)ref_data, spr);
  176. }
  177. /**
  178. * Add REFE tag.
  179. * @note Qwords are NOT counted automatically, so
  180. * there is NO need to use packet2_chain_close_tag();
  181. * For more details, check description of dma_tag_t.
  182. * @param packet2 Pointer to packet.
  183. * @param ref_data Pointer to data.
  184. * @param qw_length Size of data in qwords.
  185. * @param irq Interrupt Request. False by default.
  186. * @param spr Memory/SPR Selection. False by default.
  187. * @param pce Priority Control Enable. 0 by default.
  188. */
  189. static inline void packet2_chain_refe(packet2_t *packet2, const void *ref_data, u32 qw_length, u8 irq, u8 spr, u32 pce)
  190. {
  191. assert(((u32)packet2->next & 0xF) == 0); // Free space in packet is aligned properly.
  192. packet2_chain_add_dma_tag(packet2, qw_length, pce, P2_DMA_TAG_REFE, irq, (const u128 *)ref_data, spr);
  193. }
  194. /**
  195. * Add CALL tag.
  196. * @note packet2_chain_close_tag() required. Qwords
  197. * are counted automatically.
  198. * For more details, check description of dma_tag_t.
  199. * @param packet2 Pointer to packet.
  200. * @param next_tag Pointer to next tag.
  201. * @param irq Interrupt Request. False by default.
  202. * @param spr Memory/SPR Selection. False by default.
  203. * @param pce Priority Control Enable. 0 by default.
  204. */
  205. static inline void packet2_chain_call(packet2_t *packet2, const void *next_tag, u8 irq, u8 spr, u32 pce)
  206. {
  207. assert(((u32)packet2->next & 0xF) == 0); // Free space in packet is aligned properly.
  208. packet2_chain_add_dma_tag(packet2, PACKET2_COUNT_QWC, pce, P2_DMA_TAG_CALL, irq, (u128 *)next_tag, spr);
  209. }
  210. /**
  211. * Add RET tag.
  212. * @note packet2_chain_close_tag() required. Qwords
  213. * are counted automatically.
  214. * For more details, check description of dma_tag_t.
  215. * @param packet2 Pointer to packet.
  216. * @param irq Interrupt Request. False by default.
  217. * @param pce Priority Control Enable. 0 by default.
  218. */
  219. static inline void packet2_chain_ret(packet2_t *packet2, u8 irq, u32 pce)
  220. {
  221. packet2_chain_add_dma_tag(packet2, PACKET2_COUNT_QWC, pce, P2_DMA_TAG_RET, irq, 0, 0);
  222. }
  223. #ifdef __cplusplus
  224. }
  225. #endif
  226. #endif /* __PACKET2_CHAIN_H__ */
  227. /** @} */ // end of packet2_chain subgroup