From 072f5bb847288b1843e8731c65a01182c8726a16 Mon Sep 17 00:00:00 2001 From: CocoSimone Date: Sun, 15 May 2022 21:35:27 +0200 Subject: [PATCH] regs --- src/core/gb/CMakeLists.txt | 2 +- src/core/gb/Core.cpp | 5 ++ src/core/gb/Core.hpp | 4 +- src/core/gb/Cpu.cpp | 6 +++ src/core/gb/Cpu.hpp | 94 +++++++++++++++++++++++++-------- src/frontend/sdl/CMakeLists.txt | 2 +- src/frontend/sdl/Frontend.hpp | 2 + 7 files changed, 90 insertions(+), 25 deletions(-) diff --git a/src/core/gb/CMakeLists.txt b/src/core/gb/CMakeLists.txt index 1a2d9426..48577f07 100644 --- a/src/core/gb/CMakeLists.txt +++ b/src/core/gb/CMakeLists.txt @@ -4,4 +4,4 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) add_library(gb Core.hpp Core.cpp Cpu.hpp Cpu.cpp Ppu.hpp Ppu.cpp) -target_include_directories(gb PUBLIC . ..) +target_include_directories(gb PUBLIC .) diff --git a/src/core/gb/Core.cpp b/src/core/gb/Core.cpp index e69de29b..3bf765be 100644 --- a/src/core/gb/Core.cpp +++ b/src/core/gb/Core.cpp @@ -0,0 +1,5 @@ +#include + +namespace natsukashii::core { +Core::Core() {} +} \ No newline at end of file diff --git a/src/core/gb/Core.hpp b/src/core/gb/Core.hpp index 3512c54e..728dee25 100644 --- a/src/core/gb/Core.hpp +++ b/src/core/gb/Core.hpp @@ -1,6 +1,6 @@ #pragma once -#include -#include +#include "Cpu.hpp" +#include "Ppu.hpp" namespace natsukashii::core { struct Core { diff --git a/src/core/gb/Cpu.cpp b/src/core/gb/Cpu.cpp index e69de29b..d4f4df7f 100644 --- a/src/core/gb/Cpu.cpp +++ b/src/core/gb/Cpu.cpp @@ -0,0 +1,6 @@ +#include + +namespace natsukashii::core { +Cpu::Cpu() { +} +} \ No newline at end of file diff --git a/src/core/gb/Cpu.hpp b/src/core/gb/Cpu.hpp index 435b2515..c57bc52c 100644 --- a/src/core/gb/Cpu.hpp +++ b/src/core/gb/Cpu.hpp @@ -1,34 +1,86 @@ #pragma once -#include +#include "../common.hpp" namespace natsukashii::core { -template -struct RegisterPair { - A low; - B high; - auto operator=(const u16& rhs) { - low = rhs & 0xff; - high = rhs >> 8; +#define af regs.AF +#define bc regs.BC +#define de regs.DE +#define hl regs.HL + +#define a af.A +#define f af.F +#define b bc.B +#define c bc.C +#define d de.D +#define e de.E +#define h hl.H +#define l hl.L + +#define REGIMPL(type1, reg1, type2, reg2) \ + struct reg##reg1##reg2 { \ + reg##reg1##reg2() {} \ + union { \ + type1 reg1; \ + type2 reg2; \ + }; \ + u16 raw = 0; \ + reg##reg1##reg2& operator=(const u16& rhs) { \ + reg1 = rhs >> 8; \ + reg2 = rhs & 0xff; \ + return *this; \ + } \ + } reg1##reg2 + +struct RegF { + RegF() : raw(0) {} + RegF(const u8& val) : raw(val) {} + u8 raw = 0; + + RegF& operator=(const u8& rhs) { + raw |= ((rhs >> 7) << 7); + raw |= ((rhs >> 6) << 6); + raw |= ((rhs >> 5) << 5); + raw |= ((rhs >> 4) << 4); + return *this; } + + bool zero() { return (raw >> 7) & 1; } + bool negative() { return (raw >> 6) & 1; } + bool halfcarry() { return (raw >> 5) & 1; } + bool carry() { return (raw >> 4) & 1; } + + void zero(const bool& rhs) { + raw &= ~0xF; + raw |= (rhs << 7); + } + + void negative(const bool& rhs) { + raw &= ~0xF; + raw |= (rhs << 6); + } + + void halfcarry(const bool& rhs) { + raw &= ~0xF; + raw |= (rhs << 5); + } + + void carry(const bool& rhs) { + raw &= ~0xF; + raw |= (rhs << 4); + } }; -union RegF { - struct { - unsigned z:1; - unsigned n:1; - unsigned h:1; - unsigned c:1; - unsigned:4; - }; - u8 raw; +struct Registers { + REGIMPL(u8, A, RegF, F); + REGIMPL(u8, B, u8, C); + REGIMPL(u8, C, u8, E); + REGIMPL(u8, D, u8, L); }; struct Cpu { + Cpu(); private: - RegisterPair af; - RegisterPair bc; - RegisterPair de; - RegisterPair hl; + Registers regs; }; } diff --git a/src/frontend/sdl/CMakeLists.txt b/src/frontend/sdl/CMakeLists.txt index 3d0dc11a..1d2bd5a5 100644 --- a/src/frontend/sdl/CMakeLists.txt +++ b/src/frontend/sdl/CMakeLists.txt @@ -6,5 +6,5 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(SDL2 REQUIRED) add_library(frontend Frontend.cpp Frontend.hpp) -target_include_directories(frontend PUBLIC .) +target_include_directories(frontend PUBLIC . ../../core) target_link_libraries(frontend PUBLIC SDL2) diff --git a/src/frontend/sdl/Frontend.hpp b/src/frontend/sdl/Frontend.hpp index 76cfff20..dd9058c7 100644 --- a/src/frontend/sdl/Frontend.hpp +++ b/src/frontend/sdl/Frontend.hpp @@ -1,5 +1,6 @@ #pragma once #include +#include namespace natsukashii::frontend { struct App { @@ -11,5 +12,6 @@ private: SDL_Renderer *renderer = nullptr; Uint32 id; bool quit = false; + core::Core gb; }; }