diff --git a/src/arm/common.h b/src/arm/common.h index 552d98366..ee13c37d3 100644 --- a/src/arm/common.h +++ b/src/arm/common.h @@ -64,6 +64,8 @@ #define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START)) #define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END)) #define EXT_BITS(SRC, START, END) (((SRC) >> (START)) & ((1 << ((END) - (START))) - 1)) +#define CLEAR_BITS(SRC, START, END) ((SRC) & ~MAKE_MASK(START, END)) +#define FILL_BITS(SRC, START, END) ((SRC) | MAKE_MASK(START, END)) #define DECL_BITFIELD(NAME, TYPE) typedef TYPE NAME @@ -73,6 +75,12 @@ } \ static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \ return EXT_BITS(src, (START), (START) + (SIZE)); \ + } \ + static inline TYPE TYPE ## Clear ## FIELD (TYPE src) { \ + return CLEAR_BITS(src, (START), (START) + (SIZE)); \ + } \ + static inline TYPE TYPE ## Fill ## FIELD (TYPE src) { \ + return FILL_BITS(src, (START), (START) + (SIZE)); \ } #define DECL_BIT(TYPE, FIELD, BIT) DECL_BITS(TYPE, FIELD, BIT, 1) diff --git a/src/gba/gba-video.c b/src/gba/gba-video.c index fdd343c8c..9d97ebaa9 100644 --- a/src/gba/gba-video.c +++ b/src/gba/gba-video.c @@ -36,14 +36,7 @@ void GBAVideoInit(struct GBAVideo* video) { } void GBAVideoReset(struct GBAVideo* video) { - video->inHblank = 0; - video->inVblank = 0; - video->vcounter = 0; - video->vblankIRQ = 0; - video->hblankIRQ = 0; - video->vcounterIRQ = 0; - video->vcountSetting = 0; - + video->dispstat = 0; video->vcount = 0; video->lastHblank = 0; @@ -94,9 +87,9 @@ int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles) { video->nextHblankIRQ -= video->eventDiff; video->nextVcounterIRQ -= video->eventDiff; - if (video->inHblank) { + if (GBARegisterDISPSTATIsInHblank(video->dispstat)) { // End Hblank - video->inHblank = 0; + video->dispstat = GBARegisterDISPSTATClearInHblank(video->dispstat); video->nextEvent = video->nextHblank; ++video->vcount; @@ -104,13 +97,13 @@ int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles) { switch (video->vcount) { case VIDEO_VERTICAL_PIXELS: - video->inVblank = 1; + video->dispstat = GBARegisterDISPSTATFillInVblank(video->dispstat); if (GBASyncDrawingFrame(video->p->sync)) { video->renderer->finishFrame(video->renderer); } video->nextVblankIRQ = video->nextEvent + VIDEO_TOTAL_LENGTH; GBAMemoryRunVblankDMAs(video->p, lastEvent); - if (video->vblankIRQ) { + if (GBARegisterDISPSTATIsVblankIRQ(video->dispstat)) { GBARaiseIRQ(video->p, IRQ_VBLANK); } GBASyncPostFrame(video->p->sync); @@ -119,7 +112,7 @@ int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles) { if (video->p->rr) { GBARRNextFrame(video->p->rr); } - video->inVblank = 0; + video->dispstat = GBARegisterDISPSTATClearInVblank(video->dispstat); break; case VIDEO_VERTICAL_TOTAL_PIXELS: video->vcount = 0; @@ -127,14 +120,18 @@ int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles) { break; } - video->vcounter = video->vcount == video->vcountSetting; - if (video->vcounter && video->vcounterIRQ) { - GBARaiseIRQ(video->p, IRQ_VCOUNTER); - video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH; + if (video->vcount == GBARegisterDISPSTATGetVcountSetting(video->dispstat)) { + video->dispstat = GBARegisterDISPSTATFillVcounter(video->dispstat); + if (GBARegisterDISPSTATIsVcounterIRQ(video->dispstat)) { + GBARaiseIRQ(video->p, IRQ_VCOUNTER); + video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH; + } + } else { + video->dispstat = GBARegisterDISPSTATClearVcounter(video->dispstat); } } else { // Begin Hblank - video->inHblank = 1; + video->dispstat = GBARegisterDISPSTATFillInHblank(video->dispstat); video->lastHblank = video->nextHblank; video->nextEvent = video->lastHblank + VIDEO_HBLANK_LENGTH; video->nextHblank = video->nextEvent + VIDEO_HDRAW_LENGTH; @@ -147,7 +144,7 @@ int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles) { if (video->vcount < VIDEO_VERTICAL_PIXELS) { GBAMemoryRunHblankDMAs(video->p, lastEvent); } - if (video->hblankIRQ) { + if (GBARegisterDISPSTATIsHblankIRQ(video->dispstat)) { GBARaiseIRQ(video->p, IRQ_HBLANK); } } @@ -155,21 +152,16 @@ int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles) { video->eventDiff = 0; } video->p->memory.io[REG_DISPSTAT >> 1] &= 0xFFF8; - video->p->memory.io[REG_DISPSTAT >> 1] |= (video->inVblank) | (video->inHblank << 1) | (video->vcounter << 2); + video->p->memory.io[REG_DISPSTAT >> 1] |= video->dispstat & 0x7; return video->nextEvent; } void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value) { - union GBARegisterDISPSTAT dispstat; - dispstat.packed = value; - video->vblankIRQ = dispstat.vblankIRQ; - video->hblankIRQ = dispstat.hblankIRQ; - video->vcounterIRQ = dispstat.vcounterIRQ; - video->vcountSetting = dispstat.vcountSetting; + video->dispstat = value; - if (video->vcounterIRQ) { + if (GBARegisterDISPSTATIsVcounterIRQ(video->dispstat)) { // 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; + video->nextVcounterIRQ = video->nextHblank + VIDEO_HBLANK_LENGTH + (GBARegisterDISPSTATGetVcountSetting(video->dispstat) - video->vcount) * VIDEO_HORIZONTAL_LENGTH; if (video->nextVcounterIRQ < video->nextEvent) { video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH; } @@ -233,15 +225,7 @@ void GBAVideoSerialize(struct GBAVideo* video, struct GBASerializedState* state) memcpy(state->vram, video->renderer->vram, SIZE_VRAM); memcpy(state->oam, video->oam.raw, SIZE_OAM); memcpy(state->pram, video->palette, SIZE_PALETTE_RAM); - union GBARegisterDISPSTAT dispstat; - dispstat.inVblank = video->inVblank; - dispstat.inHblank = video->inHblank; - dispstat.vcounter = video->vcounter; - dispstat.vblankIRQ = video->vblankIRQ; - dispstat.hblankIRQ = video->hblankIRQ; - dispstat.vcounterIRQ = video->vcounterIRQ; - dispstat.vcountSetting = video->vcountSetting; - state->io[REG_DISPSTAT >> 1] = dispstat.packed; + state->io[REG_DISPSTAT >> 1] = video->dispstat; state->video.nextEvent = video->nextEvent; state->video.eventDiff = video->eventDiff; state->video.lastHblank = video->lastHblank; @@ -260,15 +244,7 @@ void GBAVideoDeserialize(struct GBAVideo* video, struct GBASerializedState* stat for (i = 0; i < SIZE_PALETTE_RAM; i += 2) { GBAStore16(video->p->cpu, BASE_PALETTE_RAM | i, state->pram[i >> 1], 0); } - union GBARegisterDISPSTAT dispstat; - dispstat.packed = state->io[REG_DISPSTAT >> 1]; - video->inVblank = dispstat.inVblank; - video->inHblank = dispstat.inHblank; - video->vcounter = dispstat.vcounter; - video->vblankIRQ = dispstat.vblankIRQ; - video->hblankIRQ = dispstat.hblankIRQ; - video->vcounterIRQ = dispstat.vcounterIRQ; - video->vcountSetting = dispstat.vcountSetting; + video->dispstat = state->io[REG_DISPSTAT >> 1]; video->nextEvent = state->video.nextEvent; video->eventDiff = state->video.eventDiff; video->lastHblank = state->video.lastHblank; diff --git a/src/gba/gba-video.h b/src/gba/gba-video.h index e90a38831..f2d8e5287 100644 --- a/src/gba/gba-video.h +++ b/src/gba/gba-video.h @@ -132,19 +132,14 @@ DECL_BIT(GBARegisterDISPCNT, Win0Enable, 13); DECL_BIT(GBARegisterDISPCNT, Win1Enable, 14); DECL_BIT(GBARegisterDISPCNT, ObjwinEnable, 15); -union GBARegisterDISPSTAT { - struct { - unsigned inVblank : 1; - unsigned inHblank : 1; - unsigned vcounter : 1; - unsigned vblankIRQ : 1; - unsigned hblankIRQ : 1; - unsigned vcounterIRQ : 1; - unsigned : 2; - unsigned vcountSetting : 8; - }; - uint32_t packed; -}; +DECL_BITFIELD(GBARegisterDISPSTAT, uint16_t); +DECL_BIT(GBARegisterDISPSTAT, InVblank, 0); +DECL_BIT(GBARegisterDISPSTAT, InHblank, 1); +DECL_BIT(GBARegisterDISPSTAT, Vcounter, 2); +DECL_BIT(GBARegisterDISPSTAT, VblankIRQ, 3); +DECL_BIT(GBARegisterDISPSTAT, HblankIRQ, 4); +DECL_BIT(GBARegisterDISPSTAT, VcounterIRQ, 5); +DECL_BITS(GBARegisterDISPSTAT, VcountSetting, 8, 8); union GBARegisterBGCNT { struct { @@ -183,14 +178,7 @@ struct GBAVideo { struct GBA* p; struct GBAVideoRenderer* renderer; - // DISPSTAT - int inHblank; - int inVblank; - int vcounter; - int vblankIRQ; - int hblankIRQ; - int vcounterIRQ; - int vcountSetting; + GBARegisterDISPSTAT dispstat; // VCOUNT int vcount;