metaforce/Runtime/MP1/CGBASupport.cpp

222 lines
5.4 KiB
C++
Raw Normal View History

#include "Runtime/MP1/CGBASupport.hpp"
#include <cstring>
#include "Runtime/CBasics.hpp"
#include "Runtime/CDvdRequest.hpp"
#include <jbus/Endpoint.hpp>
#include <jbus/Listener.hpp>
2016-12-14 22:56:59 +00:00
2021-04-10 08:42:06 +00:00
namespace metaforce::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
2018-12-08 05:30:43 +00:00
void CGBASupport::Initialize() {
jbus::Initialize();
g_JbusListener.start();
2017-01-01 06:46:52 +00:00
}
2018-12-08 05:30:43 +00:00
void CGBASupport::GlobalPoll() {
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
}
2018-12-08 05:30:43 +00:00
CGBASupport::CGBASupport() : CDvdFile("client_pad.bin") {
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-12-08 05:30:43 +00:00
CGBASupport::~CGBASupport() {
if (x30_dvdReq)
x30_dvdReq->PostCancelRequest();
2018-05-08 05:10:24 +00:00
}
2018-12-08 05:30:43 +00:00
u8 CGBASupport::CalculateFusionJBusChecksum(const u8* data, size_t len) {
u32 sum = -1;
for (size_t i = 0; i < len; ++i) {
2018-12-08 05:30:43 +00:00
u8 ch = *data++;
sum ^= ch;
for (int j = 0; j < 8; ++j) {
if ((sum & 1)) {
sum >>= 1;
sum ^= 0xb010;
} else
sum >>= 1;
2017-01-01 06:56:34 +00:00
}
2018-12-08 05:30:43 +00:00
}
return sum;
2017-01-01 06:56:34 +00:00
}
2018-12-08 05:30:43 +00:00
bool CGBASupport::PollResponse() {
if (!g_JbusEndpoint)
return false;
2017-01-08 02:52:49 +00:00
2018-12-08 05:30:43 +00:00
u8 status;
if (g_JbusEndpoint->GBAReset(&status) == jbus::GBA_NOT_READY)
2017-01-08 02:52:49 +00:00
if (g_JbusEndpoint->GBAReset(&status) == jbus::GBA_NOT_READY)
2018-12-08 05:30:43 +00:00
return false;
2017-01-01 06:46:52 +00:00
2018-12-08 05:30:43 +00:00
if (g_JbusEndpoint->GBAGetStatus(&status) == jbus::GBA_NOT_READY)
return false;
if (status != (jbus::GBA_JSTAT_PSF1 | jbus::GBA_JSTAT_SEND))
return false;
2017-01-01 06:46:52 +00:00
jbus::ReadWriteBuffer bytes;
if (g_JbusEndpoint->GBARead(bytes, &status) == jbus::GBA_NOT_READY) {
2018-12-08 05:30:43 +00:00
return false;
}
u32 bytesU32;
std::memcpy(&bytesU32, bytes.data(), sizeof(bytes));
if (bytesU32 != SBIG('AMTE')) {
2018-12-08 05:30:43 +00:00
return false;
}
2017-01-01 06:46:52 +00:00
if (g_JbusEndpoint->GBAGetStatus(&status) == jbus::GBA_NOT_READY) {
2018-12-08 05:30:43 +00:00
return false;
}
if (status != jbus::GBA_JSTAT_PSF1) {
2018-12-08 05:30:43 +00:00
return false;
}
2018-12-08 05:30:43 +00:00
if (g_JbusEndpoint->GBAWrite({'A', 'M', 'T', 'E'}, &status) == jbus::GBA_NOT_READY) {
2018-12-08 05:30:43 +00:00
return false;
}
2018-12-08 05:30:43 +00:00
if (g_JbusEndpoint->GBAGetStatus(&status) == jbus::GBA_NOT_READY)
return false;
if ((status & jbus::GBA_JSTAT_FLAGS_MASK) != jbus::GBA_JSTAT_FLAGS_MASK)
return false;
2017-01-01 06:46:52 +00:00
2018-12-08 05:30:43 +00:00
u64 profStart = jbus::GetGCTicks();
const u64 timeToSpin = jbus::GetGCTicksPerSec() / 8000;
for (;;) {
u64 curTime = jbus::GetGCTicks();
if (curTime - profStart > timeToSpin)
return true;
2017-01-01 06:46:52 +00:00
2017-01-08 02:52:49 +00:00
if (g_JbusEndpoint->GBAGetStatus(&status) == jbus::GBA_NOT_READY)
2018-12-08 05:30:43 +00:00
continue;
if (!(status & jbus::GBA_JSTAT_SEND))
continue;
2017-01-01 06:46:52 +00:00
2018-12-08 05:30:43 +00:00
if (g_JbusEndpoint->GBAGetStatus(&status) == jbus::GBA_NOT_READY)
continue;
if (status != (jbus::GBA_JSTAT_FLAGS_MASK | jbus::GBA_JSTAT_SEND))
continue;
break;
}
2017-01-01 06:46:52 +00:00
if (g_JbusEndpoint->GBARead(bytes, &status) != jbus::GBA_READY) {
2018-12-08 05:30:43 +00:00
return false;
}
2017-01-01 06:46:52 +00:00
if (bytes[3] != CalculateFusionJBusChecksum(bytes.data(), 3)) {
2018-12-08 05:30:43 +00:00
return false;
}
2017-01-01 06:46:52 +00:00
2018-12-08 05:30:43 +00:00
x44_fusionLinked = (bytes[2] & 0x2) == 0;
if (x44_fusionLinked && (bytes[2] & 0x1) != 0)
x45_fusionBeat = true;
return true;
2017-01-01 06:46:52 +00:00
}
2017-01-08 02:52:49 +00:00
static void JoyBootDone(jbus::ThreadLocalEndpoint& endpoint, jbus::EJoyReturn status) {}
2018-12-08 05:30:43 +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;
2019-02-18 05:47:46 +00:00
[[fallthrough]];
2018-12-08 05:30:43 +00:00
case EPhase::PollProbe:
/* SIProbe poll normally occurs here with 4 second timeout */
if (!g_JbusEndpoint) {
x34_phase = EPhase::Failed;
break;
2017-01-01 06:46:52 +00:00
}
2018-12-08 05:30:43 +00:00
x40_siChan = g_JbusEndpoint->getChan();
x34_phase = EPhase::StartJoyBusBoot;
2019-02-18 05:47:46 +00:00
[[fallthrough]];
2018-12-08 05:30:43 +00:00
case EPhase::StartJoyBusBoot:
x34_phase = EPhase::PollJoyBusBoot;
if (!g_JbusEndpoint || g_JbusEndpoint->GBAJoyBootAsync(x40_siChan * 2, 2, x2c_buffer.get(), x28_fileSize,
&x3c_status, JoyBootDone) != jbus::GBA_READY) {
2018-12-08 05:30:43 +00:00
x34_phase = EPhase::Failed;
}
2018-12-08 05:30:43 +00:00
break;
case EPhase::PollJoyBusBoot:
u8 percent;
if (g_JbusEndpoint && g_JbusEndpoint->GBAGetProcessStatus(percent) == jbus::GBA_BUSY)
break;
if (!g_JbusEndpoint || g_JbusEndpoint->GBAGetStatus(&x3c_status) == jbus::GBA_NOT_READY) {
x34_phase = EPhase::Failed;
break;
2017-01-01 06:46:52 +00:00
}
2018-12-08 05:30:43 +00:00
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;
}
2016-12-16 04:35:49 +00:00
}
2018-12-08 05:30:43 +00:00
bool CGBASupport::IsReady() {
if (x34_phase != EPhase::LoadClientPad)
return true;
if (x30_dvdReq->IsComplete()) {
x30_dvdReq.reset();
2017-01-01 06:46:52 +00:00
x34_phase = EPhase::Standby;
2018-12-08 05:30:43 +00:00
/* Conveniently already little-endian */
reinterpret_cast<u32&>(x2c_buffer[0xc8]) = u32(CBasics::GetGCTicks());
x2c_buffer[0xaf] = 'E';
x2c_buffer[0xbd] = 0xc9;
return true;
}
return false;
2017-01-01 06:46:52 +00:00
}
2018-12-08 05:30:43 +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;
2017-01-01 06:46:52 +00:00
}
2018-12-08 05:30:43 +00:00
void CGBASupport::StartLink() {
x34_phase = EPhase::StartProbeTimeout;
x40_siChan = -1;
2016-12-14 22:56:59 +00:00
}
2018-12-08 05:30:43 +00:00
2021-04-10 08:42:06 +00:00
} // namespace metaforce::MP1