Merge pull request #5 from lioncash/array

Endpoint: Use std::array where applicable
This commit is contained in:
Phillip Stephens 2019-09-03 19:54:02 -07:00 committed by GitHub
commit 092f7dd0f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 84 additions and 64 deletions

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <array>
#include <condition_variable> #include <condition_variable>
#include <cstddef> #include <cstddef>
#include <functional> #include <functional>
@ -11,9 +12,13 @@
namespace jbus { namespace jbus {
using ReadWriteBuffer = std::array<u8, 4>;
/** Main class for performing JoyBoot and subsequent JoyBus I/O operations. /** Main class for performing JoyBoot and subsequent JoyBus I/O operations.
* Instances should be obtained though the jbus::Listener::accept method. */ * Instances should be obtained though the jbus::Listener::accept method. */
class Endpoint { class Endpoint {
using Buffer = std::array<u8, 5>;
/** Self-contained class for solving Kawasedo's GBA BootROM challenge. /** Self-contained class for solving Kawasedo's GBA BootROM challenge.
* GBA will boot client_pad.bin code on completion. * GBA will boot client_pad.bin code on completion.
* *
@ -51,14 +56,14 @@ class Endpoint {
u32 xc_progLen; u32 xc_progLen;
u8* x10_statusPtr; u8* x10_statusPtr;
FGBACallback x14_callback; FGBACallback x14_callback;
u8 x18_readBuf[4]; ReadWriteBuffer x18_readBuf;
u8 x1c_writeBuf[4]; ReadWriteBuffer x1c_writeBuf;
s32 x20_byteInWindow; s32 x20_byteInWindow;
u64 x28_ticksAfterXf; u64 x28_ticksAfterXf;
u32 x30_justStarted; u32 x30_justStarted;
u32 x34_bytesSent; u32 x34_bytesSent;
u32 x38_crc; u32 x38_crc;
u32 x3c_checkStore[7]; std::array<u32, 7> x3c_checkStore;
s32 x58_currentKey; s32 x58_currentKey;
s32 x5c_initMessage; s32 x5c_initMessage;
s32 x60_gameId; s32 x60_gameId;
@ -112,7 +117,7 @@ class Endpoint {
std::condition_variable m_issueCv; std::condition_variable m_issueCv;
KawasedoChallenge m_joyBoot; KawasedoChallenge m_joyBoot;
FGBACallback m_callback; FGBACallback m_callback;
u8 m_buffer[5]; Buffer m_buffer{};
u8* m_readDstPtr = nullptr; u8* m_readDstPtr = nullptr;
u8* m_statusPtr = nullptr; u8* m_statusPtr = nullptr;
u64 m_lastGCTick = 0; u64 m_lastGCTick = 0;
@ -123,9 +128,9 @@ class Endpoint {
bool m_running = true; bool m_running = true;
void clockSync(); void clockSync();
void send(const u8* buffer); void send(Buffer buffer);
size_t receive(u8* buffer); size_t receive(Buffer& buffer);
size_t runBuffer(u8* buffer, std::unique_lock<std::mutex>& lk); size_t runBuffer(Buffer& buffer, std::unique_lock<std::mutex>& lk);
bool idleGetStatus(std::unique_lock<std::mutex>& lk); bool idleGetStatus(std::unique_lock<std::mutex>& lk);
void transferProc(); void transferProc();
void transferWakeup(ThreadLocalEndpoint& endpoint, u8 status); void transferWakeup(ThreadLocalEndpoint& endpoint, u8 status);
@ -166,30 +171,30 @@ public:
EJoyReturn GBAReset(u8* status); EJoyReturn GBAReset(u8* status);
/** @brief Send READ command to GBA asynchronously. /** @brief Send READ command to GBA asynchronously.
* @param dst Destination pointer for 4-byte packet of data. * @param dst Destination reference for 4-byte packet of data.
* @param status Destination pointer for EJStatFlags. * @param status Destination pointer for EJStatFlags.
* @param callback Functor to execute when operation completes. * @param callback Functor to execute when operation completes.
* @return GBA_READY if submitted, or GBA_NOT_READY if another operation in progress. */ * @return GBA_READY if submitted, or GBA_NOT_READY if another operation in progress. */
EJoyReturn GBAReadAsync(u8* dst, u8* status, FGBACallback&& callback); EJoyReturn GBAReadAsync(ReadWriteBuffer& dst, u8* status, FGBACallback&& callback);
/** @brief Send READ command to GBA synchronously. /** @brief Send READ command to GBA synchronously.
* @param dst Destination pointer for 4-byte packet of data. * @param dst Destination reference for 4-byte packet of data.
* @param status Destination pointer for EJStatFlags. * @param status Destination pointer for EJStatFlags.
* @return GBA_READY if submitted, or GBA_NOT_READY if another operation in progress. */ * @return GBA_READY if submitted, or GBA_NOT_READY if another operation in progress. */
EJoyReturn GBARead(u8* dst, u8* status); EJoyReturn GBARead(ReadWriteBuffer& dst, u8* status);
/** @brief Send WRITE command to GBA asynchronously. /** @brief Send WRITE command to GBA asynchronously.
* @param src Source pointer for 4-byte packet of data. It is not required to keep resident. * @param src Source pointer for 4-byte packet of data. It is not required to keep resident.
* @param status Destination pointer for EJStatFlags. * @param status Destination pointer for EJStatFlags.
* @param callback Functor to execute when operation completes. * @param callback Functor to execute when operation completes.
* @return GBA_READY if submitted, or GBA_NOT_READY if another operation in progress. */ * @return GBA_READY if submitted, or GBA_NOT_READY if another operation in progress. */
EJoyReturn GBAWriteAsync(const u8* src, u8* status, FGBACallback&& callback); EJoyReturn GBAWriteAsync(ReadWriteBuffer src, u8* status, FGBACallback&& callback);
/** @brief Send WRITE command to GBA synchronously. /** @brief Send WRITE command to GBA synchronously.
* @param src Source pointer for 4-byte packet of data. It is not required to keep resident. * @param src Source pointer for 4-byte packet of data. It is not required to keep resident.
* @param status Destination pointer for EJStatFlags. * @param status Destination pointer for EJStatFlags.
* @return GBA_READY if submitted, or GBA_NOT_READY if another operation in progress. */ * @return GBA_READY if submitted, or GBA_NOT_READY if another operation in progress. */
EJoyReturn GBAWrite(const u8* src, u8* status); EJoyReturn GBAWrite(ReadWriteBuffer src, u8* status);
/** @brief Initiate JoyBoot sequence on this endpoint. /** @brief Initiate JoyBoot sequence on this endpoint.
* @param paletteColor Palette for displaying logo in ROM header [0,6]. * @param paletteColor Palette for displaying logo in ROM header [0,6].
@ -244,18 +249,18 @@ public:
EJoyReturn GBAResetAsync(u8* status, FGBACallback&& callback); EJoyReturn GBAResetAsync(u8* status, FGBACallback&& callback);
/** @brief Send READ command to GBA asynchronously. /** @brief Send READ command to GBA asynchronously.
* @param dst Destination pointer for 4-byte packet of data. * @param dst Destination reference for 4-byte packet of data.
* @param status Destination pointer for EJStatFlags. * @param status Destination pointer for EJStatFlags.
* @param callback Functor to execute when operation completes. * @param callback Functor to execute when operation completes.
* @return GBA_READY if submitted, or GBA_NOT_READY if another operation in progress. */ * @return GBA_READY if submitted, or GBA_NOT_READY if another operation in progress. */
EJoyReturn GBAReadAsync(u8* dst, u8* status, FGBACallback&& callback); EJoyReturn GBAReadAsync(ReadWriteBuffer& dst, u8* status, FGBACallback&& callback);
/** @brief Send WRITE command to GBA asynchronously. /** @brief Send WRITE command to GBA asynchronously.
* @param src Source pointer for 4-byte packet of data. It is not required to keep resident. * @param src 4-byte packet of data. It is not required to keep resident.
* @param status Destination pointer for EJStatFlags. * @param status Destination pointer for EJStatFlags.
* @param callback Functor to execute when operation completes. * @param callback Functor to execute when operation completes.
* @return GBA_READY if submitted, or GBA_NOT_READY if another operation in progress. */ * @return GBA_READY if submitted, or GBA_NOT_READY if another operation in progress. */
EJoyReturn GBAWriteAsync(const u8* src, u8* status, FGBACallback&& callback); EJoyReturn GBAWriteAsync(ReadWriteBuffer src, u8* status, FGBACallback&& callback);
/** @brief Get virtual SI channel assigned to this endpoint. /** @brief Get virtual SI channel assigned to this endpoint.
* @return SI channel */ * @return SI channel */

View File

@ -1,6 +1,6 @@
#include "jbus/Endpoint.hpp" #include "jbus/Endpoint.hpp"
#include <cstring> #include <algorithm>
#define LOG_TRANSFER 0 #define LOG_TRANSFER 0
@ -361,18 +361,20 @@ void Endpoint::clockSync() {
m_running = false; m_running = false;
} }
void Endpoint::send(const u8* buffer) { void Endpoint::send(Buffer buffer) {
m_lastCmd = buffer[0]; m_lastCmd = buffer[0];
net::Socket::EResult result; net::Socket::EResult result;
size_t sentBytes; size_t sentBytes;
if (m_lastCmd == CMD_WRITE) if (m_lastCmd == CMD_WRITE) {
result = m_dataSocket.send(buffer, 5, sentBytes); result = m_dataSocket.send(buffer.data(), buffer.size(), sentBytes);
else } else {
result = m_dataSocket.send(buffer, 1, sentBytes); result = m_dataSocket.send(buffer.data(), 1, sentBytes);
}
if (m_lastCmd != CMD_STATUS) if (m_lastCmd != CMD_STATUS) {
m_booted = true; m_booted = true;
}
if (result != net::Socket::EResult::OK) { if (result != net::Socket::EResult::OK) {
m_running = false; m_running = false;
@ -384,30 +386,31 @@ void Endpoint::send(const u8* buffer) {
#endif #endif
} }
size_t Endpoint::receive(u8* buffer) { size_t Endpoint::receive(Buffer& buffer) {
if (!m_dataSocket) { if (!m_dataSocket) {
m_running = false; m_running = false;
return 5; return buffer.size();
} }
size_t recvBytes = 0; size_t recvBytes = 0;
net::Socket::EResult result = m_dataSocket.recv(buffer, 5, recvBytes); const net::Socket::EResult result = m_dataSocket.recv(buffer.data(), buffer.size(), recvBytes);
if (result == net::Socket::EResult::Error) { if (result == net::Socket::EResult::Error) {
m_running = false; m_running = false;
return 5; return buffer.size();
} }
if (recvBytes > 5) if (recvBytes > buffer.size()) {
recvBytes = 5; recvBytes = buffer.size();
}
#if LOG_TRANSFER #if LOG_TRANSFER
if (recvBytes > 0) { if (recvBytes > 0) {
if (m_lastCmd == CMD_STATUS || m_lastCmd == CMD_RESET) { if (m_lastCmd == CMD_STATUS || m_lastCmd == CMD_RESET) {
printf("Stat/Reset [< %02x%02x%02x%02x%02x] (%lu)\n", (u8)buffer[0], (u8)buffer[1], (u8)buffer[2], (u8)buffer[3], printf("Stat/Reset [< %02x%02x%02x%02x%02x] (%lu)\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4],
(u8)buffer[4], recvBytes); recvBytes);
} else { } else {
printf("Receive [< %02x%02x%02x%02x%02x] (%lu)\n", (u8)buffer[0], (u8)buffer[1], (u8)buffer[2], (u8)buffer[3], printf("Receive [< %02x%02x%02x%02x%02x] (%lu)\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4],
(u8)buffer[4], recvBytes); recvBytes);
} }
} }
#endif #endif
@ -415,23 +418,22 @@ size_t Endpoint::receive(u8* buffer) {
return recvBytes; return recvBytes;
} }
size_t Endpoint::runBuffer(u8* buffer, std::unique_lock<std::mutex>& lk) { size_t Endpoint::runBuffer(Buffer& buffer, std::unique_lock<std::mutex>& lk) {
u8 tmpBuffer[5]; Buffer tmpBuffer = buffer;
memmove(tmpBuffer, buffer, 5);
lk.unlock(); lk.unlock();
clockSync(); clockSync();
send(tmpBuffer); send(tmpBuffer);
size_t receivedBytes = receive(tmpBuffer); const size_t receivedBytes = receive(tmpBuffer);
lk.lock(); lk.lock();
memmove(buffer, tmpBuffer, 5);
buffer = tmpBuffer;
return receivedBytes; return receivedBytes;
} }
bool Endpoint::idleGetStatus(std::unique_lock<std::mutex>& lk) { bool Endpoint::idleGetStatus(std::unique_lock<std::mutex>& lk) {
u8 buffer[] = {CMD_STATUS, 0, 0, 0, 0}; Buffer buffer{u8(CMD_STATUS), 0, 0, 0, 0};
return runBuffer(buffer, lk); return runBuffer(buffer, lk) != 0;
} }
void Endpoint::transferProc() { void Endpoint::transferProc() {
@ -461,10 +463,12 @@ void Endpoint::transferProc() {
*m_statusPtr = m_buffer[0]; *m_statusPtr = m_buffer[0];
break; break;
case CMD_READ: case CMD_READ:
if (m_statusPtr) if (m_statusPtr != nullptr) {
*m_statusPtr = m_buffer[4]; *m_statusPtr = m_buffer[4];
if (m_readDstPtr) }
memmove(m_readDstPtr, m_buffer, 4); if (m_readDstPtr != nullptr) {
std::copy(m_buffer.cbegin(), m_buffer.cbegin() + 4, m_readDstPtr);
}
break; break;
default: default:
break; break;
@ -600,17 +604,19 @@ EJoyReturn Endpoint::GBAReset(u8* status) {
return GBA_READY; return GBA_READY;
} }
EJoyReturn Endpoint::GBAReadAsync(u8* dst, u8* status, FGBACallback&& callback) { EJoyReturn Endpoint::GBAReadAsync(ReadWriteBuffer& dst, u8* status, FGBACallback&& callback) {
if (!m_running) if (!m_running) {
return GBA_NOT_READY; return GBA_NOT_READY;
}
std::unique_lock<std::mutex> lk(m_syncLock); std::unique_lock<std::mutex> lk(m_syncLock);
if (m_cmdIssued) if (m_cmdIssued) {
return GBA_NOT_READY; return GBA_NOT_READY;
}
m_cmdIssued = true; m_cmdIssued = true;
m_statusPtr = status; m_statusPtr = status;
m_readDstPtr = dst; m_readDstPtr = dst.data();
m_buffer[0] = CMD_READ; m_buffer[0] = CMD_READ;
m_callback = std::move(callback); m_callback = std::move(callback);
@ -619,17 +625,19 @@ EJoyReturn Endpoint::GBAReadAsync(u8* dst, u8* status, FGBACallback&& callback)
return GBA_READY; return GBA_READY;
} }
EJoyReturn Endpoint::GBARead(u8* dst, u8* status) { EJoyReturn Endpoint::GBARead(ReadWriteBuffer& dst, u8* status) {
if (!m_running) if (!m_running) {
return GBA_NOT_READY; return GBA_NOT_READY;
}
std::unique_lock<std::mutex> lk(m_syncLock); std::unique_lock<std::mutex> lk(m_syncLock);
if (m_cmdIssued) if (m_cmdIssued) {
return GBA_NOT_READY; return GBA_NOT_READY;
}
m_cmdIssued = true; m_cmdIssued = true;
m_statusPtr = status; m_statusPtr = status;
m_readDstPtr = dst; m_readDstPtr = dst.data();
m_buffer[0] = CMD_READ; m_buffer[0] = CMD_READ;
m_callback = bindSync(); m_callback = bindSync();
@ -639,19 +647,22 @@ EJoyReturn Endpoint::GBARead(u8* dst, u8* status) {
return GBA_READY; return GBA_READY;
} }
EJoyReturn Endpoint::GBAWriteAsync(const u8* src, u8* status, FGBACallback&& callback) { EJoyReturn Endpoint::GBAWriteAsync(ReadWriteBuffer src, u8* status, FGBACallback&& callback) {
if (!m_running) if (!m_running) {
return GBA_NOT_READY; return GBA_NOT_READY;
}
std::unique_lock<std::mutex> lk(m_syncLock); std::unique_lock<std::mutex> lk(m_syncLock);
if (m_cmdIssued) if (m_cmdIssued) {
return GBA_NOT_READY; return GBA_NOT_READY;
}
m_cmdIssued = true; m_cmdIssued = true;
m_statusPtr = status; m_statusPtr = status;
m_buffer[0] = CMD_WRITE; m_buffer[0] = CMD_WRITE;
for (int i = 0; i < 4; ++i) for (size_t i = 0; i < src.size(); ++i) {
m_buffer[i + 1] = src[i]; m_buffer[i + 1] = src[i];
}
m_callback = std::move(callback); m_callback = std::move(callback);
m_issueCv.notify_one(); m_issueCv.notify_one();
@ -659,19 +670,22 @@ EJoyReturn Endpoint::GBAWriteAsync(const u8* src, u8* status, FGBACallback&& cal
return GBA_READY; return GBA_READY;
} }
EJoyReturn Endpoint::GBAWrite(const u8* src, u8* status) { EJoyReturn Endpoint::GBAWrite(ReadWriteBuffer src, u8* status) {
if (!m_running) if (!m_running) {
return GBA_NOT_READY; return GBA_NOT_READY;
}
std::unique_lock<std::mutex> lk(m_syncLock); std::unique_lock<std::mutex> lk(m_syncLock);
if (m_cmdIssued) if (m_cmdIssued) {
return GBA_NOT_READY; return GBA_NOT_READY;
}
m_cmdIssued = true; m_cmdIssued = true;
m_statusPtr = status; m_statusPtr = status;
m_buffer[0] = CMD_WRITE; m_buffer[0] = CMD_WRITE;
for (int i = 0; i < 4; ++i) for (size_t i = 0; i < src.size(); ++i) {
m_buffer[i + 1] = src[i]; m_buffer[i + 1] = src[i];
}
m_callback = bindSync(); m_callback = bindSync();
m_issueCv.notify_one(); m_issueCv.notify_one();
@ -739,28 +753,29 @@ EJoyReturn ThreadLocalEndpoint::GBAResetAsync(u8* status, FGBACallback&& callbac
return GBA_READY; return GBA_READY;
} }
EJoyReturn ThreadLocalEndpoint::GBAReadAsync(u8* dst, u8* status, FGBACallback&& callback) { EJoyReturn ThreadLocalEndpoint::GBAReadAsync(ReadWriteBuffer& dst, u8* status, FGBACallback&& callback) {
if (!m_ep.m_running || m_ep.m_cmdIssued) if (!m_ep.m_running || m_ep.m_cmdIssued)
return GBA_NOT_READY; return GBA_NOT_READY;
m_ep.m_cmdIssued = true; m_ep.m_cmdIssued = true;
m_ep.m_statusPtr = status; m_ep.m_statusPtr = status;
m_ep.m_readDstPtr = dst; m_ep.m_readDstPtr = dst.data();
m_ep.m_buffer[0] = Endpoint::CMD_READ; m_ep.m_buffer[0] = Endpoint::CMD_READ;
m_ep.m_callback = std::move(callback); m_ep.m_callback = std::move(callback);
return GBA_READY; return GBA_READY;
} }
EJoyReturn ThreadLocalEndpoint::GBAWriteAsync(const u8* src, u8* status, FGBACallback&& callback) { EJoyReturn ThreadLocalEndpoint::GBAWriteAsync(ReadWriteBuffer src, u8* status, FGBACallback&& callback) {
if (!m_ep.m_running || m_ep.m_cmdIssued) if (!m_ep.m_running || m_ep.m_cmdIssued)
return GBA_NOT_READY; return GBA_NOT_READY;
m_ep.m_cmdIssued = true; m_ep.m_cmdIssued = true;
m_ep.m_statusPtr = status; m_ep.m_statusPtr = status;
m_ep.m_buffer[0] = Endpoint::CMD_WRITE; m_ep.m_buffer[0] = Endpoint::CMD_WRITE;
for (int i = 0; i < 4; ++i) for (size_t i = 0; i < src.size(); ++i) {
m_ep.m_buffer[i + 1] = src[i]; m_ep.m_buffer[i + 1] = src[i];
}
m_ep.m_callback = std::move(callback); m_ep.m_callback = std::move(callback);
return GBA_READY; return GBA_READY;