From e423cd45e5805f0d45fceb032eee8bee6d00f1c6 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Fri, 23 Sep 2016 22:26:01 -0700 Subject: [PATCH] Core: Scheduling should be relative to current cycles --- src/core/timing.c | 5 +++-- src/core/timing.h | 3 ++- src/gb/gb.c | 13 ++++++------- src/gb/memory.c | 2 +- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/core/timing.c b/src/core/timing.c index 2498ff30b..9ee376fb0 100644 --- a/src/core/timing.c +++ b/src/core/timing.c @@ -7,9 +7,10 @@ DEFINE_VECTOR(mTimingEventList, struct mTimingEvent*); -void mTimingInit(struct mTiming* timing) { +void mTimingInit(struct mTiming* timing, int32_t* relativeCycles) { mTimingEventListInit(&timing->events, 0); timing->masterCycles = 0; + timing->relativeCycles = relativeCycles; } void mTimingDeinit(struct mTiming* timing) { @@ -22,7 +23,7 @@ void mTimingClear(struct mTiming* timing) { } void mTimingSchedule(struct mTiming* timing, struct mTimingEvent* event, int32_t when) { - event->when = when + timing->masterCycles; + event->when = when + timing->masterCycles + *timing->relativeCycles; size_t e; for (e = 0; e < mTimingEventListSize(&timing->events); ++e) { struct mTimingEvent* next = *mTimingEventListGetPointer(&timing->events, e); diff --git a/src/core/timing.h b/src/core/timing.h index 7a085e750..55934c6b4 100644 --- a/src/core/timing.h +++ b/src/core/timing.h @@ -23,9 +23,10 @@ struct mTiming { struct mTimingEventList events; uint32_t masterCycles; + int32_t* relativeCycles; }; -void mTimingInit(struct mTiming* timing); +void mTimingInit(struct mTiming* timing, int32_t* relativeCycles); void mTimingDeinit(struct mTiming* timing); void mTimingClear(struct mTiming* timing); void mTimingSchedule(struct mTiming* timing, struct mTimingEvent*, int32_t when); diff --git a/src/gb/gb.c b/src/gb/gb.c index 38f281419..d304f3449 100644 --- a/src/gb/gb.c +++ b/src/gb/gb.c @@ -83,7 +83,7 @@ static void GBInit(void* cpu, struct mCPUComponent* component) { gb->coreCallbacks = NULL; gb->stream = NULL; - mTimingInit(&gb->timing); + mTimingInit(&gb->timing, &gb->cpu->cycles); } static void GBDeinit(struct mCPUComponent* component) { @@ -529,9 +529,12 @@ void GBProcessEvents(struct LR35902Core* cpu) { struct GB* gb = (struct GB*) cpu->master; do { int32_t cycles = cpu->nextEvent; - int32_t nextEvent = INT_MAX; + int32_t nextEvent; int32_t testEvent; + cpu->cycles -= cycles; + cpu->nextEvent = INT_MAX; + if (gb->eiPending != INT_MAX) { gb->eiPending -= cycles; if (gb->eiPending <= 0) { @@ -544,10 +547,7 @@ void GBProcessEvents(struct LR35902Core* cpu) { } mTimingTick(&gb->timing, cycles); - testEvent = mTimingNextEvent(&gb->timing); - if (testEvent < nextEvent) { - nextEvent = testEvent; - } + nextEvent = cpu->nextEvent; testEvent = GBAudioProcessEvents(&gb->audio, cycles >> gb->doubleSpeed); if (testEvent != INT_MAX) { @@ -557,7 +557,6 @@ void GBProcessEvents(struct LR35902Core* cpu) { } } - cpu->cycles -= cycles; cpu->nextEvent = nextEvent; if (cpu->halted) { diff --git a/src/gb/memory.c b/src/gb/memory.c index f6cb3eb60..ebcd107b5 100644 --- a/src/gb/memory.c +++ b/src/gb/memory.c @@ -409,7 +409,6 @@ void _GBMemoryHDMAService(struct mTiming* timing, void* context, uint32_t cycles ++gb->memory.hdmaSource; ++gb->memory.hdmaDest; --gb->memory.hdmaRemaining; - gb->cpu->cycles += 2; if (gb->memory.hdmaRemaining) { mTimingSchedule(timing, &gb->memory.hdmaEvent, 2 - cyclesLate); } else { @@ -426,6 +425,7 @@ void _GBMemoryHDMAService(struct mTiming* timing, void* context, uint32_t cycles gb->memory.io[REG_HDMA5] = 0xFF; } } + gb->cpu->cycles += 2; } struct OAMBlock {