Replace dispstat assorted variables with a bitfield

This commit is contained in:
Jeffrey Pfau 2014-09-30 23:29:30 -07:00
parent 015e6700d7
commit 7e4dc6c15c
3 changed files with 39 additions and 67 deletions

View File

@ -64,6 +64,8 @@
#define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START)) #define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START))
#define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END)) #define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END))
#define EXT_BITS(SRC, START, END) (((SRC) >> (START)) & ((1 << ((END) - (START))) - 1)) #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 #define DECL_BITFIELD(NAME, TYPE) typedef TYPE NAME
@ -73,6 +75,12 @@
} \ } \
static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \ static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \
return EXT_BITS(src, (START), (START) + (SIZE)); \ 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) #define DECL_BIT(TYPE, FIELD, BIT) DECL_BITS(TYPE, FIELD, BIT, 1)

View File

@ -36,14 +36,7 @@ void GBAVideoInit(struct GBAVideo* video) {
} }
void GBAVideoReset(struct GBAVideo* video) { void GBAVideoReset(struct GBAVideo* video) {
video->inHblank = 0; video->dispstat = 0;
video->inVblank = 0;
video->vcounter = 0;
video->vblankIRQ = 0;
video->hblankIRQ = 0;
video->vcounterIRQ = 0;
video->vcountSetting = 0;
video->vcount = 0; video->vcount = 0;
video->lastHblank = 0; video->lastHblank = 0;
@ -94,9 +87,9 @@ int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles) {
video->nextHblankIRQ -= video->eventDiff; video->nextHblankIRQ -= video->eventDiff;
video->nextVcounterIRQ -= video->eventDiff; video->nextVcounterIRQ -= video->eventDiff;
if (video->inHblank) { if (GBARegisterDISPSTATIsInHblank(video->dispstat)) {
// End Hblank // End Hblank
video->inHblank = 0; video->dispstat = GBARegisterDISPSTATClearInHblank(video->dispstat);
video->nextEvent = video->nextHblank; video->nextEvent = video->nextHblank;
++video->vcount; ++video->vcount;
@ -104,13 +97,13 @@ int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles) {
switch (video->vcount) { switch (video->vcount) {
case VIDEO_VERTICAL_PIXELS: case VIDEO_VERTICAL_PIXELS:
video->inVblank = 1; video->dispstat = GBARegisterDISPSTATFillInVblank(video->dispstat);
if (GBASyncDrawingFrame(video->p->sync)) { if (GBASyncDrawingFrame(video->p->sync)) {
video->renderer->finishFrame(video->renderer); video->renderer->finishFrame(video->renderer);
} }
video->nextVblankIRQ = video->nextEvent + VIDEO_TOTAL_LENGTH; video->nextVblankIRQ = video->nextEvent + VIDEO_TOTAL_LENGTH;
GBAMemoryRunVblankDMAs(video->p, lastEvent); GBAMemoryRunVblankDMAs(video->p, lastEvent);
if (video->vblankIRQ) { if (GBARegisterDISPSTATIsVblankIRQ(video->dispstat)) {
GBARaiseIRQ(video->p, IRQ_VBLANK); GBARaiseIRQ(video->p, IRQ_VBLANK);
} }
GBASyncPostFrame(video->p->sync); GBASyncPostFrame(video->p->sync);
@ -119,7 +112,7 @@ int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles) {
if (video->p->rr) { if (video->p->rr) {
GBARRNextFrame(video->p->rr); GBARRNextFrame(video->p->rr);
} }
video->inVblank = 0; video->dispstat = GBARegisterDISPSTATClearInVblank(video->dispstat);
break; break;
case VIDEO_VERTICAL_TOTAL_PIXELS: case VIDEO_VERTICAL_TOTAL_PIXELS:
video->vcount = 0; video->vcount = 0;
@ -127,14 +120,18 @@ int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles) {
break; break;
} }
video->vcounter = video->vcount == video->vcountSetting; if (video->vcount == GBARegisterDISPSTATGetVcountSetting(video->dispstat)) {
if (video->vcounter && video->vcounterIRQ) { video->dispstat = GBARegisterDISPSTATFillVcounter(video->dispstat);
GBARaiseIRQ(video->p, IRQ_VCOUNTER); if (GBARegisterDISPSTATIsVcounterIRQ(video->dispstat)) {
video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH; GBARaiseIRQ(video->p, IRQ_VCOUNTER);
video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH;
}
} else {
video->dispstat = GBARegisterDISPSTATClearVcounter(video->dispstat);
} }
} else { } else {
// Begin Hblank // Begin Hblank
video->inHblank = 1; video->dispstat = GBARegisterDISPSTATFillInHblank(video->dispstat);
video->lastHblank = video->nextHblank; video->lastHblank = video->nextHblank;
video->nextEvent = video->lastHblank + VIDEO_HBLANK_LENGTH; video->nextEvent = video->lastHblank + VIDEO_HBLANK_LENGTH;
video->nextHblank = video->nextEvent + VIDEO_HDRAW_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) { if (video->vcount < VIDEO_VERTICAL_PIXELS) {
GBAMemoryRunHblankDMAs(video->p, lastEvent); GBAMemoryRunHblankDMAs(video->p, lastEvent);
} }
if (video->hblankIRQ) { if (GBARegisterDISPSTATIsHblankIRQ(video->dispstat)) {
GBARaiseIRQ(video->p, IRQ_HBLANK); GBARaiseIRQ(video->p, IRQ_HBLANK);
} }
} }
@ -155,21 +152,16 @@ int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles) {
video->eventDiff = 0; video->eventDiff = 0;
} }
video->p->memory.io[REG_DISPSTAT >> 1] &= 0xFFF8; 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; return video->nextEvent;
} }
void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value) { void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value) {
union GBARegisterDISPSTAT dispstat; video->dispstat = value;
dispstat.packed = value;
video->vblankIRQ = dispstat.vblankIRQ;
video->hblankIRQ = dispstat.hblankIRQ;
video->vcounterIRQ = dispstat.vcounterIRQ;
video->vcountSetting = dispstat.vcountSetting;
if (video->vcounterIRQ) { if (GBARegisterDISPSTATIsVcounterIRQ(video->dispstat)) {
// FIXME: this can be too late if we're in the middle of an Hblank // 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) { if (video->nextVcounterIRQ < video->nextEvent) {
video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH; 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->vram, video->renderer->vram, SIZE_VRAM);
memcpy(state->oam, video->oam.raw, SIZE_OAM); memcpy(state->oam, video->oam.raw, SIZE_OAM);
memcpy(state->pram, video->palette, SIZE_PALETTE_RAM); memcpy(state->pram, video->palette, SIZE_PALETTE_RAM);
union GBARegisterDISPSTAT dispstat; state->io[REG_DISPSTAT >> 1] = video->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->video.nextEvent = video->nextEvent; state->video.nextEvent = video->nextEvent;
state->video.eventDiff = video->eventDiff; state->video.eventDiff = video->eventDiff;
state->video.lastHblank = video->lastHblank; 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) { for (i = 0; i < SIZE_PALETTE_RAM; i += 2) {
GBAStore16(video->p->cpu, BASE_PALETTE_RAM | i, state->pram[i >> 1], 0); GBAStore16(video->p->cpu, BASE_PALETTE_RAM | i, state->pram[i >> 1], 0);
} }
union GBARegisterDISPSTAT dispstat; video->dispstat = state->io[REG_DISPSTAT >> 1];
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->nextEvent = state->video.nextEvent; video->nextEvent = state->video.nextEvent;
video->eventDiff = state->video.eventDiff; video->eventDiff = state->video.eventDiff;
video->lastHblank = state->video.lastHblank; video->lastHblank = state->video.lastHblank;

View File

@ -132,19 +132,14 @@ DECL_BIT(GBARegisterDISPCNT, Win0Enable, 13);
DECL_BIT(GBARegisterDISPCNT, Win1Enable, 14); DECL_BIT(GBARegisterDISPCNT, Win1Enable, 14);
DECL_BIT(GBARegisterDISPCNT, ObjwinEnable, 15); DECL_BIT(GBARegisterDISPCNT, ObjwinEnable, 15);
union GBARegisterDISPSTAT { DECL_BITFIELD(GBARegisterDISPSTAT, uint16_t);
struct { DECL_BIT(GBARegisterDISPSTAT, InVblank, 0);
unsigned inVblank : 1; DECL_BIT(GBARegisterDISPSTAT, InHblank, 1);
unsigned inHblank : 1; DECL_BIT(GBARegisterDISPSTAT, Vcounter, 2);
unsigned vcounter : 1; DECL_BIT(GBARegisterDISPSTAT, VblankIRQ, 3);
unsigned vblankIRQ : 1; DECL_BIT(GBARegisterDISPSTAT, HblankIRQ, 4);
unsigned hblankIRQ : 1; DECL_BIT(GBARegisterDISPSTAT, VcounterIRQ, 5);
unsigned vcounterIRQ : 1; DECL_BITS(GBARegisterDISPSTAT, VcountSetting, 8, 8);
unsigned : 2;
unsigned vcountSetting : 8;
};
uint32_t packed;
};
union GBARegisterBGCNT { union GBARegisterBGCNT {
struct { struct {
@ -183,14 +178,7 @@ struct GBAVideo {
struct GBA* p; struct GBA* p;
struct GBAVideoRenderer* renderer; struct GBAVideoRenderer* renderer;
// DISPSTAT GBARegisterDISPSTAT dispstat;
int inHblank;
int inVblank;
int vcounter;
int vblankIRQ;
int hblankIRQ;
int vcounterIRQ;
int vcountSetting;
// VCOUNT // VCOUNT
int vcount; int vcount;