From c89c3964dbde285b318d80cb82b642dcc0eefb21 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Sun, 14 Feb 2016 01:57:53 -0800 Subject: [PATCH] LR35902: Optimize CPU loop to prevent no-op cycles from being calculated --- src/lr35902/lr35902.c | 9 ++++++++- src/lr35902/lr35902.h | 18 +++++++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/lr35902/lr35902.c b/src/lr35902/lr35902.c index f40ab6462..ff92587fb 100644 --- a/src/lr35902/lr35902.c +++ b/src/lr35902/lr35902.c @@ -148,8 +148,15 @@ void LR35902Tick(struct LR35902Core* cpu) { } void LR35902Run(struct LR35902Core* cpu) { - while (cpu->cycles < cpu->nextEvent) { + while (true) { _LR35902Step(cpu); + if (cpu->cycles >= cpu->nextEvent) { + break; + } else if (cpu->executionState < LR35902_CORE_EXECUTE) { + // Silly hack: keep us from calling step if we know the next step is a no-op + ++cpu->cycles; + ++cpu->executionState; + } } cpu->irqh.processEvents(cpu); } diff --git a/src/lr35902/lr35902.h b/src/lr35902/lr35902.h index 240195c9d..93c8c8613 100644 --- a/src/lr35902/lr35902.h +++ b/src/lr35902/lr35902.h @@ -36,16 +36,16 @@ union FlagRegister { #pragma pack(pop) enum LR35902ExecutionState { - LR35902_CORE_FETCH = 0, - LR35902_CORE_IDLE_0, - LR35902_CORE_IDLE_1, - LR35902_CORE_EXECUTE = 3, + LR35902_CORE_FETCH = 3, + LR35902_CORE_IDLE_0 = 0, + LR35902_CORE_IDLE_1 = 1, + LR35902_CORE_EXECUTE = 2, - LR35902_CORE_MEMORY_LOAD = 4, - LR35902_CORE_MEMORY_STORE = 8, - LR35902_CORE_READ_PC = 12, - LR35902_CORE_STALL = 16, - LR35902_CORE_OP2 = 20 + LR35902_CORE_MEMORY_LOAD = 7, + LR35902_CORE_MEMORY_STORE = 11, + LR35902_CORE_READ_PC = 15, + LR35902_CORE_STALL = 19, + LR35902_CORE_OP2 = 23 }; struct LR35902Memory {