Core: Scheduling should be relative to current cycles

This commit is contained in:
Jeffrey Pfau 2016-09-23 22:26:01 -07:00
parent bf9be29ad5
commit e423cd45e5
4 changed files with 12 additions and 11 deletions

View File

@ -7,9 +7,10 @@
DEFINE_VECTOR(mTimingEventList, struct mTimingEvent*); DEFINE_VECTOR(mTimingEventList, struct mTimingEvent*);
void mTimingInit(struct mTiming* timing) { void mTimingInit(struct mTiming* timing, int32_t* relativeCycles) {
mTimingEventListInit(&timing->events, 0); mTimingEventListInit(&timing->events, 0);
timing->masterCycles = 0; timing->masterCycles = 0;
timing->relativeCycles = relativeCycles;
} }
void mTimingDeinit(struct mTiming* timing) { 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) { 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; size_t e;
for (e = 0; e < mTimingEventListSize(&timing->events); ++e) { for (e = 0; e < mTimingEventListSize(&timing->events); ++e) {
struct mTimingEvent* next = *mTimingEventListGetPointer(&timing->events, e); struct mTimingEvent* next = *mTimingEventListGetPointer(&timing->events, e);

View File

@ -23,9 +23,10 @@ struct mTiming {
struct mTimingEventList events; struct mTimingEventList events;
uint32_t masterCycles; 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 mTimingDeinit(struct mTiming* timing);
void mTimingClear(struct mTiming* timing); void mTimingClear(struct mTiming* timing);
void mTimingSchedule(struct mTiming* timing, struct mTimingEvent*, int32_t when); void mTimingSchedule(struct mTiming* timing, struct mTimingEvent*, int32_t when);

View File

@ -83,7 +83,7 @@ static void GBInit(void* cpu, struct mCPUComponent* component) {
gb->coreCallbacks = NULL; gb->coreCallbacks = NULL;
gb->stream = NULL; gb->stream = NULL;
mTimingInit(&gb->timing); mTimingInit(&gb->timing, &gb->cpu->cycles);
} }
static void GBDeinit(struct mCPUComponent* component) { static void GBDeinit(struct mCPUComponent* component) {
@ -529,9 +529,12 @@ void GBProcessEvents(struct LR35902Core* cpu) {
struct GB* gb = (struct GB*) cpu->master; struct GB* gb = (struct GB*) cpu->master;
do { do {
int32_t cycles = cpu->nextEvent; int32_t cycles = cpu->nextEvent;
int32_t nextEvent = INT_MAX; int32_t nextEvent;
int32_t testEvent; int32_t testEvent;
cpu->cycles -= cycles;
cpu->nextEvent = INT_MAX;
if (gb->eiPending != INT_MAX) { if (gb->eiPending != INT_MAX) {
gb->eiPending -= cycles; gb->eiPending -= cycles;
if (gb->eiPending <= 0) { if (gb->eiPending <= 0) {
@ -544,10 +547,7 @@ void GBProcessEvents(struct LR35902Core* cpu) {
} }
mTimingTick(&gb->timing, cycles); mTimingTick(&gb->timing, cycles);
testEvent = mTimingNextEvent(&gb->timing); nextEvent = cpu->nextEvent;
if (testEvent < nextEvent) {
nextEvent = testEvent;
}
testEvent = GBAudioProcessEvents(&gb->audio, cycles >> gb->doubleSpeed); testEvent = GBAudioProcessEvents(&gb->audio, cycles >> gb->doubleSpeed);
if (testEvent != INT_MAX) { if (testEvent != INT_MAX) {
@ -557,7 +557,6 @@ void GBProcessEvents(struct LR35902Core* cpu) {
} }
} }
cpu->cycles -= cycles;
cpu->nextEvent = nextEvent; cpu->nextEvent = nextEvent;
if (cpu->halted) { if (cpu->halted) {

View File

@ -409,7 +409,6 @@ void _GBMemoryHDMAService(struct mTiming* timing, void* context, uint32_t cycles
++gb->memory.hdmaSource; ++gb->memory.hdmaSource;
++gb->memory.hdmaDest; ++gb->memory.hdmaDest;
--gb->memory.hdmaRemaining; --gb->memory.hdmaRemaining;
gb->cpu->cycles += 2;
if (gb->memory.hdmaRemaining) { if (gb->memory.hdmaRemaining) {
mTimingSchedule(timing, &gb->memory.hdmaEvent, 2 - cyclesLate); mTimingSchedule(timing, &gb->memory.hdmaEvent, 2 - cyclesLate);
} else { } else {
@ -426,6 +425,7 @@ void _GBMemoryHDMAService(struct mTiming* timing, void* context, uint32_t cycles
gb->memory.io[REG_HDMA5] = 0xFF; gb->memory.io[REG_HDMA5] = 0xFF;
} }
} }
gb->cpu->cycles += 2;
} }
struct OAMBlock { struct OAMBlock {