ppsspp/Core/HLE/sceKernelMbx.cpp

311 lines
8.4 KiB
C++
Raw Normal View History

2012-11-01 15:19:01 +00:00
// Copyright (c) 2012- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
2012-11-01 15:19:01 +00:00
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include "sceKernel.h"
#include "sceKernelThread.h"
2012-11-01 15:19:01 +00:00
#include "sceKernelMbx.h"
#include "HLE.h"
2012-11-08 13:24:51 +00:00
#define SCE_KERNEL_MBA_THPRI 0x100
#define SCE_KERNEL_MBA_MSPRI 0x400
2012-11-01 15:19:01 +00:00
2012-11-08 13:24:51 +00:00
// TODO: when a thread is being resumed (message received or cancellation), sceKernelReceiveMbx() always returns 0
2012-11-01 15:19:01 +00:00
struct NativeMbx
{
SceSize size;
char name[32];
SceUInt attr;
int numWaitThreads;
int numMessages;
u32 packetListHead;
};
struct Mbx : public KernelObject
{
const char *GetName() {return nmb.name;}
const char *GetTypeName() {return "Mbx";}
static u32 GetMissingErrorCode() { return SCE_KERNEL_ERROR_UNKNOWN_MBXID; }
int GetIDType() const { return SCE_KERNEL_TMID_Mbox; }
2012-11-08 13:24:51 +00:00
void AddWaitingThread(SceUID id, u32 addr)
{
if (nmb.attr & SCE_KERNEL_MBA_THPRI)
{
for (std::vector<std::pair<SceUID, u32>>::iterator it = waitingThreads.begin(); it != waitingThreads.end(); it++)
{
if (__KernelGetThreadPrio(id) >= __KernelGetThreadPrio((*it).first))
{
waitingThreads.insert(it, std::make_pair(id, addr));
break;
}
}
}
else
{
waitingThreads.push_back(std::make_pair(id, addr));
}
}
2012-11-01 15:19:01 +00:00
NativeMbx nmb;
2012-11-08 13:24:51 +00:00
std::vector<std::pair<SceUID, u32>> waitingThreads;
std::vector<u32> messageQueue;
2012-11-01 15:19:01 +00:00
};
2012-11-08 13:24:51 +00:00
SceUID sceKernelCreateMbx(const char *name, int memoryPartition, SceUInt attr, int size, u32 optAddr)
2012-11-01 15:19:01 +00:00
{
2012-11-08 13:24:51 +00:00
DEBUG_LOG(HLE, "sceKernelCreateMbx(%s, %i, %08x, %i, %08x)", name, memoryPartition, attr, size, optAddr);
2012-11-01 15:19:01 +00:00
Mbx *m = new Mbx();
SceUID id = kernelObjects.Create(m);
m->nmb.size = sizeof(NativeMbx);
strncpy(m->nmb.name, name, sizeof(m->nmb.name));
m->nmb.attr = attr;
m->nmb.numWaitThreads = 0;
m->nmb.numMessages = 0;
m->nmb.packetListHead = 0;
2012-11-08 13:24:51 +00:00
return id;
2012-11-01 15:19:01 +00:00
}
2012-11-08 13:24:51 +00:00
int sceKernelDeleteMbx(SceUID id)
2012-11-01 15:19:01 +00:00
{
u32 error;
Mbx *m = kernelObjects.Get<Mbx>(id, error);
if (m)
{
DEBUG_LOG(HLE, "sceKernelDeleteMbx(%i)", id);
for (size_t i = 0; i < m->waitingThreads.size(); i++)
{
Memory::Write_U32(0, m->waitingThreads[i].second);
__KernelResumeThreadFromWait(m->waitingThreads[i].first);
}
}
else
{
ERROR_LOG(HLE, "sceKernelDeleteMbx(%i): invalid mbx id", id);
}
2012-11-08 13:24:51 +00:00
return kernelObjects.Destroy<Mbx>(id);
2012-11-01 15:19:01 +00:00
}
2012-11-08 15:24:06 +00:00
void sceKernelSendMbx(SceUID id, u32 packetAddr)
2012-11-01 15:19:01 +00:00
{
2012-11-08 13:24:51 +00:00
u32 error;
Mbx *m = kernelObjects.Get<Mbx>(id, error);
NativeMbxPacket *addPacket = (NativeMbxPacket*)Memory::GetPointer(packetAddr);
if (addPacket == 0)
{
ERROR_LOG(HLE, "sceKernelSendMbx(%i, %08x): invalid packet address", id, packetAddr);
2012-11-08 15:24:06 +00:00
RETURN(-1);
return;
2012-11-08 13:24:51 +00:00
}
if (!m)
{
ERROR_LOG(HLE, "sceKernelSendMbx(%i, %08x): invalid mbx id", id, packetAddr);
2012-11-08 15:24:06 +00:00
RETURN(error);
return;
2012-11-08 13:24:51 +00:00
}
2012-11-01 15:19:01 +00:00
2012-11-08 13:24:51 +00:00
if (m->waitingThreads.empty())
{
DEBUG_LOG(HLE, "sceKernelSendMbx(%i, %08x): no threads currently waiting, adding message to queue", id, packetAddr);
if (m->nmb.attr & SCE_KERNEL_MBA_MSPRI)
{
for (std::vector<u32>::iterator it = m->messageQueue.begin(); it != m->messageQueue.end(); it++)
{
NativeMbxPacket *p = (NativeMbxPacket*)Memory::GetPointer(*it);
if (addPacket->priority >= p->priority)
{
m->messageQueue.insert(it, packetAddr);
break;
}
}
}
else
{
m->messageQueue.push_back(packetAddr);
}
2012-11-08 15:24:06 +00:00
RETURN(0);
2012-11-08 13:24:51 +00:00
}
else if (m->messageQueue.empty())
{
Memory::Write_U32(packetAddr, m->waitingThreads.front().second);
__KernelResumeThreadFromWait(m->waitingThreads.front().first);
DEBUG_LOG(HLE, "sceKernelSendMbx(%i, %08x): threads waiting, resuming %d", id, packetAddr, m->waitingThreads.front().first);
m->waitingThreads.erase(m->waitingThreads.begin());
2012-11-08 15:24:06 +00:00
RETURN(0);
2012-11-08 13:24:51 +00:00
__KernelReSchedule();
}
else
{
ERROR_LOG(HLE, "sceKernelSendMbx(%i, %08x): WTF!? thread waiting while there is a message in the queue?", id, packetAddr);
2012-11-08 15:24:06 +00:00
RETURN(-1);
2012-11-08 13:24:51 +00:00
}
2012-11-01 15:19:01 +00:00
}
2012-11-08 15:24:06 +00:00
void sceKernelReceiveMbx(SceUID id, u32 packetAddrPtr, u32 timeoutPtr)
2012-11-01 15:19:01 +00:00
{
2012-11-08 13:24:51 +00:00
u32 error;
Mbx *m = kernelObjects.Get<Mbx>(id, error);
if (!m)
{
ERROR_LOG(HLE, "sceKernelReceiveMbx(%i, %08x, %08x): invalid mbx id", id, packetAddrPtr, timeoutPtr);
2012-11-08 15:24:06 +00:00
RETURN(error);
return;
2012-11-08 13:24:51 +00:00
}
if (!m->messageQueue.empty())
{
DEBUG_LOG(HLE, "sceKernelReceiveMbx(%i, %08x, %08x): sending first queue message", id, packetAddrPtr, timeoutPtr);
Memory::Write_U32(m->messageQueue.front(), packetAddrPtr);
m->messageQueue.erase(m->messageQueue.begin());
2012-11-08 15:24:06 +00:00
RETURN(0);
2012-11-08 13:24:51 +00:00
}
else
{
DEBUG_LOG(HLE, "sceKernelReceiveMbx(%i, %08x, %08x): no message in queue, waiting", id, packetAddrPtr, timeoutPtr);
m->AddWaitingThread(__KernelGetCurThread(), packetAddrPtr);
2012-11-08 15:24:06 +00:00
RETURN(0);
2012-11-08 13:24:51 +00:00
__KernelWaitCurThread(WAITTYPE_MBX, 0, 0, 0, false); // ?
}
2012-11-01 15:19:01 +00:00
}
2012-11-08 15:24:06 +00:00
void sceKernelReceiveMbxCB(SceUID id, u32 packetAddrPtr, u32 timeoutPtr)
2012-11-01 15:19:01 +00:00
{
u32 error;
Mbx *m = kernelObjects.Get<Mbx>(id, error);
__KernelCheckCallbacks();
2012-11-01 15:19:01 +00:00
2012-11-08 13:24:51 +00:00
if (!m)
{
ERROR_LOG(HLE, "sceKernelReceiveMbxCB(%i, %08x, %08x): invalid mbx id", id, packetAddrPtr, timeoutPtr);
2012-11-08 15:24:06 +00:00
RETURN(error);
return;
2012-11-08 13:24:51 +00:00
}
2012-11-01 15:19:01 +00:00
2012-11-08 13:24:51 +00:00
if (!m->messageQueue.empty())
{
DEBUG_LOG(HLE, "sceKernelReceiveMbxCB(%i, %08x, %08x): sending first queue message", id, packetAddrPtr, timeoutPtr);
Memory::Write_U32(m->messageQueue.front(), packetAddrPtr);
m->messageQueue.erase(m->messageQueue.begin());
RETURN(0);
2012-11-08 13:24:51 +00:00
}
else
{
DEBUG_LOG(HLE, "sceKernelReceiveMbxCB(%i, %08x, %08x): no message in queue, waiting", id, packetAddrPtr, timeoutPtr);
m->AddWaitingThread(id, packetAddrPtr);
2012-11-08 15:24:06 +00:00
RETURN(0);
2012-11-08 13:24:51 +00:00
__KernelWaitCurThread(WAITTYPE_MBX, 0, 0, 0, true); // ?
}
2012-11-01 15:19:01 +00:00
}
2012-11-08 13:24:51 +00:00
int sceKernelPollMbx(SceUID id, u32 packetAddrPtr)
2012-11-01 15:19:01 +00:00
{
u32 error;
Mbx *m = kernelObjects.Get<Mbx>(id, error);
2012-11-08 13:24:51 +00:00
if (!m)
{
ERROR_LOG(HLE, "sceKernelPollMbx(%i, %08x): invalid mbx id", id, packetAddrPtr);
return error;
}
2012-11-01 15:19:01 +00:00
2012-11-08 13:24:51 +00:00
if (!m->messageQueue.empty())
{
DEBUG_LOG(HLE, "sceKernelPollMbx(%i, %08x): sending first queue message", id, packetAddrPtr);
Memory::Write_U32(m->messageQueue.front(), packetAddrPtr);
m->messageQueue.erase(m->messageQueue.begin());
return 0;
}
else
{
DEBUG_LOG(HLE, "SCE_KERNEL_ERROR_MBOX_NOMSG=sceKernelPollMbx(%i, %08x): no message in queue", id, packetAddrPtr);
return SCE_KERNEL_ERROR_MBOX_NOMSG;
}
2012-11-01 15:19:01 +00:00
}
2012-11-08 13:24:51 +00:00
int sceKernelCancelReceiveMbx(SceUID id, u32 numWaitingThreadsAddr)
2012-11-01 15:19:01 +00:00
{
2012-11-08 13:24:51 +00:00
u32 error;
Mbx *m = kernelObjects.Get<Mbx>(id, error);
int *numWaitingThreads = (int*)Memory::GetPointer(numWaitingThreadsAddr);
2012-11-01 15:19:01 +00:00
2012-11-08 13:24:51 +00:00
if (!m)
{
ERROR_LOG(HLE, "sceKernelCancelReceiveMbx(%i, %08x): invalid mbx id", id, numWaitingThreadsAddr);
return error;
}
u32 count = m->waitingThreads.size();
DEBUG_LOG(HLE, "sceKernelCancelReceiveMbx(%i, %08x): cancelling %d threads", id, numWaitingThreadsAddr, count);
for (size_t i = 0; i < m->waitingThreads.size(); i++)
{
Memory::Write_U32(0, m->waitingThreads[i].second);
__KernelResumeThreadFromWait(m->waitingThreads[i].first);
}
m->waitingThreads.clear();
if (numWaitingThreads)
*numWaitingThreads = count;
return 0;
2012-11-01 15:19:01 +00:00
}
2012-11-08 13:24:51 +00:00
int sceKernelReferMbxStatus(SceUID id, u32 infoAddr)
2012-11-01 15:19:01 +00:00
{
u32 error;
2012-11-08 13:24:51 +00:00
Mbx *m = kernelObjects.Get<Mbx>(id, error);
if (!m)
2012-11-01 15:19:01 +00:00
{
2012-11-08 13:24:51 +00:00
ERROR_LOG(HLE, "sceKernelReferMbxStatus(%i, %08x): invalid mbx id", id, infoAddr);
return error;
}
SceKernelMbxInfo *info = (SceKernelMbxInfo*)Memory::GetPointer(infoAddr);
DEBUG_LOG(HLE, "sceKernelReferMbxStatus(%i, %08x)", id, infoAddr);
if (info)
{
strncpy(info->name, m->nmb.name, 32);
info->attr = m->nmb.attr;
info->numWaitThreads = m->waitingThreads.size();
info->numMessage = m->messageQueue.size();
// Fill the 'next' parameter of packets which we don't use by default but could be used by a game
if (m->messageQueue.size() != 0)
{
info->topPacketAddr = m->messageQueue[0];
for (u32 i = 0; i < m->messageQueue.size() - 1; i++)
{
Memory::Write_U32(m->messageQueue[i + 1], Memory::Read_U32(m->messageQueue[i]));
}
Memory::Write_U32(m->messageQueue[m->messageQueue.size() - 1], 0);
}
else
{
info->topPacketAddr = 0;
}
2012-11-01 15:19:01 +00:00
}
else
{
2012-11-08 13:24:51 +00:00
return -1;
2012-11-01 15:19:01 +00:00
}
2012-11-08 13:24:51 +00:00
return 0;
2012-11-01 15:19:01 +00:00
}
2012-11-08 13:24:51 +00:00