From 004991d79e991cf7825ab95fceecb4324d9b3c8c Mon Sep 17 00:00:00 2001 From: MerryMage Date: Sun, 6 Mar 2016 21:13:12 +0000 Subject: 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 | 150 +++++++++++++++++++++++++++++++++++++------- src/audio_core/hle/pipe.h | 33 +++++++--- 3 files changed, 194 insertions(+), 34 deletions(-) (limited to 'src/audio_core/hle') 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::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::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 #include +#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, static_cast(DspPipe::DspPipe_MAX)> pipe_data; void ResetPipes() { - pipe2position = 0; + for (auto& data : pipe_data) { + data.clear(); + } + dsp_state = DspState::Off; } -std::vector 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. +std::vector 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& data = pipe_data[static_cast(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(); } - // 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 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()) { + if (length == 0) return {}; + + std::vector ret(data.begin(), data.begin() + length); + data.erase(data.begin(), data.begin() + length); + return ret; +} + +size_t GetPipeReadableSize(DspPipe pipe_number) { + if (pipe_number >= DspPipe::DspPipe_MAX) { + LOG_ERROR(Audio_DSP, "pipe_number = %u invalid", pipe_number); + return 0; } - std::vector ret; - for (size_t i = 0; i < length; i++, pipe2position++) { - ret.emplace_back(canned_response[pipe2position]); + return pipe_data[static_cast(pipe_number)].size(); +} + +static void WriteU16(DspPipe pipe_number, u16 value) { + std::vector& data = pipe_data[static_cast(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 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); } +} - return ret; +void PipeWrite(DspPipe pipe_number, const std::vector& 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(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; + } } -void PipeWrite(u32 pipe_number, const std::vector& buffer) { - // TODO: proper pipe behaviour +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 #include #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 PipeRead(u32 pipe_number, u32 length); +std::vector 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& buffer); +void PipeWrite(DspPipe pipe_number, const std::vector& buffer); + +enum class DspState { + Off, + On, + Sleeping +}; +/// Get the state of the DSP +DspState GetDspState(); } // namespace HLE } // namespace DSP -- cgit v1.2.3