diff --git a/LR35902/inc/Bus.h b/LR35902/inc/Bus.h index 210640d..21806e2 100644 --- a/LR35902/inc/Bus.h +++ b/LR35902/inc/Bus.h @@ -163,9 +163,9 @@ namespace EightBit { return (peekRegister(TAC) & Processor::Bit2) == 0; } - void incrementDIV() { - auto current = peekRegister(DIV); - pokeRegister(DIV, ++current); + void incrementDIV(int cycles) { + m_divCounter.word += cycles; + pokeRegister(DIV, m_divCounter.high); } void incrementTIMA() { @@ -218,13 +218,12 @@ namespace EightBit { int m_romBank; int m_ramBank; - int m_divCounter; + register16_t m_divCounter; int m_timerCounter; int m_timerRate; void Bus_WrittenByte(const AddressEventArgs& e); - void checkDiv(int cycles); void checkTimer(int cycles); void validateCartridgeType(); diff --git a/LR35902/src/Bus.cpp b/LR35902/src/Bus.cpp index 5767797..3741f6b 100644 --- a/LR35902/src/Bus.cpp +++ b/LR35902/src/Bus.cpp @@ -12,17 +12,19 @@ EightBit::Bus::Bus() m_ramBankSwitching(false), m_romBank(1), m_ramBank(0), - m_divCounter(256), m_timerCounter(0), m_timerRate(0) { m_bootRom.resize(0x100); m_gameRom.resize(0x10000); WrittenByte.connect(std::bind(&Bus::Bus_WrittenByte, this, std::placeholders::_1)); + m_divCounter.word = 0xabcc; } void EightBit::Bus::reset() { pokeRegister(NR52, 0xf1); pokeRegister(LCDC, DisplayBackground | BackgroundCharacterDataSelection | LcdEnable); + m_divCounter.word = 0xabcc; + m_timerCounter = 0; } void EightBit::Bus::clear() { @@ -109,7 +111,7 @@ void EightBit::Bus::Bus_WrittenByte(const AddressEventArgs& e) { case BASE + DIV: // R/W Memory::reference() = 0; - m_timerCounter = 0; + m_timerCounter = m_divCounter.word = 0; break; case BASE + TIMA: // R/W case BASE + TMA: // R/W @@ -164,18 +166,10 @@ void EightBit::Bus::Bus_WrittenByte(const AddressEventArgs& e) { } void EightBit::Bus::checkTimers(int cycles) { - checkDiv(cycles); + incrementDIV(cycles); checkTimer(cycles); } -void EightBit::Bus::checkDiv(int cycles) { - m_divCounter -= cycles; - if (m_divCounter <= 0) { - m_divCounter = 256; - incrementDIV(); - } -} - void EightBit::Bus::checkTimer(int cycles) { if (timerEnabled()) { m_timerCounter -= cycles;