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.

401 lines
14 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 VIF related functions for packet2.
  12. * @defgroup packet2_vif VIF
  13. * VIF related functions of libpacket2.
  14. * @ingroup packet2
  15. * @{
  16. */
  17. #ifndef __PACKET2_VIF_H__
  18. #define __PACKET2_VIF_H__
  19. #include <packet2.h>
  20. #include <packet2_types.h>
  21. #include <assert.h>
  22. #define MAKE_VIF_CODE(_immediate, _num, _cmd, _irq) ((u32)(_immediate) | ((u32)(_num) << 16) | ((u32)(_cmd) << 24) | ((u32)(_irq) << 31))
  23. #ifdef __cplusplus
  24. extern "C"
  25. {
  26. #endif
  27. /**
  28. * Add UNPACK VIF opcode.
  29. * @note packet2_vif_close_unpack() required. Qwords
  30. * are counted automatically.
  31. * For more details, check description of vif_code_t.
  32. * @param packet2 Pointer to packet.
  33. * @param mode Unpack mode
  34. * @param vuAddr Memory address (divided by 16).
  35. * @param dblBuffered (VIF1 only)
  36. * 1 - Adds VIF1_TOPS register to ADDR
  37. * 0 - Does not use VIF1_TOPS register.
  38. * @param masked Is masked
  39. * @param usigned
  40. * 1 - Unsigned - Decompress by padding 0 to the upper field
  41. * 0 - Signed - Decompress by sign extension
  42. * @param irq Interrupt Request. False by default.
  43. */
  44. static inline void packet2_vif_open_unpack(packet2_t *packet2, enum UnpackMode mode, u32 vuAddr, u8 dblBuffered, u8 masked, u8 usigned, u8 irq)
  45. {
  46. assert(packet2->vif_code_opened_at == NULL); // All previous UNPACK/DIRECT are closed.
  47. packet2->vif_code_opened_at = (vif_code_t *)packet2->next;
  48. packet2_add_u32(packet2,
  49. MAKE_VIF_CODE(vuAddr | ((u32)usigned << 14) | ((u32)dblBuffered << 15),
  50. 0,
  51. mode | ((u32)masked << 4) | 0x60, irq));
  52. }
  53. /**
  54. * Close UNPACK manually.
  55. * In reality, get back to pointer "vif_code_opened_at" and
  56. * fix num value with qwords counted from last packet2_vif_open_unpack().
  57. * @param packet2 Pointer to packet.
  58. * @param unpack_num Amount of data written to the VU Mem (qwords) or MicroMem (dwords).
  59. * 256 is max value!
  60. */
  61. static inline void packet2_vif_close_unpack_manual(packet2_t *packet2, u32 unpack_num)
  62. {
  63. assert(packet2->vif_code_opened_at != NULL); // There is open UNPACK/DIRECT.
  64. assert(((u32)packet2->next & 0x3) == 0); // Make sure we're u32 aligned
  65. assert((packet2->vif_code_opened_at->cmd & 0x60) == 0x60); // It was UNPACK
  66. assert(unpack_num <= 256);
  67. packet2->vif_code_opened_at->num = (unpack_num == 256) ? 0 : unpack_num;
  68. packet2->vif_code_opened_at = (vif_code_t *)NULL;
  69. }
  70. /**
  71. * Close UNPACK automatically.
  72. * In reality, get back to pointer "vif_code_opened_at" and
  73. * fix num value with qwords counted from last packet2_vif_open_unpack().
  74. * @param packet2 Pointer to packet.
  75. * @param wl WL Value (look at STCYCL)
  76. * @param cl CL Value (look at STCYCL)
  77. * @returns Unpacked qwords count
  78. */
  79. u32 packet2_vif_close_unpack_auto(packet2_t *packet2, u32 wl, u32 cl);
  80. /**
  81. * Add DIRECT VIF opcode.
  82. * @note Direct must be closed via
  83. * packet2_vif_close_direct_manual() or
  84. * packet2_vif_close : "memory"
  85. */
  86. static inline void packet2_vif_open_direct(packet2_t *packet2, u8 irq)
  87. {
  88. assert(packet2->vif_code_opened_at == NULL); // All previous UNPACK/DIRECT are closed.
  89. packet2->vif_code_opened_at = (vif_code_t *)packet2->next;
  90. packet2_add_u32(packet2, MAKE_VIF_CODE(0, 0, P2_VIF_DIRECT, irq));
  91. }
  92. /**
  93. * Close DIRECT manually.
  94. * In reality, get back to pointer "vif_code_opened_at" and
  95. * fix immediate value with given value.
  96. * @param packet2 Pointer to packet.
  97. * @param qwords Qwords count.
  98. */
  99. static inline void packet2_vif_close_direct_manual(packet2_t *packet2, u32 qwords)
  100. {
  101. assert(packet2->vif_code_opened_at != NULL); // There is open UNPACK/DIRECT.
  102. assert((((u32)packet2->next - ((u32)packet2->vif_code_opened_at + 4)) & 0xF) == 0);
  103. packet2->vif_code_opened_at->immediate = qwords;
  104. packet2->vif_code_opened_at = (vif_code_t *)NULL;
  105. }
  106. /**
  107. * Close DIRECT automatically.
  108. * In reality, get back to pointer "vif_code_opened_at" and
  109. * fix immediate value with qwords counted from last packet2_vif_open_direct().
  110. * @param packet2 Pointer to packet.
  111. */
  112. static inline void packet2_vif_close_direct_auto(packet2_t *packet2)
  113. {
  114. return packet2_vif_close_direct_manual(packet2, ((u32)packet2->next - ((u32)packet2->vif_code_opened_at + 4)) >> 4);
  115. }
  116. /**
  117. * Add NOP VIF opcode.
  118. * @note Alignment alert. Size of word (1/4).
  119. * For more details, check description of VIFOpcode.
  120. * @param packet2 Pointer to packet.
  121. * @param irq Interrupt Request. False by default.
  122. */
  123. static inline void packet2_vif_nop(packet2_t *packet2, u8 irq)
  124. {
  125. packet2_add_u32(packet2, MAKE_VIF_CODE(0, 0, P2_VIF_NOP, irq));
  126. }
  127. /**
  128. * Fill with NOP VIF opcode to align to 96bits
  129. * @param packet2 Pointer to packet.
  130. */
  131. static inline void packet2_vif_pad96(packet2_t *packet2)
  132. {
  133. packet2_pad96(packet2, MAKE_VIF_CODE(0, 0, P2_VIF_NOP, 0));
  134. }
  135. /**
  136. * Fill with NOP VIF opcode to align to 128bits
  137. * @param packet2 Pointer to packet.
  138. */
  139. static inline void packet2_vif_pad128(packet2_t *packet2)
  140. {
  141. packet2_pad128(packet2, MAKE_VIF_CODE(0, 0, P2_VIF_NOP, 0));
  142. }
  143. /**
  144. * Add MPG VIF opcode.
  145. * @note Alignment alert. Size of word (1/4).
  146. * For more details, check description of VIFOpcode.
  147. * @param packet2 Pointer to packet.
  148. * @param num Number of 64-bit data.
  149. * @param addr Address.
  150. * @param irq Interrupt Request. False by default.
  151. */
  152. static inline void packet2_vif_mpg(packet2_t *packet2, u32 num, u32 addr, u8 irq)
  153. {
  154. packet2_add_u32(packet2, MAKE_VIF_CODE(addr, num, P2_VIF_MPG, irq));
  155. }
  156. /**
  157. * Add STCYCL VIF opcode.
  158. * @note Alignment alert. Size of word (1/4).
  159. * For more details, check description of VIFOpcode.
  160. * @param packet2 Pointer to packet.
  161. * @param wl WL field.
  162. * @param cl CL field.
  163. * @param irq Interrupt Request. False by default.
  164. */
  165. static inline void packet2_vif_stcycl(packet2_t *packet2, u32 wl, u32 cl, u8 irq)
  166. {
  167. packet2_add_u32(packet2, MAKE_VIF_CODE(cl | (wl << 8), 0, P2_VIF_STCYCL, irq));
  168. }
  169. /**
  170. * Add OFFSET VIF opcode.
  171. * @note Alignment alert. Size of word (1/4).
  172. * For more details, check description of VIFOpcode.
  173. * @param packet2 Pointer to packet.
  174. * @param offset Offset. BASE+OFFSET will be the address of second buffer.
  175. * @param irq Interrupt Request. False by default.
  176. */
  177. static inline void packet2_vif_offset(packet2_t *packet2, u32 offset, u8 irq)
  178. {
  179. packet2_add_u32(packet2, MAKE_VIF_CODE(offset, 0, P2_VIF_OFFSET, irq));
  180. }
  181. /**
  182. * Add BASE VIF opcode.
  183. * @note Alignment alert. Size of word (1/4).
  184. * For more details, check description of VIFOpcode.
  185. * @param packet2 Pointer to packet.
  186. * @param base Base address of double buffer.
  187. * @param irq Interrupt Request. False by default.
  188. */
  189. static inline void packet2_vif_base(packet2_t *packet2, u32 base, u8 irq)
  190. {
  191. packet2_add_u32(packet2, MAKE_VIF_CODE(base, 0, P2_VIF_BASE, irq));
  192. }
  193. /**
  194. * Add FLUSH VIF opcode.
  195. * @note Alignment alert. Size of word (1/4).
  196. * For more details, check description of VIFOpcode.
  197. * @param packet2 Pointer to packet.
  198. * @param irq Interrupt Request. False by default.
  199. */
  200. static inline void packet2_vif_flush(packet2_t *packet2, u8 irq)
  201. {
  202. packet2_add_u32(packet2, MAKE_VIF_CODE(0, 0, P2_VIF_FLUSH, irq));
  203. }
  204. /**
  205. * Add MSCAL VIF opcode.
  206. * @note Alignment alert. Size of word (1/4).
  207. * For more details, check description of VIFOpcode.
  208. * @param packet2 Pointer to packet.
  209. * @param addr Starting address.
  210. * @param irq Interrupt Request. False by default.
  211. */
  212. static inline void packet2_vif_mscal(packet2_t *packet2, u32 addr, u8 irq)
  213. {
  214. packet2_add_u32(packet2, MAKE_VIF_CODE(addr, 0, P2_VIF_MSCAL, irq));
  215. }
  216. /**
  217. * Add MSCNT VIF opcode.
  218. * @note Alignment alert. Size of word (1/4).
  219. * For more details, check description of VIFOpcode.
  220. * @param packet2 Pointer to packet.
  221. * @param irq Interrupt Request. False by default.
  222. */
  223. static inline void packet2_vif_mscnt(packet2_t *packet2, u8 irq)
  224. {
  225. packet2_add_u32(packet2, MAKE_VIF_CODE(0, 0, P2_VIF_MSCNT, irq));
  226. }
  227. /**
  228. * Add ITOP VIF opcode.
  229. * @note Alignment alert. Size of word (1/4).
  230. * For more details, check description of VIFOpcode.
  231. * @param packet2 Pointer to packet.
  232. * @param itops Value for VU XITOP instruction.
  233. * @param irq Interrupt Request. False by default.
  234. */
  235. static inline void packet2_vif_itop(packet2_t *packet2, u32 itops, u8 irq)
  236. {
  237. packet2_add_u32(packet2, MAKE_VIF_CODE(itops, 0, P2_VIF_ITOP, irq));
  238. }
  239. /**
  240. * Add MSKPATH3 VIF opcode.
  241. * @note Alignment alert. Size of word (1/4).
  242. * For more details, check description of VIFOpcode.
  243. * @param packet2 Pointer to packet.
  244. * @param mode Decompression mode.
  245. * @param irq Interrupt Request. False by default.
  246. */
  247. static inline void packet2_vif_stmod(packet2_t *packet2, u32 mode, u8 irq)
  248. {
  249. packet2_add_u32(packet2, MAKE_VIF_CODE(mode, 0, P2_VIF_STMOD, irq));
  250. }
  251. /**
  252. * Add MSKPATH3 VIF opcode.
  253. * @note Alignment alert. Size of word (1/4).
  254. * For more details, check description of VIFOpcode.
  255. * @param packet2 Pointer to packet.
  256. * @param mask Mask.
  257. * @param irq Interrupt Request. False by default.
  258. */
  259. static inline void packet2_vif_mskpath3(packet2_t *packet2, u32 mask, u8 irq)
  260. {
  261. packet2_add_u32(packet2, MAKE_VIF_CODE(mask, 0, P2_VIF_MSKPATH3, irq));
  262. }
  263. /**
  264. * Add MARK VIF opcode.
  265. * @note Alignment alert. Size of word (1/4).
  266. * For more details, check description of VIFOpcode.
  267. * @param packet2 Pointer to packet.
  268. * @param value Value.
  269. * @param irq Interrupt Request. False by default.
  270. */
  271. static inline void packet2_vif_mark(packet2_t *packet2, u32 value, u8 irq)
  272. {
  273. packet2_add_u32(packet2, MAKE_VIF_CODE(value, 0, P2_VIF_MARK, irq));
  274. }
  275. /**
  276. * Add FLUSHE VIF opcode.
  277. * @note Alignment alert. Size of word (1/4).
  278. * For more details, check description of VIFOpcode.
  279. * @param packet2 Pointer to packet.
  280. * @param irq Interrupt Request. False by default.
  281. */
  282. static inline void packet2_vif_flushe(packet2_t *packet2, u8 irq)
  283. {
  284. packet2_add_u32(packet2, MAKE_VIF_CODE(0, 0, P2_VIF_FLUSHE, irq));
  285. }
  286. /**
  287. * Add FLUSHA VIF opcode.
  288. * @note Alignment alert. Size of word (1/4).
  289. * For more details, check description of VIFOpcode.
  290. * @param packet2 Pointer to packet.
  291. * @param irq Interrupt Request. False by default.
  292. */
  293. static inline void packet2_vif_flusha(packet2_t *packet2, u8 irq)
  294. {
  295. packet2_add_u32(packet2, MAKE_VIF_CODE(0, 0, P2_VIF_FLUSHA, irq));
  296. }
  297. /**
  298. * Add MSCALF VIF opcode.
  299. * @note Alignment alert. Size of word (1/4).
  300. * For more details, check description of VIFOpcode.
  301. * @param packet2 Pointer to packet.
  302. * @param addr Address.
  303. * @param irq Interrupt Request. False by default.
  304. */
  305. static inline void packet2_vif_mscalf(packet2_t *packet2, u32 addr, u8 irq)
  306. {
  307. packet2_add_u32(packet2, MAKE_VIF_CODE(addr, 0, P2_VIF_MSCALF, irq));
  308. }
  309. /**
  310. * Add STMASK VIF opcode.
  311. * @note Alignment alert. Size of dword (1/2).
  312. * For more details, check description of VIFOpcode.
  313. * @param packet2 Pointer to packet.
  314. * @param mask Mask.
  315. * @param irq Interrupt Request. False by default.
  316. */
  317. static inline void packet2_vif_stmask(packet2_t *packet2, Mask mask, u8 irq)
  318. {
  319. packet2_add_u32(packet2, MAKE_VIF_CODE(0, 0, P2_VIF_STMASK, irq));
  320. packet2_add_u32(packet2, mask.m);
  321. }
  322. /**
  323. * Add STROW VIF opcode.
  324. * @note Alignment alert. Size of 5 x word (1+1/4).
  325. * For more details, check description of VIFOpcode.
  326. * @param packet2 Pointer to packet.
  327. * @param row_arr Row array.
  328. * @param irq Interrupt Request. False by default.
  329. */
  330. static inline void packet2_vif_strow(packet2_t *packet2, const u32 *row_arr, u8 irq)
  331. {
  332. packet2_add_u32(packet2, MAKE_VIF_CODE(0, 0, P2_VIF_STROW, irq));
  333. packet2_add_u32(packet2, row_arr[0]);
  334. packet2_add_u32(packet2, row_arr[1]);
  335. packet2_add_u32(packet2, row_arr[2]);
  336. packet2_add_u32(packet2, row_arr[3]);
  337. }
  338. /**
  339. * Add STCOL VIF opcode.
  340. * @note Alignment alert. Size of 5 x word (1+1/4).
  341. * For more details, check description of VIFOpcode.
  342. * @param packet2 Pointer to packet.
  343. * @param col_arr Column array.
  344. * @param irq Interrupt Request. False by default.
  345. */
  346. static inline void packet2_vif_stcol(packet2_t *packet2, const u32 *col_arr, u8 irq)
  347. {
  348. packet2_add_u32(packet2, MAKE_VIF_CODE(0, 0, P2_VIF_STCOL, irq));
  349. packet2_add_u32(packet2, col_arr[0]);
  350. packet2_add_u32(packet2, col_arr[1]);
  351. packet2_add_u32(packet2, col_arr[2]);
  352. packet2_add_u32(packet2, col_arr[3]);
  353. }
  354. /**
  355. * Add VU micro program into packet2.
  356. * Packet2 MODE for micro program upload: Chain
  357. * @param dest VU destination address (divided by 16).
  358. * @param start Start address.
  359. * @param end End address.
  360. */
  361. void packet2_vif_add_micro_program(packet2_t *packet2, u32 dest, u32 *start, u32 *end);
  362. #ifdef __cplusplus
  363. }
  364. #endif
  365. #endif /* __PACKET2_VIF_H__ */
  366. /** @} */ // end of packet2_vif subgroup