Skip to content

Commit

Permalink
Access IO registers directly in doTimers (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
kremi151 committed Dec 28, 2020
1 parent 1a0399e commit 4392ed6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
10 changes: 5 additions & 5 deletions core/source/emulator/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions core/source/emulator/io_registers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 4392ed6

Please sign in to comment.