dsp_dsp.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "audio_core/hle/pipe.h"
  5. #include "common/logging/log.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;
  12. static Kernel::SharedPtr<Kernel::Event> semaphore_event;
  13. struct PairHash {
  14. template <typename T, typename U>
  15. std::size_t operator()(const std::pair<T, U> &x) const {
  16. // TODO(yuriks): Replace with better hash combining function.
  17. return std::hash<T>()(x.first) ^ std::hash<U>()(x.second);
  18. }
  19. };
  20. /// Map of (audio interrupt number, channel number) to Kernel::Events. See: RegisterInterruptEvents
  21. static std::unordered_map<std::pair<u32, u32>, Kernel::SharedPtr<Kernel::Event>, PairHash> interrupt_events;
  22. // DSP Interrupts:
  23. // Interrupt #2 occurs every frame tick. Userland programs normally have a thread that's waiting
  24. // for an interrupt event. Immediately after this interrupt event, userland normally updates the
  25. // state in the next region and increments the relevant frame counter by two.
  26. void SignalAllInterrupts() {
  27. // HACK: The other interrupts have currently unknown purpose, we trigger them each tick in any case.
  28. for (auto& interrupt_event : interrupt_events)
  29. interrupt_event.second->Signal();
  30. }
  31. void SignalInterrupt(u32 interrupt, u32 channel) {
  32. interrupt_events[std::make_pair(interrupt, channel)]->Signal();
  33. }
  34. /**
  35. * DSP_DSP::ConvertProcessAddressFromDspDram service function
  36. * Inputs:
  37. * 1 : Address
  38. * Outputs:
  39. * 1 : Result of function, 0 on success, otherwise error code
  40. * 2 : (inaddr << 1) + 0x1FF40000 (where 0x1FF00000 is the DSP RAM address)
  41. */
  42. static void ConvertProcessAddressFromDspDram(Service::Interface* self) {
  43. u32* cmd_buff = Kernel::GetCommandBuffer();
  44. u32 addr = cmd_buff[1];
  45. cmd_buff[1] = 0; // No error
  46. cmd_buff[2] = (addr << 1) + (Memory::DSP_RAM_VADDR + 0x40000);
  47. LOG_TRACE(Service_DSP, "addr=0x%08X", addr);
  48. }
  49. /**
  50. * DSP_DSP::LoadComponent service function
  51. * Inputs:
  52. * 1 : Size
  53. * 2 : Unknown (observed only half word used)
  54. * 3 : Unknown (observed only half word used)
  55. * 4 : (size << 4) | 0xA
  56. * 5 : Buffer address
  57. * Outputs:
  58. * 1 : Result of function, 0 on success, otherwise error code
  59. * 2 : Component loaded, 0 on not loaded, 1 on loaded
  60. */
  61. static void LoadComponent(Service::Interface* self) {
  62. u32* cmd_buff = Kernel::GetCommandBuffer();
  63. u32 size = cmd_buff[1];
  64. u32 unk1 = cmd_buff[2];
  65. u32 unk2 = cmd_buff[3];
  66. u32 new_size = cmd_buff[4];
  67. u32 buffer = cmd_buff[5];
  68. cmd_buff[1] = 0; // No error
  69. cmd_buff[2] = 1; // Pretend that we actually loaded the DSP firmware
  70. // TODO(bunnei): Implement real DSP firmware loading
  71. LOG_WARNING(Service_DSP, "(STUBBED) called size=0x%X, unk1=0x%08X, unk2=0x%08X, new_size=0x%X, buffer=0x%08X",
  72. size, unk1, unk2, new_size, buffer);
  73. }
  74. /**
  75. * DSP_DSP::GetSemaphoreEventHandle service function
  76. * Outputs:
  77. * 1 : Result of function, 0 on success, otherwise error code
  78. * 3 : Semaphore event handle
  79. */
  80. static void GetSemaphoreEventHandle(Service::Interface* self) {
  81. u32* cmd_buff = Kernel::GetCommandBuffer();
  82. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  83. cmd_buff[3] = Kernel::g_handle_table.Create(semaphore_event).MoveFrom(); // Event handle
  84. LOG_WARNING(Service_DSP, "(STUBBED) called");
  85. }
  86. /**
  87. * DSP_DSP::FlushDataCache service function
  88. *
  89. * This Function is a no-op, We aren't emulating the CPU cache any time soon.
  90. *
  91. * Inputs:
  92. * 1 : Address
  93. * 2 : Size
  94. * 3 : Value 0, some descriptor for the KProcess Handle
  95. * 4 : KProcess handle
  96. * Outputs:
  97. * 1 : Result of function, 0 on success, otherwise error code
  98. */
  99. static void FlushDataCache(Service::Interface* self) {
  100. u32* cmd_buff = Kernel::GetCommandBuffer();
  101. u32 address = cmd_buff[1];
  102. u32 size = cmd_buff[2];
  103. u32 process = cmd_buff[4];
  104. // TODO(purpasmart96): Verify return header on HW
  105. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  106. LOG_DEBUG(Service_DSP, "(STUBBED) called address=0x%08X, size=0x%X, process=0x%08X",
  107. address, size, process);
  108. }
  109. /**
  110. * DSP_DSP::RegisterInterruptEvents service function
  111. * Inputs:
  112. * 1 : Interrupt Number
  113. * 2 : Channel Number
  114. * 4 : Interrupt event handle
  115. * Outputs:
  116. * 1 : Result of function, 0 on success, otherwise error code
  117. */
  118. static void RegisterInterruptEvents(Service::Interface* self) {
  119. u32* cmd_buff = Kernel::GetCommandBuffer();
  120. u32 interrupt = cmd_buff[1];
  121. u32 channel = cmd_buff[2];
  122. u32 event_handle = cmd_buff[4];
  123. if (event_handle) {
  124. auto evt = Kernel::g_handle_table.Get<Kernel::Event>(cmd_buff[4]);
  125. if (evt) {
  126. interrupt_events[std::make_pair(interrupt, channel)] = evt;
  127. cmd_buff[1] = RESULT_SUCCESS.raw;
  128. LOG_WARNING(Service_DSP, "Registered interrupt=%u, channel=%u, event_handle=0x%08X", interrupt, channel, event_handle);
  129. } else {
  130. cmd_buff[1] = -1;
  131. LOG_ERROR(Service_DSP, "Invalid event handle! interrupt=%u, channel=%u, event_handle=0x%08X", interrupt, channel, event_handle);
  132. }
  133. } else {
  134. interrupt_events.erase(std::make_pair(interrupt, channel));
  135. LOG_WARNING(Service_DSP, "Unregistered interrupt=%u, channel=%u, event_handle=0x%08X", interrupt, channel, event_handle);
  136. }
  137. }
  138. /**
  139. * DSP_DSP::SetSemaphore service function
  140. * Inputs:
  141. * 1 : Unknown (observed only half word used)
  142. * Outputs:
  143. * 1 : Result of function, 0 on success, otherwise error code
  144. */
  145. static void SetSemaphore(Service::Interface* self) {
  146. u32* cmd_buff = Kernel::GetCommandBuffer();
  147. cmd_buff[1] = 0; // No error
  148. LOG_WARNING(Service_DSP, "(STUBBED) called");
  149. }
  150. /**
  151. * DSP_DSP::WriteProcessPipe service function
  152. * Inputs:
  153. * 1 : Channel
  154. * 2 : Size
  155. * 3 : (size << 14) | 0x402
  156. * 4 : Buffer
  157. * Outputs:
  158. * 0 : Return header
  159. * 1 : Result of function, 0 on success, otherwise error code
  160. */
  161. static void WriteProcessPipe(Service::Interface* self) {
  162. u32* cmd_buff = Kernel::GetCommandBuffer();
  163. u32 channel = cmd_buff[1];
  164. u32 size = cmd_buff[2];
  165. u32 buffer = cmd_buff[4];
  166. if (IPC::StaticBufferDesc(size, 1) != cmd_buff[3]) {
  167. LOG_ERROR(Service_DSP, "IPC static buffer descriptor failed validation (0x%X). channel=%u, size=0x%X, buffer=0x%08X", cmd_buff[3], channel, size, buffer);
  168. cmd_buff[1] = -1; // TODO
  169. return;
  170. }
  171. if (!Memory::GetPointer(buffer)) {
  172. LOG_ERROR(Service_DSP, "Invalid Buffer: channel=%u, size=0x%X, buffer=0x%08X", channel, size, buffer);
  173. cmd_buff[1] = -1; // TODO
  174. return;
  175. }
  176. std::vector<u8> message(size);
  177. for (size_t i = 0; i < size; i++) {
  178. message[i] = Memory::Read8(buffer + i);
  179. }
  180. DSP::HLE::PipeWrite(channel, message);
  181. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  182. LOG_TRACE(Service_DSP, "channel=%u, size=0x%X, buffer=0x%08X", channel, size, buffer);
  183. }
  184. /**
  185. * DSP_DSP::ReadPipeIfPossible service function
  186. * A pipe is a means of communication between the ARM11 and DSP that occurs on
  187. * hardware by writing to/reading from the DSP registers at 0x10203000.
  188. * Pipes are used for initialisation. See also DSP::HLE::PipeRead.
  189. * Inputs:
  190. * 1 : Pipe Number
  191. * 2 : Unknown
  192. * 3 : Size in bytes of read (observed only lower half word used)
  193. * 0x41 : Virtual address to read from DSP pipe to in memory
  194. * Outputs:
  195. * 1 : Result of function, 0 on success, otherwise error code
  196. * 2 : Number of bytes read from pipe
  197. */
  198. static void ReadPipeIfPossible(Service::Interface* self) {
  199. u32* cmd_buff = Kernel::GetCommandBuffer();
  200. u32 pipe = cmd_buff[1];
  201. u32 unk2 = cmd_buff[2];
  202. u32 size = cmd_buff[3] & 0xFFFF;// Lower 16 bits are size
  203. VAddr addr = cmd_buff[0x41];
  204. if (!Memory::GetPointer(addr)) {
  205. LOG_ERROR(Service_DSP, "Invalid addr: pipe=0x%08X, unk2=0x%08X, size=0x%X, buffer=0x%08X", pipe, unk2, size, addr);
  206. cmd_buff[1] = -1; // TODO
  207. return;
  208. }
  209. std::vector<u8> response = DSP::HLE::PipeRead(pipe, size);
  210. Memory::WriteBlock(addr, response.data(), response.size());
  211. cmd_buff[1] = 0; // No error
  212. cmd_buff[2] = (u32)response.size();
  213. LOG_TRACE(Service_DSP, "pipe=0x%08X, unk2=0x%08X, size=0x%X, buffer=0x%08X", pipe, unk2, size, addr);
  214. }
  215. /**
  216. * DSP_DSP::SetSemaphoreMask service function
  217. * Inputs:
  218. * 1 : Mask
  219. * Outputs:
  220. * 1 : Result of function, 0 on success, otherwise error code
  221. */
  222. static void SetSemaphoreMask(Service::Interface* self) {
  223. u32* cmd_buff = Kernel::GetCommandBuffer();
  224. u32 mask = cmd_buff[1];
  225. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  226. LOG_WARNING(Service_DSP, "(STUBBED) called mask=0x%08X", mask);
  227. }
  228. /**
  229. * DSP_DSP::GetHeadphoneStatus service function
  230. * Inputs:
  231. * 1 : None
  232. * Outputs:
  233. * 1 : Result of function, 0 on success, otherwise error code
  234. * 2 : The headphone status response, 0 = Not using headphones?,
  235. * 1 = using headphones?
  236. */
  237. static void GetHeadphoneStatus(Service::Interface* self) {
  238. u32* cmd_buff = Kernel::GetCommandBuffer();
  239. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  240. cmd_buff[2] = 0; // Not using headphones?
  241. LOG_DEBUG(Service_DSP, "(STUBBED) called");
  242. }
  243. const Interface::FunctionInfo FunctionTable[] = {
  244. {0x00010040, nullptr, "RecvData"},
  245. {0x00020040, nullptr, "RecvDataIsReady"},
  246. {0x00030080, nullptr, "SendData"},
  247. {0x00040040, nullptr, "SendDataIsEmpty"},
  248. {0x000500C2, nullptr, "SendFifoEx"},
  249. {0x000600C0, nullptr, "RecvFifoEx"},
  250. {0x00070040, SetSemaphore, "SetSemaphore"},
  251. {0x00080000, nullptr, "GetSemaphore"},
  252. {0x00090040, nullptr, "ClearSemaphore"},
  253. {0x000A0040, nullptr, "MaskSemaphore"},
  254. {0x000B0000, nullptr, "CheckSemaphoreRequest"},
  255. {0x000C0040, ConvertProcessAddressFromDspDram, "ConvertProcessAddressFromDspDram"},
  256. {0x000D0082, WriteProcessPipe, "WriteProcessPipe"},
  257. {0x000E00C0, nullptr, "ReadPipe"},
  258. {0x000F0080, nullptr, "GetPipeReadableSize"},
  259. {0x001000C0, ReadPipeIfPossible, "ReadPipeIfPossible"},
  260. {0x001100C2, LoadComponent, "LoadComponent"},
  261. {0x00120000, nullptr, "UnloadComponent"},
  262. {0x00130082, FlushDataCache, "FlushDataCache"},
  263. {0x00140082, nullptr, "InvalidateDCache"},
  264. {0x00150082, RegisterInterruptEvents, "RegisterInterruptEvents"},
  265. {0x00160000, GetSemaphoreEventHandle, "GetSemaphoreEventHandle"},
  266. {0x00170040, SetSemaphoreMask, "SetSemaphoreMask"},
  267. {0x00180040, nullptr, "GetPhysicalAddress"},
  268. {0x00190040, nullptr, "GetVirtualAddress"},
  269. {0x001A0042, nullptr, "SetIirFilterI2S1_cmd1"},
  270. {0x001B0042, nullptr, "SetIirFilterI2S1_cmd2"},
  271. {0x001C0082, nullptr, "SetIirFilterEQ"},
  272. {0x001D00C0, nullptr, "ReadMultiEx_SPI2"},
  273. {0x001E00C2, nullptr, "WriteMultiEx_SPI2"},
  274. {0x001F0000, GetHeadphoneStatus, "GetHeadphoneStatus"},
  275. {0x00200040, nullptr, "ForceHeadphoneOut"},
  276. {0x00210000, nullptr, "GetIsDspOccupied"},
  277. };
  278. ////////////////////////////////////////////////////////////////////////////////////////////////////
  279. // Interface class
  280. Interface::Interface() {
  281. semaphore_event = Kernel::Event::Create(RESETTYPE_ONESHOT, "DSP_DSP::semaphore_event");
  282. read_pipe_count = 0;
  283. Register(FunctionTable);
  284. }
  285. Interface::~Interface() {
  286. semaphore_event = nullptr;
  287. interrupt_events.clear();
  288. }
  289. } // namespace