metaforce/Runtime/MP1/CGBASupport.cpp

230 lines
5.8 KiB
C++
Raw Normal View History

2016-12-14 22:56:59 +00:00
#include "CGBASupport.hpp"
2017-01-01 06:46:52 +00:00
#include "CDvdRequest.hpp"
#include "CBasics.hpp"
2017-01-08 02:52:49 +00:00
#include "jbus/Listener.hpp"
#include "jbus/Endpoint.hpp"
2016-12-14 22:56:59 +00:00
2017-12-29 08:08:12 +00:00
namespace urde::MP1
2016-12-14 22:56:59 +00:00
{
2017-01-08 02:52:49 +00:00
static jbus::Listener g_JbusListener;
static std::unique_ptr<jbus::Endpoint> g_JbusEndpoint;
2017-01-01 06:46:52 +00:00
2017-01-08 02:52:49 +00:00
void CGBASupport::Initialize()
2017-01-01 06:46:52 +00:00
{
2017-01-08 02:52:49 +00:00
jbus::Initialize();
g_JbusListener.start();
2017-01-01 06:46:52 +00:00
}
2017-01-08 02:52:49 +00:00
void CGBASupport::GlobalPoll()
2017-01-01 06:46:52 +00:00
{
2017-01-08 02:52:49 +00:00
if (g_JbusEndpoint && !g_JbusEndpoint->connected())
g_JbusEndpoint.reset();
if (!g_JbusEndpoint)
{
g_JbusEndpoint = g_JbusListener.accept();
if (g_JbusEndpoint)
g_JbusEndpoint->setChan(3);
}
2017-01-01 06:46:52 +00:00
}
2017-01-08 02:52:49 +00:00
CGBASupport::CGBASupport()
: CDvdFile("client_pad.bin")
2017-01-01 06:46:52 +00:00
{
2017-01-08 02:52:49 +00:00
x28_fileSize = ROUND_UP_32(Length());
x2c_buffer.reset(new u8[x28_fileSize]);
x30_dvdReq = AsyncRead(x2c_buffer.get(), x28_fileSize);
2017-01-01 06:46:52 +00:00
}
2018-05-08 05:10:24 +00:00
CGBASupport::~CGBASupport()
{
if (x30_dvdReq)
x30_dvdReq->PostCancelRequest();
}
2017-01-08 02:52:49 +00:00
u8 CGBASupport::CalculateFusionJBusChecksum(const u8* data, size_t len)
2017-01-01 06:56:34 +00:00
{
u32 sum = -1;
2017-01-08 02:52:49 +00:00
for (int i = 0 ; i < len; ++i)
2017-01-01 06:56:34 +00:00
{
2017-01-08 02:52:49 +00:00
u8 ch = *data++;
2017-01-01 06:56:34 +00:00
sum ^= ch;
for (int j = 0; j < 8; ++j)
{
if ((sum & 1))
{
sum >>= 1;
sum ^= 0xb010;
}
else
sum >>= 1;
}
}
return sum;
}
2017-01-01 06:46:52 +00:00
bool CGBASupport::PollResponse()
2016-12-16 04:35:49 +00:00
{
2017-01-08 02:52:49 +00:00
if (!g_JbusEndpoint)
return false;
2017-01-01 06:46:52 +00:00
u8 status;
2017-01-08 02:52:49 +00:00
if (g_JbusEndpoint->GBAReset(&status) == jbus::GBA_NOT_READY)
if (g_JbusEndpoint->GBAReset(&status) == jbus::GBA_NOT_READY)
2017-01-01 06:46:52 +00:00
return false;
2017-01-08 02:52:49 +00:00
if (g_JbusEndpoint->GBAGetStatus(&status) == jbus::GBA_NOT_READY)
2017-01-01 06:46:52 +00:00
return false;
2017-01-08 02:52:49 +00:00
if (status != (jbus::GBA_JSTAT_PSF1 | jbus::GBA_JSTAT_SEND))
2017-01-01 06:46:52 +00:00
return false;
u8 bytes[4];
2017-01-08 02:52:49 +00:00
if (g_JbusEndpoint->GBARead(bytes, &status) == jbus::GBA_NOT_READY)
2017-01-01 06:46:52 +00:00
return false;
if (reinterpret_cast<u32&>(bytes) != SBIG('AMTE'))
return false;
2017-01-08 02:52:49 +00:00
if (g_JbusEndpoint->GBAGetStatus(&status) == jbus::GBA_NOT_READY)
2017-01-01 06:46:52 +00:00
return false;
2017-01-08 02:52:49 +00:00
if (status != jbus::GBA_JSTAT_PSF1)
2017-01-01 06:46:52 +00:00
return false;
2017-01-08 02:52:49 +00:00
if (g_JbusEndpoint->GBAWrite((unsigned char*)"AMTE", &status) == jbus::GBA_NOT_READY)
2017-01-01 06:46:52 +00:00
return false;
2017-01-08 02:52:49 +00:00
if (g_JbusEndpoint->GBAGetStatus(&status) == jbus::GBA_NOT_READY)
2017-01-01 06:46:52 +00:00
return false;
2017-01-08 02:52:49 +00:00
if ((status & jbus::GBA_JSTAT_FLAGS_MASK) != jbus::GBA_JSTAT_FLAGS_MASK)
2017-01-01 06:46:52 +00:00
return false;
2017-01-08 02:52:49 +00:00
u64 profStart = jbus::GetGCTicks();
const u64 timeToSpin = jbus::GetGCTicksPerSec() / 8000;
2017-01-01 06:46:52 +00:00
for (;;)
{
2017-01-08 02:52:49 +00:00
u64 curTime = jbus::GetGCTicks();
2017-01-01 06:46:52 +00:00
if (curTime - profStart > timeToSpin)
return true;
2017-01-08 02:52:49 +00:00
if (g_JbusEndpoint->GBAGetStatus(&status) == jbus::GBA_NOT_READY)
2017-01-01 06:46:52 +00:00
continue;
2017-01-08 02:52:49 +00:00
if (!(status & jbus::GBA_JSTAT_SEND))
2017-01-01 06:46:52 +00:00
continue;
2017-01-08 02:52:49 +00:00
if (g_JbusEndpoint->GBAGetStatus(&status) == jbus::GBA_NOT_READY)
2017-01-01 06:46:52 +00:00
continue;
2017-01-08 02:52:49 +00:00
if (status != (jbus::GBA_JSTAT_FLAGS_MASK | jbus::GBA_JSTAT_SEND))
2017-01-01 06:46:52 +00:00
continue;
break;
}
2017-01-08 02:52:49 +00:00
if (g_JbusEndpoint->GBARead(bytes, &status) != jbus::GBA_READY)
2017-01-01 06:46:52 +00:00
return false;
2017-01-08 02:52:49 +00:00
if (bytes[3] != CalculateFusionJBusChecksum(bytes, 3))
2017-01-01 06:56:34 +00:00
return false;
2017-01-01 06:46:52 +00:00
2017-01-08 02:52:49 +00:00
x44_fusionLinked = (bytes[2] & 0x2) == 0;
2017-01-01 06:46:52 +00:00
if (x44_fusionLinked && (bytes[2] & 0x1) != 0)
x45_fusionBeat = true;
return true;
}
2017-01-08 02:52:49 +00:00
static void JoyBootDone(jbus::ThreadLocalEndpoint& endpoint, jbus::EJoyReturn status) {}
2017-01-01 06:46:52 +00:00
void CGBASupport::Update(float dt)
{
switch (x34_phase)
{
case EPhase::LoadClientPad:
IsReady();
break;
case EPhase::StartProbeTimeout:
x38_timeout = 4.f;
x34_phase = EPhase::PollProbe;
case EPhase::PollProbe:
/* SIProbe poll normally occurs here with 4 second timeout */
2017-01-08 02:52:49 +00:00
if (!g_JbusEndpoint)
{
x34_phase = EPhase::Failed;
break;
}
x40_siChan = g_JbusEndpoint->getChan();
2017-01-01 06:46:52 +00:00
x34_phase = EPhase::StartJoyBusBoot;
case EPhase::StartJoyBusBoot:
x34_phase = EPhase::PollJoyBusBoot;
2017-01-08 02:52:49 +00:00
if (!g_JbusEndpoint || g_JbusEndpoint->GBAJoyBootAsync(x40_siChan * 2, 2,
x2c_buffer.get(), x28_fileSize, &x3c_status,
std::bind(JoyBootDone,
std::placeholders::_1,
std::placeholders::_2)) != jbus::GBA_READY)
x34_phase = EPhase::Failed;
2017-01-01 06:46:52 +00:00
break;
case EPhase::PollJoyBusBoot:
2017-01-08 02:52:49 +00:00
u8 percent;
if (g_JbusEndpoint && g_JbusEndpoint->GBAGetProcessStatus(percent) == jbus::GBA_BUSY)
2017-01-01 06:46:52 +00:00
break;
2017-01-08 02:52:49 +00:00
if (!g_JbusEndpoint || g_JbusEndpoint->GBAGetStatus(&x3c_status) == jbus::GBA_NOT_READY)
2017-01-01 06:46:52 +00:00
{
x34_phase = EPhase::Failed;
break;
}
x38_timeout = 4.f;
x34_phase = EPhase::DataTransfer;
break;
case EPhase::DataTransfer:
if (PollResponse())
{
x34_phase = EPhase::Complete;
break;
}
x38_timeout = std::max(0.f, x38_timeout - dt);
if (x38_timeout == 0.f)
x34_phase = EPhase::Failed;
break;
default: break;
}
}
bool CGBASupport::IsReady()
{
if (x34_phase != EPhase::LoadClientPad)
return true;
if (x30_dvdReq->IsComplete())
{
x30_dvdReq.reset();
x34_phase = EPhase::Standby;
/* Conveniently already little-endian */
reinterpret_cast<u32&>(x2c_buffer[0xc8]) = u32(CBasics::GetGCTicks());
x2c_buffer[0xaf] = 'E';
x2c_buffer[0xbd] = 0xc9;
return true;
}
2016-12-16 04:35:49 +00:00
return false;
}
2017-01-01 06:46:52 +00:00
void CGBASupport::InitializeSupport()
{
x34_phase = EPhase::Standby;
x38_timeout = 0.f;
x3c_status = false;
x40_siChan = -1;
x44_fusionLinked = false;
x45_fusionBeat = false;
}
void CGBASupport::StartLink()
{
x34_phase = EPhase::StartProbeTimeout;
x40_siChan = -1;
}
2016-12-14 22:56:59 +00:00
}