2020-01-19 22:49:22 +00:00
|
|
|
#pragma optimize("", off)
|
2017-12-20 18:44:32 +00:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2020-01-19 22:49:22 +00:00
|
|
|
#include <boost/serialization/array.hpp>
|
|
|
|
#include <boost/serialization/base_object.hpp>
|
|
|
|
#include <boost/serialization/shared_ptr.hpp>
|
|
|
|
#include <boost/serialization/vector.hpp>
|
|
|
|
#include <boost/serialization/weak_ptr.hpp>
|
2017-12-20 18:44:32 +00:00
|
|
|
#include "audio_core/audio_types.h"
|
2018-12-19 16:12:57 +00:00
|
|
|
#ifdef HAVE_MF
|
|
|
|
#include "audio_core/hle/wmf_decoder.h"
|
2019-01-05 00:06:12 +00:00
|
|
|
#elif HAVE_FFMPEG
|
|
|
|
#include "audio_core/hle/ffmpeg_decoder.h"
|
2020-02-23 10:01:21 +00:00
|
|
|
#elif HAVE_FDK
|
|
|
|
#include "audio_core/hle/fdk_decoder.h"
|
2018-12-09 21:25:45 +00:00
|
|
|
#endif
|
2020-03-27 22:19:29 +00:00
|
|
|
#include <iostream>
|
2017-12-20 18:44:32 +00:00
|
|
|
#include "audio_core/hle/common.h"
|
2018-12-09 21:25:45 +00:00
|
|
|
#include "audio_core/hle/decoder.h"
|
2017-12-20 18:44:32 +00:00
|
|
|
#include "audio_core/hle/hle.h"
|
|
|
|
#include "audio_core/hle/mixers.h"
|
|
|
|
#include "audio_core/hle/shared_memory.h"
|
|
|
|
#include "audio_core/hle/source.h"
|
|
|
|
#include "audio_core/sink.h"
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/common_types.h"
|
2018-12-06 13:35:21 +00:00
|
|
|
#include "common/hash.h"
|
2017-12-20 18:44:32 +00:00
|
|
|
#include "common/logging/log.h"
|
2018-10-27 19:53:20 +00:00
|
|
|
#include "core/core.h"
|
2017-12-20 18:44:32 +00:00
|
|
|
#include "core/core_timing.h"
|
2018-07-24 19:54:33 +00:00
|
|
|
|
2020-01-19 22:49:22 +00:00
|
|
|
SERIALIZE_EXPORT_IMPL(AudioCore::DspHle)
|
|
|
|
|
2018-07-24 19:54:33 +00:00
|
|
|
using InterruptType = Service::DSP::DSP_DSP::InterruptType;
|
|
|
|
using Service::DSP::DSP_DSP;
|
2017-12-20 18:44:32 +00:00
|
|
|
|
|
|
|
namespace AudioCore {
|
|
|
|
|
2020-01-19 22:49:22 +00:00
|
|
|
DspHle::DspHle() : DspHle(Core::System::GetInstance().Memory()) {}
|
|
|
|
|
|
|
|
template <class Archive>
|
|
|
|
void DspHle::serialize(Archive& ar, const unsigned int) {
|
|
|
|
ar& boost::serialization::base_object<DspInterface>(*this);
|
|
|
|
ar&* impl.get();
|
|
|
|
}
|
|
|
|
|
2017-12-20 18:44:32 +00:00
|
|
|
static constexpr u64 audio_frame_ticks = 1310252ull; ///< Units: ARM11 cycles
|
|
|
|
|
|
|
|
struct DspHle::Impl final {
|
|
|
|
public:
|
2018-11-21 16:36:24 +00:00
|
|
|
explicit Impl(DspHle& parent, Memory::MemorySystem& memory);
|
2017-12-20 18:44:32 +00:00
|
|
|
~Impl();
|
|
|
|
|
|
|
|
DspState GetDspState() const;
|
|
|
|
|
2018-12-06 12:18:26 +00:00
|
|
|
u16 RecvData(u32 register_number);
|
2018-12-06 12:28:47 +00:00
|
|
|
bool RecvDataIsReady(u32 register_number) const;
|
2017-12-20 18:44:32 +00:00
|
|
|
std::vector<u8> PipeRead(DspPipe pipe_number, u32 length);
|
2018-09-06 20:03:28 +00:00
|
|
|
std::size_t GetPipeReadableSize(DspPipe pipe_number) const;
|
2017-12-20 18:44:32 +00:00
|
|
|
void PipeWrite(DspPipe pipe_number, const std::vector<u8>& buffer);
|
|
|
|
|
|
|
|
std::array<u8, Memory::DSP_RAM_SIZE>& GetDspMemory();
|
|
|
|
|
2018-07-29 18:26:41 +00:00
|
|
|
void SetServiceToInterrupt(std::weak_ptr<DSP_DSP> dsp);
|
2018-07-24 19:54:33 +00:00
|
|
|
|
2017-12-20 18:44:32 +00:00
|
|
|
private:
|
|
|
|
void ResetPipes();
|
|
|
|
void WriteU16(DspPipe pipe_number, u16 value);
|
|
|
|
void AudioPipeWriteStructAddresses();
|
|
|
|
|
2018-09-06 20:03:28 +00:00
|
|
|
std::size_t CurrentRegionIndex() const;
|
2017-12-20 18:44:32 +00:00
|
|
|
HLE::SharedMemory& ReadRegion();
|
|
|
|
HLE::SharedMemory& WriteRegion();
|
|
|
|
|
|
|
|
StereoFrame16 GenerateCurrentFrame();
|
|
|
|
bool Tick();
|
2018-07-23 21:08:14 +00:00
|
|
|
void AudioTickCallback(s64 cycles_late);
|
2017-12-20 18:44:32 +00:00
|
|
|
|
|
|
|
DspState dsp_state = DspState::Off;
|
2020-01-19 22:49:22 +00:00
|
|
|
std::array<std::vector<u8>, num_dsp_pipe> pipe_data{};
|
2017-12-20 18:44:32 +00:00
|
|
|
|
|
|
|
HLE::DspMemory dsp_memory;
|
|
|
|
std::array<HLE::Source, HLE::num_sources> sources{{
|
|
|
|
HLE::Source(0), HLE::Source(1), HLE::Source(2), HLE::Source(3), HLE::Source(4),
|
|
|
|
HLE::Source(5), HLE::Source(6), HLE::Source(7), HLE::Source(8), HLE::Source(9),
|
|
|
|
HLE::Source(10), HLE::Source(11), HLE::Source(12), HLE::Source(13), HLE::Source(14),
|
|
|
|
HLE::Source(15), HLE::Source(16), HLE::Source(17), HLE::Source(18), HLE::Source(19),
|
|
|
|
HLE::Source(20), HLE::Source(21), HLE::Source(22), HLE::Source(23),
|
|
|
|
}};
|
2020-01-19 22:49:22 +00:00
|
|
|
HLE::Mixers mixers{};
|
2017-12-20 18:44:32 +00:00
|
|
|
|
|
|
|
DspHle& parent;
|
2020-01-19 22:49:22 +00:00
|
|
|
Core::TimingEventType* tick_event{};
|
2018-07-24 19:54:33 +00:00
|
|
|
|
2020-01-19 22:49:22 +00:00
|
|
|
std::unique_ptr<HLE::DecoderBase> decoder{};
|
2018-12-09 21:25:45 +00:00
|
|
|
|
2020-01-19 22:49:22 +00:00
|
|
|
std::weak_ptr<DSP_DSP> dsp_dsp{};
|
|
|
|
|
|
|
|
template <class Archive>
|
|
|
|
void serialize(Archive& ar, const unsigned int) {
|
|
|
|
ar& dsp_state;
|
|
|
|
ar& pipe_data;
|
|
|
|
ar& dsp_memory.raw_memory;
|
|
|
|
ar& sources;
|
|
|
|
ar& mixers;
|
|
|
|
ar& dsp_dsp;
|
|
|
|
}
|
|
|
|
friend class boost::serialization::access;
|
2017-12-20 18:44:32 +00:00
|
|
|
};
|
|
|
|
|
2018-11-21 16:36:24 +00:00
|
|
|
DspHle::Impl::Impl(DspHle& parent_, Memory::MemorySystem& memory) : parent(parent_) {
|
2018-02-04 22:23:51 +00:00
|
|
|
dsp_memory.raw_memory.fill(0);
|
|
|
|
|
2018-11-21 16:36:24 +00:00
|
|
|
for (auto& source : sources) {
|
|
|
|
source.SetMemory(memory);
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:19:19 +00:00
|
|
|
#if defined(HAVE_MF) && defined(HAVE_FFMPEG)
|
2018-12-19 16:12:57 +00:00
|
|
|
decoder = std::make_unique<HLE::WMFDecoder>(memory);
|
2019-12-08 21:19:19 +00:00
|
|
|
if (!decoder->IsValid()) {
|
|
|
|
LOG_WARNING(Audio_DSP, "Unable to load MediaFoundation. Attempting to load FFMPEG instead");
|
|
|
|
decoder = std::make_unique<HLE::FFMPEGDecoder>(memory);
|
|
|
|
}
|
|
|
|
#elif defined(HAVE_MF)
|
|
|
|
decoder = std::make_unique<HLE::WMFDecoder>(memory);
|
|
|
|
#elif defined(HAVE_FFMPEG)
|
2019-01-05 00:06:12 +00:00
|
|
|
decoder = std::make_unique<HLE::FFMPEGDecoder>(memory);
|
2020-02-23 10:01:21 +00:00
|
|
|
#elif defined(HAVE_FDK)
|
|
|
|
decoder = std::make_unique<HLE::FDKDecoder>(memory);
|
2018-12-09 21:25:45 +00:00
|
|
|
#else
|
2019-01-05 00:06:12 +00:00
|
|
|
LOG_WARNING(Audio_DSP, "No decoder found, this could lead to missing audio");
|
2018-12-09 21:25:45 +00:00
|
|
|
decoder = std::make_unique<HLE::NullDecoder>();
|
2018-12-19 16:12:57 +00:00
|
|
|
#endif // HAVE_MF
|
2018-12-09 21:25:45 +00:00
|
|
|
|
2019-12-08 21:19:19 +00:00
|
|
|
if (!decoder->IsValid()) {
|
|
|
|
LOG_WARNING(Audio_DSP,
|
|
|
|
"Unable to load any decoders, this could cause missing audio in some games");
|
|
|
|
decoder = std::make_unique<HLE::NullDecoder>();
|
|
|
|
}
|
|
|
|
|
2018-10-27 19:53:20 +00:00
|
|
|
Core::Timing& timing = Core::System::GetInstance().CoreTiming();
|
2017-12-20 18:44:32 +00:00
|
|
|
tick_event =
|
2018-10-27 19:53:20 +00:00
|
|
|
timing.RegisterEvent("AudioCore::DspHle::tick_event", [this](u64, s64 cycles_late) {
|
2017-12-20 18:44:32 +00:00
|
|
|
this->AudioTickCallback(cycles_late);
|
|
|
|
});
|
2018-10-27 19:53:20 +00:00
|
|
|
timing.ScheduleEvent(audio_frame_ticks, tick_event);
|
2017-12-20 18:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DspHle::Impl::~Impl() {
|
2020-03-27 22:19:29 +00:00
|
|
|
LOG_WARNING(Audio_DSP, "b1");
|
2018-10-27 19:53:20 +00:00
|
|
|
Core::Timing& timing = Core::System::GetInstance().CoreTiming();
|
2020-03-27 22:19:29 +00:00
|
|
|
LOG_WARNING(Audio_DSP, "b2");
|
2018-10-27 19:53:20 +00:00
|
|
|
timing.UnscheduleEvent(tick_event, 0);
|
2020-03-27 22:19:29 +00:00
|
|
|
LOG_WARNING(Audio_DSP, "b3");
|
2017-12-20 18:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DspState DspHle::Impl::GetDspState() const {
|
|
|
|
return dsp_state;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:18:26 +00:00
|
|
|
u16 DspHle::Impl::RecvData(u32 register_number) {
|
|
|
|
ASSERT_MSG(register_number == 0, "Unknown register_number {}", register_number);
|
|
|
|
|
|
|
|
// Application reads this after requesting DSP shutdown, to verify the DSP has indeed shutdown
|
|
|
|
// or slept.
|
|
|
|
|
|
|
|
switch (GetDspState()) {
|
|
|
|
case AudioCore::DspState::On:
|
|
|
|
return 0;
|
|
|
|
case AudioCore::DspState::Off:
|
|
|
|
case AudioCore::DspState::Sleeping:
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:28:47 +00:00
|
|
|
bool DspHle::Impl::RecvDataIsReady(u32 register_number) const {
|
|
|
|
ASSERT_MSG(register_number == 0, "Unknown register_number {}", register_number);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-20 18:44:32 +00:00
|
|
|
std::vector<u8> DspHle::Impl::PipeRead(DspPipe pipe_number, u32 length) {
|
2018-09-06 20:03:28 +00:00
|
|
|
const std::size_t pipe_index = static_cast<std::size_t>(pipe_number);
|
2017-12-20 18:44:32 +00:00
|
|
|
|
|
|
|
if (pipe_index >= num_dsp_pipe) {
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_ERROR(Audio_DSP, "pipe_number = {} invalid", pipe_index);
|
2017-12-20 18:44:32 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length > UINT16_MAX) { // Can only read at most UINT16_MAX from the pipe
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_ERROR(Audio_DSP, "length of {} greater than max of {}", length, UINT16_MAX);
|
2017-12-20 18:44:32 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<u8>& data = pipe_data[pipe_index];
|
|
|
|
|
|
|
|
if (length > data.size()) {
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_WARNING(
|
2017-12-20 18:44:32 +00:00
|
|
|
Audio_DSP,
|
2018-03-24 13:35:44 +00:00
|
|
|
"pipe_number = {} is out of data, application requested read of {} but {} remain",
|
2017-12-20 18:44:32 +00:00
|
|
|
pipe_index, length, data.size());
|
|
|
|
length = static_cast<u32>(data.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length == 0)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
std::vector<u8> ret(data.begin(), data.begin() + length);
|
|
|
|
data.erase(data.begin(), data.begin() + length);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t DspHle::Impl::GetPipeReadableSize(DspPipe pipe_number) const {
|
2018-09-06 20:03:28 +00:00
|
|
|
const std::size_t pipe_index = static_cast<std::size_t>(pipe_number);
|
2017-12-20 18:44:32 +00:00
|
|
|
|
|
|
|
if (pipe_index >= num_dsp_pipe) {
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_ERROR(Audio_DSP, "pipe_number = {} invalid", pipe_index);
|
2017-12-20 18:44:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pipe_data[pipe_index].size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DspHle::Impl::PipeWrite(DspPipe pipe_number, const std::vector<u8>& buffer) {
|
|
|
|
switch (pipe_number) {
|
|
|
|
case DspPipe::Audio: {
|
|
|
|
if (buffer.size() != 4) {
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_ERROR(Audio_DSP, "DspPipe::Audio: Unexpected buffer length {} was written",
|
2018-06-29 13:56:12 +00:00
|
|
|
buffer.size());
|
2017-12-20 18:44:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum class StateChange {
|
|
|
|
Initialize = 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::Initialize:
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_INFO(Audio_DSP, "Application has requested initialization of DSP hardware");
|
2017-12-20 18:44:32 +00:00
|
|
|
ResetPipes();
|
|
|
|
AudioPipeWriteStructAddresses();
|
|
|
|
dsp_state = DspState::On;
|
|
|
|
break;
|
|
|
|
case StateChange::Shutdown:
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_INFO(Audio_DSP, "Application has requested shutdown of DSP hardware");
|
2017-12-20 18:44:32 +00:00
|
|
|
dsp_state = DspState::Off;
|
|
|
|
break;
|
|
|
|
case StateChange::Wakeup:
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_INFO(Audio_DSP, "Application has requested wakeup of DSP hardware");
|
2017-12-20 18:44:32 +00:00
|
|
|
ResetPipes();
|
|
|
|
AudioPipeWriteStructAddresses();
|
|
|
|
dsp_state = DspState::On;
|
|
|
|
break;
|
|
|
|
case StateChange::Sleep:
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_INFO(Audio_DSP, "Application has requested sleep of DSP hardware");
|
2017-12-20 18:44:32 +00:00
|
|
|
UNIMPLEMENTED();
|
|
|
|
dsp_state = DspState::Sleeping;
|
|
|
|
break;
|
|
|
|
default:
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_ERROR(Audio_DSP,
|
2018-06-29 13:56:12 +00:00
|
|
|
"Application has requested unknown state transition of DSP hardware {}",
|
|
|
|
buffer[0]);
|
2017-12-20 18:44:32 +00:00
|
|
|
dsp_state = DspState::Off;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-12-09 21:25:45 +00:00
|
|
|
case DspPipe::Binary: {
|
|
|
|
// TODO(B3N30): Make this async, and signal the interrupt
|
|
|
|
HLE::BinaryRequest request;
|
|
|
|
if (sizeof(request) != buffer.size()) {
|
|
|
|
LOG_CRITICAL(Audio_DSP, "got binary pipe with wrong size {}", buffer.size());
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::memcpy(&request, buffer.data(), buffer.size());
|
|
|
|
if (request.codec != HLE::DecoderCodec::AAC) {
|
|
|
|
LOG_CRITICAL(Audio_DSP, "got unknown codec {}", static_cast<u16>(request.codec));
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::optional<HLE::BinaryResponse> response = decoder->ProcessRequest(request);
|
|
|
|
if (response) {
|
|
|
|
const HLE::BinaryResponse& value = *response;
|
|
|
|
pipe_data[static_cast<u32>(pipe_number)].resize(sizeof(value));
|
|
|
|
std::memcpy(pipe_data[static_cast<u32>(pipe_number)].data(), &value, sizeof(value));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-12-20 18:44:32 +00:00
|
|
|
default:
|
2018-09-06 20:03:28 +00:00
|
|
|
LOG_CRITICAL(Audio_DSP, "pipe_number = {} unimplemented",
|
|
|
|
static_cast<std::size_t>(pipe_number));
|
2017-12-20 18:44:32 +00:00
|
|
|
UNIMPLEMENTED();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::array<u8, Memory::DSP_RAM_SIZE>& DspHle::Impl::GetDspMemory() {
|
|
|
|
return dsp_memory.raw_memory;
|
|
|
|
}
|
|
|
|
|
2018-07-29 18:26:41 +00:00
|
|
|
void DspHle::Impl::SetServiceToInterrupt(std::weak_ptr<DSP_DSP> dsp) {
|
2018-07-24 19:54:33 +00:00
|
|
|
dsp_dsp = std::move(dsp);
|
|
|
|
}
|
|
|
|
|
2017-12-20 18:44:32 +00:00
|
|
|
void DspHle::Impl::ResetPipes() {
|
|
|
|
for (auto& data : pipe_data) {
|
|
|
|
data.clear();
|
|
|
|
}
|
|
|
|
dsp_state = DspState::Off;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DspHle::Impl::WriteU16(DspPipe pipe_number, u16 value) {
|
2018-09-06 20:03:28 +00:00
|
|
|
const std::size_t pipe_index = static_cast<std::size_t>(pipe_number);
|
2017-12-20 18:44:32 +00:00
|
|
|
|
|
|
|
std::vector<u8>& data = pipe_data.at(pipe_index);
|
|
|
|
// Little endian
|
|
|
|
data.emplace_back(value & 0xFF);
|
|
|
|
data.emplace_back(value >> 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DspHle::Impl::AudioPipeWriteStructAddresses() {
|
|
|
|
// These struct addresses are DSP dram addresses.
|
|
|
|
// See also: DSP_DSP::ConvertProcessAddressFromDspDram
|
|
|
|
static const std::array<u16, 15> struct_addresses = {
|
|
|
|
0x8000 + offsetof(HLE::SharedMemory, frame_counter) / 2,
|
|
|
|
0x8000 + offsetof(HLE::SharedMemory, source_configurations) / 2,
|
|
|
|
0x8000 + offsetof(HLE::SharedMemory, source_statuses) / 2,
|
|
|
|
0x8000 + offsetof(HLE::SharedMemory, adpcm_coefficients) / 2,
|
|
|
|
0x8000 + offsetof(HLE::SharedMemory, dsp_configuration) / 2,
|
|
|
|
0x8000 + offsetof(HLE::SharedMemory, dsp_status) / 2,
|
|
|
|
0x8000 + offsetof(HLE::SharedMemory, final_samples) / 2,
|
|
|
|
0x8000 + offsetof(HLE::SharedMemory, intermediate_mix_samples) / 2,
|
|
|
|
0x8000 + offsetof(HLE::SharedMemory, compressor) / 2,
|
|
|
|
0x8000 + offsetof(HLE::SharedMemory, dsp_debug) / 2,
|
|
|
|
0x8000 + offsetof(HLE::SharedMemory, unknown10) / 2,
|
|
|
|
0x8000 + offsetof(HLE::SharedMemory, unknown11) / 2,
|
|
|
|
0x8000 + offsetof(HLE::SharedMemory, unknown12) / 2,
|
|
|
|
0x8000 + offsetof(HLE::SharedMemory, unknown13) / 2,
|
|
|
|
0x8000 + offsetof(HLE::SharedMemory, unknown14) / 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Begin with a u16 denoting the number of structs.
|
|
|
|
WriteU16(DspPipe::Audio, static_cast<u16>(struct_addresses.size()));
|
|
|
|
// Then write the struct addresses.
|
|
|
|
for (u16 addr : struct_addresses) {
|
|
|
|
WriteU16(DspPipe::Audio, addr);
|
|
|
|
}
|
|
|
|
// Signal that we have data on this pipe.
|
2018-07-29 18:26:41 +00:00
|
|
|
if (auto service = dsp_dsp.lock()) {
|
|
|
|
service->SignalInterrupt(InterruptType::Pipe, DspPipe::Audio);
|
|
|
|
}
|
2017-12-20 18:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t DspHle::Impl::CurrentRegionIndex() const {
|
|
|
|
// The region with the higher frame counter is chosen unless there is wraparound.
|
|
|
|
// This function only returns a 0 or 1.
|
|
|
|
const u16 frame_counter_0 = dsp_memory.region_0.frame_counter;
|
|
|
|
const u16 frame_counter_1 = dsp_memory.region_1.frame_counter;
|
|
|
|
|
|
|
|
if (frame_counter_0 == 0xFFFFu && frame_counter_1 != 0xFFFEu) {
|
|
|
|
// Wraparound has occurred.
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame_counter_1 == 0xFFFFu && frame_counter_0 != 0xFFFEu) {
|
|
|
|
// Wraparound has occurred.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (frame_counter_0 > frame_counter_1) ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
HLE::SharedMemory& DspHle::Impl::ReadRegion() {
|
|
|
|
return CurrentRegionIndex() == 0 ? dsp_memory.region_0 : dsp_memory.region_1;
|
|
|
|
}
|
|
|
|
|
|
|
|
HLE::SharedMemory& DspHle::Impl::WriteRegion() {
|
|
|
|
return CurrentRegionIndex() != 0 ? dsp_memory.region_0 : dsp_memory.region_1;
|
|
|
|
}
|
|
|
|
|
|
|
|
StereoFrame16 DspHle::Impl::GenerateCurrentFrame() {
|
|
|
|
HLE::SharedMemory& read = ReadRegion();
|
|
|
|
HLE::SharedMemory& write = WriteRegion();
|
|
|
|
|
|
|
|
std::array<QuadFrame32, 3> intermediate_mixes = {};
|
|
|
|
|
|
|
|
// Generate intermediate mixes
|
2018-09-06 20:03:28 +00:00
|
|
|
for (std::size_t i = 0; i < HLE::num_sources; i++) {
|
2017-12-20 18:44:32 +00:00
|
|
|
write.source_statuses.status[i] =
|
|
|
|
sources[i].Tick(read.source_configurations.config[i], read.adpcm_coefficients.coeff[i]);
|
2018-09-06 20:03:28 +00:00
|
|
|
for (std::size_t mix = 0; mix < 3; mix++) {
|
2017-12-20 18:44:32 +00:00
|
|
|
sources[i].MixInto(intermediate_mixes[mix], mix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate final mix
|
|
|
|
write.dsp_status = mixers.Tick(read.dsp_configuration, read.intermediate_mix_samples,
|
|
|
|
write.intermediate_mix_samples, intermediate_mixes);
|
|
|
|
|
|
|
|
StereoFrame16 output_frame = mixers.GetOutput();
|
|
|
|
|
|
|
|
// Write current output frame to the shared memory region
|
2018-09-06 20:03:28 +00:00
|
|
|
for (std::size_t samplei = 0; samplei < output_frame.size(); samplei++) {
|
|
|
|
for (std::size_t channeli = 0; channeli < output_frame[0].size(); channeli++) {
|
2017-12-20 18:44:32 +00:00
|
|
|
write.final_samples.pcm16[samplei][channeli] = s16_le(output_frame[samplei][channeli]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output_frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DspHle::Impl::Tick() {
|
|
|
|
StereoFrame16 current_frame = {};
|
|
|
|
|
|
|
|
// TODO: Check dsp::DSP semaphore (which indicates emulated application has finished writing to
|
|
|
|
// shared memory region)
|
|
|
|
current_frame = GenerateCurrentFrame();
|
|
|
|
|
|
|
|
parent.OutputFrame(current_frame);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-23 21:08:14 +00:00
|
|
|
void DspHle::Impl::AudioTickCallback(s64 cycles_late) {
|
2017-12-20 18:44:32 +00:00
|
|
|
if (Tick()) {
|
|
|
|
// TODO(merry): Signal all the other interrupts as appropriate.
|
2018-07-29 18:26:41 +00:00
|
|
|
if (auto service = dsp_dsp.lock()) {
|
|
|
|
service->SignalInterrupt(InterruptType::Pipe, DspPipe::Audio);
|
2018-07-24 19:54:33 +00:00
|
|
|
// HACK(merry): Added to prevent regressions. Will remove soon.
|
2018-07-29 18:26:41 +00:00
|
|
|
service->SignalInterrupt(InterruptType::Pipe, DspPipe::Binary);
|
2018-07-24 19:54:33 +00:00
|
|
|
}
|
2017-12-20 18:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reschedule recurrent event
|
2018-10-27 19:53:20 +00:00
|
|
|
Core::Timing& timing = Core::System::GetInstance().CoreTiming();
|
|
|
|
timing.ScheduleEvent(audio_frame_ticks - cycles_late, tick_event);
|
2017-12-20 18:44:32 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 16:36:24 +00:00
|
|
|
DspHle::DspHle(Memory::MemorySystem& memory) : impl(std::make_unique<Impl>(*this, memory)) {}
|
2020-03-27 22:19:29 +00:00
|
|
|
DspHle::~DspHle() {
|
|
|
|
|
|
|
|
LOG_WARNING(Audio_DSP, "a1");
|
|
|
|
impl.reset();
|
|
|
|
LOG_WARNING(Audio_DSP, "a2");
|
|
|
|
}
|
2017-12-20 18:44:32 +00:00
|
|
|
|
2018-12-06 12:18:26 +00:00
|
|
|
u16 DspHle::RecvData(u32 register_number) {
|
|
|
|
return impl->RecvData(register_number);
|
2017-12-20 18:44:32 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:28:47 +00:00
|
|
|
bool DspHle::RecvDataIsReady(u32 register_number) const {
|
|
|
|
return impl->RecvDataIsReady(register_number);
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:35:07 +00:00
|
|
|
void DspHle::SetSemaphore(u16 semaphore_value) {
|
|
|
|
// Do nothing in HLE
|
|
|
|
}
|
|
|
|
|
2017-12-20 18:44:32 +00:00
|
|
|
std::vector<u8> DspHle::PipeRead(DspPipe pipe_number, u32 length) {
|
|
|
|
return impl->PipeRead(pipe_number, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t DspHle::GetPipeReadableSize(DspPipe pipe_number) const {
|
|
|
|
return impl->GetPipeReadableSize(pipe_number);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DspHle::PipeWrite(DspPipe pipe_number, const std::vector<u8>& buffer) {
|
|
|
|
impl->PipeWrite(pipe_number, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::array<u8, Memory::DSP_RAM_SIZE>& DspHle::GetDspMemory() {
|
|
|
|
return impl->GetDspMemory();
|
|
|
|
}
|
|
|
|
|
2018-07-29 18:26:41 +00:00
|
|
|
void DspHle::SetServiceToInterrupt(std::weak_ptr<DSP_DSP> dsp) {
|
|
|
|
impl->SetServiceToInterrupt(std::move(dsp));
|
2018-07-24 19:54:33 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 13:35:21 +00:00
|
|
|
void DspHle::LoadComponent(const std::vector<u8>& component_data) {
|
|
|
|
// HLE doesn't need DSP program. Only log some info here
|
|
|
|
LOG_INFO(Service_DSP, "Firmware hash: {:#018x}",
|
|
|
|
Common::ComputeHash64(component_data.data(), component_data.size()));
|
|
|
|
// Some versions of the firmware have the location of DSP structures listed here.
|
|
|
|
if (component_data.size() > 0x37C) {
|
|
|
|
LOG_INFO(Service_DSP, "Structures hash: {:#018x}",
|
|
|
|
Common::ComputeHash64(component_data.data() + 0x340, 60));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 14:50:55 +00:00
|
|
|
void DspHle::UnloadComponent() {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
2017-12-20 18:44:32 +00:00
|
|
|
} // namespace AudioCore
|