metaforce/gbalink/main.cpp

294 lines
7.5 KiB
C++
Raw Normal View History

2017-12-29 08:08:12 +00:00
#include <cstdio>
#include "GCNTypes.hpp"
#include <memory>
2017-01-07 05:06:54 +00:00
#include <thread>
#include <queue>
#include "optional.hpp"
#include "jbus/Endpoint.hpp"
#include "jbus/Listener.hpp"
2017-06-11 04:37:09 +00:00
#undef min
#undef max
class CGBASupport
{
public:
enum class EPhase
{
LoadClientPad,
Standby,
StartProbeTimeout,
PollProbe,
StartJoyBusBoot,
PollJoyBusBoot,
DataTransfer,
Complete,
Failed
};
private:
2017-01-07 05:06:54 +00:00
std::unique_ptr<jbus::Endpoint> m_endpoint;
u32 x28_fileSize;
std::unique_ptr<u8[]> x2c_buffer;
EPhase x34_phase = EPhase::LoadClientPad;
float x38_timeout = 0.f;
u8 x3c_status = 0;
u32 x40_siChan = -1;
bool x44_fusionLinked = false;
bool x45_fusionBeat = false;
static CGBASupport* SharedInstance;
2017-01-07 05:06:54 +00:00
static u8 CalculateFusionJBusChecksum(const u8* data, size_t len);
public:
2017-01-07 05:06:54 +00:00
CGBASupport(const char* clientPadPath, std::unique_ptr<jbus::Endpoint>&& ep);
~CGBASupport();
bool PollResponse();
void Update(float dt);
bool IsReady();
void InitializeSupport();
void StartLink();
EPhase GetPhase() const { return x34_phase; }
bool IsFusionLinked() const { return x44_fusionLinked; }
bool IsFusionBeat() const { return x45_fusionBeat; }
};
CGBASupport* CGBASupport::SharedInstance;
2017-01-07 05:06:54 +00:00
CGBASupport::CGBASupport(const char* clientPadPath, std::unique_ptr<jbus::Endpoint>&& ep)
: m_endpoint(std::move(ep))
2017-01-05 00:53:50 +00:00
{
2017-01-07 05:06:54 +00:00
FILE* fp = fopen(clientPadPath, "rb");
if (!fp)
2017-01-04 05:27:20 +00:00
{
2017-01-07 05:06:54 +00:00
fprintf(stderr, "No file at %s\n", clientPadPath);
2017-01-04 05:27:20 +00:00
exit(1);
}
2017-01-07 05:06:54 +00:00
fseek(fp, 0, SEEK_END);
x28_fileSize = ftell(fp);
fseek(fp, 0, SEEK_SET);
x2c_buffer.reset(new u8[x28_fileSize]);
fread(x2c_buffer.get(), 1, x28_fileSize, fp);
fclose(fp);
SharedInstance = this;
}
2017-01-07 05:06:54 +00:00
CGBASupport::~CGBASupport()
{
2017-01-07 05:06:54 +00:00
SharedInstance = nullptr;
}
2017-01-07 05:06:54 +00:00
u8 CGBASupport::CalculateFusionJBusChecksum(const u8* data, size_t len)
{
u32 sum = -1;
2017-01-05 00:53:50 +00:00
for (int i = 0 ; i < len; ++i)
{
2017-01-05 00:53:50 +00:00
u8 ch = *data++;
sum ^= ch;
for (int j = 0; j < 8; ++j)
{
if ((sum & 1))
{
sum >>= 1;
sum ^= 0xb010;
}
else
sum >>= 1;
}
}
return sum;
}
bool CGBASupport::PollResponse()
{
u8 status;
2017-01-07 05:06:54 +00:00
if (m_endpoint->GBAReset(&status) == jbus::GBA_NOT_READY)
if (m_endpoint->GBAReset(&status) == jbus::GBA_NOT_READY)
return false;
2017-01-07 05:06:54 +00:00
if (m_endpoint->GBAGetStatus(&status) == jbus::GBA_NOT_READY)
return false;
2017-01-07 05:06:54 +00:00
if (status != (jbus::GBA_JSTAT_PSF1 | jbus::GBA_JSTAT_SEND))
return false;
u8 bytes[4];
2017-01-07 05:06:54 +00:00
if (m_endpoint->GBARead(bytes, &status) == jbus::GBA_NOT_READY)
return false;
if (reinterpret_cast<u32&>(bytes) != SBIG('AMTE'))
return false;
2017-01-07 05:06:54 +00:00
if (m_endpoint->GBAGetStatus(&status) == jbus::GBA_NOT_READY)
return false;
2017-01-07 05:06:54 +00:00
if (status != jbus::GBA_JSTAT_PSF1)
return false;
2017-01-07 05:06:54 +00:00
if (m_endpoint->GBAWrite((unsigned char*)"AMTE", &status) == jbus::GBA_NOT_READY)
return false;
2017-01-07 05:06:54 +00:00
if (m_endpoint->GBAGetStatus(&status) == jbus::GBA_NOT_READY)
return false;
2017-01-07 05:06:54 +00:00
if ((status & jbus::GBA_JSTAT_FLAGS_MASK) != jbus::GBA_JSTAT_FLAGS_MASK)
return false;
2017-01-07 05:06:54 +00:00
u64 profStart = jbus::GetGCTicks();
const u64 timeToSpin = jbus::GetGCTicksPerSec() / 8000;
for (;;)
{
2017-01-07 05:06:54 +00:00
u64 curTime = jbus::GetGCTicks();
if (curTime - profStart > timeToSpin)
return true;
2017-01-07 05:06:54 +00:00
if (m_endpoint->GBAGetStatus(&status) == jbus::GBA_NOT_READY)
continue;
2017-01-07 05:06:54 +00:00
if (!(status & jbus::GBA_JSTAT_SEND))
continue;
2017-01-07 05:06:54 +00:00
if (m_endpoint->GBAGetStatus(&status) == jbus::GBA_NOT_READY)
continue;
2017-01-07 05:06:54 +00:00
if (status != (jbus::GBA_JSTAT_FLAGS_MASK | jbus::GBA_JSTAT_SEND))
continue;
break;
}
2017-01-07 05:06:54 +00:00
if (m_endpoint->GBARead(bytes, &status) != jbus::GBA_READY)
return false;
2017-01-07 05:06:54 +00:00
if (bytes[3] != CalculateFusionJBusChecksum(bytes, 3))
return false;
2017-01-05 00:53:50 +00:00
x44_fusionLinked = (bytes[2] & 0x2) == 0;
if (x44_fusionLinked && (bytes[2] & 0x1) != 0)
x45_fusionBeat = true;
return true;
}
2017-01-07 05:06:54 +00:00
static void JoyBootDone(jbus::ThreadLocalEndpoint& endpoint, jbus::EJoyReturn status) {}
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
x40_siChan = m_endpoint->getChan();
x34_phase = EPhase::StartJoyBusBoot;
case EPhase::StartJoyBusBoot:
x34_phase = EPhase::PollJoyBusBoot;
2017-01-07 05:06:54 +00:00
if (m_endpoint->GBAJoyBootAsync(x40_siChan * 2, 2,
2017-01-07 08:29:38 +00:00
x2c_buffer.get(), x28_fileSize, &x3c_status,
std::bind(JoyBootDone,
std::placeholders::_1,
std::placeholders::_2)) != jbus::GBA_READY)
2017-01-04 05:27:20 +00:00
x34_phase = EPhase::Failed;
break;
case EPhase::PollJoyBusBoot:
2017-01-07 05:06:54 +00:00
u8 percent;
2017-01-08 00:15:06 +00:00
if (m_endpoint->GBAGetProcessStatus(percent) == jbus::GBA_BUSY)
break;
2017-01-07 05:06:54 +00:00
if (m_endpoint->GBAGetStatus(&x3c_status) == jbus::GBA_NOT_READY)
{
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;
x34_phase = EPhase::Standby;
2017-01-07 05:06:54 +00:00
reinterpret_cast<u32&>(x2c_buffer[0xc8]) = u32(jbus::GetGCTicks());
x2c_buffer[0xaf] = 'E';
x2c_buffer[0xbd] = 0xc9;
return true;
}
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;
}
int main(int argc, char** argv)
{
2017-01-07 05:06:54 +00:00
jbus::Initialize();
printf("Listening for client\n");
jbus::Listener listener;
listener.start();
std::unique_ptr<jbus::Endpoint> endpoint;
while (true)
{
s64 frameStart = jbus::GetGCTicks();
endpoint = listener.accept();
if (endpoint)
break;
s64 frameEnd = jbus::GetGCTicks();
2017-01-07 21:21:02 +00:00
s64 waitTicks = jbus::GetGCTicksPerSec() / 60;
2017-01-07 05:06:54 +00:00
if (waitTicks > 0)
jbus::WaitGCTicks(waitTicks);
}
2017-01-04 05:27:20 +00:00
2017-01-07 05:06:54 +00:00
CGBASupport gba("client_pad.bin", std::move(endpoint));
2017-01-04 05:27:20 +00:00
gba.Update(0.f);
gba.InitializeSupport();
gba.StartLink();
2017-01-04 05:27:20 +00:00
printf("Waiting 5 sec\n");
2017-01-07 05:06:54 +00:00
jbus::WaitGCTicks(jbus::GetGCTicksPerSec() * 5);
2017-01-04 05:27:20 +00:00
printf("Connecting\n");
while (gba.GetPhase() < CGBASupport::EPhase::Complete)
{
gba.Update(1.f / 60.f);
2017-01-07 05:06:54 +00:00
s64 waitTicks = jbus::GetGCTicksPerSec() / 60;
2017-01-04 05:27:20 +00:00
if (waitTicks > 0)
2017-01-07 05:06:54 +00:00
jbus::WaitGCTicks(waitTicks);
2017-01-04 05:27:20 +00:00
}
CGBASupport::EPhase finalPhase = gba.GetPhase();
printf("%s Linked: %d Beat: %d\n",
finalPhase == CGBASupport::EPhase::Complete ? "Complete" : "Failed",
gba.IsFusionLinked(), gba.IsFusionBeat());
return 0;
}