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.

209 lines
8.3 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "dma_packets.h"
  2. #define SPR_BEGIN 0x70000000
  3. #define P_ALIGNMENT 64
  4. #define P2_ALIGNMENT 64
  5. #define P2_MAKE_PTR_NORMAL(PTR) ((uint32_t)(PTR)&0x0FFFFFFF)
  6. packet_t *packet_init( uint32_t qwords, uint16_t type){
  7. uint32_t byte_size = 0;
  8. packet_t *packet = (packet_t*)malloc(sizeof(packet_t));
  9. if( packet == NULL ) return NULL;
  10. if( type == PACKET_SPR ){
  11. packet->data = (qword_t *) SPR_BEGIN;
  12. packet->qwc = 0x1000;
  13. } else {
  14. // Size of qwords in bytes.
  15. byte_size = qwords << 4;
  16. // Allocate the data area in bytes aligned to cache line.
  17. if(( packet->data = (qword_t *) memalign( P_ALIGNMENT, byte_size)) == NULL ){
  18. free( packet );
  19. return NULL;
  20. };
  21. };
  22. // Set the pointer attribute to ucab space.
  23. if( type == PACKET_UCAB ){
  24. packet->data = (qword_t *)((uint32_t)packet->data | 0x30000000);
  25. };
  26. // Clear the packet area.
  27. memset( packet->data, 0, byte_size );
  28. // Set the packet counts
  29. packet->qwc = 0;
  30. packet->qwords = qwords;
  31. packet->type = type;
  32. // End function.
  33. return packet;
  34. };
  35. void packet_free( packet_t *packet ){
  36. if( packet == NULL ) return;
  37. // Free the allocated data buffer.
  38. if( packet->type == PACKET_SPR ){
  39. packet->data = NULL;
  40. } else {
  41. if( packet->type == PACKET_UCAB ){
  42. packet->data = (qword_t *)((uint32_t)packet->data ^ 0x30000000);
  43. };
  44. free(packet->data);
  45. };
  46. free(packet);
  47. };
  48. void packet_reset( packet_t *packet ){
  49. if( packet == NULL ) return;
  50. // Reset the quadword counter.
  51. packet->qwc = 0;
  52. if( packet->type == PACKET_SPR ){
  53. packet->data = (qword_t *)SPR_BEGIN;
  54. return;
  55. };
  56. // Zero out the data
  57. memset(packet->data, 0, packet->qwords << 4);
  58. };
  59. // ---
  60. // Packet2 management
  61. // ---
  62. packet2_t* packet2_create(uint16_t qwords, packet2_type_t type, packet2_mode_t mode, uint8_t tte ){
  63. packet2_t *packet2 = (packet2_t *)malloc(sizeof(packet2_t));
  64. if( packet2 == NULL ) return NULL;
  65. memset( packet2, 0 , sizeof( packet2_t ));
  66. packet2->max_qwords_count = qwords;
  67. packet2->type = type;
  68. packet2->mode = mode;
  69. packet2->tte = tte;
  70. packet2->tag_opened_at = NULL;
  71. packet2->vif_code_opened_at = NULL;
  72. // Dma buffer size should be a whole number of cache lines (64 bytes = 4 quads)
  73. assert(!((packet2->type == P2_TYPE_UNCACHED || packet2->type == P2_TYPE_UNCACHED_ACCL) && packet2->max_qwords_count & (4 - 1)));
  74. uint32_t byte_size = packet2->max_qwords_count << 4;
  75. if((packet2->base = (qword_t *) memalign(P2_ALIGNMENT, byte_size)) == NULL){
  76. free(packet2);
  77. return NULL;
  78. };
  79. packet2->base = packet2->next = (qword_t *)((uint32_t)packet2->base | packet2->type);
  80. memset(packet2->base, 0, byte_size);
  81. // "I hate to do this, but I've wasted FAR too much time hunting down cache incoherency"
  82. if (packet2->type == P2_TYPE_UNCACHED || packet2->type == P2_TYPE_UNCACHED_ACCL) FlushCache(0);
  83. return packet2;
  84. };
  85. packet2_t* packet2_create_from( qword_t *base, qword_t *next, uint16_t qwords, packet2_type_t type, packet2_mode_t mode, uint8_t tte ){
  86. // Dma buffer size should be a whole number of cache lines (64 bytes = 4 quads)
  87. assert(!((type == P2_TYPE_UNCACHED || type == P2_TYPE_UNCACHED_ACCL) && qwords & (4 - 1)));
  88. // Dma buffer should be aligned on a cache line (64-byte boundary)
  89. assert(!((type == P2_TYPE_UNCACHED || type == P2_TYPE_UNCACHED_ACCL) && (uint32_t)base & (64 - 1)));
  90. packet2_t *packet2 = (packet2_t *) malloc(sizeof(packet2_t));
  91. if (packet2 == NULL) return NULL;
  92. memset( packet2, 0 , sizeof( packet2_t ));
  93. packet2->base = base;
  94. packet2->next = next;
  95. packet2->max_qwords_count = qwords;
  96. packet2->type = type;
  97. packet2->tte = tte;
  98. packet2->mode = mode;
  99. return packet2;
  100. };
  101. void packet2_free( packet2_t *packet2 ){
  102. if( packet2 == NULL ) return;
  103. if( packet2->base != NULL ) free((qword_t *)P2_MAKE_PTR_NORMAL(packet2->base));
  104. free( packet2 );
  105. };
  106. void packet2_reset( packet2_t *packet2, uint8_t clear_mem ){
  107. if( packet2 == NULL ) return;
  108. packet2->next = packet2->base;
  109. packet2->vif_code_opened_at = NULL;
  110. packet2->tag_opened_at = NULL;
  111. if( clear_mem ) memset( packet2->base, 0, packet2->max_qwords_count << 4 );
  112. };
  113. // ---
  114. // Utils
  115. // ---
  116. void packet2_add( packet2_t *a, packet2_t *b ){
  117. if( a== NULL || b == NULL ) return;
  118. assert(packet2_get_qw_count(a) + packet2_get_qw_count(b) <= a->max_qwords_count);
  119. memcpy(a->next, b->base, (uint32_t)b->next - (uint32_t)b->base);
  120. a->next = a->base + packet2_get_qw_count(b) + 1;
  121. };
  122. void packet2_print( packet2_t *packet2, uint32_t qw_count){
  123. if( packet2 == NULL ) return;
  124. if( qw_count == 0 ) qw_count = ((uint32_t)packet2->next - (uint32_t)packet2->base) >> 4;
  125. printf("\n============================\n");
  126. printf("Packet2: Dumping %d words...\n", ((uint32_t)packet2->next - (uint32_t)packet2->base) >> 2);
  127. uint32_t i = 0;
  128. uint32_t * nextWord;
  129. for( nextWord = (uint32_t *)packet2->base; nextWord != (uint32_t *)packet2->next; nextWord++, i++ ){
  130. if ((i % 4) == 0) printf("\n0x%08x: ", (uint32_t)nextWord );
  131. printf( "0x%08x ", *nextWord );
  132. if (i / 4 == qw_count) break;
  133. }
  134. printf("\n============================\n");
  135. };
  136. void packet2_print_qw_count( packet2_t *packet2 ){
  137. if ( packet2 == NULL ) return;
  138. printf("Packet2: Qwords count:%d \n", packet2_get_qw_count(packet2));
  139. };
  140. void packet2_vif_add_micro_program( packet2_t *packet2, uint32_t dest, puint32_t start, puint32_t end ){
  141. // get the size of the code as we can only send 256 instructions in each MPGtag
  142. uint32_t count = (end - start) >> 1;
  143. if( count & 1 ) count++;
  144. puint32_t l_start = start;
  145. while( count > 0 ){
  146. uint16_t curr_count = ( count > 256 )? 256 : count;
  147. packet2_chain_ref( packet2, l_start, ( curr_count >> 1 ), 0, 0, 0);
  148. packet2_vif_nop( packet2, 0 );
  149. packet2_vif_mpg( packet2, curr_count & 0xFF, dest, 0 );
  150. l_start += ( curr_count << 1 );
  151. count -= curr_count;
  152. dest += curr_count;
  153. };
  154. };
  155. uint32_t packet2_vif_close_unpack_auto( packet2_t *packet2, uint32_t wl, uint32_t cl ){
  156. if( packet2 == NULL ) return 0;
  157. assert(packet2->vif_code_opened_at != NULL); // There is open UNPACK/DIRECT.
  158. assert(((uint32_t)packet2->next & 0x3) == 0); // Make sure we're u32 aligned
  159. assert((packet2->vif_code_opened_at->cmd & 0x60) == 0x60); // It was UNPACK
  160. uint32_t vn = (packet2->vif_code_opened_at->cmd & 0xC) >> 2;
  161. uint32_t vl = packet2->vif_code_opened_at->cmd & 0x3;
  162. // "the goal here is to find the num field of the open unpack, which is the number of
  163. // qwords ACTUALLY WRITTEN to vu memory (it does not count quads that are skipped in
  164. // "skipping write" mode.) But first forget about skipping/filling writes and compute the number
  165. // of qwords that the data in this packet will expand to."
  166. uint32_t bytes_count = (uint32_t)packet2->next - (uint32_t)packet2->vif_code_opened_at - 4;
  167. uint32_t block_bytes_count = 4 >> vl;
  168. uint32_t quad_blocks_count = vn + 1;
  169. // "make sure that the data length is a multiple of 8, 16, or 32 bits, whichever is appropriate"
  170. assert((bytes_count & (block_bytes_count - 1)) == 0);
  171. uint32_t quads_count = (bytes_count / block_bytes_count) / quad_blocks_count;
  172. // "We have the number of quads our data will directly expand to, so now we need to account for
  173. // skipping/filling write modes.""
  174. // "skipping write is easy -- we are already done"
  175. // "filling write: now we get ambiguous -- what to do when quads_count == CL in the last
  176. // block? I will say that in this case the vif should still do the full wl length block, filling
  177. // with internal registers. If you want different behavior call packet2_vif_close_unpack_manual
  178. // with a num field you've computed yourself"
  179. if( cl < wl ){
  180. uint32_t wl_blocks_count = (quads_count / cl);
  181. uint32_t last_block_quads = quads_count - wl_blocks_count * cl;
  182. if (last_block_quads == cl) last_block_quads = wl;
  183. quads_count = wl_blocks_count * wl + last_block_quads;
  184. };
  185. packet2_vif_close_unpack_manual( packet2, quads_count );
  186. return quads_count;
  187. };