From 6243b7fd3bbbf4a5569ff4d4739fbfbbf2929429 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Sat, 17 Sep 2016 19:04:36 -0700 Subject: [PATCH] GB: Convert SIO events to mTiming --- src/gb/gb.c | 5 ----- src/gb/sio.c | 36 ++++++++++++++++++------------------ src/gb/sio.h | 5 ++++- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/gb/gb.c b/src/gb/gb.c index 7b374a92a..687d74101 100644 --- a/src/gb/gb.c +++ b/src/gb/gb.c @@ -565,11 +565,6 @@ void GBProcessEvents(struct LR35902Core* cpu) { } } - testEvent = GBSIOProcessEvents(&gb->sio, cycles); - if (testEvent < nextEvent) { - nextEvent = testEvent; - } - cpu->cycles -= cycles; cpu->nextEvent = nextEvent; diff --git a/src/gb/sio.c b/src/gb/sio.c index 035b3a42e..cd9642861 100644 --- a/src/gb/sio.c +++ b/src/gb/sio.c @@ -9,8 +9,13 @@ #include "gb/io.h" #include "gb/serialize.h" +void _GBSIOProcessEvents(struct mTiming* timing, void* context, uint32_t cyclesLate); + void GBSIOInit(struct GBSIO* sio) { sio->pendingSB = 0xFF; + sio->event.context = sio; + sio->event.name = "GB SIO"; + sio->event.callback = _GBSIOProcessEvents; } void GBSIOReset(struct GBSIO* sio) { @@ -23,31 +28,26 @@ void GBSIODeinit(struct GBSIO* sio) { // Nothing to do yet } -int32_t GBSIOProcessEvents(struct GBSIO* sio, int32_t cycles) { - if (sio->nextEvent != INT_MAX) { - sio->nextEvent -= cycles; +void _GBSIOProcessEvents(struct mTiming* timing, void* context, uint32_t cyclesLate) { + UNUSED(cyclesLate); + struct GBSIO* sio = context; + --sio->remainingBits; + sio->p->memory.io[REG_SB] &= ~(8 >> sio->remainingBits); + sio->p->memory.io[REG_SB] |= sio->pendingSB & ~(8 >> sio->remainingBits); + if (!sio->remainingBits) { + sio->p->memory.io[REG_IF] |= (1 << GB_IRQ_SIO); + sio->p->memory.io[REG_SC] = GBRegisterSCClearEnable(sio->p->memory.io[REG_SC]); + GBUpdateIRQs(sio->p); + } else { + mTimingSchedule(timing, &sio->event, sio->period); } - if (sio->nextEvent <= 0) { - --sio->remainingBits; - sio->p->memory.io[REG_SB] &= ~(8 >> sio->remainingBits); - sio->p->memory.io[REG_SB] |= sio->pendingSB & ~(8 >> sio->remainingBits); - if (!sio->remainingBits) { - sio->p->memory.io[REG_IF] |= (1 << GB_IRQ_SIO); - sio->p->memory.io[REG_SC] = GBRegisterSCClearEnable(sio->p->memory.io[REG_SC]); - GBUpdateIRQs(sio->p); - sio->nextEvent = INT_MAX; - } else { - sio->nextEvent += sio->period; - } - } - return sio->nextEvent; } void GBSIOWriteSC(struct GBSIO* sio, uint8_t sc) { sio->period = 0x1000; // TODO Shift Clock if (GBRegisterSCIsEnable(sc)) { if (GBRegisterSCIsShiftClock(sc)) { - sio->nextEvent = sio->p->cpu->cycles + sio->period; + mTimingSchedule(&sio->p->timing, &sio->event, sio->period); } sio->remainingBits = 8; } diff --git a/src/gb/sio.h b/src/gb/sio.h index 8ff10265b..715b5b9c8 100644 --- a/src/gb/sio.h +++ b/src/gb/sio.h @@ -8,10 +8,14 @@ #include "util/common.h" +#include "core/timing.h" + struct GB; struct GBSIO { struct GB* p; + struct mTimingEvent event; + int32_t nextEvent; int32_t period; int remainingBits; @@ -27,7 +31,6 @@ DECL_BIT(GBRegisterSC, Enable, 7); void GBSIOInit(struct GBSIO* sio); void GBSIOReset(struct GBSIO* sio); void GBSIODeinit(struct GBSIO* sio); -int32_t GBSIOProcessEvents(struct GBSIO* sio, int32_t cycles); void GBSIOWriteSC(struct GBSIO* sio, uint8_t sc); #endif