gba_functions.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef GBA_FUNCTIONS_H
  2. #define GBA_FUNCTIONS_H
  3. #include <string.h>
  4. #include "doomtype.h"
  5. #include "m_fixed.h"
  6. //#define __arm__
  7. #ifdef __arm__
  8. #include <gba_systemcalls.h>
  9. #include <gba_dma.h>
  10. #endif
  11. //***********************************************************************
  12. //The following math functions were taken from the Jaguar Port of Doom
  13. //here: https://github.com/Arc0re/jaguardoom/blob/master/jagonly.c
  14. //
  15. //There may be a licence incompatibility with the iD release
  16. //and the GPL that prBoom (and this as derived work) is under.
  17. //***********************************************************************
  18. static CONSTFUNC unsigned UDiv32 (unsigned aa, unsigned bb)
  19. {
  20. #ifdef __arm__
  21. unsigned int udiv32_arm(unsigned int a, unsigned int b);
  22. return udiv32_arm(aa, bb);
  23. #else
  24. if(bb == 0)
  25. return UINT_MAX;
  26. return aa / bb;
  27. #endif
  28. }
  29. inline static CONSTFUNC int IDiv32 (int a, int b)
  30. {
  31. //use bios divide on gba.
  32. #ifdef __arm__
  33. return Div(a, b);
  34. #else
  35. return a / b;
  36. #endif
  37. }
  38. inline static void BlockCopy(void* dest, const void* src, const unsigned int len)
  39. {
  40. #ifdef __arm__
  41. const int words = len >> 2;
  42. DMA3COPY(src, dest, DMA_DST_INC | DMA_SRC_INC | DMA32 | DMA_IMMEDIATE | words)
  43. #else
  44. memcpy(dest, src, len & 0xfffffffc);
  45. #endif
  46. }
  47. inline static void CpuBlockCopy(void* dest, const void* src, const unsigned int len)
  48. {
  49. #ifdef __arm__
  50. const unsigned int words = len >> 2;
  51. CpuFastSet(src, dest, words);
  52. #else
  53. BlockCopy(dest, src, len);
  54. #endif
  55. }
  56. inline static void BlockSet(void* dest, volatile unsigned int val, const unsigned int len)
  57. {
  58. #ifdef __arm__
  59. const int words = len >> 2;
  60. DMA3COPY(&val, dest, DMA_SRC_FIXED | DMA_DST_INC | DMA32 | DMA_IMMEDIATE | words)
  61. #else
  62. memset(dest, val, len & 0xfffffffc);
  63. #endif
  64. }
  65. inline static void ByteCopy(byte* dest, const byte* src, unsigned int count)
  66. {
  67. do
  68. {
  69. *dest++ = *src++;
  70. } while(--count);
  71. }
  72. inline static void ByteSet(byte* dest, byte val, unsigned int count)
  73. {
  74. do
  75. {
  76. *dest++ = val;
  77. } while(--count);
  78. }
  79. inline static void* ByteFind(byte* mem, byte val, unsigned int count)
  80. {
  81. do
  82. {
  83. if(*mem == val)
  84. return mem;
  85. mem++;
  86. } while(--count);
  87. return NULL;
  88. }
  89. inline static void SaveSRAM(const byte* eeprom, unsigned int size, unsigned int offset)
  90. {
  91. #ifdef __arm__
  92. ByteCopy(0xE000000 + offset, eeprom, size);
  93. #endif
  94. }
  95. inline static void LoadSRAM(byte* eeprom, unsigned int size, unsigned int offset)
  96. {
  97. #ifdef __arm__
  98. ByteCopy(eeprom, 0xE000000 + offset, size);
  99. #endif
  100. }
  101. //Cheap mul by 120. Not sure if faster.
  102. #define ScreenYToOffset(x) ((x << 7) - (x << 3))
  103. #endif // GBA_FUNCTIONS_H