From d785b1eae770e89692d8190d578b369edd0fe321 Mon Sep 17 00:00:00 2001 From: Adrian Conlon Date: Fri, 13 Nov 2020 17:28:35 +0000 Subject: [PATCH] Simplify LR35902 machine timing. --- LR35902/inc/LR35902.h | 21 +++++++++++++-------- LR35902/src/LR35902.cpp | 22 ++++++++++------------ 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/LR35902/inc/LR35902.h b/LR35902/inc/LR35902.h index 93d8353..a8009a4 100644 --- a/LR35902/inc/LR35902.h +++ b/LR35902/inc/LR35902.h @@ -37,6 +37,11 @@ namespace EightBit { [[nodiscard]] uint8_t flaggedInterrupts(); [[nodiscard]] uint8_t maskedInterrupts(); + Signal MachineTicked; + + void tickMachine(const int extra) { for (int i = 0; i < extra; ++i) tickMachine(); } + void tickMachine() { tick(4); MachineTicked.fire(EventArgs::empty()); } + protected: virtual int execute() final; virtual int step() final; @@ -45,46 +50,46 @@ namespace EightBit { void handleINT() final; void memoryWrite() final { - tick(4); + tickMachine(); IntelProcessor::memoryWrite(); } uint8_t memoryRead() final { - tick(4); + tickMachine(); return IntelProcessor::memoryRead(); } void pushWord(register16_t value) final { - tick(4); + tickMachine(); IntelProcessor::pushWord(value); } void jr(int8_t offset) final { IntelProcessor::jr(offset); - tick(4); + tickMachine(); } int jumpConditional(const int condition) final { if (IntelProcessor::jumpConditional(condition)) - tick(4); + tickMachine(); return condition; } int returnConditional(const int condition) final { IntelProcessor::returnConditional(condition); - tick(4); + tickMachine(); return condition; } int jrConditional(const int condition) final { if (!IntelProcessor::jrConditional(condition)) - tick(4); + tickMachine(); return condition; } void ret() final { IntelProcessor::ret(); - tick(4); + tickMachine(); } private: diff --git a/LR35902/src/LR35902.cpp b/LR35902/src/LR35902.cpp index 2b10635..b50f15d 100644 --- a/LR35902/src/LR35902.cpp +++ b/LR35902/src/LR35902.cpp @@ -7,11 +7,9 @@ EightBit::GameBoy::LR35902::LR35902(Bus& memory) : IntelProcessor(memory), m_bus(memory) { - Ticked.connect([this](EventArgs) { - if ((cycles() % 4) == 0) { - m_bus.IO().incrementTimers(); - m_bus.IO().transferDma(); - } + MachineTicked.connect([this](EventArgs) { + m_bus.IO().incrementTimers(); + m_bus.IO().transferDma(); }); } @@ -36,7 +34,7 @@ void EightBit::GameBoy::LR35902::handleRESET() { IntelProcessor::handleRESET(); di(); SP() = Mask16 - 1; - tick(4 * 4); + tickMachine(4); } void EightBit::GameBoy::LR35902::handleINT() { @@ -110,7 +108,7 @@ void EightBit::GameBoy::LR35902::reti() { EightBit::register16_t EightBit::GameBoy::LR35902::add(uint8_t& f, const register16_t operand, const register16_t value) { - tick(4); + tickMachine(); const int addition = operand.word + value.word; const register16_t result = addition; @@ -508,7 +506,7 @@ void EightBit::GameBoy::LR35902::executeOther(const int x, const int y, const in default: UNREACHABLE; } - tick(4); + tickMachine(); break; case 4: { // 8-bit INC auto operand = R(y); @@ -608,7 +606,7 @@ void EightBit::GameBoy::LR35902::executeOther(const int x, const int y, const in case 5: { // GB: ADD SP,dd const auto before = SP().word; const int8_t value = fetchByte(); - tick(2 * 4); + tickMachine(2); const auto result = before + value; SP() = result; const auto carried = before ^ value ^ (result & Mask16); @@ -623,7 +621,7 @@ void EightBit::GameBoy::LR35902::executeOther(const int x, const int y, const in case 7: { // GB: LD HL,SP + dd const auto before = SP().word; const int8_t value = fetchByte(); - tick(4); + tickMachine(); const auto result = before + value; HL() = result; const auto carried = before ^ value ^ (result & Mask16); @@ -654,7 +652,7 @@ void EightBit::GameBoy::LR35902::executeOther(const int x, const int y, const in break; case 3: // LD SP,HL SP() = HL(); - tick(4); + tickMachine(); break; default: UNREACHABLE; @@ -694,7 +692,7 @@ void EightBit::GameBoy::LR35902::executeOther(const int x, const int y, const in switch (y) { case 0: // JP nn jump(MEMPTR() = fetchWord()); - tick(4); + tickMachine(); break; case 1: // CB prefix m_prefixCB = true;