From 4392ed6988b9bbc9b633d3a0a190aa60da0cd54b Mon Sep 17 00:00:00 2001 From: Michel Kremer Date: Mon, 28 Dec 2020 23:04:10 +0100 Subject: [PATCH] Access IO registers directly in doTimers (#20) --- core/source/emulator/cpu.cpp | 10 +++++----- core/source/emulator/io_registers.h | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/core/source/emulator/cpu.cpp b/core/source/emulator/cpu.cpp index 975b70ab..b4cbc7f7 100644 --- a/core/source/emulator/cpu.cpp +++ b/core/source/emulator/cpu.cpp @@ -341,25 +341,25 @@ void CPU::doTimers(Memory &memory, u8 clocks) { timerOverflowingCycles -= clocks; if (timerOverflowingCycles <= 0) { //fprintf(stdout, "# Request Timer interrupt\n"); - memory.write8BitsTo(FB_REG_TIMA, memory.read8BitsAt(FB_REG_TMA)); + ioRegisters.getTIMA() = ioRegisters.getTMA(); requestInterrupt(InterruptType::TIMER); timerOverflowingCycles = -1; } } - u8 tac = memory.read8BitsAt(FB_REG_TAC); + u8 tac = ioRegisters.getTAC(); bool comp1 = (tac & 0b100u) != 0; comp1 &= doTimerObscureCheck(clocks, sysCounter, tac); // Falling edge detector if (delayedTIMAIncrease && !comp1) { - u8 tima = memory.read8BitsAt(FB_REG_TIMA); + u8 tima = ioRegisters.getTIMA(); if (tima == 0xff) { //fprintf(stdout, "# TIMA has overflown\n"); // Delay TIMA load by 1 m-cycle timerOverflowingCycles = 4; // In the meantime, set TIMA to 0 - memory.write8BitsTo(FB_REG_TIMA, 0x00); + ioRegisters.getTIMA() = 0x00; } else if (timerOverflowingCycles == -1) { // TIME has to be 0 for one full m-cycle, so we do not increase here in that case - memory.write8BitsTo(FB_REG_TIMA, tima + 1); + ioRegisters.getTIMA() = tima + 1; } } delayedTIMAIncrease = comp1; diff --git a/core/source/emulator/io_registers.h b/core/source/emulator/io_registers.h index a61a3848..7b2637e2 100644 --- a/core/source/emulator/io_registers.h +++ b/core/source/emulator/io_registers.h @@ -46,6 +46,9 @@ #define __FB_REG_OFFSET_P1 (FB_REG_P1 - 0xFF00) #define __FB_REG_OFFSET_DIV (FB_REG_DIV - 0xFF00) +#define __FB_REG_OFFSET_TIMA (FB_REG_TIMA - 0xFF00) +#define __FB_REG_OFFSET_TMA (FB_REG_TMA - 0xFF00) +#define __FB_REG_OFFSET_TAC (FB_REG_TAC - 0xFF00) #define __FB_REG_OFFSET_IF (FB_REG_IF - 0xFF00) #define __FB_REG_OFFSET_LCDC (FB_REG_LCDC - 0xFF00) #define __FB_REG_OFFSET_STAT (FB_REG_STAT - 0xFF00) @@ -92,6 +95,18 @@ namespace FunkyBoy { return *(hwIO + __FB_REG_OFFSET_P1); } + inline u8 &getTIMA() { + return *(hwIO + __FB_REG_OFFSET_TIMA); + } + + inline u8 &getTMA() { + return *(hwIO + __FB_REG_OFFSET_TMA); + } + + inline u8 &getTAC() { + return *(hwIO + __FB_REG_OFFSET_TAC); + } + inline u8 &getIF() { return *(hwIO + __FB_REG_OFFSET_IF); }