2018-12-06 12:14:54 +00:00
|
|
|
// Copyright 2018 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-12-06 14:09:14 +00:00
|
|
|
#include <array>
|
2018-12-20 00:45:22 +00:00
|
|
|
#include <atomic>
|
|
|
|
#include <thread>
|
2018-12-09 01:36:04 +00:00
|
|
|
#include <teakra/teakra.h>
|
2018-12-06 12:14:54 +00:00
|
|
|
#include "audio_core/lle/lle.h"
|
2018-12-06 12:42:07 +00:00
|
|
|
#include "common/assert.h"
|
2018-12-06 14:09:14 +00:00
|
|
|
#include "common/bit_field.h"
|
2018-12-06 12:42:07 +00:00
|
|
|
#include "common/swap.h"
|
2018-12-20 00:45:22 +00:00
|
|
|
#include "common/thread.h"
|
2018-12-06 14:09:14 +00:00
|
|
|
#include "core/core.h"
|
|
|
|
#include "core/core_timing.h"
|
2018-12-20 00:45:22 +00:00
|
|
|
#include "core/hle/lock.h"
|
2018-12-06 13:10:00 +00:00
|
|
|
#include "core/hle/service/dsp/dsp_dsp.h"
|
2018-12-06 12:14:54 +00:00
|
|
|
|
|
|
|
namespace AudioCore {
|
|
|
|
|
2018-12-06 14:09:14 +00:00
|
|
|
enum class SegmentType : u8 {
|
|
|
|
ProgramA = 0,
|
|
|
|
ProgramB = 1,
|
|
|
|
Data = 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
class Dsp1 {
|
|
|
|
public:
|
2018-12-09 01:36:04 +00:00
|
|
|
explicit Dsp1(const std::vector<u8>& raw);
|
2018-12-06 14:09:14 +00:00
|
|
|
|
|
|
|
struct Header {
|
|
|
|
std::array<u8, 0x100> signature;
|
2018-12-09 01:36:04 +00:00
|
|
|
std::array<u8, 0x4> magic;
|
2018-12-06 14:09:14 +00:00
|
|
|
u32_le binary_size;
|
|
|
|
u16_le memory_layout;
|
|
|
|
INSERT_PADDING_BYTES(3);
|
|
|
|
SegmentType special_segment_type;
|
|
|
|
u8 num_segments;
|
|
|
|
union {
|
|
|
|
BitField<0, 1, u8> recv_data_on_start;
|
|
|
|
BitField<1, 1, u8> load_special_segment;
|
|
|
|
};
|
|
|
|
u32_le special_segment_address;
|
|
|
|
u32_le special_segment_size;
|
|
|
|
u64_le zero;
|
|
|
|
struct Segment {
|
|
|
|
u32_le offset;
|
|
|
|
u32_le address;
|
|
|
|
u32_le size;
|
|
|
|
INSERT_PADDING_BYTES(3);
|
|
|
|
SegmentType memory_type;
|
|
|
|
std::array<u8, 0x20> sha256;
|
|
|
|
};
|
|
|
|
std::array<Segment, 10> segments;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(Header) == 0x300);
|
|
|
|
|
|
|
|
struct Segment {
|
|
|
|
std::vector<u8> data;
|
|
|
|
SegmentType memory_type;
|
|
|
|
u32 target;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<Segment> segments;
|
|
|
|
bool recv_data_on_start;
|
|
|
|
};
|
|
|
|
|
|
|
|
Dsp1::Dsp1(const std::vector<u8>& raw) {
|
|
|
|
Header header;
|
|
|
|
std::memcpy(&header, raw.data(), sizeof(header));
|
|
|
|
recv_data_on_start = header.recv_data_on_start != 0;
|
|
|
|
for (u32 i = 0; i < header.num_segments; ++i) {
|
|
|
|
Segment segment;
|
|
|
|
segment.data =
|
|
|
|
std::vector<u8>(raw.begin() + header.segments[i].offset,
|
|
|
|
raw.begin() + header.segments[i].offset + header.segments[i].size);
|
|
|
|
segment.memory_type = header.segments[i].memory_type;
|
|
|
|
segment.target = header.segments[i].address;
|
2018-12-09 01:36:04 +00:00
|
|
|
segments.push_back(std::move(segment));
|
2018-12-06 14:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:42:07 +00:00
|
|
|
struct PipeStatus {
|
|
|
|
u16_le waddress;
|
|
|
|
u16_le bsize;
|
|
|
|
u16_le read_bptr;
|
|
|
|
u16_le write_bptr;
|
|
|
|
u8 slot_index;
|
|
|
|
u8 flags;
|
2018-12-09 01:36:04 +00:00
|
|
|
|
|
|
|
static constexpr u16 WrapBit = 0x8000;
|
|
|
|
static constexpr u16 PtrMask = 0x7FFF;
|
|
|
|
|
|
|
|
bool IsFull() const {
|
|
|
|
return (read_bptr ^ write_bptr) == WrapBit;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsEmpty() const {
|
|
|
|
return (read_bptr ^ write_bptr) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* IsWrapped: Are read and write pointers not in the same pass.
|
|
|
|
* false: ----[xxxx]----
|
|
|
|
* true: xxxx]----[xxxx (data is wrapping around the end)
|
|
|
|
*/
|
|
|
|
bool IsWrapped() const {
|
|
|
|
return (read_bptr ^ write_bptr) >= WrapBit;
|
|
|
|
}
|
2018-12-06 12:42:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static_assert(sizeof(PipeStatus) == 10);
|
|
|
|
|
|
|
|
enum class PipeDirection : u8 {
|
|
|
|
DSPtoCPU = 0,
|
|
|
|
CPUtoDSP = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
static u8 PipeIndexToSlotIndex(u8 pipe_index, PipeDirection direction) {
|
2018-12-09 01:36:04 +00:00
|
|
|
return (pipe_index << 1) + static_cast<u8>(direction);
|
2018-12-06 12:42:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:14:54 +00:00
|
|
|
struct DspLle::Impl final {
|
2018-12-20 00:45:22 +00:00
|
|
|
Impl(bool multithread) : multithread(multithread) {
|
2018-12-06 14:09:14 +00:00
|
|
|
teakra_slice_event = Core::System::GetInstance().CoreTiming().RegisterEvent(
|
|
|
|
"DSP slice", [this](u64, int late) { TeakraSliceEvent(static_cast<u64>(late)); });
|
|
|
|
}
|
|
|
|
|
2018-12-20 00:45:22 +00:00
|
|
|
~Impl() {
|
|
|
|
StopTeakraThread();
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:14:54 +00:00
|
|
|
Teakra::Teakra teakra;
|
2018-12-06 12:42:07 +00:00
|
|
|
u16 pipe_base_waddr = 0;
|
2018-12-06 12:22:31 +00:00
|
|
|
|
2018-12-06 13:10:00 +00:00
|
|
|
bool semaphore_signaled = false;
|
|
|
|
bool data_signaled = false;
|
|
|
|
|
2018-12-06 14:09:14 +00:00
|
|
|
Core::TimingEventType* teakra_slice_event;
|
2018-12-20 00:45:22 +00:00
|
|
|
std::atomic<bool> loaded = false;
|
|
|
|
|
|
|
|
const bool multithread;
|
|
|
|
std::thread teakra_thread;
|
|
|
|
Common::Barrier teakra_slice_barrier{2};
|
|
|
|
std::atomic<bool> stop_signal = false;
|
|
|
|
std::size_t stop_generation;
|
2018-12-06 14:09:14 +00:00
|
|
|
|
2018-12-09 01:36:04 +00:00
|
|
|
static constexpr u32 DspDataOffset = 0x40000;
|
2020-06-08 00:06:22 +00:00
|
|
|
static constexpr u32 TeakraSlice = 16384;
|
2018-12-09 01:36:04 +00:00
|
|
|
|
2018-12-20 00:45:22 +00:00
|
|
|
void TeakraThread() {
|
|
|
|
while (true) {
|
|
|
|
teakra.Run(TeakraSlice);
|
|
|
|
teakra_slice_barrier.Sync();
|
|
|
|
if (stop_signal) {
|
|
|
|
if (stop_generation == teakra_slice_barrier.Generation())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stop_signal = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StopTeakraThread() {
|
|
|
|
if (teakra_thread.joinable()) {
|
|
|
|
stop_generation = teakra_slice_barrier.Generation() + 1;
|
|
|
|
stop_signal = true;
|
|
|
|
teakra_slice_barrier.Sync();
|
|
|
|
teakra_thread.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:22:31 +00:00
|
|
|
void RunTeakraSlice() {
|
2018-12-20 00:45:22 +00:00
|
|
|
if (multithread) {
|
|
|
|
teakra_slice_barrier.Sync();
|
|
|
|
} else {
|
|
|
|
teakra.Run(TeakraSlice);
|
|
|
|
}
|
2018-12-06 12:22:31 +00:00
|
|
|
}
|
2018-12-06 12:42:07 +00:00
|
|
|
|
2018-12-06 14:09:14 +00:00
|
|
|
void TeakraSliceEvent(u64 late) {
|
|
|
|
RunTeakraSlice();
|
|
|
|
u64 next = TeakraSlice * 2; // DSP runs at clock rate half of the CPU rate
|
|
|
|
if (next < late)
|
|
|
|
next = 0;
|
|
|
|
else
|
|
|
|
next -= late;
|
|
|
|
Core::System::GetInstance().CoreTiming().ScheduleEvent(next, teakra_slice_event, 0);
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:42:07 +00:00
|
|
|
u8* GetDspDataPointer(u32 baddr) {
|
|
|
|
auto& memory = teakra.GetDspMemory();
|
2018-12-09 01:36:04 +00:00
|
|
|
return &memory[DspDataOffset + baddr];
|
2018-12-06 12:42:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-15 18:36:33 +00:00
|
|
|
const u8* GetDspDataPointer(u32 baddr) const {
|
|
|
|
auto& memory = teakra.GetDspMemory();
|
|
|
|
return &memory[DspDataOffset + baddr];
|
|
|
|
}
|
|
|
|
|
|
|
|
PipeStatus GetPipeStatus(u8 pipe_index, PipeDirection direction) const {
|
2018-12-06 12:42:07 +00:00
|
|
|
u8 slot_index = PipeIndexToSlotIndex(pipe_index, direction);
|
|
|
|
PipeStatus pipe_status;
|
|
|
|
std::memcpy(&pipe_status,
|
|
|
|
GetDspDataPointer(pipe_base_waddr * 2 + slot_index * sizeof(PipeStatus)),
|
|
|
|
sizeof(PipeStatus));
|
|
|
|
ASSERT(pipe_status.slot_index == slot_index);
|
|
|
|
return pipe_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdatePipeStatus(const PipeStatus& pipe_status) {
|
|
|
|
u8 slot_index = pipe_status.slot_index;
|
|
|
|
u8* status_address =
|
|
|
|
GetDspDataPointer(pipe_base_waddr * 2 + slot_index * sizeof(PipeStatus));
|
|
|
|
if (slot_index % 2 == 0) {
|
|
|
|
std::memcpy(status_address + 4, &pipe_status.read_bptr, sizeof(u16));
|
|
|
|
} else {
|
|
|
|
std::memcpy(status_address + 6, &pipe_status.write_bptr, sizeof(u16));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WritePipe(u8 pipe_index, const std::vector<u8>& data) {
|
|
|
|
PipeStatus pipe_status = GetPipeStatus(pipe_index, PipeDirection::CPUtoDSP);
|
|
|
|
bool need_update = false;
|
|
|
|
const u8* buffer_ptr = data.data();
|
2018-12-09 01:36:04 +00:00
|
|
|
u16 bsize = static_cast<u16>(data.size());
|
2018-12-06 12:42:07 +00:00
|
|
|
while (bsize != 0) {
|
2018-12-09 01:36:04 +00:00
|
|
|
ASSERT_MSG(!pipe_status.IsFull(), "Pipe is Full");
|
2018-12-06 12:42:07 +00:00
|
|
|
u16 write_bend;
|
2018-12-09 01:36:04 +00:00
|
|
|
if (pipe_status.IsWrapped())
|
|
|
|
write_bend = pipe_status.read_bptr & PipeStatus::PtrMask;
|
2018-12-06 12:42:07 +00:00
|
|
|
else
|
|
|
|
write_bend = pipe_status.bsize;
|
2018-12-09 01:36:04 +00:00
|
|
|
u16 write_bbegin = pipe_status.write_bptr & PipeStatus::PtrMask;
|
2018-12-06 12:42:07 +00:00
|
|
|
ASSERT_MSG(write_bend > write_bbegin,
|
|
|
|
"Pipe is in inconsistent state: end {:04X} <= begin {:04X}, size {:04X}",
|
|
|
|
write_bend, write_bbegin, pipe_status.bsize);
|
|
|
|
u16 write_bsize = std::min<u16>(bsize, write_bend - write_bbegin);
|
|
|
|
std::memcpy(GetDspDataPointer(pipe_status.waddress * 2 + write_bbegin), buffer_ptr,
|
|
|
|
write_bsize);
|
|
|
|
buffer_ptr += write_bsize;
|
|
|
|
pipe_status.write_bptr += write_bsize;
|
|
|
|
bsize -= write_bsize;
|
2018-12-09 01:36:04 +00:00
|
|
|
ASSERT_MSG((pipe_status.write_bptr & PipeStatus::PtrMask) <= pipe_status.bsize,
|
2018-12-06 12:42:07 +00:00
|
|
|
"Pipe is in inconsistent state: write > size");
|
2018-12-09 01:36:04 +00:00
|
|
|
if ((pipe_status.write_bptr & PipeStatus::PtrMask) == pipe_status.bsize) {
|
|
|
|
pipe_status.write_bptr &= PipeStatus::WrapBit;
|
|
|
|
pipe_status.write_bptr ^= PipeStatus::WrapBit;
|
2018-12-06 12:42:07 +00:00
|
|
|
}
|
|
|
|
need_update = true;
|
|
|
|
}
|
|
|
|
if (need_update) {
|
|
|
|
UpdatePipeStatus(pipe_status);
|
|
|
|
while (!teakra.SendDataIsEmpty(2))
|
|
|
|
RunTeakraSlice();
|
|
|
|
teakra.SendData(2, pipe_status.slot_index);
|
|
|
|
}
|
|
|
|
}
|
2018-12-06 12:49:07 +00:00
|
|
|
|
|
|
|
std::vector<u8> ReadPipe(u8 pipe_index, u16 bsize) {
|
|
|
|
PipeStatus pipe_status = GetPipeStatus(pipe_index, PipeDirection::DSPtoCPU);
|
|
|
|
bool need_update = false;
|
|
|
|
std::vector<u8> data(bsize);
|
|
|
|
u8* buffer_ptr = data.data();
|
|
|
|
while (bsize != 0) {
|
2018-12-09 01:36:04 +00:00
|
|
|
ASSERT_MSG(!pipe_status.IsEmpty(), "Pipe is empty");
|
2018-12-06 12:49:07 +00:00
|
|
|
u16 read_bend;
|
2018-12-09 01:36:04 +00:00
|
|
|
if (pipe_status.IsWrapped()) {
|
2018-12-06 12:49:07 +00:00
|
|
|
read_bend = pipe_status.bsize;
|
|
|
|
} else {
|
2018-12-09 01:36:04 +00:00
|
|
|
read_bend = pipe_status.write_bptr & PipeStatus::PtrMask;
|
2018-12-06 12:49:07 +00:00
|
|
|
}
|
2018-12-09 01:36:04 +00:00
|
|
|
u16 read_bbegin = pipe_status.read_bptr & PipeStatus::PtrMask;
|
2018-12-06 12:49:07 +00:00
|
|
|
ASSERT(read_bend > read_bbegin);
|
|
|
|
u16 read_bsize = std::min<u16>(bsize, read_bend - read_bbegin);
|
|
|
|
std::memcpy(buffer_ptr, GetDspDataPointer(pipe_status.waddress * 2 + read_bbegin),
|
|
|
|
read_bsize);
|
|
|
|
buffer_ptr += read_bsize;
|
|
|
|
pipe_status.read_bptr += read_bsize;
|
|
|
|
bsize -= read_bsize;
|
2018-12-09 01:36:04 +00:00
|
|
|
ASSERT_MSG((pipe_status.read_bptr & PipeStatus::PtrMask) <= pipe_status.bsize,
|
2018-12-06 12:49:07 +00:00
|
|
|
"Pipe is in inconsistent state: read > size");
|
2018-12-09 01:36:04 +00:00
|
|
|
if ((pipe_status.read_bptr & PipeStatus::PtrMask) == pipe_status.bsize) {
|
|
|
|
pipe_status.read_bptr &= PipeStatus::WrapBit;
|
|
|
|
pipe_status.read_bptr ^= PipeStatus::WrapBit;
|
2018-12-06 12:49:07 +00:00
|
|
|
}
|
|
|
|
need_update = true;
|
|
|
|
}
|
|
|
|
if (need_update) {
|
|
|
|
UpdatePipeStatus(pipe_status);
|
|
|
|
while (!teakra.SendDataIsEmpty(2))
|
|
|
|
RunTeakraSlice();
|
|
|
|
teakra.SendData(2, pipe_status.slot_index);
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
2018-12-15 18:36:33 +00:00
|
|
|
u16 GetPipeReadableSize(u8 pipe_index) const {
|
2018-12-06 12:49:07 +00:00
|
|
|
PipeStatus pipe_status = GetPipeStatus(pipe_index, PipeDirection::DSPtoCPU);
|
|
|
|
u16 size = pipe_status.write_bptr - pipe_status.read_bptr;
|
2018-12-09 01:36:04 +00:00
|
|
|
if (pipe_status.IsWrapped()) {
|
2018-12-06 12:49:07 +00:00
|
|
|
size += pipe_status.bsize;
|
|
|
|
}
|
2018-12-09 01:36:04 +00:00
|
|
|
return size & PipeStatus::PtrMask;
|
2018-12-06 12:49:07 +00:00
|
|
|
}
|
2018-12-06 14:09:14 +00:00
|
|
|
|
|
|
|
void LoadComponent(const std::vector<u8>& buffer) {
|
2018-12-06 14:50:55 +00:00
|
|
|
if (loaded) {
|
|
|
|
LOG_ERROR(Audio_DSP, "Component already loaded!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
teakra.Reset();
|
|
|
|
|
2018-12-06 14:09:14 +00:00
|
|
|
Dsp1 dsp(buffer);
|
|
|
|
auto& dsp_memory = teakra.GetDspMemory();
|
|
|
|
u8* program = dsp_memory.data();
|
2018-12-09 01:36:04 +00:00
|
|
|
u8* data = dsp_memory.data() + DspDataOffset;
|
2018-12-06 14:09:14 +00:00
|
|
|
for (const auto& segment : dsp.segments) {
|
|
|
|
if (segment.memory_type == SegmentType::ProgramA ||
|
|
|
|
segment.memory_type == SegmentType::ProgramB) {
|
|
|
|
std::memcpy(program + segment.target * 2, segment.data.data(), segment.data.size());
|
|
|
|
} else if (segment.memory_type == SegmentType::Data) {
|
|
|
|
std::memcpy(data + segment.target * 2, segment.data.data(), segment.data.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: load special segment
|
|
|
|
|
|
|
|
Core::System::GetInstance().CoreTiming().ScheduleEvent(TeakraSlice, teakra_slice_event, 0);
|
|
|
|
|
2018-12-20 00:45:22 +00:00
|
|
|
if (multithread) {
|
|
|
|
teakra_thread = std::thread(&Impl::TeakraThread, this);
|
|
|
|
}
|
|
|
|
|
2018-12-06 14:09:14 +00:00
|
|
|
// Wait for initialization
|
|
|
|
if (dsp.recv_data_on_start) {
|
2018-12-09 01:36:04 +00:00
|
|
|
for (u8 i = 0; i < 3; ++i) {
|
|
|
|
do {
|
|
|
|
while (!teakra.RecvDataIsReady(i))
|
|
|
|
RunTeakraSlice();
|
|
|
|
} while (teakra.RecvData(i) != 1);
|
2018-12-06 14:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get pipe base address
|
|
|
|
while (!teakra.RecvDataIsReady(2))
|
|
|
|
RunTeakraSlice();
|
|
|
|
pipe_base_waddr = teakra.RecvData(2);
|
2018-12-06 14:50:55 +00:00
|
|
|
|
|
|
|
loaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnloadComponent() {
|
|
|
|
if (!loaded) {
|
|
|
|
LOG_ERROR(Audio_DSP, "Component not loaded!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-20 00:45:22 +00:00
|
|
|
loaded = false;
|
|
|
|
|
2018-12-15 18:36:33 +00:00
|
|
|
// Send finalization signal via command/reply register 2
|
2018-12-09 01:36:04 +00:00
|
|
|
constexpr u16 FinalizeSignal = 0x8000;
|
2018-12-06 14:50:55 +00:00
|
|
|
while (!teakra.SendDataIsEmpty(2))
|
|
|
|
RunTeakraSlice();
|
|
|
|
|
2018-12-09 01:36:04 +00:00
|
|
|
teakra.SendData(2, FinalizeSignal);
|
2018-12-06 14:50:55 +00:00
|
|
|
|
|
|
|
// Wait for completion
|
|
|
|
while (!teakra.RecvDataIsReady(2))
|
|
|
|
RunTeakraSlice();
|
|
|
|
|
|
|
|
teakra.RecvData(2); // discard the value
|
|
|
|
|
|
|
|
Core::System::GetInstance().CoreTiming().UnscheduleEvent(teakra_slice_event, 0);
|
2018-12-20 00:45:22 +00:00
|
|
|
StopTeakraThread();
|
2018-12-06 14:09:14 +00:00
|
|
|
}
|
2018-12-06 12:14:54 +00:00
|
|
|
};
|
|
|
|
|
2018-12-06 12:22:31 +00:00
|
|
|
u16 DspLle::RecvData(u32 register_number) {
|
|
|
|
while (!impl->teakra.RecvDataIsReady(register_number)) {
|
|
|
|
impl->RunTeakraSlice();
|
|
|
|
}
|
|
|
|
return impl->teakra.RecvData(static_cast<u8>(register_number));
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:30:10 +00:00
|
|
|
bool DspLle::RecvDataIsReady(u32 register_number) const {
|
|
|
|
return impl->teakra.RecvDataIsReady(register_number);
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:35:07 +00:00
|
|
|
void DspLle::SetSemaphore(u16 semaphore_value) {
|
|
|
|
impl->teakra.SetSemaphore(semaphore_value);
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:49:07 +00:00
|
|
|
std::vector<u8> DspLle::PipeRead(DspPipe pipe_number, u32 length) {
|
|
|
|
return impl->ReadPipe(static_cast<u8>(pipe_number), static_cast<u16>(length));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t DspLle::GetPipeReadableSize(DspPipe pipe_number) const {
|
|
|
|
return impl->GetPipeReadableSize(static_cast<u8>(pipe_number));
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:42:07 +00:00
|
|
|
void DspLle::PipeWrite(DspPipe pipe_number, const std::vector<u8>& buffer) {
|
|
|
|
impl->WritePipe(static_cast<u8>(pipe_number), buffer);
|
|
|
|
}
|
|
|
|
|
2018-12-06 13:10:00 +00:00
|
|
|
std::array<u8, Memory::DSP_RAM_SIZE>& DspLle::GetDspMemory() {
|
|
|
|
return impl->teakra.GetDspMemory();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DspLle::SetServiceToInterrupt(std::weak_ptr<Service::DSP::DSP_DSP> dsp) {
|
2018-12-20 00:45:22 +00:00
|
|
|
impl->teakra.SetRecvDataHandler(0, [this, dsp]() {
|
|
|
|
if (!impl->loaded)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::lock_guard lock(HLE::g_hle_lock);
|
2018-12-06 13:10:00 +00:00
|
|
|
if (auto locked = dsp.lock()) {
|
|
|
|
locked->SignalInterrupt(Service::DSP::DSP_DSP::InterruptType::Zero,
|
|
|
|
static_cast<DspPipe>(0));
|
|
|
|
}
|
|
|
|
});
|
2018-12-20 00:45:22 +00:00
|
|
|
impl->teakra.SetRecvDataHandler(1, [this, dsp]() {
|
|
|
|
if (!impl->loaded)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::lock_guard lock(HLE::g_hle_lock);
|
2018-12-06 13:10:00 +00:00
|
|
|
if (auto locked = dsp.lock()) {
|
|
|
|
locked->SignalInterrupt(Service::DSP::DSP_DSP::InterruptType::One,
|
|
|
|
static_cast<DspPipe>(0));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
auto ProcessPipeEvent = [this, dsp](bool event_from_data) {
|
2018-12-06 16:12:47 +00:00
|
|
|
if (!impl->loaded)
|
|
|
|
return;
|
|
|
|
|
2018-12-06 13:10:00 +00:00
|
|
|
auto& teakra = impl->teakra;
|
|
|
|
if (event_from_data) {
|
|
|
|
impl->data_signaled = true;
|
|
|
|
} else {
|
|
|
|
if ((teakra.GetSemaphore() & 0x8000) == 0)
|
|
|
|
return;
|
|
|
|
impl->semaphore_signaled = true;
|
|
|
|
}
|
|
|
|
if (impl->semaphore_signaled && impl->data_signaled) {
|
|
|
|
impl->semaphore_signaled = impl->data_signaled = false;
|
|
|
|
u16 slot = teakra.RecvData(2);
|
|
|
|
u16 side = slot % 2;
|
|
|
|
u16 pipe = slot / 2;
|
|
|
|
ASSERT(pipe < 16);
|
|
|
|
if (side != static_cast<u16>(PipeDirection::DSPtoCPU))
|
|
|
|
return;
|
|
|
|
if (pipe == 0) {
|
|
|
|
// pipe 0 is for debug. 3DS automatically drains this pipe and discards the data
|
|
|
|
impl->ReadPipe(pipe, impl->GetPipeReadableSize(pipe));
|
|
|
|
} else {
|
2018-12-20 00:45:22 +00:00
|
|
|
std::lock_guard lock(HLE::g_hle_lock);
|
2018-12-06 13:10:00 +00:00
|
|
|
if (auto locked = dsp.lock()) {
|
|
|
|
locked->SignalInterrupt(Service::DSP::DSP_DSP::InterruptType::Pipe,
|
|
|
|
static_cast<DspPipe>(pipe));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
impl->teakra.SetRecvDataHandler(2, [ProcessPipeEvent]() { ProcessPipeEvent(true); });
|
|
|
|
impl->teakra.SetSemaphoreHandler([ProcessPipeEvent]() { ProcessPipeEvent(false); });
|
|
|
|
}
|
|
|
|
|
2018-12-06 14:09:14 +00:00
|
|
|
void DspLle::LoadComponent(const std::vector<u8>& buffer) {
|
|
|
|
impl->LoadComponent(buffer);
|
|
|
|
}
|
|
|
|
|
2018-12-06 14:50:55 +00:00
|
|
|
void DspLle::UnloadComponent() {
|
|
|
|
impl->UnloadComponent();
|
|
|
|
}
|
|
|
|
|
2018-12-20 00:45:22 +00:00
|
|
|
DspLle::DspLle(Memory::MemorySystem& memory, bool multithread)
|
|
|
|
: impl(std::make_unique<Impl>(multithread)) {
|
2018-12-06 17:19:58 +00:00
|
|
|
Teakra::AHBMCallback ahbm;
|
|
|
|
ahbm.read8 = [&memory](u32 address) -> u8 {
|
|
|
|
return *memory.GetFCRAMPointer(address - Memory::FCRAM_PADDR);
|
|
|
|
};
|
|
|
|
ahbm.write8 = [&memory](u32 address, u8 value) {
|
|
|
|
*memory.GetFCRAMPointer(address - Memory::FCRAM_PADDR) = value;
|
|
|
|
};
|
|
|
|
impl->teakra.SetAHBMCallback(ahbm);
|
2020-01-28 14:19:36 +00:00
|
|
|
impl->teakra.SetAudioCallback(
|
|
|
|
[this](std::array<s16, 2> sample) { OutputSample(std::move(sample)); });
|
2018-12-06 17:19:58 +00:00
|
|
|
}
|
2018-12-06 12:14:54 +00:00
|
|
|
DspLle::~DspLle() = default;
|
|
|
|
|
|
|
|
} // namespace AudioCore
|