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.

217 lines
6.8 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 Successor of standard packet library.
  12. * @defgroup packet2 Packet2 library
  13. * Successor of packet_t. Main goal of this library is to
  14. * create simple API for packet management.
  15. * Library is splitted into several parts:
  16. * - This - core functionality of libpacket2
  17. * - "_types" - Enums and structures used in libpacket2
  18. * - "_chain" - DMA tags
  19. * - "_vif" - VU related
  20. * - "_utils" - useful functions, and examples
  21. * @{
  22. */
  23. #ifndef __PACKET2_H__
  24. #define __PACKET2_H__
  25. #include <packet2_types.h>
  26. #ifdef __cplusplus
  27. extern "C"
  28. {
  29. #endif
  30. // ---
  31. // Packet2 management
  32. // ---
  33. /**
  34. * Allocate new packet2.
  35. * @param qwords Maximum data size in qwords (128bit).
  36. * @param type Memory mapping type.
  37. * @param mode Packet mode. Normal or chain.
  38. * @param tte Tag transfer enable.
  39. * Used only for CHAIN mode!
  40. * If >0 transfer tag is set during packet sending and
  41. * add_dma_tag() (so also every open_tag()) will move buffer by DWORD,
  42. * so remember to align memory!
  43. * @returns Pointer to packet2 on success or NULL if memory allocation fail.
  44. */
  45. packet2_t *packet2_create(u16 qwords, enum Packet2Type type, enum Packet2Mode mode, u8 tte);
  46. /**
  47. * Create new packet2 with given data pointer.
  48. * @param base Pointer to base (start of buffer).
  49. * @param next Pointer to next (current position of buffer).
  50. * @param qwords Maximum data size in qwords (128bit).
  51. * @param type Memory mapping type.
  52. * @param mode Packet mode. Normal or chain (so also vif/vu).
  53. * @param tte Tag transfer enable.
  54. * Used only for CHAIN mode!
  55. * If >0 transfer tag is set during packet sending and
  56. * add_dma_tag() (so also every open_tag()) will move buffer by DWORD,
  57. * so remember to align memory!
  58. * @returns Pointer to packet2 on success or NULL if memory allocation fail.
  59. */
  60. packet2_t *packet2_create_from(qword_t *base, qword_t *next, u16 qwords, enum Packet2Type type, enum Packet2Mode mode, u8 tte);
  61. /**
  62. * Free packet2 memory.
  63. * @param packet2 Pointer to packet2.
  64. */
  65. void packet2_free(packet2_t *packet2);
  66. /**
  67. * Reset packet.
  68. * Move next pointer back to base pointer,
  69. * Make all "openedAt" vars to NULL
  70. * Do memset on data if required.
  71. * @param packet2 Pointer to packet.
  72. * @param clear_mem If >0, data is cleared via memset(). SLOW!
  73. */
  74. void packet2_reset(packet2_t *packet2, u8 clear_mem);
  75. /**
  76. * Update current position of packet buffer.
  77. * Useful with drawlib functions.
  78. */
  79. static inline void packet2_update(packet2_t *packet2, qword_t *qw) { packet2->next = qw; }
  80. // ---
  81. // Basic add
  82. // ---
  83. /** Move "next" pointer forward. */
  84. static inline void packet2_advance_next(packet2_t *packet2, u32 i)
  85. {
  86. packet2->next = (qword_t *)((u8 *)packet2->next + i);
  87. }
  88. static inline void packet2_add_u128(packet2_t *packet2, const u128 val)
  89. {
  90. *((u128 *)packet2->next) = val;
  91. packet2_advance_next(packet2, sizeof(u128));
  92. }
  93. /** @note Alignment alert. Size of dword (1/2) */
  94. static inline void packet2_add_s64(packet2_t *packet2, const s64 val)
  95. {
  96. *((s64 *)packet2->next) = val;
  97. packet2_advance_next(packet2, sizeof(s64));
  98. }
  99. static inline void packet2_add_2x_s64(packet2_t *packet2, const s64 v1, const s64 v2)
  100. {
  101. packet2_add_s64(packet2, v1);
  102. packet2_add_s64(packet2, v2);
  103. }
  104. static inline void packet2_add_s128(packet2_t *packet2, const s128 val)
  105. {
  106. *((s128 *)packet2->next) = val;
  107. packet2_advance_next(packet2, sizeof(s128));
  108. }
  109. /** @note Alignment alert. Size of dword (1/2) */
  110. static inline void packet2_add_u64(packet2_t *packet2, const u64 val)
  111. {
  112. *((u64 *)packet2->next) = val;
  113. packet2_advance_next(packet2, sizeof(u64));
  114. }
  115. /** @note Alignment alert. Size of word (1/4) */
  116. static inline void packet2_add_u32(packet2_t *packet2, const u32 val)
  117. {
  118. *((u32 *)packet2->next) = val;
  119. packet2_advance_next(packet2, sizeof(u32));
  120. }
  121. /** @note Alignment alert. Size of word (1/4) */
  122. static inline void packet2_add_s32(packet2_t *packet2, const s32 val)
  123. {
  124. *((s32 *)packet2->next) = val;
  125. packet2_advance_next(packet2, sizeof(s32));
  126. }
  127. /** @note Alignment alert. Size of word (1/4) */
  128. static inline void packet2_add_float(packet2_t *packet2, const float val)
  129. {
  130. *((float *)packet2->next) = val;
  131. packet2_advance_next(packet2, sizeof(float));
  132. }
  133. static inline void packet2_add_data(packet2_t *packet2, void *t_data, u32 t_size)
  134. {
  135. int i;
  136. for (i = 0; i < t_size; i++)
  137. packet2_add_u128(packet2, ((u128 *)t_data)[i]);
  138. }
  139. /**
  140. * Fill to align to 96bits
  141. */
  142. static inline void packet2_pad96(packet2_t *packet2, const u32 val)
  143. {
  144. while ((((u32)packet2->next + 4) & 0xf) != 0)
  145. packet2_add_u32(packet2, val);
  146. }
  147. /**
  148. * Fill to align to 128bits
  149. */
  150. static inline void packet2_pad128(packet2_t *packet2, const u32 val)
  151. {
  152. while (((u32)packet2->next & 0xf) != 0)
  153. packet2_add_u32(packet2, val);
  154. }
  155. // ---
  156. // Other
  157. // ---
  158. /**
  159. * Print data of packet.
  160. * @param packet2 Pointer to packet.
  161. * @param clear_mem Count of qwords, type 0 for all.
  162. */
  163. void packet2_print(packet2_t *packet2, u32 qw_count);
  164. /**
  165. * Print amount of qwords of packet.
  166. * @param packet2 Pointer to packet.
  167. */
  168. void packet2_print_qw_count(packet2_t *packet2);
  169. /** Copy data from b packet into a packet with memcpy() */
  170. void packet2_add(packet2_t *a, packet2_t *b);
  171. /** Returns count of added qwords into packet. */
  172. static inline u32 packet2_get_qw_count(packet2_t *packet2) { return ((u32)packet2->next - (u32)packet2->base) >> 4; }
  173. /** True if packet doesnt have even number of quads. */
  174. static inline u8 packet2_doesnt_have_even_number_of_quads(packet2_t *packet2) { return ((u32)packet2->next & 0xF) != 0; }
  175. /** True if dma tag is opened. */
  176. static inline u8 packet2_is_dma_tag_opened(packet2_t *packet2) { return packet2->tag_opened_at != NULL; }
  177. /** True if DIRECT/UNPACK is opened. */
  178. static inline u8 packet2_is_vif_code_opened(packet2_t *packet2) { return packet2->vif_code_opened_at != NULL; }
  179. #ifdef __cplusplus
  180. }
  181. #endif
  182. #endif /* __PACKET2_H__ */
  183. /** @} */ // end of packet2 group