Core: Scheduling should be relative to current cycles
Jeffrey Pfau jeffrey@endrift.com
Fri, 23 Sep 2016 22:26:01 -0700
4 files changed,
12 insertions(+),
11 deletions(-)
M
src/core/timing.c
→
src/core/timing.c
@@ -7,9 +7,10 @@ #include "timing.h"
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 @@ timing->masterCycles = 0;
} 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);
M
src/core/timing.h
→
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);
M
src/gb/gb.c
→
src/gb/gb.c
@@ -83,7 +83,7 @@
gb->coreCallbacks = NULL; gb->stream = NULL; - mTimingInit(&gb->timing); + mTimingInit(&gb->timing, &gb->cpu->cycles); } static void GBDeinit(struct mCPUComponent* component) {@@ -529,8 +529,11 @@ 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;@@ -544,10 +547,7 @@ }
} 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 @@ nextEvent = testEvent;
} } - cpu->cycles -= cycles; cpu->nextEvent = nextEvent; if (cpu->halted) {
M
src/gb/memory.c
→
src/gb/memory.c
@@ -409,7 +409,6 @@ gb->cpu->memory.store8(gb->cpu, gb->memory.hdmaDest, b);
++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 @@ } else {
gb->memory.io[REG_HDMA5] = 0xFF; } } + gb->cpu->cycles += 2; } struct OAMBlock {