From a2a6de65a1f4f2c5b2b5ab291fce4b613a815e80 Mon Sep 17 00:00:00 2001 From: CocoSimone Date: Sat, 7 May 2022 12:15:09 +0200 Subject: [PATCH] cursed bit slice helper --- src/core/common.hpp | 13 +++++++++++++ src/core/gb/Cpu.hpp | 30 +++++++++++++++++++++++++++++- src/core/util.hpp | 12 ++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/core/util.hpp 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); +} +}