function_wrappers.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include "common/common_types.h"
  6. #include "core/mem_map.h"
  7. #include "core/hle/hle.h"
  8. namespace HLE {
  9. #define PARAM(n) Core::g_app_core->GetReg(n)
  10. /**
  11. * HLE a function return from the current ARM11 userland process
  12. * @param res Result to return
  13. */
  14. static inline void FuncReturn(u32 res) {
  15. Core::g_app_core->SetReg(0, res);
  16. }
  17. /**
  18. * HLE a function return (64-bit) from the current ARM11 userland process
  19. * @param res Result to return (64-bit)
  20. * @todo Verify that this function is correct
  21. */
  22. static inline void FuncReturn64(u64 res) {
  23. Core::g_app_core->SetReg(0, (u32)(res & 0xFFFFFFFF));
  24. Core::g_app_core->SetReg(1, (u32)((res >> 32) & 0xFFFFFFFF));
  25. }
  26. ////////////////////////////////////////////////////////////////////////////////////////////////////
  27. // Function wrappers that return type s32
  28. template<s32 func(u32, u32, u32, u32)> void Wrap() {
  29. FuncReturn(func(PARAM(0), PARAM(1), PARAM(2), PARAM(3)));
  30. }
  31. template<s32 func(u32, u32, u32, u32, u32)> void Wrap() {
  32. FuncReturn(func(PARAM(0), PARAM(1), PARAM(2), PARAM(3), PARAM(4)));
  33. }
  34. template<s32 func(u32*, u32, u32, u32, u32, u32)> void Wrap(){
  35. u32 param_1 = 0;
  36. u32 retval = func(&param_1, PARAM(0), PARAM(1), PARAM(2), PARAM(3), PARAM(4));
  37. Core::g_app_core->SetReg(1, param_1);
  38. FuncReturn(retval);
  39. }
  40. template<s32 func(s32*, u32*, s32, bool, s64)> void Wrap() {
  41. s32 param_1 = 0;
  42. s32 retval = func(&param_1, (Handle*)Memory::GetPointer(PARAM(1)), (s32)PARAM(2),
  43. (PARAM(3) != 0), (((s64)PARAM(4) << 32) | PARAM(0)));
  44. Core::g_app_core->SetReg(1, (u32)param_1);
  45. FuncReturn(retval);
  46. }
  47. // TODO(bunnei): Is this correct? Probably not - Last parameter looks wrong for ArbitrateAddress
  48. template<s32 func(u32, u32, u32, u32, s64)> void Wrap() {
  49. FuncReturn(func(PARAM(0), PARAM(1), PARAM(2), PARAM(3), (((s64)PARAM(5) << 32) | PARAM(4))));
  50. }
  51. template<s32 func(u32*)> void Wrap(){
  52. u32 param_1 = 0;
  53. u32 retval = func(&param_1);
  54. Core::g_app_core->SetReg(1, param_1);
  55. FuncReturn(retval);
  56. }
  57. template<s32 func(u32, s64)> void Wrap() {
  58. FuncReturn(func(PARAM(0), (((s64)PARAM(3) << 32) | PARAM(2))));
  59. }
  60. template<s32 func(void*, void*, u32)> void Wrap(){
  61. FuncReturn(func(Memory::GetPointer(PARAM(0)), Memory::GetPointer(PARAM(1)), PARAM(2)));
  62. }
  63. template<s32 func(s32*, u32)> void Wrap(){
  64. s32 param_1 = 0;
  65. u32 retval = func(&param_1, PARAM(1));
  66. Core::g_app_core->SetReg(1, param_1);
  67. FuncReturn(retval);
  68. }
  69. template<s32 func(u32, s32)> void Wrap() {
  70. FuncReturn(func(PARAM(0), (s32)PARAM(1)));
  71. }
  72. template<s32 func(u32*, u32)> void Wrap(){
  73. u32 param_1 = 0;
  74. u32 retval = func(&param_1, PARAM(1));
  75. Core::g_app_core->SetReg(1, param_1);
  76. FuncReturn(retval);
  77. }
  78. template<s32 func(u32)> void Wrap() {
  79. FuncReturn(func(PARAM(0)));
  80. }
  81. template<s32 func(void*)> void Wrap() {
  82. FuncReturn(func(Memory::GetPointer(PARAM(0))));
  83. }
  84. template<s32 func(s64*, u32, void*, s32)> void Wrap(){
  85. FuncReturn(func((s64*)Memory::GetPointer(PARAM(0)), PARAM(1), Memory::GetPointer(PARAM(2)),
  86. (s32)PARAM(3)));
  87. }
  88. template<s32 func(u32*, const char*)> void Wrap() {
  89. u32 param_1 = 0;
  90. u32 retval = func(&param_1, Memory::GetCharPointer(PARAM(1)));
  91. Core::g_app_core->SetReg(1, param_1);
  92. FuncReturn(retval);
  93. }
  94. template<s32 func(u32*, s32, s32)> void Wrap() {
  95. u32 param_1 = 0;
  96. u32 retval = func(&param_1, PARAM(1), PARAM(2));
  97. Core::g_app_core->SetReg(1, param_1);
  98. FuncReturn(retval);
  99. }
  100. ////////////////////////////////////////////////////////////////////////////////////////////////////
  101. // Function wrappers that return type u32
  102. template<u32 func()> void Wrap() {
  103. FuncReturn(func());
  104. }
  105. ////////////////////////////////////////////////////////////////////////////////////////////////////
  106. // Function wrappers that return type s64
  107. template<s64 func()> void Wrap() {
  108. FuncReturn64(func());
  109. }
  110. ////////////////////////////////////////////////////////////////////////////////////////////////////
  111. /// Function wrappers that return type void
  112. template<void func(s64)> void Wrap() {
  113. func(((s64)PARAM(1) << 32) | PARAM(0));
  114. }
  115. template<void func(const char*)> void Wrap() {
  116. func(Memory::GetCharPointer(PARAM(0)));
  117. }
  118. #undef PARAM
  119. #undef FuncReturn
  120. } // namespace HLE