Skip to content

Commit

Permalink
Use StructManager to manage Deci2Handlers.
Browse files Browse the repository at this point in the history
Also add check when we fail to allocate.
  • Loading branch information
jpd002 committed Jun 4, 2024
1 parent 1fe85f2 commit 2f3d825
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 37 deletions.
55 changes: 22 additions & 33 deletions Source/ee/PS2OS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#define BIOS_ADDRESS_STATE_ITEM(a) (BIOS_ADDRESS_STATE_BASE + offsetof(BIOS_STATE, a))

#define BIOS_ADDRESS_INTERRUPT_THREAD_CONTEXT (BIOS_ADDRESS_STATE_BASE + sizeof(BIOS_STATE))
#define BIOS_ADDRESS_DECI2HANDLER_BASE 0x00008000
#define BIOS_ADDRESS_INTCHANDLER_BASE 0x0000A000
#define BIOS_ADDRESS_DMACHANDLER_BASE 0x0000C000
#define BIOS_ADDRESS_SEMAPHORE_BASE 0x0000E000
Expand Down Expand Up @@ -218,6 +219,7 @@ CPS2OS::CPS2OS(CMIPS& ee, uint8* ram, uint8* bios, uint8* spr, CGSHandler*& gs,
, m_sif(sif)
, m_libMc2(ram, *this, iopBios)
, m_iopBios(iopBios)
, m_deci2Handlers(reinterpret_cast<DECI2HANDLER*>(m_ram + BIOS_ADDRESS_DECI2HANDLER_BASE), BIOS_ID_BASE, MAX_DECI2HANDLER)
, m_threads(reinterpret_cast<THREAD*>(m_ram + BIOS_ADDRESS_THREAD_BASE), BIOS_ID_BASE, MAX_THREAD)
, m_semaphores(reinterpret_cast<SEMAPHORE*>(m_ram + BIOS_ADDRESS_SEMAPHORE_BASE), SEMA_ID_BASE, MAX_SEMAPHORE)
, m_intcHandlers(reinterpret_cast<INTCHANDLER*>(m_ram + BIOS_ADDRESS_INTCHANDLER_BASE), BIOS_ID_BASE, MAX_INTCHANDLER)
Expand Down Expand Up @@ -1408,26 +1410,6 @@ uint8* CPS2OS::GetStructPtr(uint32 address) const
return memory + address;
}

uint32 CPS2OS::GetNextAvailableDeci2HandlerId()
{
for(uint32 i = 1; i < MAX_DECI2HANDLER; i++)
{
DECI2HANDLER* handler = GetDeci2Handler(i);
if(handler->valid != 1)
{
return i;
}
}

return 0xFFFFFFFF;
}

CPS2OS::DECI2HANDLER* CPS2OS::GetDeci2Handler(uint32 id)
{
id--;
return &((DECI2HANDLER*)&m_ram[0x00008000])[id];
}

Ee::CLibMc2& CPS2OS::GetLibMc2()
{
return m_libMc2;
Expand Down Expand Up @@ -3144,24 +3126,31 @@ void CPS2OS::sc_Deci2Call()
case 0x01:
//Deci2Open
{
uint32 id = GetNextAvailableDeci2HandlerId();

auto handler = GetDeci2Handler(id);
handler->valid = 1;
handler->device = *reinterpret_cast<uint32*>(GetStructPtr(param + 0x00));
handler->bufferAddr = *reinterpret_cast<uint32*>(GetStructPtr(param + 0x04));

uint32 id = m_deci2Handlers.Allocate();
if(id == Deci2HandlerList::INVALID_ID)
{
//This happens in 007: Nightfire. The game has a custom ELF loader and newly loaded
//ELF attempts to clean everything, but they forgot about DECI2 handlers.
CLog::GetInstance().Warn(LOG_NAME, "Failed to allocate DECI2 handler.\r\n");
}
else
{
auto handler = m_deci2Handlers[id];
handler->isValid = 1;
handler->device = *reinterpret_cast<uint32*>(GetStructPtr(param + 0x00));
handler->bufferAddr = *reinterpret_cast<uint32*>(GetStructPtr(param + 0x04));
}
m_ee.m_State.nGPR[SC_RETURN].nD0 = static_cast<int32>(id);
}
break;
case 0x03:
//Deci2Send
{
uint32 id = *reinterpret_cast<uint32*>(GetStructPtr(param + 0x00));
auto handler = GetDeci2Handler(id);

assert(handler->valid);
if(handler->valid)
auto handler = m_deci2Handlers[id];
assert(handler);
if(handler)
{
auto buffer = reinterpret_cast<DECI2BUFFER*>(GetStructPtr(handler->bufferAddr));
if(buffer->dataAddr != 0)
Expand Down Expand Up @@ -3189,9 +3178,9 @@ void CPS2OS::sc_Deci2Call()
{
uint32 id = *reinterpret_cast<uint32*>(GetStructPtr(param + 0x00));

auto handler = GetDeci2Handler(id);
assert(handler->valid);
if(handler->valid)
auto handler = m_deci2Handlers[id];
assert(handler);
if(handler)
{
auto buffer = reinterpret_cast<DECI2BUFFER*>(GetStructPtr(handler->bufferAddr));
buffer->status1 = 0;
Expand Down
7 changes: 3 additions & 4 deletions Source/ee/PS2OS.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class CPS2OS : public CBiosDebugInfoProvider

struct DECI2HANDLER
{
uint32 valid;
uint32 isValid;
uint32 device;
uint32 bufferAddr;
};
Expand Down Expand Up @@ -324,6 +324,7 @@ class CPS2OS : public CBiosDebugInfoProvider
THS_DORMANT = 0x10,
};

typedef COsStructManager<DECI2HANDLER> Deci2HandlerList;
typedef COsStructManager<THREAD> ThreadList;
typedef COsStructManager<SEMAPHORE> SemaphoreList;
typedef COsStructManager<INTCHANDLER> IntcHandlerList;
Expand Down Expand Up @@ -377,9 +378,6 @@ class CPS2OS : public CBiosDebugInfoProvider
std::pair<uint32, uint32> GetVsyncFlagPtrs() const;
void SetVsyncFlagPtrs(uint32, uint32);

uint32 GetNextAvailableDeci2HandlerId();
DECI2HANDLER* GetDeci2Handler(uint32);

//Various system calls
void sc_GsSetCrt();
void sc_Exit();
Expand Down Expand Up @@ -452,6 +450,7 @@ class CPS2OS : public CBiosDebugInfoProvider
CIopBios& m_iopBios;

std::unique_ptr<CELF32> m_elf;
Deci2HandlerList m_deci2Handlers;
ThreadList m_threads;
SemaphoreList m_semaphores;
IntcHandlerList m_intcHandlers;
Expand Down

0 comments on commit 2f3d825

Please sign in to comment.