mirror of
				https://git.suyu.dev/suyu/suyu
				synced 2025-11-04 00:49:02 -06:00 
			
		
		
		
	AudioCore: Skeleton Implementation
This commit: * Adds a new subproject, audio_core. * Defines structures that exist in DSP shared memory. * Hooks up various other parts of the emulator into audio core. This sets the foundation for a later HLE DSP implementation.
This commit is contained in:
		@@ -2,6 +2,8 @@
 | 
			
		||||
// Licensed under GPLv2 or any later version
 | 
			
		||||
// Refer to the license.txt file included.
 | 
			
		||||
 | 
			
		||||
#include "audio_core/hle/pipe.h"
 | 
			
		||||
 | 
			
		||||
#include "common/logging/log.h"
 | 
			
		||||
 | 
			
		||||
#include "core/hle/kernel/event.h"
 | 
			
		||||
@@ -14,17 +16,30 @@ namespace DSP_DSP {
 | 
			
		||||
 | 
			
		||||
static u32 read_pipe_count;
 | 
			
		||||
static Kernel::SharedPtr<Kernel::Event> semaphore_event;
 | 
			
		||||
static Kernel::SharedPtr<Kernel::Event> interrupt_event;
 | 
			
		||||
 | 
			
		||||
void SignalInterrupt() {
 | 
			
		||||
    // TODO(bunnei): This is just a stub, it does not do anything other than signal to the emulated
 | 
			
		||||
    // application that a DSP interrupt occurred, without specifying which one. Since we do not
 | 
			
		||||
    // emulate the DSP yet (and how it works is largely unknown), this is a work around to get games
 | 
			
		||||
    // that check the DSP interrupt signal event to run. We should figure out the different types of
 | 
			
		||||
    // DSP interrupts, and trigger them at the appropriate times.
 | 
			
		||||
struct PairHash {
 | 
			
		||||
    template <typename T, typename U>
 | 
			
		||||
    std::size_t operator()(const std::pair<T, U> &x) const {
 | 
			
		||||
        // TODO(yuriks): Replace with better hash combining function.
 | 
			
		||||
        return std::hash<T>()(x.first) ^ std::hash<U>()(x.second);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
    if (interrupt_event != 0)
 | 
			
		||||
        interrupt_event->Signal();
 | 
			
		||||
/// Map of (audio interrupt number, channel number) to Kernel::Events. See: RegisterInterruptEvents
 | 
			
		||||
static std::unordered_map<std::pair<u32, u32>, Kernel::SharedPtr<Kernel::Event>, PairHash> interrupt_events;
 | 
			
		||||
 | 
			
		||||
// DSP Interrupts:
 | 
			
		||||
// Interrupt #2 occurs every frame tick. Userland programs normally have a thread that's waiting
 | 
			
		||||
// for an interrupt event. Immediately after this interrupt event, userland normally updates the
 | 
			
		||||
// state in the next region and increments the relevant frame counter by two.
 | 
			
		||||
void SignalAllInterrupts() {
 | 
			
		||||
    // HACK: The other interrupts have currently unknown purpose, we trigger them each tick in any case.
 | 
			
		||||
    for (auto& interrupt_event : interrupt_events)
 | 
			
		||||
        interrupt_event.second->Signal();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SignalInterrupt(u32 interrupt, u32 channel) {
 | 
			
		||||
    interrupt_events[std::make_pair(interrupt, channel)]->Signal();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -43,7 +58,7 @@ static void ConvertProcessAddressFromDspDram(Service::Interface* self) {
 | 
			
		||||
    cmd_buff[1] = 0; // No error
 | 
			
		||||
    cmd_buff[2] = (addr << 1) + (Memory::DSP_RAM_VADDR + 0x40000);
 | 
			
		||||
 | 
			
		||||
    LOG_WARNING(Service_DSP, "(STUBBED) called with address 0x%08X", addr);
 | 
			
		||||
    LOG_TRACE(Service_DSP, "addr=0x%08X", addr);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -121,8 +136,8 @@ static void FlushDataCache(Service::Interface* self) {
 | 
			
		||||
/**
 | 
			
		||||
 * DSP_DSP::RegisterInterruptEvents service function
 | 
			
		||||
 *  Inputs:
 | 
			
		||||
 *      1 : Parameter 0 (purpose unknown)
 | 
			
		||||
 *      2 : Parameter 1 (purpose unknown)
 | 
			
		||||
 *      1 : Interrupt Number
 | 
			
		||||
 *      2 : Channel Number
 | 
			
		||||
 *      4 : Interrupt event handle
 | 
			
		||||
 *  Outputs:
 | 
			
		||||
 *      1 : Result of function, 0 on success, otherwise error code
 | 
			
		||||
@@ -130,22 +145,24 @@ static void FlushDataCache(Service::Interface* self) {
 | 
			
		||||
static void RegisterInterruptEvents(Service::Interface* self) {
 | 
			
		||||
    u32* cmd_buff = Kernel::GetCommandBuffer();
 | 
			
		||||
 | 
			
		||||
    u32 param0 = cmd_buff[1];
 | 
			
		||||
    u32 param1 = cmd_buff[2];
 | 
			
		||||
    u32 interrupt = cmd_buff[1];
 | 
			
		||||
    u32 channel = cmd_buff[2];
 | 
			
		||||
    u32 event_handle = cmd_buff[4];
 | 
			
		||||
 | 
			
		||||
    auto evt = Kernel::g_handle_table.Get<Kernel::Event>(cmd_buff[4]);
 | 
			
		||||
    if (evt != nullptr) {
 | 
			
		||||
        interrupt_event = evt;
 | 
			
		||||
        cmd_buff[1] = 0; // No error
 | 
			
		||||
    if (event_handle) {
 | 
			
		||||
        auto evt = Kernel::g_handle_table.Get<Kernel::Event>(cmd_buff[4]);
 | 
			
		||||
        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);
 | 
			
		||||
        } else {
 | 
			
		||||
            cmd_buff[1] = -1;
 | 
			
		||||
            LOG_ERROR(Service_DSP, "Invalid event handle! interrupt=%u, channel=%u, event_handle=0x%08X", interrupt, channel, event_handle);
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        LOG_ERROR(Service_DSP, "called with invalid handle=%08X", cmd_buff[4]);
 | 
			
		||||
 | 
			
		||||
        // TODO(yuriks): An error should be returned from SendSyncRequest, not in the cmdbuf
 | 
			
		||||
        cmd_buff[1] = -1;
 | 
			
		||||
        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_WARNING(Service_DSP, "(STUBBED) called param0=%u, param1=%u, event_handle=0x%08X", param0, param1, event_handle);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -158,8 +175,6 @@ static void RegisterInterruptEvents(Service::Interface* self) {
 | 
			
		||||
static void SetSemaphore(Service::Interface* self) {
 | 
			
		||||
    u32* cmd_buff = Kernel::GetCommandBuffer();
 | 
			
		||||
 | 
			
		||||
    SignalInterrupt();
 | 
			
		||||
 | 
			
		||||
    cmd_buff[1] = 0; // No error
 | 
			
		||||
 | 
			
		||||
    LOG_WARNING(Service_DSP, "(STUBBED) called");
 | 
			
		||||
@@ -168,9 +183,9 @@ static void SetSemaphore(Service::Interface* self) {
 | 
			
		||||
/**
 | 
			
		||||
 * DSP_DSP::WriteProcessPipe service function
 | 
			
		||||
 *  Inputs:
 | 
			
		||||
 *      1 : Number
 | 
			
		||||
 *      1 : Channel
 | 
			
		||||
 *      2 : Size
 | 
			
		||||
 *      3 : (size <<14) | 0x402
 | 
			
		||||
 *      3 : (size << 14) | 0x402
 | 
			
		||||
 *      4 : Buffer
 | 
			
		||||
 *  Outputs:
 | 
			
		||||
 *      0 : Return header
 | 
			
		||||
@@ -179,21 +194,42 @@ static void SetSemaphore(Service::Interface* self) {
 | 
			
		||||
static void WriteProcessPipe(Service::Interface* self) {
 | 
			
		||||
    u32* cmd_buff = Kernel::GetCommandBuffer();
 | 
			
		||||
 | 
			
		||||
    u32 number   = cmd_buff[1];
 | 
			
		||||
    u32 channel  = cmd_buff[1];
 | 
			
		||||
    u32 size     = cmd_buff[2];
 | 
			
		||||
    u32 new_size = cmd_buff[3];
 | 
			
		||||
    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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    std::vector<u8> message(size);
 | 
			
		||||
 | 
			
		||||
    for (size_t i = 0; i < size; i++) {
 | 
			
		||||
        message[i] = Memory::Read8(buffer + i);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    DSP::HLE::PipeWrite(channel, message);
 | 
			
		||||
 | 
			
		||||
    cmd_buff[1] = RESULT_SUCCESS.raw; // No error
 | 
			
		||||
 | 
			
		||||
    LOG_WARNING(Service_DSP, "(STUBBED) called number=%u, size=0x%X, new_size=0x%X, buffer=0x%08X",
 | 
			
		||||
                number, size, new_size, buffer);
 | 
			
		||||
    LOG_TRACE(Service_DSP, "channel=%u, size=0x%X, buffer=0x%08X", channel, size, buffer);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * DSP_DSP::ReadPipeIfPossible service function
 | 
			
		||||
 *      A pipe is a means of communication between the ARM11 and DSP that occurs on
 | 
			
		||||
 *      hardware by writing to/reading from the DSP registers at 0x10203000.
 | 
			
		||||
 *      Pipes are used for initialisation. See also DSP::HLE::PipeRead.
 | 
			
		||||
 *  Inputs:
 | 
			
		||||
 *      1 : Unknown
 | 
			
		||||
 *      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
 | 
			
		||||
@@ -204,35 +240,25 @@ static void WriteProcessPipe(Service::Interface* self) {
 | 
			
		||||
static void ReadPipeIfPossible(Service::Interface* self) {
 | 
			
		||||
    u32* cmd_buff = Kernel::GetCommandBuffer();
 | 
			
		||||
 | 
			
		||||
    u32 unk1 = cmd_buff[1];
 | 
			
		||||
    u32 pipe = cmd_buff[1];
 | 
			
		||||
    u32 unk2 = cmd_buff[2];
 | 
			
		||||
    u32 size = cmd_buff[3] & 0xFFFF;// Lower 16 bits are size
 | 
			
		||||
    VAddr addr = cmd_buff[0x41];
 | 
			
		||||
 | 
			
		||||
    // Canned DSP responses that games expect. These were taken from HW by 3dmoo team.
 | 
			
		||||
    // TODO: Remove this hack :)
 | 
			
		||||
    static const std::array<u16, 16> canned_read_pipe = {{
 | 
			
		||||
        0x000F, 0xBFFF, 0x9E8E, 0x8680, 0xA78E, 0x9430, 0x8400, 0x8540,
 | 
			
		||||
        0x948E, 0x8710, 0x8410, 0xA90E, 0xAA0E, 0xAACE, 0xAC4E, 0xAC58
 | 
			
		||||
    }};
 | 
			
		||||
 | 
			
		||||
    u32 initial_size = read_pipe_count;
 | 
			
		||||
 | 
			
		||||
    for (unsigned offset = 0; offset < size; offset += sizeof(u16)) {
 | 
			
		||||
        if (read_pipe_count < canned_read_pipe.size()) {
 | 
			
		||||
            Memory::Write16(addr + offset, canned_read_pipe[read_pipe_count]);
 | 
			
		||||
            read_pipe_count++;
 | 
			
		||||
        } else {
 | 
			
		||||
            LOG_ERROR(Service_DSP, "canned read pipe log exceeded!");
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
    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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    cmd_buff[1] = 0; // No error
 | 
			
		||||
    cmd_buff[2] = (read_pipe_count - initial_size) * sizeof(u16);
 | 
			
		||||
    std::vector<u8> response = DSP::HLE::PipeRead(pipe, size);
 | 
			
		||||
 | 
			
		||||
    LOG_WARNING(Service_DSP, "(STUBBED) called unk1=0x%08X, unk2=0x%08X, size=0x%X, buffer=0x%08X",
 | 
			
		||||
                unk1, unk2, size, addr);
 | 
			
		||||
    Memory::WriteBlock(addr, response.data(), response.size());
 | 
			
		||||
 | 
			
		||||
    cmd_buff[1] = 0; // No error
 | 
			
		||||
    cmd_buff[2] = (u32)response.size();
 | 
			
		||||
 | 
			
		||||
    LOG_TRACE(Service_DSP, "pipe=0x%08X, unk2=0x%08X, size=0x%X, buffer=0x%08X", pipe, unk2, size, addr);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -311,7 +337,6 @@ const Interface::FunctionInfo FunctionTable[] = {
 | 
			
		||||
 | 
			
		||||
Interface::Interface() {
 | 
			
		||||
    semaphore_event = Kernel::Event::Create(RESETTYPE_ONESHOT, "DSP_DSP::semaphore_event");
 | 
			
		||||
    interrupt_event = nullptr;
 | 
			
		||||
    read_pipe_count = 0;
 | 
			
		||||
 | 
			
		||||
    Register(FunctionTable);
 | 
			
		||||
@@ -319,7 +344,7 @@ Interface::Interface() {
 | 
			
		||||
 | 
			
		||||
Interface::~Interface() {
 | 
			
		||||
    semaphore_event = nullptr;
 | 
			
		||||
    interrupt_event = nullptr;
 | 
			
		||||
    interrupt_events.clear();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
} // namespace
 | 
			
		||||
 
 | 
			
		||||
@@ -23,7 +23,15 @@ public:
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/// Signals that a DSP interrupt has occurred to userland code
 | 
			
		||||
void SignalInterrupt();
 | 
			
		||||
/// Signal all audio related interrupts.
 | 
			
		||||
void SignalAllInterrupts();
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Signal a specific audio related interrupt based on interrupt id and channel id.
 | 
			
		||||
 * @param interrupt_id The interrupt id
 | 
			
		||||
 * @param channel_id The channel id
 | 
			
		||||
 * The significance of various values of interrupt_id and channel_id is not yet known.
 | 
			
		||||
 */
 | 
			
		||||
void SignalInterrupt(u32 interrupt_id, u32 channel_id);
 | 
			
		||||
 | 
			
		||||
} // namespace
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user