From 4dd3106af0f8d5a293e1175347d787b85eb0c49b Mon Sep 17 00:00:00 2001 From: Adrian Conlon Date: Mon, 8 Mar 2021 16:42:01 +0000 Subject: [PATCH] Tidy up some C++ issues in GameBoy emulator Signed-off-by: Adrian Conlon --- LR35902/inc/LR35902.h | 152 +++++----------------------------------- LR35902/src/LR35902.cpp | 127 +++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+), 135 deletions(-) diff --git a/LR35902/inc/LR35902.h b/LR35902/inc/LR35902.h index a8009a4..cfaf6fa 100644 --- a/LR35902/inc/LR35902.h +++ b/LR35902/inc/LR35902.h @@ -43,54 +43,22 @@ namespace EightBit { void tickMachine() { tick(4); MachineTicked.fire(EventArgs::empty()); } protected: - virtual int execute() final; - virtual int step() final; + int execute() final; + int step() final; void handleRESET() final; void handleINT() final; - void memoryWrite() final { - tickMachine(); - IntelProcessor::memoryWrite(); - } + void memoryWrite() final; + uint8_t memoryRead() final; - uint8_t memoryRead() final { - tickMachine(); - return IntelProcessor::memoryRead(); - } + void pushWord(register16_t value) final; - void pushWord(register16_t value) final { - tickMachine(); - IntelProcessor::pushWord(value); - } - - void jr(int8_t offset) final { - IntelProcessor::jr(offset); - tickMachine(); - } - - int jumpConditional(const int condition) final { - if (IntelProcessor::jumpConditional(condition)) - tickMachine(); - return condition; - } - - int returnConditional(const int condition) final { - IntelProcessor::returnConditional(condition); - tickMachine(); - return condition; - } - - int jrConditional(const int condition) final { - if (!IntelProcessor::jrConditional(condition)) - tickMachine(); - return condition; - } - - void ret() final { - IntelProcessor::ret(); - tickMachine(); - } + void jr(int8_t offset) final; + int jumpConditional(int condition) final; + int returnConditional(int condition) final; + int jrConditional(int condition) final; + void ret() final; private: Bus& m_bus; @@ -105,97 +73,11 @@ namespace EightBit { bool m_prefixCB = false; - [[nodiscard]] auto R(const int r) { - ASSUME(r >= 0); - ASSUME(r <= 7); - switch (r) { - case 0: - return B(); - case 1: - return C(); - case 2: - return D(); - case 3: - return E(); - case 4: - return H(); - case 5: - return L(); - case 6: - return IntelProcessor::memoryRead(HL()); - case 7: - return A(); - default: - UNREACHABLE; - } - } + [[nodiscard]] uint8_t R(int r); + void R(int r, uint8_t value); - void R(const int r, const uint8_t value) { - ASSUME(r >= 0); - ASSUME(r <= 7); - switch (r) { - case 0: - B() = value; - break; - case 1: - C() = value; - break; - case 2: - D() = value; - break; - case 3: - E() = value; - break; - case 4: - H() = value; - break; - case 5: - L() = value; - break; - case 6: - IntelProcessor::memoryWrite(HL(), value); - break; - case 7: - A() = value; - break; - default: - UNREACHABLE; - } - } - - [[nodiscard]] auto& RP(const int rp) { - ASSUME(rp >= 0); - ASSUME(rp <= 3); - switch (rp) { - case 0b00: - return BC(); - case 0b01: - return DE(); - case 0b10: - return HL(); - case 0b11: - return SP(); - default: - UNREACHABLE; - } - } - - [[nodiscard]] auto& RP2(const int rp) { - ASSUME(rp >= 0); - ASSUME(rp <= 3); - switch (rp) { - case 0b00: - return BC(); - case 0b01: - return DE(); - case 0b10: - return HL(); - case 0b11: - return AF(); - default: - UNREACHABLE; - } - } + [[nodiscard]] register16_t& RP(int rp); + [[nodiscard]] register16_t& RP2(int rp); [[nodiscard]] static auto adjustHalfCarryAdd(uint8_t f, const uint8_t before, const uint8_t value, const int calculation) { return setBit(f, HC, calculateHalfCarryAdd(before, value, calculation)); @@ -217,7 +99,7 @@ namespace EightBit { void stop(bool value = true) noexcept { m_stopped = value; } void start() noexcept { stop(false); } - bool stopped() const noexcept { return m_stopped; } + [[nodiscard]] bool stopped() const noexcept { return m_stopped; } void di(); void ei(); @@ -255,9 +137,9 @@ namespace EightBit { static void scf(uint8_t& f, uint8_t operand); static void ccf(uint8_t& f, uint8_t operand); - static uint8_t cpl(uint8_t& f, uint8_t operand); + [[nodiscard]] static uint8_t cpl(uint8_t& f, uint8_t operand); - static uint8_t swap(uint8_t& f, uint8_t operand); + [[nodiscard]] static uint8_t swap(uint8_t& f, uint8_t operand); }; } } \ No newline at end of file diff --git a/LR35902/src/LR35902.cpp b/LR35902/src/LR35902.cpp index b50f15d..20d6a27 100644 --- a/LR35902/src/LR35902.cpp +++ b/LR35902/src/LR35902.cpp @@ -44,6 +44,49 @@ void EightBit::GameBoy::LR35902::handleINT() { restart(BUS().DATA()); } +void EightBit::GameBoy::LR35902::memoryWrite() { + tickMachine(); + IntelProcessor::memoryWrite(); +} + +uint8_t EightBit::GameBoy::LR35902::memoryRead() { + tickMachine(); + return IntelProcessor::memoryRead(); +} + +void EightBit::GameBoy::LR35902::pushWord(register16_t value) { + tickMachine(); + IntelProcessor::pushWord(value); +} + +void EightBit::GameBoy::LR35902::jr(int8_t offset) { + IntelProcessor::jr(offset); + tickMachine(); +} + +int EightBit::GameBoy::LR35902::jumpConditional(const int condition) { + if (IntelProcessor::jumpConditional(condition)) + tickMachine(); + return condition; +} + +int EightBit::GameBoy::LR35902::returnConditional(const int condition) { + IntelProcessor::returnConditional(condition); + tickMachine(); + return condition; +} + +int EightBit::GameBoy::LR35902::jrConditional(const int condition) { + if (!IntelProcessor::jrConditional(condition)) + tickMachine(); + return condition; +} + +void EightBit::GameBoy::LR35902::ret() { + IntelProcessor::ret(); + tickMachine(); +} + void EightBit::GameBoy::LR35902::di() { IME() = false; } @@ -68,6 +111,90 @@ uint8_t EightBit::GameBoy::LR35902::decrement(uint8_t& f, const uint8_t operand) return result; } +uint8_t EightBit::GameBoy::LR35902::R(const int r) { + switch (r) { + case 0: + return B(); + case 1: + return C(); + case 2: + return D(); + case 3: + return E(); + case 4: + return H(); + case 5: + return L(); + case 6: + return IntelProcessor::memoryRead(HL()); + case 7: + return A(); + default: + UNREACHABLE; + } +} + +void EightBit::GameBoy::LR35902::R(const int r, const uint8_t value) { + switch (r) { + case 0: + B() = value; + break; + case 1: + C() = value; + break; + case 2: + D() = value; + break; + case 3: + E() = value; + break; + case 4: + H() = value; + break; + case 5: + L() = value; + break; + case 6: + IntelProcessor::memoryWrite(HL(), value); + break; + case 7: + A() = value; + break; + default: + UNREACHABLE; + } +} + +EightBit::register16_t& EightBit::GameBoy::LR35902::RP(const int rp) { + switch (rp) { + case 0b00: + return BC(); + case 0b01: + return DE(); + case 0b10: + return HL(); + case 0b11: + return SP(); + default: + UNREACHABLE; + } +} + +EightBit::register16_t& EightBit::GameBoy::LR35902::RP2(const int rp) { + switch (rp) { + case 0b00: + return BC(); + case 0b01: + return DE(); + case 0b10: + return HL(); + case 0b11: + return AF(); + default: + UNREACHABLE; + } +} + bool EightBit::GameBoy::LR35902::convertCondition(const uint8_t f, int flag) { ASSUME(flag >= 0); ASSUME(flag <= 7);