From 7de2c91efb1e99409f9ab5bec0ae64580c2581e6 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Tue, 16 Apr 2013 07:10:38 -0700 Subject: [PATCH] Copy DISPSTAT implementation from GBA.js --- src/gba/gba-io.c | 8 ++++++++ src/gba/gba-video.c | 18 ++++++++++++++++++ src/gba/gba-video.h | 3 +++ 3 files changed, 29 insertions(+) diff --git a/src/gba/gba-io.c b/src/gba/gba-io.c index 62b140f4b..9217403dd 100644 --- a/src/gba/gba-io.c +++ b/src/gba/gba-io.c @@ -1,7 +1,12 @@ #include "gba-io.h" +#include "gba-video.h" + void GBAIOWrite(struct GBA* gba, uint32_t address, uint16_t value) { switch (address) { + case REG_DISPSTAT: + GBAVideoWriteDISPSTAT(&gba->video, value); + break; case REG_WAITCNT: GBAAdjustWaitstates(&gba->memory, value); break; @@ -14,6 +19,9 @@ void GBAIOWrite(struct GBA* gba, uint32_t address, uint16_t value) { uint16_t GBAIORead(struct GBA* gba, uint32_t address) { switch (address) { + case REG_DISPSTAT: + return GBAVideoReadDISPSTAT(&gba->video); + break; case REG_WAITCNT: // Handled transparently by registers break; diff --git a/src/gba/gba-video.c b/src/gba/gba-video.c index 88bff1ae0..c94e81d16 100644 --- a/src/gba/gba-video.c +++ b/src/gba/gba-video.c @@ -104,6 +104,24 @@ int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles) { return video->nextEvent; } +void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value) { + video->vblankIRQ = value & 0x0008; + video->hblankIRQ = value & 0x0010; + video->vcounterIRQ = value & 0x0020; + video->vcountSetting = (value & 0xFF00) >> 8; + + if (video->vcounterIRQ) { + // FIXME: this can be too late if we're in the middle of an Hblank + video->nextVcounterIRQ = video->nextHblank + VIDEO_HBLANK_LENGTH + (video->vcountSetting - video->vcount) * VIDEO_HORIZONTAL_LENGTH; + if (video->nextVcounterIRQ < video->nextEvent) { + video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH; + } + } +} + +uint16_t GBAVideoReadDISPSTAT(struct GBAVideo* video) { + return (video->inVblank) | (video->inHblank << 1) | (video->vcounter << 2); +} static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer) { (void)(renderer); diff --git a/src/gba/gba-video.h b/src/gba/gba-video.h index 0c59141ff..d65c778e6 100644 --- a/src/gba/gba-video.h +++ b/src/gba/gba-video.h @@ -60,4 +60,7 @@ struct GBAVideo { void GBAVideoInit(struct GBAVideo* video); int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles); +void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value); +uint16_t GBAVideoReadDISPSTAT(struct GBAVideo* video); + #endif