From da438ffb85116e7f4ff47346b0afecd624d757e3 Mon Sep 17 00:00:00 2001 From: Adrian Conlon Date: Sat, 30 Sep 2017 00:03:42 +0100 Subject: [PATCH] Try to get better cycle count/adjustments. --- LR35902/inc/LR35902.h | 6 ++++-- LR35902/src/LR35902.cpp | 24 +++++++++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/LR35902/inc/LR35902.h b/LR35902/inc/LR35902.h index 78a7774..9ccee36 100644 --- a/LR35902/inc/LR35902.h +++ b/LR35902/inc/LR35902.h @@ -40,13 +40,15 @@ namespace EightBit { virtual void reset() override; int runRasterLines(); - int runRasterLine(); int runVerticalBlankLines(); int singleStep(); protected: - int runRasterLines(int limit); + int runRasterLines(int limit, int lines); + int runVerticalBlankLines(int limit, int lines); + int runRasterLine(int limit); + virtual int execute(uint8_t opcode); int step(); diff --git a/LR35902/src/LR35902.cpp b/LR35902/src/LR35902.cpp index 0753ccb..b02d646 100644 --- a/LR35902/src/LR35902.cpp +++ b/LR35902/src/LR35902.cpp @@ -325,18 +325,23 @@ void EightBit::GameBoy::LR35902::ccf(uint8_t& a, uint8_t& f) { int EightBit::GameBoy::LR35902::runRasterLines() { m_bus.resetLY(); - return runRasterLines(Display::RasterHeight); + return runRasterLines(Display::RasterHeight * Bus::CyclesPerLine, Display::RasterHeight); } -int EightBit::GameBoy::LR35902::runRasterLines(int limit) { +int EightBit::GameBoy::LR35902::runRasterLines(int limit, int lines) { int count = 0; - for (int line = 0; line < limit; ++line) - count += runRasterLine(); + int executed = 0; + int allowed = Bus::CyclesPerLine; + for (int line = 0; line < lines; ++line) { + auto executed = runRasterLine(allowed); + count += executed; + allowed = Bus::CyclesPerLine - (executed - Bus::CyclesPerLine); + } return count; } -int EightBit::GameBoy::LR35902::runRasterLine() { - const auto count = run(Bus::CyclesPerLine); +int EightBit::GameBoy::LR35902::runRasterLine(int limit) { + const auto count = run(limit); if (m_bus.peekRegister(Bus::LCDC) & Bus::LcdEnable) { m_bus.updateLcdStatusMode(Bus::LcdStatusMode::HBlank); m_bus.incrementLY(); @@ -347,11 +352,16 @@ int EightBit::GameBoy::LR35902::runRasterLine() { } int EightBit::GameBoy::LR35902::runVerticalBlankLines() { + auto lines = Bus::TotalLineCount - Display::RasterHeight; + return runVerticalBlankLines(lines * Bus::CyclesPerLine, lines); +} + +int EightBit::GameBoy::LR35902::runVerticalBlankLines(int limit, int lines) { if (m_bus.peekRegister(Bus::LCDC) & Bus::LcdEnable) { m_bus.updateLcdStatusMode(Bus::LcdStatusMode::VBlank); m_bus.triggerInterrupt(Bus::Interrupts::VerticalBlank); } - return runRasterLines(Bus::TotalLineCount - Display::RasterHeight); + return runRasterLines(limit, lines); } int EightBit::GameBoy::LR35902::singleStep() {