From 004991d79e991cf7825ab95fceecb4324d9b3c8c Mon Sep 17 00:00:00 2001
From: MerryMage <MerryMage@users.noreply.github.com>
Date: Sun, 6 Mar 2016 21:13:12 +0000
Subject: [PATCH] DSP: Implement Pipe 2

Pipe 2 is a DSP pipe that is used to initialize both the DSP hardware (the
application signals to the DSP to initialize) and the application (the DSP
provides the memory location of structures in the shared memory region).
---
 src/audio_core/hle/dsp.h         |  45 ++++++-
 src/audio_core/hle/pipe.cpp      | 152 ++++++++++++++++++++----
 src/audio_core/hle/pipe.h        |  33 ++++--
 src/core/hle/service/dsp_dsp.cpp | 194 ++++++++++++++++++++++++-------
 4 files changed, 346 insertions(+), 78 deletions(-)

diff --git a/src/audio_core/hle/dsp.h b/src/audio_core/hle/dsp.h
index 14c4000c6..376436c29 100644
--- a/src/audio_core/hle/dsp.h
+++ b/src/audio_core/hle/dsp.h
@@ -66,9 +66,9 @@ static_assert(std::is_trivially_copyable<u32_dsp>::value, "u32_dsp isn't trivial
 #endif
 
 // There are 15 structures in each memory region. A table of them in the order they appear in memory
-// is presented below
+// is presented below:
 //
-//       Pipe 2 #    First Region DSP Address   Purpose                               Control
+//       #           First Region DSP Address   Purpose                               Control
 //       5           0x8400                     DSP Status                            DSP
 //       9           0x8410                     DSP Debug Info                        DSP
 //       6           0x8540                     Final Mix Samples                     DSP
@@ -85,6 +85,9 @@ static_assert(std::is_trivially_copyable<u32_dsp>::value, "u32_dsp isn't trivial
 //       14          0xAC5C                     Surround Sound Related
 //       0           0xBFFF                     Frame Counter                         Application
 //
+// #: This refers to the order in which they appear in the DspPipe::Audio DSP pipe.
+//    See also: DSP::HLE::PipeRead.
+//
 // Note that the above addresses do vary slightly between audio firmwares observed; the addresses are
 // not fixed in stone. The addresses above are only an examplar; they're what this implementation
 // does and provides to applications.
@@ -472,13 +475,47 @@ struct SharedMemory {
 
     AdpcmCoefficients adpcm_coefficients;
 
-    /// Unknown 10-14 (Surround sound related)
-    INSERT_PADDING_DSPWORDS(0x16ED);
+    struct {
+        INSERT_PADDING_DSPWORDS(0x100);
+    } unknown10;
+
+    struct {
+        INSERT_PADDING_DSPWORDS(0xC0);
+    } unknown11;
+
+    struct {
+        INSERT_PADDING_DSPWORDS(0x180);
+    } unknown12;
+
+    struct {
+        INSERT_PADDING_DSPWORDS(0xA);
+    } unknown13;
+
+    struct {
+        INSERT_PADDING_DSPWORDS(0x13A3);
+    } unknown14;
 
     u16_le frame_counter;
 };
 ASSERT_DSP_STRUCT(SharedMemory, 0x8000);
 
+// Structures must have an offset that is a multiple of two.
+static_assert(offsetof(SharedMemory, frame_counter) % 2 == 0, "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
+static_assert(offsetof(SharedMemory, source_configurations) % 2 == 0, "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
+static_assert(offsetof(SharedMemory, source_statuses) % 2 == 0, "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
+static_assert(offsetof(SharedMemory, adpcm_coefficients) % 2 == 0, "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
+static_assert(offsetof(SharedMemory, dsp_configuration) % 2 == 0, "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
+static_assert(offsetof(SharedMemory, dsp_status) % 2 == 0, "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
+static_assert(offsetof(SharedMemory, final_samples) % 2 == 0, "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
+static_assert(offsetof(SharedMemory, intermediate_mix_samples) % 2 == 0, "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
+static_assert(offsetof(SharedMemory, compressor) % 2 == 0, "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
+static_assert(offsetof(SharedMemory, dsp_debug) % 2 == 0, "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
+static_assert(offsetof(SharedMemory, unknown10) % 2 == 0, "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
+static_assert(offsetof(SharedMemory, unknown11) % 2 == 0, "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
+static_assert(offsetof(SharedMemory, unknown12) % 2 == 0, "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
+static_assert(offsetof(SharedMemory, unknown13) % 2 == 0, "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
+static_assert(offsetof(SharedMemory, unknown14) % 2 == 0, "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
+
 #undef INSERT_PADDING_DSPWORDS
 #undef ASSERT_DSP_STRUCT
 
diff --git a/src/audio_core/hle/pipe.cpp b/src/audio_core/hle/pipe.cpp
index 6542c760c..9381883b4 100644
--- a/src/audio_core/hle/pipe.cpp
+++ b/src/audio_core/hle/pipe.cpp
@@ -5,50 +5,154 @@
 #include <array>
 #include <vector>
 
+#include "audio_core/hle/dsp.h"
 #include "audio_core/hle/pipe.h"
 
+#include "common/assert.h"
 #include "common/common_types.h"
 #include "common/logging/log.h"
 
 namespace DSP {
 namespace HLE {
 
-static size_t pipe2position = 0;
+static DspState dsp_state = DspState::Off;
+
+static std::array<std::vector<u8>, static_cast<size_t>(DspPipe::DspPipe_MAX)> pipe_data;
 
 void ResetPipes() {
-    pipe2position = 0;
+    for (auto& data : pipe_data) {
+        data.clear();
+    }
+    dsp_state = DspState::Off;
 }
 
-std::vector<u8> PipeRead(u32 pipe_number, u32 length) {
-    if (pipe_number != 2) {
-        LOG_WARNING(Audio_DSP, "pipe_number = %u (!= 2), unimplemented", pipe_number);
-        return {}; // We currently don't handle anything other than the audio pipe.
-    }
-
-    // Canned DSP responses that games expect. These were taken from HW by 3dmoo team.
-    // TODO: Our implementation will actually use a slightly different response than this one.
-    // TODO: Use offsetof on DSP structures instead for a proper response.
-    static const std::array<u8, 32> canned_response {{
-        0x0F, 0x00, 0xFF, 0xBF, 0x8E, 0x9E, 0x80, 0x86, 0x8E, 0xA7, 0x30, 0x94, 0x00, 0x84, 0x40, 0x85,
-        0x8E, 0x94, 0x10, 0x87, 0x10, 0x84, 0x0E, 0xA9, 0x0E, 0xAA, 0xCE, 0xAA, 0x4E, 0xAC, 0x58, 0xAC
-    }};
-
-    // TODO: Move this into dsp::DSP service since it happens on the service side.
-    // Hardware observation: No data is returned if requested length reads beyond the end of the data in-pipe.
-    if (pipe2position + length > canned_response.size()) {
+std::vector<u8> PipeRead(DspPipe pipe_number, u32 length) {
+    if (pipe_number >= DspPipe::DspPipe_MAX) {
+        LOG_ERROR(Audio_DSP, "pipe_number = %u invalid", pipe_number);
         return {};
     }
 
-    std::vector<u8> ret;
-    for (size_t i = 0; i < length; i++, pipe2position++) {
-        ret.emplace_back(canned_response[pipe2position]);
+    std::vector<u8>& data = pipe_data[static_cast<size_t>(pipe_number)];
+
+    if (length > data.size()) {
+        LOG_WARNING(Audio_DSP, "pipe_number = %u is out of data, application requested read of %u but %zu remain",
+                    pipe_number, length, data.size());
+        length = data.size();
     }
 
+    if (length == 0)
+        return {};
+
+    std::vector<u8> ret(data.begin(), data.begin() + length);
+    data.erase(data.begin(), data.begin() + length);
     return ret;
 }
 
-void PipeWrite(u32 pipe_number, const std::vector<u8>& buffer) {
-    // TODO: proper pipe behaviour
+size_t GetPipeReadableSize(DspPipe pipe_number) {
+    if (pipe_number >= DspPipe::DspPipe_MAX) {
+        LOG_ERROR(Audio_DSP, "pipe_number = %u invalid", pipe_number);
+        return 0;
+    }
+
+    return pipe_data[static_cast<size_t>(pipe_number)].size();
+}
+
+static void WriteU16(DspPipe pipe_number, u16 value) {
+    std::vector<u8>& data = pipe_data[static_cast<size_t>(pipe_number)];
+    // Little endian
+    data.emplace_back(value & 0xFF);
+    data.emplace_back(value >> 8);
+}
+
+static void AudioPipeWriteStructAddresses() {
+    // These struct addresses are DSP dram addresses.
+    // See also: DSP_DSP::ConvertProcessAddressFromDspDram
+    static const std::array<u16, 15> struct_addresses = {
+        0x8000 + offsetof(SharedMemory, frame_counter) / 2,
+        0x8000 + offsetof(SharedMemory, source_configurations) / 2,
+        0x8000 + offsetof(SharedMemory, source_statuses) / 2,
+        0x8000 + offsetof(SharedMemory, adpcm_coefficients) / 2,
+        0x8000 + offsetof(SharedMemory, dsp_configuration) / 2,
+        0x8000 + offsetof(SharedMemory, dsp_status) / 2,
+        0x8000 + offsetof(SharedMemory, final_samples) / 2,
+        0x8000 + offsetof(SharedMemory, intermediate_mix_samples) / 2,
+        0x8000 + offsetof(SharedMemory, compressor) / 2,
+        0x8000 + offsetof(SharedMemory, dsp_debug) / 2,
+        0x8000 + offsetof(SharedMemory, unknown10) / 2,
+        0x8000 + offsetof(SharedMemory, unknown11) / 2,
+        0x8000 + offsetof(SharedMemory, unknown12) / 2,
+        0x8000 + offsetof(SharedMemory, unknown13) / 2,
+        0x8000 + offsetof(SharedMemory, unknown14) / 2
+    };
+
+    // Begin with a u16 denoting the number of structs.
+    WriteU16(DspPipe::Audio, struct_addresses.size());
+    // Then write the struct addresses.
+    for (u16 addr : struct_addresses) {
+        WriteU16(DspPipe::Audio, addr);
+    }
+}
+
+void PipeWrite(DspPipe pipe_number, const std::vector<u8>& buffer) {
+    switch (pipe_number) {
+    case DspPipe::Audio: {
+        if (buffer.size() != 4) {
+            LOG_ERROR(Audio_DSP, "DspPipe::Audio: Unexpected buffer length %zu was written", buffer.size());
+            return;
+        }
+
+        enum class StateChange {
+            Initalize = 0,
+            Shutdown = 1,
+            Wakeup = 2,
+            Sleep = 3
+        };
+
+        // The difference between Initialize and Wakeup is that Input state is maintained
+        // when sleeping but isn't when turning it off and on again. (TODO: Implement this.)
+        // Waking up from sleep garbles some of the structs in the memory region. (TODO:
+        // Implement this.) Applications store away the state of these structs before
+        // sleeping and reset it back after wakeup on behalf of the DSP.
+
+        switch (static_cast<StateChange>(buffer[0])) {
+        case StateChange::Initalize:
+            LOG_INFO(Audio_DSP, "Application has requested initialization of DSP hardware");
+            ResetPipes();
+            AudioPipeWriteStructAddresses();
+            dsp_state = DspState::On;
+            break;
+        case StateChange::Shutdown:
+            LOG_INFO(Audio_DSP, "Application has requested shutdown of DSP hardware");
+            dsp_state = DspState::Off;
+            break;
+        case StateChange::Wakeup:
+            LOG_INFO(Audio_DSP, "Application has requested wakeup of DSP hardware");
+            ResetPipes();
+            AudioPipeWriteStructAddresses();
+            dsp_state = DspState::On;
+            break;
+        case StateChange::Sleep:
+            LOG_INFO(Audio_DSP, "Application has requested sleep of DSP hardware");
+            UNIMPLEMENTED();
+            dsp_state = DspState::Sleeping;
+            break;
+        default:
+            LOG_ERROR(Audio_DSP, "Application has requested unknown state transition of DSP hardware %hhu", buffer[0]);
+            dsp_state = DspState::Off;
+            break;
+        }
+
+        return;
+    }
+    default:
+        LOG_CRITICAL(Audio_DSP, "pipe_number = %u unimplemented", pipe_number);
+        UNIMPLEMENTED();
+        return;
+    }
+}
+
+DspState GetDspState() {
+    return dsp_state;
 }
 
 } // namespace HLE
diff --git a/src/audio_core/hle/pipe.h b/src/audio_core/hle/pipe.h
index ff6536950..382d35e87 100644
--- a/src/audio_core/hle/pipe.h
+++ b/src/audio_core/hle/pipe.h
@@ -4,6 +4,7 @@
 
 #pragma once
 
+#include <cstddef>
 #include <vector>
 
 #include "common/common_types.h"
@@ -14,25 +15,43 @@ namespace HLE {
 /// Reset the pipes by setting pipe positions back to the beginning.
 void ResetPipes();
 
+enum class DspPipe {
+    Debug = 0,
+    Dma = 1,
+    Audio = 2,
+    Binary = 3,
+    DspPipe_MAX
+};
+
 /**
  * Read a DSP pipe.
- * Pipe IDs:
- *   pipe_number = 0: Debug
- *   pipe_number = 1: P-DMA
- *   pipe_number = 2: Audio
- *   pipe_number = 3: Binary
  * @param pipe_number The Pipe ID
  * @param length How much data to request.
  * @return The data read from the pipe. The size of this vector can be less than the length requested.
  */
-std::vector<u8> PipeRead(u32 pipe_number, u32 length);
+std::vector<u8> PipeRead(DspPipe pipe_number, u32 length);
+
+/**
+ * How much data is left in pipe
+ * @param pipe_number The Pipe ID
+ * @return The amount of data remaning in the pipe. This is the maximum length PipeRead will return.
+ */
+size_t GetPipeReadableSize(DspPipe pipe_number);
 
 /**
  * Write to a DSP pipe.
  * @param pipe_number The Pipe ID
  * @param buffer The data to write to the pipe.
  */
-void PipeWrite(u32 pipe_number, const std::vector<u8>& buffer);
+void PipeWrite(DspPipe pipe_number, const std::vector<u8>& buffer);
+
+enum class DspState {
+    Off,
+    On,
+    Sleeping
+};
+/// Get the state of the DSP
+DspState GetDspState();
 
 } // namespace HLE
 } // namespace DSP
diff --git a/src/core/hle/service/dsp_dsp.cpp b/src/core/hle/service/dsp_dsp.cpp
index 22e85cfc6..3ba24d466 100644
--- a/src/core/hle/service/dsp_dsp.cpp
+++ b/src/core/hle/service/dsp_dsp.cpp
@@ -58,10 +58,10 @@ static void ConvertProcessAddressFromDspDram(Service::Interface* self) {
 
     u32 addr = cmd_buff[1];
 
-    cmd_buff[1] = 0; // No error
+    cmd_buff[1] = RESULT_SUCCESS.raw; // No error
     cmd_buff[2] = (addr << 1) + (Memory::DSP_RAM_VADDR + 0x40000);
 
-    LOG_TRACE(Service_DSP, "addr=0x%08X", addr);
+    LOG_DEBUG(Service_DSP, "addr=0x%08X", addr);
 }
 
 /**
@@ -142,8 +142,7 @@ static void FlushDataCache(Service::Interface* self) {
 
     cmd_buff[1] = RESULT_SUCCESS.raw; // No error
 
-    LOG_DEBUG(Service_DSP, "(STUBBED) called address=0x%08X, size=0x%X, process=0x%08X",
-              address, size, process);
+    LOG_TRACE(Service_DSP, "called address=0x%08X, size=0x%X, process=0x%08X", address, size, process);
 }
 
 /**
@@ -167,14 +166,14 @@ static void RegisterInterruptEvents(Service::Interface* self) {
         if (evt) {
             interrupt_events[std::make_pair(interrupt, channel)] = evt;
             cmd_buff[1] = RESULT_SUCCESS.raw;
-            LOG_WARNING(Service_DSP, "Registered interrupt=%u, channel=%u, event_handle=0x%08X", interrupt, channel, event_handle);
+            LOG_INFO(Service_DSP, "Registered interrupt=%u, channel=%u, event_handle=0x%08X", interrupt, channel, event_handle);
         } else {
-            cmd_buff[1] = -1;
-            LOG_ERROR(Service_DSP, "Invalid event handle! interrupt=%u, channel=%u, event_handle=0x%08X", interrupt, channel, event_handle);
+            LOG_CRITICAL(Service_DSP, "Invalid event handle! interrupt=%u, channel=%u, event_handle=0x%08X", interrupt, channel, event_handle);
+            ASSERT(false); // This should really be handled at a IPC translation layer.
         }
     } else {
         interrupt_events.erase(std::make_pair(interrupt, channel));
-        LOG_WARNING(Service_DSP, "Unregistered interrupt=%u, channel=%u, event_handle=0x%08X", interrupt, channel, event_handle);
+        LOG_INFO(Service_DSP, "Unregistered interrupt=%u, channel=%u, event_handle=0x%08X", interrupt, channel, event_handle);
     }
 }
 
@@ -188,7 +187,7 @@ static void RegisterInterruptEvents(Service::Interface* self) {
 static void SetSemaphore(Service::Interface* self) {
     u32* cmd_buff = Kernel::GetCommandBuffer();
 
-    cmd_buff[1] = 0; // No error
+    cmd_buff[1] = RESULT_SUCCESS.raw; // No error
 
     LOG_WARNING(Service_DSP, "(STUBBED) called");
 }
@@ -207,21 +206,12 @@ static void SetSemaphore(Service::Interface* self) {
 static void WriteProcessPipe(Service::Interface* self) {
     u32* cmd_buff = Kernel::GetCommandBuffer();
 
-    u32 channel  = cmd_buff[1];
-    u32 size     = cmd_buff[2];
-    u32 buffer   = cmd_buff[4];
+    DSP::HLE::DspPipe pipe = static_cast<DSP::HLE::DspPipe>(cmd_buff[1]);
+    u32 size = cmd_buff[2];
+    u32 buffer = cmd_buff[4];
 
-    if (IPC::StaticBufferDesc(size, 1) != cmd_buff[3]) {
-        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);
-        cmd_buff[1] = -1; // TODO
-        return;
-    }
-
-    if (!Memory::GetPointer(buffer)) {
-        LOG_ERROR(Service_DSP, "Invalid Buffer: channel=%u, size=0x%X, buffer=0x%08X", channel, size, buffer);
-        cmd_buff[1] = -1; // TODO
-        return;
-    }
+    ASSERT_MSG(IPC::StaticBufferDesc(size, 1) == cmd_buff[3], "IPC static buffer descriptor failed validation (0x%X). pipe=%u, size=0x%X, buffer=0x%08X", cmd_buff[3], pipe, size, buffer);
+    ASSERT_MSG(Memory::GetPointer(buffer) != nullptr, "Invalid Buffer: pipe=%u, size=0x%X, buffer=0x%08X", pipe, size, buffer);
 
     std::vector<u8> message(size);
 
@@ -229,11 +219,11 @@ static void WriteProcessPipe(Service::Interface* self) {
         message[i] = Memory::Read8(buffer + i);
     }
 
-    DSP::HLE::PipeWrite(channel, message);
+    DSP::HLE::PipeWrite(pipe, message);
 
     cmd_buff[1] = RESULT_SUCCESS.raw; // No error
 
-    LOG_TRACE(Service_DSP, "channel=%u, size=0x%X, buffer=0x%08X", channel, size, buffer);
+    LOG_DEBUG(Service_DSP, "pipe=%u, size=0x%X, buffer=0x%08X", pipe, size, buffer);
 }
 
 /**
@@ -245,7 +235,7 @@ static void WriteProcessPipe(Service::Interface* self) {
  *      1 : Pipe Number
  *      2 : Unknown
  *      3 : Size in bytes of read (observed only lower half word used)
- *      0x41 : Virtual address to read from DSP pipe to in memory
+ *      0x41 : Virtual address of memory buffer to write pipe contents to
  *  Outputs:
  *      1 : Result of function, 0 on success, otherwise error code
  *      2 : Number of bytes read from pipe
@@ -253,25 +243,82 @@ static void WriteProcessPipe(Service::Interface* self) {
 static void ReadPipeIfPossible(Service::Interface* self) {
     u32* cmd_buff = Kernel::GetCommandBuffer();
 
-    u32 pipe = cmd_buff[1];
-    u32 unk2 = cmd_buff[2];
-    u32 size = cmd_buff[3] & 0xFFFF;// Lower 16 bits are size
+    DSP::HLE::DspPipe pipe = static_cast<DSP::HLE::DspPipe>(cmd_buff[1]);
+    u32 unknown = cmd_buff[2];
+    u32 size = cmd_buff[3] & 0xFFFF; // Lower 16 bits are size
     VAddr addr = cmd_buff[0x41];
 
-    if (!Memory::GetPointer(addr)) {
-        LOG_ERROR(Service_DSP, "Invalid addr: pipe=0x%08X, unk2=0x%08X, size=0x%X, buffer=0x%08X", pipe, unk2, size, addr);
-        cmd_buff[1] = -1; // TODO
-        return;
+    ASSERT_MSG(Memory::GetPointer(addr) != nullptr, "Invalid addr: pipe=0x%08X, unknown=0x%08X, size=0x%X, buffer=0x%08X", pipe, unknown, size, addr);
+
+    cmd_buff[1] = RESULT_SUCCESS.raw; // No error
+    if (DSP::HLE::GetPipeReadableSize(pipe) >= size) {
+        std::vector<u8> response = DSP::HLE::PipeRead(pipe, size);
+
+        Memory::WriteBlock(addr, response.data(), response.size());
+
+        cmd_buff[2] = static_cast<u32>(response.size());
+    } else {
+        cmd_buff[2] = 0; // Return no data
     }
 
-    std::vector<u8> response = DSP::HLE::PipeRead(pipe, size);
+    LOG_DEBUG(Service_DSP, "pipe=0x%08X, unknown=0x%08X, size=0x%X, buffer=0x%08X, return cmd_buff[2]=0x%08X", pipe, unknown, size, addr, cmd_buff[2]);
+}
 
-    Memory::WriteBlock(addr, response.data(), response.size());
+/**
+ * DSP_DSP::ReadPipe service function
+ *  Inputs:
+ *      1 : Pipe Number
+ *      2 : Unknown
+ *      3 : Size in bytes of read (observed only lower half word used)
+ *      0x41 : Virtual address of memory buffer to write pipe contents to
+ *  Outputs:
+ *      1 : Result of function, 0 on success, otherwise error code
+ *      2 : Number of bytes read from pipe
+ */
+static void ReadPipe(Service::Interface* self) {
+    u32* cmd_buff = Kernel::GetCommandBuffer();
 
-    cmd_buff[1] = 0; // No error
-    cmd_buff[2] = (u32)response.size();
+    DSP::HLE::DspPipe pipe = static_cast<DSP::HLE::DspPipe>(cmd_buff[1]);
+    u32 unknown = cmd_buff[2];
+    u32 size = cmd_buff[3] & 0xFFFF; // Lower 16 bits are size
+    VAddr addr = cmd_buff[0x41];
 
-    LOG_TRACE(Service_DSP, "pipe=0x%08X, unk2=0x%08X, size=0x%X, buffer=0x%08X", pipe, unk2, size, addr);
+    ASSERT_MSG(Memory::GetPointer(addr) != nullptr, "Invalid addr: pipe=0x%08X, unknown=0x%08X, size=0x%X, buffer=0x%08X", pipe, unknown, size, addr);
+
+    if (DSP::HLE::GetPipeReadableSize(pipe) >= size) {
+        std::vector<u8> response = DSP::HLE::PipeRead(pipe, size);
+
+        Memory::WriteBlock(addr, response.data(), response.size());
+
+        cmd_buff[1] = RESULT_SUCCESS.raw; // No error
+        cmd_buff[2] = static_cast<u32>(response.size());
+    } else {
+        // No more data is in pipe. Hardware hangs in this case; this should never happen.
+        UNREACHABLE();
+    }
+
+    LOG_DEBUG(Service_DSP, "pipe=0x%08X, unknown=0x%08X, size=0x%X, buffer=0x%08X, return cmd_buff[2]=0x%08X", pipe, unknown, size, addr, cmd_buff[2]);
+}
+
+/**
+ * DSP_DSP::GetPipeReadableSize service function
+ *  Inputs:
+ *      1 : Pipe Number
+ *      2 : Unknown
+ *  Outputs:
+ *      1 : Result of function, 0 on success, otherwise error code
+ *      2 : Number of bytes readable from pipe
+ */
+static void GetPipeReadableSize(Service::Interface* self) {
+    u32* cmd_buff = Kernel::GetCommandBuffer();
+
+    DSP::HLE::DspPipe pipe = static_cast<DSP::HLE::DspPipe>(cmd_buff[1]);
+    u32 unknown = cmd_buff[2];
+
+    cmd_buff[1] = RESULT_SUCCESS.raw; // No error
+    cmd_buff[2] = DSP::HLE::GetPipeReadableSize(pipe);
+
+    LOG_DEBUG(Service_DSP, "pipe=0x%08X, unknown=0x%08X, return cmd_buff[2]=0x%08X", pipe, unknown, cmd_buff[2]);
 }
 
 /**
@@ -306,12 +353,73 @@ static void GetHeadphoneStatus(Service::Interface* self) {
     cmd_buff[1] = RESULT_SUCCESS.raw; // No error
     cmd_buff[2] = 0; // Not using headphones?
 
-    LOG_DEBUG(Service_DSP, "(STUBBED) called");
+    LOG_WARNING(Service_DSP, "(STUBBED) called");
+}
+
+/**
+ * DSP_DSP::RecvData service function
+ *      This function reads a value out of a DSP register.
+ *  Inputs:
+ *      1 : Register Number
+ *  Outputs:
+ *      1 : Result of function, 0 on success, otherwise error code
+ *      2 : Value in the register
+ *  Notes:
+ *      This function has only been observed being called with a register number of 0.
+ */
+static void RecvData(Service::Interface* self) {
+    u32* cmd_buff = Kernel::GetCommandBuffer();
+
+    u32 register_number = cmd_buff[1];
+
+    ASSERT_MSG(register_number == 0, "Unknown register_number %u", register_number);
+
+    // Application reads this after requesting DSP shutdown, to verify the DSP has indeed shutdown or slept.
+
+    cmd_buff[1] = RESULT_SUCCESS.raw;
+    switch (DSP::HLE::GetDspState()) {
+    case DSP::HLE::DspState::On:
+        cmd_buff[2] = 0;
+        break;
+    case DSP::HLE::DspState::Off:
+    case DSP::HLE::DspState::Sleeping:
+        cmd_buff[2] = 1;
+        break;
+    default:
+        UNREACHABLE();
+        break;
+    }
+
+    LOG_DEBUG(Service_DSP, "register_number=%u", register_number);
+}
+
+/**
+ * DSP_DSP::RecvDataIsReady service function
+ *      This function checks whether a DSP register is ready to be read.
+ *  Inputs:
+ *      1 : Register Number
+ *  Outputs:
+ *      1 : Result of function, 0 on success, otherwise error code
+ *      2 : non-zero == ready
+ *  Note:
+ *      This function has only been observed being called with a register number of 0.
+ */
+static void RecvDataIsReady(Service::Interface* self) {
+    u32* cmd_buff = Kernel::GetCommandBuffer();
+
+    u32 register_number = cmd_buff[1];
+
+    ASSERT_MSG(register_number == 0, "Unknown register_number %u", register_number);
+
+    cmd_buff[1] = RESULT_SUCCESS.raw;
+    cmd_buff[2] = 1; // Ready to read
+
+    LOG_DEBUG(Service_DSP, "register_number=%u", register_number);
 }
 
 const Interface::FunctionInfo FunctionTable[] = {
-    {0x00010040, nullptr,                          "RecvData"},
-    {0x00020040, nullptr,                          "RecvDataIsReady"},
+    {0x00010040, RecvData,                         "RecvData"},
+    {0x00020040, RecvDataIsReady,                  "RecvDataIsReady"},
     {0x00030080, nullptr,                          "SendData"},
     {0x00040040, nullptr,                          "SendDataIsEmpty"},
     {0x000500C2, nullptr,                          "SendFifoEx"},
@@ -323,8 +431,8 @@ const Interface::FunctionInfo FunctionTable[] = {
     {0x000B0000, nullptr,                          "CheckSemaphoreRequest"},
     {0x000C0040, ConvertProcessAddressFromDspDram, "ConvertProcessAddressFromDspDram"},
     {0x000D0082, WriteProcessPipe,                 "WriteProcessPipe"},
-    {0x000E00C0, nullptr,                          "ReadPipe"},
-    {0x000F0080, nullptr,                          "GetPipeReadableSize"},
+    {0x000E00C0, ReadPipe,                         "ReadPipe"},
+    {0x000F0080, GetPipeReadableSize,              "GetPipeReadableSize"},
     {0x001000C0, ReadPipeIfPossible,               "ReadPipeIfPossible"},
     {0x001100C2, LoadComponent,                    "LoadComponent"},
     {0x00120000, nullptr,                          "UnloadComponent"},