jbus/include/jbus/Common.hpp

171 lines
5.2 KiB
C++
Raw Normal View History

2018-10-06 20:42:04 -07:00
#pragma once
2017-01-06 19:13:23 -08:00
#include <functional>
2017-12-29 00:05:42 -08:00
#include <cstdint>
#include <cstdlib>
2017-01-06 19:13:23 -08:00
2018-12-07 21:20:55 -08:00
namespace jbus {
2017-01-06 19:13:23 -08:00
using s8 = int8_t;
using u8 = uint8_t;
using s16 = int16_t;
using u16 = uint16_t;
using s32 = int32_t;
using u32 = uint32_t;
using s64 = int64_t;
using u64 = uint64_t;
2017-01-07 15:55:41 -08:00
#ifndef DOXYGEN_SHOULD_SKIP_THIS
2017-01-06 19:13:23 -08:00
#undef bswap16
#undef bswap32
#undef bswap64
/* Type-sensitive byte swappers */
template <typename T>
2019-07-19 21:23:46 -07:00
constexpr T bswap16(T val) {
2017-01-06 19:13:23 -08:00
#if __GNUC__
2018-12-07 21:20:55 -08:00
return __builtin_bswap16(val);
2017-01-06 19:13:23 -08:00
#elif _WIN32
2018-12-07 21:20:55 -08:00
return _byteswap_ushort(val);
2017-01-06 19:13:23 -08:00
#else
2018-12-07 21:20:55 -08:00
return (val = (val << 8) | ((val >> 8) & 0xFF));
2017-01-06 19:13:23 -08:00
#endif
}
template <typename T>
2019-07-19 21:23:46 -07:00
constexpr T bswap32(T val) {
2017-01-06 19:13:23 -08:00
#if __GNUC__
2018-12-07 21:20:55 -08:00
return __builtin_bswap32(val);
2017-01-06 19:13:23 -08:00
#elif _WIN32
2018-12-07 21:20:55 -08:00
return _byteswap_ulong(val);
2017-01-06 19:13:23 -08:00
#else
2018-12-07 21:20:55 -08:00
val = (val & 0x0000FFFF) << 16 | (val & 0xFFFF0000) >> 16;
val = (val & 0x00FF00FF) << 8 | (val & 0xFF00FF00) >> 8;
return val;
2017-01-06 19:13:23 -08:00
#endif
}
template <typename T>
2019-07-19 21:23:46 -07:00
constexpr T bswap64(T val) {
2017-01-06 19:13:23 -08:00
#if __GNUC__
2018-12-07 21:20:55 -08:00
return __builtin_bswap64(val);
2017-01-06 19:13:23 -08:00
#elif _WIN32
2018-12-07 21:20:55 -08:00
return _byteswap_uint64(val);
2017-01-06 19:13:23 -08:00
#else
2018-12-07 21:20:55 -08:00
return ((val & 0xFF00000000000000ULL) >> 56) | ((val & 0x00FF000000000000ULL) >> 40) |
((val & 0x0000FF0000000000ULL) >> 24) | ((val & 0x000000FF00000000ULL) >> 8) |
((val & 0x00000000FF000000ULL) << 8) | ((val & 0x0000000000FF0000ULL) << 24) |
((val & 0x000000000000FF00ULL) << 40) | ((val & 0x00000000000000FFULL) << 56);
2017-01-06 19:13:23 -08:00
#endif
}
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
2019-07-19 21:23:46 -07:00
constexpr int16_t SBig(int16_t val) { return bswap16(val); }
constexpr uint16_t SBig(uint16_t val) { return bswap16(val); }
constexpr int32_t SBig(int32_t val) { return bswap32(val); }
constexpr uint32_t SBig(uint32_t val) { return bswap32(val); }
constexpr int64_t SBig(int64_t val) { return bswap64(val); }
constexpr uint64_t SBig(uint64_t val) { return bswap64(val); }
constexpr float SBig(float val) {
2019-06-11 19:03:39 -07:00
union { float f; int32_t i; } uval1 = {val};
union { int32_t i; float f; } uval2 = {bswap32(uval1.i)};
return uval2.f;
2017-01-06 19:13:23 -08:00
}
2019-07-19 21:23:46 -07:00
constexpr double SBig(double val) {
2019-06-11 19:03:39 -07:00
union { double f; int64_t i; } uval1 = {val};
union { int64_t i; double f; } uval2 = {bswap64(uval1.i)};
return uval2.f;
2017-01-06 19:13:23 -08:00
}
#ifndef SBIG
2018-12-07 21:20:55 -08:00
#define SBIG(q) (((q)&0x000000FF) << 24 | ((q)&0x0000FF00) << 8 | ((q)&0x00FF0000) >> 8 | ((q)&0xFF000000) >> 24)
2017-01-06 19:13:23 -08:00
#endif
2019-07-19 21:23:46 -07:00
constexpr int16_t SLittle(int16_t val) { return val; }
constexpr uint16_t SLittle(uint16_t val) { return val; }
constexpr int32_t SLittle(int32_t val) { return val; }
constexpr uint32_t SLittle(uint32_t val) { return val; }
constexpr int64_t SLittle(int64_t val) { return val; }
constexpr uint64_t SLittle(uint64_t val) { return val; }
constexpr float SLittle(float val) { return val; }
constexpr double SLittle(double val) { return val; }
2017-01-06 19:13:23 -08:00
#ifndef SLITTLE
#define SLITTLE(q) (q)
#endif
#else
2019-07-19 21:23:46 -07:00
constexpr int16_t SLittle(int16_t val) { return bswap16(val); }
constexpr uint16_t SLittle(uint16_t val) { return bswap16(val); }
constexpr int32_t SLittle(int32_t val) { return bswap32(val); }
constexpr uint32_t SLittle(uint32_t val) { return bswap32(val); }
constexpr int64_t SLittle(int64_t val) { return bswap64(val); }
constexpr uint64_t SLittle(uint64_t val) { return bswap64(val); }
constexpr float SLittle(float val) {
2018-12-07 21:20:55 -08:00
int32_t ival = bswap32(*((int32_t*)(&val)));
return *((float*)(&ival));
2017-01-06 19:13:23 -08:00
}
2019-07-19 21:23:46 -07:00
constexpr double SLittle(double val) {
2018-12-07 21:20:55 -08:00
int64_t ival = bswap64(*((int64_t*)(&val)));
return *((double*)(&ival));
2017-01-06 19:13:23 -08:00
}
#ifndef SLITTLE
2018-12-07 21:20:55 -08:00
#define SLITTLE(q) (((q)&0x000000FF) << 24 | ((q)&0x0000FF00) << 8 | ((q)&0x00FF0000) >> 8 | ((q)&0xFF000000) >> 24)
2017-01-06 19:13:23 -08:00
#endif
2019-07-19 21:23:46 -07:00
constexpr int16_t SBig(int16_t val) { return val; }
constexpr uint16_t SBig(uint16_t val) { return val; }
constexpr int32_t SBig(int32_t val) { return val; }
constexpr uint32_t SBig(uint32_t val) { return val; }
constexpr int64_t SBig(int64_t val) { return val; }
constexpr uint64_t SBig(uint64_t val) { return val; }
constexpr float SBig(float val) { return val; }
constexpr double SBig(double val) { return val; }
2017-01-06 19:13:23 -08:00
#ifndef SBIG
#define SBIG(q) (q)
#endif
#endif
class Endpoint;
2017-01-06 20:57:28 -08:00
class ThreadLocalEndpoint;
2017-01-06 19:13:23 -08:00
2017-01-07 15:55:41 -08:00
#endif
2018-12-07 21:20:55 -08:00
enum EJStatFlags {
GBA_JSTAT_MASK = 0x3a,
GBA_JSTAT_FLAGS_SHIFT = 4,
GBA_JSTAT_FLAGS_MASK = 0x30,
GBA_JSTAT_PSF1 = 0x20,
GBA_JSTAT_PSF0 = 0x10,
GBA_JSTAT_SEND = 0x08,
GBA_JSTAT_RECV = 0x02
2017-01-06 19:13:23 -08:00
};
2018-12-07 21:20:55 -08:00
enum EJoyReturn {
GBA_READY = 0,
GBA_NOT_READY = 1,
GBA_BUSY = 2,
GBA_JOYBOOT_UNKNOWN_STATE = 3,
GBA_JOYBOOT_ERR_INVALID = 4
2017-01-06 19:13:23 -08:00
};
2017-01-07 15:55:41 -08:00
/** @brief Standard callback for asynchronous jbus::Endpoint APIs.
* @param endpoint Thread-local Endpoint interface for optionally issuing next command in sequence.
* @param status GBA_READY if connection is still open, GBA_NOT_READY if connection lost. */
2017-01-06 20:57:28 -08:00
using FGBACallback = std::function<void(ThreadLocalEndpoint& endpoint, EJoyReturn status)>;
2017-01-06 19:13:23 -08:00
2017-01-07 15:55:41 -08:00
/** @brief Get host system's timebase scaled into Dolphin ticks.
* @return Scaled ticks from host timebase. */
2017-01-06 19:13:23 -08:00
u64 GetGCTicks();
2017-01-07 15:55:41 -08:00
/** @brief Wait an approximate Dolphin tick duration (avoid using, it's rather inaccurate).
* @param ticks CPU ticks to wait. */
2017-01-06 19:13:23 -08:00
void WaitGCTicks(u64 ticks);
2017-01-07 15:55:41 -08:00
/** @brief Obtain CPU ticks per second of Dolphin hardware (clock speed).
* @return 486Mhz - always. */
2019-07-19 21:23:46 -07:00
constexpr u64 GetGCTicksPerSec() { return 486000000ull; }
2017-01-07 15:55:41 -08:00
/** @brief Initialize platform specifics of JBus library */
2017-01-06 19:13:23 -08:00
void Initialize();
2018-12-07 21:20:55 -08:00
} // namespace jbus