dsp_dsp.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/log.h"
  5. #include "core/hle/hle.h"
  6. #include "core/hle/kernel/event.h"
  7. #include "core/hle/service/dsp_dsp.h"
  8. ////////////////////////////////////////////////////////////////////////////////////////////////////
  9. // Namespace DSP_DSP
  10. namespace DSP_DSP {
  11. static u32 read_pipe_count = 0;
  12. static Kernel::SharedPtr<Kernel::Event> semaphore_event;
  13. static Kernel::SharedPtr<Kernel::Event> interrupt_event;
  14. void SignalInterrupt() {
  15. // TODO(bunnei): This is just a stub, it does not do anything other than signal to the emulated
  16. // application that a DSP interrupt occurred, without specifying which one. Since we do not
  17. // emulate the DSP yet (and how it works is largely unknown), this is a work around to get games
  18. // that check the DSP interrupt signal event to run. We should figure out the different types of
  19. // DSP interrupts, and trigger them at the appropriate times.
  20. if (interrupt_event != 0)
  21. interrupt_event->Signal();
  22. }
  23. /**
  24. * DSP_DSP::ConvertProcessAddressFromDspDram service function
  25. * Inputs:
  26. * 1 : Address
  27. * Outputs:
  28. * 1 : Result of function, 0 on success, otherwise error code
  29. * 2 : (inaddr << 1) + 0x1FF40000 (where 0x1FF00000 is the DSP RAM address)
  30. */
  31. void ConvertProcessAddressFromDspDram(Service::Interface* self) {
  32. u32* cmd_buff = Kernel::GetCommandBuffer();
  33. u32 addr = cmd_buff[1];
  34. cmd_buff[1] = 0; // No error
  35. cmd_buff[2] = (addr << 1) + (Memory::DSP_MEMORY_VADDR + 0x40000);
  36. LOG_WARNING(Service_DSP, "(STUBBED) called with address %u", addr);
  37. }
  38. /**
  39. * DSP_DSP::LoadComponent service function
  40. * Inputs:
  41. * 1 : Size
  42. * 2 : Unknown (observed only half word used)
  43. * 3 : Unknown (observed only half word used)
  44. * 4 : (size << 4) | 0xA
  45. * 5 : Buffer address
  46. * Outputs:
  47. * 1 : Result of function, 0 on success, otherwise error code
  48. * 2 : Component loaded, 0 on not loaded, 1 on loaded
  49. */
  50. void LoadComponent(Service::Interface* self) {
  51. u32* cmd_buff = Kernel::GetCommandBuffer();
  52. cmd_buff[1] = 0; // No error
  53. cmd_buff[2] = 1; // Pretend that we actually loaded the DSP firmware
  54. // TODO(bunnei): Implement real DSP firmware loading
  55. LOG_WARNING(Service_DSP, "(STUBBED) called");
  56. }
  57. /**
  58. * DSP_DSP::GetSemaphoreEventHandle service function
  59. * Outputs:
  60. * 1 : Result of function, 0 on success, otherwise error code
  61. * 3 : Semaphore event handle
  62. */
  63. void GetSemaphoreEventHandle(Service::Interface* self) {
  64. u32* cmd_buff = Kernel::GetCommandBuffer();
  65. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  66. cmd_buff[3] = Kernel::g_handle_table.Create(semaphore_event).MoveFrom(); // Event handle
  67. LOG_WARNING(Service_DSP, "(STUBBED) called");
  68. }
  69. /**
  70. * DSP_DSP::RegisterInterruptEvents service function
  71. * Inputs:
  72. * 1 : Parameter 0 (purpose unknown)
  73. * 2 : Parameter 1 (purpose unknown)
  74. * 4 : Interrupt event handle
  75. * Outputs:
  76. * 1 : Result of function, 0 on success, otherwise error code
  77. */
  78. void RegisterInterruptEvents(Service::Interface* self) {
  79. u32* cmd_buff = Kernel::GetCommandBuffer();
  80. auto evt = Kernel::g_handle_table.Get<Kernel::Event>(cmd_buff[4]);
  81. if (evt != nullptr) {
  82. interrupt_event = evt;
  83. cmd_buff[1] = 0; // No error
  84. } else {
  85. LOG_ERROR(Service_DSP, "called with invalid handle=%08X", cmd_buff[4]);
  86. // TODO(yuriks): An error should be returned from SendSyncRequest, not in the cmdbuf
  87. cmd_buff[1] = -1;
  88. }
  89. LOG_WARNING(Service_DSP, "(STUBBED) called");
  90. }
  91. /**
  92. * DSP_DSP::WriteReg0x10 service function
  93. * Inputs:
  94. * 1 : Unknown (observed only half word used)
  95. * Outputs:
  96. * 1 : Result of function, 0 on success, otherwise error code
  97. */
  98. void WriteReg0x10(Service::Interface* self) {
  99. u32* cmd_buff = Kernel::GetCommandBuffer();
  100. SignalInterrupt();
  101. cmd_buff[1] = 0; // No error
  102. LOG_WARNING(Service_DSP, "(STUBBED) called");
  103. }
  104. /**
  105. * DSP_DSP::WriteProcessPipe service function
  106. * Inputs:
  107. * 1 : Number
  108. * 2 : Size
  109. * 3 : (size <<14) | 0x402
  110. * 4 : Buffer
  111. * Outputs:
  112. * 0 : Return header
  113. * 1 : Result of function, 0 on success, otherwise error code
  114. */
  115. void WriteProcessPipe(Service::Interface* self) {
  116. u32* cmd_buff = Kernel::GetCommandBuffer();
  117. u32 number = cmd_buff[1];
  118. u32 size = cmd_buff[2];
  119. u32 new_size = cmd_buff[3];
  120. u32 buffer = cmd_buff[4];
  121. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  122. LOG_WARNING(Service_DSP, "(STUBBED) called number=%u, size=0x%08X, new_size=0x%08X, buffer=0x%08X",
  123. number, size, new_size, buffer);
  124. }
  125. /**
  126. * DSP_DSP::ReadPipeIfPossible service function
  127. * Inputs:
  128. * 1 : Unknown
  129. * 2 : Unknown
  130. * 3 : Size in bytes of read (observed only lower half word used)
  131. * 0x41 : Virtual address to read from DSP pipe to in memory
  132. * Outputs:
  133. * 1 : Result of function, 0 on success, otherwise error code
  134. * 2 : Number of bytes read from pipe
  135. */
  136. void ReadPipeIfPossible(Service::Interface* self) {
  137. u32* cmd_buff = Kernel::GetCommandBuffer();
  138. u32 size = cmd_buff[3] & 0xFFFF;// Lower 16 bits are size
  139. VAddr addr = cmd_buff[0x41];
  140. // Canned DSP responses that games expect. These were taken from HW by 3dmoo team.
  141. // TODO: Remove this hack :)
  142. static const std::array<u16, 16> canned_read_pipe = {
  143. 0x000F, 0xBFFF, 0x9E8E, 0x8680, 0xA78E, 0x9430, 0x8400, 0x8540,
  144. 0x948E, 0x8710, 0x8410, 0xA90E, 0xAA0E, 0xAACE, 0xAC4E, 0xAC58
  145. };
  146. u32 initial_size = read_pipe_count;
  147. for (unsigned offset = 0; offset < size; offset += sizeof(u16)) {
  148. if (read_pipe_count < canned_read_pipe.size()) {
  149. Memory::Write16(addr + offset, canned_read_pipe[read_pipe_count]);
  150. read_pipe_count++;
  151. } else {
  152. LOG_ERROR(Service_DSP, "canned read pipe log exceeded!");
  153. break;
  154. }
  155. }
  156. cmd_buff[1] = 0; // No error
  157. cmd_buff[2] = (read_pipe_count - initial_size) * sizeof(u16);
  158. LOG_WARNING(Service_DSP, "(STUBBED) called size=0x%08X, buffer=0x%08X", size, addr);
  159. }
  160. /**
  161. * DSP_DSP::SetSemaphoreMask service function
  162. * Inputs:
  163. * 1 : Mask
  164. * Outputs:
  165. * 1 : Result of function, 0 on success, otherwise error code
  166. */
  167. void SetSemaphoreMask(Service::Interface* self) {
  168. u32* cmd_buff = Kernel::GetCommandBuffer();
  169. u32 mask = cmd_buff[1];
  170. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  171. LOG_WARNING(Service_DSP, "(STUBBED) called mask=0x%08X", mask);
  172. }
  173. /**
  174. * DSP_DSP::GetHeadphoneStatus service function
  175. * Inputs:
  176. * 1 : None
  177. * Outputs:
  178. * 1 : Result of function, 0 on success, otherwise error code
  179. * 2 : The headphone status response, 0 = Not using headphones?,
  180. * 1 = using headphones?
  181. */
  182. void GetHeadphoneStatus(Service::Interface* self) {
  183. u32* cmd_buff = Kernel::GetCommandBuffer();
  184. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  185. cmd_buff[2] = 0; // Not using headphones?
  186. LOG_WARNING(Service_DSP, "(STUBBED) called");
  187. }
  188. const Interface::FunctionInfo FunctionTable[] = {
  189. {0x00010040, nullptr, "RecvData"},
  190. {0x00020040, nullptr, "RecvDataIsReady"},
  191. {0x00030080, nullptr, "SendData"},
  192. {0x00040040, nullptr, "SendDataIsEmpty"},
  193. {0x00070040, WriteReg0x10, "WriteReg0x10"},
  194. {0x00080000, nullptr, "GetSemaphore"},
  195. {0x00090040, nullptr, "ClearSemaphore"},
  196. {0x000B0000, nullptr, "CheckSemaphoreRequest"},
  197. {0x000C0040, ConvertProcessAddressFromDspDram, "ConvertProcessAddressFromDspDram"},
  198. {0x000D0082, WriteProcessPipe, "WriteProcessPipe"},
  199. {0x001000C0, ReadPipeIfPossible, "ReadPipeIfPossible"},
  200. {0x001100C2, LoadComponent, "LoadComponent"},
  201. {0x00120000, nullptr, "UnloadComponent"},
  202. {0x00130082, nullptr, "FlushDataCache"},
  203. {0x00140082, nullptr, "InvalidateDCache"},
  204. {0x00150082, RegisterInterruptEvents, "RegisterInterruptEvents"},
  205. {0x00160000, GetSemaphoreEventHandle, "GetSemaphoreEventHandle"},
  206. {0x00170040, SetSemaphoreMask, "SetSemaphoreMask"},
  207. {0x00180040, nullptr, "GetPhysicalAddress"},
  208. {0x00190040, nullptr, "GetVirtualAddress"},
  209. {0x001A0042, nullptr, "SetIirFilterI2S1_cmd1"},
  210. {0x001B0042, nullptr, "SetIirFilterI2S1_cmd2"},
  211. {0x001C0082, nullptr, "SetIirFilterEQ"},
  212. {0x001F0000, GetHeadphoneStatus, "GetHeadphoneStatus"},
  213. {0x00210000, nullptr, "GetIsDspOccupied"},
  214. };
  215. ////////////////////////////////////////////////////////////////////////////////////////////////////
  216. // Interface class
  217. Interface::Interface() {
  218. semaphore_event = Kernel::Event::Create(RESETTYPE_ONESHOT,
  219. "DSP_DSP::semaphore_event").MoveFrom();
  220. interrupt_event = nullptr;
  221. read_pipe_count = 0;
  222. Register(FunctionTable, ARRAY_SIZE(FunctionTable));
  223. }
  224. } // namespace