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.

75 lines
3.1 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 "packet2_vif.h"
  11. #include "packet2_chain.h"
  12. void packet2_vif_add_micro_program(packet2_t *packet2, u32 dest, u32 *start, u32 *end)
  13. {
  14. // get the size of the code as we can only send 256 instructions in each MPGtag
  15. u32 count = (end - start) / 2;
  16. if (count & 1)
  17. count++;
  18. u32 *l_start = start;
  19. while (count > 0)
  20. {
  21. u16 curr_count = count > 256 ? 256 : count;
  22. packet2_chain_ref(packet2, l_start, curr_count / 2, 0, 0, 0);
  23. packet2_vif_nop(packet2, 0);
  24. packet2_vif_mpg(packet2, curr_count & 0xFF, dest, 0);
  25. l_start += curr_count * 2;
  26. count -= curr_count;
  27. dest += curr_count;
  28. }
  29. }
  30. u32 packet2_vif_close_unpack_auto(packet2_t *packet2, u32 wl, u32 cl)
  31. {
  32. assert(packet2->vif_code_opened_at != NULL); // There is open UNPACK/DIRECT.
  33. assert(((u32)packet2->next & 0x3) == 0); // Make sure we're u32 aligned
  34. assert((packet2->vif_code_opened_at->cmd & 0x60) == 0x60); // It was UNPACK
  35. u32 vn = (packet2->vif_code_opened_at->cmd & 0xC) >> 2;
  36. u32 vl = packet2->vif_code_opened_at->cmd & 0x3;
  37. // "the goal here is to find the num field of the open unpack, which is the number of
  38. // qwords ACTUALLY WRITTEN to vu memory (it does not count quads that are skipped in
  39. // "skipping write" mode.) But first forget about skipping/filling writes and compute the number
  40. // of qwords that the data in this packet will expand to."
  41. u32 bytes_count = (u32)packet2->next - (u32)packet2->vif_code_opened_at - 4;
  42. u32 block_bytes_count = 4 >> vl;
  43. u32 quad_blocks_count = vn + 1;
  44. // "make sure that the data length is a multiple of 8, 16, or 32 bits, whichever is appropriate"
  45. assert((bytes_count & (block_bytes_count - 1)) == 0);
  46. u32 quads_count = (bytes_count / block_bytes_count) / quad_blocks_count;
  47. // "We have the number of quads our data will directly expand to, so now we need to account for
  48. // skipping/filling write modes.""
  49. // "skipping write is easy -- we are already done"
  50. // "filling write: now we get ambiguous -- what to do when quads_count == CL in the last
  51. // block? I will say that in this case the vif should still do the full wl length block, filling
  52. // with internal registers. If you want different behavior call packet2_vif_close_unpack_manual
  53. // with a num field you've computed yourself"
  54. if (cl < wl)
  55. {
  56. u32 wl_blocks_count = (quads_count / cl);
  57. u32 last_block_quads = quads_count - wl_blocks_count * cl;
  58. if (last_block_quads == cl)
  59. last_block_quads = wl;
  60. quads_count = wl_blocks_count * wl + last_block_quads;
  61. }
  62. packet2_vif_close_unpack_manual(packet2, quads_count);
  63. return quads_count;
  64. }