Core: Manage next event internally to mTiming

This commit is contained in:
Jeffrey Pfau 2016-09-24 11:04:20 -07:00
parent a506f6cd9d
commit 4680a767c6
4 changed files with 16 additions and 10 deletions

View File

@ -7,10 +7,11 @@
DEFINE_VECTOR(mTimingEventList, struct mTimingEvent*); DEFINE_VECTOR(mTimingEventList, struct mTimingEvent*);
void mTimingInit(struct mTiming* timing, int32_t* relativeCycles) { void mTimingInit(struct mTiming* timing, int32_t* relativeCycles, int32_t* nextEvent) {
mTimingEventListInit(&timing->events, 0); mTimingEventListInit(&timing->events, 0);
timing->masterCycles = 0; timing->masterCycles = 0;
timing->relativeCycles = relativeCycles; timing->relativeCycles = relativeCycles;
timing->nextEvent = nextEvent;
} }
void mTimingDeinit(struct mTiming* timing) { void mTimingDeinit(struct mTiming* timing) {
@ -23,7 +24,11 @@ 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 + *timing->relativeCycles; int32_t nextEvent = when + *timing->relativeCycles;
event->when = nextEvent + timing->masterCycles;
if (nextEvent < *timing->nextEvent) {
*timing->nextEvent = nextEvent;
}
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);
@ -54,6 +59,7 @@ void mTimingTick(struct mTiming* timing, int32_t cycles) {
struct mTimingEvent* next = *mTimingEventListGetPointer(&timing->events, 0); struct mTimingEvent* next = *mTimingEventListGetPointer(&timing->events, 0);
int32_t nextWhen = next->when - timing->masterCycles; int32_t nextWhen = next->when - timing->masterCycles;
if (nextWhen > 0) { if (nextWhen > 0) {
*timing->nextEvent = nextWhen;
return; return;
} }
mTimingEventListShift(&timing->events, 0, 1); mTimingEventListShift(&timing->events, 0, 1);

View File

@ -24,9 +24,10 @@ struct mTiming {
uint32_t masterCycles; uint32_t masterCycles;
int32_t* relativeCycles; int32_t* relativeCycles;
int32_t* nextEvent;
}; };
void mTimingInit(struct mTiming* timing, int32_t* relativeCycles); void mTimingInit(struct mTiming* timing, int32_t* relativeCycles, int32_t* nextEvent);
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, &gb->cpu->cycles); mTimingInit(&gb->timing, &gb->cpu->cycles, &gb->cpu->nextEvent);
} }
static void GBDeinit(struct mCPUComponent* component) { static void GBDeinit(struct mCPUComponent* component) {

View File

@ -112,7 +112,7 @@ static void GBAInit(void* cpu, struct mCPUComponent* component) {
gba->pristineRomSize = 0; gba->pristineRomSize = 0;
gba->yankedRomSize = 0; gba->yankedRomSize = 0;
mTimingInit(&gba->timing, &gba->cpu->cycles); mTimingInit(&gba->timing, &gba->cpu->cycles, &gba->cpu->nextEvent);
} }
void GBAUnloadROM(struct GBA* gba) { void GBAUnloadROM(struct GBA* gba) {
@ -188,6 +188,7 @@ void GBAReset(struct ARMCore* cpu) {
gba->memory.romMask = toPow2(gba->memory.romSize) - 1; gba->memory.romMask = toPow2(gba->memory.romSize) - 1;
gba->yankedRomSize = 0; gba->yankedRomSize = 0;
} }
mTimingClear(&gba->timing);
GBAMemoryReset(gba); GBAMemoryReset(gba);
GBAVideoReset(&gba->video); GBAVideoReset(&gba->video);
GBAAudioReset(&gba->audio); GBAAudioReset(&gba->audio);
@ -235,10 +236,11 @@ static void GBAProcessEvents(struct ARMCore* cpu) {
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->cycles -= cycles;
cpu->nextEvent = INT_MAX;
#ifndef NDEBUG #ifndef NDEBUG
if (cycles < 0) { if (cycles < 0) {
@ -247,10 +249,7 @@ static void GBAProcessEvents(struct ARMCore* cpu) {
#endif #endif
mTimingTick(&gba->timing, cycles); mTimingTick(&gba->timing, cycles);
testEvent = mTimingNextEvent(&gba->timing); nextEvent = cpu->nextEvent;
if (testEvent < nextEvent) {
nextEvent = testEvent;
}
testEvent = GBAVideoProcessEvents(&gba->video, cycles); testEvent = GBAVideoProcessEvents(&gba->video, cycles);
if (testEvent < nextEvent) { if (testEvent < nextEvent) {