diff --git a/src/core/common.hpp b/src/core/common.hpp index fc68d029..6fa5f89f 100644 --- a/src/core/common.hpp +++ b/src/core/common.hpp @@ -14,7 +14,20 @@ using s64 = int64_t; using u128 = __uint128_t; using s128 = __int128_t; using m128 = __m128i; + #define UINT128_MAX ((u128)0xFFFF'FFFF'FFFF'FFFF << 64) | 0xFFFF'FFFF'FFFF'FFFF #define UINT128_MIN 0 #define INT128_MAX ((u128)0x7FFF'FFFF'FFFF'FFFF << 64) | 0xFFFF'FFFF'FFFF'FFFF #define INT128_MIN (-INT128_MAX - 1LL) + +template +using BitSliceType = +std::conditional<(end - start) <= 7, u8, + std::conditional<(end - start) <= 15, u16, + std::conditional<(end - start) <= 31, u32, + std::conditional<(end - start) <= 63, u64, + std::conditional<(end - start) <= 127, u128, u128> + > + > + > +>; diff --git a/src/core/gb/Cpu.hpp b/src/core/gb/Cpu.hpp index 0992de0b..435b2515 100644 --- a/src/core/gb/Cpu.hpp +++ b/src/core/gb/Cpu.hpp @@ -2,5 +2,33 @@ #include namespace natsukashii::core { -struct Cpu {}; +template +struct RegisterPair { + A low; + B high; + auto operator=(const u16& rhs) { + low = rhs & 0xff; + high = rhs >> 8; + return *this; + } +}; + +union RegF { + struct { + unsigned z:1; + unsigned n:1; + unsigned h:1; + unsigned c:1; + unsigned:4; + }; + u8 raw; +}; + +struct Cpu { +private: + RegisterPair af; + RegisterPair bc; + RegisterPair de; + RegisterPair hl; +}; } diff --git a/src/core/util.hpp b/src/core/util.hpp new file mode 100644 index 00000000..27b1902f --- /dev/null +++ b/src/core/util.hpp @@ -0,0 +1,12 @@ +#pragma once +#include +#include + +namespace natsukashii::util { +template +BitSliceType BitSlice(const T& num) { + static_assert(end - start > 127); + static_assert(end - start > sizeof(T)); + return (num >> start) & ((1 << end) - 1); +} +}