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.

130 lines
3.9 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. #include <malloc.h>
  11. #include <kernel.h>
  12. #include <assert.h>
  13. #include <packet2.h>
  14. #include <string.h>
  15. #include <stdio.h>
  16. #define P2_ALIGNMENT 64
  17. #define P2_MAKE_PTR_NORMAL(PTR) ((u32)(PTR)&0x0FFFFFFF)
  18. // ---
  19. // Packet2 management
  20. // ---
  21. packet2_t *packet2_create(u16 qwords, enum Packet2Type type, enum Packet2Mode mode, u8 tte)
  22. {
  23. packet2_t *packet2 = (packet2_t *)calloc(1, sizeof(packet2_t));
  24. if (packet2 == NULL)
  25. return NULL;
  26. packet2->max_qwords_count = qwords;
  27. packet2->type = type;
  28. packet2->mode = mode;
  29. packet2->tte = tte;
  30. packet2->tag_opened_at = NULL;
  31. packet2->vif_code_opened_at = NULL;
  32. // Dma buffer size should be a whole number of cache lines (64 bytes = 4 quads)
  33. assert(!((packet2->type == P2_TYPE_UNCACHED || packet2->type == P2_TYPE_UNCACHED_ACCL) && packet2->max_qwords_count & (4 - 1)));
  34. u32 byte_size = packet2->max_qwords_count << 4;
  35. if ((packet2->base = memalign(P2_ALIGNMENT, byte_size)) == NULL)
  36. {
  37. free(packet2);
  38. return NULL;
  39. }
  40. packet2->base = packet2->next = (qword_t *)((u32)packet2->base | packet2->type);
  41. memset(packet2->base, 0, byte_size);
  42. // "I hate to do this, but I've wasted FAR too much time hunting down cache incoherency"
  43. if (packet2->type == P2_TYPE_UNCACHED || packet2->type == P2_TYPE_UNCACHED_ACCL)
  44. FlushCache(0);
  45. return packet2;
  46. }
  47. packet2_t *packet2_create_from(qword_t *base, qword_t *next, u16 qwords, enum Packet2Type type, enum Packet2Mode mode, u8 tte)
  48. {
  49. // Dma buffer size should be a whole number of cache lines (64 bytes = 4 quads)
  50. assert(!((type == P2_TYPE_UNCACHED || type == P2_TYPE_UNCACHED_ACCL) && qwords & (4 - 1)));
  51. // Dma buffer should be aligned on a cache line (64-byte boundary)
  52. assert(!((type == P2_TYPE_UNCACHED || type == P2_TYPE_UNCACHED_ACCL) && (u32)base & (64 - 1)));
  53. packet2_t *packet2 = (packet2_t *)calloc(1, sizeof(packet2_t));
  54. if (packet2 == NULL)
  55. return NULL;
  56. packet2->base = base;
  57. packet2->next = next;
  58. packet2->max_qwords_count = qwords;
  59. packet2->type = type;
  60. packet2->tte = tte;
  61. packet2->mode = mode;
  62. return packet2;
  63. }
  64. void packet2_free(packet2_t *packet2)
  65. {
  66. if (packet2->base != NULL)
  67. free((qword_t *)P2_MAKE_PTR_NORMAL(packet2->base));
  68. free(packet2);
  69. }
  70. void packet2_reset(packet2_t *packet2, u8 clear_mem)
  71. {
  72. packet2->next = packet2->base;
  73. packet2->vif_code_opened_at = NULL;
  74. packet2->tag_opened_at = NULL;
  75. if (clear_mem)
  76. memset(packet2->base, 0, packet2->max_qwords_count << 4);
  77. }
  78. // ---
  79. // Utils
  80. // ---
  81. void packet2_add(packet2_t *a, packet2_t *b)
  82. {
  83. assert(packet2_get_qw_count(a) + packet2_get_qw_count(b) <= a->max_qwords_count);
  84. memcpy(a->next, b->base, (u32)b->next - (u32)b->base);
  85. a->next = a->base + packet2_get_qw_count(b) + 1;
  86. }
  87. void packet2_print(packet2_t *packet2, u32 qw_count)
  88. {
  89. if (qw_count == 0)
  90. qw_count = ((u32)packet2->next - (u32)packet2->base) >> 4;
  91. printf("\n============================\n");
  92. printf("Packet2: Dumping %d words...\n", ((u32)packet2->next - (u32)packet2->base) >> 2);
  93. u32 i = 0;
  94. u32 *nextWord;
  95. for (nextWord = (u32 *)packet2->base; nextWord != (u32 *)packet2->next; nextWord++, i++)
  96. {
  97. if ((i % 4) == 0)
  98. printf("\n0x%08x: ", (u32)nextWord);
  99. printf("0x%08x ", *nextWord);
  100. if (i / 4 == qw_count)
  101. break;
  102. }
  103. printf("\n============================\n");
  104. }
  105. void packet2_print_qw_count(packet2_t *packet2)
  106. {
  107. printf("Packet2: Qwords count:%d \n", packet2_get_qw_count(packet2));
  108. }